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
+93
View File
@@ -0,0 +1,93 @@
// @vitest-environment jsdom
import { screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "test-utils/render-with-providers";
const { useWidgetAPI } = vi.hoisted(() => ({
useWidgetAPI: vi.fn(),
}));
vi.mock("utils/proxy/use-widget-api", () => ({
default: useWidgetAPI,
}));
import Component from "./component";
describe("widgets/pihole/component", () => {
it("renders error UI when widget API errors", () => {
useWidgetAPI.mockReturnValue({ data: undefined, error: { message: "nope" } });
renderWithProviders(<Component service={{ widget: { type: "pihole", url: "http://x" } }} />, {
settings: { hideErrors: false },
});
expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
});
it("renders placeholders while loading and defaults fields (3 visible blocks)", () => {
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
const { container } = renderWithProviders(<Component service={{ widget: { type: "pihole", url: "http://x" } }} />, {
settings: { hideErrors: false },
});
// Default fields are queries/blocked/gravity; blocked_percent is present in JSX but filtered out by Container.
expect(container.querySelectorAll(".service-block")).toHaveLength(3);
expect(screen.getByText("pihole.queries")).toBeInTheDocument();
expect(screen.getByText("pihole.blocked")).toBeInTheDocument();
expect(screen.getByText("pihole.gravity")).toBeInTheDocument();
expect(screen.queryByText("pihole.blocked_percent")).toBeNull();
expect(screen.getAllByText("-")).toHaveLength(3);
});
it("renders values and appends percent to blocked when blocked_percent is not a field", () => {
useWidgetAPI.mockReturnValue({
data: {
ads_blocked_today: "5",
ads_percentage_today: "12.345",
dns_queries_today: "99",
domains_being_blocked: "123",
},
error: undefined,
});
const { container } = renderWithProviders(<Component service={{ widget: { type: "pihole", url: "http://x" } }} />, {
settings: { hideErrors: false },
});
expect(container.querySelectorAll(".service-block")).toHaveLength(3);
// common.number/common.percent are formatted by the test i18n stub in vitest.setup.js
expect(screen.getByText("99")).toBeInTheDocument();
expect(screen.getByText("123")).toBeInTheDocument();
expect(screen.getByText("5 (12.3)")).toBeInTheDocument();
});
it("renders blocked_percent as its own block when configured", () => {
useWidgetAPI.mockReturnValue({
data: {
ads_blocked_today: "5",
ads_percentage_today: "12.345",
dns_queries_today: "99",
domains_being_blocked: "123",
},
error: undefined,
});
const { container } = renderWithProviders(
<Component
service={{
widget: { type: "pihole", url: "http://x", fields: ["queries", "blocked", "blocked_percent", "gravity"] },
}}
/>,
{ settings: { hideErrors: false } },
);
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
expect(screen.getByText("5")).toBeInTheDocument(); // blocked (no percent appended)
expect(screen.getByText("12.3")).toBeInTheDocument(); // blocked_percent
});
});
+174
View File
@@ -0,0 +1,174 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
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: {
pihole: {
apiv5: "{url}/{endpoint}",
api: "{url}/{endpoint}",
},
},
}));
import piholeProxyHandler from "./proxy";
function createRes() {
const res = {
statusCode: null,
body: null,
};
res.status = vi.fn((code) => {
res.statusCode = code;
return res;
});
res.json = vi.fn((body) => {
res.body = body;
return res;
});
res.send = vi.fn((body) => {
res.body = body;
return res;
});
return res;
}
describe("widgets/pihole/proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
cache._reset();
});
it("proxies Pi-hole v5 via apiv5 summaryRaw and returns raw data", async () => {
getServiceWidget.mockResolvedValue({ type: "pihole", version: 5, url: "http://pi" });
httpProxy.mockResolvedValue([200, "application/json", '{"ok":true}']);
const req = { query: { group: "g", service: "svc", index: "0" } };
const res = createRes();
await piholeProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledWith("http://pi/summaryRaw");
expect(res.status).toHaveBeenCalledWith(200);
expect(res.send).toHaveBeenCalledWith('{"ok":true}');
});
it("proxies Pi-hole v6 without auth when key is missing", async () => {
getServiceWidget.mockResolvedValue({ type: "pihole", version: 6, url: "http://pi" });
httpProxy.mockResolvedValue([
200,
"application/json",
JSON.stringify({
gravity: { domains_being_blocked: 123 },
queries: { blocked: 4, percent_blocked: 5.5, total: 99 },
}),
]);
const req = { query: { group: "g", service: "svc", index: "0" } };
const res = createRes();
await piholeProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledWith("http://pi/stats/summary", {
headers: { "Content-Type": "application/json" },
});
expect(res.statusCode).toBe(200);
expect(res.body).toEqual({
domains_being_blocked: 123,
ads_blocked_today: 4,
ads_percentage_today: 5.5,
dns_queries_today: 99,
});
});
it("returns 500 when key is provided but login fails and no SID is cached", async () => {
getServiceWidget.mockResolvedValue({ type: "pihole", version: 6, url: "http://pi", key: "pw" });
httpProxy.mockResolvedValueOnce([401, "application/json", JSON.stringify({ session: null })]);
const req = { query: { group: "g", service: "svc", index: "0" } };
const res = createRes();
await piholeProxyHandler(req, res);
expect(res.statusCode).toBe(500);
expect(res.body).toEqual({ error: "Failed to authenticate with Pi-hole" });
});
it("logs in and uses X-FTL-SID header for Pi-hole v6 when key is provided", async () => {
getServiceWidget.mockResolvedValue({ type: "pihole", version: 6, url: "http://pi", key: "pw" });
httpProxy
.mockResolvedValueOnce([200, "application/json", JSON.stringify({ session: { sid: "sid123", validity: 1000 } })])
.mockResolvedValueOnce([
200,
"application/json",
JSON.stringify({
gravity: { domains_being_blocked: 1 },
queries: { blocked: 2, percent_blocked: 3, total: 4 },
}),
]);
const req = { query: { group: "g", service: "svc", index: "0" } };
const res = createRes();
await piholeProxyHandler(req, res);
// First call: login endpoint
expect(httpProxy).toHaveBeenNthCalledWith(
1,
"http://pi/auth",
expect.objectContaining({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password: "pw" }),
}),
);
// Second call: stats/summary with SID header
expect(httpProxy).toHaveBeenNthCalledWith(
2,
"http://pi/stats/summary",
expect.objectContaining({
headers: { "Content-Type": "application/json", "X-FTL-SID": "sid123" },
}),
);
expect(res.statusCode).toBe(200);
expect(res.body.dns_queries_today).toBe(4);
});
});
+11
View File
@@ -0,0 +1,11 @@
import { describe, it } from "vitest";
import { expectWidgetConfigShape } from "test-utils/widget-config";
import widget from "./widget";
describe("pihole widget config", () => {
it("exports a valid widget config", () => {
expectWidgetConfigShape(widget);
});
});