@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 64 lines 3.05 KB
Raw
1 import { describe, expect, it, vi } from "vite-plus/test";
2
3 import { createMessageTimeFormatter, formatMessageTimestamp } from "./messageTimestamp";
4
5 describe("formatMessageTimestamp", () => {
6 const now = new Date(2026, 10, 8, 20, 0).getTime();
7 const time = (date: Date) =>
8 date.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
9
10 it("shows only the time for today, and the day and month for any earlier day", () => {
11 const today = new Date(2026, 10, 8, 0, 5);
12 const yesterday = new Date(2026, 10, 7, 23, 55);
13 const lastYear = new Date(2025, 10, 5, 19, 17);
14 expect(formatMessageTimestamp(today.toISOString(), now)).toBe(time(today));
15 expect(formatMessageTimestamp(yesterday.toISOString(), now)).toBe(
16 `${yesterday.toLocaleDateString(undefined, { month: "numeric", day: "numeric" })} ${time(yesterday)}`,
17 );
18 expect(formatMessageTimestamp(lastYear.toISOString(), now)).toBe(
19 `${lastYear.toLocaleDateString(undefined, { month: "numeric", day: "numeric", year: "numeric" })} ${time(lastYear)}`,
20 );
21 });
22
23 it("follows a system time zone change", () => {
24 const previous = process.env.TZ;
25 vi.useFakeTimers({ now: 0 });
26 const sameDay = "2026-11-07T04:45:43.877Z";
27 const earlierInWeek = "2026-11-04T20:00:00.000Z";
28 const nowMs = Date.parse(sameDay);
29 const timeIn = (iso: string, timeZone: string) =>
30 new Date(iso).toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit", timeZone });
31 const dayIn = (iso: string, timeZone: string) =>
32 new Date(iso).toLocaleDateString(undefined, { month: "numeric", day: "numeric", timeZone });
33 try {
34 // Node re-resolves the default zone when TZ is assigned, like the OS zone changing.
35 process.env.TZ = "America/New_York";
36 const format24 = createMessageTimeFormatter(true);
37 expect(formatMessageTimestamp(sameDay, nowMs)).toBe(timeIn(sameDay, "America/New_York"));
38 expect(format24(sameDay, nowMs)).toBe("23:45");
39 process.env.TZ = "Asia/Bangkok";
40 vi.setSystemTime(Date.now() + 1_000);
41 expect(formatMessageTimestamp(sameDay, nowMs)).toBe(timeIn(sameDay, "Asia/Bangkok"));
42 expect(format24(sameDay, nowMs)).toBe("11:45");
43 expect(formatMessageTimestamp(earlierInWeek, nowMs)).toBe(
44 `${dayIn(earlierInWeek, "Asia/Bangkok")} ${timeIn(earlierInWeek, "Asia/Bangkok")}`,
45 );
46 } finally {
47 vi.useRealTimers();
48 if (previous === undefined) delete process.env.TZ;
49 else process.env.TZ = previous;
50 }
51 });
52 });
53
54 describe("createMessageTimeFormatter", () => {
55 const now = new Date(2026, 0, 1, 20, 0).getTime();
56
57 it("follows the system clock preference over the locale's hour cycle", () => {
58 const format24 = createMessageTimeFormatter(true, "en-US");
59 expect(format24(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toBe("15:07");
60 expect(format24(new Date(2026, 0, 1, 0, 7).toISOString(), now)).toBe("00:07");
61 const format12 = createMessageTimeFormatter(false, "en-GB");
62 expect(format12(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toMatch(/^3:07\spm$/i);
63 });
64 });