g1t/apps/status/src/time.test.ts
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { StatusIncident } from "@g1t/contracts"; |
| 5 | |
| 6 | import { renderIncident } from "./render.ts"; |
| 7 | import { readZone, stampIn, timeIn, validZone, zoneAbbr } from "./time.ts"; |
| 8 | |
| 9 | const AT = "2026-10-06T07:25:00.000Z"; |
| 10 | |
| 11 | test("the reader's zone: their cookie, else Cloudflare's guess, else UTC", () => { |
| 12 | assert.equal(readZone(null, "America/Los_Angeles"), "America/Los_Angeles"); |
| 13 | assert.equal(readZone("a=1; g1t_tz=Europe%2FBerlin", "America/Los_Angeles"), "Europe/Berlin"); |
| 14 | assert.equal(readZone("g1t_tz=Not%2FA_Zone", "Asia/Tokyo"), "Asia/Tokyo"); |
| 15 | assert.equal(readZone(null, undefined), "UTC"); |
| 16 | assert.equal(readZone(null, "nonsense"), "UTC"); |
| 17 | assert.equal(validZone("UTC"), true); |
| 18 | assert.equal(validZone(42), false); |
| 19 | }); |
| 20 | |
| 21 | test("times carry their zone's abbreviation", () => { |
| 22 | const d = new Date(AT); |
| 23 | assert.equal(zoneAbbr(d, "UTC"), "UTC"); |
| 24 | assert.equal(zoneAbbr(d, "America/Los_Angeles"), "PDT"); |
| 25 | assert.equal(zoneAbbr(d, "Europe/Berlin"), "CEST"); |
| 26 | assert.equal(zoneAbbr(d, "Europe/London"), "BST"); |
| 27 | assert.equal(zoneAbbr(d, "Asia/Tokyo"), "UTC+9"); |
| 28 | assert.equal(timeIn(AT, "UTC"), "6 Oct 2026, 07:25 UTC"); |
| 29 | assert.equal(timeIn(AT, "America/Los_Angeles"), "6 Oct 2026, 00:25 PDT"); |
| 30 | assert.equal(timeIn("2026-10-06T03:13:32.776Z", "America/Los_Angeles"), "5 Oct 2026, 20:13 PDT"); |
| 31 | assert.equal(stampIn(AT, "Asia/Kolkata"), "6 Oct 12:55 UTC+5:30"); |
| 32 | }); |
| 33 | |
| 34 | test("the status page says times in the reader's zone, with UTC in datetime and on hover", () => { |
| 35 | const incident: StatusIncident = { |
| 36 | id: "abc", |
| 37 | title: "Pushes failing", |
| 38 | impact: "down", |
| 39 | status: "resolved", |
| 40 | components: ["git"], |
| 41 | component_impacts: [{ key: "git", impact: "major_outage" }], |
| 42 | started_at: AT, |
| 43 | resolved_at: "2026-10-06T08:00:00.000Z", |
| 44 | url: "https://status.g1t.sh/incidents/abc", |
| 45 | postmortem_published_at: null, |
| 46 | updates: [{ id: "u1", at: AT, status: "investigating", text: "Looking." }], |
| 47 | }; |
| 48 | const options = { siteUrl: "https://g1t.sh", supportUrl: "https://g1t.sh/support", ogImage: "", selfUrl: "https://status.g1t.sh/incidents/abc", now: new Date(AT) }; |
| 49 | const local = renderIncident(incident, null, new Map([["git", "Git"]]), { ...options, zone: "America/Los_Angeles" }); |
| 50 | assert.match(local, /<time datetime="2026-10-06T07:25:00.000Z" title="6 Oct 2026, 07:25 UTC">6 Oct 2026, 00:25 PDT<\/time>/); |
| 51 | const utc = renderIncident(incident, null, new Map([["git", "Git"]]), options); |
| 52 | assert.match(utc, />6 Oct 2026, 07:25 UTC<\/time>/, "one page's zone does not leak into the next"); |
| 53 | }); |