import assert from 'node:assert/strict' import test from 'node:test' import { validDirectDeliveryReplayProof } from './direct-delivery-replay-proof.mjs' const eventId = 'd54a246f-c71c-462f-ab5b-a44f0e23d29a' const payloadHash = `sha256:${'a'.repeat(64)}` const scope = { marker: 'current-run', customerId: '7', loadingPlanId: '99', containerBookingId: '66' } function proof() { return { ...scope, readOnly: true, restored: true, producer: { eventId, payloadHash, entryId: '88', publishStatus: 'PUBLISHED', eventType: 'FirstLegLoadingPlanCompleted' }, consumer: { eventId, payloadHash, status: 'PROCESSED', count: 1 }, attempts: Array.from({ length: 13 }, (_, i) => ({ attemptId: `attempt-${i}`, eventId, entryId: '88', targetName: 'lastleg-service/internal/lastleg/events', outcome: i === 12 ? 'DUPLICATE' : i === 11 ? 'DEAD' : 'RETRY' })), proxyRecords: Array.from({ length: 13 }, (_, i) => ({ eventId, payloadHash, matched: true, outcome: i === 0 ? 'APPLIED' : 'DUPLICATE', errorCode: i === 12 ? null : 'ACK_LOSS_INJECTED' })), retryAudit: { eventId, payloadHash, entryId: '88', previousFailures: 12, requestId: 'retry-1' }, beforeRetry: { eventId, payloadHash, publishStatus: 'DEAD', failedAttempts: 12, businessOrderIds: ['11', '12'] }, afterRetry: { eventId, payloadHash, publishStatus: 'PUBLISHED', businessOrderIds: ['12', '11'] } } } test('only current original event finite failure, authenticated recovery and persistent duplicate consumption together pass', () => { assert.equal(validDirectDeliveryReplayProof(proof(), scope), true) }) test('old evidence, GET stability, incomplete attempts, hash drift and business duplication fail closed', () => { const changes = [ p => { p.marker = 'old' }, p => { p.attempts = [] }, p => { p.proxyRecords = [] }, p => { p.consumer.count = 2 }, p => { p.attempts[12].outcome = 'APPLIED' }, p => { p.retryAudit.payloadHash = `sha256:${'b'.repeat(64)}` }, p => { p.afterRetry.businessOrderIds.push('13') }, p => { p.restored = false }, p => { p.attempts[11].outcome = 'RETRY' }, p => { p.beforeRetry.failedAttempts = 11 }, p => { p.attempts[12].targetName = 'lastleg-service/internal/finance/events' }, ] for (const change of changes) { const p = proof(); change(p) assert.equal(validDirectDeliveryReplayProof(p, scope), false) } })