137 lines
4.1 KiB
TypeScript
137 lines
4.1 KiB
TypeScript
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
|
import { EInvoice } from '../../../ts/index.js';
|
|
import { PerformanceTracker } from '../../helpers/performance.tracker.js';
|
|
|
|
tap.test('ERR-01: Parsing Recovery - should recover from XML parsing errors', async () => {
|
|
// ERR-01: Test error handling for parsing recovery
|
|
|
|
// Test 1: Basic error handling
|
|
console.log('\nTest 1: Basic parsing recovery handling');
|
|
const { result: basicResult, metric: basicMetric } = await PerformanceTracker.track(
|
|
'err01-basic',
|
|
async () => {
|
|
let errorCaught = false;
|
|
let errorMessage = '';
|
|
|
|
try {
|
|
// Simulate error scenario
|
|
const einvoice = new EInvoice();
|
|
|
|
// Try to load invalid content based on test type
|
|
await einvoice.fromXmlString('<invalid>xml</not-closed>');
|
|
|
|
} catch (error) {
|
|
errorCaught = true;
|
|
errorMessage = error.message || 'Unknown error';
|
|
console.log(` Error caught: ${errorMessage}`);
|
|
}
|
|
|
|
return {
|
|
success: errorCaught,
|
|
errorMessage,
|
|
gracefulHandling: errorCaught && !errorMessage.includes('FATAL')
|
|
};
|
|
}
|
|
);
|
|
|
|
console.log(` Basic error handling completed in ${basicMetric.duration}ms`);
|
|
console.log(` Error was caught: ${basicResult.success}`);
|
|
console.log(` Graceful handling: ${basicResult.gracefulHandling}`);
|
|
|
|
// Test 2: Recovery mechanism
|
|
console.log('\nTest 2: Recovery after error');
|
|
const { result: recoveryResult, metric: recoveryMetric } = await PerformanceTracker.track(
|
|
'err01-recovery',
|
|
async () => {
|
|
const einvoice = new EInvoice();
|
|
|
|
// First cause an error
|
|
try {
|
|
await einvoice.fromXmlString('<invalid>xml</not-closed>');
|
|
} catch (error) {
|
|
// Expected error
|
|
}
|
|
|
|
// Now try normal operation
|
|
einvoice.id = 'RECOVERY-TEST';
|
|
einvoice.issueDate = new Date(2025, 0, 25);
|
|
einvoice.invoiceId = 'RECOVERY-TEST';
|
|
einvoice.accountingDocId = 'RECOVERY-TEST';
|
|
|
|
einvoice.from = {
|
|
type: 'company',
|
|
name: 'Test Company',
|
|
description: 'Testing error recovery',
|
|
address: {
|
|
streetName: 'Test Street',
|
|
houseNumber: '1',
|
|
postalCode: '12345',
|
|
city: 'Test City',
|
|
country: 'DE'
|
|
},
|
|
status: 'active',
|
|
foundedDate: { year: 2020, month: 1, day: 1 },
|
|
registrationDetails: {
|
|
vatId: 'DE123456789',
|
|
registrationId: 'HRB 12345',
|
|
registrationName: 'Commercial Register'
|
|
}
|
|
};
|
|
|
|
einvoice.to = {
|
|
type: 'person',
|
|
name: 'Test',
|
|
surname: 'Customer',
|
|
salutation: 'Mr' as const,
|
|
sex: 'male' as const,
|
|
title: 'Doctor' as const,
|
|
description: 'Test customer',
|
|
address: {
|
|
streetName: 'Customer Street',
|
|
houseNumber: '2',
|
|
postalCode: '54321',
|
|
city: 'Customer City',
|
|
country: 'DE'
|
|
}
|
|
};
|
|
|
|
einvoice.items = [{
|
|
position: 1,
|
|
name: 'Test Product',
|
|
articleNumber: 'TEST-001',
|
|
unitType: 'EA',
|
|
unitQuantity: 1,
|
|
unitNetPrice: 100,
|
|
vatPercentage: 19
|
|
}];
|
|
|
|
// Try to export after error
|
|
let canRecover = false;
|
|
try {
|
|
const xml = await einvoice.toXmlString('ubl');
|
|
canRecover = xml.includes('RECOVERY-TEST');
|
|
} catch (error) {
|
|
canRecover = false;
|
|
}
|
|
|
|
return { success: canRecover };
|
|
}
|
|
);
|
|
|
|
console.log(` Recovery test completed in ${recoveryMetric.duration}ms`);
|
|
console.log(` Can recover after error: ${recoveryResult.success}`);
|
|
|
|
// Summary
|
|
console.log('\n=== Parsing Recovery Error Handling Summary ===');
|
|
console.log(`Error Detection: ${basicResult.success ? 'Working' : 'Failed'}`);
|
|
console.log(`Graceful Handling: ${basicResult.gracefulHandling ? 'Yes' : 'No'}`);
|
|
console.log(`Recovery: ${recoveryResult.success ? 'Successful' : 'Failed'}`);
|
|
|
|
// Test passes if errors are caught gracefully
|
|
expect(basicResult.success).toBeTrue();
|
|
expect(recoveryResult.success).toBeTrue();
|
|
});
|
|
|
|
// Run the test
|
|
tap.start();
|