BREAKING CHANGE(core): Rebrand XInvoice to EInvoice: update package name, class names, imports, and documentation

This commit is contained in:
2025-05-24 16:33:58 +00:00
parent 805adc6d5c
commit a93ea090ce
27 changed files with 3172 additions and 295 deletions

View File

@ -1,29 +1,29 @@
import { tap, expect } from '@push.rocks/tapbundle';
import { XInvoice } from '../ts/classes.xinvoice.js';
import { EInvoice } from '../ts/einvoice.js';
import { ValidationLevel, InvoiceFormat } from '../ts/interfaces/common.js';
import * as fs from 'fs/promises';
import * as path from 'path';
// Test loading and parsing real CII (Factur-X/ZUGFeRD) XML files
tap.test('XInvoice should load and parse real CII XML files', async () => {
tap.test('EInvoice should load and parse real CII XML files', async () => {
// Test with a simple CII file
const xmlPath = path.join(process.cwd(), 'test/assets/corpus/XML-Rechnung/CII/EN16931_Einfach.cii.xml');
const xmlContent = await fs.readFile(xmlPath, 'utf8');
// Create XInvoice from XML
const xinvoice = await XInvoice.fromXml(xmlContent);
// Create EInvoice from XML
const einvoice = await EInvoice.fromXml(xmlContent);
// Check that the XInvoice instance has the expected properties
expect(xinvoice).toBeTruthy();
expect(xinvoice.from).toBeTruthy();
expect(xinvoice.to).toBeTruthy();
expect(xinvoice.items).toBeArray();
// Check that the EInvoice instance has the expected properties
expect(einvoice).toBeTruthy();
expect(einvoice.from).toBeTruthy();
expect(einvoice.to).toBeTruthy();
expect(einvoice.items).toBeArray();
// Check that the format is detected correctly
expect(xinvoice.getFormat()).toEqual(InvoiceFormat.FACTURX);
expect(einvoice.getFormat()).toEqual(InvoiceFormat.FACTURX);
// Check that the invoice can be exported back to XML
const exportedXml = await xinvoice.exportXml('facturx');
const exportedXml = await einvoice.exportXml('facturx');
expect(exportedXml).toBeTruthy();
expect(exportedXml).toInclude('CrossIndustryInvoice');
@ -34,47 +34,47 @@ tap.test('XInvoice should load and parse real CII XML files', async () => {
});
// Test loading and parsing real UBL (XRechnung) XML files
tap.test('XInvoice should load and parse real UBL XML files', async () => {
tap.test('EInvoice should load and parse real UBL XML files', async () => {
// Test with a simple UBL file
const xmlPath = path.join(process.cwd(), 'test/assets/corpus/XML-Rechnung/UBL/EN16931_Einfach.ubl.xml');
const xmlContent = await fs.readFile(xmlPath, 'utf8');
// Create XInvoice from XML
const xinvoice = await XInvoice.fromXml(xmlContent);
// Create EInvoice from XML
const einvoice = await EInvoice.fromXml(xmlContent);
// Check that the XInvoice instance has the expected properties
expect(xinvoice).toBeTruthy();
expect(xinvoice.from).toBeTruthy();
expect(xinvoice.to).toBeTruthy();
expect(xinvoice.items).toBeArray();
// Check that the EInvoice instance has the expected properties
expect(einvoice).toBeTruthy();
expect(einvoice.from).toBeTruthy();
expect(einvoice.to).toBeTruthy();
expect(einvoice.items).toBeArray();
// Check that the format is detected correctly
// This file is a UBL format, not XRechnung
expect(xinvoice.getFormat()).toEqual(InvoiceFormat.UBL);
expect(einvoice.getFormat()).toEqual(InvoiceFormat.UBL);
// Skip the export test for now since UBL encoder is not implemented yet
// This is a legitimate limitation of the current implementation
console.log('Skipping UBL export test - UBL encoder not yet implemented');
// Just test that the format was detected correctly
expect(xinvoice.getFormat()).toEqual(InvoiceFormat.UBL);
expect(einvoice.getFormat()).toEqual(InvoiceFormat.UBL);
});
// Test PDF creation and extraction with real XML files
tap.test('XInvoice should create and parse PDFs with embedded XML', async () => {
tap.test('EInvoice should create and parse PDFs with embedded XML', async () => {
// Find a real CII XML file to use
const xmlPath = path.join(process.cwd(), 'test/assets/corpus/XML-Rechnung/CII/EN16931_Einfach.cii.xml');
const xmlContent = await fs.readFile(xmlPath, 'utf8');
// Create XInvoice from XML
const xinvoice = await XInvoice.fromXml(xmlContent);
// Create EInvoice from XML
const einvoice = await EInvoice.fromXml(xmlContent);
// Check that the XInvoice instance has the expected properties
expect(xinvoice).toBeTruthy();
expect(xinvoice.from).toBeTruthy();
expect(xinvoice.to).toBeTruthy();
expect(xinvoice.items).toBeArray();
// Check that the EInvoice instance has the expected properties
expect(einvoice).toBeTruthy();
expect(einvoice.from).toBeTruthy();
expect(einvoice.to).toBeTruthy();
expect(einvoice.items).toBeArray();
// Create a simple PDF document
const { PDFDocument } = await import('pdf-lib');
@ -84,7 +84,7 @@ tap.test('XInvoice should create and parse PDFs with embedded XML', async () =>
const pdfBytes = await pdfDoc.save();
// Set the PDF buffer
xinvoice.pdf = {
einvoice.pdf = {
name: 'test-invoice.pdf',
id: `test-invoice-${Date.now()}`,
metadata: {
@ -94,7 +94,7 @@ tap.test('XInvoice should create and parse PDFs with embedded XML', async () =>
};
// Export as PDF with embedded XML
const exportedPdf = await xinvoice.exportPdf('facturx');
const exportedPdf = await einvoice.exportPdf('facturx');
expect(exportedPdf).toBeTruthy();
expect(exportedPdf.buffer).toBeTruthy();
@ -104,21 +104,21 @@ tap.test('XInvoice should create and parse PDFs with embedded XML', async () =>
await fs.writeFile(path.join(testDir, 'test-invoice-with-xml.pdf'), exportedPdf.buffer);
// Now try to load the PDF back
const loadedXInvoice = await XInvoice.fromPdf(exportedPdf.buffer);
const loadedEInvoice = await EInvoice.fromPdf(exportedPdf.buffer);
// Check that the loaded XInvoice has the expected properties
expect(loadedXInvoice).toBeTruthy();
expect(loadedXInvoice.from).toBeTruthy();
expect(loadedXInvoice.to).toBeTruthy();
expect(loadedXInvoice.items).toBeArray();
// Check that the loaded EInvoice has the expected properties
expect(loadedEInvoice).toBeTruthy();
expect(loadedEInvoice.from).toBeTruthy();
expect(loadedEInvoice.to).toBeTruthy();
expect(loadedEInvoice.items).toBeArray();
// Check that key properties are present
expect(loadedXInvoice.id).toBeTruthy();
expect(loadedXInvoice.from.name).toBeTruthy();
expect(loadedXInvoice.to.name).toBeTruthy();
expect(loadedEInvoice.id).toBeTruthy();
expect(loadedEInvoice.from.name).toBeTruthy();
expect(loadedEInvoice.to.name).toBeTruthy();
// Export the loaded invoice back to XML
const reExportedXml = await loadedXInvoice.exportXml('facturx');
const reExportedXml = await loadedEInvoice.exportXml('facturx');
expect(reExportedXml).toBeTruthy();
expect(reExportedXml).toInclude('CrossIndustryInvoice');
@ -153,16 +153,16 @@ async function findPdfFiles(dir: string): Promise<string[]> {
};
// Test validation of real invoice files
tap.test('XInvoice should validate real invoice files', async () => {
tap.test('EInvoice should validate real invoice files', async () => {
// Test with a simple CII file
const xmlPath = path.join(process.cwd(), 'test/assets/corpus/XML-Rechnung/CII/EN16931_Einfach.cii.xml');
const xmlContent = await fs.readFile(xmlPath, 'utf8');
// Create XInvoice from XML
const xinvoice = await XInvoice.fromXml(xmlContent);
// Create EInvoice from XML
const einvoice = await EInvoice.fromXml(xmlContent);
// Validate the XML
const result = await xinvoice.validate(ValidationLevel.SYNTAX);
const result = await einvoice.validate(ValidationLevel.SYNTAX);
// Check that validation passed
expect(result.valid).toBeTrue();
@ -170,7 +170,7 @@ tap.test('XInvoice should validate real invoice files', async () => {
});
// Test with multiple real invoice files
tap.test('XInvoice should handle multiple real invoice files', async () => {
tap.test('EInvoice should handle multiple real invoice files', async () => {
// Get all CII files
const ciiDir = path.join(process.cwd(), 'test/assets/corpus/XML-Rechnung/CII');
const ciiFiles = await fs.readdir(ciiDir);
@ -184,19 +184,19 @@ tap.test('XInvoice should handle multiple real invoice files', async () => {
const xmlPath = path.join(ciiDir, file);
const xmlContent = await fs.readFile(xmlPath, 'utf8');
// Create XInvoice from XML
const xinvoice = await XInvoice.fromXml(xmlContent);
// Create EInvoice from XML
const einvoice = await EInvoice.fromXml(xmlContent);
// Check that the XInvoice instance has the expected properties
expect(xinvoice).toBeTruthy();
expect(xinvoice.from).toBeTruthy();
expect(xinvoice.to).toBeTruthy();
// Check that the EInvoice instance has the expected properties
expect(einvoice).toBeTruthy();
expect(einvoice.from).toBeTruthy();
expect(einvoice.to).toBeTruthy();
// Check that the format is detected correctly
expect(xinvoice.getFormat()).toEqual(InvoiceFormat.FACTURX);
expect(einvoice.getFormat()).toEqual(InvoiceFormat.FACTURX);
// Check that the invoice can be exported back to XML
const exportedXml = await xinvoice.exportXml('facturx');
const exportedXml = await einvoice.exportXml('facturx');
expect(exportedXml).toBeTruthy();
expect(exportedXml).toInclude('CrossIndustryInvoice');
}