Files
msd-core/tests/bug-2831-opencode-home-path-prefix.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

156 lines
5.6 KiB
JavaScript

// allow-test-rule: source-text-is-the-product
// Workflow .md / agent .md / command .md / reference .md files — their text
// IS what the runtime loads. Testing text content tests the deployed contract.
// Per CONTRIBUTING.md exception matrix.
/**
* Regression test for #2831: OpenCode @file references contain literal `$HOME`
* which OpenCode does not expand — `@$HOME/.config/opencode/...` is resolved
* as a path relative to the config command/ dir, producing
* `command/$HOME/.config/opencode/...` (file not found).
*
* Root cause: install.js pathPrefix used `$HOME`-relative paths for OpenCode on
* non-Windows hosts (only Windows was guarded by #2376). OpenCode's `@file`
* include syntax does NOT shell-expand `$HOME` on any platform.
*
* Fix: pathPrefix must use the absolute path for OpenCode on all platforms.
*
* Tests exercise install.js's exported `computePathPrefix` directly (no source
* grepping) and additionally simulate the `copyFlattenedCommands` substitution
* pipeline on a temp tree to verify no `$HOME` literal leaks into emitted files.
*/
'use strict';
const { describe, test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { cleanup } = require('./helpers.cjs');
let computePathPrefix;
before(() => {
process.env.GSD_TEST_MODE = '1';
delete require.cache[require.resolve('../bin/install.js')];
({ computePathPrefix } = require('../bin/install.js'));
});
after(() => {
delete process.env.GSD_TEST_MODE;
});
describe('bug-2831: OpenCode pathPrefix uses absolute path on all platforms', () => {
test('computePathPrefix is exported by install.js', () => {
assert.equal(typeof computePathPrefix, 'function');
});
test('OpenCode on macOS: pathPrefix is absolute (no $HOME)', () => {
const pathPrefix = computePathPrefix({
isGlobal: true,
isOpencode: true,
isWindowsHost: false,
homeDir: '/Users/alice',
resolvedTarget: '/Users/alice/.config/opencode',
});
assert.strictEqual(pathPrefix, '/Users/alice/.config/opencode/');
assert.ok(!pathPrefix.includes('$HOME'));
});
test('OpenCode on Linux: pathPrefix is absolute (no $HOME)', () => {
const pathPrefix = computePathPrefix({
isGlobal: true,
isOpencode: true,
isWindowsHost: false,
homeDir: '/home/bob',
resolvedTarget: '/home/bob/.config/opencode',
});
assert.strictEqual(pathPrefix, '/home/bob/.config/opencode/');
assert.ok(!pathPrefix.includes('$HOME'));
});
test('OpenCode on Windows: pathPrefix is absolute (preserves #2376)', () => {
const pathPrefix = computePathPrefix({
isGlobal: true,
isOpencode: true,
isWindowsHost: true,
homeDir: 'C:/Users/carol',
resolvedTarget: 'C:/Users/carol/.config/opencode',
});
assert.strictEqual(pathPrefix, 'C:/Users/carol/.config/opencode/');
});
test('Claude Code on macOS: pathPrefix still uses $HOME (unaffected)', () => {
const pathPrefix = computePathPrefix({
isGlobal: true,
isOpencode: false,
isWindowsHost: false,
homeDir: '/Users/alice',
resolvedTarget: '/Users/alice/.claude',
});
assert.strictEqual(pathPrefix, '$HOME/.claude/');
});
test('Local install (non-global): pathPrefix uses absolute path regardless of runtime', () => {
const pathPrefix = computePathPrefix({
isGlobal: false,
isOpencode: false,
isWindowsHost: false,
homeDir: '/Users/alice',
resolvedTarget: '/Users/alice/projects/foo/.claude',
});
assert.strictEqual(pathPrefix, '/Users/alice/projects/foo/.claude/');
});
test('Substitution pipeline simulation: OpenCode emits no @$HOME literal', () => {
// This validates the same regex substitution pipeline used by
// copyFlattenedCommands when writing OpenCode command files. We invoke the
// real exported computePathPrefix; the regex passes mirror the install.js
// call sites (globalClaudeRegex / globalClaudeHomeRegex).
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-2831-'));
try {
const srcRoot = path.join(tmp, 'src');
const targetRoot = path.join(tmp, 'home', '.config', 'opencode');
const srcCmdDir = path.join(srcRoot, 'commands', 'gsd');
fs.mkdirSync(srcCmdDir, { recursive: true });
fs.mkdirSync(targetRoot, { recursive: true });
const srcFile = path.join(srcCmdDir, 'autonomous.md');
fs.writeFileSync(
srcFile,
'---\nname: autonomous\n---\n<execution_context>\n@~/.claude/get-shit-done/workflows/autonomous.md\n@$HOME/.claude/get-shit-done/references/ui-brand.md\n</execution_context>\n'
);
const homeDir = path.join(tmp, 'home').replace(/\\/g, '/');
const resolvedTarget = targetRoot.replace(/\\/g, '/');
const pathPrefix = computePathPrefix({
isGlobal: true,
isOpencode: true,
isWindowsHost: false,
homeDir,
resolvedTarget,
});
let content = fs.readFileSync(srcFile, 'utf8');
content = content.replace(/~\/\.claude\//g, pathPrefix);
content = content.replace(/\$HOME\/\.claude\//g, pathPrefix);
assert.ok(
!/@\$HOME\b/.test(content),
`output must not contain @$HOME literal; got:\n${content}`
);
assert.ok(
!/\$HOME\b/.test(content),
`output must not contain $HOME literal; got:\n${content}`
);
assert.ok(
content.includes(`@${resolvedTarget}/`),
`output should include absolute path with @ prefix; got:\n${content}`
);
} finally {
cleanup(tmp);
}
});
});