145 lines
5.8 KiB
TypeScript
145 lines
5.8 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import { prepareDataset } from '../../apps/stock/orchestrator/src/data/historical-dataset';
|
|
import {
|
|
economicHash,
|
|
parameters,
|
|
researchFixture,
|
|
unavailable,
|
|
verifyRepeats,
|
|
} from '../benchmark-research';
|
|
|
|
describe('existing-path research benchmark', () => {
|
|
test('fixture is seeded, ordered, hash-verified and detached by real dataset preparation', async () => {
|
|
for (const assets of [1, 10]) {
|
|
const source = researchFixture(80 * assets, assets);
|
|
const dataset = await prepareDataset(source);
|
|
expect(dataset).not.toBe(source);
|
|
expect(Object.isFrozen(dataset.bars)).toBe(true);
|
|
expect(dataset.manifest.hash).toBe(researchFixture(80 * assets, assets).manifest.hash);
|
|
expect(dataset.manifest.hash).not.toBe(
|
|
researchFixture(80 * assets, assets, 43).manifest.hash
|
|
);
|
|
expect(dataset.manifest.barCount).toBe(80 * assets);
|
|
expect(new Set(dataset.bars.map(b => b.symbol)).size).toBe(assets);
|
|
expect(await prepareDataset(dataset)).toBe(dataset);
|
|
}
|
|
expect(() => researchFixture(81, 10)).toThrow('divisible');
|
|
expect(() => researchFixture(80, 0)).toThrow('assets');
|
|
expect(() => researchFixture(80, 1, NaN)).toThrow('seed');
|
|
});
|
|
|
|
test('economic hash ignores only run metadata, map order and unordered position summaries', () => {
|
|
const a = {
|
|
backtestId: 'first',
|
|
completedAt: 'now',
|
|
executionTime: 1,
|
|
metrics: { cash: 99, commission: 1 },
|
|
positions: [
|
|
{ symbol: 'B', quantity: 2 },
|
|
{ symbol: 'A', quantity: 1 },
|
|
],
|
|
trades: [
|
|
{ id: 'first', price: 5 },
|
|
{ id: 'second', price: 6 },
|
|
],
|
|
equity: [100, 99],
|
|
ohlcData: { B: [2, 3], A: [1, 4] },
|
|
};
|
|
const b = {
|
|
...a,
|
|
backtestId: 'second',
|
|
completedAt: 'later',
|
|
executionTime: 99,
|
|
metrics: { commission: 1, cash: 99 },
|
|
positions: [...a.positions].reverse(),
|
|
ohlcData: { A: [1, 4], B: [2, 3] },
|
|
};
|
|
expect(economicHash(a)).toBe(economicHash(b));
|
|
expect(economicHash(a)).not.toBe(economicHash({ ...b, trades: [...b.trades].reverse() }));
|
|
expect(economicHash(a)).not.toBe(economicHash({ ...b, equity: [...b.equity].reverse() }));
|
|
expect(economicHash(a)).not.toBe(economicHash({ ...b, metrics: { commission: 2, cash: 98 } }));
|
|
expect(economicHash(a)).not.toBe(economicHash({ ...b, newEconomicField: 1 }));
|
|
expect(a.positions[0]!.symbol).toBe('B');
|
|
});
|
|
|
|
test('comparison rejects changed data, policy or economics instead of accepting faster results', () => {
|
|
const sample = {
|
|
datasetHash: 'dataset',
|
|
policyHash: 'policy',
|
|
trials: [{ strategyParameters: parameters[0]!, economicHash: 'economic' }],
|
|
};
|
|
expect(() => verifyRepeats([sample, structuredClone(sample)])).not.toThrow();
|
|
expect(() => verifyRepeats([sample, { ...sample, datasetHash: 'changed' }])).toThrow(
|
|
'Dataset/policy'
|
|
);
|
|
expect(() => verifyRepeats([sample, { ...sample, policyHash: 'changed' }])).toThrow(
|
|
'Dataset/policy'
|
|
);
|
|
expect(() =>
|
|
verifyRepeats([
|
|
sample,
|
|
{ ...sample, trials: [{ ...sample.trials[0]!, economicHash: 'changed' }] },
|
|
])
|
|
).toThrow('Economic mismatch');
|
|
expect(() => verifyRepeats([])).toThrow('Missing');
|
|
expect(
|
|
Object.values(unavailable).every(reason => typeof reason === 'string' && reason.length > 0)
|
|
).toBe(true);
|
|
});
|
|
|
|
test('tiny native self-test records honest phases and pinned identities without timing thresholds', async () => {
|
|
const worker = Bun.spawn([process.execPath, 'scripts/benchmark-research.ts', '--self-test'], {
|
|
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.mode).toBe('self-test');
|
|
expect(report.source.commit).toMatch(/^[a-f0-9]{40}$/);
|
|
for (const hash of [
|
|
report.source.contentSha256,
|
|
report.native.sha256,
|
|
report.runtime.executableSha256,
|
|
report.runtime.lockSha256,
|
|
report.runtime.cargoLockSha256,
|
|
report.host.sha256,
|
|
report.seed.sha256,
|
|
]) {
|
|
expect(hash).toMatch(/^[a-f0-9]{64}$/);
|
|
}
|
|
expect(typeof report.source.dirty).toBe('boolean');
|
|
expect(report.runtime.bun).toBe(Bun.version);
|
|
expect(report.native.path).toContain('/apps/stock/engine/dist/engine.node');
|
|
expect(report.native.buildProfile).toMatch(/^(debug|release|unverified)$/);
|
|
expect(report.unavailable).toEqual(unavailable);
|
|
expect(report.measurements.map((m: any) => m.workload.assets)).toEqual([1, 10]);
|
|
for (const measurement of report.measurements) {
|
|
expect(measurement.repeatedEconomicHashesMatch).toBe(true);
|
|
expect(measurement.samples).toHaveLength(2);
|
|
verifyRepeats(measurement.samples);
|
|
for (const sample of measurement.samples) {
|
|
expect(sample.trials).toHaveLength(6);
|
|
expect(sample.datasetHash).toMatch(/^[a-f0-9]{64}$/);
|
|
expect(sample.policyHash).toMatch(/^[a-f0-9]{64}$/);
|
|
expect(sample.processWallMs).toBeGreaterThan(0);
|
|
expect(sample.phasesMs).toHaveProperty('addonImportMs');
|
|
expect(sample.phasesMs).toHaveProperty('datasetPreparationMs');
|
|
for (const trial of sample.trials) {
|
|
expect(trial.fills).toBeGreaterThan(0);
|
|
expect(trial.resultBytes).toBeGreaterThan(0);
|
|
expect(trial.rssBytes).toBeGreaterThan(0);
|
|
expect(trial.timingsMs).toHaveProperty('nativeLoadMs');
|
|
expect(trial.timingsMs).toHaveProperty('nativeRunMs');
|
|
expect(trial.timingsMs).toHaveProperty('resultParseMs');
|
|
expect(trial.economicHash).toMatch(/^[a-f0-9]{64}$/);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|