import { existsSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { describe, expect, test } from 'bun:test'; // Guards the T-208 M2 master audit (design/m2-master-audit.md) against citation rot. // Every repo path it cites must still resolve, every symbol or test it reports as // landed must still be in the file it names, and the audited baseline must stay // pinned. The gaps it reports as missing are deliberately not asserted, so closing // a gap never breaks this test. const repoRoot = fileURLToPath(new URL('../../', import.meta.url)); const audit = readFileSync(join(repoRoot, 'design/m2-master-audit.md'), 'utf8'); const designIndex = readFileSync(join(repoRoot, 'design/README.md'), 'utf8'); // Cited as gaps or future outputs, so they must not be required to exist. const CITED_AS_ABSENT = new Set([ 'scripts/export-notebook-snapshot.ts', 'design/m2-close-evidence.md', 'docs/', 'docs/*.md', 'design/*.md', ]); function expandBraces(path: string): string[] { const match = /^(.*)\{([^}]+)\}(.*)$/.exec(path); if (!match) return [path]; const [, prefix, options, suffix] = match; return options!.split(',').flatMap((option) => expandBraces(`${prefix}${option}${suffix}`)); } function citedRepoPaths(markdown: string): string[] { const spans = [...markdown.matchAll(/`([^`\s]+)`/g)].map((m) => m[1]!); const repoRelative = spans.filter((span) => /^(apps|libs|tools|python|scripts|design|test|database|docs)\//.test(span) ); return [ ...new Set(repoRelative.map((span) => span.replace(/:[\d,-]+$/, '')).flatMap(expandBraces)), ] .filter((p) => !p.includes('*') || CITED_AS_ABSENT.has(p)) .sort(); } function lineAt(file: string, line: number): string { return readFileSync(join(repoRoot, file), 'utf8').split('\n')[line - 1] ?? ''; } describe('design/m2-master-audit.md citations', () => { test('cites only repo paths that exist, except gaps and future evidence files', () => { const cited = citedRepoPaths(audit); expect(cited.length).toBeGreaterThan(40); const missing = cited.filter((p) => !CITED_AS_ABSENT.has(p) && !existsSync(join(repoRoot, p))); expect(missing).toEqual([]); }); test('paths cited as gaps are still absent, and specs live under design/', () => { expect(existsSync(join(repoRoot, 'docs'))).toBe(false); expect(existsSync(join(repoRoot, 'design/MILESTONES.md'))).toBe(true); }); // [file, 1-based line the audit cites, text that line must contain] const lineCitations: Array<[file: string, line: number, expected: string]> = [ ['tools/curate-etl/src/parquet_posts.rs', 22, 'EVENT_KIND_POST'], ['tools/curate-etl/src/run_curate_posts.rs', 133, 'EventsDatasetExists'], ['tools/curate-etl/src/mongo_stream.rs', 53, 'pub enum SourceCollection'], ['tools/curate-etl/src/run_curate_fundamentals.rs', 113, 'pub struct CurateFundamentalsArgs'], ['tools/curate-etl/src/cli.rs', 98, 'CurateBars1m('], ['tools/curate-etl/src/cli.rs', 99, 'CurateQmBars1m('], ['tools/curate-etl/src/cli.rs', 100, 'CurateFx1m('], ['tools/curate-etl/src/cli.rs', 101, 'CuratePosts('], ['tools/curate-etl/src/cli.rs', 102, 'CurateFundamentals('], ['tools/curate-etl/src/cli.rs', 103, 'CurateMacro('], ['tools/curate-etl/src/cli.rs', 104, 'SendIlp('], ['tools/curate-etl/src/cli.rs', 1317, 'CliAction::CuratePosts'], ['tools/curate-etl/src/cli.rs', 1636, 'CliAction::CurateFundamentals'], ['tools/curate-etl/src/cli.rs', 1689, 'CliAction::CurateMacro'], ['tools/curate-etl/src/cli.rs', 2110, 'CliAction::CurateBars1m'], ['tools/curate-etl/src/cli.rs', 2277, 'CliAction::CurateQmBars1m'], ['tools/curate-etl/src/cli.rs', 2381, 'CliAction::CurateFx1m'], ['tools/curate-etl/src/cli.rs', 2461, 'CliAction::SendIlp'], ['tools/curate-etl/src/main.rs', 22, 'run_send_ilp'], ['tools/curate-etl/src/main.rs', 39, 'run_curate_qm_bars_1m'], [ 'tools/curate-etl/src/fundamentals_tests.rs', 104, 'fn restatement_of_q1_after_q2_updates_q1_and_ttm_without_overwriting_q2', ], [ 'tools/curate-etl/src/parquet_fundamentals_tests.rs', 1107, 'fn restatement_of_q1_after_q2_preserves_q2_on_disk_and_updates_ttm', ], [ 'tools/curate-etl/src/run_curate_bars_1m_tests.rs', 402, 'fn split_boundary_nvda_and_nexm_match_raw_bars_1d_closes', ], ['tools/curate-etl/tests/cli.rs', 1747, 'fn send_ilp_cli_help_missing_flag_success_and_partial_error_routing'], ['tools/curate-etl/tests/cli.rs', 1787, 'TcpListener::bind("127.0.0.1:0")'], [ 'tools/curate-etl/tests/cli.rs', 1877, 'fn send_ilp_cli_shard_gap_exits_without_partial_report_or_connect_and_bad_prefix_prints_usage', ], ['tools/curate-etl/tests/cli.rs', 1939, 'fn send_ilp_cli_ignores_questdb_env_vars_and_requires_explicit_addr'], [ 'tools/curate-etl/tests/curate_macro.rs', 178, 'fn cli_publishes_runner_equivalent_macro_parquet_with_pit_lag_and_no_forecast_leakage', ], [ 'tools/curate-etl/tests/curate_macro.rs', 471, 'fn cli_publishes_runner_equivalent_macro_parquet_and_excludes_forecast_sentinel', ], ['tools/curate-etl/tests/curate_macro.rs', 709, 'fn cli_rerun_on_same_inputs_is_byte_deterministic'], [ 'tools/curate-etl/tests/curate_qm_bars_1m.rs', 336, 'fn curate_qm_bars_1m_binary_matches_direct_runner_bytes_report_and_invariants', ], [ 'tools/curate-etl/tests/curate_qm_bars_1m.rs', 527, 'fn curate_qm_bars_1m_binary_refuses_overwrite_and_coexists_with_eod_bars_1m_shards', ], [ 'tools/curate-etl/tests/curate_qm_bars_1m.rs', 622, 'fn curate_qm_bars_1m_binary_applies_date_effective_mapping_and_reports_dropped_unmapped', ], ['tools/curate-etl/src/run_curate_fx_1m.rs', 24, 'bar-start `ts`'], ['libs/data/questdb/src/schema.ts', 33, 'fx_1m: {'], ['libs/data/questdb/src/schema.ts', 139, 'export function getM2IntradayDDL'], ['libs/data/duckdb/src/research-copilot-store.ts', 33, 'export const openResearchCopilotStore'], ['libs/data/duckdb/src/research-copilot-store.ts', 40, 'vaultOperatorAuthorized: false'], ['libs/data/duckdb/test/duckdb-market-data-store.test.ts', 251, 'fundamentalsAsOf enforces restatement non-overwrite'], ['libs/research/src/market-data-store-duckdb.ts', 1448, "'events/year=*/part.parquet'"], ['libs/research/src/market-data-store-duckdb.ts', 1529, "timeframe === '1m'"], ['libs/research/src/market-data-store-duckdb.ts', 2283, 'crossSection('], ['libs/research/src/market-data-store-duckdb.ts', 2287, 'async fundamentalsAsOf('], ['libs/research/src/market-data-store-duckdb.ts', 2294, 'macroAsOf('], ['libs/research/src/market-data-store-routed.ts', 185, 'return daily.fx(q)'], ['libs/research/src/mcp/tools/analyze-signal.ts', 122, 'discriminatedUnion'], ['libs/research/src/feature-library.ts', 29, 'RET_1D_V1'], ['libs/research/src/feature-library.ts', 61, 'export const FEATURE_INPUT_DATASETS'], ['libs/research/src/notebook-snapshot-node.ts', 67, 'export function createNodeNotebookCommandRunner'], ['scripts/research-copilot-mcp.ts', 77, 'export async function runResearchCopilotMcp'], ['python/stockbot/data.py', 21, '"ceo_post"'], ['python/stockbot/data.py', 71, 'events/year=*/part.parquet'], ['python/stockbot/data.py', 259, 'NotImplementedError'], ['package.json', 56, '"mcp:research-copilot"'], ['design/MILESTONES.md', 96, 'eodIntraday1M'], ['docker-compose.quant.yml', 35, '1048576'], ]; test.each(lineCitations)('%s:%d contains %p as cited', (file, line, expected) => { expect(audit).toContain(file.split('/').pop()!); expect(lineAt(file, line)).toContain(expected); }); test('pins the audited master baseline and accepted M1 separately from the review branch', () => { expect(audit).toContain('d45014a1f6dfc5ba5a73bcc0d6e57e486c2c7c52'); expect(audit).toContain('d52aa20bff3f5c259fb6e2bcfc2d3a78ad5224b1'); expect(audit).toContain('git rev-parse master'); expect(audit).toMatch(/not `git rev-parse HEAD`/); }); test('maps every M2 deliverable and exit criterion to a Done/Partial/Missing status', () => { for (const row of ['M2.1', 'M2.2', 'M2.3', 'M2.4', 'EC-1', 'EC-2', 'EC-3']) { const line = audit.split('\n').find((l) => l.startsWith(`| **${row}`)); expect(line, row).toBeDefined(); expect(line!).toMatch(/\|\s*\*\*(Done|Partial|Missing)\b/); } }); test('keeps host and quant checks pending rather than claiming them passed', () => { expect(audit).toMatch(/\*\*PENDING \(host closure\)\*\*/); expect(audit).toMatch(/\*\*PENDING \(host\/`quant`\)\*\*/); expect(audit).toMatch(/\*\*Partial \(Synthetic Done; Live `quant` Missing\)\*\*/); }); test('lists the seven approved backlog keys in dependency order', () => { const keys = [ 'm2-ceo-posts-events-compat', 'm2-store-macro-cross-section-fx1m', 'm2-data-pipeline-m2-inputs-and-probes', 'm2-curate-etl-mongo-and-financials-old', 'm2-python-sdk-m2-parity', 'm2-ec1-snapshot-cli-and-doc-paths', 'm2-host-quant-verification-and-closure', ]; const section = audit.slice(audit.indexOf('## 5. Ordered M2 Closure Backlog')); const rows = section.split('\n').filter((l) => /^\| \*\*\d\*\* \|/.test(l)); expect(rows.map((r) => /`(m2-[a-z0-9-]+)`/.exec(r)?.[1])).toEqual(keys); rows.forEach((row, i) => { for (const dep of [...row.split('|')[4]!.matchAll(/`(m2-[a-z0-9-]+)`/g)].map((m) => m[1]!)) { expect(keys.indexOf(dep), `${keys[i]} depends on ${dep}`).toBeLessThan(i); } }); }); test('proposes no CI workflow and is indexed in design/README.md', () => { expect(audit).not.toMatch(/\.github\/workflows/); expect(designIndex).toContain('[m2-master-audit.md](m2-master-audit.md)'); }); });