|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { afterEach, test, vi } from 'vitest'; |
| 3 | +import { AppError } from '@agent-device/kernel/errors'; |
| 4 | +import type { AgentDeviceClient } from '../../client/client-types.ts'; |
| 5 | +import type { ToolResult } from '../../mcp/command-tools.ts'; |
| 6 | + |
| 7 | +const createClientCalls: unknown[] = []; |
| 8 | +const executorCalls: Array<{ name: string; input: Record<string, unknown> }> = []; |
| 9 | + |
| 10 | +const defaultExecuteResult: ToolResult = { |
| 11 | + isError: false, |
| 12 | + structuredContent: { ok: true }, |
| 13 | + content: [{ type: 'text', text: 'ok' }], |
| 14 | +}; |
| 15 | +let executeResult: ( |
| 16 | + name: string, |
| 17 | + input: Record<string, unknown>, |
| 18 | +) => Promise<ToolResult> = async () => defaultExecuteResult; |
| 19 | + |
| 20 | +vi.mock('../../agent-device-client.ts', () => ({ |
| 21 | + createAgentDeviceClient: (config: unknown) => { |
| 22 | + createClientCalls.push(config); |
| 23 | + return { marker: 'stub-client' } as unknown as AgentDeviceClient; |
| 24 | + }, |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock('../../mcp/command-tools.ts', async (importOriginal) => { |
| 28 | + const actual = await importOriginal<typeof import('../../mcp/command-tools.ts')>(); |
| 29 | + return { |
| 30 | + ...actual, |
| 31 | + createCommandToolExecutor: () => ({ |
| 32 | + execute: async (name: string, input: Record<string, unknown>) => { |
| 33 | + executorCalls.push({ name, input }); |
| 34 | + return executeResult(name, input); |
| 35 | + }, |
| 36 | + }), |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +const { createAgentDeviceTools } = await import('../index.ts'); |
| 41 | + |
| 42 | +afterEach(() => { |
| 43 | + createClientCalls.length = 0; |
| 44 | + executorCalls.length = 0; |
| 45 | + executeResult = async () => defaultExecuteResult; |
| 46 | +}); |
| 47 | + |
| 48 | +// jsonSchema() (from the real, installed `ai` package) stores the raw schema |
| 49 | +// under `.jsonSchema`, so this reads exactly what the model would see. |
| 50 | +function rawSchemaOf( |
| 51 | + tools: Awaited<ReturnType<typeof createAgentDeviceTools>>['tools'], |
| 52 | + name: string, |
| 53 | +) { |
| 54 | + return ( |
| 55 | + tools[name] as unknown as { |
| 56 | + inputSchema: { jsonSchema: { properties: Record<string, unknown> } }; |
| 57 | + } |
| 58 | + ).inputSchema.jsonSchema; |
| 59 | +} |
| 60 | + |
| 61 | +function executeOf( |
| 62 | + tools: Awaited<ReturnType<typeof createAgentDeviceTools>>['tools'], |
| 63 | + name: string, |
| 64 | +) { |
| 65 | + return tools[name]!.execute as (input: Record<string, unknown>) => Promise<unknown>; |
| 66 | +} |
| 67 | + |
| 68 | +test('defaults to the core tool set and pins the session for every call', async () => { |
| 69 | + const { tools, client, toolApproval } = await createAgentDeviceTools({ session: 'my-session' }); |
| 70 | + |
| 71 | + assert.deepEqual(createClientCalls, [{ session: 'my-session' }]); |
| 72 | + assert.equal((client as unknown as { marker: string }).marker, 'stub-client'); |
| 73 | + assert.equal(toolApproval, undefined); |
| 74 | + |
| 75 | + assert.ok('snapshot' in tools, 'snapshot is core-tier'); |
| 76 | + assert.equal( |
| 77 | + 'devices' in tools, |
| 78 | + false, |
| 79 | + 'devices is extended-tier, excluded from the core default', |
| 80 | + ); |
| 81 | + |
| 82 | + const schema = rawSchemaOf(tools, 'snapshot'); |
| 83 | + assert.equal('session' in schema.properties, false, 'session is always hidden from the model'); |
| 84 | + assert.equal('mcpOutputFormat' in schema.properties, false, 'mcpOutputFormat is always hidden'); |
| 85 | + assert.ok('platform' in schema.properties, 'platform stays visible when not pinned'); |
| 86 | + |
| 87 | + await executeOf(tools, 'snapshot')({ interactiveOnly: true }); |
| 88 | + assert.deepEqual(executorCalls, [ |
| 89 | + { name: 'snapshot', input: { interactiveOnly: true, session: 'my-session' } }, |
| 90 | + ]); |
| 91 | +}); |
| 92 | + |
| 93 | +test("set: 'all' includes extended-tier commands too", async () => { |
| 94 | + const { tools } = await createAgentDeviceTools({ session: 's', set: 'all' }); |
| 95 | + assert.ok('devices' in tools, 'set: all reaches every MCP-exposed command, not just core'); |
| 96 | + assert.ok(Object.keys(tools).length > 20); |
| 97 | +}); |
| 98 | + |
| 99 | +test('platform is pinned per call and removed from the input schema', async () => { |
| 100 | + const { tools } = await createAgentDeviceTools({ session: 's', platform: 'ios' }); |
| 101 | + |
| 102 | + const schema = rawSchemaOf(tools, 'open'); |
| 103 | + assert.equal('platform' in schema.properties, false); |
| 104 | + |
| 105 | + await executeOf(tools, 'open')({ app: 'com.example.app' }); |
| 106 | + assert.deepEqual(executorCalls, [ |
| 107 | + { name: 'open', input: { app: 'com.example.app', session: 's', platform: 'ios' } }, |
| 108 | + ]); |
| 109 | +}); |
| 110 | + |
| 111 | +test('a command error is re-thrown as the normalized AppError, not swallowed', async () => { |
| 112 | + executeResult = async () => ({ |
| 113 | + isError: true, |
| 114 | + structuredContent: { |
| 115 | + code: 'DEVICE_NOT_FOUND', |
| 116 | + message: 'No device matched the selector', |
| 117 | + details: { hint: 'try a broader selector' }, |
| 118 | + }, |
| 119 | + content: [{ type: 'text', text: 'Error (DEVICE_NOT_FOUND): No device matched the selector' }], |
| 120 | + }); |
| 121 | + |
| 122 | + const { tools } = await createAgentDeviceTools({ session: 's' }); |
| 123 | + |
| 124 | + await assert.rejects(executeOf(tools, 'snapshot')({}), (error: unknown) => { |
| 125 | + assert.ok(error instanceof AppError); |
| 126 | + assert.equal(error.code, 'DEVICE_NOT_FOUND'); |
| 127 | + assert.match(error.message, /No device matched the selector/); |
| 128 | + return true; |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +test('approval is returned as toolApproval, ready for ToolLoopAgent', async () => { |
| 133 | + const { toolApproval } = await createAgentDeviceTools({ |
| 134 | + session: 's', |
| 135 | + approval: { close: 'user-approval' }, |
| 136 | + }); |
| 137 | + assert.deepEqual(toolApproval, { close: 'user-approval' }); |
| 138 | +}); |
0 commit comments