mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-01 15:48:23 -05:00
Move app to apps/web-react in pnpm workspace layout
This commit is contained in:
86
apps/web-react/app/utils/dates.test.ts
Normal file
86
apps/web-react/app/utils/dates.test.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { afterAll, beforeAll, describe, expect, test } from "vitest";
|
||||
import {
|
||||
getDateAtNextFullHour,
|
||||
weekNumberToDate,
|
||||
weekNumberToDateRange,
|
||||
} from "./dates";
|
||||
|
||||
describe("getDateAtNextFullHour", () => {
|
||||
test("returns a date sitting exactly on a full hour (no leftover minutes/seconds/milliseconds)", () => {
|
||||
const result = getDateAtNextFullHour(new Date(2024, 0, 1, 14, 30, 0, 500));
|
||||
|
||||
expect(result).toEqual(new Date(2024, 0, 1, 15, 0, 0, 0));
|
||||
});
|
||||
|
||||
test("advances to the next hour when only seconds/milliseconds are past the hour", () => {
|
||||
const result = getDateAtNextFullHour(new Date(2024, 0, 1, 14, 0, 30, 0));
|
||||
|
||||
expect(result).toEqual(new Date(2024, 0, 1, 15, 0, 0, 0));
|
||||
});
|
||||
|
||||
test("keeps the same hour when already exactly on a full hour", () => {
|
||||
const result = getDateAtNextFullHour(new Date(2024, 0, 1, 14, 0, 0, 0));
|
||||
|
||||
expect(result).toEqual(new Date(2024, 0, 1, 14, 0, 0, 0));
|
||||
});
|
||||
});
|
||||
|
||||
describe("weekNumberToDate", () => {
|
||||
// Force a timezone west of UTC so the assertion is deterministic regardless
|
||||
// of where the test happens to run (the bug only manifests west of UTC).
|
||||
const originalTimezone = process.env.TZ;
|
||||
beforeAll(() => {
|
||||
process.env.TZ = "America/Los_Angeles";
|
||||
});
|
||||
afterAll(() => {
|
||||
process.env.TZ = originalTimezone;
|
||||
});
|
||||
|
||||
test("returns the Monday of the ISO week regardless of server timezone", () => {
|
||||
// ISO week 1 of 2024 starts on Monday 2024-01-01
|
||||
const start = weekNumberToDate({ week: 1, year: 2024 });
|
||||
|
||||
expect(start.toISOString().slice(0, 10)).toBe("2024-01-01");
|
||||
});
|
||||
|
||||
test("returns the Sunday of the ISO week regardless of server timezone", () => {
|
||||
// ISO week 1 of 2024 ends on Sunday 2024-01-07
|
||||
const end = weekNumberToDate({ week: 1, year: 2024, position: "end" });
|
||||
|
||||
expect(end.toISOString().slice(0, 10)).toBe("2024-01-07");
|
||||
});
|
||||
});
|
||||
|
||||
describe("weekNumberToDateRange", () => {
|
||||
// Force a timezone west of UTC observing DST so the assertion is deterministic
|
||||
// regardless of where the test happens to run.
|
||||
const originalTimezone = process.env.TZ;
|
||||
beforeAll(() => {
|
||||
process.env.TZ = "America/New_York";
|
||||
});
|
||||
afterAll(() => {
|
||||
process.env.TZ = originalTimezone;
|
||||
});
|
||||
|
||||
const SEVEN_DAYS_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
test("spans exactly seven days even when the week contains a DST transition", () => {
|
||||
// US spring-forward 2025 happened on Sunday 2025-03-09, which falls inside
|
||||
// ISO week 10 of 2025 (Mon 2025-03-03 .. Mon 2025-03-10).
|
||||
const { startTime, endTime } = weekNumberToDateRange({
|
||||
week: 10,
|
||||
year: 2025,
|
||||
});
|
||||
|
||||
expect(endTime.getTime() - startTime.getTime()).toBe(SEVEN_DAYS_MS);
|
||||
});
|
||||
|
||||
test("spans exactly seven days for an ordinary week", () => {
|
||||
const { startTime, endTime } = weekNumberToDateRange({
|
||||
week: 20,
|
||||
year: 2025,
|
||||
});
|
||||
|
||||
expect(endTime.getTime() - startTime.getTime()).toBe(SEVEN_DAYS_MS);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user