import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import { TEST_DIRECT_DELIVERY_URLS, INTERNAL_TOKEN_COHORT_SERVICES, validDirectDeliveryEnvironment, validInternalTokenCohort, validDirectDeliveryEvidence } from './event-bridge-pending-recovery.mjs' const read=p=>readFileSync(new URL(p,import.meta.url),'utf8') const compose=read('../../deploy/docker-compose.yml') test('all three direct targets use Compose DNS and current internal ports',()=>{ assert.equal(validDirectDeliveryEnvironment(TEST_DIRECT_DELIVERY_URLS),true) for(const [key,url] of Object.entries(TEST_DIRECT_DELIVERY_URLS)){ assert.ok(compose.includes(`${key}: ${url}`)) assert.equal(validDirectDeliveryEnvironment({...TEST_DIRECT_DELIVERY_URLS,[key]:'http://localhost:9312'}),false) } assert.doesNotMatch(compose,/EVENT_BRIDGE_URL|event-bridge-service:/) }) test('the three producers and consumers share internal token revision without exposing secrets',()=>{ assert.deepEqual(INTERNAL_TOKEN_COHORT_SERVICES,['firstleg','lastleg-service','finance']) assert.match(compose,/INTERNAL_SERVICE_TOKEN: \$\{INTERNAL_SERVICE_TOKEN\}/) assert.equal(validInternalTokenCohort({firstleg:'r1','lastleg-service':'r1',finance:'r1'}),true) assert.equal(validInternalTokenCohort({firstleg:'r1','lastleg-service':'r1',finance:'r2'}),false) assert.equal(validInternalTokenCohort({}),false) }) const attempt=(extra={})=>({eventId:'event-fixture',payloadHash:'hash-fixture',at:'2026-09-29T00:00:00Z',requestId:'request-fixture',traceId:'trace-fixture',...extra}) const facts=(attempts,extra={})=>({marker:'run-fixture',eventId:'event-fixture',payloadHash:'hash-fixture',maxAttempts:12,sqlMutationCount:0,status:'SENT',inboxEventCount:1,businessEffectCount:1,attempts,...extra}) test('DNS, invalid token and timeout recovery require original event identity and actual duplicate ACK',()=>{ for(const failure of ['DNS_UNAVAILABLE','TOKEN_REJECTED','TIMEOUT']){ const evidence=facts([attempt({failure}),attempt({outcome:'APPLIED',ackLost:true}),attempt({outcome:'DUPLICATE'})],{authorizedDeadRetry:failure==='TOKEN_REJECTED'}) assert.equal(validDirectDeliveryEvidence(evidence),true) assert.equal(validDirectDeliveryEvidence({...evidence,businessEffectCount:2}),false) assert.equal(validDirectDeliveryEvidence({...evidence,sqlMutationCount:1}),false) assert.equal(validDirectDeliveryEvidence({...evidence,attempts:[...evidence.attempts,attempt({eventId:'new-id',outcome:'DUPLICATE'})]}),false) } assert.equal(validDirectDeliveryEvidence(facts([attempt({outcome:'APPLIED',ackLost:true}),attempt({outcome:'APPLIED',ackLost:true})])),false) }) test('retry budget exhaustion stays DEAD and never schedules an automatic retry',()=>{ const evidence=facts(Array.from({length:12},()=>attempt({failure:'TIMEOUT'})),{status:'DEAD',automaticRetryScheduled:false}) assert.equal(validDirectDeliveryEvidence(evidence),true) assert.equal(validDirectDeliveryEvidence({...evidence,automaticRetryScheduled:true}),false) assert.equal(validDirectDeliveryEvidence({...evidence,attempts:[...evidence.attempts,attempt({failure:'TIMEOUT'})]}),false) }) test('GET stability does not claim real event replay coverage',()=>{ const source=read('./phase4-cross-region-events.mjs') assert.match(source,/非事件重放证据/) assert.match(source,/missing-direct-delivery-replay-evidence/) assert.match(source,/event-outbox-operations\.mjs/) assert.doesNotMatch(source,/经Bridge|不能访问.*Audience/) }) test('permanent token failure stops immediately and recovery needs authorized DEAD retry', () => { const stopped=facts([attempt({failure:'TOKEN_REJECTED'})],{status:'DEAD',automaticRetryScheduled:false}) assert.equal(validDirectDeliveryEvidence(stopped),true) const recovered=facts([attempt({failure:'TOKEN_REJECTED'}),attempt({outcome:'APPLIED',ackLost:true}),attempt({outcome:'DUPLICATE'})]) assert.equal(validDirectDeliveryEvidence(recovered),false) assert.equal(validDirectDeliveryEvidence({...recovered,authorizedDeadRetry:true}),true) })