154 lines
7.1 KiB
TypeScript
154 lines
7.1 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import { canonicalRunRequestSchema } from '@stock-bot/stock-config/canonical-research';
|
|
import { costSensitivityInputSchema } from '@stock-bot/stock-config/cost-sensitivity';
|
|
import { experimentInputSchema } from '@stock-bot/stock-config/experiments';
|
|
import {
|
|
assertCompact,
|
|
comparisonFixture,
|
|
parseArguments,
|
|
scope,
|
|
summarize,
|
|
tableFixture,
|
|
withResponseRevision,
|
|
} from '../benchmark-experiment-tables';
|
|
|
|
describe('production experiment table measurement', () => {
|
|
test('bounded explicit workloads; smoke cannot be mistaken for measured trial set', () => {
|
|
expect(parseArguments([])).toEqual({ measure: false, samples: 1 });
|
|
expect(parseArguments(['--self-test'])).toEqual({ measure: false, samples: 1 });
|
|
expect(parseArguments(['--measure'])).toEqual({ measure: true, samples: 5 });
|
|
expect(parseArguments(['--samples=20', '--measure'])).toEqual({ measure: true, samples: 20 });
|
|
for (const args of [
|
|
['--measure', '--samples=1'],
|
|
['--samples=101'],
|
|
['--samples=0'],
|
|
['--measure', '--self-test'],
|
|
['--measure', '--measure'],
|
|
['--samples=3', '--samples=3'],
|
|
['--unknown'],
|
|
]) {
|
|
expect(() => parseArguments(args)).toThrow();
|
|
}
|
|
for (const count of [0, 101, 1.5, Infinity]) {
|
|
expect(() => tableFixture(count)).toThrow();
|
|
}
|
|
expect(scope).toContain('no engine, database, queue, financial correctness');
|
|
});
|
|
test.each([1, 25, 100])(
|
|
'%i rows preserve candidate order, source identity and every compact outcome',
|
|
count => {
|
|
const detail = tableFixture(count);
|
|
expect(detail).toEqual(tableFixture(count));
|
|
expect(detail.trialCount).toBe(count);
|
|
expect(detail.trials).toHaveLength(count);
|
|
expect(detail.preview.candidates).toHaveLength(count);
|
|
expect(canonicalRunRequestSchema.safeParse(detail.preview.request).success).toBe(true);
|
|
expect(experimentInputSchema.safeParse(detail.preview.input).success).toBe(true);
|
|
expect(() => assertCompact(detail)).not.toThrow();
|
|
expect(new Set(detail.trials.map(trial => trial.trialKey)).size).toBe(count);
|
|
expect(detail.trials.map(trial => trial.index)).toEqual(
|
|
Array.from({ length: count }, (_, i) => i)
|
|
);
|
|
expect(
|
|
Object.values(detail.counts).reduce((sum, value) => sum + value, 0) +
|
|
detail.trials.filter(trial => trial.status === 'missing').length
|
|
).toBe(count);
|
|
for (const [index, trial] of detail.trials.entries()) {
|
|
expect(detail.preview.candidates[index]?.parameters).toEqual(trial.parameters);
|
|
expect(detail.preview.candidates[index]?.trialKey).toBe(trial.trialKey);
|
|
expect(trial.status === 'missing').toBe(trial.runId === null);
|
|
}
|
|
expect(detail.trials[0]?.metrics?.totalReturn).toBe(0);
|
|
expect(detail.trials[0]?.metrics?.sharpeRatio).toBeNull();
|
|
expect(detail.trials[0]?.unavailable?.sharpeRatio).toBe('insufficient-periods');
|
|
if (count > 1) {
|
|
expect(new Set(detail.trials.map(trial => trial.status))).toEqual(
|
|
new Set(['completed', 'failed', 'pending', 'running', 'paused', 'cancelled', 'missing'])
|
|
);
|
|
expect(detail.trials[1]?.metrics?.sharpeRatio).toBe(1.25);
|
|
expect(detail.trials[2]?.status).toBe('completed');
|
|
expect(detail.trials[2]?.comparable).toBe(false);
|
|
expect(detail.trials[2]?.metrics).toBeNull();
|
|
expect(detail.trials[3]?.error).toContain('callback failure');
|
|
}
|
|
}
|
|
);
|
|
test('comparison is all 200 rows, cost scenarios keep parameters fixed and settings/summary states separate', () => {
|
|
const value = comparisonFixture(),
|
|
[left, right] = value.experiments;
|
|
expect(value.rows).toHaveLength(200);
|
|
expect(value.rows.filter(row => row.inputCompatibility === 'match')).toHaveLength(101);
|
|
expect(value.rows.filter(row => row.inputCompatibility === 'mismatch')).toHaveLength(99);
|
|
expect(value.rows.filter(row => row.summaryAvailable)).toHaveLength(46);
|
|
expect(value.rows.filter(row => row.ready)).toHaveLength(24);
|
|
expect(value.rows.map(row => row.trialKey)).toEqual(
|
|
[...left.trials, ...right.trials].map(trial => trial.trialKey)
|
|
);
|
|
expect(right.preview.version).toBe('cost-sensitivity-preview/v1');
|
|
expect(costSensitivityInputSchema.safeParse(right.preview.input).success).toBe(true);
|
|
expect(new Set(right.trials.map(trial => JSON.stringify(trial.parameters))).size).toBe(1);
|
|
expect(
|
|
new Set(
|
|
[...left.trials, ...right.trials].flatMap(trial => (trial.runId ? [trial.runId] : []))
|
|
).size
|
|
).toBe(178);
|
|
expect(() => assertCompact(value)).not.toThrow();
|
|
const failed = comparisonFixture(3),
|
|
missing = comparisonFixture(8);
|
|
for (const unavailable of [failed, missing]) {
|
|
expect(unavailable.reference.summaryAvailable).toBe(false);
|
|
expect(unavailable.rows.some(row => row.ready)).toBe(false);
|
|
expect(unavailable.rows.filter(row => row.summaryAvailable)).toHaveLength(46);
|
|
expect(unavailable.rows).toHaveLength(200);
|
|
}
|
|
expect(missing.reference.runId).toBeNull();
|
|
expect(comparisonFixture(1).reference.index).toBe(1);
|
|
expect(() => comparisonFixture(100)).toThrow();
|
|
});
|
|
test('refresh marker changes visible final rows without altering retained inputs or economic summaries', () => {
|
|
for (const original of [tableFixture(100), comparisonFixture()]) {
|
|
const first = withResponseRevision(original, 1),
|
|
second = withResponseRevision(first, 2);
|
|
const experiments = 'experiments' in second ? second.experiments : [second];
|
|
const before = 'experiments' in original ? original.experiments : [original];
|
|
for (const [index, experiment] of experiments.entries()) {
|
|
expect(before[index]?.preview).toEqual(experiment.preview);
|
|
expect(before[index]?.trials.map(trial => trial.metrics)).toEqual(
|
|
experiment.trials.map(trial => trial.metrics)
|
|
);
|
|
expect(
|
|
experiment.trials
|
|
.at(-1)
|
|
?.limitations.filter(text => text.startsWith('Synthetic response revision '))
|
|
).toEqual(['Synthetic response revision 2']);
|
|
}
|
|
expect(JSON.stringify(original)).not.toContain('Synthetic response revision');
|
|
expect(JSON.stringify(first)).toContain('Synthetic response revision 1');
|
|
}
|
|
});
|
|
test('full result blobs anywhere in an envelope are rejected', () => {
|
|
for (const field of [
|
|
'financial',
|
|
'trades',
|
|
'ohlcData',
|
|
'equity',
|
|
'fills',
|
|
'trace',
|
|
'returns',
|
|
]) {
|
|
expect(() => assertCompact({ comparison: { experiments: [{ [field]: [] }] } })).toThrow(
|
|
'Full result'
|
|
);
|
|
}
|
|
});
|
|
test('raw samples remain unmodified, median is defined and small samples make no tail claim', () => {
|
|
const values = [10, 1, 3, 2];
|
|
expect(summarize(values)).toEqual({ count: 4, min: 1, median: 2.5, max: 10, p95: null });
|
|
expect(values).toEqual([10, 1, 3, 2]);
|
|
expect(summarize([3, 1, 2]).median).toBe(2);
|
|
expect(summarize(Array.from({ length: 20 }, (_, i) => i + 1)).p95).toBe(19);
|
|
for (const invalid of [[], [NaN], [Infinity], [-1]]) {
|
|
expect(() => summarize(invalid)).toThrow();
|
|
}
|
|
});
|
|
});
|