fix(diagnostics): count the 24 hours from the moment the level was raised

The countdown's clock was the moment Settings mounted, so a level raised
two minutes after opening the page read "24 h 2 m" until the first tick
(measured on prod). The clock is read at render; the interval only
re-renders.
This commit is contained in:
2026-09-19 05:03:31 +02:00
parent 2fa5c8c394
commit 6bbf532b70
2 changed files with 31 additions and 5 deletions
@@ -16,8 +16,14 @@ vi.mock("react-i18next", async (importOriginal) => {
return {
...actual,
useTranslation: () => ({
t: (key: string, opts?: { count?: number }) =>
opts?.count != null ? `${key}:${opts.count}` : key,
t: (key: string, opts?: { count?: number; h?: number; m?: number; time?: string }) =>
opts?.count != null
? `${key}:${opts.count}`
: opts?.m != null
? `${key}:${opts.h ?? 0}h${opts.m}m`
: opts?.time != null
? `${key}:${opts.time}`
: key,
i18n: { language: "en" },
}),
};
@@ -106,6 +112,23 @@ describe("DiagnosticsSettings", () => {
expect(setLevel).toHaveBeenLastCalledWith("INFO");
});
it("counts the 24 hours from the moment the level was raised, not from when the page opened", async () => {
vi.useFakeTimers({ toFake: ["Date"] });
try {
vi.setSystemTime(new Date("2026-09-19T02:54:00Z"));
mount({});
vi.setSystemTime(new Date("2026-09-19T02:56:30Z")); // the page sat open for 2.5 minutes
fireEvent.click(levelButton("settings.diagnostics.levels.DEBUG"));
fireEvent.click(screen.getByTestId("diagnostics-consent-confirm"));
await waitFor(() => expect(screen.getByTestId("diagnostics-status")).toBeTruthy());
expect(screen.getByTestId("diagnostics-status").textContent).toContain(
"settings.diagnostics.hoursMinutes:24h0m",
);
} finally {
vi.useRealTimers();
}
});
it("going back to the base level needs no consent", async () => {
const until = new Date(Date.now() + 3_600_000).toISOString();
mount({ diagnostics: { level: "DEBUG", until, retention_days: 7 } });