import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import { PalacePanel } from "./PalacePanel";

const mockListMemories = vi.fn();
const mockSearchPalace = vi.fn();

vi.mock("@/lib/palace-client", () => ({
  listMemories: (...args: unknown[]) => mockListMemories(...args),
  searchPalace: (...args: unknown[]) => mockSearchPalace(...args),
}));

beforeEach(() => {
  mockListMemories.mockReset();
  mockSearchPalace.mockReset();
});

describe("PalacePanel", () => {
  it("renders memories after loading", async () => {
    mockListMemories.mockResolvedValue([
      { id: "m1", hall: "decisions", content: "Use JWT for auth", createdAt: "2026-01-01T00:00:00Z" },
    ]);
    render(<PalacePanel />);
    await waitFor(() => expect(screen.getByText("Use JWT for auth")).toBeInTheDocument());
    expect(screen.getByText("decisions")).toBeInTheDocument();
  });

  it("renders empty state", async () => {
    mockListMemories.mockResolvedValue([]);
    render(<PalacePanel />);
    await waitFor(() =>
      expect(screen.getByText(/No memories found/)).toBeInTheDocument(),
    );
  });

  it("has search input", async () => {
    mockListMemories.mockResolvedValue([]);
    render(<PalacePanel />);
    await waitFor(() => {
      expect(screen.getByLabelText("Search palace memories")).toBeInTheDocument();
    });
  });
});
