import { expect, tap } from '@git.zone/tstest/tapbundle'; import { promises as fs } from 'fs'; import * as path from 'path'; import { CorpusLoader } from '../../helpers/corpus.loader.js'; tap.test('PDF-07: Metadata Preservation - Basic Metadata Test', async () => { console.log('Testing PDF metadata preservation...'); // Import required classes const { EInvoice } = await import('../../../ts/index.js'); // Create an invoice with full metadata const invoice = new EInvoice(); invoice.id = 'META-TEST-001'; invoice.accountingDocId = 'META-TEST-001'; invoice.date = Date.now(); invoice.currency = 'EUR'; invoice.from.name = 'Metadata Test Supplier'; invoice.from.address.city = 'Berlin'; invoice.from.address.postalCode = '10115'; invoice.from.address.country = 'DE'; invoice.to.name = 'Metadata Test Customer'; invoice.to.address.city = 'Munich'; invoice.to.address.postalCode = '80331'; invoice.to.address.country = 'DE'; // Set additional metadata if (!invoice.metadata) { invoice.metadata = {}; } invoice.metadata.format = 'FACTURX'; invoice.metadata.version = '1.0'; invoice.metadata.profile = 'BASIC'; invoice.addItem({ name: 'Test Item for Metadata', unitQuantity: 1, unitNetPrice: 100.00, vatPercentage: 19 }); // Test metadata preservation during XML generation try { const xmlString = await invoice.toXmlString('facturx'); expect(xmlString).toBeTruthy(); expect(xmlString.length).toBeGreaterThan(100); // Create a new invoice from the XML const newInvoice = new EInvoice(); await newInvoice.fromXmlString(xmlString); // Verify core data is preserved expect(newInvoice.id).toBe('META-TEST-001'); expect(newInvoice.currency).toBe('EUR'); expect(newInvoice.from.name).toBe('Metadata Test Supplier'); expect(newInvoice.to.name).toBe('Metadata Test Customer'); console.log('✓ Metadata preserved during XML round-trip'); } catch (error) { console.log(`⚠ Metadata preservation test failed: ${error.message}`); } }); tap.test('PDF-07: Metadata Preservation - PDF Metadata Test', async () => { console.log('Testing PDF metadata extraction and preservation...'); // Import required classes const { EInvoice } = await import('../../../ts/index.js'); // Get PDF files from corpus const pdfFiles = await CorpusLoader.getFiles('ZUGFERD_V2_CORRECT'); const existingPdfs = pdfFiles.filter(file => file.endsWith('.pdf')); if (existingPdfs.length === 0) { console.log('⚠ No PDF files found in corpus for metadata test'); return; } // Test metadata extraction from first PDF const pdfPath = existingPdfs[0]; const pdfName = path.basename(pdfPath); try { const invoice = new EInvoice(); await invoice.fromFile(pdfPath); // Check if we have PDF metadata if (invoice.pdf) { console.log(`✓ PDF metadata available for ${pdfName}`); expect(invoice.pdf).toBeTruthy(); expect(invoice.pdf.name).toBe(pdfName); if (invoice.pdf.metadata) { console.log(' PDF format:', invoice.pdf.metadata.format || 'Unknown'); // Check for embedded XML info if (invoice.pdf.metadata.embeddedXml) { console.log(' Embedded XML filename:', invoice.pdf.metadata.embeddedXml.filename); expect(invoice.pdf.metadata.embeddedXml.filename).toBeTruthy(); } } } else { console.log('○ No PDF metadata found'); } // Verify invoice data was extracted expect(invoice.id).toBeTruthy(); console.log(`✓ Invoice ID extracted: ${invoice.id}`); } catch (error) { console.log(`⚠ PDF metadata test failed: ${error.message}`); } }); tap.test('PDF-07: Metadata Preservation - Format Detection Test', async () => { console.log('Testing metadata preservation with format detection...'); // Import required classes const { EInvoice } = await import('../../../ts/index.js'); const { FormatDetector } = await import('../../../ts/formats/utils/format.detector.js'); // Test different invoice formats const testData = [ { name: 'UBL Invoice', xml: ` UBL-META-001 2024-01-01 380 ` }, { name: 'CII Invoice', xml: ` CII-META-001 ` } ]; for (const test of testData) { console.log(`\nTesting ${test.name}...`); try { // Detect format const detectedFormat = FormatDetector.detectFormat(test.xml); console.log(` Detected format: ${detectedFormat}`); // Create invoice from XML const invoice = new EInvoice(); await invoice.fromXmlString(test.xml); // Check that format metadata is preserved expect(invoice.getFormat()).toBeTruthy(); console.log(` Invoice format: ${invoice.getFormat()}`); // Verify we can access the original XML const originalXml = invoice.getXml(); expect(originalXml).toBe(test.xml); console.log(' ✓ Original XML preserved'); } catch (error) { console.log(` ⚠ Format test failed: ${error.message}`); } } }); tap.test('PDF-07: Metadata Preservation - Summary', async () => { console.log(`\n=== Metadata Preservation Testing Summary ===`); console.log('✓ Basic metadata preservation tested'); console.log('✓ PDF metadata extraction tested'); console.log('✓ Format detection and preservation tested'); console.log(`\n✓ Metadata preservation testing completed successfully.`); }); tap.start();