|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { CliError } from './errors.js'; |
| 5 | + |
| 6 | +const { TEST_HOME, httpGetMock, httpsGetMock } = vi.hoisted(() => ({ |
| 7 | + TEST_HOME: '/tmp/opencli-connections-vitest', |
| 8 | + httpGetMock: vi.fn(), |
| 9 | + httpsGetMock: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('node:os', async () => { |
| 13 | + const actual = await vi.importActual<typeof import('node:os')>('node:os'); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + homedir: () => TEST_HOME, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +vi.mock('node:http', async () => { |
| 21 | + const actual = await vi.importActual<typeof import('node:http')>('node:http'); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + get: httpGetMock, |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +vi.mock('node:https', async () => { |
| 29 | + const actual = await vi.importActual<typeof import('node:https')>('node:https'); |
| 30 | + return { |
| 31 | + ...actual, |
| 32 | + get: httpsGetMock, |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +import { |
| 37 | + getSavedSiteConnection, |
| 38 | + loadConnections, |
| 39 | + parseCdpPort, |
| 40 | + probeCdpEndpoint, |
| 41 | + removeSiteConnection, |
| 42 | + resolveLiveSiteEndpoint, |
| 43 | + saveSiteConnection, |
| 44 | +} from './connections.js'; |
| 45 | + |
| 46 | +const CONNECTIONS_PATH = path.join(TEST_HOME, '.opencli', 'connections.json'); |
| 47 | + |
| 48 | +function mockRequest() { |
| 49 | + return { |
| 50 | + on: vi.fn().mockReturnThis(), |
| 51 | + setTimeout: vi.fn().mockReturnThis(), |
| 52 | + destroy: vi.fn(), |
| 53 | + } as any; |
| 54 | +} |
| 55 | + |
| 56 | +function mockProbeStatuses(statusByUrl: Record<string, number>) { |
| 57 | + const req = mockRequest(); |
| 58 | + httpGetMock.mockImplementation((input: string | URL, cb: any) => { |
| 59 | + cb({ statusCode: statusByUrl[String(input)] ?? 503, resume() {} }); |
| 60 | + return req; |
| 61 | + }); |
| 62 | + return req; |
| 63 | +} |
| 64 | + |
| 65 | +describe('connections', () => { |
| 66 | + beforeEach(() => { |
| 67 | + fs.rmSync(TEST_HOME, { recursive: true, force: true }); |
| 68 | + delete process.env.OPENCLI_CDP_ENDPOINT; |
| 69 | + httpGetMock.mockReset(); |
| 70 | + httpsGetMock.mockReset(); |
| 71 | + }); |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + delete process.env.OPENCLI_CDP_ENDPOINT; |
| 75 | + vi.restoreAllMocks(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('saves and removes site connections', () => { |
| 79 | + const saved = saveSiteConnection('chatwise', 'http://127.0.0.1:9228/'); |
| 80 | + |
| 81 | + expect(saved.endpoint).toBe('http://127.0.0.1:9228'); |
| 82 | + expect(getSavedSiteConnection('chatwise')?.endpoint).toBe('http://127.0.0.1:9228'); |
| 83 | + expect(loadConnections().cdp.chatwise?.endpoint).toBe('http://127.0.0.1:9228'); |
| 84 | + expect(fs.existsSync(CONNECTIONS_PATH)).toBe(true); |
| 85 | + |
| 86 | + removeSiteConnection('chatwise'); |
| 87 | + |
| 88 | + expect(getSavedSiteConnection('chatwise')).toBeUndefined(); |
| 89 | + expect(loadConnections().cdp.chatwise).toBeUndefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('validates CDP ports', () => { |
| 93 | + expect(parseCdpPort('9222')).toBe(9222); |
| 94 | + expect(() => parseCdpPort('abc')).toThrowError(CliError); |
| 95 | + expect(() => parseCdpPort('70000')).toThrowError(CliError); |
| 96 | + }); |
| 97 | + |
| 98 | + it('probes ws endpoints via the http json/version URL', async () => { |
| 99 | + const req = mockRequest(); |
| 100 | + httpGetMock.mockImplementation((input: string | URL, cb: any) => { |
| 101 | + cb({ statusCode: 200, resume() {} }); |
| 102 | + return req; |
| 103 | + }); |
| 104 | + |
| 105 | + await expect(probeCdpEndpoint('ws://127.0.0.1:9222/devtools/browser/abc')).resolves.toBe(true); |
| 106 | + expect(httpGetMock).toHaveBeenCalledTimes(1); |
| 107 | + expect(String(httpGetMock.mock.calls[0][0])).toBe('http://127.0.0.1:9222/json/version'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('probes https endpoints with the https transport', async () => { |
| 111 | + const req = mockRequest(); |
| 112 | + httpsGetMock.mockImplementation((input: string | URL, cb: any) => { |
| 113 | + cb({ statusCode: 204, resume() {} }); |
| 114 | + return req; |
| 115 | + }); |
| 116 | + |
| 117 | + await expect(probeCdpEndpoint('https://example.com:9222')).resolves.toBe(true); |
| 118 | + expect(httpsGetMock).toHaveBeenCalledTimes(1); |
| 119 | + expect(String(httpsGetMock.mock.calls[0][0])).toBe('https://example.com:9222/json/version'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('rejects unsupported endpoint protocols with a clear error', async () => { |
| 123 | + await expect(probeCdpEndpoint('ftp://example.com')).rejects.toMatchObject({ |
| 124 | + code: 'CONNECT_UNSUPPORTED_PROTOCOL', |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('resolves a saved endpoint before falling back', async () => { |
| 129 | + saveSiteConnection('chatwise', 'http://127.0.0.1:9555'); |
| 130 | + mockProbeStatuses({ |
| 131 | + 'http://127.0.0.1:9555/json/version': 200, |
| 132 | + }); |
| 133 | + |
| 134 | + await expect(resolveLiveSiteEndpoint('chatwise')).resolves.toEqual({ |
| 135 | + endpoint: 'http://127.0.0.1:9555', |
| 136 | + source: 'saved', |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('falls back to the env endpoint when the saved one is unavailable', async () => { |
| 141 | + saveSiteConnection('chatwise', 'http://127.0.0.1:9555'); |
| 142 | + process.env.OPENCLI_CDP_ENDPOINT = 'http://127.0.0.1:9666/'; |
| 143 | + mockProbeStatuses({ |
| 144 | + 'http://127.0.0.1:9666/json/version': 200, |
| 145 | + }); |
| 146 | + |
| 147 | + await expect(resolveLiveSiteEndpoint('chatwise')).resolves.toEqual({ |
| 148 | + endpoint: 'http://127.0.0.1:9666', |
| 149 | + source: 'env', |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + it('falls back to the default site endpoint when no live saved or env endpoint exists', async () => { |
| 154 | + mockProbeStatuses({ |
| 155 | + 'http://127.0.0.1:9228/json/version': 200, |
| 156 | + }); |
| 157 | + |
| 158 | + await expect(resolveLiveSiteEndpoint('chatwise')).resolves.toEqual({ |
| 159 | + endpoint: 'http://127.0.0.1:9228', |
| 160 | + source: 'default', |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments