import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as plugins from '../../../ts/plugins.ts';
import { EInvoice } from '../../../ts/classes.xinvoice.ts';
import { CorpusLoader } from '../../helpers/corpus.loader.ts';
import { PerformanceTracker } from '../../helpers/performance.tracker.ts';
const testTimeout = 300000; // 5 minutes timeout for corpus processing
// VAL-10: Business Level Validation
// Tests business logic validation including invoice totals, tax calculations,
// payment terms, and business rule compliance
tap.test('VAL-10: Business Level Validation - Invoice Totals Consistency', async (tools) => {
const startTime = Date.now();
const totalConsistencyTests = [
{
name: 'Correct Total Calculation',
xml: `
TEST-001
2024-01-01
380
EUR
1
2
100.00
50.00
19.00
100.00
19.00
19.00
100.00
100.00
119.00
119.00
`,
valid: true
},
{
name: 'Incorrect Line Total',
xml: `
TEST-001
2024-01-01
380
EUR
1
2
150.00
50.00
150.00
150.00
150.00
`,
valid: false
}
];
for (const test of totalConsistencyTests) {
try {
const invoice = new EInvoice();
const parseResult = await invoice.fromXmlString(test.xml);
if (parseResult) {
const validationResult = await invoice.validate();
if (test.valid) {
expect(validationResult.valid).toBe(true);
tools.log(`✓ ${test.name}: Valid business logic accepted`);
} else {
expect(validationResult.valid).toBe(false);
tools.log(`✓ ${test.name}: Invalid business logic rejected`);
}
} else if (!test.valid) {
tools.log(`✓ ${test.name}: Invalid invoice rejected at parse time`);
}
} catch (error) {
if (!test.valid) {
tools.log(`✓ ${test.name}: Invalid business logic properly rejected: ${error.message}`);
} else {
throw error;
}
}
}
const duration = Date.now() - startTime;
PerformanceTracker.recordMetric('business-validation-totals', duration);
});
tap.test('VAL-10: Business Level Validation - Tax Calculation Consistency', async (tools) => {
const startTime = Date.now();
const taxCalculationTests = [
{
name: 'Standard VAT Calculation (19%)',
baseAmount: 100.00,
taxRate: 19.00,
expectedTax: 19.00,
valid: true
},
{
name: 'Zero VAT Calculation',
baseAmount: 100.00,
taxRate: 0.00,
expectedTax: 0.00,
valid: true
},
{
name: 'Reduced VAT Calculation (7%)',
baseAmount: 100.00,
taxRate: 7.00,
expectedTax: 7.00,
valid: true
},
{
name: 'Incorrect Tax Amount',
baseAmount: 100.00,
taxRate: 19.00,
expectedTax: 20.00,
valid: false
},
{
name: 'Rounding Edge Case',
baseAmount: 33.33,
taxRate: 19.00,
expectedTax: 6.33, // Should round correctly
valid: true
}
];
for (const test of taxCalculationTests) {
const xml = `
TEST-TAX-${test.taxRate}
2024-01-01
380
EUR
${test.expectedTax.toFixed(2)}
${test.baseAmount.toFixed(2)}
${test.expectedTax.toFixed(2)}
${test.taxRate.toFixed(2)}
${test.baseAmount.toFixed(2)}
${(test.baseAmount + test.expectedTax).toFixed(2)}
${(test.baseAmount + test.expectedTax).toFixed(2)}
`;
try {
const invoice = new EInvoice();
const parseResult = await invoice.fromXmlString(xml);
if (parseResult) {
const validationResult = await invoice.validate();
if (test.valid) {
// For valid tests, we expect successful validation or minor rounding tolerance
if (!validationResult.valid) {
// Check if it's just a rounding issue
const errors = validationResult.errors || [];
const hasOnlyRoundingErrors = errors.every(error =>
error.message.toLowerCase().includes('rounding') ||
error.message.toLowerCase().includes('precision')
);
if (!hasOnlyRoundingErrors) {
tools.log(`Validation failed for ${test.name}: ${errors.map(e => e.message).join(', ')}`);
}
}
tools.log(`✓ ${test.name}: Tax calculation processed`);
} else {
expect(validationResult.valid).toBe(false);
tools.log(`✓ ${test.name}: Invalid tax calculation rejected`);
}
}
} catch (error) {
if (!test.valid) {
tools.log(`✓ ${test.name}: Invalid calculation properly rejected: ${error.message}`);
} else {
tools.log(`⚠ ${test.name}: Unexpected error: ${error.message}`);
}
}
}
const duration = Date.now() - startTime;
PerformanceTracker.recordMetric('business-validation-tax', duration);
});
tap.test('VAL-10: Business Level Validation - Payment Terms Validation', async (tools) => {
const startTime = Date.now();
const paymentTermsTests = [
{
name: 'Valid Due Date (30 days)',
issueDate: '2024-01-01',
dueDate: '2024-01-31',
paymentTerms: 'Net 30 days',
valid: true
},
{
name: 'Due Date Before Issue Date',
issueDate: '2024-01-31',
dueDate: '2024-01-01',
paymentTerms: 'Immediate',
valid: false
},
{
name: 'Same Day Payment',
issueDate: '2024-01-01',
dueDate: '2024-01-01',
paymentTerms: 'Due on receipt',
valid: true
},
{
name: 'Extended Payment Terms (90 days)',
issueDate: '2024-01-01',
dueDate: '2024-03-31',
paymentTerms: 'Net 90 days',
valid: true
}
];
for (const test of paymentTermsTests) {
const xml = `
TEST-PAYMENT-${Date.now()}
${test.issueDate}
${test.dueDate}
380
EUR
${test.paymentTerms}
100.00
`;
try {
const invoice = new EInvoice();
const parseResult = await invoice.fromXmlString(xml);
if (parseResult) {
const validationResult = await invoice.validate();
if (test.valid) {
// Valid payment terms should be accepted
tools.log(`✓ ${test.name}: Valid payment terms accepted`);
} else {
expect(validationResult.valid).toBe(false);
tools.log(`✓ ${test.name}: Invalid payment terms rejected`);
}
}
} catch (error) {
if (!test.valid) {
tools.log(`✓ ${test.name}: Invalid payment terms properly rejected: ${error.message}`);
} else {
tools.log(`⚠ ${test.name}: Unexpected error: ${error.message}`);
}
}
}
const duration = Date.now() - startTime;
PerformanceTracker.recordMetric('business-validation-payment', duration);
});
tap.test('VAL-10: Business Level Validation - Business Rules Compliance', async (tools) => {
const startTime = Date.now();
// Test EN16931 business rules at business level
const businessRuleTests = [
{
name: 'BR-01: Invoice must have an identifier',
xml: `
INV-2024-001
2024-01-01
380
`,
valid: true
},
{
name: 'BR-01 Violation: Missing invoice identifier',
xml: `
2024-01-01
380
`,
valid: false
},
{
name: 'BR-02: Invoice must have an issue date',
xml: `
INV-2024-001
2024-01-01
380
`,
valid: true
},
{
name: 'BR-02 Violation: Missing issue date',
xml: `
INV-2024-001
380
`,
valid: false
}
];
for (const test of businessRuleTests) {
try {
const invoice = new EInvoice();
const parseResult = await invoice.fromXmlString(test.xml);
if (parseResult) {
const validationResult = await invoice.validate();
if (test.valid) {
expect(validationResult.valid).toBe(true);
tools.log(`✓ ${test.name}: Business rule compliance verified`);
} else {
expect(validationResult.valid).toBe(false);
tools.log(`✓ ${test.name}: Business rule violation detected`);
}
} else if (!test.valid) {
tools.log(`✓ ${test.name}: Invalid invoice rejected at parse time`);
}
} catch (error) {
if (!test.valid) {
tools.log(`✓ ${test.name}: Business rule violation properly caught: ${error.message}`);
} else {
throw error;
}
}
}
const duration = Date.now() - startTime;
PerformanceTracker.recordMetric('business-validation-rules', duration);
});
tap.test('VAL-10: Business Level Validation - Multi-Line Invoice Logic', async (tools) => {
const startTime = Date.now();
// Test complex multi-line invoice business logic
const multiLineXml = `
MULTI-LINE-001
2024-01-01
380
EUR
1
2
100.00
-
Product A
19.00
50.00
2
1
75.00
-
Product B
7.00
75.00
24.25
100.00
19.00
19.00
75.00
5.25
7.00
175.00
175.00
199.25
199.25
`;
try {
const invoice = new EInvoice();
const parseResult = await invoice.fromXmlString(multiLineXml);
expect(parseResult).toBeTruthy();
const validationResult = await invoice.validate();
// Multi-line business logic should be valid
if (!validationResult.valid) {
tools.log(`Multi-line validation issues: ${validationResult.errors?.map(e => e.message).join(', ')}`);
}
tools.log(`✓ Multi-line invoice business logic validation completed`);
} catch (error) {
tools.log(`Multi-line invoice test failed: ${error.message}`);
throw error;
}
const duration = Date.now() - startTime;
PerformanceTracker.recordMetric('business-validation-multiline', duration);
});
tap.test('VAL-10: Business Level Validation - Corpus Business Logic', { timeout: testTimeout }, async (tools) => {
const startTime = Date.now();
let processedFiles = 0;
let validBusinessLogic = 0;
let businessLogicErrors = 0;
try {
const ciiFiles = await CorpusLoader.getFiles('CII_XML_RECHNUNG');
for (const filePath of ciiFiles.slice(0, 8)) { // Process first 8 files
try {
const invoice = new EInvoice();
const parseResult = await invoice.fromFile(filePath);
processedFiles++;
if (parseResult) {
const validationResult = await invoice.validate();
if (validationResult.valid) {
validBusinessLogic++;
} else {
// Check for business logic specific errors
const businessErrorTypes = ['total', 'calculation', 'tax', 'payment', 'rule'];
const hasBusinessErrors = validationResult.errors?.some(error =>
businessErrorTypes.some(type => error.message.toLowerCase().includes(type))
);
if (hasBusinessErrors) {
businessLogicErrors++;
tools.log(`Business logic errors in ${plugins.path.basename(filePath)}`);
}
}
}
} catch (error) {
tools.log(`Failed to process ${plugins.path.basename(filePath)}: ${error.message}`);
}
}
const businessLogicSuccessRate = processedFiles > 0 ? (validBusinessLogic / processedFiles) * 100 : 0;
const businessErrorRate = processedFiles > 0 ? (businessLogicErrors / processedFiles) * 100 : 0;
tools.log(`Business logic validation completed:`);
tools.log(`- Processed: ${processedFiles} files`);
tools.log(`- Valid business logic: ${validBusinessLogic} files (${businessLogicSuccessRate.toFixed(1)}%)`);
tools.log(`- Business logic errors: ${businessLogicErrors} files (${businessErrorRate.toFixed(1)}%)`);
// Business logic should have reasonable success rate
expect(businessLogicSuccessRate).toBeGreaterThan(60);
} catch (error) {
tools.log(`Corpus business validation failed: ${error.message}`);
throw error;
}
const totalDuration = Date.now() - startTime;
PerformanceTracker.recordMetric('business-validation-corpus', totalDuration);
expect(totalDuration).toBeLessThan(120000); // 2 minutes max
tools.log(`Business validation performance: ${totalDuration}ms total`);
});
tap.test('VAL-10: Performance Summary', async (tools) => {
const operations = [
'business-validation-totals',
'business-validation-tax',
'business-validation-payment',
'business-validation-rules',
'business-validation-multiline',
'business-validation-corpus'
];
for (const operation of operations) {
const summary = await PerformanceTracker.getSummary(operation);
if (summary) {
tools.log(`${operation}: avg=${summary.average}ms, min=${summary.min}ms, max=${summary.max}ms, p95=${summary.p95}ms`);
}
}
});