Files
HealthLog/src/lib/__tests__/timezone-mirror.test.ts
T
Marc-André Bombeck 9bbd8a89dc chore(release): v1.28.35 — timestamps honor the profile timezone
Client-rendered times fell back to the hardcoded display zone
(Europe/Berlin) because neither useFormatters() nor the legacy format.ts
helpers ever received users.timezone. A third localStorage mirror
(timezone-mirror.ts, following the time-format/date-format pattern)
carries the validated profile zone from /api/auth/me into both read
paths; the fallback chain is strictly profile → Europe/Berlin — never
the browser zone, so screen, PDF, and export cannot disagree.
makeFormatters validates its zone before Intl (a poison value falls back
instead of throwing inside every render).

Same pass, per the two-document audit: formatUpdatedLabel boundary and
clock now resolve one zone in every state (the undefined-tz browser hole
is closed); @db.Date calendar fields render UTC-pinned so profiles west
of UTC cannot shift a day; dose-history day headings and GLP-1 weekday
labels derive from the display zone; chart day/month labels are
encoding-derived and UTC-pinned (bucket math untouched, byte-identical
for Berlin profiles); the clinician share view renders in the patient's
timezone instead of the container's; Playwright pins Europe/Berlin.
Server day-key semantics, streak anchoring, insight cache keys, and
verbatim wall-clock strings are untouched by construction and pinned by
the new Auckland/New_York/Manila + DST test matrix.
2026-07-14 20:19:23 +02:00

126 lines
4.2 KiB
TypeScript

/**
* Issue #490 — localStorage mirror of the per-user display timezone.
*
* The suite runs in the node environment (no jsdom), so a minimal
* `window` stub provides localStorage + the event target the mirror
* wires. The stub is deliberately tiny: the mirror only calls
* `getItem` / `setItem` / `removeItem` and `add/removeEventListener` /
* `dispatchEvent`.
*/
import { afterEach, describe, expect, it, vi } from "vitest";
import {
readStoredTimezone,
storeTimezone,
subscribeTimezone,
} from "../timezone-mirror";
import { makeFormatters } from "../format-locale";
const STORAGE_KEY = "healthlog-timezone";
function stubWindow() {
const store = new Map<string, string>();
const listeners = new Map<string, Set<() => void>>();
const win = {
localStorage: {
getItem: (k: string) => store.get(k) ?? null,
setItem: (k: string, v: string) => {
store.set(k, v);
},
removeItem: (k: string) => {
store.delete(k);
},
},
addEventListener: (type: string, fn: () => void) => {
const set = listeners.get(type) ?? new Set<() => void>();
set.add(fn);
listeners.set(type, set);
},
removeEventListener: (type: string, fn: () => void) => {
listeners.get(type)?.delete(fn);
},
dispatchEvent: (ev: Event) => {
for (const fn of listeners.get(ev.type) ?? []) fn();
return true;
},
};
vi.stubGlobal("window", win);
return { store };
}
afterEach(() => {
vi.unstubAllGlobals();
});
describe("timezone mirror (issue #490)", () => {
it("reads '' when no mirror exists", () => {
stubWindow();
expect(readStoredTimezone()).toBe("");
});
it("stores a valid IANA zone and reads it back", () => {
const { store } = stubWindow();
storeTimezone("Asia/Manila");
expect(store.get(STORAGE_KEY)).toBe("Asia/Manila");
expect(readStoredTimezone()).toBe("Asia/Manila");
});
it("notifies same-tab subscribers on change, not on a no-op write", () => {
stubWindow();
const onChange = vi.fn();
const unsubscribe = subscribeTimezone(onChange);
storeTimezone("Pacific/Auckland");
expect(onChange).toHaveBeenCalledTimes(1);
// Same value again → no event (mirrors the time-format contract).
storeTimezone("Pacific/Auckland");
expect(onChange).toHaveBeenCalledTimes(1);
unsubscribe();
storeTimezone("America/New_York");
expect(onChange).toHaveBeenCalledTimes(1);
});
it("clears the mirror instead of storing a garbage zone", () => {
const { store } = stubWindow();
storeTimezone("Europe/Berlin");
expect(store.get(STORAGE_KEY)).toBe("Europe/Berlin");
storeTimezone("Mars/Olympus");
expect(store.has(STORAGE_KEY)).toBe(false);
expect(readStoredTimezone()).toBe("");
});
it("clears the mirror on an empty value", () => {
const { store } = stubWindow();
storeTimezone("Europe/Berlin");
storeTimezone("");
expect(store.has(STORAGE_KEY)).toBe(false);
});
it("treats a stale invalid mirror (pre-validation build) as absent", () => {
const { store } = stubWindow();
store.set(STORAGE_KEY, "not-a-zone");
expect(readStoredTimezone()).toBe("");
// …and the value must never reach Intl: the formatter chain renders
// the Berlin fallback instead of throwing.
const fmt = makeFormatters("en", readStoredTimezone(), "H24");
expect(fmt.time(new Date("2026-04-18T14:30:00Z"))).toBe("16:30");
});
it("stale-mirror tolerance: an old but valid zone keeps rendering", () => {
const { store } = stubWindow();
// The profile moved to Auckland on another device; this tab still
// mirrors Manila until the next `/api/auth/me` fetch. That must
// render (in the stale zone), never throw.
store.set(STORAGE_KEY, "Asia/Manila");
const fmt = makeFormatters("en", readStoredTimezone(), "H24");
expect(fmt.time(new Date("2026-04-18T14:30:00Z"))).toBe("22:30");
});
it("no-ops on SSR (window undefined)", () => {
// Node environment without the stub — `window` is undefined.
expect(readStoredTimezone()).toBe("");
expect(() => storeTimezone("Europe/Berlin")).not.toThrow();
const unsubscribe = subscribeTimezone(() => {});
expect(() => unsubscribe()).not.toThrow();
});
});