Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/serhalp/npm.report. Audit, visualize, share, and track your npm org's trust signals. npm.report
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283import { render, screen, waitFor } from "@testing-library/svelte";import { afterEach, describe, expect, test, vi } from "vitest";import SharedReport from "./SharedReport.svelte";import { formatCompactDateTime, formatDate, formatDateTime } from "#client/dateFormatting";import { auditResult, trustReport } from "./test/fixtures";import { mockFetch, mockResolvedFetch } from "./test/mock";import { requestUrl } from "./test/request";
afterEach(() => vi.unstubAllGlobals());
describe("SharedReport", () => { test("loads and renders a read-only shared report", async () => { vi.stubGlobal( "fetch", mockResolvedFetch({ ok: true, status: 200, json: async () => ({ id: "report-id", orgs: "netlify", scopeLabel: "last 12 months", payload: auditResult, createdAt: "2026-06-27T12:34:56.000Z", dailyTrackingEnabled: false, dailyTrackingNextRunAt: null, }), }), );
render(SharedReport, { props: { id: "report/id" } });
expect(screen.getByRole("main")).toHaveAttribute("aria-busy", "true"); expect( screen .getByText("Loading report. Fetching the saved snapshot and trust history.") .closest('[role="status"]'), ).not.toBeNull(); const loadingVisual = screen.getByText("Loading report").closest(".shared-loading"); expect(loadingVisual).toHaveAttribute("aria-hidden", "true"); expect(loadingVisual?.querySelector(".signal-spinner")).not.toBeNull(); expect( await screen.findByRole("heading", { level: 2, name: "Audit of netlify" }), ).toBeInTheDocument(); expect(screen.getByRole("main")).toHaveAttribute("aria-busy", "false"); expect(screen.getByText("netlify").closest("p")).toHaveTextContent( `Audit of netlify — last 12 months, generated ${formatDate("2026-06-27T12:34:56.000Z")}`, ); expect(screen.getByText(formatDate("2026-06-27T12:34:56.000Z"))).toHaveAttribute( "title", formatDateTime("2026-06-27T12:34:56.000Z"), ); expect(screen.getAllByText("last 12 months").length).toBeGreaterThan(0); expect(screen.getByRole("link", { name: "Re-run this audit" }).getAttribute("href")).toContain( "run=1", ); expect(screen.getByRole("button", { name: "Copy link" })).toBeEnabled(); const bluesky = new URL( screen.getByRole("link", { name: "Share to Bluesky" }).getAttribute("href")!, ); expect(bluesky.origin).toBe("https://bsky.app"); expect(bluesky.searchParams.get("text")).toContain("http://localhost:3000/report/report-id"); expect(screen.getByRole("link", { name: "npm.report on GitHub" })).toHaveAttribute( "href", "https://github.com/serhalp/npm.report", ); expect(fetch).toHaveBeenCalledWith("/api/reports/report%2Fid"); });
test("shows trust history and disables tracking for an already tracked org set", async () => { let resolveHistory!: (value: { ok: boolean; status: number; json: () => Promise<unknown>; }) => void; const pendingHistory = new Promise<{ ok: boolean; status: number; json: () => Promise<unknown>; }>((resolve) => { resolveHistory = resolve; }); const fetchMock = mockFetch(async (input) => { if (requestUrl(input) === "/api/reports/report-id") { return { ok: true, status: 200, json: async () => ({ id: "report-id", orgs: "netlify", scopeLabel: "ALL org packages", payload: { ...auditResult, trust: { ...trustReport, summary: { ...trustReport.summary, scopeLabel: "ALL org packages", }, }, }, createdAt: "2026-06-27T12:34:56.000Z", dailyTrackingEnabled: true, dailyTrackingNextRunAt: "2026-06-28T12:34:56.000Z", }), }; } return pendingHistory; }); vi.stubGlobal("fetch", fetchMock);
render(SharedReport, { props: { id: "report-id" } });
await waitFor(() => { expect(fetchMock).toHaveBeenCalledWith("/api/reports/history?org=netlify"); }); expect( screen .getByText("Loading report. Fetching the saved snapshot and trust history.") .closest('[role="status"]'), ).not.toBeNull(); expect(screen.queryByRole("tab", { name: /package trust level/i })).not.toBeInTheDocument();
resolveHistory({ ok: true, status: 200, json: async () => ({ orgs: ["netlify"], points: [ { id: "report-id", url: "/report/report-id", capturedAt: "2026-06-27T12:34:56.000Z", total: 2, byLevel: trustReport.summary.byLevel, deprecated: 1, failureCount: 1, }, ], }), });
expect(await screen.findByRole("heading", { name: "Progress over time" })).toBeInTheDocument(); expect(document.querySelector(".masthead > p")).toHaveTextContent( `Audit of netlify, generated ${formatDate("2026-06-27T12:34:56.000Z")}`, ); expect(screen.getByRole("tab", { name: /package trust level/i })).toBeInTheDocument(); expect( await screen.findByRole("link", { name: formatDate("2026-06-27T12:34:56.000Z"), }), ).toHaveAttribute("href", "/report/report-id"); expect(screen.queryByRole("button", { name: "Tracking daily" })).not.toBeInTheDocument(); expect( screen.getByRole("status", { name: `Tracking daily, next run ${formatCompactDateTime("2026-06-28T12:34:56.000Z")}`, }), ).toBeInTheDocument(); expect(fetchMock).not.toHaveBeenCalledWith( "/api/reports/report-id/schedule-daily", expect.anything(), ); });
test("keeps stable org-set URLs while rendering and sharing the latest snapshot", async () => { const record = { id: "latest-report-id", orgs: "netlify", scopeLabel: "ALL org packages", payload: { ...auditResult, trust: { ...trustReport, summary: { ...trustReport.summary, orgs: ["netlify"] }, }, }, createdAt: "2026-06-27T12:34:56.000Z", dailyTrackingEnabled: true, dailyTrackingNextRunAt: "2026-06-28T12:34:56.000Z", }; const fetchMock = mockFetch(async (input) => requestUrl(input).startsWith("/api/reports/history") ? { ok: true, status: 200, json: async () => ({ orgs: ["netlify"], points: [] }) } : { ok: true, status: 200, json: async () => record }, ); vi.stubGlobal("fetch", fetchMock);
render(SharedReport, { props: { orgs: ["netlify"] } });
expect(await screen.findByRole("heading", { name: "Audit of netlify" })).toBeInTheDocument(); expect(document.querySelector(".masthead > p")).toHaveTextContent( "This is the latest read-only snapshot.", ); expect(fetchMock).toHaveBeenCalledWith("/api/reports/latest?org=netlify"); const bluesky = new URL( screen.getByRole("link", { name: "Share to Bluesky" }).getAttribute("href")!, ); expect(bluesky.searchParams.get("text")).toContain("http://localhost:3000/orgs/netlify"); });
test("still renders the report when its history request fails", async () => { const fetchMock = mockFetch(async (input) => { if (requestUrl(input) === "/api/reports/report-id") { return { ok: true, status: 200, json: async () => ({ id: "report-id", orgs: "netlify", scopeLabel: "ALL org packages", payload: { ...auditResult, trust: { ...trustReport, summary: { ...trustReport.summary, scopeLabel: "ALL org packages" }, }, }, createdAt: "2026-06-27T12:34:56.000Z", dailyTrackingEnabled: false, dailyTrackingNextRunAt: null, }), }; } return { ok: false, status: 503, json: async () => ({}), }; }); vi.stubGlobal("fetch", fetchMock);
render(SharedReport, { props: { id: "report-id" } });
expect(await screen.findByRole("tab", { name: /package trust level/i })).toBeInTheDocument(); expect(screen.getByText("No history yet")).toBeInTheDocument(); });
test("shows the specific not-found error for 404s", async () => { vi.stubGlobal( "fetch", mockResolvedFetch({ ok: false, status: 404, }), );
render(SharedReport, { props: { id: "missing" } });
expect(await screen.findByRole("alert")).toHaveTextContent("This report could not be found."); expect(screen.getByText(/Back to the audit tool/)).toBeInTheDocument(); });
test("rejects malformed stored report data at the client boundary", async () => { vi.stubGlobal( "fetch", mockResolvedFetch({ ok: true, status: 200, json: async () => ({ id: "report-id", payload: { failures: [] } }), }), );
render(SharedReport, { props: { id: "report-id" } });
expect(await screen.findByRole("alert")).toHaveTextContent( "This report is in an unexpected format and can't be displayed.", ); });
test("shows the upstream status when loading a report fails", async () => { vi.stubGlobal( "fetch", mockResolvedFetch({ ok: false, status: 503, }), );
render(SharedReport, { props: { id: "report-id" } });
expect(await screen.findByRole("alert")).toHaveTextContent("Failed to load report (503)."); });});