|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("./logger", () => ({ |
| 4 | + networkLog: { |
| 5 | + info: vi.fn(), |
| 6 | + warn: vi.fn(), |
| 7 | + error: vi.fn(), |
| 8 | + }, |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("./network-log", async (importOriginal) => { |
| 12 | + const actual = await importOriginal<typeof import("./network-log")>(); |
| 13 | + return { |
| 14 | + ...actual, |
| 15 | + recordNetworkRequest: vi.fn(), |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +import { createNetworkLoggingFetch } from "./network-fetch-logger"; |
| 20 | +import { recordNetworkRequest } from "./network-log"; |
| 21 | + |
| 22 | +const mockedRecord = vi.mocked(recordNetworkRequest); |
| 23 | + |
| 24 | +function fakeResponse( |
| 25 | + status = 200, |
| 26 | + headers: Record<string, string> = { "content-length": "1834" }, |
| 27 | +): Response { |
| 28 | + return new Response(null, { status, headers }); |
| 29 | +} |
| 30 | + |
| 31 | +beforeEach(() => { |
| 32 | + vi.clearAllMocks(); |
| 33 | +}); |
| 34 | + |
| 35 | +describe("createNetworkLoggingFetch", () => { |
| 36 | + it("records a successful request with status, duration and bytes", async () => { |
| 37 | + const original = vi.fn(async () => fakeResponse()); |
| 38 | + const wrapped = createNetworkLoggingFetch( |
| 39 | + original as unknown as typeof fetch, |
| 40 | + ); |
| 41 | + |
| 42 | + const response = await wrapped("https://us.posthog.com/api/", { |
| 43 | + method: "post", |
| 44 | + }); |
| 45 | + |
| 46 | + expect(response.status).toBe(200); |
| 47 | + expect(original).toHaveBeenCalledWith("https://us.posthog.com/api/", { |
| 48 | + method: "post", |
| 49 | + }); |
| 50 | + expect(mockedRecord).toHaveBeenCalledWith({ |
| 51 | + origin: "main", |
| 52 | + method: "POST", |
| 53 | + url: "https://us.posthog.com/api/", |
| 54 | + status: 200, |
| 55 | + durationMs: expect.any(Number), |
| 56 | + bytes: 1834, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it.each([ |
| 61 | + ["string", "https://example.com/a", "https://example.com/a", "GET"], |
| 62 | + ["URL", new URL("https://example.com/b"), "https://example.com/b", "GET"], |
| 63 | + [ |
| 64 | + "Request", |
| 65 | + new Request("https://example.com/c", { method: "PUT" }), |
| 66 | + "https://example.com/c", |
| 67 | + "PUT", |
| 68 | + ], |
| 69 | + ])( |
| 70 | + "extracts url and method from %s input", |
| 71 | + async (_kind, input, url, method) => { |
| 72 | + const original = vi.fn(async () => fakeResponse()); |
| 73 | + const wrapped = createNetworkLoggingFetch( |
| 74 | + original as unknown as typeof fetch, |
| 75 | + ); |
| 76 | + |
| 77 | + await wrapped(input); |
| 78 | + |
| 79 | + expect(mockedRecord).toHaveBeenCalledWith( |
| 80 | + expect.objectContaining({ url, method }), |
| 81 | + ); |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + it("records null bytes when content-length is missing", async () => { |
| 86 | + const original = vi.fn(async () => fakeResponse(204, {})); |
| 87 | + const wrapped = createNetworkLoggingFetch( |
| 88 | + original as unknown as typeof fetch, |
| 89 | + ); |
| 90 | + |
| 91 | + await wrapped("https://example.com/"); |
| 92 | + |
| 93 | + expect(mockedRecord).toHaveBeenCalledWith( |
| 94 | + expect.objectContaining({ status: 204, bytes: null }), |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it("records and rethrows async rejections", async () => { |
| 99 | + const original = vi.fn(async () => { |
| 100 | + throw new TypeError("fetch failed"); |
| 101 | + }); |
| 102 | + const wrapped = createNetworkLoggingFetch( |
| 103 | + original as unknown as typeof fetch, |
| 104 | + ); |
| 105 | + |
| 106 | + await expect(wrapped("https://example.com/")).rejects.toThrow( |
| 107 | + "fetch failed", |
| 108 | + ); |
| 109 | + expect(mockedRecord).toHaveBeenCalledWith( |
| 110 | + expect.objectContaining({ |
| 111 | + status: null, |
| 112 | + bytes: null, |
| 113 | + error: "TypeError: fetch failed", |
| 114 | + }), |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it("records and rethrows synchronous throws", async () => { |
| 119 | + const original = vi.fn(() => { |
| 120 | + throw new Error("boom"); |
| 121 | + }); |
| 122 | + const wrapped = createNetworkLoggingFetch( |
| 123 | + original as unknown as typeof fetch, |
| 124 | + ); |
| 125 | + |
| 126 | + await expect(wrapped("https://example.com/")).rejects.toThrow("boom"); |
| 127 | + expect(mockedRecord).toHaveBeenCalledWith( |
| 128 | + expect.objectContaining({ error: "Error: boom" }), |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it("forwards preconnect from the original fetch", () => { |
| 133 | + const preconnect = vi.fn(); |
| 134 | + const original = Object.assign( |
| 135 | + vi.fn(async () => fakeResponse()), |
| 136 | + { |
| 137 | + preconnect, |
| 138 | + }, |
| 139 | + ); |
| 140 | + const wrapped = createNetworkLoggingFetch( |
| 141 | + original as unknown as typeof fetch, |
| 142 | + ); |
| 143 | + |
| 144 | + (wrapped as unknown as { preconnect: (origin: string) => void }).preconnect( |
| 145 | + "https://example.com", |
| 146 | + ); |
| 147 | + |
| 148 | + expect(preconnect).toHaveBeenCalledWith("https://example.com"); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +describe("installMainFetchLogging", () => { |
| 153 | + afterEach(() => { |
| 154 | + vi.unstubAllGlobals(); |
| 155 | + vi.resetModules(); |
| 156 | + }); |
| 157 | + |
| 158 | + it("wraps globalThis.fetch once and stays idempotent", async () => { |
| 159 | + const original = vi.fn(async () => fakeResponse()); |
| 160 | + vi.stubGlobal("fetch", original); |
| 161 | + |
| 162 | + vi.resetModules(); |
| 163 | + const { installMainFetchLogging } = await import("./network-fetch-logger"); |
| 164 | + |
| 165 | + installMainFetchLogging(); |
| 166 | + const wrappedOnce = globalThis.fetch; |
| 167 | + expect(wrappedOnce).not.toBe(original); |
| 168 | + |
| 169 | + installMainFetchLogging(); |
| 170 | + expect(globalThis.fetch).toBe(wrappedOnce); |
| 171 | + |
| 172 | + await globalThis.fetch("https://example.com/"); |
| 173 | + expect(original).toHaveBeenCalledOnce(); |
| 174 | + }); |
| 175 | +}); |
0 commit comments