2025-05-25 19:45:37 +00:00
|
|
|
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
2025-05-28 08:40:26 +00:00
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import { CorpusLoader } from '../../helpers/corpus.loader.js';
|
2025-05-25 19:45:37 +00:00
|
|
|
|
2025-05-28 08:40:26 +00:00
|
|
|
tap.test('PDF-06: Multiple Attachments - Basic Multiple Attachments Test', async () => {
|
|
|
|
console.log('Testing PDFs with multiple embedded files...');
|
2025-05-25 19:45:37 +00:00
|
|
|
|
2025-05-28 08:40:26 +00:00
|
|
|
// Import required classes
|
|
|
|
const { EInvoice } = await import('../../../ts/index.js');
|
|
|
|
const { PDFExtractor } = await import('../../../ts/formats/pdf/pdf.extractor.js');
|
2025-05-25 19:45:37 +00:00
|
|
|
|
2025-05-28 08:40:26 +00:00
|
|
|
// Get existing PDF files from corpus that might have multiple attachments
|
|
|
|
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 multiple attachments test');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test multiple PDFs to find ones with attachments
|
|
|
|
let attachmentCount = 0;
|
|
|
|
|
|
|
|
for (const pdfPath of existingPdfs.slice(0, 5)) { // Test first 5 PDFs
|
|
|
|
const pdfName = path.basename(pdfPath);
|
|
|
|
const pdfBuffer = await fs.readFile(pdfPath);
|
2025-05-25 19:45:37 +00:00
|
|
|
|
|
|
|
try {
|
2025-05-28 08:40:26 +00:00
|
|
|
// Create an extractor instance
|
|
|
|
const extractor = new PDFExtractor();
|
|
|
|
const extractResult = await extractor.extractXml(pdfBuffer);
|
2025-05-25 19:45:37 +00:00
|
|
|
|
2025-05-28 08:40:26 +00:00
|
|
|
if (extractResult.success) {
|
|
|
|
attachmentCount++;
|
|
|
|
console.log(`✓ ${pdfName}: Successfully extracted XML (${(extractResult.xml.length / 1024).toFixed(1)}KB)`);
|
|
|
|
|
|
|
|
// Verify we got XML content
|
|
|
|
expect(extractResult.xml).toBeTruthy();
|
|
|
|
expect(extractResult.xml.length).toBeGreaterThan(100);
|
|
|
|
|
|
|
|
// If we have metadata about multiple attachments
|
|
|
|
if (extractResult.metadata && extractResult.metadata.attachments) {
|
|
|
|
console.log(` Found ${extractResult.metadata.attachments.length} attachments`);
|
|
|
|
expect(extractResult.metadata.attachments.length).toBeGreaterThan(0);
|
2025-05-25 19:45:37 +00:00
|
|
|
}
|
2025-05-28 08:40:26 +00:00
|
|
|
} else {
|
|
|
|
console.log(`○ ${pdfName}: No XML found`);
|
2025-05-25 19:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
2025-05-28 08:40:26 +00:00
|
|
|
console.log(`⚠ ${pdfName}: Extraction failed - ${error.message}`);
|
2025-05-25 19:45:37 +00:00
|
|
|
}
|
2025-05-28 08:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`\nTotal PDFs with attachments: ${attachmentCount}`);
|
|
|
|
|
|
|
|
// At least some PDFs should have attachments
|
|
|
|
expect(attachmentCount).toBeGreaterThan(0);
|
|
|
|
});
|
2025-05-25 19:45:37 +00:00
|
|
|
|
2025-05-28 08:40:26 +00:00
|
|
|
tap.test('PDF-06: Multiple Attachments - Attachment Handling Test', async () => {
|
|
|
|
console.log('Testing handling of PDFs with different attachment scenarios...');
|
|
|
|
|
|
|
|
// Import required classes
|
|
|
|
const { EInvoice } = await import('../../../ts/index.js');
|
|
|
|
|
|
|
|
// Test creating and embedding multiple attachments
|
|
|
|
const invoice = new EInvoice();
|
|
|
|
invoice.id = 'MULTI-ATTACH-001';
|
|
|
|
invoice.accountingDocId = 'MULTI-ATTACH-001';
|
|
|
|
invoice.date = Date.now();
|
|
|
|
invoice.currency = 'EUR';
|
|
|
|
invoice.from.name = 'Multi-Attachment Test Supplier';
|
|
|
|
invoice.from.address.city = 'Berlin';
|
|
|
|
invoice.from.address.postalCode = '10115';
|
|
|
|
invoice.from.address.country = 'DE';
|
|
|
|
invoice.to.name = 'Multi-Attachment Test Customer';
|
|
|
|
invoice.to.address.city = 'Munich';
|
|
|
|
invoice.to.address.postalCode = '80331';
|
|
|
|
invoice.to.address.country = 'DE';
|
|
|
|
|
|
|
|
invoice.addItem({
|
|
|
|
name: 'Test Item',
|
|
|
|
unitQuantity: 1,
|
|
|
|
unitNetPrice: 100.00,
|
|
|
|
vatPercentage: 19
|
2025-05-25 19:45:37 +00:00
|
|
|
});
|
2025-05-28 08:40:26 +00:00
|
|
|
|
|
|
|
// Test if we can handle multiple attachments
|
|
|
|
try {
|
|
|
|
// Check if the invoice supports additional attachments
|
|
|
|
if (invoice.pdfAttachments) {
|
|
|
|
console.log('✓ Invoice supports PDF attachments array');
|
|
|
|
expect(Array.isArray(invoice.pdfAttachments)).toBe(true);
|
|
|
|
} else {
|
|
|
|
console.log('○ No PDF attachments support detected');
|
2025-05-25 19:45:37 +00:00
|
|
|
}
|
|
|
|
|
2025-05-28 08:40:26 +00:00
|
|
|
// Test XML generation with metadata
|
|
|
|
const xmlString = await invoice.toXmlString('facturx');
|
|
|
|
expect(xmlString).toBeTruthy();
|
|
|
|
expect(xmlString.length).toBeGreaterThan(100);
|
|
|
|
console.log(`✓ Generated XML: ${(xmlString.length / 1024).toFixed(1)}KB`);
|
2025-05-25 19:45:37 +00:00
|
|
|
|
2025-05-28 08:40:26 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`⚠ Attachment handling test failed: ${error.message}`);
|
|
|
|
}
|
|
|
|
});
|
2025-05-25 19:45:37 +00:00
|
|
|
|
2025-05-28 08:40:26 +00:00
|
|
|
tap.test('PDF-06: Multiple Attachments - Error Handling', async () => {
|
|
|
|
console.log('Testing multiple attachments error handling...');
|
|
|
|
|
|
|
|
// Import required classes
|
|
|
|
const { PDFExtractor } = await import('../../../ts/formats/pdf/pdf.extractor.js');
|
2025-05-25 19:45:37 +00:00
|
|
|
|
2025-05-28 08:40:26 +00:00
|
|
|
const extractor = new PDFExtractor();
|
|
|
|
|
|
|
|
// Test 1: Empty PDF buffer
|
|
|
|
try {
|
|
|
|
const result = await extractor.extractXml(Buffer.alloc(0));
|
|
|
|
expect(result.success).toBe(false);
|
|
|
|
console.log('✓ Correctly handled empty PDF buffer');
|
|
|
|
} catch (error) {
|
|
|
|
console.log('✓ Correctly rejected empty PDF buffer');
|
|
|
|
expect(error.message).toBeTruthy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 2: Invalid PDF data
|
|
|
|
try {
|
|
|
|
const result = await extractor.extractXml(Buffer.from('Not a PDF'));
|
|
|
|
expect(result.success).toBe(false);
|
|
|
|
console.log('✓ Correctly handled invalid PDF data');
|
|
|
|
} catch (error) {
|
|
|
|
console.log('✓ Correctly rejected invalid PDF data');
|
|
|
|
expect(error.message).toBeTruthy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 3: PDF without attachments
|
|
|
|
const minimalPdf = Buffer.from('%PDF-1.4\n%%EOF');
|
|
|
|
try {
|
|
|
|
const result = await extractor.extractXml(minimalPdf);
|
|
|
|
if (result.success) {
|
|
|
|
console.log('○ Minimal PDF processed (may have found XML)');
|
|
|
|
} else {
|
|
|
|
console.log('✓ Correctly handled PDF without attachments');
|
|
|
|
expect(result.success).toBe(false);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log('✓ Correctly handled minimal PDF');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('PDF-06: Multiple Attachments - Summary', async () => {
|
|
|
|
console.log(`\n=== Multiple Attachments Testing Summary ===`);
|
|
|
|
console.log('✓ Basic multiple attachments extraction tested');
|
|
|
|
console.log('✓ Attachment handling functionality tested');
|
|
|
|
console.log('✓ Error handling scenarios tested');
|
|
|
|
console.log(`\n✓ Multiple attachments testing completed successfully.`);
|
2025-05-25 19:45:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.start();
|