|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { reportNewPrompt } from "../common/telemetry"; |
| 4 | + |
| 5 | +test("reportNewPrompt does not call fetch when enabled is false", () => { |
| 6 | + let called = false; |
| 7 | + const originalFetch = globalThis.fetch; |
| 8 | + globalThis.fetch = ((..._args: unknown[]) => { |
| 9 | + called = true; |
| 10 | + return Promise.resolve(new Response()); |
| 11 | + }) as typeof globalThis.fetch; |
| 12 | + |
| 13 | + try { |
| 14 | + reportNewPrompt({ enabled: false, machineId: "test-machine" }); |
| 15 | + assert.equal(called, false); |
| 16 | + } finally { |
| 17 | + globalThis.fetch = originalFetch; |
| 18 | + } |
| 19 | +}); |
| 20 | + |
| 21 | +test("reportNewPrompt does not call fetch when machineId is undefined", () => { |
| 22 | + let called = false; |
| 23 | + const originalFetch = globalThis.fetch; |
| 24 | + globalThis.fetch = ((..._args: unknown[]) => { |
| 25 | + called = true; |
| 26 | + return Promise.resolve(new Response()); |
| 27 | + }) as typeof globalThis.fetch; |
| 28 | + |
| 29 | + try { |
| 30 | + reportNewPrompt({ enabled: true }); |
| 31 | + assert.equal(called, false); |
| 32 | + } finally { |
| 33 | + globalThis.fetch = originalFetch; |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +test("reportNewPrompt does not call fetch when machineId is empty string", () => { |
| 38 | + let called = false; |
| 39 | + const originalFetch = globalThis.fetch; |
| 40 | + globalThis.fetch = ((..._args: unknown[]) => { |
| 41 | + called = true; |
| 42 | + return Promise.resolve(new Response()); |
| 43 | + }) as typeof globalThis.fetch; |
| 44 | + |
| 45 | + try { |
| 46 | + reportNewPrompt({ enabled: true, machineId: "" }); |
| 47 | + assert.equal(called, false); |
| 48 | + } finally { |
| 49 | + globalThis.fetch = originalFetch; |
| 50 | + } |
| 51 | +}); |
| 52 | + |
| 53 | +test("reportNewPrompt calls fetch with correct URL, method, headers, and body", async () => { |
| 54 | + const calls: Array<{ url: string; init: RequestInit }> = []; |
| 55 | + const originalFetch = globalThis.fetch; |
| 56 | + globalThis.fetch = ((url: string | URL | Request, init?: RequestInit) => { |
| 57 | + calls.push({ url: String(url), init: init ?? {} }); |
| 58 | + return Promise.resolve(new Response()); |
| 59 | + }) as typeof globalThis.fetch; |
| 60 | + |
| 61 | + try { |
| 62 | + reportNewPrompt({ enabled: true, machineId: "test-machine" }); |
| 63 | + |
| 64 | + // Wait for the fire-and-forget fetch to settle. |
| 65 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 66 | + |
| 67 | + assert.equal(calls.length, 1); |
| 68 | + assert.equal(calls[0].url, "https://deepcode.vegamo.cn/api/plugin/new"); |
| 69 | + assert.equal(calls[0].init.method, "POST"); |
| 70 | + assert.equal((calls[0].init.headers as Record<string, string>)["Content-Type"], "application/json"); |
| 71 | + assert.equal((calls[0].init.headers as Record<string, string>)["Token"], "test-machine"); |
| 72 | + assert.equal(calls[0].init.body, JSON.stringify({})); |
| 73 | + } finally { |
| 74 | + globalThis.fetch = originalFetch; |
| 75 | + } |
| 76 | +}); |
| 77 | + |
| 78 | +test("reportNewPrompt swallows fetch errors without throwing", async () => { |
| 79 | + const originalFetch = globalThis.fetch; |
| 80 | + globalThis.fetch = (() => { |
| 81 | + return Promise.reject(new Error("Network error")); |
| 82 | + }) as typeof globalThis.fetch; |
| 83 | + |
| 84 | + try { |
| 85 | + // Should not throw. |
| 86 | + reportNewPrompt({ enabled: true, machineId: "test-machine" }); |
| 87 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 88 | + } finally { |
| 89 | + globalThis.fetch = originalFetch; |
| 90 | + } |
| 91 | +}); |
| 92 | + |
| 93 | +test("reportNewPrompt respects custom timeoutMs", async () => { |
| 94 | + const calls: Array<{ signal: AbortSignal }> = []; |
| 95 | + const originalFetch = globalThis.fetch; |
| 96 | + globalThis.fetch = ((_url: string | URL | Request, init?: RequestInit) => { |
| 97 | + calls.push({ signal: init?.signal as AbortSignal }); |
| 98 | + return Promise.resolve(new Response()); |
| 99 | + }) as typeof globalThis.fetch; |
| 100 | + |
| 101 | + try { |
| 102 | + reportNewPrompt({ enabled: true, machineId: "test-machine", timeoutMs: 100 }); |
| 103 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 104 | + assert.equal(calls.length, 1); |
| 105 | + assert.equal(calls[0].signal.aborted, false); |
| 106 | + } finally { |
| 107 | + globalThis.fetch = originalFetch; |
| 108 | + } |
| 109 | +}); |
0 commit comments