This commit is contained in:
2025-04-03 16:41:10 +00:00
parent 21650f1181
commit a932d68f86
34 changed files with 1265 additions and 2987 deletions

View File

@ -1,63 +1,52 @@
import { tap, expect } from '@push.rocks/tapbundle';
import { FacturXDecoder } from '../ts/formats/cii/facturx/facturx.decoder.js';
import { FacturXEncoder } from '../ts/formats/cii/facturx/facturx.encoder.js';
import { FacturXValidator } from '../ts/formats/cii/facturx/facturx.validator.js';
import type { TInvoice } from '../ts/interfaces/common.js';
import { ValidationLevel } from '../ts/interfaces/common.js';
import * as assert from 'assert';
import * as fs from 'fs/promises';
import * as path from 'path';
/**
* Test for circular encoding/decoding of Factur-X
*/
async function testFacturXCircular() {
console.log('Starting Factur-X circular test...');
// Test for circular encoding/decoding of Factur-X
tap.test('Factur-X should maintain data integrity through encode/decode cycle', async () => {
// Create a sample invoice
const invoice = createSampleInvoice();
try {
// Create a sample invoice
const invoice = createSampleInvoice();
// Create encoder
const encoder = new FacturXEncoder();
// Encode to XML
const xml = await encoder.encode(invoice);
// Save XML for inspection
const testDir = path.join(process.cwd(), 'test', 'output');
await fs.mkdir(testDir, { recursive: true });
await fs.writeFile(path.join(testDir, 'facturx-circular-encoded.xml'), xml);
// Create decoder
const decoder = new FacturXDecoder(xml);
// Decode XML
const decodedInvoice = await decoder.decode();
// Check that decoded invoice is not null
assert.ok(decodedInvoice, 'Decoded invoice should not be null');
// Check that key properties match
assert.strictEqual(decodedInvoice.id, invoice.id, 'Invoice ID should match');
assert.strictEqual(decodedInvoice.from.name, invoice.from.name, 'Seller name should match');
assert.strictEqual(decodedInvoice.to.name, invoice.to.name, 'Buyer name should match');
// Create validator
const validator = new FacturXValidator(xml);
// Validate XML
const result = validator.validate(ValidationLevel.SYNTAX);
// Check that validation passed
assert.strictEqual(result.valid, true, 'XML should be valid');
assert.strictEqual(result.errors.length, 0, 'There should be no validation errors');
console.log('Factur-X circular test passed!');
} catch (error) {
console.error('Factur-X circular test failed:', error);
process.exit(1);
}
}
// Create encoder
const encoder = new FacturXEncoder();
// Encode to XML
const xml = await encoder.encode(invoice);
// Save XML for inspection
const testDir = path.join(process.cwd(), 'test', 'output');
await fs.mkdir(testDir, { recursive: true });
await fs.writeFile(path.join(testDir, 'facturx-circular-encoded.xml'), xml);
// Create decoder
const decoder = new FacturXDecoder(xml);
// Decode XML
const decodedInvoice = await decoder.decode();
// Check that decoded invoice is not null
expect(decodedInvoice).toBeTruthy();
// Check that key properties match
expect(decodedInvoice.id).toEqual(invoice.id);
expect(decodedInvoice.from.name).toEqual(invoice.from.name);
expect(decodedInvoice.to.name).toEqual(invoice.to.name);
// Create validator
const validator = new FacturXValidator(xml);
// Validate XML
const result = validator.validate(ValidationLevel.SYNTAX);
// Check that validation passed
expect(result.valid).toBeTrue();
expect(result.errors).toHaveLength(0);
});
/**
* Creates a sample invoice for testing
@ -154,5 +143,5 @@ function createSampleInvoice(): TInvoice {
} as TInvoice;
}
// Run the test
testFacturXCircular();
// Run the tests
tap.start();