78 lines
4.5 KiB
TypeScript
78 lines
4.5 KiB
TypeScript
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { expect, test } from 'bun:test';
|
|
import ts from 'typescript';
|
|
import { runtimeImports } from '../runtime-imports';
|
|
|
|
const root = fileURLToPath(new URL('../../', import.meta.url));
|
|
const api = join(root, 'apps/stock/web-api/src');
|
|
const shared = join(root, 'libs/trading-runtime/src');
|
|
|
|
test('API and shared server modules cannot import another application implementation', () => {
|
|
for (const directory of [api, shared]) {
|
|
for (const name of new Bun.Glob('**/*.ts').scanSync({ cwd: directory })) {
|
|
const source = ts.createSourceFile(name, readFileSync(join(directory, name), 'utf8'), ts.ScriptTarget.Latest, true);
|
|
for (const statement of source.statements) {
|
|
if ((ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) && statement.moduleSpecifier && ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
expect(statement.moduleSpecifier.text).not.toMatch(/orchestrator\/src|(?:\.\.\/)+apps\//);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
test('read-only custody cannot reach executable strategy loaders or a native addon', () => {
|
|
for (const name of ['artifacts', 'canonical-build', 'paper-host-identity', 'candidate-environment', 'paper-deployment-custody', 'retained-package']) {
|
|
const path = join(shared, name + '.ts');
|
|
for (const dependency of runtimeImports(path)) {
|
|
expect(dependency).not.toMatch(/@stock-bot\/engine|orchestrator|retained-program|native-replay/);
|
|
}
|
|
expect(readFileSync(path, 'utf8')).not.toMatch(/\b(?:spawn|fork)\s*\(|\bimport\s*\(/);
|
|
}
|
|
});
|
|
|
|
test('metadata imports work outside an installed checkout; artifact access fails only when requested', async () => {
|
|
const directory = mkdtempSync(join(tmpdir(), 'stock-bot-metadata-import-'));
|
|
try {
|
|
// Keep Bun's build/file-descriptor cache isolated from other in-process bundling tests.
|
|
const build = Bun.spawnSync([process.execPath, '--no-env-file', '-e', `
|
|
const result = await Bun.build(${JSON.stringify({ entrypoints: ['canonical-build', 'paper-host-identity', 'candidate-environment'].map(name => join(shared, name + '.ts')), target: 'bun', outdir: directory, external: ['@stock-bot/engine'] })});
|
|
if (!result.success) { console.error(result.logs); process.exit(1); }
|
|
`], { cwd: root, env: { PATH: process.env.PATH } });
|
|
expect(new TextDecoder().decode(build.stderr)).toBe('');
|
|
expect(build.exitCode).toBe(0);
|
|
const child = Bun.spawnSync([process.execPath, '--no-env-file', '-e', `
|
|
const { canonicalWorkerUrl } = await import('./canonical-build.js');
|
|
await import('./paper-host-identity.js');
|
|
await import('./candidate-environment.js');
|
|
let rejected = false;
|
|
try { canonicalWorkerUrl(); } catch { rejected = true; }
|
|
if (!rejected) process.exit(9);
|
|
`], { cwd: directory, env: { PATH: process.env.PATH } });
|
|
expect(new TextDecoder().decode(child.stderr)).toBe('');
|
|
expect(child.exitCode).toBe(0);
|
|
} finally { rmSync(directory, { recursive: true, force: true }); }
|
|
});
|
|
|
|
test('shared runtime dependencies participate in the actual Turbo build graph', () => {
|
|
const child = Bun.spawnSync([process.execPath, 'run', 'build', '--dry=json'], { cwd: root, env: { ...process.env, TURBO_TELEMETRY_DISABLED: '1' } });
|
|
expect(child.exitCode).toBe(0);
|
|
const plan = JSON.parse(new TextDecoder().decode(child.stdout)) as { tasks: { taskId: string; dependencies: string[] }[] };
|
|
for (const name of ['web-api', 'orchestrator']) {
|
|
expect(plan.tasks.find(task => task.taskId === `@stock-bot/${name}#build`)?.dependencies).toContain('@stock-bot/trading-runtime#build');
|
|
}
|
|
});
|
|
|
|
test('shared paper and product contracts bundle without server or native dependencies', () => {
|
|
const child = Bun.spawnSync([process.execPath, '--no-env-file', '-e', `
|
|
const bundle = await Bun.build(${JSON.stringify({ entrypoints: [join(root, 'apps/stock/config/src/contracts/paper-runtime.ts'), join(root, 'apps/stock/config/src/contracts/product-responses/index.ts')], target: 'browser' })});
|
|
if (!bundle.success) { console.error(bundle.logs); process.exit(1); }
|
|
for (const output of bundle.outputs) {
|
|
if (/PostgresClient|engine\\.node|node:fs|node:child_process/.test(await output.text())) process.exit(2);
|
|
}
|
|
`], { cwd: root, env: { PATH: process.env.PATH } });
|
|
expect(new TextDecoder().decode(child.stderr)).toBe('');
|
|
expect(child.exitCode).toBe(0);
|
|
});
|