stock-bot/test/integration/paper-readiness-projection.test.ts

171 lines
5.9 KiB
TypeScript

import { expect, test } from 'bun:test';
import type {
CanonicalAccount,
CanonicalExecutionRepository,
CanonicalTransaction,
} from '../../apps/stock/orchestrator/src/execution/canonical-paper';
import type { CanonicalPaperExecution } from '../../apps/stock/orchestrator/src/execution/CanonicalPaperExecution';
import {
CanonicalPaperRuntime,
projectPaperReadiness,
} from '../../apps/stock/orchestrator/src/paper/CanonicalPaperRuntime';
import { counterProgram } from '../fixtures/canonical-paper';
import { paperFeedConfiguration, paperFeedFrame } from '../fixtures/paper-feed';
// Native initialization supplies a real financial checkpoint. The repository fake
// clones transactions and commits only on success; financial writes are forbidden.
async function fixture() {
let stored: CanonicalAccount;
let now = 1000;
let failRead = false;
let pendingReads = 0;
const unexpected = async (): Promise<never> => {
throw new Error('Unexpected financial write in readiness-only fixture');
};
const repository: CanonicalExecutionRepository = {
async initialize(account) {
stored = structuredClone(account);
},
async transact(_id, work) {
const account = structuredClone(stored);
const tx: CanonicalTransaction = {
account,
owners: unexpected,
putOwner: unexpected,
getInput: unexpected,
async nextInput() {
pendingReads++;
if (failRead) {
throw new Error('fixture storage unavailable');
}
return undefined;
},
putInput: unexpected,
async nextBatch() {
pendingReads++;
return undefined;
},
putBatch: unexpected,
appendJournal: unexpected,
journal: unexpected,
};
const value = await work(tx);
stored = account;
return value;
},
};
const config = paperFeedConfiguration();
config.matching.risk.maxMarkAgeMs = 100;
const runtime = new CanonicalPaperRuntime({
repository,
programs: [
{
...counterProgram('owner-X'),
strategyId: 'sma_crossover',
strategyVersion: 'sma-crossover-long-only/v1',
},
],
now: () => now,
});
await runtime.initialize(config);
await runtime.connect({ sourceId: 'trusted-fixture', connectionId: 'connection-1' });
await runtime.ingest(paperFeedFrame());
await runtime.activate({ recoverPending: false });
return {
runtime,
snapshot: () => structuredClone(stored),
clock(value: number) {
now = value;
},
failRead(value: boolean) {
failRead = value;
},
pendingReads: () => pendingReads,
};
}
test('pure readiness projection reports stale and pending evidence repeatedly without changing account state', async () => {
const f = await fixture();
const account = f.snapshot();
const before = structuredClone(account);
const pending = Object.freeze({ pendingInput: 'input-1', pendingBatch: 'batch-1' });
const first = projectPaperReadiness(account, pending, 1200);
const second = projectPaperReadiness(account, pending, 1200);
expect(first).toEqual(second);
expect(first).toMatchObject({
active: true,
ready: false,
requiresHistoricalRecovery: true,
pendingInput: 'input-1',
pendingBatch: 'batch-1',
reasons: ['feed-stale', 'quote-stale:X', 'historical-recovery-required'],
});
expect(account).toEqual(before);
expect(f.snapshot()).toEqual(before);
expect(
projectPaperReadiness(account, { pendingInput: null, pendingBatch: null }, 1000)
).toMatchObject({
active: true,
ready: true,
reasons: [],
});
const inactive = structuredClone(account);
inactive.host!.active = false;
const inactiveBefore = structuredClone(inactive);
expect(
projectPaperReadiness(inactive, { pendingInput: null, pendingBatch: null }, 1000)
).toMatchObject({ active: false, ready: true });
expect(inactive).toEqual(inactiveBefore);
});
test('status commits expired activation and repeated fresh evidence cannot automatically resume it', async () => {
const f = await fixture();
const checkpoint = f.snapshot().checkpoint;
f.clock(1200);
const expired = await f.runtime.status();
expect(expired).toMatchObject({ active: false, ready: false, operatorHalted: false });
expect(expired.reasons).toContain(
'Operational readiness expired; fresh feed and operator activation required'
);
expect(f.snapshot().host?.active).toBe(false);
expect(await f.runtime.status()).toEqual(expired);
f.clock(2000);
expect(await f.runtime.ingest(paperFeedFrame(2000))).toMatchObject({
disposition: 'health-only',
readiness: { active: false, ready: false },
});
expect(f.snapshot().checkpoint).toEqual(checkpoint);
expect(await f.runtime.activate({ recoverPending: false })).toMatchObject({
active: true,
ready: true,
});
});
test('storage failure rolls back status enforcement and returns failure instead of invented readiness', async () => {
const f = await fixture();
f.clock(1200);
const before = f.snapshot();
f.failRead(true);
await expect(f.runtime.status()).rejects.toThrow('fixture storage unavailable');
expect(f.snapshot()).toEqual(before);
f.failRead(false);
expect(await f.runtime.status()).toMatchObject({ active: false, ready: false });
expect(f.snapshot().host?.active).toBe(false);
});
test('execution independently enforces expiry before reading pending financial work without a status poll', async () => {
const f = await fixture();
const before = f.snapshot();
const reads = f.pendingReads();
f.clock(1200);
const execution = Reflect.get(f.runtime, 'execution') as CanonicalPaperExecution;
await execution.drain();
expect(f.pendingReads()).toBe(reads);
expect(f.snapshot().host?.active).toBe(false);
expect(f.snapshot().checkpoint).toEqual(before.checkpoint);
f.clock(2000);
expect(await f.runtime.ingest(paperFeedFrame(2000))).toMatchObject({
disposition: 'health-only',
readiness: { active: false },
});
});