import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; import test from 'node:test'; import { fileURLToPath } from 'node:url'; import { tempDir } from '../scripts/support.mjs'; import { evaluateHookEvent, parseHookEvent, runHook } from '../lib/hook.mjs'; const bin = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'bin', 'dev-diagnosis.js'); function repoWithAiignore(entries, nested = {}) { const root = tempDir('dev-diagnosis-hook-'); fs.writeFileSync(path.join(root, '.aiignore'), entries); fs.writeFileSync(path.join(root, 'src.js'), 'x\n'); for (const [dir, content] of Object.entries(nested)) { fs.mkdirSync(path.join(root, dir), { recursive: true }); fs.writeFileSync(path.join(root, dir, '.aiignore'), content); fs.writeFileSync(path.join(root, dir, 'file.js'), 'x\n'); } return root; } function hookEvent(toolName, filePath, cwd) { return { hook_event_name: 'PreToolUse', tool_name: toolName, tool_input: { file_path: filePath }, cwd }; } test('hook: parseHookEvent returns null for empty, broken or non-object input', () => { assert.equal(parseHookEvent(''), null); assert.equal(parseHookEvent(' \n'), null); assert.equal(parseHookEvent('not json'), null); assert.equal(parseHookEvent('[1, 2]'), null); assert.equal(parseHookEvent('"str"'), null); assert.deepEqual(parseHookEvent('{"tool_name":"Read"}'), { tool_name: 'Read' }); }); test('hook: a Read of an .aiignore-covered file is blocked with exit 2 and the reason', () => { const root = repoWithAiignore('dist/\n*.log\n'); const judged = evaluateHookEvent(hookEvent('Read', 'dist/bundle.js', root), { root }); assert.equal(judged.blocked, true); assert.equal(judged.filePath, 'dist/bundle.js'); assert.equal(judged.pattern, 'dist/'); assert.match(judged.reason, /Blocked by \.aiignore/); assert.match(judged.reason, /dist\/bundle\.js matches 'dist\/' in \.aiignore/); assert.deepEqual(runHook(JSON.stringify(hookEvent('Read', 'dist/bundle.js', root)), { root }), { code: 2, message: judged.reason }); }); test('hook: a Read of a clean file is allowed', () => { const root = repoWithAiignore('dist/\n*.log\n'); assert.deepEqual(runHook(JSON.stringify(hookEvent('Read', 'src.js', root)), { root }), { code: 0, message: null }); }); test('hook: Edit and Write are blocked too, other tools are not judged', () => { const root = repoWithAiignore('*.log\n'); for (const tool of ['Edit', 'Write']) { assert.equal(evaluateHookEvent(hookEvent(tool, 'debug.log', root), { root }).blocked, true); } assert.equal(evaluateHookEvent(hookEvent('Bash', 'debug.log', root), { root }).blocked, false); assert.equal(evaluateHookEvent(hookEvent('Glob', '*.log', root), { root }).blocked, false); // A tool with no file path has nothing to judge. assert.equal(evaluateHookEvent({ tool_name: 'Read', tool_input: {}, cwd: root }, { root }).blocked, false); }); test('hook: a nested .aiignore only covers its own directory', () => { const root = repoWithAiignore('', { pkg: 'secret.txt\n' }); assert.equal(evaluateHookEvent(hookEvent('Read', 'pkg/secret.txt', root), { root }).blocked, true); assert.equal(evaluateHookEvent(hookEvent('Read', 'secret.txt', root), { root }).blocked, false); assert.equal(evaluateHookEvent(hookEvent('Read', 'pkg/file.js', root), { root }).blocked, false); }); test('hook: a path outside the repo is allowed, and a broken event never blocks', () => { const root = repoWithAiignore('*.log\n'); assert.equal(evaluateHookEvent(hookEvent('Read', '../outside.log', root), { root }).blocked, false); assert.deepEqual(runHook('', { root }), { code: 0, message: null }); assert.deepEqual(runHook('not json', { root }), { code: 0, message: null }); assert.deepEqual(runHook('{}', { root }), { code: 0, message: null }); }); test('hook: the CLI blocks a covered read with exit 2 and the reason on stderr', () => { const root = repoWithAiignore('dist/\n'); const blocked = spawnSync(process.execPath, [bin, 'hook'], { encoding: 'utf8', input: JSON.stringify(hookEvent('Read', 'dist/bundle.js', root)), cwd: root, }); assert.equal(blocked.status, 2); assert.match(blocked.stderr, /Blocked by \.aiignore: dist\/bundle\.js matches 'dist\/' in \.aiignore/); const allowed = spawnSync(process.execPath, [bin, 'hook'], { encoding: 'utf8', input: JSON.stringify(hookEvent('Read', 'src.js', root)), cwd: root, }); assert.equal(allowed.status, 0); assert.equal(allowed.stderr, ''); }); test('hook: the CLI with no stdin input explains itself instead of hanging', () => { const root = repoWithAiignore('dist/\n'); const result = spawnSync(process.execPath, [bin, 'hook'], { encoding: 'utf8', input: '', cwd: root }); assert.equal(result.status, 2); assert.match(result.stderr, /reads a Claude Code hook event as JSON on stdin/); }); test('hook: the CLI rejects options', () => { const root = repoWithAiignore('dist/\n'); const result = spawnSync(process.execPath, [bin, 'hook', '--json'], { encoding: 'utf8', input: JSON.stringify(hookEvent('Read', 'src.js', root)), cwd: root, }); assert.equal(result.status, 2); assert.match(result.stderr, /hook takes no options/); });