stock-bot/test/unit/m2-baseline-report-doc.test.ts
2026-11-30 15:18:01 -05:00

178 lines
7.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, test } from 'bun:test';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
// Guards the T-128 M2.0 baseline report (design/m2-baseline-report.md) against silent drift: it is
// the gate evidence the director reads before M2 starts, so its provenance SHAs, gate commands,
// exit codes and failure accounting must stay complete and internally consistent. Live repository
// facts it records (bun.lock contents, studio.config.json paths) are deliberately not pinned here:
// they are a point-in-time snapshot that M2 work is expected to change.
const repoRoot = join(import.meta.dir, '..', '..');
const reportPath = join(repoRoot, 'design/m2-baseline-report.md');
const doc = readFileSync(reportPath, 'utf8');
const M1_SHA = 'd52aa20bff3f5c259fb6e2bcfc2d3a78ad5224b1';
const MASTER_SHA = '56b73b7e663d293079ab7bac264950dfa7a68b00';
const STUDIO_TEST_COMMAND =
'bun --env-file=/home/boki/.studio/test-env/stock-bot.env test && CARGO_BUILD_JOBS=2 cargo test --workspace --lib --offline';
function section(title: RegExp): string {
const lines = doc.split('\n');
const start = lines.findIndex(line => /^## /.test(line) && title.test(line));
expect(start).toBeGreaterThan(-1);
const rest = lines.slice(start + 1);
const end = rest.findIndex(line => /^## /.test(line));
return rest.slice(0, end === -1 ? undefined : end).join('\n');
}
function subsection(body: string, title: RegExp): string {
const lines = body.split('\n');
const start = lines.findIndex(line => /^### /.test(line) && title.test(line));
expect(start).toBeGreaterThan(-1);
const rest = lines.slice(start + 1);
const end = rest.findIndex(line => /^### /.test(line));
return rest.slice(0, end === -1 ? undefined : end).join('\n');
}
function tableRows(body: string): string[][] {
return body
.split('\n')
.filter(line => line.startsWith('|') && !/^\|\s*-/.test(line))
.map(line => line.split('|').slice(1, -1).map(cell => cell.trim()))
.slice(1); // drop header row
}
describe('M2 baseline verification report (T-128)', () => {
test('exists and leads with an explicit GREEN verdict and zero real failures', () => {
expect(existsSync(reportPath)).toBe(true);
const verdictLine = doc.split('\n').find(line => line.startsWith('**Verdict:**'));
expect(verdictLine).toBeDefined();
expect(verdictLine).toContain('GREEN');
expect(verdictLine).toContain('0 real failures');
});
test('provenance records full M1 and master SHAs and the ancestry check exiting 0', () => {
const provenance = section(/^## 1\./);
expect(provenance).toContain(M1_SHA);
expect(provenance).toContain(MASTER_SHA);
const ancestry = tableRows(subsection(provenance, /Ancestry/));
expect(ancestry.length).toBeGreaterThan(0);
for (const [command, , exitCode] of ancestry) {
expect(command).toContain(`git merge-base --is-ancestor ${M1_SHA} `);
expect(exitCode).toBe('`0`');
}
expect(ancestry.some(([command]) => command!.includes(MASTER_SHA))).toBe(true);
});
test('sandbox git errors are preserved as exit 128, not reported as passes', () => {
const rows = tableRows(subsection(section(/^## 1\./), /Sandbox `git`/));
expect(rows.map(r => r[0])).toEqual([
'`git rev-parse HEAD`',
`\`git merge-base --is-ancestor ${M1_SHA} HEAD\``,
]);
for (const row of rows) {
expect(row[1]).toBe('`128`');
expect(row[2]).toContain('fatal:');
}
});
test('every AGENTS.md sandbox gate is listed with exit code 0 and zero failures', () => {
const rows = tableRows(section(/^## 2\./));
const gates = [
'bun run typecheck',
'bun run test:contracts',
'bun run lint:check',
'CARGO_BUILD_JOBS=2 bun run test:native',
'bun run test:utils',
'bun run test:calculations',
'CARGO_BUILD_JOBS=2 cargo test --workspace --lib --offline',
];
for (const gate of gates) {
const row = rows.find(cells => cells[0]!.startsWith(`\`${gate}\``));
expect(row, gate).toBeDefined();
expect(row![1]).toBe('`0`');
expect(row![2]).toMatch(/^PASS/);
expect(row![2]).toMatch(/0 (errors|fail|failed)/);
}
});
test('sandbox failure classification sums to the reported 40 failures, all sandbox-only', () => {
const triage = section(/^## 3\./);
expect(triage).toMatch(/`40 fail`/);
expect(triage).toMatch(/\*\*Real failures:\*\* \*\*`0`\*\*/);
const rows = tableRows(subsection(triage, /Failure Classification Summary/));
expect(rows).toHaveLength(10);
let total = 0;
let gitBoundary = 0;
let postgresBoundary = 0;
for (const [file, fails, errorText, classification] of rows) {
const path = file!.replace(/`/g, '');
expect(existsSync(join(repoRoot, path)), path).toBe(true);
const count = Number(fails!.match(/^(\d+)/)![1]);
total += count;
expect(classification).toContain('(a) Expected sandbox-only');
if (/stock_bot_safety/.test(errorText!)) {
expect(path.startsWith('test/postgres/')).toBe(true);
postgresBoundary += count;
} else {
expect(errorText).toMatch(/git|Cannot inspect tracked files/);
gitBoundary += count;
}
}
expect(total).toBe(40);
expect(gitBoundary).toBe(38);
expect(postgresBoundary).toBe(2);
});
test('individual failing test names match the per-file counts in the classification table', () => {
const triage = section(/^## 3\./);
const summary = new Map(
tableRows(subsection(triage, /Failure Classification Summary/)).map(([file, fails]) => [
file!.replace(/`/g, ''),
Number(fails!.match(/^(\d+)/)![1]),
]),
);
const lines = subsection(triage, /Individual Failing Test Names/).split('\n');
const listed = new Map<string, string[]>();
let current: string | undefined;
for (const line of lines) {
const header = line.match(/^\d+\. \*\*`([^`]+)`\*\* \((\d+) fails?;/);
if (header) {
current = header[1]!;
expect(Number(header[2])).toBe(summary.get(current)!);
listed.set(current, []);
continue;
}
const name = line.match(/^\s+- `(.+)`$/);
if (name && current) {listed.get(current)!.push(name[1]!);}
}
expect([...listed.keys()].sort()).toEqual([...summary.keys()].sort());
for (const [file, names] of listed) {
expect(names.length, file).toBe(summary.get(file)!);
expect(new Set(names).size, file).toBe(names.length);
}
});
test("Studio's authoritative build and test commands are recorded verbatim with exit 0", () => {
const rows = tableRows(section(/^## 4\./));
const build = rows.find(r => r[0]!.includes('Build'));
const tests = rows.find(r => r[0]!.includes('Test'));
expect(build?.[2]).toBe('`bun run build`');
expect(build?.[3]).toBe('`0`');
expect(tests?.[2]).toBe(`\`${STUDIO_TEST_COMMAND}\``);
expect(tests?.[3]).toBe('`0`');
for (const row of [build!, tests!]) {expect(row[4]).toContain('PASS');}
});
test('records all three M2 planning facts', () => {
const facts = section(/^## 5\./);
expect(facts).toContain('`@modelcontextprotocol/sdk` is **not present** in `bun.lock`');
expect(facts).toContain('`crates.io`, `index.crates.io`, and `static.crates.io`');
expect(facts).toContain('`python/tests/test_stockbot_sdk.py`');
expect(facts).toContain('**is not invoked**');
expect(facts).toContain('`docs/` does not exist');
expect(facts).toContain('`design/MILESTONES.md` lines 83–103');
expect(facts).toContain('left untouched');
});
});