|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +const { |
| 4 | + mockBrowserSession, |
| 5 | + mockGetDaemonHealth, |
| 6 | + mockProbeCDP, |
| 7 | + mockResolveElectronEndpoint, |
| 8 | + mockEmitHook, |
| 9 | +} = vi.hoisted(() => ({ |
| 10 | + mockBrowserSession: vi.fn(async (_Factory, fn) => fn({ |
| 11 | + goto: vi.fn(), |
| 12 | + wait: vi.fn(), |
| 13 | + } as any)), |
| 14 | + mockGetDaemonHealth: vi.fn(), |
| 15 | + mockProbeCDP: vi.fn(), |
| 16 | + mockResolveElectronEndpoint: vi.fn(), |
| 17 | + mockEmitHook: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('./runtime.js', async () => { |
| 21 | + const actual = await vi.importActual<typeof import('./runtime.js')>('./runtime.js'); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + browserSession: mockBrowserSession, |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +vi.mock('./browser/daemon-client.js', () => ({ |
| 29 | + getDaemonHealth: mockGetDaemonHealth, |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('./launcher.js', () => ({ |
| 33 | + probeCDP: mockProbeCDP, |
| 34 | + resolveElectronEndpoint: mockResolveElectronEndpoint, |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock('./hooks.js', () => ({ |
| 38 | + emitHook: mockEmitHook, |
| 39 | +})); |
| 40 | + |
| 41 | +import { CDPBridge } from './browser/index.js'; |
| 42 | +import { executeCommand } from './execution.js'; |
| 43 | +import { cli, Strategy } from './registry.js'; |
| 44 | + |
| 45 | +const youtubeCommand = cli({ |
| 46 | + site: 'youtube', |
| 47 | + name: 'search', |
| 48 | + description: 'search', |
| 49 | + browser: true, |
| 50 | + strategy: Strategy.COOKIE, |
| 51 | + domain: 'www.youtube.com', |
| 52 | + navigateBefore: false, |
| 53 | + func: vi.fn(async () => 'ok'), |
| 54 | +}); |
| 55 | + |
| 56 | +const cursorCommand = cli({ |
| 57 | + site: 'cursor', |
| 58 | + name: 'status', |
| 59 | + description: 'status', |
| 60 | + browser: true, |
| 61 | + strategy: Strategy.COOKIE, |
| 62 | + navigateBefore: false, |
| 63 | + func: vi.fn(async () => 'ok'), |
| 64 | +}); |
| 65 | + |
| 66 | +describe('executeCommand manual CDP routing', () => { |
| 67 | + beforeEach(() => { |
| 68 | + vi.unstubAllEnvs(); |
| 69 | + vi.clearAllMocks(); |
| 70 | + mockGetDaemonHealth.mockResolvedValue({ state: 'ready', status: { extensionConnected: true } }); |
| 71 | + mockProbeCDP.mockResolvedValue(true); |
| 72 | + mockResolveElectronEndpoint.mockResolvedValue('http://127.0.0.1:9333'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('uses CDPBridge for non-Electron browser commands when OPENCLI_CDP_ENDPOINT is set', async () => { |
| 76 | + vi.stubEnv('OPENCLI_CDP_ENDPOINT', 'https://abcdef.ngrok.app'); |
| 77 | + |
| 78 | + await expect(executeCommand(youtubeCommand, {})).resolves.toBe('ok'); |
| 79 | + |
| 80 | + expect(mockGetDaemonHealth).not.toHaveBeenCalled(); |
| 81 | + expect(mockProbeCDP).not.toHaveBeenCalled(); |
| 82 | + expect(mockBrowserSession).toHaveBeenCalledWith( |
| 83 | + CDPBridge, |
| 84 | + expect.any(Function), |
| 85 | + expect.objectContaining({ cdpEndpoint: 'https://abcdef.ngrok.app' }), |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('preserves manual-endpoint validation for Electron apps', async () => { |
| 90 | + vi.stubEnv('OPENCLI_CDP_ENDPOINT', 'http://127.0.0.1:9222'); |
| 91 | + |
| 92 | + await expect(executeCommand(cursorCommand, {})).resolves.toBe('ok'); |
| 93 | + |
| 94 | + expect(mockProbeCDP).toHaveBeenCalledWith(9222); |
| 95 | + expect(mockGetDaemonHealth).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('keeps Browser Bridge checks when no manual endpoint is set', async () => { |
| 99 | + mockGetDaemonHealth.mockResolvedValue({ state: 'no-extension', status: { extensionConnected: false } }); |
| 100 | + |
| 101 | + await expect(executeCommand(youtubeCommand, {})).rejects.toThrow('Browser Bridge extension not connected'); |
| 102 | + }); |
| 103 | +}); |
0 commit comments