|
| 1 | +import { waitForSettledDOM } from "@/utils/waitForSettledDOM"; |
| 2 | +import type { CDPClient, CDPSession } from "@/cdp"; |
| 3 | + |
| 4 | +jest.mock("@/cdp", () => ({ |
| 5 | + getCDPClient: jest.fn(), |
| 6 | + getOrCreateFrameContextManager: jest.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +jest.mock("@/debug/options", () => ({ |
| 10 | + getDebugOptions: jest.fn(() => ({ |
| 11 | + enabled: true, |
| 12 | + traceWait: true, |
| 13 | + })), |
| 14 | +})); |
| 15 | + |
| 16 | +const { getCDPClient, getOrCreateFrameContextManager } = jest.requireMock( |
| 17 | + "@/cdp" |
| 18 | +) as { |
| 19 | + getCDPClient: jest.Mock; |
| 20 | + getOrCreateFrameContextManager: jest.Mock; |
| 21 | +}; |
| 22 | + |
| 23 | +type EventHandler = (...args: unknown[]) => void; |
| 24 | + |
| 25 | +function createSessionWithEvents(): { |
| 26 | + session: CDPSession; |
| 27 | + emit: (event: string, payload: unknown) => void; |
| 28 | +} { |
| 29 | + const handlers = new Map<string, Set<EventHandler>>(); |
| 30 | + const session: CDPSession = { |
| 31 | + send: async <T = unknown>(): Promise<T> => ({} as T), |
| 32 | + on: <TPayload extends unknown[]>( |
| 33 | + event: string, |
| 34 | + handler: (...payload: TPayload) => void |
| 35 | + ) => { |
| 36 | + const eventHandler = handler as EventHandler; |
| 37 | + const existing = handlers.get(event); |
| 38 | + if (existing) { |
| 39 | + existing.add(eventHandler); |
| 40 | + } else { |
| 41 | + handlers.set(event, new Set([eventHandler])); |
| 42 | + } |
| 43 | + }, |
| 44 | + off: <TPayload extends unknown[]>( |
| 45 | + event: string, |
| 46 | + handler: (...payload: TPayload) => void |
| 47 | + ) => { |
| 48 | + handlers.get(event)?.delete(handler as EventHandler); |
| 49 | + }, |
| 50 | + detach: async () => undefined, |
| 51 | + id: "session-1", |
| 52 | + }; |
| 53 | + |
| 54 | + const emit = (event: string, payload: unknown): void => { |
| 55 | + handlers.get(event)?.forEach((handler) => { |
| 56 | + handler(payload); |
| 57 | + }); |
| 58 | + }; |
| 59 | + |
| 60 | + return { session, emit }; |
| 61 | +} |
| 62 | + |
| 63 | +describe("waitForSettledDOM diagnostics", () => { |
| 64 | + beforeEach(() => { |
| 65 | + jest.useFakeTimers(); |
| 66 | + jest.clearAllMocks(); |
| 67 | + }); |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + jest.useRealTimers(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("sanitizes and truncates stalled-request warning diagnostics", async () => { |
| 74 | + const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 75 | + const logSpy = jest.spyOn(console, "log").mockImplementation(() => {}); |
| 76 | + const { session, emit } = createSessionWithEvents(); |
| 77 | + const cdpClient: CDPClient = { |
| 78 | + rootSession: session, |
| 79 | + createSession: async () => session, |
| 80 | + acquireSession: async () => session, |
| 81 | + dispose: async () => undefined, |
| 82 | + }; |
| 83 | + getCDPClient.mockResolvedValue(cdpClient); |
| 84 | + getOrCreateFrameContextManager.mockReturnValue({ |
| 85 | + setDebug: jest.fn(), |
| 86 | + }); |
| 87 | + |
| 88 | + const page = { |
| 89 | + context: () => ({}), |
| 90 | + } as never; |
| 91 | + |
| 92 | + try { |
| 93 | + const waitPromise = waitForSettledDOM(page, 5_000); |
| 94 | + await Promise.resolve(); |
| 95 | + await Promise.resolve(); |
| 96 | + |
| 97 | + emit("Network.requestWillBeSent", { |
| 98 | + requestId: `req\u0000\n${"x".repeat(600)}`, |
| 99 | + type: "Document", |
| 100 | + request: { |
| 101 | + url: `https://example.com/path\u0000\n${"y".repeat(2_000)}`, |
| 102 | + }, |
| 103 | + }); |
| 104 | + |
| 105 | + await jest.advanceTimersByTimeAsync(3_100); |
| 106 | + await waitPromise; |
| 107 | + |
| 108 | + const warning = String(warnSpy.mock.calls[0]?.[0] ?? ""); |
| 109 | + expect(warning).toContain("[truncated"); |
| 110 | + expect(warning).not.toContain("\u0000"); |
| 111 | + expect(warning).not.toContain("\n"); |
| 112 | + expect(warning.length).toBeLessThan(900); |
| 113 | + } finally { |
| 114 | + warnSpy.mockRestore(); |
| 115 | + logSpy.mockRestore(); |
| 116 | + } |
| 117 | + }); |
| 118 | +}); |
0 commit comments