import { beforeEach, describe, expect, it, vi } from "vitest";

const mockIsConvexEnabled = vi.fn();
const mockGetAuthedConvexClient = vi.fn();
const mockFetchDocumentAiOrchestratorConfig = vi.fn();

vi.mock("@/lib/convex/enabled", () => ({
  isConvexEnabled: mockIsConvexEnabled,
}));

vi.mock("@/lib/convex/route", () => ({
  getAuthedConvexClient: mockGetAuthedConvexClient,
}));

vi.mock("@/lib/server/document-ai-orchestrator", () => ({
  fetchDocumentAiOrchestratorConfig: mockFetchDocumentAiOrchestratorConfig,
}));

describe("/api/ai-agent/document/config route", () => {
  beforeEach(() => {
    mockIsConvexEnabled.mockReset();
    mockGetAuthedConvexClient.mockReset();
    mockFetchDocumentAiOrchestratorConfig.mockReset();
  });

  it("应返回 orchestrator config 能力面", async () => {
    mockIsConvexEnabled.mockReturnValue(true);
    mockGetAuthedConvexClient.mockResolvedValue({
      auth: {
        userId: "user-1",
      },
    });
    mockFetchDocumentAiOrchestratorConfig.mockResolvedValue({
      provider: "online",
      defaultModelKey: "gpt-5.4",
      defaultProfileId: "page_writer_ai_first",
      sessionEnabled: true,
      models: [{ key: "gpt-5.4", title: "GPT-5.4" }],
      profiles: [{ id: "page_writer_ai_first", title: "AI 主写" }],
      tools: [{ name: "doc_get", description: "读取当前页摘要" }],
    });

    const { GET } = await import("./route");
    const response = await GET(new Request("http://127.0.0.1:3000/api/ai-agent/document/config"));
    const payload = await response.json();

    expect(response.status).toBe(200);
    expect(mockFetchDocumentAiOrchestratorConfig).toHaveBeenCalledTimes(1);
    expect(payload.defaultModelKey).toBe("gpt-5.4");
    expect(payload.defaultProfileId).toBe("page_writer_ai_first");
    expect(payload.tools[0]?.name).toBe("doc_get");
  });
});
