|
| 1 | +/** |
| 2 | + * Unit tests for PageManagement and isPrivilegedUrl |
| 3 | + * |
| 4 | + * The moz-extension:// fix: geckodriver's driver.get() hangs on extension URLs |
| 5 | + * because it waits for BiDi navigation completion events that the Remote Agent |
| 6 | + * never emits for extension contexts. The fix routes moz-extension:// URLs |
| 7 | + * through BiDi browsingContext.navigate with wait:"none", which returns |
| 8 | + * immediately without waiting for load events. |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, it, expect, vi } from 'vitest'; |
| 12 | +import { isPrivilegedUrl, PageManagement } from '@/firefox/pages.js'; |
| 13 | + |
| 14 | +// -- isPrivilegedUrl ---------------------------------------------------------- |
| 15 | + |
| 16 | +describe('isPrivilegedUrl', () => { |
| 17 | + it('returns true for moz-extension:// URLs', () => { |
| 18 | + expect(isPrivilegedUrl('moz-extension://abc123/popup.html')).toBe(true); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns false for https:// URLs', () => { |
| 22 | + expect(isPrivilegedUrl('https://example.com')).toBe(false); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns false for http:// URLs', () => { |
| 26 | + expect(isPrivilegedUrl('http://localhost:3000')).toBe(false); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns false for file:// URLs', () => { |
| 30 | + expect(isPrivilegedUrl('file:///path/to/page.html')).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns false for about: URLs (not in PRIVILEGED_URL_SCHEMES)', () => { |
| 34 | + expect(isPrivilegedUrl('about:blank')).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + // Malformed strings make URL constructor throw; the catch block returns false |
| 38 | + it('returns false for malformed URLs', () => { |
| 39 | + expect(isPrivilegedUrl('not-a-url')).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns false for empty string', () => { |
| 43 | + expect(isPrivilegedUrl('')).toBe(false); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +// -- PageManagement ----------------------------------------------------------- |
| 48 | + |
| 49 | +describe('PageManagement', () => { |
| 50 | + type BiDiCommandFn = (method: string, params: Record<string, any>) => Promise<any>; |
| 51 | + |
| 52 | + /** |
| 53 | + * Build mocked PageManagement dependencies. |
| 54 | + * |
| 55 | + * The contextId handling is subtle: null and undefined mean different things. |
| 56 | + * - contextId: null → explicitly null (no browsing context available) |
| 57 | + * - contextId omitted → defaults to 'ctx-1' (normal case) |
| 58 | + * |
| 59 | + * We use `'contextId' in overrides` to distinguish these, because |
| 60 | + * `null ?? 'ctx-1'` would incorrectly collapse null into the default. |
| 61 | + */ |
| 62 | + function createMocks(overrides?: { contextId?: string | null; sendBiDiCommand?: BiDiCommandFn }) { |
| 63 | + const driver = { get: vi.fn().mockResolvedValue(undefined) } as any; |
| 64 | + const hasContextId = 'contextId' in (overrides ?? {}); |
| 65 | + const contextId = hasContextId ? overrides!.contextId : 'ctx-1'; |
| 66 | + const getCurrentContextId = vi.fn().mockImplementation(() => contextId); |
| 67 | + const setCurrentContextId = vi.fn(); |
| 68 | + |
| 69 | + // When sendBiDiCommand is provided, wrap it in a mock so we can assert calls. |
| 70 | + // When omitted, it stays undefined — simulates BiDi being unavailable. |
| 71 | + const sendBiDiCommand = overrides?.sendBiDiCommand |
| 72 | + ? vi.fn().mockImplementation(overrides.sendBiDiCommand) |
| 73 | + : undefined; |
| 74 | + |
| 75 | + const pages = new PageManagement(driver, getCurrentContextId, setCurrentContextId, sendBiDiCommand as any); |
| 76 | + return { pages, driver, getCurrentContextId, setCurrentContextId, sendBiDiCommand: sendBiDiCommand as any }; |
| 77 | + } |
| 78 | + |
| 79 | + describe('navigate', () => { |
| 80 | + // Core fix: moz-extension:// must use BiDi, not driver.get() |
| 81 | + it('uses BiDi browsingContext.navigate with wait:none for moz-extension:// URLs', async () => { |
| 82 | + const bidiFn = vi.fn().mockResolvedValue({}); |
| 83 | + const { pages, driver, sendBiDiCommand } = createMocks({ sendBiDiCommand: bidiFn }); |
| 84 | + |
| 85 | + await pages.navigate('moz-extension://abc123/popup.html'); |
| 86 | + |
| 87 | + expect(sendBiDiCommand).toHaveBeenCalledWith('browsingContext.navigate', { |
| 88 | + context: 'ctx-1', |
| 89 | + url: 'moz-extension://abc123/popup.html', |
| 90 | + wait: 'none', |
| 91 | + }); |
| 92 | + expect(driver.get).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + // Guard: no context ID means we can't send a BiDi command. |
| 96 | + // Also verify sendBiDiCommand was never called (not just that it threw). |
| 97 | + it('throws when context ID is null for moz-extension:// URL', async () => { |
| 98 | + const bidiFn = vi.fn().mockResolvedValue({}); |
| 99 | + const { pages, sendBiDiCommand } = createMocks({ contextId: null, sendBiDiCommand: bidiFn }); |
| 100 | + |
| 101 | + await expect(pages.navigate('moz-extension://abc123/popup.html')).rejects.toThrow( |
| 102 | + 'Cannot navigate to privileged URL moz-extension://abc123/popup.html: no browsing context ID' |
| 103 | + ); |
| 104 | + expect(sendBiDiCommand).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + // Fallback: if BiDi is not available (e.g. no Remote Agent), we still try |
| 108 | + // driver.get(). This will hang for moz-extension://, but there's no alternative. |
| 109 | + it('falls through to driver.get() for moz-extension:// when BiDi is unavailable', async () => { |
| 110 | + const { pages, driver, sendBiDiCommand } = createMocks(); |
| 111 | + |
| 112 | + await pages.navigate('moz-extension://abc123/popup.html'); |
| 113 | + |
| 114 | + expect(driver.get).toHaveBeenCalledWith('moz-extension://abc123/popup.html'); |
| 115 | + expect(sendBiDiCommand).toBeUndefined(); |
| 116 | + }); |
| 117 | + |
| 118 | + // Non-privileged URLs always use the standard path |
| 119 | + it('uses driver.get() for normal URLs', async () => { |
| 120 | + const bidiFn = vi.fn().mockResolvedValue({}); |
| 121 | + const { pages, driver, sendBiDiCommand } = createMocks({ sendBiDiCommand: bidiFn }); |
| 122 | + |
| 123 | + await pages.navigate('https://example.com'); |
| 124 | + |
| 125 | + expect(driver.get).toHaveBeenCalledWith('https://example.com'); |
| 126 | + expect(sendBiDiCommand).not.toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + // If the BiDi command rejects, the error should propagate (no silent swallow) |
| 130 | + it('propagates BiDi navigation errors for moz-extension:// URLs', async () => { |
| 131 | + const bidiFn = vi.fn().mockRejectedValue(new Error('BiDi error: invalid context')); |
| 132 | + const { pages } = createMocks({ sendBiDiCommand: bidiFn }); |
| 133 | + |
| 134 | + await expect(pages.navigate('moz-extension://abc123/popup.html')).rejects.toThrow( |
| 135 | + 'BiDi error: invalid context' |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('createNewPage', () => { |
| 141 | + // createNewPage must delegate to navigate(), not call driver.get() directly, |
| 142 | + // so that the moz-extension:// BiDi path is also used for new tabs |
| 143 | + it('uses BiDi navigate for moz-extension:// URL after creating the tab', async () => { |
| 144 | + const bidiFn = vi.fn().mockResolvedValue({}); |
| 145 | + const allHandles = ['handle-1', 'handle-2']; |
| 146 | + const driver = { |
| 147 | + get: vi.fn().mockResolvedValue(undefined), |
| 148 | + switchTo: vi.fn().mockReturnValue({ newWindow: vi.fn().mockResolvedValue(undefined) }), |
| 149 | + getAllWindowHandles: vi.fn().mockResolvedValue(allHandles), |
| 150 | + } as any; |
| 151 | + const getCurrentContextId = vi.fn().mockReturnValue('handle-2'); |
| 152 | + const setCurrentContextId = vi.fn(); |
| 153 | + |
| 154 | + const pages = new PageManagement(driver, getCurrentContextId, setCurrentContextId, bidiFn as any); |
| 155 | + |
| 156 | + const idx = await pages.createNewPage('moz-extension://abc123/popup.html'); |
| 157 | + |
| 158 | + // Verify the BiDi navigate was used (not driver.get) |
| 159 | + expect(bidiFn).toHaveBeenCalledWith('browsingContext.navigate', { |
| 160 | + context: 'handle-2', |
| 161 | + url: 'moz-extension://abc123/popup.html', |
| 162 | + wait: 'none', |
| 163 | + }); |
| 164 | + // Verify context was set before navigation |
| 165 | + expect(setCurrentContextId).toHaveBeenCalledWith('handle-2'); |
| 166 | + expect(driver.get).not.toHaveBeenCalled(); |
| 167 | + expect(idx).toBe(1); |
| 168 | + }); |
| 169 | + |
| 170 | + // With a normal URL, createNewPage delegates to navigate() which uses driver.get() |
| 171 | + it('uses driver.get() for normal URLs after creating the tab', async () => { |
| 172 | + const bidiFn = vi.fn().mockResolvedValue({}); |
| 173 | + const allHandles = ['handle-1', 'handle-2']; |
| 174 | + const driver = { |
| 175 | + get: vi.fn().mockResolvedValue(undefined), |
| 176 | + switchTo: vi.fn().mockReturnValue({ newWindow: vi.fn().mockResolvedValue(undefined) }), |
| 177 | + getAllWindowHandles: vi.fn().mockResolvedValue(allHandles), |
| 178 | + } as any; |
| 179 | + const getCurrentContextId = vi.fn().mockReturnValue('handle-2'); |
| 180 | + const setCurrentContextId = vi.fn(); |
| 181 | + |
| 182 | + const pages = new PageManagement(driver, getCurrentContextId, setCurrentContextId, bidiFn as any); |
| 183 | + |
| 184 | + const idx = await pages.createNewPage('https://example.com'); |
| 185 | + |
| 186 | + expect(driver.get).toHaveBeenCalledWith('https://example.com'); |
| 187 | + expect(bidiFn).not.toHaveBeenCalled(); |
| 188 | + expect(idx).toBe(1); |
| 189 | + }); |
| 190 | + }); |
| 191 | +}); |
0 commit comments