|
| 1 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { EXIT, run, type CliDeps } from './automate-cli.js'; |
| 6 | +import { readCommands } from './automation/trace.js'; |
| 7 | +import type { CdpConnection } from './automation/cdp-client.js'; |
| 8 | + |
| 9 | +// Fake conn: Runtime.evaluate returns {ok:true} — good enough for clickRef/fillRef |
| 10 | +// and for the trace's post-command snapshot capture (shape is not asserted here). |
| 11 | +function conn(hooks: { onEval?: (expr: string) => void } = {}): CdpConnection { |
| 12 | + return { |
| 13 | + send: (async (method: string, params?: { expression?: string }) => { |
| 14 | + if (method === 'Runtime.evaluate') { |
| 15 | + hooks.onEval?.(params?.expression ?? ''); |
| 16 | + return { result: { value: { ok: true, ref: '@e1' } } }; |
| 17 | + } |
| 18 | + return {}; |
| 19 | + }) as CdpConnection['send'], |
| 20 | + on: vi.fn(), |
| 21 | + close: vi.fn(), |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +let dataDir: string; |
| 26 | +let bundle: string; |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + dataDir = mkdtempSync(join(tmpdir(), 'cli-trace-')); |
| 30 | + bundle = join(dataDir, 'run.trontrace'); |
| 31 | +}); |
| 32 | +afterEach(() => rmSync(dataDir, { recursive: true, force: true })); |
| 33 | + |
| 34 | +function harness(evalHook?: (expr: string) => void) { |
| 35 | + const out: string[] = []; |
| 36 | + const err: string[] = []; |
| 37 | + const deps: Partial<CliDeps> = { |
| 38 | + env: { TRONBROWSER_DATA: dataDir }, |
| 39 | + loadDescriptor: async () => ({ |
| 40 | + version: 1, pid: 1, host: '127.0.0.1', port: 9222, profileDir: '/x', |
| 41 | + profileName: 'agent', headless: false, ephemeral: false, createdAt: 't', activeTabId: 'p1', |
| 42 | + }), |
| 43 | + fetchTargets: async () => [{ id: 'p1', type: 'page', webSocketDebuggerUrl: 'ws://x/p1' }], |
| 44 | + connect: async () => conn(evalHook ? { onEval: evalHook } : {}), |
| 45 | + out: (t) => out.push(t), |
| 46 | + err: (t) => err.push(t), |
| 47 | + }; |
| 48 | + return { deps, out, err }; |
| 49 | +} |
| 50 | + |
| 51 | +describe('tron trace / replay', () => { |
| 52 | + it('records commands into a bundle between start and stop', async () => { |
| 53 | + const h = harness(); |
| 54 | + expect(await run(['trace', 'start', bundle], h.deps)).toBe(EXIT.ok); |
| 55 | + await run(['click', '@e1'], h.deps); |
| 56 | + await run(['fill', '@e2', 'secret'], h.deps); |
| 57 | + expect(await run(['trace', 'stop'], h.deps)).toBe(EXIT.ok); |
| 58 | + |
| 59 | + const cmds = await readCommands(bundle); |
| 60 | + expect(cmds.map((c) => c.name)).toEqual(['click', 'fill']); |
| 61 | + expect(cmds[1].args.value).toBe('[redacted]'); // never persists the value |
| 62 | + }); |
| 63 | + |
| 64 | + it('does not record when no trace is active', async () => { |
| 65 | + const h = harness(); |
| 66 | + await run(['click', '@e1'], h.deps); // no trace started |
| 67 | + // starting now yields an empty bundle |
| 68 | + await run(['trace', 'start', bundle], h.deps); |
| 69 | + expect(await readCommands(bundle)).toHaveLength(0); |
| 70 | + }); |
| 71 | + |
| 72 | + it('reports trace status', async () => { |
| 73 | + const h = harness(); |
| 74 | + await run(['trace', 'status'], h.deps); |
| 75 | + await run(['trace', 'start', bundle], h.deps); |
| 76 | + await run(['trace', 'status'], h.deps); |
| 77 | + expect(h.out[0]).toBe('no active trace'); |
| 78 | + expect(h.out[h.out.length - 1]).toContain(bundle); |
| 79 | + }); |
| 80 | + |
| 81 | + it('replays clicks and skips redacted fills', async () => { |
| 82 | + // record a click + a fill |
| 83 | + const rec = harness(); |
| 84 | + await run(['trace', 'start', bundle], rec.deps); |
| 85 | + await run(['click', '@e1'], rec.deps); |
| 86 | + await run(['fill', '@e2', 'secret'], rec.deps); |
| 87 | + await run(['trace', 'stop'], rec.deps); |
| 88 | + |
| 89 | + // replay against a fresh session, watching which expressions run |
| 90 | + const exprs: string[] = []; |
| 91 | + const play = harness((e) => exprs.push(e)); |
| 92 | + const code = await run(['replay', bundle], play.deps); |
| 93 | + expect(code).toBe(EXIT.ok); |
| 94 | + expect(play.out.join('\n')).toContain('replayed click @e1'); |
| 95 | + expect(play.out.join('\n')).toContain('skip fill @e2 (value redacted)'); |
| 96 | + // the click expression ran; the redacted fill did not |
| 97 | + expect(exprs.some((e) => e.includes("data-tron-ref") && e.includes('click'))).toBe(true); |
| 98 | + }); |
| 99 | + |
| 100 | + it('rejects an unknown trace subcommand', async () => { |
| 101 | + const h = harness(); |
| 102 | + expect(await run(['trace', 'wat'], h.deps)).toBe(EXIT.usage); |
| 103 | + }); |
| 104 | +}); |
0 commit comments