Chore: homepage tests (#6278)

This commit is contained in:
shamoon
2026-02-04 19:58:39 -08:00
committed by GitHub
parent 7d019185a3
commit 872a3600aa
558 changed files with 32606 additions and 84 deletions

View File

@@ -0,0 +1,61 @@
// @vitest-environment jsdom
import { screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "test-utils/render-with-providers";
import { findServiceBlockByLabel } from "test-utils/widget-assertions";
const { useWidgetAPI } = vi.hoisted(() => ({ useWidgetAPI: vi.fn() }));
vi.mock("utils/proxy/use-widget-api", () => ({ default: useWidgetAPI }));
import Component from "./component";
function expectBlockValue(container, label, value) {
const block = findServiceBlockByLabel(container, label);
expect(block, `missing block for ${label}`).toBeTruthy();
expect(block.textContent).toContain(String(value));
}
describe("widgets/npm/component", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("renders placeholders while loading", () => {
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
const { container } = renderWithProviders(<Component service={{ widget: { type: "npm" } }} />, {
settings: { hideErrors: false },
});
expect(container.querySelectorAll(".service-block")).toHaveLength(3);
expect(screen.getByText("npm.enabled")).toBeInTheDocument();
expect(screen.getByText("npm.disabled")).toBeInTheDocument();
expect(screen.getByText("npm.total")).toBeInTheDocument();
});
it("renders error UI when endpoint errors", () => {
useWidgetAPI.mockReturnValue({ data: undefined, error: { message: "nope" } });
renderWithProviders(<Component service={{ widget: { type: "npm" } }} />, { settings: { hideErrors: false } });
expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
expect(screen.getByText("nope")).toBeInTheDocument();
});
it("renders enabled/disabled/total host counts", () => {
useWidgetAPI.mockReturnValue({
data: [{ enabled: true }, { enabled: false }, { enabled: 1 }, { enabled: 0 }, { enabled: true }],
error: undefined,
});
const { container } = renderWithProviders(<Component service={{ widget: { type: "npm" } }} />, {
settings: { hideErrors: false },
});
expectBlockValue(container, "npm.enabled", 3);
expectBlockValue(container, "npm.disabled", 2);
expectBlockValue(container, "npm.total", 5);
});
});

View File

@@ -0,0 +1,114 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import createMockRes from "test-utils/create-mock-res";
const { httpProxy, getServiceWidget, cache, logger } = vi.hoisted(() => {
const store = new Map();
return {
httpProxy: vi.fn(),
getServiceWidget: vi.fn(),
cache: {
get: vi.fn((k) => store.get(k)),
put: vi.fn((k, v) => store.set(k, v)),
del: vi.fn((k) => store.delete(k)),
_reset: () => store.clear(),
},
logger: {
debug: vi.fn(),
error: vi.fn(),
},
};
});
vi.mock("utils/logger", () => ({
default: () => logger,
}));
vi.mock("utils/config/service-helpers", () => ({
default: getServiceWidget,
}));
vi.mock("utils/proxy/http", () => ({
httpProxy,
}));
vi.mock("memory-cache", () => ({
default: cache,
...cache,
}));
vi.mock("widgets/widgets", () => ({
default: {
npm: {
api: "{url}/{endpoint}",
},
},
}));
import npmProxyHandler from "./proxy";
describe("widgets/npm/proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
cache._reset();
});
it("logs in when token is missing and uses Bearer token for requests", async () => {
getServiceWidget.mockResolvedValue({
type: "npm",
url: "http://npm",
username: "u",
password: "p",
});
httpProxy
.mockResolvedValueOnce([
200,
"application/json",
Buffer.from(JSON.stringify({ token: "t1", expires: new Date(Date.now() + 60_000).toISOString() })),
])
.mockResolvedValueOnce([200, "application/json", Buffer.from("data")]);
const req = { query: { group: "g", service: "svc", endpoint: "api/v1/stats", index: "0" } };
const res = createMockRes();
await npmProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(2);
expect(httpProxy.mock.calls[0][0]).toBe("http://npm/api/tokens");
expect(httpProxy.mock.calls[1][1].headers.Authorization).toBe("Bearer t1");
expect(res.body).toEqual(Buffer.from("data"));
});
it("retries after a 403 response by clearing cache and logging in again", async () => {
cache.put("npmProxyHandler__token.svc", "old");
getServiceWidget.mockResolvedValue({
type: "npm",
url: "http://npm",
username: "u",
password: "p",
});
httpProxy
.mockResolvedValueOnce([403, "application/json", Buffer.from("nope")])
.mockResolvedValueOnce([
200,
"application/json",
Buffer.from(JSON.stringify({ token: "new", expires: new Date(Date.now() + 60_000).toISOString() })),
])
.mockResolvedValueOnce([200, "application/json", Buffer.from("ok")]);
const req = { query: { group: "g", service: "svc", endpoint: "api/v1/stats", index: "0" } };
const res = createMockRes();
await npmProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(3);
expect(httpProxy.mock.calls[0][1].headers.Authorization).toBe("Bearer old");
expect(httpProxy.mock.calls[1][0]).toBe("http://npm/api/tokens");
expect(httpProxy.mock.calls[2][1].headers.Authorization).toBe("Bearer new");
expect(res.body).toEqual(Buffer.from("ok"));
});
});

View File

@@ -0,0 +1,11 @@
import { describe, it } from "vitest";
import { expectWidgetConfigShape } from "test-utils/widget-config";
import widget from "./widget";
describe("npm widget config", () => {
it("exports a valid widget config", () => {
expectWidgetConfigShape(widget);
});
});