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

View File

@@ -0,0 +1,61 @@
// @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";
const { useSWR } = vi.hoisted(() => ({ useSWR: vi.fn() }));
vi.mock("swr", () => ({
default: useSWR,
}));
import Component from "./component";
describe("widgets/docker/component", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("renders offline status when container is not running", () => {
useSWR
.mockReturnValueOnce({ data: { status: "exited" }, error: undefined }) // status
.mockReturnValueOnce({ data: undefined, error: undefined }); // stats
renderWithProviders(<Component service={{ widget: { type: "docker", container: "c" } }} />, {
settings: { hideErrors: false },
});
expect(screen.getByText("widget.status")).toBeInTheDocument();
expect(screen.getByText("docker.offline")).toBeInTheDocument();
});
it("renders cpu/mem/rx/tx values when stats are available", () => {
useSWR
.mockReturnValueOnce({ data: { status: "running" }, error: undefined }) // status
.mockReturnValueOnce({
data: {
stats: {
cpu_stats: { cpu_usage: { total_usage: 200 }, system_cpu_usage: 2000, online_cpus: 2 },
precpu_stats: { cpu_usage: { total_usage: 100 }, system_cpu_usage: 1000 },
memory_stats: { usage: 1000, total_inactive_file: 100 },
networks: { eth0: { rx_bytes: 1, tx_bytes: 2 }, eth1: { rx_bytes: 3, tx_bytes: 4 } },
},
},
error: undefined,
});
const { container } = renderWithProviders(<Component service={{ widget: { type: "docker", container: "c" } }} />, {
settings: { hideErrors: false },
});
// cpu: (100/1000)*2*100=20
expect(container.textContent).toContain("20");
// mem used: 1000-100=900
expect(container.textContent).toContain("900");
// rx=4, tx=6
expect(container.textContent).toContain("4");
expect(container.textContent).toContain("6");
});
});

View File

@@ -0,0 +1,55 @@
import { describe, expect, it } from "vitest";
import { calculateCPUPercent, calculateThroughput, calculateUsedMemory } from "./stats-helpers";
describe("widgets/docker/stats-helpers", () => {
it("calculateCPUPercent returns 0 when deltas are not positive", () => {
expect(
calculateCPUPercent({
cpu_stats: { cpu_usage: { total_usage: 100 }, system_cpu_usage: 1000, online_cpus: 2 },
precpu_stats: { cpu_usage: { total_usage: 100 }, system_cpu_usage: 1000 },
}),
).toBe(0);
});
it("calculateCPUPercent computes percent and rounds to 1 decimal", () => {
// cpuDelta=100, systemDelta=1000, cpus=2 => (100/1000)*2*100 = 20.0
expect(
calculateCPUPercent({
cpu_stats: { cpu_usage: { total_usage: 200 }, system_cpu_usage: 2000, online_cpus: 2 },
precpu_stats: { cpu_usage: { total_usage: 100 }, system_cpu_usage: 1000 },
}),
).toBe(20);
});
it("calculateUsedMemory subtracts inactive file (prefers total_inactive_file)", () => {
const stats = {
memory_stats: {
usage: 1000,
total_inactive_file: 100,
stats: { inactive_file: 200 },
},
};
expect(calculateUsedMemory(stats)).toBe(900);
});
it("calculateUsedMemory falls back to stats.inactive_file when total_inactive_file missing", () => {
const stats = {
memory_stats: {
usage: 1000,
stats: { inactive_file: 200 },
},
};
expect(calculateUsedMemory(stats)).toBe(800);
});
it("calculateThroughput uses the special networks.network key when present", () => {
const stats = { networks: { network: { rx_bytes: 5, tx_bytes: 6 }, eth0: { rx_bytes: 1, tx_bytes: 2 } } };
expect(calculateThroughput(stats)).toEqual({ rxBytes: 5, txBytes: 6 });
});
it("calculateThroughput sums all interfaces otherwise", () => {
const stats = { networks: { eth0: { rx_bytes: 1, tx_bytes: 2 }, eth1: { rx_bytes: 3, tx_bytes: 4 } } };
expect(calculateThroughput(stats)).toEqual({ rxBytes: 4, txBytes: 6 });
});
});