Files
t3code/apps/web/src/timestampFormat.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

319 lines
11 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
import {
formatDayAwareTimestamp,
formatElapsedDurationLabel,
formatExpiresInLabel,
formatRelativeTime,
formatRelativeTimeLabel,
formatRelativeTimeUntil,
formatRelativeTimeUntilLabel,
formatShortTimestamp,
formatTimestamp,
formatUpcomingTimestamp,
getRelativeTimeState,
resolveTimestampLocale,
} from "./timestampFormat";
describe("resolveTimestampLocale", () => {
it("defers to the runtime default when the host reports no locale", () => {
expect(resolveTimestampLocale(null)).toBeUndefined();
expect(resolveTimestampLocale(undefined)).toBeUndefined();
expect(resolveTimestampLocale(" ")).toBeUndefined();
});
it("uses a BCP-47 tag reported by the host", () => {
expect(resolveTimestampLocale("en-GB")).toBe("en-GB");
});
it("defers to the runtime default rather than throwing on an unusable tag", () => {
// The desktop bridge normalizes POSIX identifiers before reporting them, so
// anything Intl still rejects here falls back instead of breaking every
// timestamp in the UI.
expect(resolveTimestampLocale("not a locale")).toBeUndefined();
expect(resolveTimestampLocale("en_GB")).toBeUndefined();
});
});
describe("formatShortTimestamp", () => {
afterEach(() => {
vi.unstubAllGlobals();
vi.resetModules();
});
it.each([
["en-GB", "15:44"],
["en-US", "3:44 PM"],
])("honors %s and the explicit hour-cycle settings", async (locale, localTime) => {
vi.stubGlobal("window", { desktopBridge: { getSystemLocale: () => locale } });
vi.resetModules();
const { formatShortTimestamp: format } = await import("./timestampFormat");
const date = new Date(2026, 3, 7, 15, 44).toISOString();
// ICU can separate the day period with a narrow no-break space.
expect(format(date, "locale").replace(/[  ]/g, " ")).toBe(localTime);
expect(format(date, "12-hour").replace(/[  ]/g, " ")).toMatch(/^3:44 [ap]m$/i);
expect(format(date, "24-hour")).toBe("15:44");
});
});
describe("resolveWeekStartsOn", () => {
it.each([
["en-US", 0],
["en-GB", 1],
["pl-PL", 1],
["ar-EG", 6],
])("starts the %s week on weekday %i", async (locale, weekday) => {
const { resolveWeekStartsOn } = await import("./timestampFormat");
expect(resolveWeekStartsOn(locale)).toBe(weekday);
});
it("leaves the default to the caller for a malformed locale", async () => {
const { resolveWeekStartsOn } = await import("./timestampFormat");
expect(resolveWeekStartsOn("not a locale")).toBeUndefined();
});
it("follows the locale the desktop host reports", async () => {
vi.stubGlobal("window", { desktopBridge: { getSystemLocale: () => "en-GB" } });
vi.resetModules();
const { weekStartsOn } = await import("./timestampFormat");
expect(weekStartsOn).toBe(1);
vi.unstubAllGlobals();
vi.resetModules();
});
});
describe("formatChatTimestampTooltip", () => {
afterEach(() => {
vi.restoreAllMocks();
vi.resetModules();
});
it.each(["de-DE", "it-IT"])("keeps the English date label in a %s runtime", async (locale) => {
const DateTimeFormat = Intl.DateTimeFormat;
vi.spyOn(Intl, "DateTimeFormat").mockImplementation(function (locales, options) {
return new DateTimeFormat(locales ?? locale, options);
});
vi.resetModules();
const { formatChatTimestampTooltip: format } = await import("./timestampFormat");
const date = new Date(2026, 5, 4, 14, 4).toISOString();
expect(format(date, "24-hour")).toBe("14:04, 4th June 2026");
});
});
describe("formatRelativeTimeUntilLabel", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
});
afterEach(() => {
vi.useRealTimers();
});
it("returns Expired when the instant is in the past", () => {
expect(formatRelativeTimeUntilLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
});
it("formats seconds remaining", () => {
expect(formatRelativeTimeUntilLabel("2026-04-07T12:00:45.000Z")).toBe("45s left");
});
it("formats minutes remaining", () => {
expect(formatRelativeTimeUntilLabel("2026-04-07T12:15:00.000Z")).toBe("15m left");
});
it("formats hours remaining", () => {
expect(formatRelativeTimeUntilLabel("2026-04-07T18:00:00.000Z")).toBe("6h left");
});
});
describe("formatExpiresInLabel", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
});
afterEach(() => {
vi.useRealTimers();
});
it("returns Expired when the instant is in the past", () => {
expect(formatExpiresInLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
});
it("uses sub-minute second count", () => {
expect(formatExpiresInLabel("2026-04-07T12:00:45.000Z")).toBe("Expires in 45s");
});
it("uses minutes and seconds under one hour", () => {
expect(formatExpiresInLabel("2026-04-07T12:04:12.000Z")).toBe("Expires in 4m 12s");
expect(formatExpiresInLabel("2026-04-07T12:15:00.000Z")).toBe("Expires in 15m");
});
it("uses hours with minute and second remainder", () => {
expect(formatExpiresInLabel("2026-04-07T14:02:03.000Z")).toBe("Expires in 2h 2m 3s");
expect(formatExpiresInLabel("2026-04-07T18:00:00.000Z")).toBe("Expires in 6h");
});
});
describe("formatDayAwareTimestamp", () => {
// Instants are built with the local-time Date constructor so the
// calendar-day boundaries hold in any test timezone or locale.
const iso = (y: number, monthIndex: number, d: number, h: number, mi: number) =>
new Date(y, monthIndex, d, h, mi).toISOString();
const now = new Date(2026, 7, 14, 12, 0).getTime();
const time = (isoDate: string) => formatShortTimestamp(isoDate, "12-hour");
it("shows time only for today", () => {
const messageAt = iso(2026, 7, 14, 9, 30);
expect(formatDayAwareTimestamp(messageAt, "12-hour", now)).toBe(time(messageAt));
});
it("labels the previous calendar day as yesterday even when under 24h old", () => {
const messageAt = iso(2026, 7, 13, 23, 30);
const justPastMidnight = new Date(2026, 7, 14, 0, 30).getTime();
expect(formatDayAwareTimestamp(messageAt, "12-hour", justPastMidnight)).toBe(
`yesterday at ${time(messageAt)}`,
);
});
it("prefixes older same-year messages with the numeric date", () => {
const messageAt = iso(2026, 7, 12, 12, 34);
const datePart = new Intl.DateTimeFormat(undefined, {
month: "numeric",
day: "numeric",
}).format(new Date(messageAt));
expect(formatDayAwareTimestamp(messageAt, "12-hour", now)).toBe(
`${datePart} ${time(messageAt)}`,
);
});
it("includes the year once the calendar year differs", () => {
const messageAt = iso(2025, 11, 31, 18, 0);
const datePart = new Intl.DateTimeFormat(undefined, {
month: "numeric",
day: "numeric",
year: "numeric",
}).format(new Date(messageAt));
expect(formatDayAwareTimestamp(messageAt, "12-hour", now)).toBe(
`${datePart} ${time(messageAt)}`,
);
});
it("uses the host locale for both the numeric date and wall-clock time", async () => {
vi.stubGlobal("window", {
desktopBridge: { getSystemLocale: () => "en-GB" },
});
vi.resetModules();
const { formatDayAwareTimestamp: formatWithHostLocale } = await import("./timestampFormat");
const messageAt = iso(2026, 7, 12, 15, 44);
expect(formatWithHostLocale(messageAt, "locale", now)).toBe("12/08 15:44");
vi.unstubAllGlobals();
});
it("returns an empty string for invalid input", () => {
expect(formatDayAwareTimestamp("not-a-date", "12-hour", now)).toBe("");
});
});
describe("formatUpcomingTimestamp", () => {
const now = new Date(2026, 7, 14, 12, 0).getTime();
it.each([
[14, ""],
[15, "tomorrow at "],
[13, "yesterday at "],
])("keeps the reset day visible for day %i", (day, prefix) => {
const resetAt = new Date(2026, 7, day, 14, 30).toISOString();
expect(formatUpcomingTimestamp(resetAt, "12-hour", now)).toBe(
`${prefix}${formatShortTimestamp(resetAt, "12-hour")}`,
);
});
it("preserves the date of an older reset in the transcript", () => {
const resetAt = new Date(2026, 7, 12, 14, 30).toISOString();
expect(formatUpcomingTimestamp(resetAt, "12-hour", now)).toBe(
formatDayAwareTimestamp(resetAt, "12-hour", now),
);
});
});
describe("invalid timestamp inputs", () => {
it("returns an empty timestamp instead of throwing", () => {
expect(formatTimestamp("not-a-date", "12-hour")).toBe("");
});
it("returns an empty short timestamp instead of throwing", () => {
expect(formatShortTimestamp("not-a-date", "12-hour")).toBe("");
});
it("returns an empty relative time label instead of a NaN label", () => {
expect(formatRelativeTime("not-a-date")).toBeNull();
expect(formatRelativeTimeLabel("not-a-date")).toBe("");
});
it("distinguishes missing and invalid relative time state", () => {
expect(getRelativeTimeState(null)).toEqual({ status: "missing" });
expect(getRelativeTimeState("not-a-date")).toEqual({ status: "invalid" });
});
it("returns an empty elapsed duration instead of a NaN label", () => {
expect(formatElapsedDurationLabel("not-a-date")).toBe("");
});
it("returns an empty relative time until label instead of a NaN label", () => {
expect(formatRelativeTimeUntil("not-a-date")).toBeNull();
expect(formatRelativeTimeUntilLabel("not-a-date")).toBe("");
});
it("returns an empty expires-in label instead of a NaN label", () => {
expect(formatExpiresInLabel("not-a-date")).toBe("");
});
});
describe("getRelativeTimeState", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
});
afterEach(() => {
vi.useRealTimers();
});
it("returns relative parts for valid timestamps", () => {
expect(getRelativeTimeState("2026-04-07T11:45:00.000Z")).toEqual({
status: "relative",
value: "15m",
suffix: "ago",
});
});
});
describe("formatElapsedDurationLabel", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
});
afterEach(() => {
vi.useRealTimers();
});
it("returns just now when the instant is current or in the future", () => {
expect(formatElapsedDurationLabel("2026-04-07T12:00:00.000Z")).toBe("just now");
expect(formatElapsedDurationLabel("2026-04-07T12:01:00.000Z")).toBe("just now");
});
it("formats seconds, minutes, hours, and days", () => {
expect(formatElapsedDurationLabel("2026-04-07T11:59:45.000Z")).toBe("15s");
expect(formatElapsedDurationLabel("2026-04-07T11:45:00.000Z")).toBe("15m");
expect(formatElapsedDurationLabel("2026-04-07T06:00:00.000Z")).toBe("6h");
expect(formatElapsedDurationLabel("2026-04-03T12:00:00.000Z")).toBe("4d");
});
});