Phase 2 of #3464, following #3465. Rewrites every assertion that read a shipped .cjs/.js file and text-searched it, so the file no longer needs an allow-test-rule exemption. 6 of the 8 files are now marker-free; the ceiling drops 285 -> 278 against a measured 277. A source-grep passes when a STRING is present, not when the code WORKS. It survives a refactor that keeps the string but breaks the behavior, and breaks on a refactor that keeps the behavior but renames the string. Both failure modes are silent about the thing the test claims to protect. That is the anti-pattern ADR-456 and local/no-source-grep exist to prevent. Rewrites, each against the real exported seam: - discuss-mode: calls cmdInitPlanPhase() against a fixture whose config sets workflow.text_mode, asserts the value propagates to its emitted JSON. - effort-surface-axis: runs review-lane invoke against a real project with a fake `claude` PATH shim, asserts the resolved --effort actually lands in the shim's captured argv. - install-minimal-hooks: calls applySettingsJsonHooks() with a hook source missing, asserts it is neither registered nor silently registers wholesale, with sibling present hooks as the positive control. - install: calls the exported inferPreferredRuntime({fs, env, preferredConfigDir}) via its injected fs seam, asserting 'kilo' from both the config-marker and env-var paths. - opencode-permissions: spawns the real installer with a custom config dir, asserts the written opencode.json permission paths are anchored on it. - repo-layout: spawns the installer for copilot local vs global, asserting AGENTS.md is written only in the local case. - runtime-config-adapter-registry: stubs resolveInstallPlan and force-reloads install.js, asserting the runtime's artifact stops being written -- proving install.js genuinely routes through the registry. - runtime-homes-descriptor-drive: calls buildAgentSkillsBlock() for cursor and claude against real fixture SKILL.md files, asserting each runtime's refs land under its own config dir and never the other's. Every rewrite was mutation-checked before its marker was dropped. Because node --test is not runnable locally in this repo, each assertion was replicated in a standalone probe that requires the same module: run green against the real file, then red against a deliberately broken one (text_mode propagation deleted, existsSync guard removed, config dir hardcoded, !isGlobal guard dropped, kilo branch removed, effort resolution bypassed, skills base hardcoded back to .claude), then the production file restored and confirmed byte-identical. An assertion that could not be made to fail would not have shipped -- a behavioral test that passes regardless of correctness is strictly worse than the source-grep it replaces, because it looks rigorous while asserting nothing. Three claims were checked rather than trusted. All three were wrong: - repo-layout's own marker cited #1188 asserting the `!isGlobal` lexical scope was "unprovable at runtime". It is provable: the guard decides whether AGENTS.md is written, which is directly observable. Both directions verified. - The triage for runtime-config-adapter-registry claimed its source-grep was redundant with the file's EXPECTED_TABLE tests, so deletion would be safe. Those tests only exercise resolveInstallPlan() directly and never load bin/install.js, so they do not cover it. A real behavioral assertion was written instead of deleting coverage. - An earlier revision of this change dropped runtime-config-adapter-registry's two markers on the grounds that ESLint stayed silent without them. An adversarial review caught that this was wrong, and it is restored here. The file still genuinely source-greps bin/install.js at two sites; ESLint is silent only because no-source-grep's TEXT_METHODS omits matchAll. Dropping a marker because the linter cannot see the violation is exploiting the blind spot, not resolving it -- and it would go red the moment the rule is widened. Those two assertions are also irreducible: they assert that EVERY inline `runtime === '...'` branch in install.js names a registry-known runtime, and a branch naming an unregistered runtime would simply never execute, so no runtime observation can prove its absence. The markers now say so explicitly. Two coverage gaps in no-source-grep surfaced while doing this, recorded on #3464 rather than fixed here, since widening the rule is its own change with its own blast radius: - TEXT_METHODS omits matchAll, so a matchAll source-grep never trips the rule (the case above). - The path test requires a literal quoted bin/lib/gsd-core/src segment and tracks the binding one hop, so a read through a dynamic path or an intermediate variable evades it. install-minimal-hooks' genuinely load-bearing read at line 975 is itself unmarked for a related reason. install-minimal-hooks therefore keeps its markers too: its remaining real source-grep of bin/install.js (the Codex legacy gsd-update-check migration check, line 975) is outside this issue's 8 sites. It is the last blocker for that file and is a clean follow-up. Closes #3466 Co-authored-by: sim <sim@local> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
148 lines
6.1 KiB
JavaScript
148 lines
6.1 KiB
JavaScript
/**
|
|
* Regression tests for OpenCode permission config handling.
|
|
*
|
|
* Ensures the installer does not crash when opencode.json uses the valid
|
|
* top-level string form: "permission": "allow", and that path-specific
|
|
* permissions are written against the actual resolved install directory.
|
|
*/
|
|
|
|
process.env.GSD_TEST_MODE = '1';
|
|
|
|
const { test, describe, beforeEach, afterEach } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const { createTempDir, cleanup } = require('./helpers.cjs');
|
|
const { configureOpencodePermissions } = require('../bin/install.js');
|
|
const { PACKAGE_NAME } = require('../gsd-core/bin/lib/package-identity.cjs');
|
|
const { runNode } = require('./helpers/process-seam.cjs');
|
|
const { INSTALL_SCRIPT, installerEnv } = require('./helpers/install-shared.cjs');
|
|
const { INSTALL_TIMEOUT_MS } = require('./helpers/timeouts.cjs');
|
|
|
|
const envKeys = ['OPENCODE_CONFIG_DIR', 'OPENCODE_CONFIG', 'XDG_CONFIG_HOME'];
|
|
const originalEnv = Object.fromEntries(envKeys.map((key) => [key, process.env[key]]));
|
|
|
|
function restoreEnv(snapshot) {
|
|
for (const key of envKeys) {
|
|
if (snapshot[key] === undefined) {
|
|
delete process.env[key];
|
|
} else {
|
|
process.env[key] = snapshot[key];
|
|
}
|
|
}
|
|
}
|
|
|
|
describe('configureOpencodePermissions', () => {
|
|
let configDir;
|
|
|
|
beforeEach(() => {
|
|
configDir = createTempDir('gsd-opencode-');
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(configDir);
|
|
restoreEnv(originalEnv);
|
|
});
|
|
|
|
test('does not crash or rewrite top-level string permissions', () => {
|
|
const configPath = path.join(configDir, 'opencode.json');
|
|
const original = JSON.stringify({
|
|
$schema: 'https://opencode.ai/config.json',
|
|
permission: 'allow',
|
|
skills: { paths: ['/tmp/skills'] },
|
|
}, null, 2) + '\n';
|
|
|
|
fs.writeFileSync(configPath, original);
|
|
process.env.OPENCODE_CONFIG_DIR = configDir;
|
|
|
|
assert.doesNotThrow(() => configureOpencodePermissions(true, configDir));
|
|
assert.strictEqual(fs.readFileSync(configPath, 'utf8'), original);
|
|
});
|
|
|
|
test('adds path-specific read and external_directory permissions for object configs', () => {
|
|
const configPath = path.join(configDir, 'opencode.json');
|
|
fs.writeFileSync(configPath, JSON.stringify({ permission: {} }, null, 2) + '\n');
|
|
process.env.OPENCODE_CONFIG_DIR = configDir;
|
|
|
|
configureOpencodePermissions(true, configDir);
|
|
|
|
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
const gsdPath = `${configDir.replace(/\\/g, '/')}/gsd-core/*`;
|
|
|
|
assert.strictEqual(config.permission.read[gsdPath], 'allow');
|
|
assert.strictEqual(config.permission.external_directory[gsdPath], 'allow');
|
|
});
|
|
|
|
test('registers the companion MCP server (mcp.gsd) for object configs (#1682)', () => {
|
|
const configPath = path.join(configDir, 'opencode.json');
|
|
fs.writeFileSync(configPath, JSON.stringify({ permission: {} }, null, 2) + '\n');
|
|
process.env.OPENCODE_CONFIG_DIR = configDir;
|
|
|
|
configureOpencodePermissions(true, configDir);
|
|
|
|
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
assert.deepEqual(config.mcp.gsd, {
|
|
type: 'local',
|
|
command: ['npx', '-y', '-p', PACKAGE_NAME, 'gsd-mcp-server'],
|
|
enabled: true,
|
|
});
|
|
});
|
|
|
|
test('does not clobber a user-defined mcp.gsd entry (#1682)', () => {
|
|
const configPath = path.join(configDir, 'opencode.json');
|
|
const userMcp = { type: 'local', command: ['node', '/custom/server.js'], enabled: false };
|
|
fs.writeFileSync(configPath, JSON.stringify({ permission: {}, mcp: { gsd: userMcp } }, null, 2) + '\n');
|
|
process.env.OPENCODE_CONFIG_DIR = configDir;
|
|
|
|
configureOpencodePermissions(true, configDir);
|
|
|
|
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
// User's own mcp.gsd is preserved untouched (Hyrum's Law — non-clobbering).
|
|
assert.deepEqual(config.mcp.gsd, userMcp);
|
|
});
|
|
|
|
test('finishInstall passes the REAL resolved config dir to OpenCode permissions (not a hardcoded path)', () => {
|
|
// Behavioral replacement for a source-grep assertion (#3466): spawns the
|
|
// real installer CLI end-to-end (`bin/install.js --opencode --global
|
|
// --config-dir <configDir>`) — the only code path that actually reaches
|
|
// finishInstall's `configureOpencodePermissions(isGlobal, configDir);`
|
|
// call site (the exported `install()` function alone does NOT call
|
|
// finishInstall — only the CLI/installAllRuntimes flow does, and that
|
|
// flow is also where the GSD_TEST_MODE gate on this writer is normally
|
|
// active, so a real spawn — whose env deliberately excludes
|
|
// GSD_TEST_MODE via installerEnv() — is required to exercise it) —
|
|
// then asserts the resulting opencode.json's permission paths are
|
|
// anchored on THAT exact config dir. A hardcoded or stale configDir at
|
|
// the finishInstall call site would write permission paths anchored on
|
|
// the wrong directory (or fail to find/produce opencode.json at all),
|
|
// so this can only pass if the real, per-call configDir reaches
|
|
// configureOpencodePermissions.
|
|
const result = runNode(
|
|
[INSTALL_SCRIPT, '--opencode', '--global', '--config-dir', configDir],
|
|
{ env: installerEnv(), timeoutMs: INSTALL_TIMEOUT_MS },
|
|
);
|
|
assert.strictEqual(
|
|
result.exitCode, 0,
|
|
`installer must exit 0 for --opencode --global\nstdout: ${result.stdout}\nstderr: ${result.stderr}`
|
|
);
|
|
|
|
const configPath = path.join(configDir, 'opencode.json');
|
|
assert.ok(
|
|
fs.existsSync(configPath),
|
|
'finishInstall must write opencode.json into the resolved config dir'
|
|
);
|
|
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
const gsdPath = `${configDir.replace(/\\/g, '/')}/gsd-core/*`;
|
|
assert.strictEqual(
|
|
config.permission?.read?.[gsdPath], 'allow',
|
|
'finishInstall must call configureOpencodePermissions with the REAL resolved configDir, ' +
|
|
`not a hardcoded path — expected a read permission entry for ${gsdPath}`
|
|
);
|
|
assert.strictEqual(
|
|
config.permission?.external_directory?.[gsdPath], 'allow',
|
|
`expected an external_directory permission entry for ${gsdPath}`
|
|
);
|
|
});
|
|
});
|