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

@@ -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');