|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { test } from 'vitest'; |
| 4 | +import type { AgentDeviceClient } from '../../client/client-types.ts'; |
| 5 | +import type { RequestProgressSink } from '../../daemon/request-progress.ts'; |
| 6 | +import { AppError } from '../../kernel/errors.ts'; |
| 7 | +import type { AcpSessionUpdate } from '../contracts.ts'; |
| 8 | +import { createPromptRunner, type PromptRunnerDeps } from '../prompt-runner.ts'; |
| 9 | +import type { AcpSessionState } from '../session-state.ts'; |
| 10 | + |
| 11 | +const FAKE_CLIENT = {} as AgentDeviceClient; |
| 12 | + |
| 13 | +function makeSession(sticky = {}): AcpSessionState { |
| 14 | + return { |
| 15 | + id: 'sess-1', |
| 16 | + cwd: tmpdir(), |
| 17 | + sticky, |
| 18 | + cancelled: false, |
| 19 | + promptActive: true, |
| 20 | + toolCallCounter: 0, |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +function collectUpdates(): { |
| 25 | + updates: AcpSessionUpdate[]; |
| 26 | + sendUpdate: (u: AcpSessionUpdate) => void; |
| 27 | +} { |
| 28 | + const updates: AcpSessionUpdate[] = []; |
| 29 | + return { updates, sendUpdate: (update) => updates.push(update) }; |
| 30 | +} |
| 31 | + |
| 32 | +function runnerWith(deps: Partial<PromptRunnerDeps>) { |
| 33 | + return createPromptRunner({ |
| 34 | + createClient: async () => FAKE_CLIENT, |
| 35 | + readImageFile: () => undefined, |
| 36 | + ...deps, |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +test('runs each prompt line as a tool call and summarizes the turn', async () => { |
| 41 | + const executed: string[] = []; |
| 42 | + const runner = runnerWith({ |
| 43 | + runCommandLine: async ({ command }) => { |
| 44 | + executed.push(command); |
| 45 | + return { result: { ok: true }, cliOutput: { data: {}, text: `${command} done` } }; |
| 46 | + }, |
| 47 | + }); |
| 48 | + const { updates, sendUpdate } = collectUpdates(); |
| 49 | + |
| 50 | + const stopReason = await runner.runPrompt({ |
| 51 | + session: makeSession(), |
| 52 | + promptText: 'devices\nsnapshot -i', |
| 53 | + sendUpdate, |
| 54 | + }); |
| 55 | + |
| 56 | + assert.equal(stopReason, 'end_turn'); |
| 57 | + assert.deepEqual(executed, ['devices', 'snapshot']); |
| 58 | + const kinds = updates.map((update) => update.sessionUpdate); |
| 59 | + assert.deepEqual(kinds, [ |
| 60 | + 'tool_call', |
| 61 | + 'tool_call_update', |
| 62 | + 'tool_call', |
| 63 | + 'tool_call_update', |
| 64 | + 'agent_message_chunk', |
| 65 | + ]); |
| 66 | + const firstCall = updates[0] as Extract<AcpSessionUpdate, { sessionUpdate: 'tool_call' }>; |
| 67 | + assert.equal(firstCall.title, 'devices'); |
| 68 | + assert.equal(firstCall.status, 'in_progress'); |
| 69 | + assert.equal(firstCall.kind, 'read'); |
| 70 | + const firstDone = updates[1] as Extract<AcpSessionUpdate, { sessionUpdate: 'tool_call_update' }>; |
| 71 | + assert.equal(firstDone.toolCallId, firstCall.toolCallId); |
| 72 | + assert.equal(firstDone.status, 'completed'); |
| 73 | + assert.deepEqual(firstDone.rawOutput, { ok: true }); |
| 74 | + assert.match(JSON.stringify(firstDone.content), /devices done/); |
| 75 | + const summary = updates.at(-1) as Extract< |
| 76 | + AcpSessionUpdate, |
| 77 | + { sessionUpdate: 'agent_message_chunk' } |
| 78 | + >; |
| 79 | + assert.match(summary.content.type === 'text' ? summary.content.text : '', /✓ devices/); |
| 80 | +}); |
| 81 | + |
| 82 | +test('command failures become failed tool calls with the full error contract', async () => { |
| 83 | + const runner = runnerWith({ |
| 84 | + runCommandLine: async () => { |
| 85 | + throw new AppError('DEVICE_NOT_FOUND', 'No booted device.', { hint: 'Boot one first.' }); |
| 86 | + }, |
| 87 | + }); |
| 88 | + const { updates, sendUpdate } = collectUpdates(); |
| 89 | + |
| 90 | + const stopReason = await runner.runPrompt({ |
| 91 | + session: makeSession(), |
| 92 | + promptText: 'devices', |
| 93 | + sendUpdate, |
| 94 | + }); |
| 95 | + |
| 96 | + assert.equal(stopReason, 'end_turn'); |
| 97 | + const failed = updates[1] as Extract<AcpSessionUpdate, { sessionUpdate: 'tool_call_update' }>; |
| 98 | + assert.equal(failed.status, 'failed'); |
| 99 | + const text = JSON.stringify(failed.content); |
| 100 | + assert.match(text, /Error \(DEVICE_NOT_FOUND\): No booted device\./); |
| 101 | + assert.match(text, /Hint: Boot one first\./); |
| 102 | +}); |
| 103 | + |
| 104 | +test('prompts with no runnable command lines are refused without touching the daemon', async () => { |
| 105 | + let clientCreated = false; |
| 106 | + const runner = runnerWith({ |
| 107 | + createClient: async () => { |
| 108 | + clientCreated = true; |
| 109 | + return FAKE_CLIENT; |
| 110 | + }, |
| 111 | + runCommandLine: async () => { |
| 112 | + throw new Error('must not run'); |
| 113 | + }, |
| 114 | + }); |
| 115 | + const { updates, sendUpdate } = collectUpdates(); |
| 116 | + |
| 117 | + const stopReason = await runner.runPrompt({ |
| 118 | + session: makeSession(), |
| 119 | + promptText: 'please tap the login button', |
| 120 | + sendUpdate, |
| 121 | + }); |
| 122 | + |
| 123 | + assert.equal(stopReason, 'refusal'); |
| 124 | + assert.equal(clientCreated, false); |
| 125 | + assert.equal(updates.length, 1); |
| 126 | + assert.equal(updates[0]!.sessionUpdate, 'agent_message_chunk'); |
| 127 | +}); |
| 128 | + |
| 129 | +test('unparseable lines fail individually while runnable lines still execute', async () => { |
| 130 | + const executed: string[] = []; |
| 131 | + const runner = runnerWith({ |
| 132 | + runCommandLine: async ({ command }) => { |
| 133 | + executed.push(command); |
| 134 | + return { result: {}, cliOutput: { data: {}, text: 'ok' } }; |
| 135 | + }, |
| 136 | + }); |
| 137 | + const { updates, sendUpdate } = collectUpdates(); |
| 138 | + |
| 139 | + const stopReason = await runner.runPrompt({ |
| 140 | + session: makeSession(), |
| 141 | + promptText: 'frobnicate\ndevices', |
| 142 | + sendUpdate, |
| 143 | + }); |
| 144 | + |
| 145 | + assert.equal(stopReason, 'end_turn'); |
| 146 | + assert.deepEqual(executed, ['devices']); |
| 147 | + const failedCall = updates[0] as Extract<AcpSessionUpdate, { sessionUpdate: 'tool_call' }>; |
| 148 | + assert.equal(failedCall.status, 'failed'); |
| 149 | + assert.match(JSON.stringify(failedCall.content), /Unknown command: frobnicate/); |
| 150 | +}); |
| 151 | + |
| 152 | +test('sticky flags from one line carry into later lines of the same session', async () => { |
| 153 | + const platforms: Array<string | undefined> = []; |
| 154 | + const session = makeSession(); |
| 155 | + const runner = runnerWith({ |
| 156 | + runCommandLine: async ({ flags }) => { |
| 157 | + platforms.push(flags.platform); |
| 158 | + return { result: {}, cliOutput: { data: {}, text: 'ok' } }; |
| 159 | + }, |
| 160 | + }); |
| 161 | + const { sendUpdate } = collectUpdates(); |
| 162 | + |
| 163 | + await runner.runPrompt({ |
| 164 | + session, |
| 165 | + promptText: 'apps --platform ios\nsnapshot -i', |
| 166 | + sendUpdate, |
| 167 | + }); |
| 168 | + |
| 169 | + assert.deepEqual(platforms, ['ios', 'ios']); |
| 170 | + assert.equal(session.sticky.platform, 'ios'); |
| 171 | +}); |
| 172 | + |
| 173 | +test('cancellation between lines stops the turn with the cancelled stop reason', async () => { |
| 174 | + const session = makeSession(); |
| 175 | + const executed: string[] = []; |
| 176 | + const runner = runnerWith({ |
| 177 | + runCommandLine: async ({ command }) => { |
| 178 | + executed.push(command); |
| 179 | + session.cancelled = true; |
| 180 | + return { result: {}, cliOutput: { data: {}, text: 'ok' } }; |
| 181 | + }, |
| 182 | + }); |
| 183 | + const { sendUpdate } = collectUpdates(); |
| 184 | + |
| 185 | + const stopReason = await runner.runPrompt({ |
| 186 | + session, |
| 187 | + promptText: 'devices\nsnapshot -i', |
| 188 | + sendUpdate, |
| 189 | + }); |
| 190 | + |
| 191 | + assert.equal(stopReason, 'cancelled'); |
| 192 | + assert.deepEqual(executed, ['devices']); |
| 193 | +}); |
| 194 | + |
| 195 | +test('screenshot results attach an inline image when the artifact is readable', async () => { |
| 196 | + const runner = runnerWith({ |
| 197 | + runCommandLine: async () => ({ |
| 198 | + result: { path: '/artifacts/screen.png' }, |
| 199 | + cliOutput: { data: {}, text: 'Saved screenshot' }, |
| 200 | + }), |
| 201 | + readImageFile: (path) => |
| 202 | + path === '/artifacts/screen.png' |
| 203 | + ? { type: 'image', data: 'aGk=', mimeType: 'image/png' } |
| 204 | + : undefined, |
| 205 | + }); |
| 206 | + const { updates, sendUpdate } = collectUpdates(); |
| 207 | + |
| 208 | + await runner.runPrompt({ session: makeSession(), promptText: 'screenshot', sendUpdate }); |
| 209 | + |
| 210 | + const done = updates[1] as Extract<AcpSessionUpdate, { sessionUpdate: 'tool_call_update' }>; |
| 211 | + assert.equal(done.status, 'completed'); |
| 212 | + const image = done.content?.find((entry) => entry.content.type === 'image'); |
| 213 | + assert.ok(image, 'expected an inline image content block'); |
| 214 | +}); |
| 215 | + |
| 216 | +test('daemon progress events stream as in_progress tool call updates', async () => { |
| 217 | + let progressSink: RequestProgressSink | undefined; |
| 218 | + const runner = runnerWith({ |
| 219 | + createClient: async (_config, onProgress) => { |
| 220 | + progressSink = onProgress; |
| 221 | + return FAKE_CLIENT; |
| 222 | + }, |
| 223 | + runCommandLine: async () => { |
| 224 | + progressSink?.({ type: 'command', status: 'progress', message: 'Booting simulator…' }); |
| 225 | + return { result: {}, cliOutput: { data: {}, text: 'ok' } }; |
| 226 | + }, |
| 227 | + }); |
| 228 | + const { updates, sendUpdate } = collectUpdates(); |
| 229 | + |
| 230 | + await runner.runPrompt({ session: makeSession(), promptText: 'boot', sendUpdate }); |
| 231 | + |
| 232 | + const progress = updates[1] as Extract<AcpSessionUpdate, { sessionUpdate: 'tool_call_update' }>; |
| 233 | + assert.equal(progress.status, 'in_progress'); |
| 234 | + assert.match(JSON.stringify(progress.content), /Booting simulator…/); |
| 235 | +}); |
0 commit comments