einvoice/test/suite/einvoice_edge-cases/test.edge-04.unusual-charsets.ts

488 lines
16 KiB
TypeScript
Raw Normal View History

2025-05-28 14:46:32 +00:00
import { expect, tap } from '@git.zone/tstest/tapbundle';
2025-05-26 04:04:51 +00:00
import { EInvoice } from '../../../ts/index.js';
2025-05-27 15:26:22 +00:00
tap.test('EDGE-04: Unusual Character Sets - should handle unusual and exotic character encodings', async () => {
2025-05-28 14:46:32 +00:00
console.log('Testing unusual character sets in e-invoices...\n');
2025-05-27 15:26:22 +00:00
// Test 1: Unicode edge cases with real invoice data
2025-05-28 14:46:32 +00:00
const testUnicodeEdgeCases = async () => {
2025-05-27 15:26:22 +00:00
const testCases = [
{
name: 'zero-width-characters',
text: 'Invoice\u200B\u200C\u200D\uFEFFNumber',
description: 'Zero-width spaces and joiners'
},
{
name: 'right-to-left',
text: 'مرحبا INV-001 שלום',
description: 'RTL Arabic and Hebrew mixed with LTR'
},
{
name: 'surrogate-pairs',
text: '𝐇𝐞𝐥𝐥𝐨 😀 🎉 Invoice',
description: 'Mathematical bold text and emojis'
},
{
name: 'combining-characters',
text: 'Ińvȯíçë̃ Nüm̈bër̊',
description: 'Combining diacritical marks'
},
{
name: 'control-characters',
text: 'Invoice Test', // Remove actual control chars as they break XML
description: 'Control characters (removed for XML safety)'
},
{
name: 'bidi-override',
text: '\u202Eتسا Invoice 123\u202C',
description: 'Bidirectional override characters'
}
];
2025-05-28 14:46:32 +00:00
const results = [];
2025-05-27 15:26:22 +00:00
for (const testCase of testCases) {
try {
2025-05-28 14:46:32 +00:00
const einvoice = new EInvoice();
einvoice.issueDate = new Date(2024, 0, 1);
einvoice.id = testCase.text;
einvoice.subject = testCase.description;
// Set required fields for EN16931 compliance
einvoice.from = {
type: 'company',
name: 'Test Unicode Company',
description: testCase.description,
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: 'Commercial Register'
}
};
einvoice.to = {
type: 'person',
name: 'Test',
surname: 'Customer',
salutation: 'Mr' as const,
sex: 'male' as const,
title: 'Doctor' as const,
description: 'Test customer',
address: {
streetName: 'Customer Street',
houseNumber: '2',
postalCode: '54321',
city: 'Customer City',
country: 'DE'
}
};
// Add test item
einvoice.items = [{
position: 1,
name: `Item with ${testCase.name}`,
articleNumber: 'ART-001',
unitType: 'EA',
unitQuantity: 1,
unitNetPrice: 100,
vatPercentage: 19
}];
2025-05-27 15:26:22 +00:00
// Export to UBL format
const ublString = await einvoice.toXmlString('ubl');
2025-05-26 04:04:51 +00:00
2025-05-27 15:26:22 +00:00
// Check if special characters are preserved
const preserved = ublString.includes(testCase.text);
// Try to import it back
const newInvoice = new EInvoice();
await newInvoice.fromXmlString(ublString);
2025-05-28 14:46:32 +00:00
const roundTripPreserved = (newInvoice.id === testCase.text ||
newInvoice.invoiceId === testCase.text ||
newInvoice.accountingDocId === testCase.text);
console.log(`Test 1.${testCase.name}:`);
console.log(` Unicode preserved in XML: ${preserved ? 'Yes' : 'No'}`);
console.log(` Round-trip successful: ${roundTripPreserved ? 'Yes' : 'No'}`);
results.push({ name: testCase.name, preserved, roundTripPreserved });
2025-05-27 15:26:22 +00:00
} catch (error) {
2025-05-28 14:46:32 +00:00
console.log(`Test 1.${testCase.name}:`);
console.log(` Error: ${error.message}`);
results.push({ name: testCase.name, preserved: false, roundTripPreserved: false, error: error.message });
2025-05-26 04:04:51 +00:00
}
}
2025-05-28 14:46:32 +00:00
return results;
};
2025-05-26 04:04:51 +00:00
2025-05-27 15:26:22 +00:00
// Test 2: Various character encodings in invoice content
2025-05-28 14:46:32 +00:00
const testVariousEncodings = async () => {
2025-05-27 15:26:22 +00:00
const encodingTests = [
{
encoding: 'UTF-8',
text: 'Übung macht den Meister - äöüß'
},
{
encoding: 'Latin',
text: 'Ñoño español - ¡Hola!'
},
{
encoding: 'Cyrillic',
text: 'Счёт-фактура № 2024'
},
{
encoding: 'Greek',
text: 'Τιμολόγιο: ΜΜΚΔ'
},
{
encoding: 'Chinese',
text: '發票編號:貳零貳肆'
}
];
2025-05-28 14:46:32 +00:00
const results = [];
2025-05-27 15:26:22 +00:00
for (const test of encodingTests) {
try {
2025-05-28 14:46:32 +00:00
const einvoice = new EInvoice();
einvoice.issueDate = new Date(2024, 0, 1);
einvoice.id = `ENC-${test.encoding}`;
einvoice.subject = test.text;
einvoice.from = {
type: 'company',
name: test.text,
description: `Company using ${test.encoding}`,
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: 'Commercial Register'
}
};
einvoice.to = {
type: 'person',
name: 'Test',
surname: 'Customer',
salutation: 'Mr' as const,
sex: 'male' as const,
title: 'Doctor' as const,
description: 'Test customer',
address: {
streetName: 'Customer Street',
houseNumber: '2',
postalCode: '54321',
city: 'Customer City',
country: 'DE'
}
};
einvoice.items = [{
position: 1,
name: test.text,
articleNumber: 'ENC-001',
unitType: 'EA',
unitQuantity: 1,
unitNetPrice: 100,
vatPercentage: 19
}];
2025-05-27 15:26:22 +00:00
// Test both UBL and CII formats
2025-05-28 14:46:32 +00:00
const ublString = await einvoice.toXmlString('ubl');
const ciiString = await einvoice.toXmlString('cii');
// Check preservation in both formats
const ublPreserved = ublString.includes(test.text);
const ciiPreserved = ciiString.includes(test.text);
// Test round-trip for both formats
const ublInvoice = new EInvoice();
await ublInvoice.fromXmlString(ublString);
const ciiInvoice = new EInvoice();
await ciiInvoice.fromXmlString(ciiString);
const ublRoundTrip = ublInvoice.from?.name?.includes(test.text.substring(0, 10)) || false;
const ciiRoundTrip = ciiInvoice.from?.name?.includes(test.text.substring(0, 10)) || false;
console.log(`\nTest 2.${test.encoding}:`);
console.log(` UBL preserves encoding: ${ublPreserved ? 'Yes' : 'No'}`);
console.log(` CII preserves encoding: ${ciiPreserved ? 'Yes' : 'No'}`);
console.log(` UBL round-trip: ${ublRoundTrip ? 'Yes' : 'No'}`);
console.log(` CII round-trip: ${ciiRoundTrip ? 'Yes' : 'No'}`);
results.push({
encoding: test.encoding,
ublPreserved,
ciiPreserved,
ublRoundTrip,
ciiRoundTrip
});
2025-05-27 15:26:22 +00:00
} catch (error) {
2025-05-28 14:46:32 +00:00
console.log(`\nTest 2.${test.encoding}:`);
console.log(` Error: ${error.message}`);
results.push({
encoding: test.encoding,
ublPreserved: false,
ciiPreserved: false,
ublRoundTrip: false,
ciiRoundTrip: false,
error: error.message
});
2025-05-26 04:04:51 +00:00
}
}
2025-05-28 14:46:32 +00:00
return results;
};
2025-05-26 04:04:51 +00:00
2025-05-28 14:46:32 +00:00
// Test 3: Extremely unusual characters
const testExtremelyUnusualChars = async () => {
const extremeTests = [
2025-05-27 15:26:22 +00:00
{
2025-05-28 14:46:32 +00:00
name: 'ancient-scripts',
text: '𐀀𐀁𐀂 Invoice 𓀀𓀁𓀂',
description: 'Linear B and Egyptian hieroglyphs'
2025-05-27 15:26:22 +00:00
},
{
2025-05-28 14:46:32 +00:00
name: 'musical-symbols',
text: '♪♫♪ Invoice ♫♪♫',
description: 'Musical notation symbols'
2025-05-27 15:26:22 +00:00
},
{
2025-05-28 14:46:32 +00:00
name: 'math-symbols',
text: '∫∂ Invoice ∆∇',
description: 'Mathematical operators'
2025-05-27 15:26:22 +00:00
},
{
2025-05-28 14:46:32 +00:00
name: 'private-use',
text: '\uE000\uE001 Invoice \uE002\uE003',
description: 'Private use area characters'
2025-05-27 15:26:22 +00:00
}
];
2025-05-28 14:46:32 +00:00
const results = [];
for (const test of extremeTests) {
2025-05-27 15:26:22 +00:00
try {
2025-05-28 14:46:32 +00:00
const einvoice = new EInvoice();
einvoice.id = `EXTREME-${test.name}`;
einvoice.issueDate = new Date(2024, 0, 1);
einvoice.subject = test.description;
2025-05-26 04:04:51 +00:00
2025-05-28 14:46:32 +00:00
einvoice.from = {
type: 'company',
name: `Company ${test.text}`,
description: test.description,
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: 'Commercial Register'
}
};
2025-05-27 15:26:22 +00:00
2025-05-28 14:46:32 +00:00
einvoice.to = {
type: 'person',
name: 'Test',
surname: 'Customer',
salutation: 'Mr' as const,
sex: 'male' as const,
title: 'Doctor' as const,
description: 'Test customer',
address: {
streetName: 'Customer Street',
houseNumber: '2',
postalCode: '54321',
city: 'Customer City',
country: 'DE'
}
};
2025-05-26 04:04:51 +00:00
2025-05-28 14:46:32 +00:00
einvoice.items = [{
position: 1,
name: `Product ${test.text}`,
articleNumber: 'EXT-001',
unitType: 'EA',
unitQuantity: 1,
unitNetPrice: 100,
vatPercentage: 19
}];
const xmlString = await einvoice.toXmlString('ubl');
const preserved = xmlString.includes(test.text);
2025-05-26 04:04:51 +00:00
2025-05-27 15:26:22 +00:00
const newInvoice = new EInvoice();
2025-05-28 14:46:32 +00:00
await newInvoice.fromXmlString(xmlString);
const roundTrip = newInvoice.from?.name?.includes(test.text) || false;
console.log(`\nTest 3.${test.name}:`);
console.log(` Extreme chars preserved: ${preserved ? 'Yes' : 'No'}`);
console.log(` Round-trip successful: ${roundTrip ? 'Yes' : 'No'}`);
2025-05-27 15:26:22 +00:00
2025-05-28 14:46:32 +00:00
results.push({ name: test.name, preserved, roundTrip });
2025-05-27 15:26:22 +00:00
} catch (error) {
2025-05-28 14:46:32 +00:00
console.log(`\nTest 3.${test.name}:`);
console.log(` Error: ${error.message}`);
results.push({ name: test.name, preserved: false, roundTrip: false, error: error.message });
2025-05-26 04:04:51 +00:00
}
}
2025-05-28 14:46:32 +00:00
return results;
};
2025-05-26 04:04:51 +00:00
2025-05-28 14:46:32 +00:00
// Test 4: Normalization issues
const testNormalizationIssues = async () => {
const normalizationTests = [
{
name: 'nfc-nfd',
nfc: 'é', // NFC: single character
nfd: 'é', // NFD: e + combining acute
description: 'NFC vs NFD normalization'
},
{
name: 'ligatures',
text: 'ff Invoice ffi', // ff and ffi ligatures
description: 'Unicode ligatures'
}
2025-05-27 15:26:22 +00:00
];
2025-05-28 14:46:32 +00:00
const results = [];
for (const test of normalizationTests) {
2025-05-27 15:26:22 +00:00
try {
2025-05-28 14:46:32 +00:00
const einvoice = new EInvoice();
einvoice.id = `NORM-${test.name}`;
einvoice.issueDate = new Date(2024, 0, 1);
einvoice.subject = test.description;
// Use the test text in company name
const testText = test.text || test.nfc;
einvoice.from = {
type: 'company',
name: `Company ${testText}`,
description: test.description,
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: 'Commercial Register'
}
};
einvoice.to = {
type: 'person',
name: 'Test',
surname: 'Customer',
salutation: 'Mr' as const,
sex: 'male' as const,
title: 'Doctor' as const,
description: 'Test customer',
address: {
streetName: 'Customer Street',
houseNumber: '2',
postalCode: '54321',
city: 'Customer City',
country: 'DE'
}
};
einvoice.items = [{
position: 1,
name: `Product ${testText}`,
articleNumber: 'NORM-001',
unitType: 'EA',
unitQuantity: 1,
unitNetPrice: 100,
vatPercentage: 19
}];
2025-05-27 15:26:22 +00:00
const xmlString = await einvoice.toXmlString('ubl');
2025-05-28 14:46:32 +00:00
const preserved = xmlString.includes(testText);
2025-05-26 04:04:51 +00:00
2025-05-27 15:26:22 +00:00
const newInvoice = new EInvoice();
2025-05-28 14:46:32 +00:00
await newInvoice.fromXmlString(xmlString);
const roundTrip = newInvoice.from?.name?.includes(testText) || false;
2025-05-26 04:04:51 +00:00
2025-05-28 14:46:32 +00:00
console.log(`\nTest 4.${test.name}:`);
console.log(` Normalization preserved: ${preserved ? 'Yes' : 'No'}`);
console.log(` Round-trip successful: ${roundTrip ? 'Yes' : 'No'}`);
2025-05-27 15:26:22 +00:00
2025-05-28 14:46:32 +00:00
results.push({ name: test.name, preserved, roundTrip });
2025-05-27 15:26:22 +00:00
} catch (error) {
2025-05-28 14:46:32 +00:00
console.log(`\nTest 4.${test.name}:`);
console.log(` Error: ${error.message}`);
results.push({ name: test.name, preserved: false, roundTrip: false, error: error.message });
2025-05-26 04:04:51 +00:00
}
}
2025-05-28 14:46:32 +00:00
return results;
};
// Run all tests
const unicodeResults = await testUnicodeEdgeCases();
const encodingResults = await testVariousEncodings();
const extremeResults = await testExtremelyUnusualChars();
const normalizationResults = await testNormalizationIssues();
console.log(`\n=== Unusual Character Sets Test Summary ===`);
// Count successful tests
const unicodeSuccess = unicodeResults.filter(r => r.roundTripPreserved).length;
const encodingSuccess = encodingResults.filter(r => r.ublRoundTrip || r.ciiRoundTrip).length;
const extremeSuccess = extremeResults.filter(r => r.roundTrip).length;
const normalizationSuccess = normalizationResults.filter(r => r.roundTrip).length;
console.log(`Unicode edge cases: ${unicodeSuccess}/${unicodeResults.length} successful`);
console.log(`Various encodings: ${encodingSuccess}/${encodingResults.length} successful`);
console.log(`Extreme characters: ${extremeSuccess}/${extremeResults.length} successful`);
console.log(`Normalization tests: ${normalizationSuccess}/${normalizationResults.length} successful`);
// Test passes if at least basic Unicode handling works
const basicUnicodeWorks = unicodeResults.some(r => r.roundTripPreserved);
const basicEncodingWorks = encodingResults.some(r => r.ublRoundTrip || r.ciiRoundTrip);
expect(basicUnicodeWorks).toBeTrue();
expect(basicEncodingWorks).toBeTrue();
2025-05-26 04:04:51 +00:00
});
// Run the test
tap.start();