fix: resolve stop hooks without plugin root env

This commit is contained in:
Affaan Mustafa
2026-03-29 21:26:12 -04:00
parent dd675d4258
commit 08a5ae6784
3 changed files with 72 additions and 10 deletions

View File

@@ -1965,7 +1965,7 @@ async function runTests() {
passed++;
else failed++;
if (
test('script references use CLAUDE_PLUGIN_ROOT variable or safe SessionStart inline resolver', () => {
test('script references use CLAUDE_PLUGIN_ROOT variable or a safe inline resolver', () => {
const hooksPath = path.join(__dirname, '..', '..', 'hooks', 'hooks.json');
const hooks = JSON.parse(fs.readFileSync(hooksPath, 'utf8'));
@@ -1973,9 +1973,8 @@ async function runTests() {
for (const entry of hookArray) {
for (const hook of entry.hooks) {
if (hook.type === 'command' && hook.command.includes('scripts/hooks/')) {
// Check for the literal string "${CLAUDE_PLUGIN_ROOT}" in the command
const isSessionStartInlineResolver = hook.command.startsWith('node -e') && hook.command.includes('session:start') && hook.command.includes('run-with-flags.js');
const hasPluginRoot = hook.command.includes('${CLAUDE_PLUGIN_ROOT}') || isSessionStartInlineResolver;
const isInlineResolver = hook.command.startsWith('node -e') && hook.command.includes('run-with-flags.js');
const hasPluginRoot = hook.command.includes('${CLAUDE_PLUGIN_ROOT}') || isInlineResolver;
assert.ok(hasPluginRoot, `Script paths should use CLAUDE_PLUGIN_ROOT: ${hook.command.substring(0, 80)}...`);
}
}
@@ -1990,6 +1989,24 @@ async function runTests() {
passed++;
else failed++;
if (
test('Stop and SessionEnd lifecycle hooks use inline resolvers when plugin root may be unset', () => {
const hooksPath = path.join(__dirname, '..', '..', 'hooks', 'hooks.json');
const hooks = JSON.parse(fs.readFileSync(hooksPath, 'utf8'));
const lifecycleHooks = [...(hooks.hooks.Stop || []), ...(hooks.hooks.SessionEnd || [])];
for (const entry of lifecycleHooks) {
for (const hook of entry.hooks || []) {
assert.ok(hook.command.startsWith('node -e "'), `Expected inline resolver for lifecycle hook: ${hook.command.substring(0, 80)}...`);
assert.ok(hook.command.includes("const hookId='"), 'Inline lifecycle resolver should embed hook identifiers');
assert.ok(hook.command.includes('plugins'), 'Inline lifecycle resolver should probe known plugin roots');
}
}
})
)
passed++;
else failed++;
if (
test('InsAIts hook is opt-in and scoped to high-signal tool inputs', () => {
const hooksPath = path.join(__dirname, '..', '..', 'hooks', 'hooks.json');

View File

@@ -179,6 +179,22 @@ function cleanupTestDir(testDir) {
fs.rmSync(testDir, { recursive: true, force: true });
}
function linkPluginCacheInstall(homeDir) {
const installRoot = path.join(
homeDir,
'.claude',
'plugins',
'cache',
'everything-claude-code',
'affaan-m',
'1.9.0'
);
fs.mkdirSync(path.dirname(installRoot), { recursive: true });
fs.symlinkSync(REPO_ROOT, installRoot, process.platform === 'win32' ? 'junction' : 'dir');
return installRoot;
}
function getHookCommandByDescription(hooks, lifecycle, descriptionText) {
const hookGroup = hooks.hooks[lifecycle]?.find(
entry => entry.description && entry.description.includes(descriptionText)
@@ -267,6 +283,35 @@ async function runTests() {
assert.strictEqual(payload.hookSpecificOutput.hookEventName, 'SessionStart');
})) passed++; else failed++;
if (await asyncTest('Stop hooks resolve plugin root without CLAUDE_PLUGIN_ROOT', async () => {
const hookCommand = getHookCommandByDescription(
hooks,
'Stop',
'Check for console.log in modified files'
);
const testHome = createTestDir();
try {
linkPluginCacheInstall(testHome);
const result = await runHookCommand(
hookCommand,
{ tool_input: { file_path: 'src/example.js' } },
{
CLAUDE_PLUGIN_ROOT: '',
HOME: testHome,
USERPROFILE: testHome
}
);
assert.strictEqual(result.code, 0, 'Stop hook should exit 0 when plugin root is resolved from plugin cache');
assert.ok(!result.stderr.includes('/scripts/hooks/run-with-flags.js'), 'Should not fall back to an invalid absolute root path');
assert.ok(!result.stderr.includes('MODULE_NOT_FOUND'), 'Should not fail to locate run-with-flags.js');
} finally {
cleanupTestDir(testHome);
}
})) passed++; else failed++;
if (await asyncTest('PreCompact hook logs to stderr', async () => {
const result = await runHookWithInput(path.join(scriptsDir, 'pre-compact.js'), {});
assert.ok(result.stderr.includes('[PreCompact]'), 'Should output to stderr with prefix');