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
@@ -0,0 +1,63 @@
// @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 }));
vi.mock("../../components/widgets/queue/queueEntry", () => ({
default: ({ title }) => <div data-testid="queue-entry">{title}</div>,
}));
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/qbittorrent/component", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("renders placeholders while loading", () => {
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
const { container } = renderWithProviders(<Component service={{ widget: { type: "qbittorrent" } }} />, {
settings: { hideErrors: false },
});
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
expect(screen.getByText("qbittorrent.leech")).toBeInTheDocument();
expect(screen.getByText("qbittorrent.download")).toBeInTheDocument();
expect(screen.getByText("qbittorrent.seed")).toBeInTheDocument();
expect(screen.getByText("qbittorrent.upload")).toBeInTheDocument();
});
it("computes leech/seed counts and upload/download rates, and can render leech progress entries", () => {
useWidgetAPI.mockReturnValue({
data: [
{ name: "A", dlspeed: 10, upspeed: 1, progress: 1, state: "uploading" },
{ name: "B", dlspeed: 5, upspeed: 2, progress: 0.5, state: "downloading", eta: 60, size: 100, amount_left: 50 },
],
error: undefined,
});
const service = { widget: { type: "qbittorrent", enableLeechProgress: true } };
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
// total=2, completed=1 => leech=1
expectBlockValue(container, "qbittorrent.leech", 1);
expectBlockValue(container, "qbittorrent.seed", 1);
expectBlockValue(container, "qbittorrent.download", 15);
expectBlockValue(container, "qbittorrent.upload", 3);
expect(screen.getAllByTestId("queue-entry").map((el) => el.textContent)).toEqual(["B"]);
});
});
+67
View File
@@ -0,0 +1,67 @@
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 qbittorrentProxyHandler from "./proxy";
describe("widgets/qbittorrent/proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("logs in and retries after a 403 response", async () => {
getServiceWidget.mockResolvedValue({ url: "http://qb", username: "u", password: "p" });
httpProxy
.mockResolvedValueOnce([403, "application/json", Buffer.from("nope")])
.mockResolvedValueOnce([200, "text/plain", Buffer.from("Ok.")])
.mockResolvedValueOnce([200, "application/json", Buffer.from("data")]);
const req = { query: { group: "g", service: "svc", endpoint: "torrents/info", index: "0" } };
const res = createMockRes();
await qbittorrentProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(3);
expect(httpProxy.mock.calls[1][0]).toBe("http://qb/api/v2/auth/login");
expect(res.statusCode).toBe(200);
expect(res.body).toEqual(Buffer.from("data"));
});
it("returns 401 when login succeeds but response body is not Ok.", async () => {
getServiceWidget.mockResolvedValue({ url: "http://qb", username: "u", password: "p" });
httpProxy
.mockResolvedValueOnce([403, "application/json", Buffer.from("nope")])
.mockResolvedValueOnce([200, "text/plain", Buffer.from("Denied")]);
const req = { query: { group: "g", service: "svc", endpoint: "torrents/info", index: "0" } };
const res = createMockRes();
await qbittorrentProxyHandler(req, res);
expect(res.statusCode).toBe(401);
expect(res.body).toEqual(Buffer.from("Denied"));
});
});
+11
View File
@@ -0,0 +1,11 @@
import { describe, it } from "vitest";
import { expectWidgetConfigShape } from "test-utils/widget-config";
import widget from "./widget";
describe("qbittorrent widget config", () => {
it("exports a valid widget config", () => {
expectWidgetConfigShape(widget);
});
});