81 lines
3.2 KiB
TypeScript
81 lines
3.2 KiB
TypeScript
import { expect, test } from 'bun:test';
|
|
import { turnoverOracle, verifySessionRepeats } from '../benchmark-replay';
|
|
|
|
test('canonical benchmark integer-cent oracle includes fees and terminal marks', () => {
|
|
expect(turnoverOracle(3, 1, 1)).toEqual({
|
|
cash: '1000001.74000000',
|
|
equity: '1000001.74000000',
|
|
feePerOwner: '0.26000000',
|
|
quantity: '0.00000000',
|
|
remainingCost: '0.00000000',
|
|
fills: 2,
|
|
decisions: 2,
|
|
});
|
|
expect(turnoverOracle(4, 2, 2)).toEqual({
|
|
cash: '999942.32000000',
|
|
equity: '1000010.32000000',
|
|
feePerOwner: '0.84000000',
|
|
quantity: '1.00000000',
|
|
remainingCost: '16.00000000',
|
|
fills: 12,
|
|
decisions: 6,
|
|
});
|
|
expect(() => turnoverOracle(2, 1, 1)).toThrow('three bars');
|
|
});
|
|
|
|
test('canonical benchmark rejects any changed provenance or complete result hash', () => {
|
|
const sample = {
|
|
datasetHash: 'dataset',
|
|
policyHash: 'policy',
|
|
trials: [{ resultHash: 'result' }, { resultHash: 'result' }],
|
|
};
|
|
expect(() => verifySessionRepeats([sample, structuredClone(sample)])).not.toThrow();
|
|
for (const changed of [
|
|
{ ...sample, datasetHash: 'changed' },
|
|
{ ...sample, policyHash: 'changed' },
|
|
{ ...sample, trials: [{ resultHash: 'changed' }] },
|
|
{ ...sample, trials: [] },
|
|
]) {
|
|
expect(() => verifySessionRepeats([sample, changed])).toThrow('mismatch');
|
|
}
|
|
expect(() => verifySessionRepeats([])).toThrow('Missing');
|
|
});
|
|
|
|
test('canonical self-test measures real sessions and verifies repeated full-result hashes', async () => {
|
|
const worker = Bun.spawn([process.execPath, 'scripts/benchmark-research.ts', '--session'], {
|
|
cwd: new URL('../..', import.meta.url).pathname,
|
|
stdout: 'pipe',
|
|
stderr: 'pipe',
|
|
});
|
|
const [stdout, stderr, exit] = await Promise.all([
|
|
new Response(worker.stdout).text(),
|
|
new Response(worker.stderr).text(),
|
|
worker.exited,
|
|
]);
|
|
expect(exit, stderr).toBe(0);
|
|
const report = JSON.parse(stdout);
|
|
expect(report.path).toBe('canonical-pull-session');
|
|
expect(report.mode).toBe('self-test');
|
|
expect(report.native.sha256).toMatch(/^[a-f0-9]{64}$/);
|
|
expect(report.measurements.map((m: any) => m.workload.owners)).toEqual([1, 2]);
|
|
for (const measurement of report.measurements) {
|
|
verifySessionRepeats(measurement.samples);
|
|
expect(measurement.samples).toHaveLength(2);
|
|
for (const sample of measurement.samples) {
|
|
expect(sample.trials).toHaveLength(2);
|
|
expect(sample.phasesMs.frozenDatasetVerificationMs).toBeGreaterThanOrEqual(0);
|
|
expect(sample.phasesMs.preparedReuseAndMappingMs).toBeGreaterThanOrEqual(0);
|
|
for (const trial of sample.trials) {
|
|
expect(trial.resultHash).toMatch(/^[a-f0-9]{64}$/);
|
|
expect(trial.calls.decisions).toBe(trial.economic.decisions);
|
|
expect(trial.economic.fees).toBe(trial.economic.fills);
|
|
expect(trial.calls.advance).toBeGreaterThan(trial.calls.submit);
|
|
expect(trial.bytes.advanceResponses).toBeGreaterThan(0);
|
|
expect(trial.bytes.submitRequests).toBeGreaterThan(0);
|
|
expect(trial.bytes.finalizeResponse).toBeGreaterThan(0);
|
|
expect(trial.timingsMs.nativeCreateMs).toBeGreaterThanOrEqual(0);
|
|
expect(trial.timingsMs.finalize).toBeGreaterThanOrEqual(0);
|
|
}
|
|
}
|
|
}
|
|
});
|