import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as plugins from '../plugins.js';
import { EInvoice } from '../../../ts/index.js';
import { CorpusLoader } from '../corpus.loader.js';
import { PerformanceTracker } from '../performance.tracker.js';
tap.test('ENC-08: Mixed Content Encoding - should handle mixed content (text and elements) correctly', async (t) => {
// ENC-08: Verify proper encoding of mixed content scenarios
// This test ensures text nodes, elements, CDATA, and comments are properly encoded together
const performanceTracker = new PerformanceTracker('ENC-08: Mixed Content');
const corpusLoader = new CorpusLoader();
t.test('Basic mixed content', async () => {
const startTime = performance.now();
const xmlContent = `
2.1
MIXED-BASIC-001
This invoice includes important payment terms:
Net 30 days with 2% early payment discount.
Please pay by 2025-02-25.
Payment due in 30 days.
If paid within 10 days: 2% discount
If paid after 30 days: 1.5% interest
Item includes 10 units of Widget A
at €9.99 each.
Total: €99.90
`;
const einvoice = new EInvoice();
await einvoice.loadFromString(xmlContent);
const xmlString = einvoice.getXmlString();
// Verify mixed content is preserved
expect(xmlString).toContain('This invoice includes');
expect(xmlString).toContain('important');
expect(xmlString).toContain('payment terms:');
expect(xmlString).toContain('Net 30 days');
expect(xmlString).toContain('with');
expect(xmlString).toContain('2%');
expect(xmlString).toContain('Please pay by');
expect(xmlString).toContain('2025-02-25');
// Verify nested mixed content
expect(xmlString).toContain('If paid within');
expect(xmlString).toContain('10');
expect(xmlString).toContain('days:');
expect(xmlString).toContain('2%');
const elapsed = performance.now() - startTime;
performanceTracker.addMeasurement('basic-mixed', elapsed);
});
t.test('Mixed content with special characters', async () => {
const startTime = performance.now();
const xmlContent = `
2.1
MIXED-SPECIAL-001
Price: 100.00 € (VAT 19% = 19.00 €)
Total: 119.00 € for Müller & Söhne GmbH
See contract [§12.3] for terms & conditions.
Payment < 30 days required.
Contact: info@müller-söhne.de
≥ 100 items → 5% discount
> 30 days → 1.5% interest
Total = Price × Quantity × (1 + VAT%)
`;
const einvoice = new EInvoice();
await einvoice.loadFromString(xmlContent);
const xmlString = einvoice.getXmlString();
// Verify special characters in mixed content
expect(xmlString).toContain('Price:');
expect(xmlString).toContain('€');
expect(xmlString).toContain('Müller & Söhne GmbH');
expect(xmlString).toContain('§12.3');
expect(xmlString).toContain('terms & conditions');
expect(xmlString).toContain('< 30 days');
expect(xmlString).toContain('info@müller-söhne.de');
expect(xmlString).toContain('≥ 100 items → 5% discount');
expect(xmlString).toContain('> 30 days → 1.5% interest');
expect(xmlString).toContain('×');
const elapsed = performance.now() - startTime;
performanceTracker.addMeasurement('special-mixed', elapsed);
});
t.test('Mixed content with CDATA sections', async () => {
const startTime = performance.now();
const xmlContent = `
2.1
MIXED-CDATA-001
Regular text before CDATA.
tags & special chars: < > & " ']]>
Text after CDATA with nested element.
HTML content example:
Invoice Details
Amount: €100.00
VAT: 19%