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-203 M2 gap audit (design/m2-gap-audit.md) against citation rot: // every repo path it cites must resolve, and every symbol it reports as // "present" must still live in the file it names. Gaps the audit reports as // missing are deliberately NOT asserted, so landing a fix never breaks this test. const repoRoot = fileURLToPath(new URL('../../', import.meta.url)); const auditPath = join(repoRoot, 'design/m2-gap-audit.md'); const audit = readFileSync(auditPath, 'utf8'); // Paths the audit cites as absent (the gap itself) or as the wrong location. const CITED_AS_ABSENT = new Set(['scripts/research-copilot-mcp.ts', 'docs/']); 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 pathLike = spans.filter( (span) => /^(apps|libs|tools|python|scripts|design|test|docs)\//.test(span) || /^[A-Za-z0-9_.-]+\.(md|json|yml)$/.test(span) ); return [...new Set(pathLike.map((span) => span.replace(/:\d+$/, '')).flatMap(expandBraces))].sort(); } describe('design/m2-gap-audit.md citations', () => { test('cites only repo paths that exist, except those it reports as absent', () => { const cited = citedRepoPaths(audit); expect(cited.length).toBeGreaterThan(30); const missing = cited.filter((p) => !CITED_AS_ABSENT.has(p) && !existsSync(join(repoRoot, p))); expect(missing).toEqual([]); }); test('every path listed as absent is actually absent in the audited checkout or is the docs/ relocation note', () => { // docs/ is cited because milestone docs moved to design/; it must stay absent for that note to hold. expect(existsSync(join(repoRoot, 'docs'))).toBe(false); expect(existsSync(join(repoRoot, 'design/MILESTONES.md'))).toBe(true); }); // Symbols the audit reports as PRESENT, keyed by the file it attributes them to. const presentSymbols: Array<[file: string, symbol: string | RegExp]> = [ ['libs/research/src/mcp/tools/query-data.ts', 'createQueryDataTool'], ['libs/research/src/mcp/tools/query-data.ts', 'fenceVaultQueryBounds'], ['libs/research/src/mcp/tools/query-data.ts', "'vault_fenced'"], ['libs/research/src/mcp/adapters/postgres-copilot.ts', 'createPostgresResearchCopilotServer'], ['libs/data/duckdb/src/research-copilot-store.ts', 'openResearchCopilotStore'], ['libs/data/postgres/src/research-copilot-executor.ts', 'openResearchCopilotExecutor'], ['libs/research/src/notebook-snapshot.ts', 'exportNotebookSnapshot'], ['libs/research/src/mcp/host/stdio-host.ts', 'runResearchCopilotStdioHost'], ['libs/research/src/feature-library.ts', 'registerSignal'], ['libs/research/src/feature-library.ts', 'runFeatureBatch'], ['libs/data/questdb/src/schema.ts', 'M2_INTRADAY_SCHEMAS'], ['libs/data/questdb/src/schema.ts', 'getM2IntradayDDL'], ['libs/research/src/market-data-store-routed.ts', 'createRoutedMarketDataStore'], ['libs/research/src/market-data-store-questdb.ts', 'createQuestDbIntradayBarsReader'], ['tools/curate-etl/src/cli.rs', 'CurateMacro('], ['tools/curate-etl/src/cli.rs', 'SendIlp('], ['tools/curate-etl/src/cli.rs', 'CuratePosts('], ['tools/curate-etl/src/cli.rs', 'CurateFundamentals('], ['tools/curate-etl/src/cli.rs', 'CurateBars1m('], ['tools/curate-etl/src/cli.rs', 'CurateFx1m('], ['tools/curate-etl/src/run_curate_bars_1m_tests.rs', 'fn split_boundary_nvda_and_nexm_match_raw_bars_1d_closes'], ['tools/curate-etl/src/run_curate_posts.rs', 'EventsDatasetExists'], ['tools/curate-etl/src/run_curate_qm_bars_1m.rs', /pub fn run_curate_qm_bars_1m\b/], ['tools/curate-etl/src/fundamentals.rs', 'build_fundamentals_rows'], ]; test.each(presentSymbols)('%s defines %p as the audit reports', (file, symbol) => { const source = readFileSync(join(repoRoot, file), 'utf8'); if (typeof symbol === 'string') expect(source).toContain(symbol); else expect(source).toMatch(symbol); }); test('pins QuestDB 10.2.0 in every compose file the audit cites', () => { for (const file of ['docker-compose.yml', 'docker-compose.dev.yml', 'docker-compose.prod.yml']) { expect(readFileSync(join(repoRoot, file), 'utf8')).toContain('questdb/questdb:10.2.0'); } }); test('lists the three closure-checklist sections the task requires', () => { expect(audit).toMatch(/^## 2\. Verified on `master`/m); expect(audit).toMatch(/^## 3\. Remaining M2 Code Gaps/m); expect(audit).toMatch(/^## 4\. Manual Host-Only Closure Gates/m); }); test('keeps master verification pending rather than claiming unverified passes', () => { expect(audit).toContain('**OPEN'); expect(audit).toMatch(/PENDING/); expect(audit).not.toMatch(/\|\s*\*\*PASS\*\*\s*\|/); }); });