import assert from "node:assert/strict"
import test from "node:test"

import { findCurrentSprint, isSprintInProgress } from "../../src/lib/server/sprintSelection"

const NOW = new Date("2026-02-24T15:30:00")

test("isSprintInProgress uses inclusive boundaries", () => {
  assert.equal(isSprintInProgress("2026-02-24", "2026-02-24", NOW), true)
  assert.equal(isSprintInProgress("2026-02-25", "2026-02-26", NOW), false)
})

test("findCurrentSprint returns in-range sprint regardless of status", () => {
  const sprint = findCurrentSprint(
    [
      { id: "done", status: "completed", startDate: "2026-02-20", endDate: "2026-02-28" },
    ],
    { now: NOW }
  )

  assert.equal(sprint?.id, "done")
})

test("findCurrentSprint prefers the most recently started overlapping sprint", () => {
  const sprint = findCurrentSprint(
    [
      { id: "earlier", status: "planning", startDate: "2026-02-18", endDate: "2026-02-27" },
      { id: "latest", status: "planning", startDate: "2026-02-22", endDate: "2026-02-26" },
    ],
    { now: NOW }
  )

  assert.equal(sprint?.id, "latest")
})

test("findCurrentSprint keeps deterministic order when date windows are identical", () => {
  const sprint = findCurrentSprint(
    [
      { id: "planning", status: "planning", startDate: "2026-02-20", endDate: "2026-02-28" },
      { id: "active", status: "active", startDate: "2026-02-20", endDate: "2026-02-28" },
    ],
    { now: NOW }
  )

  assert.equal(sprint?.id, "planning")
})

test("findCurrentSprint returns null when no sprint is in range", () => {
  const sprint = findCurrentSprint(
    [{ id: "future", status: "active", startDate: "2026-03-01", endDate: "2026-03-07" }],
    { now: NOW }
  )

  assert.equal(sprint, null)
})
