Files
msd-core/tests/bug-3584-runtime-slash-formatter.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

309 lines
11 KiB
JavaScript

/**
* Regression tests for bug #3584
*
* Runtime/user-facing strings emitted by get-shit-done/bin/lib/*.cjs hardcoded
* the deprecated `/gsd:<cmd>` colon form (16 files, ~50 occurrences). After
* #2808 unified GSD installs to register skills under the hyphen form
* (`name: gsd-execute-phase`), pasting the emitted `/gsd:execute-phase` into
* Claude Code yields `Unknown command: /gsd:execute-phase. Did you mean
* /gsd-execute-phase?`. Codex installs require `$gsd-<cmd>` (shell-var) form.
*
* Fix: a runtime-aware slash formatter (`runtime-slash.cjs`) is now the single
* source of truth for emitting `/gsd-<cmd>` (hyphen) for skills-based runtimes
* and `$gsd-<cmd>` for Codex. Tests assert on the formatter's typed output and
* — for the integration tests in `bug-3584-runtime-slash-emitters.test.cjs` —
* on the structured `--json` payloads from the runtime command handlers.
*/
'use strict';
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const path = require('path');
const ROOT = path.join(__dirname, '..');
const { formatGsdSlash, resolveRuntime } = require(
path.join(ROOT, 'get-shit-done', 'bin', 'lib', 'runtime-slash.cjs'),
);
const { cleanup } = require('./helpers.cjs');
describe('formatGsdSlash — runtime-aware slash command formatter', () => {
describe('hyphen-form runtimes (claude, cursor, opencode, kilo, etc.)', () => {
test('emits /gsd-<cmd> for claude', () => {
assert.strictEqual(formatGsdSlash('execute-phase', 'claude'), '/gsd-execute-phase');
});
test('emits /gsd-<cmd> for cursor', () => {
assert.strictEqual(formatGsdSlash('plan-phase', 'cursor'), '/gsd-plan-phase');
});
test('emits /gsd-<cmd> for opencode', () => {
assert.strictEqual(formatGsdSlash('discuss-phase', 'opencode'), '/gsd-discuss-phase');
});
test('emits /gsd-<cmd> for kilo', () => {
assert.strictEqual(formatGsdSlash('health', 'kilo'), '/gsd-health');
});
test('unknown runtime defaults to hyphen form', () => {
assert.strictEqual(
formatGsdSlash('new-project', 'some-future-runtime'),
'/gsd-new-project',
);
});
test('null/undefined runtime defaults to hyphen form (claude)', () => {
assert.strictEqual(formatGsdSlash('new-milestone', null), '/gsd-new-milestone');
assert.strictEqual(formatGsdSlash('new-milestone', undefined), '/gsd-new-milestone');
});
test('runtime aliases for non-codex runtimes still emit hyphen form', () => {
assert.strictEqual(formatGsdSlash('new-project', 'claude-code'), '/gsd-new-project');
assert.strictEqual(formatGsdSlash('new-project', 'gemini-cli'), '/gsd-new-project');
assert.strictEqual(formatGsdSlash('new-project', 'opencode-cli'), '/gsd-new-project');
});
});
describe('codex shell-var form', () => {
test('emits $gsd-<cmd> for codex', () => {
assert.strictEqual(formatGsdSlash('execute-phase', 'codex'), '$gsd-execute-phase');
});
test('emits $gsd-<cmd> for codex aliases (app/cli)', () => {
assert.strictEqual(formatGsdSlash('execute-phase', 'codex-app'), '$gsd-execute-phase');
assert.strictEqual(formatGsdSlash('execute-phase', 'codex_cli'), '$gsd-execute-phase');
});
test('codex output is lowercased', () => {
assert.strictEqual(
formatGsdSlash('Execute-Phase', 'codex'),
'$gsd-execute-phase',
);
});
});
describe('input normalization', () => {
test('strips existing /gsd: colon prefix', () => {
assert.strictEqual(
formatGsdSlash('/gsd:execute-phase', 'claude'),
'/gsd-execute-phase',
);
});
test('strips existing /gsd- hyphen prefix (idempotent)', () => {
assert.strictEqual(
formatGsdSlash('/gsd-plan-phase', 'claude'),
'/gsd-plan-phase',
);
});
test('strips bare gsd: prefix without leading slash', () => {
assert.strictEqual(
formatGsdSlash('gsd:new-project', 'claude'),
'/gsd-new-project',
);
});
test('strips existing $gsd- shell prefix (codex idempotent)', () => {
assert.strictEqual(
formatGsdSlash('$gsd-execute-phase', 'codex'),
'$gsd-execute-phase',
);
});
test('runtime swap: /gsd:execute-phase + codex → $gsd-execute-phase', () => {
assert.strictEqual(
formatGsdSlash('/gsd:execute-phase', 'codex'),
'$gsd-execute-phase',
);
});
test('case-insensitive prefix stripping', () => {
assert.strictEqual(
formatGsdSlash('GSD:execute-phase', 'claude'),
'/gsd-execute-phase',
);
});
});
describe('defensive returns for unsafe inputs', () => {
test('non-string commandName returns input unchanged', () => {
assert.strictEqual(formatGsdSlash(null, 'claude'), null);
assert.strictEqual(formatGsdSlash(undefined, 'claude'), undefined);
assert.strictEqual(formatGsdSlash(42, 'claude'), 42);
});
test('empty string returns empty string', () => {
assert.strictEqual(formatGsdSlash('', 'claude'), '');
});
test('whitespace-only string returns empty string (no spurious /gsd- emission)', () => {
assert.strictEqual(formatGsdSlash(' ', 'claude'), '');
assert.strictEqual(formatGsdSlash('\t\n', 'codex'), '');
});
test('degenerate prefix-only input returns empty (does NOT re-emit colon form)', () => {
// Regression guard for the CodeRabbit finding on the original PR:
// a previous fallback returned `commandName` unchanged when the bare
// tail was empty, which re-introduced the deprecated `/gsd:` shape for
// inputs like `/gsd:`, `gsd:`, or `gsd-`. The formatter must never
// emit the colon form — return empty so callers detect "no command"
// instead of receiving an unroutable string.
assert.strictEqual(formatGsdSlash('/gsd:', 'claude'), '');
assert.strictEqual(formatGsdSlash('gsd:', 'claude'), '');
assert.strictEqual(formatGsdSlash('gsd-', 'claude'), '');
assert.strictEqual(formatGsdSlash('/gsd-', 'codex'), '');
assert.strictEqual(formatGsdSlash('$gsd-', 'codex'), '');
});
test('commands with arguments preserve the argument tail', () => {
// `execute-phase 03` is a valid call shape — the formatter only
// rewrites the command token; everything after the first whitespace
// belongs to the caller.
assert.strictEqual(
formatGsdSlash('execute-phase 03', 'claude'),
'/gsd-execute-phase 03',
);
assert.strictEqual(
formatGsdSlash('/gsd:execute-phase 03', 'claude'),
'/gsd-execute-phase 03',
);
});
test('codex form lowercases only the command token, not the argument tail', () => {
// Regression for codex review finding: a previous implementation
// lowercased the full input including arguments, which would corrupt
// Windows paths and case-sensitive flag values passed as args.
assert.strictEqual(
formatGsdSlash('Map-Codebase --paths C:\\Users\\Me\\Project', 'codex'),
'$gsd-map-codebase --paths C:\\Users\\Me\\Project',
);
assert.strictEqual(
formatGsdSlash('execute-phase 03 --Name FooBar', 'codex'),
'$gsd-execute-phase 03 --Name FooBar',
);
});
test('hyphen form preserves token case (it does not get lowercased)', () => {
// Symmetry with codex: only codex lowercases the token. Hyphen-form
// runtimes preserve whatever case the caller supplied for the token.
assert.strictEqual(
formatGsdSlash('Plan-Phase 03', 'claude'),
'/gsd-Plan-Phase 03',
);
});
});
});
describe('resolveRuntime — env > config > default', () => {
test('process.env.GSD_RUNTIME wins over everything', () => {
const saved = process.env.GSD_RUNTIME;
try {
process.env.GSD_RUNTIME = 'codex';
assert.strictEqual(resolveRuntime(null), 'codex');
assert.strictEqual(resolveRuntime('/nonexistent'), 'codex');
} finally {
if (saved === undefined) delete process.env.GSD_RUNTIME;
else process.env.GSD_RUNTIME = saved;
}
});
test('canonicalizes codex env aliases', () => {
const saved = process.env.GSD_RUNTIME;
try {
process.env.GSD_RUNTIME = 'codex-app';
assert.strictEqual(resolveRuntime(null), 'codex');
process.env.GSD_RUNTIME = 'codex_cli';
assert.strictEqual(resolveRuntime('/nonexistent'), 'codex');
} finally {
if (saved === undefined) delete process.env.GSD_RUNTIME;
else process.env.GSD_RUNTIME = saved;
}
});
test('defaults to claude when env is unset and projectDir missing', () => {
const saved = process.env.GSD_RUNTIME;
try {
delete process.env.GSD_RUNTIME;
assert.strictEqual(resolveRuntime(null), 'claude');
assert.strictEqual(resolveRuntime(undefined), 'claude');
} finally {
if (saved !== undefined) process.env.GSD_RUNTIME = saved;
}
});
test('reads config.runtime when env is unset and projectDir has a config', (t) => {
const fs = require('fs');
const os = require('os');
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3584-'));
t.after(() => cleanup(tmp));
fs.mkdirSync(path.join(tmp, '.planning'), { recursive: true });
fs.writeFileSync(
path.join(tmp, '.planning', 'config.json'),
JSON.stringify({ runtime: 'codex' }),
);
const saved = process.env.GSD_RUNTIME;
try {
delete process.env.GSD_RUNTIME;
assert.strictEqual(resolveRuntime(tmp), 'codex');
} finally {
if (saved !== undefined) process.env.GSD_RUNTIME = saved;
}
});
test('canonicalizes codex config aliases', (t) => {
const fs = require('fs');
const os = require('os');
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3584-'));
t.after(() => cleanup(tmp));
fs.mkdirSync(path.join(tmp, '.planning'), { recursive: true });
fs.writeFileSync(
path.join(tmp, '.planning', 'config.json'),
JSON.stringify({ runtime: 'codex-cli' }),
);
const saved = process.env.GSD_RUNTIME;
try {
delete process.env.GSD_RUNTIME;
assert.strictEqual(resolveRuntime(tmp), 'codex');
} finally {
if (saved !== undefined) process.env.GSD_RUNTIME = saved;
}
});
test('canonicalizes non-codex config aliases', (t) => {
const fs = require('fs');
const os = require('os');
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3584-'));
t.after(() => cleanup(tmp));
fs.mkdirSync(path.join(tmp, '.planning'), { recursive: true });
fs.writeFileSync(
path.join(tmp, '.planning', 'config.json'),
JSON.stringify({ runtime: 'claude-code' }),
);
const saved = process.env.GSD_RUNTIME;
try {
delete process.env.GSD_RUNTIME;
assert.strictEqual(resolveRuntime(tmp), 'claude');
} finally {
if (saved !== undefined) process.env.GSD_RUNTIME = saved;
}
});
test('lowercases the resolved runtime', () => {
const saved = process.env.GSD_RUNTIME;
try {
process.env.GSD_RUNTIME = 'CLAUDE';
assert.strictEqual(resolveRuntime(null), 'claude');
} finally {
if (saved === undefined) delete process.env.GSD_RUNTIME;
else process.env.GSD_RUNTIME = saved;
}
});
});