Intelligence, Advanced Charting & Trading Platform

1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
4 import { join } from 'node:path';
5 import { tmpdir } from 'node:os';
6 import { evaluateP0Safety } from './check-p0-safety.mjs';
7
8 async function fixture() {
9 const root = await mkdtemp(join(tmpdir(), 'velyqor-p0-'));
10 await mkdir(join(root, 'db', 'migrations'), { recursive: true });
11 await mkdir(join(root, 'packages', 'market-domain', 'src', 'adapters'), { recursive: true });
12 await writeFile(join(root, 'db', 'migrations', '0001_market_core.sql'), 'CREATE TABLE harmless(id int);');
13 await writeFile(join(root, 'packages', 'market-domain', 'src', 'adapters', 'navasan.mjs'), "rawUnit: 'IRR_PER_USD'; rawUnit: 'IRR_PER_GRAM';");
14 await writeFile(join(root, 'AGENTS.md'), 'AI/signals may never directly execute an order.');
15 return root;
16 }
17
18 async function withFixture(fn) {
19 const root = await fixture();
20 try { await fn(root); } finally { await rm(root, { recursive: true, force: true }); }
21 }
22
23 test('baseline passes without altering source files', () => withFixture(async (root) => {
24 assert.deepEqual(await evaluateP0Safety(root), { ok: true, violations: [] });
25 }));
26
27 test('unreviewed 0005 OHLC migration is blocked', () => withFixture(async (root) => {
28 await writeFile(join(root, 'db', 'migrations', '0005_navasan_private_daily_ohlc.sql'), 'SELECT 1;');
29 const result = await evaluateP0Safety(root);
30 assert.equal(result.ok, false);
31 assert.match(result.violations.join('\n'), /0005_navasan_private_daily_ohlc/);
32 }));
33
34 test('renamed Navasan OHLC migration is blocked', () => withFixture(async (root) => {
35 await writeFile(join(root, 'db', 'migrations', '0006_more.sql'), "INSERT INTO providers(code) VALUES ('navasan_ohlc');");
36 assert.equal((await evaluateP0Safety(root)).ok, false);
37 }));
38
39 test('changed unit assumptions are blocked', () => withFixture(async (root) => {
40 await writeFile(join(root, 'packages', 'market-domain', 'src', 'adapters', 'navasan.mjs'), "rawUnit: 'IRT_PER_USD'; rawUnit: 'IRT_PER_GRAM';");
41 assert.match((await evaluateP0Safety(root)).violations.join('\n'), /raw-unit contract/);
42 }));
43
44 test('removing no-AI-order rule is blocked', () => withFixture(async (root) => {
45 await writeFile(join(root, 'AGENTS.md'), '# Rules\n');
46 assert.match((await evaluateP0Safety(root)).violations.join('\n'), /AI-to-live-order/);
47 }));
48
49 test('missing migration directory fails closed', async () => {
50 const root = await mkdtemp(join(tmpdir(), 'velyqor-p0-empty-'));
51 try { assert.equal((await evaluateP0Safety(root)).ok, false); }
52 finally { await rm(root, { recursive: true, force: true }); }
53 });