Files
msd-core/tests/6-validate-cjs-drift-regression.test.cjs
Tom Boucher a28dcec981 chore(#597): replace count-based ratchet guards with AST lint + named-set allowlists (#603)
The windows-test-parity ratchet greps test source for fs.rmSync-without-
maxRetries (and six other Windows-portability anti-patterns), failing when an
integer offender COUNT exceeds a frozen baseline (rmSync: 95). A count ratchet
is a Goodhart metric: fixing one offender and adding another keeps the count
constant, so a new defect slips through green. Replace it — and every other
count ratchet in the repo — with a layered, masking-proof design.

Behavioral seam test
- tests/helpers-cleanup.test.cjs proves helpers.cleanup() carries the Windows
  EBUSY retry budget. cleanup() delegates retries to Node's fs.rmSync via
  maxRetries (it owns no loop), so the test asserts the option contract
  (recursive/force/maxRetries>0/retryDelay>0) + real-FS removal + the cwd-guard,
  rather than a loop that does not exist. The EBUSY risk is now tested ONCE at
  the helper, not approximated textually at every call site.

Write-time ESLint rule (AST-accurate, replaces the grep)
- eslint-rules/no-raw-rmsync-in-tests.cjs (error in tests/**/*.test.cjs) bans
  raw fs.rmSync, steering to cleanup(). Catches member, computed (fs['rmSync']),
  destructured and aliased forms; escape hatch is inline
  `// eslint-disable-next-line local/no-raw-rmsync-in-tests -- <reason>` only.
- Migrated 336 raw fs.rmSync teardown calls across ~116 test files to cleanup().
  ~18 genuinely load-bearing sites (mid-test SUT/fault-injection removals,
  error-swallowing or name-colliding local teardown helpers) keep the raw call
  with an inline eslint-disable + reason.

Shared anti-ratchet primitive
- scripts/lib/allowlist-ratchet.cjs:
  - assertWithinAllowlist: fails on NOVEL ids (new offender introduced) AND on
    STALE ids (a known offender was fixed but not pruned) — identity, not count,
    and a ratchet DOWN toward zero.
  - assertTightCeiling: a size/length budget whose ceiling must stay within a
    grace band of the high-water mark, so budgets may only tighten, never creep.

Ratchets converted onto the primitive
- windows-test-parity-guard.test.cjs: rmSync rule deleted (now ESLint-enforced);
  the remaining six patterns moved from integer baselines to named-set
  allowlists with ratchet-down.
- scripts/lint-test-file-count.{cjs,allowlist.json}: per-module integer counts →
  named filename sets (closes the swap-a-file-keep-the-count blind spot); a
  module dropping under cap now FAILS to force pruning its allowlist entry.
- enh-2790 skill-count `<= 63` → named skill allowlist (ratchets toward ~58).

Size budgets hardened (tighten-only)
- agent-size / workflow-size / feat-3039 help-tiered: ceilings lowered to the
  current high-water mark and an assertTightCeiling anti-creep check added per
  tier. Fixed external-contract limits (description ≤100 chars, agent ≤100 KB)
  are intentionally left as-is — they are not grandfathered creeping budgets.

No user-facing behavior change (tests + tooling only); no USER_FACING_PREFIXES
touched, so no changeset fragment is required.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-01 22:43:49 -04:00

305 lines
12 KiB
JavaScript

'use strict';
/**
* Regression tests for issue #6 (open-gsd/gsd-core):
* Three validation behaviors present in validate.ts are missing from verify.cjs,
* producing silent false negatives on the CJS production path.
*
* Three drift items fixed by porting phaseVariants() and activeDiskPhases to verify.cjs:
*
* 1. W007 activeDiskPhases — verify.cjs uses diskPhases (includes archived) for the
* W007 check; validate.ts uses activeDiskPhases (active phasesDir only). Archived
* phases absent from current ROADMAP trigger false W007 in verify.cjs.
*
* 2. phaseVariants() normalization — validate.ts has a phaseVariants() function
* generating padded/unpadded/letter-suffix variants for matching. verify.cjs uses
* only parseInt→padded (drops letter suffix), causing false W006/W007 for
* letter-suffix phases with padding mismatch between ROADMAP and disk.
*
* 3. W006 unchecked-phase variant skip — same phaseVariants() gap causes false W006
* for phases with padding mismatch: ROADMAP says "3B", disk has "03B-foo", but
* verify.cjs padded("3B") = "03" (drops letter) → "03B" on disk not matched.
*
* References:
* - ADR-3524 (docs/adr/3524-cjs-sdk-hard-seam.md)
* - Issue #6 (open-gsd/gsd-core)
* - PR #154 (issue #4) — precedent for the generator pattern
*/
const { describe, test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const { runGsdTools, cleanup } = require('./helpers.cjs');
// ── Fixture helpers ──────────────────────────────────────────────────────────
function mkplanning(base) {
const planningDir = path.join(base, '.planning');
const phasesDir = path.join(planningDir, 'phases');
fs.mkdirSync(phasesDir, { recursive: true });
return { planningDir, phasesDir };
}
function writeProjectMd(planningDir) {
fs.writeFileSync(
path.join(planningDir, 'PROJECT.md'),
'# Project\n\n## What This Is\nTest.\n\n## Core Value\nTest.\n\n## Requirements\nTest.\n',
);
}
function writeStateMd(planningDir, phase = '2') {
fs.writeFileSync(
path.join(planningDir, 'STATE.md'),
`# State\n\n**Current Phase:** ${phase}\n**Status:** In progress\n`,
);
}
function writeConfigJson(planningDir) {
fs.writeFileSync(
path.join(planningDir, 'config.json'),
JSON.stringify({ model_profile: 'balanced' }),
);
}
// ── Drift Item 1: W007 activeDiskPhases ────────────────────────────────────
//
// Project has a shipped milestone archive (milestones/v1.0-phases/) with old phase
// "1" inside, and active phasesDir with phase "2". Current ROADMAP only mentions
// Phase 2 (v1.0 phases were shipped and removed from ROADMAP).
//
// validate.ts: activeDiskPhases = phases from active phasesDir only (not archived).
// "1" is only in diskPhases (via forEachArchivedPhaseToken), not activeDiskPhases.
// W007 iterates activeDiskPhases → "1" never checked → no false W007.
//
// verify.cjs pre-fix: diskPhases = collectDiskPhases() + forEachArchivedPhaseToken().
// diskPhases includes "1" (from old archive). W007 iterates diskPhases → "1" not
// in roadmapPhases → W007 fires for "1". False positive.
describe('Drift item 1 — W007 activeDiskPhases: no false W007 for archived phases', () => {
let tmpDir;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-6-d1-'));
const { planningDir, phasesDir } = mkplanning(tmpDir);
writeProjectMd(planningDir);
writeStateMd(planningDir);
writeConfigJson(planningDir);
fs.writeFileSync(
path.join(planningDir, 'ROADMAP.md'),
[
'# Roadmap',
'',
'- [x] **Phase 2:** API',
'',
'### Phase 2: API',
'',
'## Progress',
'| Phase | Status |',
'|-------|--------|',
'| 2 | Complete |',
].join('\n'),
);
// Active phasesDir: only phase 2
fs.mkdirSync(path.join(phasesDir, '2-api'), { recursive: true });
// Current active milestone archive: v1.1-phases contains phase 2 (in ROADMAP)
fs.mkdirSync(
path.join(planningDir, 'milestones', 'v1.1-phases', '2-api'),
{ recursive: true },
);
// Old shipped milestone archive: v1.0-phases contains phase 1 (no longer in ROADMAP)
// v1.0 sorts before v1.1 so getActiveMilestoneArchiveDir returns v1.1.
// forEachArchivedPhaseToken walks BOTH v1.0 and v1.1, so diskPhases gets "1".
// collectDiskPhases (activeDiskPhases) only uses v1.1 (active archive) — "1" excluded.
fs.mkdirSync(
path.join(planningDir, 'milestones', 'v1.0-phases', '1-foundation'),
{ recursive: true },
);
});
after(() => {
cleanup(tmpDir);
});
test('no W007 for archived phase "1" absent from current ROADMAP', () => {
// validate.ts: activeDiskPhases has only "2" (from active phasesDir + v1.1 archive).
// "1" is in diskPhases (forEachArchivedPhaseToken walks v1.0) but NOT activeDiskPhases.
// W007 iterates activeDiskPhases → "1" never checked → no false W007. Correct.
// verify.cjs pre-fix: diskPhases = collectDiskPhases + forEachArchivedPhaseToken.
// diskPhases includes "1" (from v1.0 archive). W007 iterates diskPhases → "1" not
// in roadmapPhases → W007 fires for "1". False positive.
const result = runGsdTools(['validate', 'health', '--json'], tmpDir);
assert.strictEqual(result.success, true, `unexpected failure: ${result.error}`);
const data = JSON.parse(result.output);
const w007 = (data.warnings ?? []).filter((w) => w.code === 'W007');
// Filter to W007 that mentions only phase "1" (not "1A", "01A", etc.)
const w007Phase1 = w007.filter(
(w) => /\bPhase 1\b/i.test(w.message) && !/\b1[A-Z]\b/i.test(w.message),
);
assert.strictEqual(
w007Phase1.length,
0,
`Expected no W007 for archived phase 1 (v1.0 archive), got: ${JSON.stringify(w007)}`,
);
});
});
// ── Drift Item 2: phaseVariants() normalization ────────────────────────────
//
// ROADMAP has "### Phase 01A:" (zero-padded letter-suffix heading).
// Disk has directory "1A-foo" (unpadded letter-suffix form).
// These should match because phaseVariants("01A") = {"01A", "1A", "01A"}.
//
// validate.ts: diskPhases has "1A". phaseVariants("01A") includes "1A" → match → no W006.
// activeDiskPhases has "1A". phaseVariants("1A") includes "01A" → roadmapPhaseVariants
// has "01A" → match → no W007.
//
// verify.cjs pre-fix:
// W006 loop for "01A": padded = String(parseInt("01A",10)).padStart(2,'0') = "01".
// diskPhases.has("01A")? NO. diskPhases.has("01")? NO. → W006 fires. Bug.
// W007 loop for "1A": unpadded = String(parseInt("1A",10)) = "1".
// roadmapPhases.has("1A")? NO. roadmapPhases.has("1")? NO. → W007 fires. Bug.
describe('Drift item 2 — phaseVariants() normalization: letter-suffix zero-padding mismatch', () => {
let tmpDir;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-6-d2-'));
const { planningDir, phasesDir } = mkplanning(tmpDir);
writeProjectMd(planningDir);
writeStateMd(planningDir);
writeConfigJson(planningDir);
// ROADMAP: Phase 01A (zero-padded + letter suffix)
fs.writeFileSync(
path.join(planningDir, 'ROADMAP.md'),
[
'# Roadmap',
'',
'- [x] **Phase 01A:** Suffix Phase',
'',
'### Phase 01A: Suffix Phase',
'',
'## Progress',
'| Phase | Status |',
'|-------|--------|',
'| 01A | Complete |',
].join('\n'),
);
// Disk: unpadded form "1A-foo"
fs.mkdirSync(path.join(phasesDir, '1A-suffix-phase'), { recursive: true });
});
after(() => {
cleanup(tmpDir);
});
test('no false W006 when ROADMAP says 01A and disk has 1A-... (phaseVariants normalizes)', () => {
const result = runGsdTools(['validate', 'health', '--json'], tmpDir);
assert.strictEqual(result.success, true, `unexpected failure: ${result.error}`);
const data = JSON.parse(result.output);
const w006 = (data.warnings ?? []).filter((w) => w.code === 'W006');
assert.strictEqual(
w006.length,
0,
`Expected no W006 (01A == 1A after normalization), got: ${JSON.stringify(w006)}`,
);
});
test('no false W007 when disk has 1A and ROADMAP says 01A (phaseVariants normalizes)', () => {
const result = runGsdTools(['validate', 'health', '--json'], tmpDir);
assert.strictEqual(result.success, true, `unexpected failure: ${result.error}`);
const data = JSON.parse(result.output);
const w007 = (data.warnings ?? []).filter((w) => w.code === 'W007');
assert.strictEqual(
w007.length,
0,
`Expected no W007 (1A on disk matches 01A in ROADMAP), got: ${JSON.stringify(w007)}`,
);
});
});
// ── Drift Item 3: W006 false positive when disk uses zero-padded letter form ─
//
// ROADMAP has "### Phase 3B:" (unpadded letter-suffix heading).
// Disk has directory "03B-feature" (zero-padded letter-suffix form).
//
// validate.ts:
// diskPhases has "03B". phaseVariants("3B") = {"3B","3B","03B"}.
// existsOnDisk = diskPhases.has("03B") = TRUE → no W006.
// activeDiskPhases has "03B". phaseVariants("03B") = {"03B","3B","03B"}.
// roadmapPhaseVariants has {"3B","3B","03B"} → "03B" found → no W007.
//
// verify.cjs pre-fix:
// W006 for "3B": padded = parseInt("3B")=3 → "03" (drops "B").
// diskPhases.has("3B")? NO. diskPhases.has("03")? NO (dir is "03B" not "03"). → W006 fires.
// W007 for "03B": unpadded = String(parseInt("03B",10)) = "3".
// roadmapPhases.has("03B")? NO. roadmapPhases.has("3")? NO (ROADMAP has "3B" not "3"). → W007.
describe('Drift item 3 — W006 false positive when disk has zero-padded letter form', () => {
let tmpDir;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-6-d3-'));
const { planningDir, phasesDir } = mkplanning(tmpDir);
writeProjectMd(planningDir);
writeStateMd(planningDir, '3B');
writeConfigJson(planningDir);
// ROADMAP: Phase 3B (unpadded in heading)
fs.writeFileSync(
path.join(planningDir, 'ROADMAP.md'),
[
'# Roadmap',
'',
'- [x] **Phase 3B:** Feature Extension',
'',
'### Phase 3B: Feature Extension',
'',
'## Progress',
'| Phase | Status |',
'|-------|--------|',
'| 3B | Complete |',
].join('\n'),
);
// Disk: "03B-feature" (zero-padded letter-suffix form)
fs.mkdirSync(path.join(phasesDir, '03B-feature'), { recursive: true });
});
after(() => {
cleanup(tmpDir);
});
test('no false W006 when ROADMAP says 3B and disk has 03B-... (phaseVariants covers zero-padded)', () => {
const result = runGsdTools(['validate', 'health', '--json'], tmpDir);
assert.strictEqual(result.success, true, `unexpected failure: ${result.error}`);
const data = JSON.parse(result.output);
const w006 = (data.warnings ?? []).filter((w) => w.code === 'W006');
assert.strictEqual(
w006.length,
0,
`Expected no W006 (3B == 03B after normalization), got: ${JSON.stringify(w006)}`,
);
});
test('no false W007 when disk has 03B and ROADMAP says 3B (phaseVariants covers both forms)', () => {
const result = runGsdTools(['validate', 'health', '--json'], tmpDir);
assert.strictEqual(result.success, true, `unexpected failure: ${result.error}`);
const data = JSON.parse(result.output);
const w007 = (data.warnings ?? []).filter((w) => w.code === 'W007');
assert.strictEqual(
w007.length,
0,
`Expected no W007 (03B on disk matches 3B in ROADMAP), got: ${JSON.stringify(w007)}`,
);
});
});