import { tap, expect } from '@push.rocks/tapbundle'; import { XInvoice } from '../ts/classes.xinvoice.js'; import { type ExportFormat } from '../ts/interfaces.js'; import { PDFDocument, PDFName } from 'pdf-lib'; // Test PDF export with type-safe format parameters tap.test('XInvoice should support PDF export with type-safe formats', async () => { // 1. Create a sample invoice with correct structure for the encoder const invoice = new XInvoice(); invoice.content.invoiceData.id = `TYPE-SAFETY-TEST-${Date.now()}`; invoice.content.invoiceData.billedBy.name = 'Test Seller'; invoice.content.invoiceData.billedTo.name = 'Test Buyer'; // Add address info needed by the encoder invoice.content.invoiceData.billedBy.address.streetName = '123 Seller St'; invoice.content.invoiceData.billedBy.address.city = 'Seller City'; invoice.content.invoiceData.billedBy.address.postalCode = '12345'; invoice.content.invoiceData.billedTo.address.streetName = '456 Buyer St'; invoice.content.invoiceData.billedTo.address.city = 'Buyer City'; invoice.content.invoiceData.billedTo.address.postalCode = '67890'; // Add an item with correct structure invoice.content.invoiceData.items.push({ position: 1, name: 'Test Product', unitType: 'piece', unitQuantity: 2, unitNetPrice: 99.95, vatPercentage: 19 }); // Create a simple PDF const pdfDoc = await PDFDocument.create(); pdfDoc.addPage().drawText('Export Type Safety Test'); const pdfBuffer = await pdfDoc.save(); // Load the PDF invoice.pdf = { name: 'type-safety-test.pdf', id: `type-safety-${Date.now()}`, metadata: { textExtraction: 'Type Safety Test' }, buffer: pdfBuffer }; // Test each valid export format const formats: ExportFormat[] = ['facturx', 'zugferd', 'xrechnung', 'ubl']; for (const format of formats) { // This should compile without type errors console.log(`Testing export with format: ${format}`); const exportedPdf = await invoice.exportPdf(format); // Verify PDF was created and is larger than original (due to XML) expect(exportedPdf).toBeDefined(); expect(exportedPdf.buffer).toBeDefined(); expect(exportedPdf.buffer.byteLength).toBeGreaterThan(pdfBuffer.byteLength); // Additional check: directly examine PDF structure for embedded file const pdfDoc = await PDFDocument.load(exportedPdf.buffer); const namesDict = pdfDoc.catalog.lookup(PDFName.of('Names')); expect(namesDict).toBeDefined(); } console.log('Successfully tested PDF export with all supported formats'); }); // Format parameter type check test tap.test('XInvoice should accept only valid export formats', async () => { // This test doesn't actually run code, but verifies that the type system works // The compiler should catch invalid format types // Create a sample XInvoice instance const xInvoice = new XInvoice(); // These should compile fine - they're valid ExportFormat values const validFormats: ExportFormat[] = ['facturx', 'zugferd', 'xrechnung', 'ubl']; // For each format, verify it's part of the expected enum values for (const format of validFormats) { expect(['facturx', 'zugferd', 'xrechnung', 'ubl'].includes(format)).toBeTrue(); } // This test passes if it compiles without type errors expect(true).toBeTrue(); }); // Start the tests export default tap.start();