|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | | -import { buildWeeklyRange, toISODate, parseLocalDate } from "./date-range.js"; |
| 2 | +import { buildWeeklyRange, buildCurrentWeekRange, toISODate, parseLocalDate } from "./date-range.js"; |
3 | 3 |
|
4 | 4 | // Helper: verify the range covers exactly 7 calendar days. |
5 | 5 | // In non-DST zones this is 7 * 86400000 - 1 ms. |
@@ -366,6 +366,68 @@ describe("buildWeeklyRange", () => { |
366 | 366 | }); |
367 | 367 | }); |
368 | 368 |
|
| 369 | +// ------------------------------------------------------------------- |
| 370 | +// buildCurrentWeekRange |
| 371 | +// ------------------------------------------------------------------- |
| 372 | + |
| 373 | +describe("buildCurrentWeekRange", () => { |
| 374 | + it("returns current ISO week range for the given date (UTC)", () => { |
| 375 | + // 2026-04-03 is Friday W14 -> current week W14: Mar 30 (Mon) - Apr 3 (today) |
| 376 | + const now = new Date("2026-04-03T12:00:00Z"); |
| 377 | + const range = buildCurrentWeekRange(now); |
| 378 | + |
| 379 | + expect(toISODate(range.from)).toBe("2026-03-30"); |
| 380 | + expect(toISODate(range.to)).toBe("2026-04-03"); |
| 381 | + }); |
| 382 | + |
| 383 | + it("on Monday, from and to are both Monday", () => { |
| 384 | + // 2026-03-30 is Monday W14 |
| 385 | + const now = new Date("2026-03-30T12:00:00Z"); |
| 386 | + const range = buildCurrentWeekRange(now); |
| 387 | + |
| 388 | + expect(toISODate(range.from)).toBe("2026-03-30"); |
| 389 | + expect(toISODate(range.to)).toBe("2026-03-30"); |
| 390 | + }); |
| 391 | + |
| 392 | + it("on Sunday, covers full Mon-Sun", () => { |
| 393 | + // 2026-04-05 is Sunday W14 |
| 394 | + const now = new Date("2026-04-05T12:00:00Z"); |
| 395 | + const range = buildCurrentWeekRange(now); |
| 396 | + |
| 397 | + expect(toISODate(range.from)).toBe("2026-03-30"); |
| 398 | + expect(toISODate(range.to)).toBe("2026-04-05"); |
| 399 | + }); |
| 400 | + |
| 401 | + it("respects Asia/Tokyo timezone", () => { |
| 402 | + // 2026-04-04 08:00 JST = 2026-04-03 23:00 UTC |
| 403 | + // JST local: Apr 4 (Sat W14) -> current week: Mar 30 (Mon) - Apr 4 (today) |
| 404 | + const now = new Date("2026-04-03T23:00:00Z"); |
| 405 | + const range = buildCurrentWeekRange(now, "Asia/Tokyo"); |
| 406 | + |
| 407 | + expect(toISODate(range.from, "Asia/Tokyo")).toBe("2026-03-30"); |
| 408 | + expect(toISODate(range.to, "Asia/Tokyo")).toBe("2026-04-04"); |
| 409 | + }); |
| 410 | + |
| 411 | + it("respects America/New_York timezone", () => { |
| 412 | + // 2026-04-03 20:00 EDT = 2026-04-04 00:00 UTC |
| 413 | + // NYC local: Apr 3 (Fri W14) -> current week: Mar 30 (Mon) - Apr 3 (today) |
| 414 | + const now = new Date("2026-04-04T00:00:00Z"); |
| 415 | + const range = buildCurrentWeekRange(now, "America/New_York"); |
| 416 | + |
| 417 | + expect(toISODate(range.from, "America/New_York")).toBe("2026-03-30"); |
| 418 | + expect(toISODate(range.to, "America/New_York")).toBe("2026-04-03"); |
| 419 | + }); |
| 420 | + |
| 421 | + it("year boundary: first week of 2026", () => { |
| 422 | + // 2026-01-01 is Thursday W1. Current week: Dec 29 (Mon) - Jan 1 (today) |
| 423 | + const now = new Date("2026-01-01T12:00:00Z"); |
| 424 | + const range = buildCurrentWeekRange(now); |
| 425 | + |
| 426 | + expect(toISODate(range.from)).toBe("2025-12-29"); |
| 427 | + expect(toISODate(range.to)).toBe("2026-01-01"); |
| 428 | + }); |
| 429 | +}); |
| 430 | + |
369 | 431 | // ------------------------------------------------------------------- |
370 | 432 | // toISODate |
371 | 433 | // ------------------------------------------------------------------- |
|
0 commit comments