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
+68
View File
@@ -0,0 +1,68 @@
// @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/flood/component", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("renders error UI when widget API errors", () => {
useWidgetAPI.mockReturnValue({ data: undefined, error: { message: "nope" } });
renderWithProviders(<Component service={{ widget: { type: "flood", url: "http://x" } }} />, {
settings: { hideErrors: false },
});
expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
expect(screen.getByText("nope")).toBeInTheDocument();
});
it("renders a helpful error when the API returns no torrent data", () => {
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
renderWithProviders(<Component service={{ widget: { type: "flood", url: "http://x" } }} />, {
settings: { hideErrors: false },
});
expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
expect(screen.getByText("No torrent data returned")).toBeInTheDocument();
});
it("renders computed leech/seed counts and up/down rates", () => {
useWidgetAPI.mockReturnValue({
data: {
torrents: {
a: { downRate: 10, upRate: 20, status: ["downloading"] },
b: { downRate: 5, upRate: 0, status: ["complete"] },
c: { downRate: 0, upRate: 1, status: ["complete", "downloading"] },
},
},
error: undefined,
});
const { container } = renderWithProviders(<Component service={{ widget: { type: "flood", url: "http://x" } }} />, {
settings: { hideErrors: false },
});
expectBlockValue(container, "flood.leech", 2);
expectBlockValue(container, "flood.download", 15);
expectBlockValue(container, "flood.seed", 2);
expectBlockValue(container, "flood.upload", 21);
});
});
+69
View File
@@ -0,0 +1,69 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import createMockRes from "test-utils/create-mock-res";
const { httpProxy, getServiceWidget, logger } = vi.hoisted(() => ({
httpProxy: vi.fn(),
getServiceWidget: vi.fn(),
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,
}));
import floodProxyHandler from "./proxy";
describe("widgets/flood/proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("logs in and retries after a 401 response", async () => {
getServiceWidget.mockResolvedValue({ url: "http://flood" });
httpProxy
.mockResolvedValueOnce([401, "application/json", Buffer.from("nope")])
.mockResolvedValueOnce([200, "application/json", Buffer.from("ok")])
.mockResolvedValueOnce([200, "application/json", Buffer.from("data")]);
const req = { query: { group: "g", service: "svc", endpoint: "stats", index: "0" } };
const res = createMockRes();
await floodProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(3);
expect(httpProxy.mock.calls[0][0].toString()).toBe("http://flood/api/stats");
expect(httpProxy.mock.calls[1][0]).toBe("http://flood/api/auth/authenticate");
expect(httpProxy.mock.calls[1][1].body).toBeNull();
expect(httpProxy.mock.calls[2][0].toString()).toBe("http://flood/api/stats");
expect(res.statusCode).toBe(200);
expect(res.body).toEqual(Buffer.from("data"));
});
it("returns the login error status when authentication fails", async () => {
getServiceWidget.mockResolvedValue({ url: "http://flood", username: "u", password: "p" });
httpProxy
.mockResolvedValueOnce([401, "application/json", Buffer.from("nope")])
.mockResolvedValueOnce([500, "application/json", Buffer.from("bad")]);
const req = { query: { group: "g", service: "svc", endpoint: "stats", index: "0" } };
const res = createMockRes();
await floodProxyHandler(req, res);
expect(httpProxy.mock.calls[1][1].body).toBe(JSON.stringify({ username: "u", password: "p" }));
expect(res.statusCode).toBe(500);
expect(res.end).toHaveBeenCalledWith(Buffer.from("bad"));
});
});
+11
View File
@@ -0,0 +1,11 @@
import { describe, it } from "vitest";
import { expectWidgetConfigShape } from "test-utils/widget-config";
import widget from "./widget";
describe("flood widget config", () => {
it("exports a valid widget config", () => {
expectWidgetConfigShape(widget);
});
});