|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import consola from "consola"; |
| 3 | + |
| 4 | +vi.mock("consola", () => import("@repo/test-utils/mock-consola")); |
| 5 | + |
| 6 | +vi.mock("undici", () => { |
| 7 | + const MockAgent = vi.fn(); |
| 8 | + MockAgent.prototype.compose = vi.fn(function (this: unknown) { |
| 9 | + return this; |
| 10 | + }); |
| 11 | + return { |
| 12 | + Agent: MockAgent, |
| 13 | + setGlobalDispatcher: vi.fn(), |
| 14 | + }; |
| 15 | +}); |
| 16 | + |
| 17 | +const newHandler = () => ({ |
| 18 | + onRequestStart: vi.fn(), |
| 19 | + onResponseStart: vi.fn(), |
| 20 | + onResponseData: vi.fn(), |
| 21 | + onResponseEnd: vi.fn(), |
| 22 | + onResponseError: vi.fn(), |
| 23 | + onRequestUpgrade: vi.fn(), |
| 24 | +}); |
| 25 | + |
| 26 | +describe("createLoggingInterceptor", () => { |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks(); |
| 29 | + vi.restoreAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("returns a function", async () => { |
| 33 | + const { createLoggingInterceptor } = await import("./http-logger"); |
| 34 | + const interceptor = createLoggingInterceptor(); |
| 35 | + expect(typeof interceptor).toBe("function"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("logs API host on the first request only", async () => { |
| 39 | + const { createLoggingInterceptor } = await import("./http-logger"); |
| 40 | + const interceptor = createLoggingInterceptor(); |
| 41 | + |
| 42 | + const mockDispatch = vi.fn().mockReturnValue(true); |
| 43 | + const wrappedDispatch = interceptor(mockDispatch); |
| 44 | + |
| 45 | + const opts1 = { method: "GET", origin: "https://example.backlog.com", path: "/api/v2/space" }; |
| 46 | + const opts2 = { method: "GET", origin: "https://example.backlog.com", path: "/api/v2/users" }; |
| 47 | + |
| 48 | + wrappedDispatch(opts1 as never, newHandler() as never); |
| 49 | + wrappedDispatch(opts2 as never, newHandler() as never); |
| 50 | + |
| 51 | + const hostCalls = vi |
| 52 | + .mocked(consola.debug) |
| 53 | + .mock.calls.filter((call) => typeof call[0] === "string" && call[0].startsWith("API host:")); |
| 54 | + expect(hostCalls).toHaveLength(1); |
| 55 | + expect(hostCalls[0]![0]).toBe("API host: https://example.backlog.com"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("logs request start with method and path", async () => { |
| 59 | + const { createLoggingInterceptor } = await import("./http-logger"); |
| 60 | + const interceptor = createLoggingInterceptor(); |
| 61 | + |
| 62 | + const mockDispatch = vi.fn().mockReturnValue(true); |
| 63 | + const wrappedDispatch = interceptor(mockDispatch); |
| 64 | + |
| 65 | + const opts = { method: "GET", origin: "https://example.backlog.com", path: "/api/v2/issues" }; |
| 66 | + wrappedDispatch(opts as never, newHandler() as never); |
| 67 | + |
| 68 | + expect(consola.debug).toHaveBeenCalledWith("→ GET /api/v2/issues"); |
| 69 | + }); |
| 70 | + |
| 71 | + it("logs response with status code and duration", async () => { |
| 72 | + const { createLoggingInterceptor } = await import("./http-logger"); |
| 73 | + const interceptor = createLoggingInterceptor(); |
| 74 | + |
| 75 | + const mockDispatch = vi.fn().mockImplementation((_opts, wrappedHandler) => { |
| 76 | + wrappedHandler.onResponseStart({}, 200, {}, "OK"); |
| 77 | + return true; |
| 78 | + }); |
| 79 | + |
| 80 | + const wrappedDispatch = interceptor(mockDispatch); |
| 81 | + const opts = { method: "POST", origin: "https://example.backlog.com", path: "/api/v2/issues" }; |
| 82 | + const handler = newHandler(); |
| 83 | + |
| 84 | + wrappedDispatch(opts as never, handler as never); |
| 85 | + |
| 86 | + expect(consola.debug).toHaveBeenCalledWith( |
| 87 | + expect.stringMatching(/← 200 POST \/api\/v2\/issues \(\d+ms\)/), |
| 88 | + ); |
| 89 | + expect(handler.onResponseStart).toHaveBeenCalledWith({}, 200, {}, "OK"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("logs error with duration", async () => { |
| 93 | + const { createLoggingInterceptor } = await import("./http-logger"); |
| 94 | + const interceptor = createLoggingInterceptor(); |
| 95 | + |
| 96 | + const testError = new Error("connection refused"); |
| 97 | + const mockDispatch = vi.fn().mockImplementation((_opts, wrappedHandler) => { |
| 98 | + wrappedHandler.onResponseError({}, testError); |
| 99 | + return true; |
| 100 | + }); |
| 101 | + |
| 102 | + const wrappedDispatch = interceptor(mockDispatch); |
| 103 | + const opts = { |
| 104 | + method: "DELETE", |
| 105 | + origin: "https://example.backlog.com", |
| 106 | + path: "/api/v2/issues/1", |
| 107 | + }; |
| 108 | + const handler = newHandler(); |
| 109 | + |
| 110 | + wrappedDispatch(opts as never, handler as never); |
| 111 | + |
| 112 | + expect(consola.debug).toHaveBeenCalledWith( |
| 113 | + expect.stringMatching(/✗ DELETE \/api\/v2\/issues\/1 \(\d+ms\)/), |
| 114 | + ); |
| 115 | + expect(handler.onResponseError).toHaveBeenCalledWith({}, testError); |
| 116 | + }); |
| 117 | + |
| 118 | + it("masks apiKey and decodes percent-encoded query parameters", async () => { |
| 119 | + const { createLoggingInterceptor } = await import("./http-logger"); |
| 120 | + const interceptor = createLoggingInterceptor(); |
| 121 | + |
| 122 | + const mockDispatch = vi.fn().mockReturnValue(true); |
| 123 | + const wrappedDispatch = interceptor(mockDispatch); |
| 124 | + |
| 125 | + const opts = { |
| 126 | + method: "GET", |
| 127 | + origin: "https://example.backlog.com", |
| 128 | + path: "/api/v2/issues?apiKey=secret123&projectId%5B%5D=1", |
| 129 | + }; |
| 130 | + wrappedDispatch(opts as never, newHandler() as never); |
| 131 | + |
| 132 | + expect(consola.debug).toHaveBeenCalledWith("→ GET /api/v2/issues?apiKey=***&projectId[]=1"); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +describe("installHttpLogger", () => { |
| 137 | + beforeEach(() => { |
| 138 | + vi.clearAllMocks(); |
| 139 | + }); |
| 140 | + |
| 141 | + it("calls setGlobalDispatcher with a composed agent", async () => { |
| 142 | + const { Agent, setGlobalDispatcher } = await import("undici"); |
| 143 | + const { installHttpLogger } = await import("./http-logger"); |
| 144 | + |
| 145 | + installHttpLogger(); |
| 146 | + |
| 147 | + expect(Agent).toHaveBeenCalled(); |
| 148 | + expect(vi.mocked(Agent).prototype.compose).toHaveBeenCalled(); |
| 149 | + expect(setGlobalDispatcher).toHaveBeenCalled(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments