mirror of
https://github.com/gethomepage/homepage.git
synced 2026-10-17 16:01:43 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
// @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/beszel/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders placeholders while loading (systems view)", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
|
||||
|
||||
const service = { widget: { type: "beszel" } };
|
||||
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||
|
||||
expect(service.widget.fields).toEqual(["systems", "up"]);
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(2);
|
||||
expect(screen.getByText("beszel.systems")).toBeInTheDocument();
|
||||
expect(screen.getByText("beszel.up")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders system totals when loaded (systems view)", () => {
|
||||
useWidgetAPI.mockReturnValue({
|
||||
data: {
|
||||
totalItems: 3,
|
||||
items: [{ status: "up" }, { status: "down" }, { status: "up" }],
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
const { container } = renderWithProviders(<Component service={{ widget: { type: "beszel" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expectBlockValue(container, "beszel.systems", 3);
|
||||
expectBlockValue(container, "beszel.up", "2 / 3");
|
||||
});
|
||||
|
||||
it("renders selected system details and filters to 4 default fields", () => {
|
||||
useWidgetAPI.mockReturnValue({
|
||||
data: {
|
||||
totalItems: 1,
|
||||
items: [
|
||||
{
|
||||
id: "sys1",
|
||||
name: "MySystem",
|
||||
status: "up",
|
||||
updated: 123,
|
||||
info: { cpu: 10, mp: 20, dp: 30, b: 40 },
|
||||
},
|
||||
],
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
const service = { widget: { type: "beszel", systemId: "sys1" } };
|
||||
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||
|
||||
expect(service.widget.fields).toEqual(["name", "status", "cpu", "memory"]);
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||
|
||||
expectBlockValue(container, "beszel.name", "MySystem");
|
||||
expectBlockValue(container, "beszel.status", "beszel.up");
|
||||
expectBlockValue(container, "beszel.cpu", 10);
|
||||
expectBlockValue(container, "beszel.memory", 20);
|
||||
expect(screen.queryByText("beszel.updated")).toBeNull();
|
||||
});
|
||||
|
||||
it("renders error when systemId is not found", () => {
|
||||
useWidgetAPI.mockReturnValue({
|
||||
data: { totalItems: 1, items: [{ id: "sys1", name: "MySystem", status: "up", info: {} }] },
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
renderWithProviders(<Component service={{ widget: { type: "beszel", systemId: "missing" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(screen.getByText("System with id missing not found")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,117 @@
|
||||
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: {
|
||||
beszel: {
|
||||
api: "{url}/{endpoint}",
|
||||
mappings: {
|
||||
authv1: { endpoint: "api/auth" },
|
||||
authv2: { endpoint: "api/auth/v2" },
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import beszelProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/beszel/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
cache._reset();
|
||||
});
|
||||
|
||||
it("logs in when token is missing and uses Bearer token for requests", async () => {
|
||||
getServiceWidget.mockResolvedValue({
|
||||
type: "beszel",
|
||||
url: "http://beszel",
|
||||
username: "u",
|
||||
password: "p",
|
||||
});
|
||||
|
||||
httpProxy
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ token: "t1" }))])
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ items: [1] }))]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", endpoint: "items", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await beszelProxyHandler(req, res);
|
||||
|
||||
expect(httpProxy).toHaveBeenCalledTimes(2);
|
||||
expect(httpProxy.mock.calls[0][0]).toBe("http://beszel/api/auth");
|
||||
expect(httpProxy.mock.calls[1][0].toString()).toBe("http://beszel/items");
|
||||
expect(httpProxy.mock.calls[1][1]).toEqual({
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer t1",
|
||||
},
|
||||
});
|
||||
expect(res.send).toHaveBeenCalledWith(Buffer.from(JSON.stringify({ items: [1] })));
|
||||
});
|
||||
|
||||
it("retries after receiving an empty list by clearing cache and logging in again", async () => {
|
||||
cache.put("beszelProxyHandler__token.svc", "old");
|
||||
|
||||
getServiceWidget.mockResolvedValue({
|
||||
type: "beszel",
|
||||
url: "http://beszel",
|
||||
username: "u",
|
||||
password: "p",
|
||||
});
|
||||
|
||||
httpProxy
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ items: [] }))])
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ token: "new" }))])
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ items: [1] }))]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", endpoint: "items", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await beszelProxyHandler(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://beszel/api/auth");
|
||||
expect(httpProxy.mock.calls[2][1].headers.Authorization).toBe("Bearer new");
|
||||
expect(res.send).toHaveBeenCalledWith(Buffer.from(JSON.stringify({ items: [1] })));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import { describe, it } from "vitest";
|
||||
|
||||
import { expectWidgetConfigShape } from "test-utils/widget-config";
|
||||
|
||||
import widget from "./widget";
|
||||
|
||||
describe("beszel widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user