import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const getPublishApiBaseUrlMock = vi.hoisted(() => vi.fn(() => "https://api.example.com")); vi.mock("./publishProject.js", () => ({ getPublishApiBaseUrl: getPublishApiBaseUrlMock, })); import { submitCatalogSearchMiss, submitFeedback } from "./submitFeedback.js"; describe("submitFeedback", () => { beforeEach(() => { getPublishApiBaseUrlMock.mockReturnValue("https://api.example.com"); }); afterEach(() => { vi.clearAllMocks(); vi.unstubAllGlobals(); }); it("leaves the rating and its scale out of a report that has none", async () => { const fetchMock = vi.fn(async () => new Response(null, { status: 202 })); vi.stubGlobal("fetch", fetchMock); await submitFeedback({ comment: "MISSING FEATURE: trim", cliVersion: "1.2.3" }); const body = JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body)); expect(body).not.toHaveProperty("rating"); expect(body).not.toHaveProperty("rating_scale"); expect(body.comment).toBe("MISSING FEATURE: trim"); }); it("posts feedback to the backend endpoint", async () => { const fetchMock = vi.fn(async () => new Response(null, { status: 202 })); vi.stubGlobal("fetch", fetchMock); await submitFeedback({ rating: 4, comment: "fast but font missing", cliVersion: "1.2.3", env: "os=darwin/arm64 node=v22.11.0", }); expect(fetchMock).toHaveBeenCalledOnce(); expect(getPublishApiBaseUrlMock).toHaveBeenCalledOnce(); expect(fetchMock).toHaveBeenCalledWith( "https://api.example.com/v1/hyperframes/feedback", expect.objectContaining({ method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ rating: 4, rating_scale: 10, comment: "fast but font missing", cli_version: "1.2.3", env: "os=darwin/arm64 node=v22.11.0", }), signal: expect.any(AbortSignal), }), ); }); it("sends who wrote it and the attached email, the email capped like the backend's", async () => { const fetchMock = vi.fn(async () => new Response(null, { status: 202 })); vi.stubGlobal("fetch", fetchMock); await submitFeedback({ rating: 5, cliVersion: "1", source: "person", email: "e".repeat(300) }); const body = JSON.parse(fetchMock.mock.calls[0]?.[1]?.body as string); expect(body.source).toBe("person"); expect(body.email).toHaveLength(254); }); it.each([0, 10])("serializes the NPS boundary %i without changing it", async (rating) => { const fetchMock = vi.fn(async () => new Response(null, { status: 202 })); vi.stubGlobal("fetch", fetchMock); await submitFeedback({ rating, cliVersion: "1.2.3" }); const requestInit = fetchMock.mock.calls[0]?.[1]; const body = JSON.parse(requestInit?.body as string); expect(body).toMatchObject({ rating, rating_scale: 10 }); }); it("truncates over-long fields to the backend caps", async () => { const fetchMock = vi.fn(async () => new Response(null, { status: 202 })); vi.stubGlobal("fetch", fetchMock); await submitFeedback({ rating: 3, comment: "x".repeat(2500), cliVersion: "v".repeat(200), env: "e".repeat(600), }); const requestInit = fetchMock.mock.calls[0]?.[1]; expect(requestInit).toBeDefined(); const body = JSON.parse(requestInit?.body as string); expect(body.comment).toHaveLength(2000); expect(body.cli_version).toHaveLength(100); expect(body.env).toHaveLength(500); }); it("does not reject when fetch rejects", async () => { const fetchMock = vi.fn().mockRejectedValueOnce(new TypeError("fetch failed")); vi.stubGlobal("fetch", fetchMock); await expect( submitFeedback({ rating: 1, cliVersion: "1.2.3", env: "os=linux" }), ).resolves.toBeUndefined(); }); it("always resolves regardless of fetch outcome", async () => { const fetchMock = vi .fn() .mockResolvedValueOnce(new Response(null, { status: 500 })) .mockRejectedValueOnce(new Error("offline")); vi.stubGlobal("fetch", fetchMock); await expect(submitFeedback({ rating: 2, cliVersion: "1.2.3" })).resolves.toBeUndefined(); await expect(submitFeedback({ rating: 3, cliVersion: "1.2.3" })).resolves.toBeUndefined(); }); }); describe("submitCatalogSearchMiss", () => { beforeEach(() => { getPublishApiBaseUrlMock.mockReturnValue("https://api.example.com"); }); afterEach(() => { vi.clearAllMocks(); vi.unstubAllGlobals(); }); it("posts the gap to the catalog endpoint", async () => { const fetchMock = vi.fn(async () => new Response(null, { status: 202 })); vi.stubGlobal("fetch", fetchMock); await submitCatalogSearchMiss({ query: "typewriter that deletes", wanted: "text that types then backspaces", tier: "on-device", cliVersion: "0.7.106", }); expect(fetchMock).toHaveBeenCalledOnce(); const [url, init] = fetchMock.mock.calls[0]!; expect(url).toBe("https://api.example.com/v1/hyperframes/catalog_search_miss"); expect(JSON.parse(String(init?.body))).toEqual({ query: "typewriter that deletes", wanted: "text that types then backspaces", tier: "on-device", cli_version: "0.7.106", }); }); it("truncates a field the backend would reject outright", async () => { const fetchMock = vi.fn(async () => new Response(null, { status: 202 })); vi.stubGlobal("fetch", fetchMock); await submitCatalogSearchMiss({ query: "q".repeat(900), cliVersion: "0.7.106" }); const [, init] = fetchMock.mock.calls[0]!; expect(JSON.parse(String(init?.body)).query).toHaveLength(500); }); it("never throws when the forward fails", async () => { vi.stubGlobal( "fetch", vi.fn(async () => { throw new Error("offline"); }), ); await expect( submitCatalogSearchMiss({ query: "a query", cliVersion: "0.7.106" }), ).resolves.toBeUndefined(); }); });