import { describe, expect, it } from "vitest";
import { buildPaginatedResult, DEFAULT_PAGE_SIZE } from "./pagination";

describe("buildPaginatedResult", () => {
  it("returns empty result when no rows", () => {
    const result = buildPaginatedResult([], DEFAULT_PAGE_SIZE);
    expect(result).toEqual({ items: [], hasNextPage: false });
  });

  it("returns items without hasNextPage when rows < limit", () => {
    const rows = [{ id: "1" }, { id: "2" }];
    const result = buildPaginatedResult(rows, 10);
    expect(result).toEqual({ items: rows, hasNextPage: false });
  });

  it("returns items with hasNextPage when rows = limit + 1", () => {
    const rows = [{ id: "1" }, { id: "2" }, { id: "3" }];
    const result = buildPaginatedResult(rows, 2);
    expect(result).toEqual({
      items: [{ id: "1" }, { id: "2" }],
      hasNextPage: true,
    });
  });

  it("returns items without hasNextPage when rows = limit exactly", () => {
    const rows = [{ id: "1" }, { id: "2" }, { id: "3" }];
    const result = buildPaginatedResult(rows, 3);
    expect(result).toEqual({
      items: rows,
      hasNextPage: false,
    });
  });

  it("includes total when provided", () => {
    const rows = [{ id: "1" }];
    const result = buildPaginatedResult(rows, 10, 42);
    expect(result).toEqual({ items: rows, hasNextPage: false, total: 42 });
  });

  it("uses DEFAULT_PAGE_SIZE of 20", () => {
    expect(DEFAULT_PAGE_SIZE).toBe(20);
  });
});
