mirror of
https://github.com/gethomepage/homepage.git
synced 2026-04-04 01:01:22 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
61
src/widgets/docker/component.test.jsx
Normal file
61
src/widgets/docker/component.test.jsx
Normal 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");
|
||||
});
|
||||
});
|
||||
55
src/widgets/docker/stats-helpers.test.js
Normal file
55
src/widgets/docker/stats-helpers.test.js
Normal 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 });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user