import { act, type ReactNode } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { AiAgentPanel } from "./AiAgentPanel";

vi.mock("@/components/ui/scroll-area", () => ({
  ScrollArea: ({ children, className }: { children: ReactNode; className?: string }) => (
    <div data-testid="scroll-area" className={className}>
      {children}
    </div>
  ),
}));

describe("AiAgentPanel", () => {
  let container: HTMLDivElement;
  let root: Root;

  beforeEach(() => {
    (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
    Element.prototype.scrollIntoView = vi.fn();
    container = document.createElement("div");
    document.body.appendChild(container);
    root = createRoot(container);
  });

  afterEach(() => {
    act(() => {
      root.unmount();
    });
    container.remove();
  });

  it("空状态会展示全局 AI 标题与能力说明", () => {
    act(() => {
      root.render(<AiAgentPanel />);
    });

    expect(container.textContent).toContain("全局 AI");
    expect(container.textContent).toContain("前端已经退为桥接层");
    expect(container.textContent).toContain("联网检索");
    expect(container.textContent).toContain("LightRAG");
    expect(container.textContent).toContain("跨页面文档");
    expect(container.textContent).toContain("图片 OCR");
  });

  it("可以切换工具活动面板显隐", () => {
    act(() => {
      root.render(<AiAgentPanel />);
    });

    const toggleButton = container.querySelector('button[aria-label="切换工具活动面板"]');
    expect(toggleButton).not.toBeNull();
    expect(container.textContent).toContain("活动轨迹");
    expect(container.textContent).toContain("暂无工具活动");

    act(() => {
      toggleButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
    });

    expect(container.textContent).not.toContain("暂无工具活动");
  });
});
