import { describe, expect, it } from "bun:test";

import {
  agentModelOptions,
  isSupportedModel,
  resolveDefaultModel,
  supportedModels,
} from "../../src/chat/models.js";
import { parseAgentModelOptions } from "../../src/chat/modelConfig.js";

describe("agent model config", () => {
  it("keeps every exposed option in the supported model list", () => {
    expect(agentModelOptions.map((model) => model.id)).toEqual(supportedModels);
    expect(
      agentModelOptions.every(
        (model) => model.label && model.description && model.icon,
      ),
    ).toBe(true);
  });

  it("validates supported model ids", () => {
    expect(isSupportedModel("deepseek/deepseek-v4-flash")).toBe(true);
    expect(isSupportedModel("unknown/model")).toBe(false);
  });

  it("rejects unsupported default models", () => {
    expect(resolveDefaultModel("deepseek/deepseek-v4-pro")).toBe(
      "deepseek/deepseek-v4-pro",
    );
    expect(() => resolveDefaultModel("unknown/model")).toThrow(
      "unsupported default agent model",
    );
  });

  it("parses full model option config from JSON", () => {
    expect(
      parseAgentModelOptions(
        JSON.stringify([
          {
            id: "provider/model",
            label: "自定义",
            description: "自定义模型",
            icon: "bolt",
          },
        ]),
      ),
    ).toEqual([
      {
        id: "provider/model",
        label: "自定义",
        description: "自定义模型",
        icon: "bolt",
      },
    ]);
  });

  it("rejects invalid model option config", () => {
    expect(() => parseAgentModelOptions("not-json")).toThrow(
      "OPENCODE_MODEL_OPTIONS must be valid JSON",
    );
    expect(() =>
      parseAgentModelOptions(
        JSON.stringify([
          { id: "provider/model", label: "模型" },
          { id: "provider/model", label: "重复模型" },
        ]),
      ),
    ).toThrow("duplicate OPENCODE_MODEL_OPTIONS id");
  });
});
