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

import { activeUrl } from "@/lib/nav-active"

/** The real sidebar, in the order the three groups are drawn. */
const SIDEBAR = [
  "/notifications",
  "/tasks",
  "/job-description",
  "/review",
  "/kpi",
  "/team",
  "/tasks/new",
  "#", // Reminders, not built
  "/positions",
  "/admin/people",
  "/admin/job-descriptions",
  "/admin/holidays",
  "/admin/recurrence",
] as const

describe("activeUrl", () => {
  test("the page itself", () => {
    expect(activeUrl("/tasks", SIDEBAR)).toBe("/tasks")
    expect(activeUrl("/team", SIDEBAR)).toBe("/team")
    expect(activeUrl("/kpi", SIDEBAR)).toBe("/kpi")
  })

  test("a longer entry wins over a shorter one it sits under", () => {
    // The defect this file exists for: My tasks was hard-coded active, so it
    // stayed lit on every screen. Even once that was read from the path, the
    // naive rule would light My tasks while somebody writes a duty.
    expect(activeUrl("/tasks/new", SIDEBAR)).toBe("/tasks/new")
  })

  test("a page with no entry of its own belongs to the entry above it", () => {
    expect(
      activeUrl("/tasks/8f10bb1c-0000-4000-8000-000000000000", SIDEBAR),
    ).toBe("/tasks")
    expect(activeUrl("/positions/abc", SIDEBAR)).toBe("/positions")
    expect(activeUrl("/admin/people/abc", SIDEBAR)).toBe("/admin/people")
  })

  test("a path prefix is not a name prefix", () => {
    // `/team` must not claim a route whose name merely starts with it.
    expect(activeUrl("/teamwork", ["/team"])).toBe(null)
    expect(activeUrl("/tasksomething", ["/tasks"])).toBe(null)
  })

  test("an entry that points at no screen is never the answer", () => {
    expect(activeUrl("#", SIDEBAR)).toBe(null)
    expect(activeUrl("/anything", ["#"])).toBe(null)
  })

  test("a page outside the sidebar lights nothing", () => {
    // `/settings` is reached from the person's own menu, not from a nav entry,
    // and `/login` has no sidebar at all.
    expect(activeUrl("/settings", SIDEBAR)).toBe(null)
    expect(activeUrl("/login", SIDEBAR)).toBe(null)
    expect(activeUrl("/", SIDEBAR)).toBe(null)
  })

  test("nothing at all is nothing, not a crash", () => {
    expect(activeUrl("/tasks", [])).toBe(null)
  })

  test("order in the list does not decide the answer", () => {
    const forwards = ["/tasks", "/tasks/new"]
    const backwards = ["/tasks/new", "/tasks"]
    expect(activeUrl("/tasks/new", forwards)).toBe("/tasks/new")
    expect(activeUrl("/tasks/new", backwards)).toBe("/tasks/new")
  })
})
