diff --git a/scripts/test-free-shards.ts b/scripts/test-free-shards.ts index 3ad2c1611..cb0f95525 100755 --- a/scripts/test-free-shards.ts +++ b/scripts/test-free-shards.ts @@ -391,8 +391,6 @@ export const TREE_MUTATING: Record = { 'test/catalog-mode-full.test.ts': 'regenerates ALL SKILL.md in full-catalog mode, then restores', 'test/spec-template-sync.test.ts': 'regenerates all SKILL.md in place to compare spec/SKILL.md', 'test/gen-skill-docs-idempotency.test.ts': 'regenerates all SKILL.md twice to prove idempotency', - 'test/gbrain-detection-override.test.ts': - 'regenerates SKILL.md in place with --respect-detection (gbrain variant), then git-restores — readers see inflated skeletons mid-window', // Ratchet readers (measure the tree; need it quiet): 'test/parity-suite.test.ts': 'RATCHET READER — parity caps measure live SKILL.md/section bytes', 'test/skill-size-budget.test.ts': 'RATCHET READER — per-skill and corpus size budgets measure the live tree', diff --git a/test/gbrain-detection-override.test.ts b/test/gbrain-detection-override.test.ts index 7ecae820b..361c38e75 100644 --- a/test/gbrain-detection-override.test.ts +++ b/test/gbrain-detection-override.test.ts @@ -9,7 +9,8 @@ * factory, opencode, openclaw, cursor, kiro). * * Tests drive gen-skill-docs as a subprocess against a temp GSTACK_HOME - * with each detection state, then assert what landed in the generated + * with each detection state, rendering into an isolated --out-dir (never + * writing the working tree), then assert what landed in the rendered * Claude-host SKILL.md. This is end-to-end through the actual override * pipeline — no mocking — so it catches regressions in either the loader * or the suppressedResolvers filter. @@ -18,9 +19,9 @@ * generation against the real repo; --host claude scopes to one host). */ -import { describe, test, expect, beforeAll, afterAll } from 'bun:test'; +import { describe, test, expect } from 'bun:test'; import { execFileSync } from 'child_process'; -import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs'; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs'; import { tmpdir } from 'os'; import { join } from 'path'; @@ -49,33 +50,29 @@ function makeFixture(detectionJson: string | null): FixtureEnv { } /** - * Run gen-skill-docs with --respect-detection and an isolated GSTACK_HOME. - * Returns the regenerated office-hours/SKILL.md content WITHOUT writing - * over the committed file: we use --dry-run to keep the working tree - * clean, then parse the output via re-reading the committed file... no, - * that doesn't work for dry-run since dry-run doesn't write. - * - * Approach: generate to a temp output dir by running gen-skill-docs in a - * temp checkout. Simpler alternative: actually regenerate, snapshot the - * file content, then git-checkout the committed version back. We use this - * since gen-skill-docs doesn't expose an output-path arg. + * Run gen-skill-docs with --respect-detection and an isolated GSTACK_HOME, + * rendering into a fresh --out-dir. The working tree is never written: the + * generator reads its inputs (templates, resolvers) from the repo but lands + * every output in the temp dir, which we snapshot and delete. This replaced + * the old mutate-then-restore approach (which regenerated the committed + * files in place and only restored the probe files, leaving every OTHER + * generated file rewritten — a partial-restore hazard for concurrent + * readers). */ function regenAndSnapshot(opts: { respectDetection: boolean; tmpHome: string; files: string[]; }): Map { - // Save committed content so we can restore after snapshotting. - const original = new Map(); - for (const f of opts.files) { - original.set(f, readFileSync(join(REPO_ROOT, f), 'utf-8')); - } + const outDir = mkdtempSync(join(tmpdir(), 'gbrain-detect-out-')); const args = [ 'run', 'scripts/gen-skill-docs.ts', '--host', 'claude', + '--out-dir', + outDir, ]; if (opts.respectDetection) args.push('--respect-detection'); @@ -87,17 +84,14 @@ function regenAndSnapshot(opts: { timeout: 30_000, }); - // Snapshot the regenerated content. + // Snapshot the rendered content from the out-dir. const snapshot = new Map(); for (const f of opts.files) { - snapshot.set(f, readFileSync(join(REPO_ROOT, f), 'utf-8')); + snapshot.set(f, readFileSync(join(outDir, f), 'utf-8')); } return snapshot; } finally { - // Always restore so the test leaves the working tree clean. - for (const [f, content] of original) { - writeFileSync(join(REPO_ROOT, f), content); - } + rmSync(outDir, { recursive: true, force: true }); } }