Files
pi2d/web/tests/uptime.test.tsx
T
yuez 2f2f1bb518 feat(daemon): report host uptime, not only the daemon's process age
The status tile labelled "uptime" showed the age of the daemon *process*
(`/info.uptimeMs`), and `/info` was fetched once with a 30 s staleTime and no
refetch interval, so the figure also froze: on a box five days up, a
`pi2 update` restart made the tile read "3m" and it never moved again.

`os.uptime()` is now sampled with the other host figures and served as
`system.hostUptimeSec` — which the 3 s `/resources` poll already refreshes —
plus an additive `/info.hostUptimeMs`. The tile renders it through a
`LiveUptime` label that anchors the reported snapshot once and ticks locally,
so the counter advances without a refetch and cannot rewind on a re-render.
The daemon's own process age stays in the server card, relabelled
`status.daemonUptime`, next to the `pid`/`daemonId` that give it meaning.
2026-10-30 16:18:48 +08:00

52 lines
1.8 KiB
TypeScript

import { act, render } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { LiveUptime, uptimeAnchor, uptimeSecondsFrom } from "@/features/status/uptime";
afterEach(() => {
vi.useRealTimers();
});
describe("uptime anchoring", () => {
it("anchors a reported duration at the matching epoch instant", () => {
expect(uptimeAnchor(5_000, 10_000)).toBe(5_000);
expect(uptimeAnchor(3 * 86_400_000, 1_000)).toBe(1_000 - 3 * 86_400_000);
});
it("rejects values a clock cannot start from", () => {
expect(uptimeAnchor(null)).toBeNull();
expect(uptimeAnchor(undefined)).toBeNull();
expect(uptimeAnchor(Number.NaN)).toBeNull();
expect(uptimeAnchor(Number.POSITIVE_INFINITY)).toBeNull();
// A negative duration would render as a rewind, not as uptime.
expect(uptimeAnchor(-1)).toBeNull();
});
it("counts forward and never below zero", () => {
expect(uptimeSecondsFrom(1_000, 62_000)).toBe(61);
expect(uptimeSecondsFrom(1_000, 1_000)).toBe(0);
expect(uptimeSecondsFrom(1_000, 0)).toBe(0);
});
});
describe("LiveUptime", () => {
it("renders an em dash when the daemon reports nothing", () => {
const { container } = render(<LiveUptime ms={null} />);
expect(container.textContent).toBe("—");
});
it("ticks without needing a fresh /info sample", () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-01-01T00:00:00Z"));
// `/info` was only fetched once; the label must keep counting locally.
const { container } = render(<LiveUptime ms={5 * 60_000} />);
expect(container.textContent).toBe("0h 5m");
act(() => {
vi.advanceTimersByTime(60_000);
});
expect(container.textContent).toBe("0h 6m");
});
});