stock-bot/libs/calculations/test/preservation.test.ts

55 lines
2.4 KiB
TypeScript

import { createHash } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, test } from 'bun:test';
import ts from 'typescript';
import * as legacy from '@stock-bot/calculations/legacy';
import audit from '../migration-audit.json';
const root = join(import.meta.dir, '..');
const namespaces: Record<string, object> = {
'basic-calculations.ts': legacy.basic,
'technical-indicators.ts': legacy.indicators,
'risk-metrics.ts': legacy.risk,
'performance-metrics.ts': legacy.performance,
};
const hash = (value: string) => createHash('sha256').update(value).digest('hex');
describe('calculation capability preservation', () => {
test('all 105 previously active functions remain available with unchanged legacy bodies', () => {
let count = 0;
for (const module of audit.modules.filter(module => module.status === 'legacy-api')) {
const name = module.original.split('/').at(-1)!;
expect(Object.keys(namespaces[name]!).sort()).toEqual(module.functions);
const source = readFileSync(join(root, module.destination), 'utf8');
const ast = ts.createSourceFile(name, source, ts.ScriptTarget.Latest, true);
expect(hash(ts.createPrinter({ removeComments: true }).printFile(ast))).toBe(
module.astSha256!
);
count += module.functions.length;
}
expect(count).toBe(105);
// Legacy behavior is preserved, not silently relabeled as validated core behavior.
expect(legacy.basic.simpleReturn(0, 100)).toBe(0);
expect(legacy.indicators.sma([1, 2, 3], 2)).toEqual([1.5, 2.5]);
expect(legacy.risk.maxDrawdown([100, 80, 100])).toBeCloseTo(0.2);
expect(
legacy.performance.analyzeTradePerformance([{ pnl: 1, date: new Date(0) }]).profitFactor
).toBe(Infinity);
});
test('all seven disabled prototypes survive as non-executable, source-verified references', () => {
const modules = audit.modules.filter(module => module.status === 'research-reference');
expect(modules).toHaveLength(7);
for (const module of modules) {
const reference = readFileSync(join(root, module.destination), 'utf8');
const start = reference.indexOf('```ts\n') + 6;
const end = reference.lastIndexOf('```');
expect(start).toBeGreaterThan(5);
expect(end).toBeGreaterThan(start);
expect(hash(reference.slice(start, end).replaceAll('\r\n', '\n').trimEnd())).toBe(
module.sourceSha256
);
}
});
});