import { fetchAgentModels } from "./chatModels";

const apiFetch = jest.fn();

jest.mock("@/lib/apiFetch", () => ({
  apiFetch: (...args: unknown[]) => apiFetch(...args),
}));

describe("fetchAgentModels", () => {
  beforeEach(() => {
    apiFetch.mockReset();
  });

  it("loads model options and backend default model", async () => {
    apiFetch.mockResolvedValueOnce({
      ok: true,
      json: async () => ({
        default_model: "deepseek/deepseek-v4-flash",
        models: [
          {
            id: "deepseek/deepseek-v4-flash",
            label: "快速",
            description: "快速回答和任务执行",
            icon: "bolt",
          },
        ],
      }),
    });

    await expect(fetchAgentModels()).resolves.toEqual({
      defaultModel: "deepseek/deepseek-v4-flash",
      models: [
        {
          id: "deepseek/deepseek-v4-flash",
          label: "快速",
          description: "快速回答和任务执行",
          icon: "bolt",
        },
      ],
    });
    expect(apiFetch).toHaveBeenCalledWith(
      expect.stringContaining("/api/v1/agent/models"),
      expect.objectContaining({
        method: "GET",
        projectHeaderMode: "include",
        skipAuthRedirect: true,
      }),
    );
  });

  it("falls back to the first option when default model is omitted", async () => {
    apiFetch.mockResolvedValueOnce({
      ok: true,
      json: async () => ({
        models: [{ id: "provider/model", label: "Model" }],
      }),
    });

    await expect(fetchAgentModels()).resolves.toEqual({
      defaultModel: "provider/model",
      models: [{ id: "provider/model", label: "Model" }],
    });
  });
});
