Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248import type { PreToolUseInput, } from '@monochromatic-dev/claude-code-plugin-hook-type/ts';import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import { guardrailHandler, type GuardrailOutput,} from './guardrail.ts';
/** Builds a synthetic Agent PreToolUse event with the given tool_input. Test-only helper; production events are produced by Claude Code's hook dispatcher and parsed via `guardrailParser`. @param toolInput - shape passed to the Agent tool @returns event object that satisfies {@link PreToolUseInput} */function makeAgentEvent(toolInput: Record<string, unknown>,): PreToolUseInput { return { hook_event_name: 'PreToolUse', session_id: 'session-test', transcript_path: '/tmp/transcript.jsonl', cwd: '/tmp/cwd', permission_mode: 'default', tool_name: 'Agent', tool_input: toolInput, tool_use_id: 'tool-use-test', };}
/** Builds a synthetic Bash PreToolUse event with the given command string. @param command - shell command that the Bash tool would execute @returns event object that satisfies {@link PreToolUseInput} */function makeBashEvent(command: string,): PreToolUseInput { return { hook_event_name: 'PreToolUse', session_id: 'session-test', transcript_path: '/tmp/transcript.jsonl', cwd: '/tmp/cwd', permission_mode: 'default', tool_name: 'Bash', tool_input: { command, }, tool_use_id: 'tool-use-test', };}
/** Returns whether the handler output is a deny response. @param output - value returned by {@link guardrailHandler} @returns `true` when `output` carries a deny decision */function isDeny(output: GuardrailOutput,): boolean { return ('hookSpecificOutput' in output) && (output.hookSpecificOutput.permissionDecision === 'deny');}
await describe({ name: 'guardrail handler', children: [ describe({ name: 'Agent: general-purpose now allowed', children: [ it({ name: 'allows when subagent_type is missing', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeAgentEvent({},),); e(isDeny(result,),).toBe(false,); }, },), it({ name: 'allows when subagent_type is "general-purpose"', fn: async ({ expect: e, },) => { const result = guardrailHandler( makeAgentEvent({ subagent_type: 'general-purpose', },), ); e(isDeny(result,),).toBe(false,); }, },), it({ name: 'allows specialized subagent_type "Explore"', fn: async ({ expect: e, },) => { const result = guardrailHandler( makeAgentEvent({ subagent_type: 'Explore', },), ); e(isDeny(result,),).toBe(false,); }, },), ], },), describe({ name: 'Agent: resume blocking', children: [ it({ name: 'denies when resume is a string', fn: async ({ expect: e, },) => { const result = guardrailHandler( makeAgentEvent({ subagent_type: 'Explore', resume: 'agent-id-123', },), ); e(isDeny(result,),).toBe(true,); }, },), it({ name: 'allows when resume is absent', fn: async ({ expect: e, },) => { const result = guardrailHandler( makeAgentEvent({ subagent_type: 'Explore', },), ); e(isDeny(result,),).toBe(false,); }, },), ], },), describe({ name: 'Bash: bun test blocking', children: [ it({ name: 'denies "bun test"', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('bun test',),); e(isDeny(result,),).toBe(true,); }, },), it({ name: 'denies "bun test foo.ts"', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('bun test foo.ts',),); e(isDeny(result,),).toBe(true,); }, },), it({ name: 'denies "cd x && bun test"', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('cd x && bun test',),); e(isDeny(result,),).toBe(true,); }, },), it({ name: 'denies "(bun test)"', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('(bun test)',),); e(isDeny(result,),).toBe(true,); }, },), it({ name: 'denies "bun test 2>&1 | tail"', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('bun test 2>&1 | tail',),); e(isDeny(result,),).toBe(true,); }, },), it({ name: 'denies "f(){ bun test; }; f"', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('f(){ bun test; }; f',),); e(isDeny(result,),).toBe(true,); }, },), it({ name: 'allows "node src/foo.unit.test.ts" (direct file invocation)', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('node src/foo.unit.test.ts',),); e(isDeny(result,),).toBe(false,); }, },), it({ name: 'allows "bun build" (different subcommand)', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('bun build',),); e(isDeny(result,),).toBe(false,); }, },), it({ name: 'allows "bun run test" (script invocation, not test runner)', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('bun run test',),); e(isDeny(result,),).toBe(false,); }, },), it({ name: 'allows "mise run //pkg:test"', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('mise run //pkg:test',),); e(isDeny(result,),).toBe(false,); }, },), it({ name: 'allows quoted "bun test" inside echo (segment-anchored)', fn: async ({ expect: e, },) => { const result = guardrailHandler( makeBashEvent('echo "use bun test instead"',), ); e(isDeny(result,),).toBe(false,); }, },), it({ name: 'allows "bunx test" (different binary)', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('bunx test',),); e(isDeny(result,),).toBe(false,); }, },), it({ name: 'allows "bun tests" (different word, no test subcommand)', fn: async ({ expect: e, },) => { const result = guardrailHandler(makeBashEvent('bun tests',),); e(isDeny(result,),).toBe(false,); }, },), ], },), describe({ name: 'other tools pass through', children: [ it({ name: 'allows Edit tool', fn: async ({ expect: e, },) => { const result = guardrailHandler({ hook_event_name: 'PreToolUse', session_id: 'session-test', transcript_path: '/tmp/transcript.jsonl', cwd: '/tmp/cwd', permission_mode: 'default', tool_name: 'Edit', tool_input: { file_path: '/tmp/foo.ts', old_string: 'a', new_string: 'b', }, tool_use_id: 'tool-use-test', },); e(isDeny(result,),).toBe(false,); }, },), ], },), ],},);