Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { runComputerAction } from '../src/actions'
3import type { ComputerDriver } from '../src/types'
4
5type Call = [string, ...Array<unknown>]
6
7const ZOOM_BYTES = new Uint8Array([1, 2, 3])
8
9/** A driver that records every call in order. */
10function createDriver(options: { zoom?: boolean; cursor?: boolean } = {}) {
11 const calls: Array<Call> = []
12 const record =
13 (name: string) =>
14 async (...args: Array<unknown>) => {
15 calls.push([name, ...args])
16 }
17 const driver: ComputerDriver = {
18 display: { width: 1024, height: 768 },
19 environment: 'browser',
20 screenshot: async () => new Uint8Array(),
21 click: record('click'),
22 move: record('move'),
23 mouseDown: record('mouseDown'),
24 mouseUp: record('mouseUp'),
25 scroll: record('scroll'),
26 keyDown: record('keyDown'),
27 keyUp: record('keyUp'),
28 press: record('press'),
29 type: record('type'),
30 }
31 if (options.zoom) {
32 driver.zoom = async (region) => {
33 calls.push(['zoom', region])
34 return ZOOM_BYTES
35 }
36 }
37 if (options.cursor) {
38 driver.cursorPosition = async () => ({ x: 7, y: 9 })
39 }
40 return { driver, calls }
41}
42
43async function run(
44 input: unknown,
45 options?: { zoom?: boolean; cursor?: boolean },
46) {
47 const { driver, calls } = createDriver(options)
48 const result = await runComputerAction(driver, input)
49 return { result, calls }
50}
51
52/** Checks that the action ends after `ms` and not before. */
53async function expectToFinishAfter(input: unknown, ms: number) {
54 vi.useFakeTimers()
55 const { driver } = createDriver()
56 let finished = false
57 void runComputerAction(driver, input).then(() => {
58 finished = true
59 })
60 await vi.advanceTimersByTimeAsync(ms - 1)
61 expect(finished).toBe(false)
62 await vi.advanceTimersByTimeAsync(1)
63 expect(finished).toBe(true)
64}
65
66afterEach(() => {
67 vi.useRealTimers()
68})
69
70describe('Anthropic actions', () => {
71 it('needs no driver call for a screenshot', async () => {
72 expect(await run({ action: 'screenshot' })).toEqual({
73 result: undefined,
74 calls: [],
75 })
76 })
77
78 it('presses each combo in a key action', async () => {
79 const { calls } = await run({ action: 'key', text: 'ctrl+s Return' })
80 expect(calls).toEqual([
81 ['press', ['Control', 's']],
82 ['press', ['Enter']],
83 ])
84 })
85
86 it('repeats a key action', async () => {
87 const { calls } = await run({ action: 'key', text: 'Down', repeat: 2 })
88 expect(calls).toEqual([
89 ['press', ['ArrowDown']],
90 ['press', ['ArrowDown']],
91 ])
92 })
93
94 it('types text', async () => {
95 const { calls } = await run({ action: 'type', text: 'hello' })
96 expect(calls).toEqual([['type', 'hello']])
97 })
98
99 it('moves the mouse', async () => {
100 const { calls } = await run({ action: 'mouse_move', coordinate: [3, 4] })
101 expect(calls).toEqual([['move', 3, 4]])
102 })
103
104 it.each([
105 ['left_click', 'left', 1],
106 ['right_click', 'right', 1],
107 ['middle_click', 'middle', 1],
108 ['double_click', 'left', 2],
109 ['triple_click', 'left', 3],
110 ])('runs %s as a %s click with count %i', async (action, button, count) => {
111 const { calls } = await run({ action, coordinate: [10, 20] })
112 expect(calls).toEqual([['click', 10, 20, { button, count }]])
113 })
114
115 it('holds the modifiers around a click and releases them in reverse', async () => {
116 const { calls } = await run({
117 action: 'left_click',
118 coordinate: [10, 20],
119 text: 'ctrl+shift',
120 })
121 expect(calls).toEqual([
122 ['keyDown', 'Control'],
123 ['keyDown', 'Shift'],
124 ['click', 10, 20, { button: 'left', count: 1 }],
125 ['keyUp', 'Shift'],
126 ['keyUp', 'Control'],
127 ])
128 })
129
130 it('releases the keys that went down when a later keyDown throws', async () => {
131 const { driver, calls } = createDriver()
132 driver.keyDown = async (key) => {
133 calls.push(['keyDown', key])
134 if (key === 'Shift') throw new Error('key down failed')
135 }
136 await expect(
137 runComputerAction(driver, {
138 action: 'left_click',
139 coordinate: [1, 2],
140 text: 'ctrl+shift',
141 }),
142 ).rejects.toThrow('key down failed')
143 expect(calls).toEqual([
144 ['keyDown', 'Control'],
145 ['keyDown', 'Shift'],
146 ['keyUp', 'Control'],
147 ])
148 })
149
150 it('releases the remaining keys when one keyUp throws', async () => {
151 const { driver, calls } = createDriver()
152 driver.keyUp = async (key) => {
153 calls.push(['keyUp', key])
154 if (key === 'Shift') throw new Error('key up failed')
155 }
156 await expect(
157 runComputerAction(driver, {
158 action: 'left_click',
159 coordinate: [1, 2],
160 text: 'ctrl+shift',
161 }),
162 ).rejects.toThrow('key up failed')
163 expect(calls).toEqual([
164 ['keyDown', 'Control'],
165 ['keyDown', 'Shift'],
166 ['click', 1, 2, { button: 'left', count: 1 }],
167 ['keyUp', 'Shift'],
168 ['keyUp', 'Control'],
169 ])
170 })
171
172 it('clicks at the mouse position when the click has no coordinate', async () => {
173 const { calls } = await run({ action: 'left_click' }, { cursor: true })
174 expect(calls).toEqual([['click', 7, 9, { button: 'left', count: 1 }]])
175 })
176
177 it('throws for a click with no coordinate when the driver has no cursorPosition', async () => {
178 await expect(run({ action: 'left_click' })).rejects.toThrow(
179 '"coordinate" is missing',
180 )
181 })
182
183 it('releases the drag button when a later move throws', async () => {
184 const { driver, calls } = createDriver()
185 let moves = 0
186 driver.move = async (x, y) => {
187 calls.push(['move', x, y])
188 moves += 1
189 if (moves === 2) throw new Error('move failed')
190 }
191 await expect(
192 runComputerAction(driver, {
193 action: 'left_click_drag',
194 start_coordinate: [1, 2],
195 coordinate: [3, 4],
196 }),
197 ).rejects.toThrow('move failed')
198 expect(calls).toEqual([
199 ['move', 1, 2],
200 ['mouseDown', 'left'],
201 ['move', 3, 4],
202 ['mouseUp', 'left'],
203 ])
204 })
205
206 it('rejects a key repeat outside 1 to 100 before it presses', async () => {
207 const { driver, calls } = createDriver()
208 await expect(
209 runComputerAction(driver, { action: 'key', text: 'a', repeat: 101 }),
210 ).rejects.toThrow('"repeat"')
211 await expect(
212 runComputerAction(driver, { action: 'key', text: 'a', repeat: 1.5 }),
213 ).rejects.toThrow('"repeat"')
214 expect(calls).toEqual([])
215 })
216
217 it('rejects a hold or a wait longer than 300 seconds', async () => {
218 const { driver, calls } = createDriver()
219 await expect(
220 runComputerAction(driver, {
221 action: 'hold_key',
222 text: 'shift',
223 duration: 301,
224 }),
225 ).rejects.toThrow('"duration"')
226 await expect(
227 runComputerAction(driver, { action: 'wait', duration: 301 }),
228 ).rejects.toThrow('"duration"')
229 expect(calls).toEqual([])
230 })
231
232 it('drags from the start to the end coordinate', async () => {
233 const { calls } = await run({
234 action: 'left_click_drag',
235 start_coordinate: [1, 2],
236 coordinate: [30, 40],
237 })
238 expect(calls).toEqual([
239 ['move', 1, 2],
240 ['mouseDown', 'left'],
241 ['move', 30, 40],
242 ['mouseUp', 'left'],
243 ])
244 })
245
246 it('presses and releases the left button', async () => {
247 const down = await run({ action: 'left_mouse_down' })
248 const up = await run({ action: 'left_mouse_up' })
249 expect([...down.calls, ...up.calls]).toEqual([
250 ['mouseDown', 'left'],
251 ['mouseUp', 'left'],
252 ])
253 })
254
255 it.each([
256 ['up', 0, -300],
257 ['down', 0, 300],
258 ['left', -300, 0],
259 ['right', 300, 0],
260 ])('scrolls %s in 100 px steps', async (direction, deltaX, deltaY) => {
261 const { calls } = await run({
262 action: 'scroll',
263 coordinate: [50, 60],
264 scroll_direction: direction,
265 scroll_amount: 3,
266 })
267 expect(calls).toEqual([['scroll', 50, 60, deltaX, deltaY]])
268 })
269
270 it('holds a modifier while it scrolls', async () => {
271 const { calls } = await run({
272 action: 'scroll',
273 coordinate: [50, 60],
274 scroll_direction: 'down',
275 scroll_amount: 1,
276 text: 'shift',
277 })
278 expect(calls).toEqual([
279 ['keyDown', 'Shift'],
280 ['scroll', 50, 60, 0, 100],
281 ['keyUp', 'Shift'],
282 ])
283 })
284
285 it('throws for an unknown scroll direction', async () => {
286 await expect(
287 run({
288 action: 'scroll',
289 coordinate: [1, 1],
290 scroll_direction: 'sideways',
291 scroll_amount: 1,
292 }),
293 ).rejects.toThrow('"scroll_direction"')
294 })
295
296 it('holds a key for the duration, then releases it', async () => {
297 vi.useFakeTimers()
298 const { driver, calls } = createDriver()
299 const done = runComputerAction(driver, {
300 action: 'hold_key',
301 text: 'shift',
302 duration: 2,
303 })
304 await vi.advanceTimersByTimeAsync(1999)
305 expect(calls).toEqual([['keyDown', 'Shift']])
306 await vi.advanceTimersByTimeAsync(1)
307 await done
308 expect(calls).toEqual([
309 ['keyDown', 'Shift'],
310 ['keyUp', 'Shift'],
311 ])
312 })
313
314 it('waits for the duration in seconds', async () => {
315 await expectToFinishAfter({ action: 'wait', duration: 1.5 }, 1500)
316 })
317
318 it('returns the zoom image for the region', async () => {
319 const { result, calls } = await run(
320 { action: 'zoom', region: [10, 20, 110, 70] },
321 { zoom: true },
322 )
323 expect(result).toEqual({ type: 'image', bytes: ZOOM_BYTES })
324 expect(calls).toEqual([['zoom', { x: 10, y: 20, width: 100, height: 50 }]])
325 })
326
327 it('throws for zoom when the driver has no zoom method', async () => {
328 await expect(run({ action: 'zoom', region: [0, 0, 1, 1] })).rejects.toThrow(
329 'no zoom method',
330 )
331 })
332
333 it('returns the cursor position as text', async () => {
334 const { result } = await run(
335 { action: 'cursor_position' },
336 { cursor: true },
337 )
338 expect(result).toEqual({ type: 'text', text: 'X=7,Y=9' })
339 })
340
341 it('throws for cursor_position when the driver has no cursorPosition method', async () => {
342 await expect(run({ action: 'cursor_position' })).rejects.toThrow(
343 'no cursorPosition method',
344 )
345 })
346
347 it('throws for an unknown action', async () => {
348 await expect(run({ action: 'fly' })).rejects.toThrow(
349 'Unknown computer action "fly"',
350 )
351 })
352
353 it('throws for a missing field', async () => {
354 await expect(run({ action: 'type' })).rejects.toThrow(
355 '"text" must be a string',
356 )
357 })
358})
359
360describe('OpenAI actions', () => {
361 it.each([
362 ['left', 'left'],
363 ['right', 'right'],
364 ['wheel', 'middle'],
365 ['back', 'back'],
366 ['forward', 'forward'],
367 ])('clicks the %s button as %s', async (button, driverButton) => {
368 const { calls } = await run({
369 action: { type: 'click', button, x: 5, y: 6 },
370 })
371 expect(calls).toEqual([['click', 5, 6, { button: driverButton, count: 1 }]])
372 })
373
374 it('holds the keys around a click and releases them in reverse', async () => {
375 const { calls } = await run({
376 action: {
377 type: 'click',
378 button: 'left',
379 x: 5,
380 y: 6,
381 keys: ['CTRL', 'shift'],
382 },
383 })
384 expect(calls).toEqual([
385 ['keyDown', 'Control'],
386 ['keyDown', 'Shift'],
387 ['click', 5, 6, { button: 'left', count: 1 }],
388 ['keyUp', 'Shift'],
389 ['keyUp', 'Control'],
390 ])
391 })
392
393 it('throws for an unknown button', async () => {
394 await expect(
395 run({ action: { type: 'click', button: 'thumb', x: 1, y: 1 } }),
396 ).rejects.toThrow('"button"')
397 })
398
399 it('double-clicks', async () => {
400 const { calls } = await run({
401 action: { type: 'double_click', x: 5, y: 6, keys: null },
402 })
403 expect(calls).toEqual([['click', 5, 6, { button: 'left', count: 2 }]])
404 })
405
406 it('drags along every point of the path', async () => {
407 const { calls } = await run({
408 action: {
409 type: 'drag',
410 path: [
411 { x: 1, y: 1 },
412 { x: 2, y: 2 },
413 { x: 3, y: 3 },
414 ],
415 },
416 })
417 expect(calls).toEqual([
418 ['move', 1, 1],
419 ['mouseDown', 'left'],
420 ['move', 2, 2],
421 ['move', 3, 3],
422 ['mouseUp', 'left'],
423 ])
424 })
425
426 it('drags with a one-point path', async () => {
427 const { calls } = await run({
428 action: { type: 'drag', path: [{ x: 4, y: 4 }] },
429 })
430 expect(calls).toEqual([
431 ['move', 4, 4],
432 ['mouseDown', 'left'],
433 ['mouseUp', 'left'],
434 ])
435 })
436
437 it('throws for a drag with an empty path', async () => {
438 await expect(run({ action: { type: 'drag', path: [] } })).rejects.toThrow(
439 '"path"',
440 )
441 })
442
443 it('presses a keypress combo', async () => {
444 const { calls } = await run({
445 action: { type: 'keypress', keys: ['CTRL', 'S'] },
446 })
447 expect(calls).toEqual([['press', ['Control', 's']]])
448 })
449
450 it('moves the mouse', async () => {
451 const { calls } = await run({ action: { type: 'move', x: 8, y: 9 } })
452 expect(calls).toEqual([['move', 8, 9]])
453 })
454
455 it('scrolls by the pixel deltas', async () => {
456 const { calls } = await run({
457 action: { type: 'scroll', x: 10, y: 20, scroll_x: -15, scroll_y: 250 },
458 })
459 expect(calls).toEqual([['scroll', 10, 20, -15, 250]])
460 })
461
462 it('types text', async () => {
463 const { calls } = await run({ action: { type: 'type', text: 'hi' } })
464 expect(calls).toEqual([['type', 'hi']])
465 })
466
467 it('waits 2 seconds', async () => {
468 await expectToFinishAfter({ action: { type: 'wait' } }, 2000)
469 })
470
471 it('needs no driver call for a screenshot', async () => {
472 expect(await run({ action: { type: 'screenshot' } })).toEqual({
473 result: undefined,
474 calls: [],
475 })
476 })
477
478 it('runs an actions batch in order', async () => {
479 const { result, calls } = await run({
480 actions: [
481 { type: 'click', button: 'left', x: 1, y: 2 },
482 { type: 'type', text: 'abc' },
483 { type: 'keypress', keys: ['ENTER'] },
484 ],
485 pending_safety_checks: [],
486 })
487 expect(result).toBeUndefined()
488 expect(calls).toEqual([
489 ['click', 1, 2, { button: 'left', count: 1 }],
490 ['type', 'abc'],
491 ['press', ['Enter']],
492 ])
493 })
494
495 it('throws for an unknown action type', async () => {
496 await expect(run({ action: { type: 'fly' } })).rejects.toThrow(
497 'Unknown computer action "fly"',
498 )
499 })
500})
501
502it('throws for an input with no action', async () => {
503 await expect(run({ foo: 1 })).rejects.toThrow('"action"')
504})