Manual timezone is the single source of local-time truth (task 7e173881) (#285)
Co-authored-by: dev_agent <dev_agent@cubecraftlabs.com> Co-committed-by: dev_agent <dev_agent@cubecraftlabs.com>
This commit was merged in pull request #285.
This commit is contained in:
@@ -74,6 +74,10 @@ async function routeFetch(url: string, init: RequestInit = {}): Promise<Response
|
||||
overrides = overrides.filter((o) => o.id !== Number(deleteMatch[1]));
|
||||
return jsonResponse({ status: "ok" });
|
||||
}
|
||||
// The shell loads the application timezone first ("" = none published).
|
||||
if (path.endsWith("/admin/settings")) {
|
||||
return jsonResponse({ timezone: "", effective_timezone: "" });
|
||||
}
|
||||
if (path.endsWith("/admin/children")) return jsonResponse({ children: CHILDREN });
|
||||
const patternsMatch = /\/children\/(\d+)\/presence-patterns$/.exec(path);
|
||||
if (patternsMatch) {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import ScheduleTab from "./ScheduleTab";
|
||||
import type { AdminChild } from "../api/definitions";
|
||||
import type { PresenceOverride, PresencePattern } from "../api/presence";
|
||||
import { storeTokens } from "../auth/oidc";
|
||||
import { AppTimezoneProvider } from "../time/AppTimezone";
|
||||
import { cycleWeekIndex, monthDates, weekdayOf } from "./cycle";
|
||||
|
||||
function child(id: number, display_name: string, sort_order: number, is_active = true): AdminChild {
|
||||
@@ -655,3 +656,55 @@ describe("ScheduleTab", () => {
|
||||
expect(await screen.findByTestId("pattern-1")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ScheduleTab today in the stored application timezone", () => {
|
||||
async function renderWithZone(timezone: string, effective_timezone = timezone) {
|
||||
fetchMock = vi.fn(async (url: string, init?: RequestInit) =>
|
||||
String(url).endsWith("/api/v1/admin/settings")
|
||||
? jsonResponse({ timezone, effective_timezone })
|
||||
: routeFetch(url, init),
|
||||
);
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
render(
|
||||
<AppTimezoneProvider>
|
||||
<ScheduleTab />
|
||||
</AppTimezoneProvider>,
|
||||
);
|
||||
await screen.findByTestId("pattern-1");
|
||||
}
|
||||
|
||||
beforeAll(() => {
|
||||
vi.stubEnv("TZ", "UTC");
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
sessionStorage.clear();
|
||||
storeTokens({ access_token: "at-123", expires_in: 600 });
|
||||
patterns = { 1: [DECLAN_HOME], 2: [MAEVE_THU_FRI, MAEVE_WEEKENDS], 3: [] };
|
||||
writeFailure = null;
|
||||
// 13:00 UTC on Sep 23 is already 01:00 on Sep 24 in Auckland.
|
||||
vi.useFakeTimers({ toFake: ["Date"], now: new Date("2026-09-23T13:00:00Z") });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("marks today in the stored zone", async () => {
|
||||
await renderWithZone("Pacific/Auckland");
|
||||
expect(day("2026-09-24").className).toContain("schedule-day--today");
|
||||
expect(day("2026-09-23").className).not.toContain("schedule-day--today");
|
||||
});
|
||||
|
||||
it("an empty stored zone marks today in the API host's published zone", async () => {
|
||||
await renderWithZone("", "Pacific/Auckland");
|
||||
expect(day("2026-09-24").className).toContain("schedule-day--today");
|
||||
expect(day("2026-09-23").className).not.toContain("schedule-day--today");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,7 +20,6 @@ import {
|
||||
import {
|
||||
cycleLabel,
|
||||
dayState,
|
||||
localTodayIso,
|
||||
monthDates,
|
||||
monthStart,
|
||||
patternDaysLabel,
|
||||
@@ -38,6 +37,8 @@ import {
|
||||
} from "./glyphs";
|
||||
import OverrideEditor from "./OverrideEditor";
|
||||
import PatternEditor from "./PatternEditor";
|
||||
import { useAppTimezone } from "../time/AppTimezone";
|
||||
import { todayIn } from "../time/zone";
|
||||
import "./ScheduleTab.css";
|
||||
|
||||
/** ADMIN-SPEC §1: every scroll column ends with 92px so content clears the tab bar. */
|
||||
@@ -324,11 +325,13 @@ function OverrideRow({ override, childName, deletion, onAskDelete, onKeep, onDel
|
||||
}
|
||||
|
||||
export interface ScheduleTabProps {
|
||||
/** "YYYY-MM-DD"; defaults to the viewer's local date. */
|
||||
/** "YYYY-MM-DD"; defaults to today in the application timezone. */
|
||||
today?: string;
|
||||
}
|
||||
|
||||
export default function ScheduleTab({ today = localTodayIso() }: ScheduleTabProps) {
|
||||
export default function ScheduleTab({ today: todayProp }: ScheduleTabProps) {
|
||||
const timeZone = useAppTimezone();
|
||||
const today = todayProp ?? todayIn(timeZone);
|
||||
const [state, setState] = useState<LoadState>({ kind: "loading" });
|
||||
const [attempt, setAttempt] = useState(0);
|
||||
const [selectedId, setSelectedId] = useState<number | null>(null);
|
||||
|
||||
@@ -55,13 +55,6 @@ export function cycleWeekIndex(anchorDate: string, date: string, cycleLengthWeek
|
||||
return mod(Math.floor(days / 7), cycleLengthWeeks);
|
||||
}
|
||||
|
||||
/** The viewer's local calendar date. */
|
||||
export function localTodayIso(now: Date = new Date()): string {
|
||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(now.getDate()).padStart(2, "0");
|
||||
return `${now.getFullYear()}-${month}-${day}`;
|
||||
}
|
||||
|
||||
/** "YYYY-MM-01" of the month containing `iso`, shifted by `delta` months. */
|
||||
export function monthStart(iso: string, delta = 0): string {
|
||||
const [year, month] = iso.split("-").map(Number);
|
||||
|
||||
Reference in New Issue
Block a user