import type { Page } from 'playwright-core'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { createFakeNavigationFence, FAKE_MAIN_FRAME_ID } from './navigation-fence.test-support.js'; import { withPageNavigationRequestGuard } from './navigation.js'; afterEach(() => vi.restoreAllMocks()); const permissive = { dangerouslyAllowPrivateNetwork: true }; function fixture() { let url = 'http://93.184.216.34/start'; let closed = false; const fence = createFakeNavigationFence({ url: () => url }); const page = { url: () => url, context: () => ({ newCDPSession: fence.newCDPSession }), on: vi.fn(), off: vi.fn(), mainFrame: () => ({}), isClosed: () => closed, } as unknown as Page; const requested = (frameId = FAKE_MAIN_FRAME_ID, disposition = 'currentTab') => { fence.emit('Page.frameRequestedNavigation', { frameId, url: 'http://93.184.216.34/next', disposition }); }; return { page, fence, requested, setUrl: (value: string) => { url = value; }, close: () => { closed = true; }, }; } async function timedSettle( page: Page, during: () => void, after?: () => void, cdpUrl = 'http://localhost:39881', ): Promise { return await withPageNavigationRequestGuard({ cdpUrl, page, ssrfPolicy: permissive, action: async (_baseline, settle) => { during(); const started = Date.now(); const settling = settle(); after?.(); await settling; return Date.now() - started; }, }); } describe('interaction settle', () => { it('returns once the renderer barrier answers when no navigation was requested', async () => { const { page } = fixture(); expect(await timedSettle(page, () => undefined)).toBeLessThan(50); }); it('keeps waiting after Fetch.requestPaused until frameNavigated or the cap', async () => { const { page, fence, requested } = fixture(); let paused = false; const elapsed = await timedSettle( page, () => { requested(); }, () => { void fence.pauseDocument('http://93.184.216.34/next').then(() => { paused = true; setTimeout(() => { fence.emit('Page.frameNavigated', { frame: { id: FAKE_MAIN_FRAME_ID, url: 'http://93.184.216.34/next' } }); }, 60); }); }, ); expect(paused).toBe(true); expect(elapsed).toBeGreaterThanOrEqual(55); expect(elapsed).toBeLessThan(200); }); it('waits up to the cap for a requested navigation whose request has not paused yet', async () => { const { page, requested } = fixture(); const elapsed = await timedSettle(page, () => { requested(); }); expect(elapsed).toBeGreaterThanOrEqual(240); expect(elapsed).toBeLessThan(400); }); it('stops waiting when the requested navigation commits within the document', async () => { const { page, fence, requested } = fixture(); const elapsed = await timedSettle( page, () => { requested(); }, () => { setTimeout(() => { fence.emit('Page.navigatedWithinDocument', { frameId: FAKE_MAIN_FRAME_ID, url: 'http://93.184.216.34/#x' }); }, 20); }, ); expect(elapsed).toBeLessThan(150); }); it('ends the wait when the requested navigation stops without committing', async () => { const { page, fence, requested } = fixture(); const elapsed = await timedSettle( page, () => { requested(); }, () => { setTimeout(() => { fence.emit('Page.frameStoppedLoading', { frameId: FAKE_MAIN_FRAME_ID }); }, 20); }, ); expect(elapsed).toBeLessThan(150); }); it('ignores subframe and new-window requested navigations', async () => { const { page, requested } = fixture(); const elapsed = await timedSettle(page, () => { requested('child-frame'); requested(FAKE_MAIN_FRAME_ID, 'newWindow'); requested(FAKE_MAIN_FRAME_ID, 'newTab'); }); expect(elapsed).toBeLessThan(50); }); it('bounds a held barrier by the cap', async () => { const { page, fence } = fixture(); let open: (() => void) | undefined; const elapsed = await timedSettle( page, () => { open = fence.hold(['Runtime.evaluate']); }, () => { setTimeout(() => open?.(), 300); }, ); expect(elapsed).toBeGreaterThanOrEqual(240); expect(elapsed).toBeLessThan(300); }); it('does not let a requested navigation left over from an earlier action hold the next one', async () => { const { page, requested } = fixture(); await timedSettle(page, () => { requested(); }); expect(await timedSettle(page, () => undefined)).toBeLessThan(50); }); it("a second invocation's claim keeps the first invocation's requested navigation", async () => { const { page, fence, requested } = fixture(); const first = timedSettle(page, () => { requested(); }); await new Promise((resolve) => setTimeout(resolve, 20)); const second = withPageNavigationRequestGuard({ cdpUrl: 'http://localhost:39881', page, ssrfPolicy: permissive, action: () => Promise.resolve('second'), }); expect(await second).toBe('second'); setTimeout(() => { fence.emit('Page.frameNavigated', { frame: { id: FAKE_MAIN_FRAME_ID, url: 'http://93.184.216.34/next' } }); }, 80); const elapsed = await first; expect(elapsed).toBeGreaterThanOrEqual(90); expect(elapsed).toBeLessThan(240); }); it('resolves when the page closes during the action', async () => { const { page, fence, close } = fixture(); const result = await withPageNavigationRequestGuard({ cdpUrl: 'http://localhost:39882', page, ssrfPolicy: permissive, action: async (_baseline, settle) => { close(); fence.failNext( 'Runtime.evaluate', new Error('cdpSession.send: Target page, context or browser has been closed'), ); await settle(); return 'closed'; }, }); expect(result).toBe('closed'); }); it('warns and ends the settle when the barrier fails on a live page', async () => { const { page, fence } = fixture(); const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined); const elapsed = await timedSettle(page, () => { fence.failNext('Runtime.evaluate', new Error('renderer crashed')); }); expect(elapsed).toBeLessThan(50); expect(warn).toHaveBeenCalledWith(expect.stringContaining('could not settle the action: renderer crashed')); }); });