mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-30 07:51:17 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
// @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/jdownloader/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders placeholders while loading", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
|
||||
|
||||
const { container } = renderWithProviders(
|
||||
<Component service={{ widget: { type: "jdownloader", url: "http://x" } }} />,
|
||||
{ settings: { hideErrors: false } },
|
||||
);
|
||||
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||
expect(screen.getByText("jdownloader.downloadCount")).toBeInTheDocument();
|
||||
expect(screen.getByText("jdownloader.downloadTotalBytes")).toBeInTheDocument();
|
||||
expect(screen.getByText("jdownloader.downloadBytesRemaining")).toBeInTheDocument();
|
||||
expect(screen.getByText("jdownloader.downloadSpeed")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("calls the unified endpoint with a 30s refresh interval", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
|
||||
|
||||
renderWithProviders(<Component service={{ widget: { type: "jdownloader", url: "http://x" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(useWidgetAPI.mock.calls[0][1]).toBe("unified");
|
||||
expect(useWidgetAPI.mock.calls[0][2]?.refreshInterval).toBe(30000);
|
||||
});
|
||||
|
||||
it("renders error UI when widget API errors", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: { message: "nope" } });
|
||||
|
||||
renderWithProviders(<Component service={{ widget: { type: "jdownloader", url: "http://x" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
|
||||
expect(screen.getByText("nope")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders values when loaded", () => {
|
||||
useWidgetAPI.mockReturnValue({
|
||||
data: {
|
||||
downloadCount: 1,
|
||||
totalBytes: 100,
|
||||
bytesRemaining: 40,
|
||||
totalSpeed: 10,
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
const { container } = renderWithProviders(
|
||||
<Component service={{ widget: { type: "jdownloader", url: "http://x" } }} />,
|
||||
{ settings: { hideErrors: false } },
|
||||
);
|
||||
|
||||
expectBlockValue(container, "jdownloader.downloadCount", 1);
|
||||
expectBlockValue(container, "jdownloader.downloadTotalBytes", 100);
|
||||
expectBlockValue(container, "jdownloader.downloadBytesRemaining", 40);
|
||||
expectBlockValue(container, "jdownloader.downloadSpeed", 10);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import createMockRes from "test-utils/create-mock-res";
|
||||
|
||||
const { httpProxy, getServiceWidget, tools, logger } = vi.hoisted(() => ({
|
||||
httpProxy: vi.fn(),
|
||||
getServiceWidget: vi.fn(),
|
||||
tools: {
|
||||
uniqueRid: vi.fn(() => 123),
|
||||
sha256: vi.fn(() => "secret"),
|
||||
validateRid: vi.fn(() => true),
|
||||
createEncryptionToken: vi.fn(() => "enc-token"),
|
||||
decrypt: vi.fn((cipherText) => {
|
||||
if (cipherText === "connect") {
|
||||
return JSON.stringify({ rid: 123, sessiontoken: "sess" });
|
||||
}
|
||||
if (cipherText === "devices") {
|
||||
return JSON.stringify({ list: [{ name: "myclient", id: "dev1" }] });
|
||||
}
|
||||
if (cipherText === "packages") {
|
||||
return JSON.stringify({
|
||||
data: [
|
||||
{ bytesLoaded: 40, bytesTotal: 100, finished: false, speed: 10 },
|
||||
{ bytesLoaded: 100, bytesTotal: 100, finished: true, speed: 0 },
|
||||
],
|
||||
});
|
||||
}
|
||||
return JSON.stringify({});
|
||||
}),
|
||||
encrypt: vi.fn(() => "encrypted-body"),
|
||||
},
|
||||
logger: { debug: vi.fn(), error: vi.fn() },
|
||||
}));
|
||||
|
||||
vi.mock("./tools", () => tools);
|
||||
vi.mock("utils/logger", () => ({
|
||||
default: () => logger,
|
||||
}));
|
||||
vi.mock("utils/config/service-helpers", () => ({
|
||||
default: getServiceWidget,
|
||||
}));
|
||||
vi.mock("utils/proxy/http", () => ({
|
||||
httpProxy,
|
||||
}));
|
||||
|
||||
import jdownloaderProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/jdownloader/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("aggregates package stats from the JDownloader API", async () => {
|
||||
getServiceWidget.mockResolvedValue({
|
||||
url: "http://ignored",
|
||||
username: "user@example.com",
|
||||
password: "pw",
|
||||
client: "myclient",
|
||||
});
|
||||
|
||||
httpProxy
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from("connect")])
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from("devices")])
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from("packages")]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await jdownloaderProxyHandler(req, res);
|
||||
|
||||
expect(httpProxy).toHaveBeenCalledTimes(3);
|
||||
expect(res.body).toEqual({
|
||||
downloadCount: 2,
|
||||
bytesRemaining: 60,
|
||||
totalBytes: 200,
|
||||
totalSpeed: 10,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
import crypto from "crypto";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createEncryptionToken, decrypt, encrypt, sha256, uniqueRid, validateRid } from "./tools";
|
||||
|
||||
describe("widgets/jdownloader/tools", () => {
|
||||
it("sha256 returns a 32-byte buffer", () => {
|
||||
expect(sha256("hello")).toBeInstanceOf(Buffer);
|
||||
expect(sha256("hello")).toHaveLength(32);
|
||||
});
|
||||
|
||||
it("uniqueRid returns an integer", () => {
|
||||
vi.spyOn(Math, "random").mockReturnValueOnce(0.123);
|
||||
expect(uniqueRid()).toBeTypeOf("number");
|
||||
expect(Number.isInteger(uniqueRid())).toBe(true);
|
||||
Math.random.mockRestore();
|
||||
});
|
||||
|
||||
it("validateRid throws when mismatched", () => {
|
||||
expect(() => validateRid({ rid: 1 }, 2)).toThrow(/RequestID mismatch/i);
|
||||
expect(validateRid({ rid: 5 }, 5)).toEqual({ rid: 5 });
|
||||
});
|
||||
|
||||
it("encrypt/decrypt roundtrip with a 32-byte ivKey", () => {
|
||||
const ivKey = crypto.randomBytes(32);
|
||||
const plaintext = "secret";
|
||||
const encrypted = encrypt(plaintext, ivKey);
|
||||
const decrypted = decrypt(encrypted, ivKey);
|
||||
expect(decrypted).toBe(plaintext);
|
||||
});
|
||||
|
||||
it("createEncryptionToken merges buffers and hashes", () => {
|
||||
const oldToken = Buffer.from("aa", "hex");
|
||||
const updateToken = "bb";
|
||||
const token = createEncryptionToken(oldToken, updateToken);
|
||||
expect(token).toBeInstanceOf(Buffer);
|
||||
expect(token).toHaveLength(32);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import { describe, it } from "vitest";
|
||||
|
||||
import { expectWidgetConfigShape } from "test-utils/widget-config";
|
||||
|
||||
import widget from "./widget";
|
||||
|
||||
describe("jdownloader widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user