Files
hapi/cli/src/claude/utils/permissionHandler.test.ts
T
bd13fac7ae fix(claude): apply mid-turn permission mode changes to canCallTool (#764)
`PermissionHandler` stored its own `permissionMode` field and only updated
it inside `handleModeChange`, which is called when a new batch is pulled
from the queue. The `SetSessionConfig` RPC (web dropdown changes) updates
`runClaude.ts`'s `currentPermissionMode` and the session keepalive
metadata, but never reaches the handler — so switching to Yolo mid-turn
left `canCallTool` checking the stale mode and still prompting for
approval. Closes #735.

Drop the stored field and read live from `session.getPermissionMode()`,
mirroring how the OpenCode permission handler already works. Override
`Session.getPermissionMode()` in `claude/session.ts` to return the
Claude-narrow `PermissionMode`, sound because the matching
`setPermissionMode` setter only accepts that subset.

via [HAPI](https://hapi.run)

Co-authored-by: HAPI <noreply@hapi.run>
2026-06-01 18:50:54 +08:00

144 lines
5.2 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { PermissionHandler } from './permissionHandler';
import { PLAN_FAKE_REJECT, PLAN_FAKE_RESTART } from '../sdk/prompts';
import type { Session } from '../session';
function createFakeSession() {
const queueItems: { message: string; mode: unknown }[] = [];
let permissionMode: string | undefined;
const session = {
client: {
rpcHandlerManager: {
registerHandler: vi.fn(),
},
updateAgentState: vi.fn(),
},
queue: {
unshift: vi.fn((message: string, mode: unknown) => {
queueItems.push({ message, mode });
}),
},
setPermissionMode: vi.fn((mode: string) => {
permissionMode = mode;
}),
getPermissionMode: vi.fn(() => permissionMode),
} as unknown as Session;
return { session, queueItems };
}
describe('PermissionHandler — YOLO plan mode', () => {
it('injects PLAN_FAKE_RESTART and denies exit_plan_mode in bypassPermissions', async () => {
const { session, queueItems } = createFakeSession();
const handler = new PermissionHandler(session);
handler.handleModeChange('bypassPermissions');
// Simulate Claude emitting an assistant message with exit_plan_mode tool_use
handler.onMessage({
type: 'assistant',
message: {
role: 'assistant',
content: [{ type: 'tool_use', id: 'tc-1', name: 'exit_plan_mode', input: {} }],
},
} as any);
const result = await handler.handleToolCall(
'exit_plan_mode',
{},
{ permissionMode: 'bypassPermissions' } as any,
{ signal: new AbortController().signal }
);
// Should deny with PLAN_FAKE_REJECT (so Claude restarts)
expect(result.behavior).toBe('deny');
expect(result).toEqual({ behavior: 'deny', message: PLAN_FAKE_REJECT });
// Should inject PLAN_FAKE_RESTART into the queue
expect(queueItems).toHaveLength(1);
expect(queueItems[0].message).toBe(PLAN_FAKE_RESTART);
expect(queueItems[0].mode).toEqual({ permissionMode: 'bypassPermissions' });
});
it('injects PLAN_FAKE_RESTART for ExitPlanMode variant', async () => {
const { session, queueItems } = createFakeSession();
const handler = new PermissionHandler(session);
handler.handleModeChange('bypassPermissions');
handler.onMessage({
type: 'assistant',
message: {
role: 'assistant',
content: [{ type: 'tool_use', id: 'tc-2', name: 'ExitPlanMode', input: {} }],
},
} as any);
const result = await handler.handleToolCall(
'ExitPlanMode',
{},
{ permissionMode: 'bypassPermissions' } as any,
{ signal: new AbortController().signal }
);
expect(result.behavior).toBe('deny');
expect(result).toEqual({ behavior: 'deny', message: PLAN_FAKE_REJECT });
expect(queueItems).toHaveLength(1);
expect(queueItems[0].message).toBe(PLAN_FAKE_RESTART);
});
it('allows normal tools in bypassPermissions without queue injection', async () => {
const { session, queueItems } = createFakeSession();
const handler = new PermissionHandler(session);
handler.handleModeChange('bypassPermissions');
handler.onMessage({
type: 'assistant',
message: {
role: 'assistant',
content: [{ type: 'tool_use', id: 'tc-3', name: 'Bash', input: { command: 'ls' } }],
},
} as any);
const result = await handler.handleToolCall(
'Bash',
{ command: 'ls' },
{ permissionMode: 'bypassPermissions' } as any,
{ signal: new AbortController().signal }
);
expect(result.behavior).toBe('allow');
expect(queueItems).toHaveLength(0);
});
// Regression: turn-in-progress switch from default to bypassPermissions via
// SetSessionConfig RPC updates session.setPermissionMode but doesn't go
// through handler.handleModeChange. The next canCallTool must reflect the
// new mode. See issue #735.
it('reflects session permission mode changes between tool calls', async () => {
const { session } = createFakeSession();
const handler = new PermissionHandler(session);
handler.handleModeChange('default');
// Simulate RPC handler in runClaude updating the session directly,
// bypassing handler.handleModeChange (as happens on web dropdown change).
session.setPermissionMode('bypassPermissions');
handler.onMessage({
type: 'assistant',
message: {
role: 'assistant',
content: [{ type: 'tool_use', id: 'tc-4', name: 'Bash', input: { command: 'ls' } }],
},
} as any);
const result = await handler.handleToolCall(
'Bash',
{ command: 'ls' },
{ permissionMode: 'bypassPermissions' } as any,
{ signal: new AbortController().signal }
);
expect(result.behavior).toBe('allow');
});
});