|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +// Mock the real spawner so the default `exec` path (detached spawn + unref) |
| 4 | +// is exercised without launching a real process. |
| 5 | +const spawnMock = vi.fn(); |
| 6 | +vi.mock('node:child_process', () => ({ |
| 7 | + spawn: (command: string, args: readonly string[], opts: unknown) => |
| 8 | + spawnMock(command, args, opts), |
| 9 | +})); |
| 10 | + |
| 11 | +import { openInBrowser } from './browser.js'; |
| 12 | +import { ApiError } from './errors.js'; |
| 13 | + |
| 14 | +describe('openInBrowser', () => { |
| 15 | + const url = 'https://portal.example.com/tests/t_123'; |
| 16 | + |
| 17 | + it('uses `open <url>` on darwin', () => { |
| 18 | + const calls: Array<{ command: string; args: readonly string[] }> = []; |
| 19 | + openInBrowser(url, { |
| 20 | + platform: 'darwin', |
| 21 | + exec: (command, args) => calls.push({ command, args }), |
| 22 | + }); |
| 23 | + expect(calls).toEqual([{ command: 'open', args: [url] }]); |
| 24 | + }); |
| 25 | + |
| 26 | + it('uses rundll32 FileProtocolHandler on win32', () => { |
| 27 | + const calls: Array<{ command: string; args: readonly string[] }> = []; |
| 28 | + openInBrowser(url, { |
| 29 | + platform: 'win32', |
| 30 | + exec: (command, args) => calls.push({ command, args }), |
| 31 | + }); |
| 32 | + expect(calls).toEqual([{ command: 'rundll32', args: ['url.dll,FileProtocolHandler', url] }]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('uses xdg-open on other platforms', () => { |
| 36 | + const calls: Array<{ command: string; args: readonly string[] }> = []; |
| 37 | + openInBrowser(url, { |
| 38 | + platform: 'linux', |
| 39 | + exec: (command, args) => calls.push({ command, args }), |
| 40 | + }); |
| 41 | + expect(calls).toEqual([{ command: 'xdg-open', args: [url] }]); |
| 42 | + }); |
| 43 | + |
| 44 | + it('refuses a non-http(s) URL with exit 5 before spawning', () => { |
| 45 | + const exec = vi.fn(); |
| 46 | + let error: unknown; |
| 47 | + try { |
| 48 | + openInBrowser('file:///etc/passwd', { platform: 'linux', exec }); |
| 49 | + } catch (err) { |
| 50 | + error = err; |
| 51 | + } |
| 52 | + expect(error).toBeInstanceOf(ApiError); |
| 53 | + expect((error as ApiError).exitCode).toBe(5); |
| 54 | + expect(exec).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('throws on a malformed URL', () => { |
| 58 | + const exec = vi.fn(); |
| 59 | + expect(() => openInBrowser('not a url', { exec })).toThrow(); |
| 60 | + expect(exec).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('default spawner', () => { |
| 64 | + beforeEach(() => { |
| 65 | + spawnMock.mockReset(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('spawns detached, ignores stdio, and unrefs the child', () => { |
| 69 | + const unref = vi.fn(); |
| 70 | + spawnMock.mockReturnValue({ unref, on: vi.fn() }); |
| 71 | + openInBrowser(url, { platform: 'darwin' }); |
| 72 | + expect(spawnMock).toHaveBeenCalledWith('open', [url], { detached: true, stdio: 'ignore' }); |
| 73 | + expect(unref).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | + |
| 76 | + it("handles the child's async 'error' (missing binary) with a stderr hint, not a crash", () => { |
| 77 | + // spawn() reports ENOENT asynchronously on the child; an unhandled |
| 78 | + // 'error' event would crash the CLI. The default spawner must register |
| 79 | + // a listener that degrades to the manual-open hint. |
| 80 | + const listeners = new Map<string, (err: Error) => void>(); |
| 81 | + spawnMock.mockReturnValue({ |
| 82 | + unref: vi.fn(), |
| 83 | + on: (event: string, listener: (err: Error) => void) => { |
| 84 | + listeners.set(event, listener); |
| 85 | + }, |
| 86 | + }); |
| 87 | + const stderrSpy = vi.spyOn(process.stderr, 'write').mockReturnValue(true); |
| 88 | + try { |
| 89 | + openInBrowser(url, { platform: 'linux' }); |
| 90 | + const onError = listeners.get('error'); |
| 91 | + expect(onError).toBeDefined(); |
| 92 | + // Firing the listener must not throw and must print the hint. |
| 93 | + expect(() => onError!(new Error('spawn xdg-open ENOENT'))).not.toThrow(); |
| 94 | + expect(String(stderrSpy.mock.calls.at(-1)?.[0])).toContain('could not launch a browser'); |
| 95 | + } finally { |
| 96 | + stderrSpy.mockRestore(); |
| 97 | + } |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments