Files
msd-core/tests/next-decimal-roadmap-scan.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

219 lines
7.5 KiB
JavaScript

/**
* GSD Tools Tests — phase next-decimal ROADMAP.md scanning
*
* Covers issue #1865: next-decimal only scanned directory names in
* .planning/phases/ to determine the next available decimal number.
* It did not check ROADMAP.md entries. When agents add backlog items
* by writing ROADMAP.md + creating dirs without calling next-decimal,
* collisions occur.
*
* After the fix, next-decimal unions directory names AND ROADMAP.md
* phase headers before computing the next available number.
*/
const { describe, test, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const { runGsdTools, createTempProject, cleanup } = require('./helpers.cjs');
describe('phase next-decimal ROADMAP.md scanning (#1865)', () => {
let tmpDir;
beforeEach(() => {
tmpDir = createTempProject();
});
afterEach(() => {
cleanup(tmpDir);
});
test('directory-only scan still works (existing behavior)', () => {
// Create directory-based decimal phases
fs.mkdirSync(path.join(tmpDir, '.planning', 'phases', '999-backlog'), { recursive: true });
fs.mkdirSync(path.join(tmpDir, '.planning', 'phases', '999.1-idea-one'), { recursive: true });
fs.mkdirSync(path.join(tmpDir, '.planning', 'phases', '999.2-idea-two'), { recursive: true });
const result = runGsdTools('phase next-decimal 999', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.next, '999.3');
assert.deepStrictEqual(output.existing, ['999.1', '999.2']);
});
test('detects ROADMAP.md entries that have no directory', () => {
// Only ROADMAP.md has 999.1 and 999.2 — no directories exist
fs.writeFileSync(
path.join(tmpDir, '.planning', 'ROADMAP.md'),
[
'# Roadmap',
'',
'## v1.0',
'',
'### Phase 999: Backlog',
'**Goal:** Parking lot',
'',
'### Phase 999.1: First idea',
'**Goal:** Something',
'',
'### Phase 999.2: Second idea',
'**Goal:** Something else',
].join('\n')
);
const result = runGsdTools('phase next-decimal 999', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.next, '999.3', 'should skip past ROADMAP.md entries');
assert.deepStrictEqual(output.existing, ['999.1', '999.2']);
});
test('unions directories and ROADMAP.md entries (no duplicates)', () => {
// Directory has 999.1, ROADMAP.md has 999.1 and 999.3
fs.mkdirSync(path.join(tmpDir, '.planning', 'phases', '999.1-idea-one'), { recursive: true });
fs.writeFileSync(
path.join(tmpDir, '.planning', 'ROADMAP.md'),
[
'# Roadmap',
'',
'### Phase 999.1: First idea',
'**Goal:** Something',
'',
'### Phase 999.3: Third idea',
'**Goal:** Something more',
].join('\n')
);
const result = runGsdTools('phase next-decimal 999', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.next, '999.4', 'should be max of union + 1');
assert.deepStrictEqual(output.existing, ['999.1', '999.3']);
});
test('ROADMAP.md with higher decimal wins over directories', () => {
// Directories: 999.1, 999.2. ROADMAP.md: 999.1, 999.5
fs.mkdirSync(path.join(tmpDir, '.planning', 'phases', '999.1-one'), { recursive: true });
fs.mkdirSync(path.join(tmpDir, '.planning', 'phases', '999.2-two'), { recursive: true });
fs.writeFileSync(
path.join(tmpDir, '.planning', 'ROADMAP.md'),
[
'# Roadmap',
'',
'### Phase 999.1: One',
'**Goal:** A',
'',
'### Phase 999.5: Five',
'**Goal:** E',
].join('\n')
);
const result = runGsdTools('phase next-decimal 999', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.next, '999.6');
assert.deepStrictEqual(output.existing, ['999.1', '999.2', '999.5']);
});
test('handles missing ROADMAP.md gracefully', () => {
// No ROADMAP.md, just directories
fs.mkdirSync(path.join(tmpDir, '.planning', 'phases', '05.1-patch'), { recursive: true });
const result = runGsdTools('phase next-decimal 5', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.next, '05.2');
});
test('handles empty phases dir with ROADMAP.md entries', () => {
// Empty phases dir but ROADMAP.md has entries
fs.writeFileSync(
path.join(tmpDir, '.planning', 'ROADMAP.md'),
[
'# Roadmap',
'',
'### Phase 10.1: Widget',
'**Goal:** Build widget',
'',
'### Phase 10.2: Gadget',
'**Goal:** Build gadget',
].join('\n')
);
const result = runGsdTools('phase next-decimal 10', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.next, '10.3');
assert.deepStrictEqual(output.existing, ['10.1', '10.2']);
});
test('handles no phases dir and no ROADMAP.md', () => {
// Remove the phases directory entirely
// eslint-disable-next-line local/no-raw-rmsync-in-tests -- mid-test removal to simulate absent phases dir (SUT behavior, not teardown)
fs.rmSync(path.join(tmpDir, '.planning', 'phases'), { recursive: true });
const result = runGsdTools('phase next-decimal 999', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.next, '999.1');
assert.deepStrictEqual(output.existing, []);
});
test('does not match unrelated phase numbers in ROADMAP.md', () => {
// ROADMAP.md has 99.1 and 9.1 — neither should match when querying 999
fs.writeFileSync(
path.join(tmpDir, '.planning', 'ROADMAP.md'),
[
'# Roadmap',
'',
'### Phase 99.1: Close but not 999',
'**Goal:** Unrelated',
'',
'### Phase 9.1: Also unrelated',
'**Goal:** Nope',
'',
'### Phase 9990.1: Prefix mismatch',
'**Goal:** Should not match 999',
].join('\n')
);
const result = runGsdTools('phase next-decimal 999', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.next, '999.1', 'should not be confused by unrelated phases');
assert.deepStrictEqual(output.existing, []);
});
test('handles leading-zero phase numbers in ROADMAP.md', () => {
// ROADMAP.md uses zero-padded phase numbers
fs.writeFileSync(
path.join(tmpDir, '.planning', 'ROADMAP.md'),
[
'# Roadmap',
'',
'### Phase 007.1: Secret agent fix',
'**Goal:** Classified',
'',
'### Phase 007.2: Another fix',
'**Goal:** Also classified',
].join('\n')
);
const result = runGsdTools('phase next-decimal 7', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
// normalizePhaseName('7') pads to '07'
assert.strictEqual(output.next, '07.3');
assert.deepStrictEqual(output.existing, ['07.1', '07.2']);
});
});