Files
multica/packages/views/common/format-in-time-zone.test.ts
YYClaw 465546b83b feat(autopilots): redesign the autopilot schedule editor (#5457)
Replace the free-form trigger-config form with a structured schedule
editor built on an orthogonal cron model: separate frequency, time, and
day-of-week/day-of-month dimensions map to and from cron expressions via
a dedicated grammar and mapping layer, with validation and a
human-readable describe() summary. The grammar suite drives the editor
against a combinatorially generated corpus of 51,755 distinct cron
expressions - every token form of every field, crossed - each judged
against a reference robfig/cron v3 parser.

Add a server-side /autopilot/cron-preview endpoint (plus schema and
React Query hook) so the editor shows upcoming run times, and echo
wildcard-carrying cron lists correctly instead of collapsing them.

Supporting pieces: timezone-aware formatting helper, segmented-toggle
and debounced-value utilities, a reworked time-input, and refreshed
en/ja/ko/zh-Hans locale strings.
2026-07-17 16:44:31 +08:00

42 lines
1.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { formatInTimeZone } from "./format-in-time-zone";
// An autopilot's next run is printed on the schedule's clock, not the reader's:
// a trigger that says "18:00 (America/Los_Angeles)" must not contradict itself by
// showing its next run as 09:00 to a reader in UTC+8. A run that already happened
// is an instant in the reader's day, and passes no zone.
describe("formatInTimeZone", () => {
// 2026-07-14T01:00:00Z is 18:00 of the 13th in Los Angeles (PDT, UTC-7).
const iso = "2026-07-14T01:00:00Z";
it("renders the instant in the given timezone", () => {
const out = formatInTimeZone(iso, "America/Los_Angeles", "en-US");
expect(out).toContain("13");
expect(out).toMatch(/6:00\s?PM|18:00/);
});
it("renders the same instant differently in another timezone", () => {
const out = formatInTimeZone(iso, "Asia/Shanghai", "en-US");
expect(out).toContain("14");
expect(out).toMatch(/9:00\s?AM|09:00/);
});
it("falls back to local time for a zone this runtime does not know", () => {
// Wrong by an offset, but still a time of day — which a raw ISO string is not.
expect(formatInTimeZone(iso, "Not/AZone", "en-US")).not.toBe("");
expect(formatInTimeZone(iso, "Not/AZone", "en-US")).not.toBe(iso);
});
it("keeps the reader's locale when it falls back over a bad zone", () => {
// The zone is what failed, not the language: dropping to the runtime's default
// locale here would hand a zh-Hans reader an English date.
expect(formatInTimeZone(iso, "Not/AZone", "zh-CN")).toMatch(/月/);
});
it("hands back an unreadable timestamp instead of throwing", () => {
// Intl throws on an invalid Date. A drifted backend timestamp must degrade to
// text, not take the whole detail page down with it.
expect(formatInTimeZone("not-a-date", "UTC", "en-US")).toBe("not-a-date");
});
});