|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { EXIT, run, type AnalyzeCliDeps } from './analyze-cli.js'; |
| 3 | +import type { AnalyzeBrowser } from './analyze/analyze.js'; |
| 4 | +import type { RawFormsResult } from './analyze/form-script.js'; |
| 5 | + |
| 6 | +const contactForm: RawFormsResult = { |
| 7 | + challenge: false, |
| 8 | + forms: [{ name: 'contact', submitRef: '@e4', submitLabel: 'Send', fields: [ |
| 9 | + { ref: '@e1', label: 'Name', name: 'name', type: 'text', role: 'input', required: true }, |
| 10 | + { ref: '@e2', label: 'Email', name: 'email', type: 'email', role: 'input', required: true }, |
| 11 | + ] }], |
| 12 | +}; |
| 13 | +const lead = { lead: { name: 'Jane', email: 'jane@example.com' } }; |
| 14 | + |
| 15 | +function browser(raw: RawFormsResult, hooks: Partial<AnalyzeBrowser> = {}): AnalyzeBrowser { |
| 16 | + return { |
| 17 | + snapshot: async () => ({ url: 'https://x/contact', title: 'Contact', timestamp: 't', elements: [] }), |
| 18 | + readForms: async () => raw, |
| 19 | + fill: hooks.fill ?? (async () => {}), |
| 20 | + click: hooks.click ?? (async () => {}), |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +function harness(overrides: Partial<AnalyzeCliDeps> = {}, raw = contactForm) { |
| 25 | + const out: string[] = []; |
| 26 | + const err: string[] = []; |
| 27 | + const close = vi.fn(); |
| 28 | + const deps: Partial<AnalyzeCliDeps> = { |
| 29 | + env: {}, |
| 30 | + attach: async () => ({ browser: browser(raw), close }), |
| 31 | + readData: async () => lead, |
| 32 | + out: (t) => out.push(t), |
| 33 | + err: (t) => err.push(t), |
| 34 | + ...overrides, |
| 35 | + }; |
| 36 | + return { deps, out, err, close }; |
| 37 | +} |
| 38 | + |
| 39 | +describe('analyze CLI', () => { |
| 40 | + it('prints a JSON plan and closes the session', async () => { |
| 41 | + const { deps, out, close } = harness(); |
| 42 | + const code = await run(['Fill contact form', '--data', './lead.json', '--json'], deps); |
| 43 | + expect(code).toBe(EXIT.ok); |
| 44 | + const result = JSON.parse(out.join('\n')); |
| 45 | + expect(result.status).toBe('planned'); |
| 46 | + expect(result.detectedForms[0].fields[1].valueFrom).toBe('lead.email'); |
| 47 | + expect(close).toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('treats a bare mode keyword as mode, not a goal', async () => { |
| 51 | + const { deps, out } = harness(); |
| 52 | + await run(['form', '--json'], deps); |
| 53 | + expect(JSON.parse(out.join('\n')).goal).toBeUndefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('exits notOk (6) when required data is missing', async () => { |
| 57 | + const { deps } = harness({ readData: async () => ({ lead: { name: 'Jane' } }) }); |
| 58 | + expect(await run(['Fill', '--data', 'x', '--json'], deps)).toBe(EXIT.notOk); |
| 59 | + }); |
| 60 | + |
| 61 | + it('exits noSession (4) when no managed session', async () => { |
| 62 | + const { deps, err } = harness({ |
| 63 | + attach: async () => { |
| 64 | + const e = new Error('No managed session. Run: tron browser launch') as Error & { exit?: number }; |
| 65 | + e.exit = EXIT.noSession; |
| 66 | + throw e; |
| 67 | + }, |
| 68 | + }); |
| 69 | + const code = await run(['form'], deps); |
| 70 | + expect(code).toBe(EXIT.noSession); |
| 71 | + expect(err.join('\n')).toContain('tron browser launch'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('executes fills but not submit with --execute --no-submit', async () => { |
| 75 | + const fill = vi.fn(async () => {}); |
| 76 | + const click = vi.fn(async () => {}); |
| 77 | + const { deps } = harness({ attach: async () => ({ browser: browser(contactForm, { fill, click }), close: vi.fn() }) }); |
| 78 | + const code = await run(['Fill', '--data', 'x', '--execute', '--no-submit', '--json'], deps); |
| 79 | + expect(code).toBe(EXIT.ok); |
| 80 | + expect(fill).toHaveBeenCalledTimes(2); |
| 81 | + expect(click).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('rejects an invalid --policy', async () => { |
| 85 | + const { deps } = harness(); |
| 86 | + expect(await run(['form', '--policy', 'wild'], deps)).toBe(EXIT.usage); |
| 87 | + }); |
| 88 | +}); |
0 commit comments