import { XInvoice } from '../ts/classes.xinvoice.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 XInvoice class functionality */ async function testXInvoiceFunctionality() { console.log('Starting XInvoice functionality tests...'); try { // 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); console.log('Testing XInvoice.fromXml()...'); // Create XInvoice from XML const xinvoice = await XInvoice.fromXml(sampleXml); // Check that the XInvoice instance has the expected properties assert.strictEqual(xinvoice.id, 'INV-2023-001', 'Invoice ID should match'); assert.strictEqual(xinvoice.from.name, 'Supplier Company', 'Seller name should match'); assert.strictEqual(xinvoice.to.name, 'Customer Company', 'Buyer name should match'); console.log('Testing XInvoice.exportXml()...'); // Export XML const exportedXml = await xinvoice.exportXml('facturx'); // Check that the exported XML contains expected elements assert.ok(exportedXml.includes('CrossIndustryInvoice'), 'Exported XML should contain CrossIndustryInvoice element'); assert.ok(exportedXml.includes('INV-2023-001'), 'Exported XML should contain the invoice ID'); assert.ok(exportedXml.includes('Supplier Company'), 'Exported XML should contain the seller name'); assert.ok(exportedXml.includes('Customer Company'), 'Exported XML should contain the buyer name'); // Save the exported XML to a file const exportedXmlPath = path.join(testDir, 'exported-invoice.xml'); await fs.writeFile(exportedXmlPath, exportedXml); console.log('All XInvoice functionality tests passed!'); } catch (error) { console.error('XInvoice functionality test failed:', error); process.exit(1); } } // Run the test testXInvoiceFunctionality();