import { describe, it, expect } from "vitest";
import {
  resolveLibraryView,
  libraryViewUrl,
  LIBRARY_VIEWS,
  DEFAULT_LIBRARY_VIEW,
} from "./libraryView";

describe("resolveLibraryView", () => {
  it("resolves each known view", () => {
    for (const view of LIBRARY_VIEWS) {
      expect(resolveLibraryView(view)).toBe(view);
    }
  });

  it("defaults to browse when the param is absent", () => {
    expect(resolveLibraryView(null)).toBe("browse");
    expect(resolveLibraryView(undefined)).toBe("browse");
  });

  it("falls back to the default rather than rendering nothing for junk", () => {
    expect(resolveLibraryView("shows")).toBe(DEFAULT_LIBRARY_VIEW);
    expect(resolveLibraryView("")).toBe(DEFAULT_LIBRARY_VIEW);
  });

  it("tolerates case and surrounding whitespace", () => {
    expect(resolveLibraryView("Genres")).toBe("genres");
    expect(resolveLibraryView(" all ")).toBe("all");
  });
});

describe("libraryViewUrl", () => {
  it("omits the param for the default view so the landing URL stays clean", () => {
    expect(libraryViewUrl("/library/tv", "browse")).toBe("/library/tv");
  });

  it("names the non-default views", () => {
    expect(libraryViewUrl("/library/tv", "all")).toBe("/library/tv?view=all");
    expect(libraryViewUrl("/library/movies", "genres")).toBe("/library/movies?view=genres");
  });

  it("round-trips through resolveLibraryView", () => {
    for (const view of LIBRARY_VIEWS) {
      const url = libraryViewUrl("/library/tv", view);
      const param = new URL(url, "http://x").searchParams.get("view");
      expect(resolveLibraryView(param)).toBe(view);
    }
  });
});
