import { describe, it, expect, vi } from "vitest";
import { renderHook, act, waitFor } from "@testing-library/react";
import { useJailBannedIps } from "../useJails";
import * as api from "../../api/jails";

vi.mock("../../api/jails");

describe("useJailBannedIps", () => {
  it("loads bans and allows unban", async () => {
    const fetchMock = vi.mocked(api.fetchJailBannedIps);
    const unbanMock = vi.mocked(api.unbanIp);

    fetchMock.mockResolvedValue({ items: [{ ip: "1.2.3.4", jail: "sshd", banned_at: "2025-01-01T10:00:00+00:00", expires_at: "2025-01-01T10:10:00+00:00", ban_count: 1, country: "US" }], total: 1, page: 1, page_size: 25 });
    unbanMock.mockResolvedValue({ message: "ok", jail: "sshd" });

    const { result } = renderHook(() => useJailBannedIps("sshd"));
    await waitFor(() => {
      expect(result.current.loading).toBe(false);
    });
    expect(result.current.items.length).toBe(1);
    expect(fetchMock).toHaveBeenCalledWith("sshd", 1, 25, undefined, expect.any(AbortSignal));

    await act(async () => {
      await result.current.unban("1.2.3.4");
    });

    expect(unbanMock).toHaveBeenCalledWith("1.2.3.4", "sshd");
    expect(fetchMock).toHaveBeenCalledTimes(2);
  });
});
