|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | +import { createContextFeedback } from "../src/context-registry/feedback.js"; |
| 4 | +import { submitContextFeedback } from "../src/context-registry/feedback-transport.js"; |
| 5 | + |
| 6 | +const feedback = createContextFeedback({ |
| 7 | + repository: "C:/work/project", |
| 8 | + entryId: "official/payments", |
| 9 | + source: "official", |
| 10 | + version: "2.0.0", |
| 11 | + revision: 1, |
| 12 | + target: "entry", |
| 13 | + label: "useful" |
| 14 | +}); |
| 15 | + |
| 16 | +test("feedback transport is offline by default and does not call fetch", async () => { |
| 17 | + let calls = 0; |
| 18 | + const result = await submitContextFeedback( |
| 19 | + feedback, |
| 20 | + { enabled: true, telemetry: false, network: false, useLocalQualitySignals: false }, |
| 21 | + { fetcher: async () => { calls += 1; throw new Error("must not call"); } } |
| 22 | + ); |
| 23 | + assert.equal(result.status, "disabled"); |
| 24 | + assert.equal(calls, 0); |
| 25 | +}); |
| 26 | + |
| 27 | +test("feedback transport sends only explicit safe metadata", async () => { |
| 28 | + let body = ""; |
| 29 | + const result = await submitContextFeedback( |
| 30 | + feedback, |
| 31 | + { enabled: true, telemetry: true, network: true, useLocalQualitySignals: false, endpoint: "https://feedback.example.test/v1" }, |
| 32 | + { |
| 33 | + fetcher: async (_input, init) => { |
| 34 | + body = String(init?.body); |
| 35 | + return new Response(null, { status: 204 }); |
| 36 | + } |
| 37 | + } |
| 38 | + ); |
| 39 | + assert.equal(result.status, "sent"); |
| 40 | + const payload = JSON.parse(body) as Record<string, unknown>; |
| 41 | + assert.equal(payload.entryId, "official/payments"); |
| 42 | + assert.equal("task" in payload, false); |
| 43 | + assert.equal("content" in payload, false); |
| 44 | + assert.equal("repository" in payload, false); |
| 45 | +}); |
| 46 | + |
| 47 | +test("network failure is returned without throwing", async () => { |
| 48 | + const result = await submitContextFeedback( |
| 49 | + feedback, |
| 50 | + { enabled: true, telemetry: true, network: true, useLocalQualitySignals: false, endpoint: "https://feedback.example.test/v1" }, |
| 51 | + { fetcher: async () => { throw new Error("offline"); } } |
| 52 | + ); |
| 53 | + assert.equal(result.status, "failed"); |
| 54 | + assert.match(result.error ?? "", /offline/); |
| 55 | +}); |
0 commit comments