import { describe, it, expect } from "vitest";
import { HELP_PAGES, nextPage, prevPage, clampPage, isFirstPage, isLastPage } from "./helpPages";

describe("HELP_PAGES", () => {
  it("contains 12 pages", () => {
    expect(HELP_PAGES).toHaveLength(12);
  });

  it("has unique ids", () => {
    const ids = HELP_PAGES.map((p) => p.id);
    expect(new Set(ids).size).toBe(ids.length);
  });

  it("has non-empty titles", () => {
    for (const page of HELP_PAGES) {
      expect(page.title.length).toBeGreaterThan(0);
    }
  });
});

describe("nextPage", () => {
  it("advances to the next page", () => {
    expect(nextPage(0, 7)).toBe(1);
    expect(nextPage(3, 7)).toBe(4);
  });

  it("does not go past the last page", () => {
    expect(nextPage(6, 7)).toBe(6);
  });

  it("clamps when already at the last page", () => {
    expect(nextPage(10, 7)).toBe(6);
  });
});

describe("prevPage", () => {
  it("goes back to the previous page", () => {
    expect(prevPage(3)).toBe(2);
    expect(prevPage(1)).toBe(0);
  });

  it("does not go before the first page", () => {
    expect(prevPage(0)).toBe(0);
  });

  it("clamps when already at the first page", () => {
    expect(prevPage(-1)).toBe(0);
  });
});

describe("clampPage", () => {
  it("returns the page unchanged when in range", () => {
    expect(clampPage(3, 7)).toBe(3);
    expect(clampPage(0, 7)).toBe(0);
    expect(clampPage(6, 7)).toBe(6);
  });

  it("clamps negative indices to 0", () => {
    expect(clampPage(-1, 7)).toBe(0);
  });

  it("clamps over-range indices to the last page", () => {
    expect(clampPage(10, 7)).toBe(6);
  });

  it("returns 0 when totalPages is 0", () => {
    expect(clampPage(3, 0)).toBe(0);
  });

  it("returns 0 when totalPages is negative", () => {
    expect(clampPage(3, -1)).toBe(0);
  });
});

describe("isFirstPage", () => {
  it("returns true for index 0", () => {
    expect(isFirstPage(0)).toBe(true);
  });

  it("returns false for index greater than 0", () => {
    expect(isFirstPage(1)).toBe(false);
    expect(isFirstPage(6)).toBe(false);
  });
});

describe("isLastPage", () => {
  it("returns true for the last index", () => {
    expect(isLastPage(6, 7)).toBe(true);
  });

  it("returns false for indices before the last", () => {
    expect(isLastPage(5, 7)).toBe(false);
    expect(isLastPage(0, 7)).toBe(false);
  });

  it("returns true when index exceeds total", () => {
    expect(isLastPage(10, 7)).toBe(true);
  });
});
