import { expect, test } from "vitest";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import { useSearch } from "./queries";

function wrapper({ children }: { children: React.ReactNode }) {
  const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  return <QueryClientProvider client={qc}>{children}</QueryClientProvider>;
}

test("useSearch fetches a page and reports more pages available", async () => {
  const { result } = renderHook(() => useSearch("bronze", null), { wrapper });

  await waitFor(() => expect(result.current.data).toBeDefined());

  const first = result.current.data!.pages[0];
  expect(first.hits[0].object_name).toBe("Bronze figurine");
  expect(first.estimated_total).toBe(25);
  expect(result.current.hasNextPage).toBe(true);
});

test("useSearch is disabled for an empty query", () => {
  const { result } = renderHook(() => useSearch("   ", null), { wrapper });
  expect(result.current.fetchStatus).toBe("idle");
});
