import { describe, expect, test } from "bun:test"

import { isView, windowFor } from "@/lib/task-views"

/** August 2026: the 3rd is a Monday, the 9th a Sunday, the month has 31 days. */
describe("windowFor", () => {
  test("a day is itself, both ends", () => {
    expect(windowFor("day", "2026-08-04")).toEqual({
      from: "2026-08-04",
      to: "2026-08-04",
    })
  })

  /**
   * Monday to Sunday, the ISO numbering the schedules are already written in,
   * so a weekly duty on day 1 lands on the first day of the week somebody is
   * looking at rather than the second.
   */
  test("a week runs Monday to Sunday, whichever day it is asked on", () => {
    for (const day of ["2026-08-03", "2026-08-06", "2026-08-09"]) {
      expect(windowFor("week", day)).toEqual({
        from: "2026-08-03",
        to: "2026-08-09",
      })
    }
  })

  test("a week crosses the end of a month without help", () => {
    expect(windowFor("week", "2026-09-01")).toEqual({
      from: "2026-08-31",
      to: "2026-09-06",
    })
  })

  test("a month ends on the day the month ends, not on the 30th", () => {
    expect(windowFor("month", "2026-08-04").to).toBe("2026-08-31")
    expect(windowFor("month", "2026-02-10").to).toBe("2026-02-28")
    expect(windowFor("month", "2028-02-10").to).toBe("2028-02-29")
    expect(windowFor("month", "2026-04-10").to).toBe("2026-04-30")
  })
})

/**
 * The view arrives from the url, where anybody can type anything. Nothing
 * downstream should have to wonder whether it is one of the three.
 */
describe("isView", () => {
  test("accepts the three and nothing else", () => {
    for (const v of ["day", "week", "month"]) expect(isView(v)).toBe(true)
    for (const v of ["Day", "year", "", null, undefined, 3, ["week"]]) {
      expect(isView(v)).toBe(false)
    }
  })
})
