Files
msd-core/tests/bug-416-archive-dir-null.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

213 lines
9.0 KiB
JavaScript
Raw 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.
'use strict';
/**
* Regression tests for issue #416 (open-gsd/gsd-core).
*
* Bug: getActiveMilestoneArchiveDir falls back to the newest archive directory
* when STATE.md names a milestone that has no matching archive yet, producing
* W007 false positives for phases from a prior (completed) milestone.
*
* Fix: when STATE.md is present and parseable and names a milestone, but no
* milestones/<vX.Y>-phases/ directory matches, return null. The version-sort
* fallback to the newest archive fires only when STATE.md is absent or
* unparseable.
*
* Knuth invariant: the resolver answers one question —
* "what archive directory holds the active milestone's phases?"
* Answer space: <dir> | null.
*/
process.env.GSD_TEST_MODE = '1';
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');
// ── helpers ──────────────────────────────────────────────────────────────────
function mkplanning(base) {
const planningDir = path.join(base, '.planning');
const phasesDir = path.join(planningDir, 'phases');
fs.mkdirSync(phasesDir, { recursive: true });
return planningDir;
}
function writeMinimalRoadmap(planningDir, phases) {
// phases: array of { num, name, checked }
const checkboxes = phases.map(({ num, name, checked }) =>
`- [${checked ? 'x' : ' '}] **Phase ${num}:** ${name}`,
).join('\n');
const headings = phases.map(({ num, name }) =>
`### Phase ${num}: ${name}\n**Goal:** Completed.\n`,
).join('\n');
fs.writeFileSync(
path.join(planningDir, 'ROADMAP.md'),
`# Roadmap\n\n${checkboxes}\n\n${headings}`,
);
}
function writeStateMdMilestone(planningDir, milestone) {
fs.writeFileSync(
path.join(planningDir, 'STATE.md'),
`# State\n\n**milestone:** ${milestone}\n**Current Phase:** 23\n**Status:** In progress\n`,
);
}
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 writeConfigJson(planningDir) {
fs.writeFileSync(
path.join(planningDir, 'config.json'),
JSON.stringify({ model_profile: 'balanced' }),
);
}
function mkArchivePhases(planningDir, version, phaseNums) {
// Creates .planning/milestones/<version>-phases/<NN>-phase-name/ dirs
const archiveDir = path.join(planningDir, 'milestones', `${version}-phases`);
for (const num of phaseNums) {
const padded = String(num).padStart(2, '0');
fs.mkdirSync(path.join(archiveDir, `${padded}-phase-${num}`), { recursive: true });
}
return archiveDir;
}
// ─────────────────────────────────────────────────────────────────────────────
// Case 1: STATE.md milestone: v6.0, only v5.0-phases/ on disk
// → resolver returns null, verifier emits zero W007 for phases 17–22
// ─────────────────────────────────────────────────────────────────────────────
describe('bug #416 case 1: STATE.md v6.0 with only v5.0-phases/ on disk → null, no W007', () => {
let tmpDir;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-416-c1-'));
const planningDir = mkplanning(tmpDir);
writeProjectMd(planningDir);
writeConfigJson(planningDir);
// Active milestone is v6.0 — no archive for it yet (phases live in flat phases/)
writeStateMdMilestone(planningDir, 'v6.0');
// v5.0 was the prior completed milestone; its archive exists on disk
mkArchivePhases(planningDir, 'v5.0', [17, 18, 19, 20, 21, 22]);
// ROADMAP reflects only v6.0 phases (v5.0 phases are in a prior milestone,
// not in the current roadmap section)
writeMinimalRoadmap(planningDir, [
{ num: 23, name: 'New Foundation', checked: false },
]);
});
after(() => { cleanup(tmpDir); });
test('validate health emits zero W007 warnings (no prior-milestone phases surfaced)', () => {
const result = runGsdTools(['validate', 'health', '--json'], tmpDir);
assert.strictEqual(result.success, true, `validate health failed: ${result.error}`);
const data = JSON.parse(result.output);
const w007 = (data.warnings ?? []).filter((w) => w.code === 'W007');
assert.strictEqual(
w007.length,
0,
`Expected zero W007 — phases 17–22 from v5.0-phases/ must not appear as "active".\nGot: ${JSON.stringify(w007, null, 2)}`,
);
});
});
// ─────────────────────────────────────────────────────────────────────────────
// Case 2: No STATE.md, multiple archives on disk → version-sort fallback
// returns the highest-versioned archive (existing behavior preserved)
// ─────────────────────────────────────────────────────────────────────────────
describe('bug #416 case 2: no STATE.md + multiple archives → version-sort fallback to newest', () => {
let tmpDir;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-416-c2-'));
const planningDir = mkplanning(tmpDir);
writeProjectMd(planningDir);
writeConfigJson(planningDir);
// No STATE.md — resolver must use the version-sort fallback
// Two archives: v4.0 and v5.0; v5.0 is newer
mkArchivePhases(planningDir, 'v4.0', [10, 11, 12]);
mkArchivePhases(planningDir, 'v5.0', [17, 18, 19]);
// ROADMAP lists v5.0 phases so W007 does not fire
writeMinimalRoadmap(planningDir, [
{ num: 17, name: 'Alpha', checked: true },
{ num: 18, name: 'Beta', checked: true },
{ num: 19, name: 'Gamma', checked: true },
]);
});
after(() => { cleanup(tmpDir); });
test('validate health succeeds and does not emit W007 for v5.0 archive phases', () => {
const result = runGsdTools(['validate', 'health', '--json'], tmpDir);
assert.strictEqual(result.success, true, `validate health failed: ${result.error}`);
const data = JSON.parse(result.output);
const w007 = (data.warnings ?? []).filter((w) => w.code === 'W007');
// v5.0 phases (17–19) are in the archive returned by the fallback and
// in the ROADMAP, so no W007 should fire.
assert.strictEqual(
w007.length,
0,
`Expected zero W007 for v5.0 archive phases present in ROADMAP.\nGot: ${JSON.stringify(w007, null, 2)}`,
);
});
});
// ─────────────────────────────────────────────────────────────────────────────
// Case 3: STATE.md milestone: v5.0, matching v5.0-phases/ exists → returns it
// (regression guard — happy path must not break)
// ─────────────────────────────────────────────────────────────────────────────
describe('bug #416 case 3: STATE.md v5.0 with matching v5.0-phases/ → returns archive dir', () => {
let tmpDir;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-416-c3-'));
const planningDir = mkplanning(tmpDir);
writeProjectMd(planningDir);
writeConfigJson(planningDir);
// STATE.md names v5.0 and a matching archive exists
writeStateMdMilestone(planningDir, 'v5.0');
mkArchivePhases(planningDir, 'v5.0', [17, 18, 19, 20, 21, 22]);
// ROADMAP lists v5.0 phases so W007 does not fire
writeMinimalRoadmap(planningDir, [
{ num: 17, name: 'Alpha', checked: true },
{ num: 18, name: 'Beta', checked: true },
{ num: 19, name: 'Gamma', checked: true },
{ num: 20, name: 'Delta', checked: true },
{ num: 21, name: 'Epsilon', checked: true },
{ num: 22, name: 'Zeta', checked: true },
]);
});
after(() => { cleanup(tmpDir); });
test('validate health emits zero W007 — archive phases are in ROADMAP and active', () => {
const result = runGsdTools(['validate', 'health', '--json'], tmpDir);
assert.strictEqual(result.success, true, `validate health failed: ${result.error}`);
const data = JSON.parse(result.output);
const w007 = (data.warnings ?? []).filter((w) => w.code === 'W007');
assert.strictEqual(
w007.length,
0,
`Expected zero W007 for matching v5.0 archive with v5.0 in STATE.md.\nGot: ${JSON.stringify(w007, null, 2)}`,
);
});
});