import { describe, expect, it, vi } from "vitest";
import { renderHook } from "@testing-library/react";
import { useJails } from "../useJailList";

// Mock the API calls
vi.mock("../../api/jails", () => ({
  fetchJails: vi.fn().mockResolvedValue({
    jails: [],
    total: 0,
  }),
  startJail: vi.fn(),
  stopJail: vi.fn(),
  setJailIdle: vi.fn(),
  reloadJail: vi.fn(),
  reloadAllJails: vi.fn(),
}));

vi.mock("../../utils/fetchError", () => ({
  handleFetchError: vi.fn(),
}));

describe("useJails", () => {
  it("returns stable function references between renders", () => {
    const { result, rerender } = renderHook(() => useJails());

    const firstRender = {
      startJail: result.current.startJail,
      stopJail: result.current.stopJail,
      setIdle: result.current.setIdle,
      reloadJail: result.current.reloadJail,
      reloadAll: result.current.reloadAll,
      refresh: result.current.refresh,
    };

    rerender();

    const secondRender = {
      startJail: result.current.startJail,
      stopJail: result.current.stopJail,
      setIdle: result.current.setIdle,
      reloadJail: result.current.reloadJail,
      reloadAll: result.current.reloadAll,
      refresh: result.current.refresh,
    };

    // Function references should be the same between renders
    expect(firstRender.startJail).toBe(secondRender.startJail);
    expect(firstRender.stopJail).toBe(secondRender.stopJail);
    expect(firstRender.setIdle).toBe(secondRender.setIdle);
    expect(firstRender.reloadJail).toBe(secondRender.reloadJail);
    expect(firstRender.reloadAll).toBe(secondRender.reloadAll);
    expect(firstRender.refresh).toBe(secondRender.refresh);
  });
});
