stock-bot/test/integration/candidate-response-contract.test.ts

63 lines
2.6 KiB
TypeScript

import { expect, test } from 'bun:test';
import { candidateSandboxLimits } from '@stock-bot/stock-config/candidate-execution';
import { validateStrategyParameters } from '@stock-bot/stock-config/strategy';
import { parseExperimentPreview } from '../../apps/stock/config/src/contracts/research-responses/experiment';
import { tableFixture } from '../../scripts/benchmark-experiment-tables';
function candidatePreview() {
const value = structuredClone(tableFixture(1).preview);
const strategy = value.request.strategy;
value.input.candidate = {
versionId: strategy.versionId,
parameters: { ...strategy.selection.parameters },
};
value.request.strategy = {
...strategy,
origin: 'candidate',
reviewId: null,
baselineVersionId: strategy.versionId,
package: { ...strategy.package, execution: { kind: 'isolated-candidate/v1', warmupBars: 0 } },
execution: {
kind: 'typescript-isolated-candidate/v1',
imageId: `sha256:${'a'.repeat(64)}`,
profile: {
version: 1,
policy: 'linux-docker-guardian/v1',
protocol: 'candidate-controller/v1',
platform: 'linux',
arch: 'x64',
hostSha256: 'b'.repeat(64),
guardianSha256: 'c'.repeat(64),
bunSha256: 'd'.repeat(64),
limits: candidateSandboxLimits,
},
},
};
return value;
}
test('candidate preview binds normalized base parameters independently of valid package/version identity', () => {
const value = candidatePreview();
expect(parseExperimentPreview(value).input.candidate).toEqual(value.input.candidate!);
const changed = structuredClone(value);
changed.input.candidate!.parameters.fastPeriod = 1;
expect(() => parseExperimentPreview(changed)).toThrow('could not be verified');
const changedRequest = structuredClone(value);
if (changedRequest.request.strategy.origin !== 'candidate') {
throw new Error('Expected candidate fixture');
}
changedRequest.request.strategy.selection.parameters.fastPeriod = 1;
expect(() => parseExperimentPreview(changedRequest)).toThrow('could not be verified');
});
test('candidate parameter defaults are accepted when they exactly match the retained base selection', () => {
const value = candidatePreview();
value.input.candidate!.parameters = {};
value.request.strategy.selection.parameters = validateStrategyParameters(
value.request.strategy.package,
{}
);
expect(parseExperimentPreview(value).request.strategy.selection.parameters).toEqual(
value.request.strategy.selection.parameters
);
value.input.candidate!.parameters.fastPeriod = 1;
expect(() => parseExperimentPreview(value)).toThrow('could not be verified');
});