Files
msd-core/tests/bug-2831-opencode-home-path-prefix.test.cjs
Tom Boucher b8c33647d8 refactor(tests): retire output-grep & source-grep via typed surfaces (finish #2974) (#462)
* refactor(#455): implement typed surfaces to retire grep tests

Production surfaces added:
- hooks/managed-hooks-registry.cjs: new CJS module exporting MANAGED_HOOKS
  as a typed array; gsd-check-update-worker.js now requires it instead of
  declaring an inline array
- bin/install.js: elevate inline gsdHooks to module-level GSD_UNINSTALL_HOOKS,
  export it alongside runtimeMap/allRuntimes (already exported)
- scripts/build-hooks.js: export HOOKS_TO_COPY; guard build() behind
  require.main===module so tests can require the file without triggering a build
- get-shit-done/bin/lib/init.cjs: add --json mode to agent-skills command,
  emitting typed IR { agent_type, block, skills_count } for test assertions
- get-shit-done/bin/gsd-tools.cjs: wire --json flag for agent-skills dispatch

Category-B source-grep migrations:
- tests/managed-hooks.test.cjs: require MANAGED_HOOKS from registry, drop fs.readFileSync+regex
- tests/orphaned-hooks.test.cjs: require MANAGED_HOOKS+HOOKS_TO_COPY as typed exports
- tests/hooks-opt-in.test.cjs: replace gsdHooks regex-parse with GSD_UNINSTALL_HOOKS import
- tests/install-minimal-hooks.test.cjs: replace gsdHooks regex-parse with GSD_UNINSTALL_HOOKS
- tests/copilot-install.test.cjs: replace src.includes() checks with typed
  assertions on runtimeMap, allRuntimes, parseRuntimeInput, buildRuntimePromptText
- tests/agent-skills.test.cjs: migrate to --json typed IR assertions

pending-migration-to-typed-ir token cleared (87 of 87 files):
- 78 files already had source-text-is-the-product; removed duplicate token
- 5 files already used typed assertions; reclassified or annotated
- 4 files required individual reclassification to source-text-is-the-product
  or architectural-invariant

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(#455): update workflow-guard test to typed GSD_UNINSTALL_HOOKS import; isolate HOME in runtime-launcher (D) test

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(#455): guard install.js main() behind require.main===module so the typed export is require-safe

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs(#455): document --json typed surfaces for agent-skills, progress, validate context

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs(#455): add changeset fragment for new --json surfaces

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(#455): complete grep migration for files flagged by lint-tests

The branch commit 4e630d99 stripped `allow-test-rule: pending-migration-to-typed-ir`
from ~80 test files without replacing their assertions or adding the correct
exemption annotation. The files were NOT source-grep tests — they read .md
workflow/agent/command/reference files (source-text-is-the-product) or hook
source files for structural invariants (structural-regression-guard). No
assertion logic was changed; only the correct allow-test-rule annotation was
added to each file per CONTRIBUTING.md exception matrix.

73 files: `source-text-is-the-product` — workflow/agent/command/reference .md
7 files:  `structural-regression-guard` — hook .js / bin/install.js structural checks

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: CI Rebase Check <ci@gsd-redux>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-29 11:39:52 -04:00

155 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');
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 {
fs.rmSync(tmp, { recursive: true, force: true });
}
});
});