stock-bot/scripts/test-e2e.ts

44 lines
2.1 KiB
TypeScript

import { createHash } from 'node:crypto';
import { existsSync, readFileSync, realpathSync } from 'node:fs';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { chromium } from 'playwright';
const database = process.env.PIPELINE_TEST_DATABASE_URL;
const redis = process.env.BACKTEST_TEST_REDIS_URL;
if (!database || !new URL(database).pathname.startsWith('/stock_bot_safety')) {
throw new Error('Set PIPELINE_TEST_DATABASE_URL to a disposable stock_bot_safety database');
}
if (!redis || new URL(redis).pathname !== '/15') {
throw new Error('Set BACKTEST_TEST_REDIS_URL to a dedicated Redis fixture using database 15');
}
if (!existsSync(chromium.executablePath())) {
throw new Error('Chromium is missing. Explicitly install it with bun run test:e2e:install');
}
for (const file of [
'libs/core/di/dist/index.js',
'apps/stock/engine/dist/engine.node',
]) {
if (!existsSync(file)) {
throw new Error(`Missing ${file}. Build this worktree with bun run build before browser tests`);
}
}
const expectedEngine = realpathSync(resolve('apps/stock/engine/index.mjs'));
if (realpathSync(fileURLToPath(import.meta.resolve('@stock-bot/engine'))) !== expectedEngine) {
throw new Error('Native engine resolves outside this worktree');
}
console.log(`Browser native addon SHA256: ${createHash('sha256').update(readFileSync('apps/stock/engine/dist/engine.node')).digest('hex')}`);
// Always rebuild from this checkout. An empty override prevents a local .env from
// silently directing browser requests to a development or live API.
const build = Bun.spawn([process.execPath, 'run', '--cwd', 'apps/stock/web-app', 'build'], {
env: { ...process.env, NODE_ENV: 'production', VITE_API_BASE_URL: '' },
stdin: 'inherit', stdout: 'inherit', stderr: 'inherit',
});
if (await build.exited) {
throw new Error('Browser application build failed');
}
const tests = Bun.spawn([process.execPath, 'test', 'test/e2e/workbench.test.ts', '--timeout', '60000'], {
env: { ...process.env, STOCK_BOT_E2E_BUILT: '1' },
stdin: 'inherit', stdout: 'inherit', stderr: 'inherit',
});
process.exitCode = await tests.exited;