import { beforeEach, describe, expect, it } from "vitest";
import { readAiPanelPrefs } from "./panelShared";

describe("panelShared 在线模型真源", () => {
  beforeEach(() => {
    window.localStorage.clear();
  });

  it("读取在线偏好时不应再把旧 model 字段回退成 modelKey", () => {
    window.localStorage.setItem("doc_ai_provider", "online");
    window.localStorage.setItem("doc_ai_model", "gpt-5.4");

    const prefs = readAiPanelPrefs("doc_ai", {
      provider: "online",
      model: "",
      modelKey: "",
      profileId: "",
      maxSteps: 10,
    });

    expect(prefs.model).toBe("gpt-5.4");
    expect(prefs.modelKey).toBe("");
  });

  it("读取在线偏好时应优先使用显式存储的 modelKey", () => {
    window.localStorage.setItem("doc_ai_provider", "online");
    window.localStorage.setItem("doc_ai_model", "legacy-model");
    window.localStorage.setItem("doc_ai_model_key", "gpt-5.3-codex");

    const prefs = readAiPanelPrefs("doc_ai", {
      provider: "online",
      model: "",
      modelKey: "",
      profileId: "",
      maxSteps: 10,
    });

    expect(prefs.model).toBe("legacy-model");
    expect(prefs.modelKey).toBe("gpt-5.3-codex");
  });
});
