Extract shared calculations and preserve legacy research capabilities
This commit is contained in:
parent
ccce401f72
commit
7827220b9b
82 changed files with 3099 additions and 1994 deletions
|
|
@ -3,63 +3,11 @@ import { dirname, join, relative, resolve } from 'node:path';
|
|||
import { fileURLToPath } from 'node:url';
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import ts from 'typescript';
|
||||
import { runtimeImports } from '../runtime-imports';
|
||||
|
||||
const root = fileURLToPath(new URL('../../apps/stock/orchestrator/src/', import.meta.url));
|
||||
const files = [...new Bun.Glob('**/*.ts').scanSync({ cwd: root })].map(file => join(root, file));
|
||||
|
||||
/** Include static imports/re-exports and lazy imports; type-only edges do not load code. */
|
||||
function runtimeImports(file: string): string[] {
|
||||
const source = ts.createSourceFile(
|
||||
file,
|
||||
readFileSync(file, 'utf8'),
|
||||
ts.ScriptTarget.Latest,
|
||||
true
|
||||
);
|
||||
const imports = new Set<string>();
|
||||
function visit(node: ts.Node) {
|
||||
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
||||
const clause = node.importClause;
|
||||
const bindings = clause?.namedBindings;
|
||||
const typesOnly =
|
||||
clause?.isTypeOnly ||
|
||||
(clause &&
|
||||
!clause.name &&
|
||||
bindings &&
|
||||
ts.isNamedImports(bindings) &&
|
||||
bindings.elements.length > 0 &&
|
||||
bindings.elements.every(item => item.isTypeOnly));
|
||||
if (!typesOnly) imports.add(node.moduleSpecifier.text);
|
||||
}
|
||||
if (
|
||||
ts.isExportDeclaration(node) &&
|
||||
!node.isTypeOnly &&
|
||||
node.moduleSpecifier &&
|
||||
ts.isStringLiteral(node.moduleSpecifier)
|
||||
) {
|
||||
const clause = node.exportClause;
|
||||
if (
|
||||
!clause ||
|
||||
!ts.isNamedExports(clause) ||
|
||||
!clause.elements.length ||
|
||||
clause.elements.some(item => !item.isTypeOnly)
|
||||
) {
|
||||
imports.add(node.moduleSpecifier.text);
|
||||
}
|
||||
}
|
||||
if (
|
||||
ts.isCallExpression(node) &&
|
||||
node.expression.kind === ts.SyntaxKind.ImportKeyword &&
|
||||
node.arguments[0] &&
|
||||
ts.isStringLiteral(node.arguments[0])
|
||||
) {
|
||||
imports.add(node.arguments[0].text);
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
}
|
||||
visit(source);
|
||||
return [...imports];
|
||||
}
|
||||
|
||||
const imports = new Map(files.map(file => [file, runtimeImports(file)]));
|
||||
function localFile(from: string, specifier: string): string | undefined {
|
||||
if (!specifier.startsWith('.')) return undefined;
|
||||
|
|
@ -256,14 +204,14 @@ describe('orchestrator architecture boundaries', () => {
|
|||
expect(dependencies).toEqual(['backtest/BacktestPlayback.ts', 'backtest/execution-engine.ts']);
|
||||
});
|
||||
|
||||
it('shared analytics math is pure and cannot load report facades or application adapters', () => {
|
||||
it('reviewed analytics and window math live in the shared calculations library', () => {
|
||||
for (const entry of ['equity', 'performance', 'return-statistics', 'trade-statistics']) {
|
||||
for (const file of reachable(join(root, 'analytics/' + entry + '.ts'))) {
|
||||
expect(relative(root, file)).toMatch(/^analytics\//);
|
||||
expect(relative(root, file)).not.toBe('analytics/PerformanceAnalyzer.ts');
|
||||
expect((imports.get(file) ?? []).every(specifier => specifier.startsWith('.'))).toBe(true);
|
||||
}
|
||||
expect(existsSync(join(root, 'analytics/' + entry + '.ts'))).toBe(false);
|
||||
}
|
||||
expect(existsSync(join(root, 'indicators/window-statistics.ts'))).toBe(false);
|
||||
expect(imports.get(join(root, 'analytics/PerformanceAnalyzer.ts'))).toEqual([
|
||||
'@stock-bot/calculations/analytics',
|
||||
]);
|
||||
});
|
||||
|
||||
it('backtest reports are independent of run control, native engines, data loading and financial mutation', () => {
|
||||
|
|
@ -276,9 +224,12 @@ describe('orchestrator architecture boundaries', () => {
|
|||
for (const specifier of imports.get(file) ?? []) {
|
||||
expect(
|
||||
specifier.startsWith('.') ||
|
||||
['simple-statistics', 'zod', '@stock-bot/invariants', '@stock-bot/logger'].includes(
|
||||
specifier
|
||||
)
|
||||
[
|
||||
'zod',
|
||||
'@stock-bot/invariants',
|
||||
'@stock-bot/logger',
|
||||
'@stock-bot/calculations/analytics',
|
||||
].includes(specifier)
|
||||
).toBe(true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue