date.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { describe, expect, test } from "bun:test"
  2. import { getWeekBounds, getMonthlyBounds } from "../src/util/date"
  3. describe("util.date.getWeekBounds", () => {
  4. test("returns a Monday-based week for Sunday dates", () => {
  5. const date = new Date("2026-01-18T12:00:00Z")
  6. const bounds = getWeekBounds(date)
  7. expect(bounds.start.toISOString()).toBe("2026-01-12T00:00:00.000Z")
  8. expect(bounds.end.toISOString()).toBe("2026-01-19T00:00:00.000Z")
  9. })
  10. test("returns a seven day window", () => {
  11. const date = new Date("2026-01-14T12:00:00Z")
  12. const bounds = getWeekBounds(date)
  13. const span = bounds.end.getTime() - bounds.start.getTime()
  14. expect(span).toBe(7 * 24 * 60 * 60 * 1000)
  15. })
  16. })
  17. describe("util.date.getMonthlyBounds", () => {
  18. test("resets on subscription day mid-month", () => {
  19. const now = new Date("2026-03-20T10:00:00Z")
  20. const subscribed = new Date("2026-01-15T08:00:00Z")
  21. const bounds = getMonthlyBounds(now, subscribed)
  22. expect(bounds.start.toISOString()).toBe("2026-03-15T08:00:00.000Z")
  23. expect(bounds.end.toISOString()).toBe("2026-04-15T08:00:00.000Z")
  24. })
  25. test("before subscription day in current month uses previous month anchor", () => {
  26. const now = new Date("2026-03-10T10:00:00Z")
  27. const subscribed = new Date("2026-01-15T08:00:00Z")
  28. const bounds = getMonthlyBounds(now, subscribed)
  29. expect(bounds.start.toISOString()).toBe("2026-02-15T08:00:00.000Z")
  30. expect(bounds.end.toISOString()).toBe("2026-03-15T08:00:00.000Z")
  31. })
  32. test("clamps day for short months", () => {
  33. const now = new Date("2026-03-01T10:00:00Z")
  34. const subscribed = new Date("2026-01-31T12:00:00Z")
  35. const bounds = getMonthlyBounds(now, subscribed)
  36. expect(bounds.start.toISOString()).toBe("2026-02-28T12:00:00.000Z")
  37. expect(bounds.end.toISOString()).toBe("2026-03-31T12:00:00.000Z")
  38. })
  39. test("handles subscription on the 1st", () => {
  40. const now = new Date("2026-04-15T00:00:00Z")
  41. const subscribed = new Date("2026-01-01T00:00:00Z")
  42. const bounds = getMonthlyBounds(now, subscribed)
  43. expect(bounds.start.toISOString()).toBe("2026-04-01T00:00:00.000Z")
  44. expect(bounds.end.toISOString()).toBe("2026-05-01T00:00:00.000Z")
  45. })
  46. test("exactly on the reset boundary uses current period", () => {
  47. const now = new Date("2026-03-15T08:00:00Z")
  48. const subscribed = new Date("2026-01-15T08:00:00Z")
  49. const bounds = getMonthlyBounds(now, subscribed)
  50. expect(bounds.start.toISOString()).toBe("2026-03-15T08:00:00.000Z")
  51. expect(bounds.end.toISOString()).toBe("2026-04-15T08:00:00.000Z")
  52. })
  53. test("february to march with day 30 subscription", () => {
  54. const now = new Date("2026-02-15T06:00:00Z")
  55. const subscribed = new Date("2025-12-30T06:00:00Z")
  56. const bounds = getMonthlyBounds(now, subscribed)
  57. expect(bounds.start.toISOString()).toBe("2026-01-30T06:00:00.000Z")
  58. expect(bounds.end.toISOString()).toBe("2026-02-28T06:00:00.000Z")
  59. })
  60. })