import { expect, tap } from '@git.zone/tstest/tapbundle'; import { EInvoice } from '../../../ts/index.js'; tap.test('ENC-06: Namespace Declarations - should handle XML namespace declarations correctly', async () => { console.log('Testing XML namespace declaration handling...\n'); // Test 1: Default namespaces const testDefaultNamespaces = async () => { const einvoice = new EInvoice(); einvoice.id = 'NAMESPACE-DEFAULT-TEST'; einvoice.date = Date.now(); einvoice.currency = 'EUR'; einvoice.subject = 'Default namespace test'; einvoice.from = { type: 'company', name: 'Default Namespace Company', description: 'Testing default namespaces', address: { streetName: 'Test Street', houseNumber: '1', postalCode: '12345', city: 'Test City', country: 'DE' }, status: 'active', foundedDate: { year: 2020, month: 1, day: 1 }, registrationDetails: { vatId: 'DE123456789', registrationId: 'HRB 12345', registrationName: 'Registry' } }; einvoice.to = { type: 'company', name: 'Customer', description: 'Test customer', address: { streetName: 'Customer Street', houseNumber: '2', postalCode: '54321', city: 'Customer City', country: 'DE' }, status: 'active', foundedDate: { year: 2019, month: 1, day: 1 }, registrationDetails: { vatId: 'DE987654321', registrationId: 'HRB 54321', registrationName: 'Registry' } }; einvoice.items = [{ position: 1, name: 'Namespace Test Product', unitType: 'C62', unitQuantity: 1, unitNetPrice: 100, vatPercentage: 19 }]; const xmlString = await einvoice.toXmlString('ubl'); // Check if proper UBL namespaces are declared const hasUblNamespace = xmlString.includes('xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"'); const hasCacNamespace = xmlString.includes('xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"'); const hasCbcNamespace = xmlString.includes('xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"'); // Check if elements use proper prefixes const hasProperPrefixes = xmlString.includes('') && xmlString.includes('') && xmlString.includes(''); return { hasUblNamespace, hasCacNamespace, hasCbcNamespace, hasProperPrefixes, xmlString }; }; const defaultResult = await testDefaultNamespaces(); console.log('Test 1 - Default namespaces:'); console.log(` UBL namespace declared: ${defaultResult.hasUblNamespace ? 'Yes' : 'No'}`); console.log(` CAC namespace declared: ${defaultResult.hasCacNamespace ? 'Yes' : 'No'}`); console.log(` CBC namespace declared: ${defaultResult.hasCbcNamespace ? 'Yes' : 'No'}`); console.log(` Proper prefixes used: ${defaultResult.hasProperPrefixes ? 'Yes' : 'No'}`); // Test 2: Custom namespace handling const testCustomNamespaces = async () => { const customXml = ` CUSTOM-NS-TEST 2025-01-25 380 EUR Custom Namespace Company Test Street Test City 12345 DE Customer Customer Street Customer City 54321 DE 1 1 100.00 Test Item Custom Value `; try { const invoice = await EInvoice.fromXml(customXml); return { success: invoice.id === 'CUSTOM-NS-TEST', supplierName: invoice.from?.name || '', customerName: invoice.to?.name || '' }; } catch (error) { return { success: false, error: error.message }; } }; const customResult = await testCustomNamespaces(); console.log('\nTest 2 - Custom namespace handling:'); console.log(` Custom namespace XML parsed: ${customResult.success ? 'Yes' : 'No'}`); if (customResult.error) { console.log(` Error: ${customResult.error}`); } // Test 3: No namespace prefix handling const testNoNamespacePrefix = async () => { const noNsXml = ` NO-NS-PREFIX-TEST 2025-01-25 380 EUR No Prefix Company Test Street Test City 12345 DE Customer Customer Street Customer City 54321 DE 1 1 100.00 Test Item `; try { const invoice = await EInvoice.fromXml(noNsXml); return { success: invoice.id === 'NO-NS-PREFIX-TEST', supplierName: invoice.from?.name || '', customerName: invoice.to?.name || '' }; } catch (error) { return { success: false, error: error.message }; } }; const noNsResult = await testNoNamespacePrefix(); console.log('\nTest 3 - No namespace prefix handling:'); console.log(` No prefix XML parsed: ${noNsResult.success ? 'Yes' : 'No'}`); if (noNsResult.error) { console.log(` Error: ${noNsResult.error}`); } // Test 4: Namespace inheritance const testNamespaceInheritance = async () => { const inheritanceXml = ` INHERITANCE-TEST 2025-01-25 380 EUR Inheritance Company Test Street Test City 12345 DE Customer Customer Street Customer City 54321 DE 1 1 100.00 Test Item `; try { const invoice = await EInvoice.fromXml(inheritanceXml); // Test round-trip to see if namespaces are preserved const regeneratedXml = await invoice.toXmlString('ubl'); const reparsedInvoice = await EInvoice.fromXml(regeneratedXml); return { success: invoice.id === 'INHERITANCE-TEST', roundTripSuccess: reparsedInvoice.id === 'INHERITANCE-TEST', supplierName: invoice.from?.name || '', regeneratedXml }; } catch (error) { return { success: false, error: error.message }; } }; const inheritanceResult = await testNamespaceInheritance(); console.log('\nTest 4 - Namespace inheritance and round-trip:'); console.log(` Inheritance XML parsed: ${inheritanceResult.success ? 'Yes' : 'No'}`); console.log(` Round-trip successful: ${inheritanceResult.roundTripSuccess ? 'Yes' : 'No'}`); if (inheritanceResult.error) { console.log(` Error: ${inheritanceResult.error}`); } // Test 5: Mixed namespace scenarios const testMixedNamespaces = async () => { const mixedXml = ` MIXED-NS-TEST 2025-01-25 380 EUR Mixed Namespace Company Test Street Test City 12345 DE Customer Customer Street Customer City 54321 DE 1 1 100.00 Test Item `; try { const invoice = await EInvoice.fromXml(mixedXml); return { success: invoice.id === 'MIXED-NS-TEST', supplierName: invoice.from?.name || '' }; } catch (error) { return { success: false, error: error.message }; } }; const mixedResult = await testMixedNamespaces(); console.log('\nTest 5 - Mixed namespace scenarios:'); console.log(` Mixed namespace XML parsed: ${mixedResult.success ? 'Yes' : 'No'}`); if (mixedResult.error) { console.log(` Error: ${mixedResult.error}`); } // Summary console.log('\n=== XML Namespace Declarations Test Summary ==='); console.log(`Default namespaces: ${defaultResult.hasUblNamespace && defaultResult.hasCacNamespace && defaultResult.hasCbcNamespace ? 'Working' : 'Issues'}`); console.log(`Custom namespaces: ${customResult.success ? 'Working' : 'Issues'}`); console.log(`No prefix handling: ${noNsResult.success ? 'Working' : 'Issues'}`); console.log(`Namespace inheritance: ${inheritanceResult.success && inheritanceResult.roundTripSuccess ? 'Working' : 'Issues'}`); console.log(`Mixed namespaces: ${mixedResult.success ? 'Working' : 'Issues'}`); // Tests pass if basic namespace functionality works expect(defaultResult.hasUblNamespace).toEqual(true); expect(defaultResult.hasCacNamespace).toEqual(true); expect(defaultResult.hasCbcNamespace).toEqual(true); expect(defaultResult.hasProperPrefixes).toEqual(true); expect(customResult.success).toEqual(true); expect(inheritanceResult.success).toEqual(true); expect(inheritanceResult.roundTripSuccess).toEqual(true); console.log('\n✓ XML namespace declarations test completed'); }); tap.start();