|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +const mockUnref = vi.fn(); |
| 4 | +const mockSpawn = vi.fn().mockReturnValue({ unref: mockUnref }); |
| 5 | + |
| 6 | +vi.mock('child_process', () => ({ |
| 7 | + spawn: (...args: unknown[]) => mockSpawn(...args), |
| 8 | +})); |
| 9 | + |
| 10 | +describe('openBrowser', () => { |
| 11 | + const originalPlatform = process.platform; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + mockSpawn.mockClear(); |
| 15 | + mockUnref.mockClear(); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + Object.defineProperty(process, 'platform', { value: originalPlatform }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('uses "open" on macOS', async () => { |
| 23 | + Object.defineProperty(process, 'platform', { value: 'darwin' }); |
| 24 | + const { openBrowser } = await import('../utils'); |
| 25 | + openBrowser('http://localhost:3000'); |
| 26 | + |
| 27 | + expect(mockSpawn).toHaveBeenCalledWith('open', ['http://localhost:3000'], { |
| 28 | + stdio: 'ignore', |
| 29 | + detached: true, |
| 30 | + }); |
| 31 | + expect(mockUnref).toHaveBeenCalled(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('uses "cmd /c start" on Windows to avoid ENOENT', async () => { |
| 35 | + Object.defineProperty(process, 'platform', { value: 'win32' }); |
| 36 | + const { openBrowser } = await import('../utils'); |
| 37 | + openBrowser('http://localhost:3000'); |
| 38 | + |
| 39 | + expect(mockSpawn).toHaveBeenCalledWith('cmd', ['/c', 'start', 'http://localhost:3000'], { |
| 40 | + stdio: 'ignore', |
| 41 | + detached: true, |
| 42 | + }); |
| 43 | + expect(mockUnref).toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('uses "xdg-open" on Linux', async () => { |
| 47 | + Object.defineProperty(process, 'platform', { value: 'linux' }); |
| 48 | + const { openBrowser } = await import('../utils'); |
| 49 | + openBrowser('http://localhost:3000'); |
| 50 | + |
| 51 | + expect(mockSpawn).toHaveBeenCalledWith('xdg-open', ['http://localhost:3000'], { |
| 52 | + stdio: 'ignore', |
| 53 | + detached: true, |
| 54 | + }); |
| 55 | + expect(mockUnref).toHaveBeenCalled(); |
| 56 | + }); |
| 57 | +}); |
0 commit comments