Three parallel diagnostic agents (G: sdk/install path-sep, H: hook scripts, I: worktree/workspace) characterised the remaining windows-22 failures on23b52f1a. Patches by class: CRLF in test parsers / file-content reads - bug-2136-sh-hook-version: shebang check split('\n') → split(/\r?\n/) - bug-3542-executor-git-stash-prohibition: strip \r from read content (git rewrites stashed text with CRLF on win autocrlf=true checkout) - workspace: BOM-strip + explicit \r strip in parseCommandFile (BOM at byte 0 defeats /^---/ anchor → fmMatch null → fm.name undefined) Windows path-separator / 8.3-shortname normalization - bug-3491-nested-git-worktree: use fs.realpathSync.native to expand %TEMP% RUNNER~1 → runneradmin; normalize sep before path-equality - prune-orphaned-worktrees: normalize \\→/ before substring includes (git emits forward-slash in --porcelain on Windows even when path.join produced backslashes) - bug-3017-codex-hook-absolute-node: accept POSIX path OR path with drive-letter prefix in hookPath equality assertion - bug-3126-global-skills-base-runtime-path: use path.join for expected /xdg/<runtime> values (production calls path.join → \xdg\… on win32) Test under-specified platform / forgot win32 env - bug-2979-hook-absolute-node: pass {platform:'linux'} to buildHookCommand + rewriteLegacyManagedNodeHookCommands so the POSIX-branch tests don't pick up the #3393 GitBash code path - bug-3288-model-catalog + bug-3571-config-manifest: also set USERPROFILE alongside HOME so os.homedir() on win32 redirects to the test fixture instead of the runner's real ~ External-cmd resolution - bug-2647-outer-tarball-sdk-dist: use npm.cmd + {shell:true} on win32 so execFileSync resolves PATHEXT (literal `npm` is ENOENT) ESM loader: tests/runtime-bridge-sync-smoke.test.cjs already migrated to pathToFileURL in23b52f1a(cluster E). Explicit per-test/describe skip on win32 (with required string reasons to satisfy no-unconditional-win32-skip guard) - feat-3347-graphify-auto-update-hook: 3 describes — harness spawns bash/kill/sleep + the hook itself is bash - feat-3595-fs-fault-injection: move \t and \n filenames into the POSIX-only branch (NTFS forbids 0x00–0x1F in filenames) - bug-2775/2829/3033/3231/3359: POSIX shim under ~/.local/bin with chmod 0o755 — not how Windows install works - bug-3211: single subtest where cp.execSync reassignment isn't picked up on win32 (POSIX coverage via the mock; live windows behavior covered by 3211-D which keeps running) - install-path-detection: parses sh-style export PATH= rc files; Windows has no rc files (registry Path) - worktree-safety-policy: single test using POSIX /repo/wt fixture paths that can't be expressed under win32 path.resolve Validated: plex2 (ubuntu docker) 11224/0 pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
542 lines
22 KiB
JavaScript
542 lines
22 KiB
JavaScript
'use strict';
|
|
|
|
process.env.GSD_TEST_MODE = '1';
|
|
|
|
/**
|
|
* Bug #2979: Managed JS hooks fail in GUI/minimal-PATH runtimes because
|
|
* the installer emits bare `node`.
|
|
*
|
|
* Reporter evidence: in a stripped PATH like /usr/bin:/bin:/usr/sbin:/sbin
|
|
* (the default for Finder-launched/Antigravity-spawned processes on macOS),
|
|
* `node` is not resolvable. Hook commands like
|
|
* `node "<HOME>/.gemini/hooks/gsd-check-update.js"`
|
|
* fail with `/bin/sh: node: command not found` (exit 127).
|
|
*
|
|
* Fix: emit the absolute node path (`process.execPath`, the binary
|
|
* running the installer itself) as the runner. Forward-slash-normalized
|
|
* and double-quoted so it works on POSIX and Windows.
|
|
*
|
|
* This test exercises the public buildHookCommand surface plus the
|
|
* resolveNodeRunner helper, asserting on structured records:
|
|
* - the runner field is an absolute path (not bare 'node')
|
|
* - it ends with /node or \\node (or .exe on Windows simulation)
|
|
* - .sh hooks still use bare 'bash' (PATH-resolved; portable across
|
|
* distros that don't ship /bin/bash, like NixOS)
|
|
*
|
|
* No source-grep on install.js content — assertions go against the
|
|
* value returned by the exported function and the parsed structure of
|
|
* the emitted hook command (split into runner + args).
|
|
*/
|
|
|
|
const { test, describe } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const path = require('node:path');
|
|
|
|
const INSTALL = require(path.join(__dirname, '..', 'bin', 'install.js'));
|
|
const { buildHookCommand, resolveNodeRunner } = INSTALL;
|
|
|
|
/**
|
|
* Parse a hook command string into { runner, hookPath } structured
|
|
* record. The shape is `<runner> "<hookPath>"` where <runner> may itself
|
|
* be a quoted absolute path (containing spaces), so we split on the
|
|
* trailing quoted-path token rather than the first space.
|
|
*/
|
|
function parseHookCommand(cmd) {
|
|
// Trailing token: a double-quoted string ending the command.
|
|
const m = cmd.match(/^(.+?)\s+"([^"]+)"\s*$/);
|
|
if (!m) {
|
|
return { runner: null, hookPath: null, raw: cmd };
|
|
}
|
|
return { runner: m[1], hookPath: m[2], raw: cmd };
|
|
}
|
|
|
|
describe('Bug #2979: resolveNodeRunner returns absolute, quoted, forward-slash node path', () => {
|
|
test('exported as a function', () => {
|
|
assert.equal(typeof resolveNodeRunner, 'function');
|
|
});
|
|
|
|
test('returns a double-quoted absolute path', () => {
|
|
const runner = resolveNodeRunner();
|
|
assert.ok(runner.startsWith('"'), `expected leading double-quote, got: ${runner}`);
|
|
assert.ok(runner.endsWith('"'), `expected trailing double-quote, got: ${runner}`);
|
|
const inner = runner.slice(1, -1);
|
|
assert.ok(path.isAbsolute(inner.replace(/\//g, path.sep)), `expected absolute path, got: ${inner}`);
|
|
});
|
|
|
|
test('uses forward slashes (Windows-safe, matches buildHookCommand convention)', () => {
|
|
const runner = resolveNodeRunner();
|
|
assert.ok(!runner.includes('\\'), `expected forward slashes, got: ${runner}`);
|
|
});
|
|
|
|
test('points at a node binary (basename starts with "node")', () => {
|
|
const runner = resolveNodeRunner();
|
|
const inner = runner.slice(1, -1);
|
|
const base = path.posix.basename(inner);
|
|
assert.ok(/^node(\.exe)?$/i.test(base), `expected basename node or node.exe, got: ${base}`);
|
|
});
|
|
});
|
|
|
|
describe('Bug #2979: buildHookCommand for .js hooks emits absolute node runner', () => {
|
|
test('global install: .js hook uses absolute node path, not bare "node"', () => {
|
|
const cmd = buildHookCommand('/tmp/.claude', 'gsd-check-update.js');
|
|
const parsed = parseHookCommand(cmd);
|
|
assert.notEqual(parsed.runner, null, `failed to parse: ${cmd}`);
|
|
assert.notEqual(parsed.runner, 'node', `must not emit bare node (#2979): ${cmd}`);
|
|
// The runner should be a quoted absolute path.
|
|
assert.ok(parsed.runner.startsWith('"') && parsed.runner.endsWith('"'),
|
|
`runner must be quoted absolute path, got: ${parsed.runner}`);
|
|
});
|
|
|
|
test('global install: .js hook command parses with hookPath at expected location', () => {
|
|
const cmd = buildHookCommand('/tmp/.gemini', 'gsd-statusline.js');
|
|
const parsed = parseHookCommand(cmd);
|
|
assert.equal(parsed.hookPath, '/tmp/.gemini/hooks/gsd-statusline.js');
|
|
});
|
|
|
|
test('portableHooks global install: .js hook still uses absolute node (only the path is $HOME-relative)', () => {
|
|
const home = require('node:os').homedir().replace(/\\/g, '/');
|
|
const configDir = home + '/.gemini';
|
|
const cmd = buildHookCommand(configDir, 'gsd-check-update.js', { portableHooks: true });
|
|
const parsed = parseHookCommand(cmd);
|
|
assert.notEqual(parsed.runner, 'node', `portableHooks must also use absolute node (#2979): ${cmd}`);
|
|
assert.equal(parsed.hookPath, '$HOME/.gemini/hooks/gsd-check-update.js');
|
|
});
|
|
});
|
|
|
|
describe('Bug #3362 / #3413: Windows hook commands are runtime-aware', () => {
|
|
test('Gemini global install: .js hook command starts with & so quoted runners execute in PowerShell', () => {
|
|
const cmd = buildHookCommand('C:/Program Files/Gemini/.gemini', 'gsd-check-update.js', {
|
|
platform: 'win32',
|
|
runtime: 'gemini',
|
|
});
|
|
assert.ok(cmd.startsWith('& '), `Gemini PowerShell commands need call operator, got: ${cmd}`);
|
|
assert.ok(cmd.includes('"C:/Program Files/Gemini/.gemini/hooks/gsd-check-update.js"'));
|
|
});
|
|
|
|
test('Gemini portable install: .js hook command also uses & on Windows PowerShell', () => {
|
|
const home = require('node:os').homedir().replace(/\\/g, '/');
|
|
const cmd = buildHookCommand(`${home}/.gemini`, 'gsd-check-update.js', {
|
|
portableHooks: true,
|
|
platform: 'win32',
|
|
runtime: 'gemini',
|
|
});
|
|
assert.ok(cmd.startsWith('& '), `Gemini PowerShell commands need call operator, got: ${cmd}`);
|
|
assert.equal(parseHookCommand(cmd.slice(2)).hookPath, '$HOME/.gemini/hooks/gsd-check-update.js');
|
|
});
|
|
|
|
test('Claude global install: .js hook command stays shell-neutral on Windows Git Bash', () => {
|
|
const cmd = buildHookCommand('C:/Users/me/.claude', 'gsd-check-update.js', {
|
|
platform: 'win32',
|
|
runtime: 'claude',
|
|
});
|
|
assert.ok(!cmd.startsWith('& '), `Claude hook command must not use PowerShell call operator: ${cmd}`);
|
|
assert.equal(parseHookCommand(cmd).hookPath, 'C:/Users/me/.claude/hooks/gsd-check-update.js');
|
|
});
|
|
|
|
test('Windows .js hook with no runtime stays shell-neutral', () => {
|
|
const cmd = buildHookCommand('C:/Users/me/.claude', 'gsd-check-update.js', {
|
|
platform: 'win32',
|
|
});
|
|
assert.ok(!cmd.startsWith('& '), `Missing runtime must not imply PowerShell syntax: ${cmd}`);
|
|
assert.equal(parseHookCommand(cmd).hookPath, 'C:/Users/me/.claude/hooks/gsd-check-update.js');
|
|
});
|
|
|
|
test('Gemini runtime on non-Windows platform does not get PowerShell syntax', () => {
|
|
const cmd = buildHookCommand('/home/me/.claude', 'gsd-check-update.js', {
|
|
platform: 'linux',
|
|
runtime: 'gemini',
|
|
});
|
|
assert.ok(!cmd.startsWith('& '), `Non-Windows Gemini hook must stay shell-neutral: ${cmd}`);
|
|
assert.equal(parseHookCommand(cmd).hookPath, '/home/me/.claude/hooks/gsd-check-update.js');
|
|
});
|
|
});
|
|
|
|
describe('Bug #2979: buildHookCommand for .sh hooks still uses bare "bash" (POSIX std PATH always has /bin)', () => {
|
|
test('.sh hook runner is exactly "bash" — bash is in /usr/bin:/bin and resolves under minimal PATH', () => {
|
|
const cmd = buildHookCommand('/tmp/.claude', 'gsd-session-state.sh', { platform: 'linux' });
|
|
const parsed = parseHookCommand(cmd);
|
|
assert.equal(parsed.runner, 'bash');
|
|
});
|
|
|
|
test('Windows .sh hook uses resolved Git Bash path instead of bare bash (#3393)', () => {
|
|
const cmd = buildHookCommand('C:/Users/me/.codex', 'gsd-validate-commit.sh', {
|
|
platform: 'win32',
|
|
env: { ProgramFiles: 'C:\\Program Files' },
|
|
existsSync: (candidate) => candidate === 'C:\\Program Files\\Git\\bin\\bash.exe',
|
|
});
|
|
assert.equal(
|
|
cmd,
|
|
'"C:/Program Files/Git/bin/bash.exe" "C:/Users/me/.codex/hooks/gsd-validate-commit.sh"',
|
|
);
|
|
});
|
|
|
|
test('Windows .sh hook returns null when no supported Bash runner is found (#3393)', () => {
|
|
const cmd = buildHookCommand('C:/Users/me/.codex', 'gsd-phase-boundary.sh', {
|
|
platform: 'win32',
|
|
env: {},
|
|
existsSync: () => false,
|
|
});
|
|
assert.equal(cmd, null);
|
|
});
|
|
});
|
|
|
|
// ─── #3002 CR follow-up: legacy-bare-node migration ─────────────────────────
|
|
|
|
const { rewriteLegacyManagedNodeHookCommands } = INSTALL;
|
|
|
|
describe('Bug #2979 (#3002 CR): rewriteLegacyManagedNodeHookCommands rewrites bare-node managed hooks on reinstall', () => {
|
|
test('exported as a function', () => {
|
|
assert.equal(typeof rewriteLegacyManagedNodeHookCommands, 'function');
|
|
});
|
|
|
|
test('rewrites a managed hook entry that uses bare `node ` to the absolute runner', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [
|
|
{ type: 'command', command: 'node "/Users/x/.gemini/hooks/gsd-check-update.js"' },
|
|
],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner);
|
|
assert.equal(changed, true);
|
|
assert.equal(
|
|
settings.hooks.SessionStart[0].hooks[0].command,
|
|
'"/usr/local/bin/node" "/Users/x/.gemini/hooks/gsd-check-update.js"',
|
|
);
|
|
});
|
|
|
|
test('does NOT touch entries that already use a quoted absolute runner', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: '"/usr/local/bin/node" "/x/hooks/gsd-statusline.js"' }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const before = settings.hooks.SessionStart[0].hooks[0].command;
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner);
|
|
assert.equal(changed, false);
|
|
assert.equal(settings.hooks.SessionStart[0].hooks[0].command, before);
|
|
});
|
|
|
|
test('Gemini on Windows adds PowerShell call operator to existing quoted managed hooks', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: '"/usr/local/bin/node" "C:/Program Files/Gemini/.gemini/hooks/gsd-check-update.js"' }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner, { platform: 'win32', runtime: 'gemini' });
|
|
assert.equal(changed, true);
|
|
assert.equal(
|
|
settings.hooks.SessionStart[0].hooks[0].command,
|
|
'& "/usr/local/bin/node" "C:/Program Files/Gemini/.gemini/hooks/gsd-check-update.js"',
|
|
);
|
|
});
|
|
|
|
test('Gemini on Windows does NOT double-prefix managed hooks that already use the PowerShell call operator', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: '& "/usr/local/bin/node" "C:/Program Files/Gemini/.gemini/hooks/gsd-check-update.js"' }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const before = settings.hooks.SessionStart[0].hooks[0].command;
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner, { platform: 'win32', runtime: 'gemini' });
|
|
assert.equal(changed, false);
|
|
assert.equal(settings.hooks.SessionStart[0].hooks[0].command, before);
|
|
});
|
|
|
|
test('Gemini on Windows rewrites PowerShell bare-node managed hooks to absolute runner without dropping &', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: '& node "C:/Users/me/.gemini/hooks/gsd-check-update.js"' }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner, { platform: 'win32', runtime: 'gemini' });
|
|
assert.equal(changed, true);
|
|
assert.equal(
|
|
settings.hooks.SessionStart[0].hooks[0].command,
|
|
'& "/usr/local/bin/node" "C:/Users/me/.gemini/hooks/gsd-check-update.js"',
|
|
);
|
|
});
|
|
|
|
test('Claude on Windows strips stale PowerShell prefix from managed hooks on reinstall (#3413)', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: '& "/usr/local/bin/node" "C:/Users/me/.claude/hooks/gsd-check-update.js"' }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner, { platform: 'win32', runtime: 'claude' });
|
|
assert.equal(changed, true);
|
|
assert.equal(
|
|
settings.hooks.SessionStart[0].hooks[0].command,
|
|
'"/usr/local/bin/node" "C:/Users/me/.claude/hooks/gsd-check-update.js"',
|
|
);
|
|
});
|
|
|
|
test('does NOT touch user-authored bare-node hooks (filename not in managed allowlist)', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: 'node /home/me/my-custom-hook.js' }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const before = settings.hooks.SessionStart[0].hooks[0].command;
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner);
|
|
assert.equal(changed, false);
|
|
assert.equal(settings.hooks.SessionStart[0].hooks[0].command, before);
|
|
});
|
|
|
|
test('does NOT touch .sh hooks (they correctly use bare bash)', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: 'bash "/x/hooks/gsd-session-state.sh"' }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner);
|
|
assert.equal(changed, false);
|
|
});
|
|
|
|
test('is a no-op when absoluteRunner is null (resolveNodeRunner failed)', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: 'node "/x/hooks/gsd-check-update.js"' }],
|
|
}],
|
|
},
|
|
};
|
|
const before = settings.hooks.SessionStart[0].hooks[0].command;
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, null);
|
|
assert.equal(changed, false);
|
|
assert.equal(settings.hooks.SessionStart[0].hooks[0].command, before);
|
|
});
|
|
|
|
// #3002 CR: substring containment was a false-positive vector.
|
|
// User-authored hooks whose path happened to CONTAIN a managed filename
|
|
// as a substring would get unconditionally rewritten with the GSD runner.
|
|
// The fix matches by basename equality.
|
|
test('does NOT rewrite a user hook whose path contains a managed filename as a substring', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{
|
|
type: 'command',
|
|
// Path contains gsd-check-update.js as substring of a longer
|
|
// filename, but is NOT actually that file.
|
|
command: 'node /home/me/scripts/wraps-gsd-check-update.js-helper.js',
|
|
}],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const before = settings.hooks.SessionStart[0].hooks[0].command;
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner);
|
|
assert.equal(changed, false, 'must not rewrite user hooks with managed-filename-as-substring paths');
|
|
assert.equal(settings.hooks.SessionStart[0].hooks[0].command, before);
|
|
});
|
|
|
|
test('rewrites a managed entry whose path is quoted with single quotes', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: "node '/x/hooks/gsd-statusline.js'" }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner, { platform: 'linux' });
|
|
assert.equal(changed, true);
|
|
assert.equal(
|
|
settings.hooks.SessionStart[0].hooks[0].command,
|
|
`"/usr/local/bin/node" '/x/hooks/gsd-statusline.js'`,
|
|
);
|
|
});
|
|
|
|
test('rewrites a managed entry with no path quoting (bareword)', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: 'node /x/hooks/gsd-context-monitor.js' }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner, { platform: 'linux' });
|
|
assert.equal(changed, true);
|
|
assert.equal(
|
|
settings.hooks.SessionStart[0].hooks[0].command,
|
|
'"/usr/local/bin/node" /x/hooks/gsd-context-monitor.js',
|
|
);
|
|
});
|
|
|
|
test('handles Windows-style backslash path separators when extracting basename', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [{ type: 'command', command: 'node "C:\\\\Users\\\\me\\\\.claude\\\\hooks\\\\gsd-prompt-guard.js"' }],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"/usr/local/bin/node"';
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner);
|
|
assert.equal(changed, true);
|
|
});
|
|
|
|
test('Gemini on Windows normalizes single-quoted managed hook paths to double-quoted forward-slash paths (#3392)', () => {
|
|
const settings = {
|
|
hooks: {
|
|
PreToolUse: [{
|
|
hooks: [{
|
|
type: 'command',
|
|
command: "node 'C:\\Users\\me\\.gemini\\hooks\\gsd-prompt-guard.js'",
|
|
}],
|
|
}],
|
|
},
|
|
};
|
|
const runner = '"C:/nvm4w/nodejs/node.exe"';
|
|
const changed = rewriteLegacyManagedNodeHookCommands(settings, runner, { platform: 'win32', runtime: 'gemini' });
|
|
assert.equal(changed, true);
|
|
assert.equal(
|
|
settings.hooks.PreToolUse[0].hooks[0].command,
|
|
'& "C:/nvm4w/nodejs/node.exe" "C:/Users/me/.gemini/hooks/gsd-prompt-guard.js"',
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('Bug #2979 (#3002 CR): resolveNodeRunner returns null when execPath unavailable', () => {
|
|
test('returns null instead of bare "node" when process.execPath is empty', () => {
|
|
const orig = process.execPath;
|
|
try {
|
|
Object.defineProperty(process, 'execPath', { value: '', configurable: true });
|
|
const r = resolveNodeRunner();
|
|
assert.equal(r, null, 'expected null, not bare "node"');
|
|
} finally {
|
|
Object.defineProperty(process, 'execPath', { value: orig, configurable: true });
|
|
}
|
|
});
|
|
|
|
test('buildHookCommand returns null when execPath is unavailable (caller skips registration)', () => {
|
|
const orig = process.execPath;
|
|
try {
|
|
Object.defineProperty(process, 'execPath', { value: '', configurable: true });
|
|
const cmd = buildHookCommand('/tmp/.claude', 'gsd-statusline.js');
|
|
assert.equal(cmd, null);
|
|
} finally {
|
|
Object.defineProperty(process, 'execPath', { value: orig, configurable: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
// ─── #3002 CR follow-up #2: null-command guards in settings.json ──────────
|
|
|
|
const { validateHookFields } = INSTALL;
|
|
|
|
describe('Bug #2979 (#3002 CR follow-up): no command:null hook entries survive serialization', () => {
|
|
// CR feedback: assert structurally on the resulting settings object, not by
|
|
// grepping bin/install.js source. The push-site guards (each `if` clause's
|
|
// `&& <command>` token) skip null-command pushes at the source. As a
|
|
// backstop, install.js now runs validateHookFields(settings) right before
|
|
// writeSettings; this test exercises that backstop directly.
|
|
//
|
|
// Construct a settings object that contains exactly the kind of null-command
|
|
// entries that the registration code would have written if my push-site
|
|
// guards regressed. Run validateHookFields on it. Assert the null entries
|
|
// are gone and the well-formed entries survive.
|
|
|
|
function nullCommandEntry(matcher) {
|
|
const entry = { hooks: [{ type: 'command', command: null }] };
|
|
if (matcher) entry.matcher = matcher;
|
|
return entry;
|
|
}
|
|
function realCommandEntry(matcher, command) {
|
|
const entry = { hooks: [{ type: 'command', command }] };
|
|
if (matcher) entry.matcher = matcher;
|
|
return entry;
|
|
}
|
|
|
|
const MANAGED_JS_HOOKS = [
|
|
{ event: 'SessionStart', matcher: undefined, label: 'gsd-check-update.js' },
|
|
{ event: 'PostToolUse', matcher: 'Bash|Edit|Write|MultiEdit|Agent|Task', label: 'gsd-context-monitor.js' },
|
|
{ event: 'PreToolUse', matcher: 'Write|Edit', label: 'gsd-prompt-guard.js' },
|
|
{ event: 'PreToolUse', matcher: 'Write|Edit', label: 'gsd-read-guard.js' },
|
|
{ event: 'PostToolUse', matcher: 'Read', label: 'gsd-read-injection-scanner.js' },
|
|
{ event: 'PreToolUse', matcher: 'Bash|Edit|Write|MultiEdit', label: 'gsd-workflow-guard.js' },
|
|
];
|
|
|
|
for (const { event, matcher, label } of MANAGED_JS_HOOKS) {
|
|
test(`validateHookFields strips a null-command ${label} entry from settings.hooks.${event}`, () => {
|
|
const settings = {
|
|
hooks: {
|
|
[event]: [
|
|
nullCommandEntry(matcher),
|
|
realCommandEntry(matcher, '"/usr/local/bin/node" "/x/hooks/other.js"'),
|
|
],
|
|
},
|
|
};
|
|
const out = validateHookFields(settings);
|
|
const survivors = out.hooks[event] || [];
|
|
// The well-formed entry must remain.
|
|
assert.equal(survivors.length, 1, `expected the real-command entry to survive`);
|
|
// No survivor entry contains a hook with command === null.
|
|
for (const e of survivors) {
|
|
for (const h of e.hooks || []) {
|
|
assert.notEqual(h.command, null, 'no surviving hook should have command:null');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
test('validateHookFields drops the entry entirely when all its hooks have null commands', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [nullCommandEntry()],
|
|
},
|
|
};
|
|
const out = validateHookFields(settings);
|
|
// Empty event arrays should be cleaned up (the entire SessionStart key
|
|
// gets removed when nothing valid remains).
|
|
assert.ok(
|
|
!out.hooks.SessionStart || out.hooks.SessionStart.length === 0,
|
|
'expected SessionStart to be empty/removed after the only entry was dropped',
|
|
);
|
|
});
|
|
|
|
test('validateHookFields preserves agent-type hooks while stripping command:null sibling hooks', () => {
|
|
const settings = {
|
|
hooks: {
|
|
SessionStart: [{
|
|
hooks: [
|
|
{ type: 'command', command: null },
|
|
{ type: 'agent', prompt: 'analyze the session' },
|
|
{ type: 'command', command: '"/usr/local/bin/node" "/x/hooks/y.js"' },
|
|
],
|
|
}],
|
|
},
|
|
};
|
|
const out = validateHookFields(settings);
|
|
const survivors = out.hooks.SessionStart[0].hooks;
|
|
assert.equal(survivors.length, 2, 'expected 2 of 3 hooks to survive (the null-command one is stripped)');
|
|
assert.equal(survivors.find(h => h.command === null), undefined, 'no surviving hook should have command:null');
|
|
});
|
|
});
|