The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | formatChatTimestampTooltip, |
| 5 | formatDayAwareTimestamp, |
| 6 | formatUpcomingTimestamp, |
| 7 | } from "./timestampFormat"; |
| 8 | |
| 9 | // Node re-resolves the default zone when TZ is assigned, like a renderer after the OS zone changes. |
| 10 | function useTimeZone(timeZone: string): Disposable { |
| 11 | const previous = process.env.TZ; |
| 12 | process.env.TZ = timeZone; |
| 13 | return { |
| 14 | [Symbol.dispose]: () => { |
| 15 | if (previous === undefined) delete process.env.TZ; |
| 16 | else process.env.TZ = previous; |
| 17 | }, |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | describe("timestamps in an explicit time zone", () => { |
| 22 | // 23:00 UTC on July 1 is 7 PM on July 1 in New York, whatever the host zone. |
| 23 | const now = Date.parse("2026-07-01T23:00:00.000Z"); |
| 24 | const zone = "America/New_York"; |
| 25 | |
| 26 | it("picks relative days from the calendar in that zone", () => { |
| 27 | expect(formatUpcomingTimestamp("2026-07-02T00:30:00.000Z", "12-hour", now, zone)).not.toMatch( |
| 28 | /tomorrow/, |
| 29 | ); |
| 30 | expect(formatUpcomingTimestamp("2026-07-02T05:30:00.000Z", "12-hour", now, zone)).toMatch( |
| 31 | /^tomorrow at /, |
| 32 | ); |
| 33 | expect(formatDayAwareTimestamp("2026-07-01T03:30:00.000Z", "12-hour", now, zone)).toMatch( |
| 34 | /^yesterday at /, |
| 35 | ); |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | describe("timestamps after the system time zone changes", () => { |
| 40 | // Built fresh with an explicit zone, so expectations follow the host locale but not a stale zone. |
| 41 | const timeIn = (iso: string, timeZone: string) => |
| 42 | new Intl.DateTimeFormat(undefined, { |
| 43 | hour: "numeric", |
| 44 | minute: "2-digit", |
| 45 | hour12: true, |
| 46 | timeZone, |
| 47 | }).format(new Date(iso)); |
| 48 | const dayIn = (iso: string, timeZone: string) => |
| 49 | new Intl.DateTimeFormat(undefined, { month: "numeric", day: "numeric", timeZone }).format( |
| 50 | new Date(iso), |
| 51 | ); |
| 52 | |
| 53 | it("renders the gutter label in the new zone", () => { |
| 54 | const sameDay = "2026-11-07T04:45:43.877Z"; |
| 55 | const earlierInWeek = "2026-11-04T20:00:00.000Z"; |
| 56 | const now = Date.parse(sameDay); |
| 57 | using _zone = useTimeZone("America/New_York"); |
| 58 | expect([ |
| 59 | formatDayAwareTimestamp(sameDay, "12-hour", now), |
| 60 | formatDayAwareTimestamp(earlierInWeek, "12-hour", now), |
| 61 | ]).toEqual([ |
| 62 | timeIn(sameDay, "America/New_York"), |
| 63 | `${dayIn(earlierInWeek, "America/New_York")} ${timeIn(earlierInWeek, "America/New_York")}`, |
| 64 | ]); |
| 65 | |
| 66 | process.env.TZ = "Asia/Bangkok"; |
| 67 | expect([ |
| 68 | formatDayAwareTimestamp(sameDay, "12-hour", now), |
| 69 | formatDayAwareTimestamp(earlierInWeek, "12-hour", now), |
| 70 | ]).toEqual([ |
| 71 | timeIn(sameDay, "Asia/Bangkok"), |
| 72 | `${dayIn(earlierInWeek, "Asia/Bangkok")} ${timeIn(earlierInWeek, "Asia/Bangkok")}`, |
| 73 | ]); |
| 74 | }); |
| 75 | |
| 76 | it("renders the tooltip in the new zone", () => { |
| 77 | const monthEnd = "2026-10-31T20:00:00.000Z"; |
| 78 | using _zone = useTimeZone("America/New_York"); |
| 79 | expect(formatChatTimestampTooltip(monthEnd, "12-hour")).toBe( |
| 80 | `${timeIn(monthEnd, "America/New_York")}, 31st October 2026`, |
| 81 | ); |
| 82 | |
| 83 | process.env.TZ = "Asia/Bangkok"; |
| 84 | expect(formatChatTimestampTooltip(monthEnd, "12-hour")).toBe( |
| 85 | `${timeIn(monthEnd, "Asia/Bangkok")}, 1st November 2026`, |
| 86 | ); |
| 87 | }); |
| 88 | }); |