|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { augmentProtocolTimeoutError, isProtocolTimeoutError } from "./protocolTimeoutErrorHint.js"; |
| 3 | + |
| 4 | +describe("augmentProtocolTimeoutError", () => { |
| 5 | + it("passes non-timeout errors through unchanged (same instance)", () => { |
| 6 | + const original = new Error("V8 heap exhausted"); |
| 7 | + const result = augmentProtocolTimeoutError(original, 300_000); |
| 8 | + expect(result).toBe(original); |
| 9 | + expect(result.message).toBe("V8 heap exhausted"); |
| 10 | + }); |
| 11 | + |
| 12 | + it("augments Runtime.callFunctionOn timed out with the effective timeout", () => { |
| 13 | + const original = new Error( |
| 14 | + "Runtime.callFunctionOn timed out. Increase the 'protocolTimeout' setting.", |
| 15 | + ); |
| 16 | + const result = augmentProtocolTimeoutError(original, 600_000); |
| 17 | + expect(result).not.toBe(original); |
| 18 | + expect(result.message).toContain(original.message); |
| 19 | + expect(result.message).toContain("HyperFrames effective protocolTimeout: 600000 ms"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("includes both env and CLI hints", () => { |
| 23 | + const original = new Error("Runtime.callFunctionOn timed out"); |
| 24 | + const result = augmentProtocolTimeoutError(original, 300_000); |
| 25 | + expect(result.message).toContain("PRODUCER_PUPPETEER_PROTOCOL_TIMEOUT_MS"); |
| 26 | + expect(result.message).toContain("--protocol-timeout"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("preserves err.cause on the augmented error", () => { |
| 30 | + const original = new Error("Runtime.callFunctionOn timed out"); |
| 31 | + const result = augmentProtocolTimeoutError(original, 300_000); |
| 32 | + expect((result as Error & { cause?: unknown }).cause).toBe(original); |
| 33 | + }); |
| 34 | + |
| 35 | + it("augments Target closed errors", () => { |
| 36 | + const original = new Error("Protocol error (Runtime.callFunctionOn): Target closed."); |
| 37 | + const result = augmentProtocolTimeoutError(original, 300_000); |
| 38 | + expect(result).not.toBe(original); |
| 39 | + expect(result.message).toContain("HyperFrames effective protocolTimeout"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("matches the protocolTimeout keyword case-insensitively", () => { |
| 43 | + const original = new Error("some upstream saying PROTOCOLTIMEOUT was hit"); |
| 44 | + const result = augmentProtocolTimeoutError(original, 300_000); |
| 45 | + expect(result).not.toBe(original); |
| 46 | + }); |
| 47 | + |
| 48 | + it("coerces non-Error thrown values into Error without augmenting", () => { |
| 49 | + const result = augmentProtocolTimeoutError("plain string failure", 300_000); |
| 50 | + expect(result).toBeInstanceOf(Error); |
| 51 | + expect(result.message).toBe("plain string failure"); |
| 52 | + // Not augmented: coerced string doesn't match the protocol-timeout regex. |
| 53 | + expect(result.message).not.toContain("HyperFrames effective protocolTimeout"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("mentions the field-signal shape reporters hit", () => { |
| 57 | + const original = new Error("Runtime.callFunctionOn timed out"); |
| 58 | + const result = augmentProtocolTimeoutError(original, 300_000); |
| 59 | + expect(result.message).toContain("ts=1784047847"); |
| 60 | + expect(result.message).toContain("FFmpeg-only encoding"); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe("isProtocolTimeoutError", () => { |
| 65 | + it("returns true for matching messages", () => { |
| 66 | + expect(isProtocolTimeoutError(new Error("Runtime.callFunctionOn timed out"))).toBe(true); |
| 67 | + expect(isProtocolTimeoutError(new Error("Target closed"))).toBe(true); |
| 68 | + expect(isProtocolTimeoutError("protocolTimeout exceeded")).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it("returns false for non-matching messages", () => { |
| 72 | + expect(isProtocolTimeoutError(new Error("V8 heap exhausted"))).toBe(false); |
| 73 | + expect(isProtocolTimeoutError(new Error("Navigation timeout of 60000 ms exceeded"))).toBe( |
| 74 | + false, |
| 75 | + ); |
| 76 | + expect(isProtocolTimeoutError(null)).toBe(false); |
| 77 | + expect(isProtocolTimeoutError(undefined)).toBe(false); |
| 78 | + expect(isProtocolTimeoutError(42)).toBe(false); |
| 79 | + }); |
| 80 | +}); |
0 commit comments