/** Offline synthetic-response reading exercise, not a schema validator or SDK. */ import { readFileSync } from 'node:fs'; import { pathToFileURL } from 'node:url'; type JsonObject = Record; type ResponseInput = { data: { outcome: string; entity?: { subject: { entity_id: string } }; identity_assessment: unknown; recovery: JsonObject & { automatic_retry_allowed: unknown }; }; receipt: JsonObject; }; export function summarise(response: ResponseInput): JsonObject { const { data, receipt } = response; const outcome = data.outcome; const units = receipt.charged_units; const recovery = data.recovery; if (!['established', 'ambiguous', 'no_match', 'insufficient_evidence'].includes(outcome)) throw new Error('Unknown outcome: stop interpretation and check the contract'); if (typeof units !== 'string' || !/^(0|[1-9][0-9]*)$/.test(units)) throw new Error('Atomic units must remain canonical decimal strings'); if (recovery.automatic_retry_allowed !== false) throw new Error('This exercise cannot authorise an automatic retry'); return { synthetic: true, outcome, entity_id: outcome === 'established' ? data.entity!.subject.entity_id : null, identity_assessment: data.identity_assessment, recovery, receipt: Object.fromEntries( ['receipt_id', 'operation_id', 'charged_units', 'unit', 'unit_scale', 'settlement'] .map(key => [key, receipt[key]]) ), }; } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { const responses = JSON.parse(readFileSync(process.argv[2], 'utf8')); console.log(JSON.stringify(Object.fromEntries(Object.entries(responses) .map(([name, value]) => [name, summarise(value as ResponseInput)])), null, 2)); }