import { tap, expect } from '@push.rocks/tapbundle';
import { XInvoice } from '../ts/classes.xinvoice.js';
import { ValidationLevel } from '../ts/interfaces/common.js';
import * as fs from 'fs/promises';
import * as path from 'path';
// Test for XInvoice class functionality
tap.test('XInvoice should load XML correctly', async () => {
// Create a sample XML string
const sampleXml = `
urn:cen.eu:en16931:2017
INV-2023-001
380
20230101
Supplier Company
Supplier Street
123
12345
Supplier City
DE
DE123456789
Customer Company
Customer Street
456
54321
Customer City
DE
EUR
200.00
38.00
238.00
238.00
`;
// Save the sample XML to a file
const testDir = path.join(process.cwd(), 'test', 'output');
await fs.mkdir(testDir, { recursive: true });
const xmlPath = path.join(testDir, 'sample-invoice.xml');
await fs.writeFile(xmlPath, sampleXml);
// Create XInvoice from XML
const xinvoice = await XInvoice.fromXml(sampleXml);
// Check that the XInvoice instance has the expected properties
expect(xinvoice.id).toEqual('INV-2023-001');
expect(xinvoice.from.name).toEqual('Supplier Company');
expect(xinvoice.to.name).toEqual('Customer Company');
});
tap.test('XInvoice should export XML correctly', async () => {
// Create a sample XML string
const sampleXml = `
urn:cen.eu:en16931:2017
INV-2023-001
380
20230101
Supplier Company
Supplier Street
123
12345
Supplier City
DE
DE123456789
Customer Company
Customer Street
456
54321
Customer City
DE
EUR
200.00
38.00
238.00
238.00
`;
// Create XInvoice from XML
const xinvoice = await XInvoice.fromXml(sampleXml);
// Export XML
const exportedXml = await xinvoice.exportXml('facturx');
// Check that the exported XML contains expected elements
expect(exportedXml).toInclude('CrossIndustryInvoice');
expect(exportedXml).toInclude('INV-2023-001');
expect(exportedXml).toInclude('Supplier Company');
expect(exportedXml).toInclude('Customer Company');
// Save the exported XML to a file
const testDir = path.join(process.cwd(), 'test', 'output');
await fs.mkdir(testDir, { recursive: true });
const exportedXmlPath = path.join(testDir, 'exported-invoice.xml');
await fs.writeFile(exportedXmlPath, exportedXml);
});
// Run the tests
tap.start();