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.
This commit is contained in:
Marc-André Bombeck committed 2026-07-14 20:19:23 +02:00
1 parent 136653e3d7
commit 9bbd8a89dc
41 files changed
+958 -90

No files matched your search

+41
View File
@@ -9,6 +9,7 @@ import { describe, expect, it } from "vitest";
import {
formatDate,
formatDateWithWeekday,
parseIsoDate,
resolveDateLocale,
isDateFormatPreference,
@@ -71,6 +72,46 @@ describe("formatDate", () => {
});
});
// ── Issue #490 — `@db.Date` / day-key rendering regressions ─────────────
//
// A serialised calendar date (`@db.Date` → UTC-midnight instant, or a raw
// profile-tz `yyyy-MM-dd` day key) must render the ENCODED day in every
// display zone — the pre-fix paths re-read the encoding in a timezone-aware
// formatter and shifted the day for zones west of UTC (medication one-shot
// `startsOn`) or for browser ≠ profile pairs (dose-ledger day headings).
describe("formatDateWithWeekday (#490)", () => {
// 2026-07-14 is a Tuesday.
it("renders a day key with its zone-independent weekday", () => {
expect(formatDateWithWeekday("2026-07-14", "AUTO", "en")).toBe(
"Tue, 07/14",
);
expect(formatDateWithWeekday("2026-07-14", "AUTO", "de")).toBe(
"Di., 14.07.",
);
});
it("honours the date-order preference like the formatter hook", () => {
expect(formatDateWithWeekday("2026-07-14", "DMY", "en")).toBe(
"Di., 14.07.",
);
expect(formatDateWithWeekday("2026-07-14", "MDY", "de")).toBe("Tue, 07/14");
});
it("renders a UTC-midnight Date on its encoded day (never the day before)", () => {
// The `@db.Date` wire shape — the exact value class the medication
// one-shot `startsOn` fix feeds through `formatDate`.
const utcMidnight = new Date("2026-07-01T00:00:00.000Z");
expect(formatDateWithWeekday(utcMidnight, "AUTO", "en")).toBe("Wed, 07/01");
expect(formatDate(utcMidnight, "AUTO", "en")).toBe("07/01/2026");
});
it("returns an empty string for empty / unparseable input", () => {
expect(formatDateWithWeekday("", "AUTO", "en")).toBe("");
expect(formatDateWithWeekday(null, "AUTO", "en")).toBe("");
expect(formatDateWithWeekday("14.07.2026", "AUTO", "en")).toBe("");
});
});
describe("parseIsoDate", () => {
it("parses a valid ISO calendar date to a UTC midnight Date", () => {
const d = parseIsoDate("2026-02-19");
+75
View File
@@ -137,6 +137,81 @@ describe("makeFormatters", () => {
// The narrow check: day-of-month should be 11, not 10.
expect(tokyo.date(lateUtc)).toMatch(/11/);
});
// ── Issue #490 — profile-timezone display matrix ──────────────────────
//
// The client formatters now receive the mirrored profile zone. Pin the
// three matrix zones (east + DST, west of Berlin + DST, east no-DST)
// and the poison values that must NEVER reach `Intl.DateTimeFormat`
// unguarded (RangeError there would white-screen every date render).
describe("profile-timezone matrix (#490)", () => {
// sample = 2026-04-18T14:30:00Z (see above).
it("renders Pacific/Auckland (UTC+12, NZST after the April DST end)", () => {
const fmt = makeFormatters("en", "Pacific/Auckland", "H24");
expect(fmt.time(sample)).toBe("02:30"); // next local day
expect(fmt.date(sample)).toBe("04/19/2026");
});
it("renders America/New_York (west of Berlin, EDT)", () => {
const fmt = makeFormatters("en", "America/New_York", "H24");
expect(fmt.time(sample)).toBe("10:30");
expect(fmt.date(sample)).toBe("04/18/2026");
});
it("renders Asia/Manila (UTC+8, no DST)", () => {
const fmt = makeFormatters("en", "Asia/Manila", "H24");
expect(fmt.time(sample)).toBe("22:30");
expect(fmt.date(sample)).toBe("04/18/2026");
});
it.each([["Mars/Olympus"], ["garbage"], [""], [undefined]])(
"poison zone %s never throws and falls back to Berlin",
(zone) => {
const fmt = makeFormatters("en", zone as string | undefined, "H24");
expect(fmt.time(sample)).toBe("16:30");
expect(fmt.date(sample)).toBe("04/18/2026");
expect(fmt.dateTime(sample)).toBe("04/18/2026, 16:30");
expect(fmt.dateWithWeekday(sample)).toContain("18");
expect(fmt.monthShort(sample)).toBe("Apr");
},
);
// The 23:30-Manila boundary pin: the rendered day label must equal the
// server's profile-tz day key for the same instant — the exact split
// #490 reported (Manila-grouped data under Berlin labels).
it("renders the same calendar day the server day-keys (Asia/Manila)", () => {
// YMD preference → the date renders as the ISO day key itself.
const manila = makeFormatters("en", "Asia/Manila", "H24", "YMD");
// 15:30 UTC = 23:30 Manila, still Jul 14 in Manila.
const lateManilaEvening = new Date("2026-07-14T15:30:00Z");
expect(manila.date(lateManilaEvening)).toBe("2026-07-14");
// 17:30 UTC = 01:30 Jul 15 Manila — Berlin (19:30 Jul 14) would
// label the previous day; the profile zone must win.
const pastManilaMidnight = new Date("2026-07-14T17:30:00Z");
expect(manila.date(pastManilaMidnight)).toBe("2026-07-15");
expect(manila.time(pastManilaMidnight)).toBe("01:30");
});
// Berlin DST pins — the zone maths must follow the IANA rules at the
// instant, never a cached offset.
it("renders across the Berlin 2026-03-29 spring-forward", () => {
const fmt = makeFormatters("de", "Europe/Berlin", "H24");
// 00:30 UTC = 01:30 CET (before the 02:00→03:00 jump).
expect(fmt.time(new Date("2026-03-29T00:30:00Z"))).toBe("01:30");
// 01:30 UTC = 03:30 CEST (the 02:xx hour does not exist).
expect(fmt.time(new Date("2026-03-29T01:30:00Z"))).toBe("03:30");
expect(fmt.date(new Date("2026-03-29T01:30:00Z"))).toBe("29.03.2026");
});
it("renders across the Berlin 2026-10-25 fall-back (doubled hour)", () => {
const fmt = makeFormatters("de", "Europe/Berlin", "H24");
// 00:30 UTC = 02:30 CEST (first pass through the doubled hour).
expect(fmt.time(new Date("2026-10-25T00:30:00Z"))).toBe("02:30");
// 01:30 UTC = 02:30 CET (second pass).
expect(fmt.time(new Date("2026-10-25T01:30:00Z"))).toBe("02:30");
expect(fmt.date(new Date("2026-10-25T01:30:00Z"))).toBe("25.10.2026");
});
});
});
// v1.25.3 — the time-format preference must reach every hour/minute renderer,
+125
View File
@@ -0,0 +1,125 @@
/**
* 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();
});
});