|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + isNavigationTimeoutError, |
| 4 | + navigateForCapture, |
| 5 | + networkIdleAttemptTimeoutMs, |
| 6 | + NETWORK_IDLE_ATTEMPT_MS, |
| 7 | +} from "./navigateForCapture.js"; |
| 8 | + |
| 9 | +describe("networkIdleAttemptTimeoutMs", () => { |
| 10 | + it("caps at the network-idle attempt budget", () => { |
| 11 | + expect(networkIdleAttemptTimeoutMs(120_000)).toBe(NETWORK_IDLE_ATTEMPT_MS); |
| 12 | + }); |
| 13 | + |
| 14 | + it("honors a caller timeout below the attempt budget", () => { |
| 15 | + expect(networkIdleAttemptTimeoutMs(10_000)).toBe(10_000); |
| 16 | + }); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("isNavigationTimeoutError", () => { |
| 20 | + it("matches Puppeteer navigation timeouts", () => { |
| 21 | + expect(isNavigationTimeoutError(new Error("Navigation timeout of 30000 ms exceeded"))).toBe( |
| 22 | + true, |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it("ignores unrelated failures", () => { |
| 27 | + expect(isNavigationTimeoutError(new Error("net::ERR_NAME_NOT_RESOLVED"))).toBe(false); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("navigateForCapture", () => { |
| 32 | + it("returns after networkidle2 when the page settles", async () => { |
| 33 | + const response = { status: () => 200 }; |
| 34 | + const goto = vi.fn().mockResolvedValue(response); |
| 35 | + const result = await navigateForCapture({ goto }, "https://example.com", 120_000); |
| 36 | + |
| 37 | + expect(result).toEqual({ |
| 38 | + response, |
| 39 | + waitUntil: "networkidle2", |
| 40 | + networkIdleTimeoutMs: NETWORK_IDLE_ATTEMPT_MS, |
| 41 | + fellBackFromNetworkIdle: false, |
| 42 | + }); |
| 43 | + expect(goto).toHaveBeenCalledTimes(1); |
| 44 | + expect(goto).toHaveBeenCalledWith("https://example.com", { |
| 45 | + waitUntil: "networkidle2", |
| 46 | + timeout: NETWORK_IDLE_ATTEMPT_MS, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("falls back to domcontentloaded after a networkidle2 navigation timeout", async () => { |
| 51 | + const response = { status: () => 200 }; |
| 52 | + const goto = vi |
| 53 | + .fn() |
| 54 | + .mockRejectedValueOnce(new Error("Navigation timeout of 30000 ms exceeded")) |
| 55 | + .mockResolvedValueOnce(response); |
| 56 | + |
| 57 | + const result = await navigateForCapture({ goto }, "https://www.yahoo.com/", 120_000); |
| 58 | + |
| 59 | + expect(result).toEqual({ |
| 60 | + response, |
| 61 | + waitUntil: "domcontentloaded", |
| 62 | + networkIdleTimeoutMs: NETWORK_IDLE_ATTEMPT_MS, |
| 63 | + fellBackFromNetworkIdle: true, |
| 64 | + }); |
| 65 | + expect(goto).toHaveBeenNthCalledWith(1, "https://www.yahoo.com/", { |
| 66 | + waitUntil: "networkidle2", |
| 67 | + timeout: NETWORK_IDLE_ATTEMPT_MS, |
| 68 | + }); |
| 69 | + expect(goto).toHaveBeenNthCalledWith(2, "https://www.yahoo.com/", { |
| 70 | + waitUntil: "domcontentloaded", |
| 71 | + timeout: 90_000, |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it.each([ |
| 76 | + { totalTimeoutMs: 31_000, fallbackTimeoutMs: 1_000 }, |
| 77 | + { totalTimeoutMs: 45_000, fallbackTimeoutMs: 15_000 }, |
| 78 | + ])( |
| 79 | + "uses only the remaining budget for fallback when totalTimeoutMs=$totalTimeoutMs", |
| 80 | + async ({ totalTimeoutMs, fallbackTimeoutMs }) => { |
| 81 | + const response = { status: () => 200 }; |
| 82 | + const goto = vi |
| 83 | + .fn() |
| 84 | + .mockRejectedValueOnce(new Error("Navigation timeout of 30000 ms exceeded")) |
| 85 | + .mockResolvedValueOnce(response); |
| 86 | + |
| 87 | + await navigateForCapture({ goto }, "https://www.yahoo.com/", totalTimeoutMs); |
| 88 | + |
| 89 | + expect(goto).toHaveBeenNthCalledWith(1, "https://www.yahoo.com/", { |
| 90 | + waitUntil: "networkidle2", |
| 91 | + timeout: NETWORK_IDLE_ATTEMPT_MS, |
| 92 | + }); |
| 93 | + expect(goto).toHaveBeenNthCalledWith(2, "https://www.yahoo.com/", { |
| 94 | + waitUntil: "domcontentloaded", |
| 95 | + timeout: fallbackTimeoutMs, |
| 96 | + }); |
| 97 | + }, |
| 98 | + ); |
| 99 | + |
| 100 | + it("does not fall back for non-timeout navigation errors", async () => { |
| 101 | + const err = new Error("net::ERR_CONNECTION_REFUSED"); |
| 102 | + const goto = vi.fn().mockRejectedValue(err); |
| 103 | + |
| 104 | + await expect(navigateForCapture({ goto }, "https://example.com", 120_000)).rejects.toBe(err); |
| 105 | + expect(goto).toHaveBeenCalledTimes(1); |
| 106 | + }); |
| 107 | + |
| 108 | + it("does not fall back when the caller timeout already equals the idle attempt", async () => { |
| 109 | + const err = new Error("Navigation timeout of 10000 ms exceeded"); |
| 110 | + const goto = vi.fn().mockRejectedValue(err); |
| 111 | + |
| 112 | + await expect(navigateForCapture({ goto }, "https://example.com", 10_000)).rejects.toBe(err); |
| 113 | + expect(goto).toHaveBeenCalledWith("https://example.com", { |
| 114 | + waitUntil: "networkidle2", |
| 115 | + timeout: 10_000, |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments