221 lines
7.6 KiB
TypeScript
221 lines
7.6 KiB
TypeScript
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
|
import { promises as fs } from 'fs';
|
|
import * as path from 'path';
|
|
import { CorpusLoader } from '../../helpers/corpus.loader.js';
|
|
import { PerformanceTracker } from '../../helpers/performance.tracker.js';
|
|
import { InvoiceFormat } from '../../../ts/interfaces/common.js';
|
|
|
|
tap.test('FD-09: FatturaPA Format Detection - should correctly identify Italian FatturaPA invoices', async () => {
|
|
// Get FatturaPA test files from corpus
|
|
const fatturapaFiles = await CorpusLoader.getFiles('FATTURAPA_OFFICIAL');
|
|
const fatturaPAEigorFiles = await CorpusLoader.getFiles('FATTURAPA_EIGOR');
|
|
|
|
const allFatturapaFiles = [...fatturapaFiles, ...fatturaPAEigorFiles].filter(f => f.endsWith('.xml'));
|
|
console.log(`Testing ${allFatturapaFiles.length} FatturaPA invoice files`);
|
|
|
|
let successCount = 0;
|
|
let failureCount = 0;
|
|
const failures: { file: string; error: string }[] = [];
|
|
|
|
// Import the format detector
|
|
const { FormatDetector } = await import('../../../ts/formats/utils/format.detector.js');
|
|
const sampledFiles = allFatturapaFiles.slice(0, 10);
|
|
|
|
if (sampledFiles.length === 0) {
|
|
console.log('No FatturaPA corpus files available for detection test');
|
|
return;
|
|
}
|
|
|
|
for (const filePath of sampledFiles) {
|
|
const fileName = path.basename(filePath);
|
|
|
|
try {
|
|
// Read XML content
|
|
const xmlContent = await fs.readFile(filePath, 'utf-8');
|
|
|
|
// Track performance of format detection
|
|
const { result: format } = await PerformanceTracker.track(
|
|
'fatturapa-format-detection',
|
|
async () => {
|
|
return FormatDetector.detectFormat(xmlContent);
|
|
},
|
|
{ file: fileName }
|
|
);
|
|
|
|
if (format === InvoiceFormat.FATTURAPA) {
|
|
successCount++;
|
|
console.log(`✓ ${fileName}: Correctly detected as FatturaPA`);
|
|
} else {
|
|
failureCount++;
|
|
failures.push({
|
|
file: fileName,
|
|
error: `Detected as ${format} instead of FatturaPA`
|
|
});
|
|
console.log(`✗ ${fileName}: Detected as ${format} instead of FatturaPA`);
|
|
}
|
|
} catch (error: any) {
|
|
failureCount++;
|
|
failures.push({
|
|
file: fileName,
|
|
error: error.message
|
|
});
|
|
console.log(`✗ ${fileName}: Error - ${error.message}`);
|
|
}
|
|
}
|
|
|
|
// Report results
|
|
console.log(`\nFatturaPA Format Detection Results:`);
|
|
console.log(`✓ Success: ${successCount}/${allFatturapaFiles.length} (${(successCount/Math.min(allFatturapaFiles.length, 10)*100).toFixed(1)}%)`);
|
|
console.log(`✗ Failed: ${failureCount}/${Math.min(allFatturapaFiles.length, 10)} (${(failureCount/Math.min(allFatturapaFiles.length, 10)*100).toFixed(1)}%)`);
|
|
|
|
if (failures.length > 0) {
|
|
console.log(`\nSample failures:`);
|
|
failures.slice(0, 3).forEach(f => console.log(` - ${f.file}: ${f.error}`));
|
|
}
|
|
|
|
// Performance summary
|
|
const perfSummary = await PerformanceTracker.getSummary('fatturapa-format-detection');
|
|
if (perfSummary) {
|
|
console.log(`\nPerformance Summary:`);
|
|
console.log(` Average: ${perfSummary.average.toFixed(2)}ms`);
|
|
console.log(` Min: ${perfSummary.min.toFixed(2)}ms`);
|
|
console.log(` Max: ${perfSummary.max.toFixed(2)}ms`);
|
|
console.log(` P95: ${perfSummary.p95.toFixed(2)}ms`);
|
|
}
|
|
|
|
expect(successCount).toEqual(sampledFiles.length);
|
|
expect(failureCount).toEqual(0);
|
|
});
|
|
|
|
tap.test('FD-09: FatturaPA Structure Detection - should detect FatturaPA by root element', async () => {
|
|
const { FormatDetector } = await import('../../../ts/formats/utils/format.detector.js');
|
|
|
|
const fatturapaStructures = [
|
|
{
|
|
name: 'Standard FatturaElettronica',
|
|
xml: `<?xml version="1.0" encoding="UTF-8"?>
|
|
<p:FatturaElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
|
|
xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2"
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
versione="FPR12">
|
|
<FatturaElettronicaHeader>
|
|
<DatiTrasmissione>
|
|
<IdTrasmittente>
|
|
<IdCodice>12345678901</IdCodice>
|
|
</IdTrasmittente>
|
|
</DatiTrasmissione>
|
|
</FatturaElettronicaHeader>
|
|
</p:FatturaElettronica>`
|
|
},
|
|
{
|
|
name: 'FatturaElettronica without prefix',
|
|
xml: `<?xml version="1.0" encoding="UTF-8"?>
|
|
<FatturaElettronica xmlns="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2">
|
|
<FatturaElettronicaHeader>
|
|
<DatiTrasmissione>
|
|
<IdTrasmittente>
|
|
<IdCodice>12345678901</IdCodice>
|
|
</IdTrasmittente>
|
|
</DatiTrasmissione>
|
|
</FatturaElettronicaHeader>
|
|
</FatturaElettronica>`
|
|
}
|
|
];
|
|
|
|
for (const test of fatturapaStructures) {
|
|
const { result: format } = await PerformanceTracker.track(
|
|
'fatturapa-structure-detection',
|
|
async () => FormatDetector.detectFormat(test.xml)
|
|
);
|
|
|
|
console.log(`${test.name}: Detected as ${format}`);
|
|
expect(format).toEqual(InvoiceFormat.FATTURAPA);
|
|
console.log(' ✓ Correctly identified as FatturaPA');
|
|
}
|
|
});
|
|
|
|
tap.test('FD-09: FatturaPA Version Detection - should detect different FatturaPA versions', async () => {
|
|
const { FormatDetector } = await import('../../../ts/formats/utils/format.detector.js');
|
|
|
|
const versionTests = [
|
|
{
|
|
version: 'FPR12',
|
|
xml: `<?xml version="1.0"?>
|
|
<FatturaElettronica xmlns="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" versione="FPR12">
|
|
<FatturaElettronicaHeader>
|
|
<DatiTrasmissione>
|
|
<IdTrasmittente><IdCodice>IT12345678901</IdCodice></IdTrasmittente>
|
|
</DatiTrasmissione>
|
|
</FatturaElettronicaHeader>
|
|
</FatturaElettronica>`
|
|
},
|
|
{
|
|
version: 'FPA12',
|
|
xml: `<?xml version="1.0"?>
|
|
<FatturaElettronica xmlns="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" versione="FPA12">
|
|
<FatturaElettronicaHeader>
|
|
<DatiTrasmissione>
|
|
<IdTrasmittente><IdCodice>IT12345678901</IdCodice></IdTrasmittente>
|
|
</DatiTrasmissione>
|
|
</FatturaElettronicaHeader>
|
|
</FatturaElettronica>`
|
|
}
|
|
];
|
|
|
|
for (const test of versionTests) {
|
|
const { result: format } = await PerformanceTracker.track(
|
|
'fatturapa-version-detection',
|
|
async () => FormatDetector.detectFormat(test.xml)
|
|
);
|
|
|
|
console.log(`FatturaPA ${test.version}: Detected as ${format}`);
|
|
expect(format).toEqual(InvoiceFormat.FATTURAPA);
|
|
console.log(` ✓ Version ${test.version} correctly detected`);
|
|
}
|
|
});
|
|
|
|
tap.test('FD-09: FatturaPA vs Other Formats - should distinguish from other XML formats', async () => {
|
|
const { FormatDetector } = await import('../../../ts/formats/utils/format.detector.js');
|
|
|
|
const comparisonTests = [
|
|
{
|
|
name: 'FatturaPA',
|
|
xml: `<?xml version="1.0"?>
|
|
<FatturaElettronica xmlns="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2">
|
|
<FatturaElettronicaHeader/>
|
|
</FatturaElettronica>`,
|
|
expectedFormat: 'fattura'
|
|
},
|
|
{
|
|
name: 'UBL Invoice',
|
|
xml: `<?xml version="1.0"?>
|
|
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
|
|
<ID>UBL-001</ID>
|
|
</Invoice>`,
|
|
expectedFormat: 'ubl'
|
|
},
|
|
{
|
|
name: 'CII Invoice',
|
|
xml: `<?xml version="1.0"?>
|
|
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100">
|
|
<rsm:ExchangedDocument/>
|
|
</rsm:CrossIndustryInvoice>`,
|
|
expectedFormat: 'cii'
|
|
}
|
|
];
|
|
|
|
for (const test of comparisonTests) {
|
|
const { result: format } = await PerformanceTracker.track(
|
|
'format-distinction-test',
|
|
async () => FormatDetector.detectFormat(test.xml)
|
|
);
|
|
|
|
console.log(`${test.name}: Detected as ${format}`);
|
|
|
|
expect(format.toString().toLowerCase()).toContain(test.expectedFormat);
|
|
console.log(` ✓ Correctly distinguished ${test.name}`);
|
|
}
|
|
});
|
|
|
|
tap.start();
|