|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { EXIT, run, type CliDeps } from './automate-cli.js'; |
| 3 | +import type { CdpConnection } from './automation/cdp-client.js'; |
| 4 | +import type { AgentSnapshot } from './automation/snapshot-script.js'; |
| 5 | +import type { SessionDescriptor } from './automation/types.js'; |
| 6 | + |
| 7 | +const descriptor: SessionDescriptor = { |
| 8 | + version: 1, |
| 9 | + pid: 1, |
| 10 | + host: '127.0.0.1', |
| 11 | + port: 9222, |
| 12 | + profileDir: '/x', |
| 13 | + profileName: 'agent', |
| 14 | + headless: false, |
| 15 | + ephemeral: false, |
| 16 | + createdAt: '2026-07-04T00:00:00.000Z', |
| 17 | + activeTabId: 'p1', |
| 18 | +}; |
| 19 | + |
| 20 | +const snap: AgentSnapshot = { |
| 21 | + url: 'https://example.com', |
| 22 | + title: 'Example', |
| 23 | + timestamp: '2026-07-04T00:00:00.000Z', |
| 24 | + elements: [ |
| 25 | + { ref: '@e1', role: 'link', name: 'More', tag: 'a', interactive: true, visible: true, href: 'https://x' }, |
| 26 | + ], |
| 27 | +}; |
| 28 | + |
| 29 | +/** A CdpConnection whose Runtime.evaluate yields `evalValue`. */ |
| 30 | +function conn(evalValue: unknown): CdpConnection { |
| 31 | + return { |
| 32 | + send: (async (method: string) => |
| 33 | + method === 'Runtime.evaluate' ? { result: { value: evalValue } } : {}) as CdpConnection['send'], |
| 34 | + on: vi.fn(), |
| 35 | + close: vi.fn(), |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function harness(overrides: Partial<CliDeps> = {}) { |
| 40 | + const out: string[] = []; |
| 41 | + const err: string[] = []; |
| 42 | + const deps: Partial<CliDeps> = { |
| 43 | + env: {}, |
| 44 | + loadDescriptor: async () => descriptor, |
| 45 | + fetchTargets: async () => [ |
| 46 | + { id: 'p1', type: 'page', url: 'https://example.com', webSocketDebuggerUrl: 'ws://x/p1' }, |
| 47 | + ], |
| 48 | + connect: async () => conn(snap), |
| 49 | + out: (t) => out.push(t), |
| 50 | + err: (t) => err.push(t), |
| 51 | + ...overrides, |
| 52 | + }; |
| 53 | + return { deps, out, err }; |
| 54 | +} |
| 55 | + |
| 56 | +describe('automate-cli run', () => { |
| 57 | + it('prints a text snapshot', async () => { |
| 58 | + const { deps, out } = harness(); |
| 59 | + const code = await run(['snapshot'], deps); |
| 60 | + expect(code).toBe(EXIT.ok); |
| 61 | + expect(out.join('\n')).toContain('@e1 link "More"'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('prints JSON with --json', async () => { |
| 65 | + const { deps, out } = harness(); |
| 66 | + await run(['snapshot', '--json'], deps); |
| 67 | + expect(JSON.parse(out.join('\n')).title).toBe('Example'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('clicks a ref', async () => { |
| 71 | + const { deps, out } = harness({ connect: async () => conn({ ok: true, ref: '@e1' }) }); |
| 72 | + const code = await run(['click', '@e1'], deps); |
| 73 | + expect(code).toBe(EXIT.ok); |
| 74 | + expect(out.join('\n')).toContain('clicked @e1'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('fills a ref', async () => { |
| 78 | + const { deps, out } = harness({ connect: async () => conn({ ok: true, ref: '@e2' }) }); |
| 79 | + const code = await run(['fill', '@e2', 'hello'], deps); |
| 80 | + expect(code).toBe(EXIT.ok); |
| 81 | + expect(out.join('\n')).toContain('filled @e2'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('exits staleRef when a ref no longer resolves', async () => { |
| 85 | + const { deps, err } = harness({ |
| 86 | + connect: async () => conn({ ok: false, error: 'STALE_REF', ref: '@e9' }), |
| 87 | + }); |
| 88 | + const code = await run(['click', '@e9'], deps); |
| 89 | + expect(code).toBe(EXIT.staleRef); |
| 90 | + expect(err.join('\n')).toMatch(/stale/i); |
| 91 | + }); |
| 92 | + |
| 93 | + it('exits noSession when there is no descriptor', async () => { |
| 94 | + const { deps, err } = harness({ |
| 95 | + loadDescriptor: async () => { |
| 96 | + throw new Error('ENOENT'); |
| 97 | + }, |
| 98 | + }); |
| 99 | + const code = await run(['snapshot'], deps); |
| 100 | + expect(code).toBe(EXIT.noSession); |
| 101 | + expect(err.join('\n')).toContain('tron browser launch'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('exits usage when click is missing a ref', async () => { |
| 105 | + const { deps } = harness(); |
| 106 | + expect(await run(['click'], deps)).toBe(EXIT.usage); |
| 107 | + }); |
| 108 | +}); |
0 commit comments