import { describe, expect, it, vi } from 'vitest'; import { registerAppServerPermissionHandlers } from './appServerPermissionAdapter'; type UserInputHandler = NonNullable[0]['onUserInputRequest']>; function createClient() { const handlers = new Map Promise | unknown>(); return { client: { registerRequestHandler(method: string, handler: (params: unknown) => Promise | unknown) { handlers.set(method, handler); } }, handlers }; } describe('registerAppServerPermissionHandlers', () => { it('forwards request_user_input answers through the callback', async () => { const { client, handlers } = createClient(); const permissionHandler = { handleToolCall: vi.fn() }; const onUserInputRequest: UserInputHandler = async ({ id, input }) => { expect(id).toBe('tool-123'); expect(input).toEqual({ itemId: 'tool-123', questions: [{ id: 'approve_nav', question: 'Approve app tool call?' }] }); return { decision: 'accept', answers: { approve_nav: { answers: ['Allow'] } } }; }; registerAppServerPermissionHandlers({ client: client as never, permissionHandler: permissionHandler as never, onUserInputRequest: vi.fn(onUserInputRequest) }); const handler = handlers.get('item/tool/requestUserInput'); expect(handler).toBeTypeOf('function'); await expect(handler?.({ itemId: 'tool-123', questions: [{ id: 'approve_nav', question: 'Approve app tool call?' }] })).resolves.toEqual({ decision: 'accept', answers: { approve_nav: { answers: ['Allow'] } } }); }); it('cancels request_user_input when no callback is registered', async () => { const { client, handlers } = createClient(); const permissionHandler = { handleToolCall: vi.fn() }; registerAppServerPermissionHandlers({ client: client as never, permissionHandler: permissionHandler as never }); const handler = handlers.get('item/tool/requestUserInput'); expect(handler).toBeTypeOf('function'); await expect(handler?.({ itemId: 'tool-123' })).resolves.toEqual({ decision: 'cancel' }); }); });