einvoice/test/suite/einvoice_error-handling/test.err-09.transformation-errors.ts
2025-05-27 19:30:07 +00:00

139 lines
4.2 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-09: Transformation Errors - should handle transformation errors', async () => {
// ERR-09: Test error handling for transformation errors
// Test 1: Basic error handling
console.log('\nTest 1: Basic transformation errors handling');
const { result: basicResult, metric: basicMetric } = await PerformanceTracker.track(
'err09-basic',
async () => {
let errorCaught = false;
let errorMessage = '';
try {
// Simulate error scenario
const einvoice = new EInvoice();
// Try to load invalid content based on test type
// Invalid format transformation
await einvoice.toXmlString('invalid-format' as any);
} 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(
'err09-recovery',
async () => {
const einvoice = new EInvoice();
// First cause an error
try {
// Invalid format transformation
await einvoice.toXmlString('invalid-format' as any);
} 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=== Transformation Errors 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();