import { describe, expect, it } from "vitest";
import {
  resolveProviderSelection,
  sanitizeWorkspaceProviderSelections,
} from "../src/dimsdk/compat/archships/dim-agent/providers/index.ts";

describe("provider selection sticky provider behavior", () => {
  it("keeps an explicitly selected provider instead of silently falling back", () => {
    const selection = resolveProviderSelection({
      candidates: [
        {
          id: "next-api-oauth",
          modelIds: ["qwen3-max-2026-01-23"],
          selectable: false,
        },
        {
          id: "openai",
          modelIds: ["gpt-4o-mini"],
          selectable: true,
        },
      ],
      preferredProviderId: "next-api-oauth",
      preferredModelId: "qwen3-max-2026-01-23",
      rememberedModels: {
        "next-api-oauth": "qwen3-max-2026-01-23",
      },
    });

    expect(selection).toEqual({
      providerId: "next-api-oauth",
      modelId: "qwen3-max-2026-01-23",
    });
  });

  it("keeps an explicitly selected provider when only the workspace model is remembered", () => {
    const selection = resolveProviderSelection({
      candidates: [
        {
          id: "next-api-oauth",
          modelIds: [],
          selectable: false,
        },
        {
          id: "openai",
          modelIds: ["gpt-4o-mini"],
          selectable: true,
        },
      ],
      preferredProviderId: "next-api-oauth",
      preferredModelId: "qwen3-max-2026-01-23",
    });

    expect(selection).toEqual({
      providerId: "next-api-oauth",
      modelId: "qwen3-max-2026-01-23",
    });
  });

  it("still falls back when the preferred provider no longer exists", () => {
    const selection = resolveProviderSelection({
      candidates: [
        {
          id: "openai",
          modelIds: ["gpt-4o-mini"],
          selectable: true,
        },
      ],
      preferredProviderId: "next-api-oauth",
      preferredModelId: "qwen3-max-2026-01-23",
    });

    expect(selection).toEqual({
      providerId: "openai",
      modelId: "gpt-4o-mini",
    });
  });

  it("preserves workspace selections for temporarily unavailable providers", () => {
    const selections = sanitizeWorkspaceProviderSelections({
      workspaceSelections: {
        "/workspace": {
          providerId: "next-api-oauth",
          modelId: "qwen3-max-2026-01-23",
        },
      },
      candidates: [
        {
          id: "next-api-oauth",
          modelIds: ["qwen3-max-2026-01-23"],
          selectable: false,
        },
      ],
      rememberedModels: {
        "next-api-oauth": "qwen3-max-2026-01-23",
      },
    });

    expect(selections).toEqual({
      "/workspace": {
        providerId: "next-api-oauth",
        modelId: "qwen3-max-2026-01-23",
      },
    });
  });

  it("preserves workspace selections when the provider temporarily has no catalog models", () => {
    const selections = sanitizeWorkspaceProviderSelections({
      workspaceSelections: {
        "/workspace": {
          providerId: "next-api-oauth",
          modelId: "qwen3-max-2026-01-23",
        },
      },
      candidates: [
        {
          id: "next-api-oauth",
          modelIds: [],
          selectable: false,
        },
      ],
    });

    expect(selections).toEqual({
      "/workspace": {
        providerId: "next-api-oauth",
        modelId: "qwen3-max-2026-01-23",
      },
    });
  });
});
