import { expect, test } from 'bun:test'; import { parseEngineFinancialCommand, parseFinancialDecimal, parseFinancialEffects, parseFinancialSnapshot, parseFinancialStrategyDecision, } from '../../libs/core/types/src/financial-engine'; import contracts from '../fixtures/economics/contracts.json'; import ledgerSpecs from '../fixtures/economics/ledger-specs.json'; import scalars from '../fixtures/economics/scalars.json'; function parse(kind: string, value: unknown): unknown { switch (kind) { case 'command': return parseEngineFinancialCommand(value); case 'decision': return parseFinancialStrategyDecision(value); case 'snapshot': return parseFinancialSnapshot(value); case 'effects': return parseFinancialEffects(value); default: throw new Error(`Unknown contract fixture ${kind}`); } } function annotate(value: { id: string; owningTask: string; assumptions: string; handCalculation: string; }) { for (const key of ['id', 'owningTask', 'assumptions', 'handCalculation'] as const) { expect(value[key].length).toBeGreaterThan(0); } } test('rust_ts_fixture_parse_parity: shared scalar and versioned wire fixtures', () => { for (const fixture of scalars.cases) { annotate(fixture); if (fixture.canonical === null) { expect(() => parseFinancialDecimal(fixture.input)).toThrow(); } else { expect(String(parseFinancialDecimal(fixture.input))).toBe(fixture.canonical); } } for (const fixture of contracts.valid) { annotate(fixture); const result = parse(fixture.type, fixture.input); expect(result).toEqual(fixture.canonical); expect(parse(fixture.type, JSON.parse(JSON.stringify(result)))).toEqual(result); } for (const fixture of contracts.invalid) { annotate(fixture); const base = contracts.valid.find(candidate => candidate.id === fixture.base)!; const input: unknown = structuredClone(base.input); let parent = input as Record; for (const key of fixture.path.slice(0, -1)) { parent = parent[key] as Record; } parent[fixture.path.at(-1)!] = fixture.value; expect(() => parse(base.type, input)).toThrow(); } }); test('safe_integer_json_number_parity: integral lexical forms and exact boundaries', () => { annotate(contracts.numeric); const base = contracts.valid.find(candidate => candidate.id === 'fillCommand')!.input; const snapshot = contracts.valid.find(candidate => candidate.id === 'snapshot')!.input; for (const fixture of contracts.numeric.cases) { const value: unknown = JSON.parse(fixture.raw); const command = parseEngineFinancialCommand(base); const timestamp = () => parseEngineFinancialCommand({ ...command, eventTime: value }); const sequence = () => parseEngineFinancialCommand({ ...command, body: { ...command.body, decision: { ownerId: 'A', decisionId: 'd', sequence: value } }, }); const revision = () => parseFinancialSnapshot({ ...snapshot, revision: value }); if (fixture.timestampValid) { expect(value === 0 ? 0 : value).toBe(timestamp().eventTime); } else { expect(timestamp).toThrow(); } for (const counter of [sequence, revision]) { if (fixture.counterValid) { expect(counter).not.toThrow(); } else { expect(counter).toThrow(); } } } }); test('rounding_ties_positive_negative: TypeScript rejects internal-only precision', () => { for (const fixture of scalars.rounding) { annotate(fixture); expect(() => parseFinancialDecimal(fixture.input)).toThrow(); expect(String(parseFinancialDecimal(fixture.expected))).toBe(fixture.expected); } }); test('overflow_rejected: exact lexical bound with no floating-point conversion', () => { annotate(scalars.overflow); expect(String(parseFinancialDecimal(scalars.overflow.maximum))).toBe(scalars.overflow.maximum); expect(() => parseFinancialDecimal('792281625142643375935.43950336')).toThrow(); expect(() => parseFinancialDecimal('-792281625142643375935.43950336')).toThrow(); expect(() => parseFinancialDecimal(scalars.overflow.roundingOverflow)).toThrow(); }); test('basis_residual_spec: F02 owns transition behavior, TS validates documented values only', () => { annotate(ledgerSpecs.basis); expect(ledgerSpecs.basis.owningTask).toBe('F02'); expect(ledgerSpecs.basis.releases).toEqual(['1.66666667', '1.66666667', '1.66666666']); expect(ledgerSpecs.basis.remainingCosts).toEqual(['3.33333333', '1.66666666', '0.00000000']); for (const value of [...ledgerSpecs.basis.releases, ...ledgerSpecs.basis.remainingCosts]) { expect(String(parseFinancialDecimal(value))).toBe(value); } }); test('command_id_conflict_spec: typed canonical payload, not a second dedup engine', () => { annotate(ledgerSpecs.commandIds); expect(ledgerSpecs.commandIds.owningTask).toBe('F02'); const original = parseEngineFinancialCommand(ledgerSpecs.commandIds.original); const retry = parseEngineFinancialCommand(ledgerSpecs.commandIds.retry); const conflict = parseEngineFinancialCommand(ledgerSpecs.commandIds.conflict); const independent = parseEngineFinancialCommand(ledgerSpecs.commandIds.independent); expect(retry).toEqual(original); expect([conflict.sessionId, conflict.accountId, conflict.commandId]).toEqual([ original.sessionId, original.accountId, original.commandId, ]); expect(conflict.body).not.toEqual(original.body); expect(independent.sessionId).not.toBe(original.sessionId); expect(independent.body).toEqual(original.body); }); test('strategy submission contract cannot accept internal financial mutations', () => { const command = contracts.valid.find(candidate => candidate.id === 'fillCommand')!.input; const decision = contracts.valid.find(candidate => candidate.id === 'targetDecision')!.input; expect(() => parseFinancialStrategyDecision(command)).toThrow(); expect(() => parseEngineFinancialCommand(decision)).toThrow(); }); test('non-JSON sparse arrays cannot become valid wire contracts', () => { const snapshot = contracts.valid.find(candidate => candidate.id === 'snapshot')!.input; expect(() => parseFinancialSnapshot({ ...snapshot, positions: Array(1) })).toThrow(); }); test('identity text must round-trip through Rust UTF-8 without losing Unicode identifiers', () => { const command = contracts.valid.find(candidate => candidate.id === 'markCommand')!.input; for (const commandId of ['\ud800', '\udfff', 'a\ud800b']) { expect(() => parseEngineFinancialCommand({ ...command, commandId })).toThrow(); } expect(parseEngineFinancialCommand({ ...command, commandId: '命令:🧪' }).commandId).toBe( '命令:🧪' ); });