import { describe, expect, it } from "vitest";
import type { AgentBackendConfig } from "../../services/tauri";
import { formatBackendError } from "./backendSettingsErrors";

function backend(overrides: Partial<AgentBackendConfig> = {}): AgentBackendConfig {
  return {
    id: "ollama",
    label: "Ollama",
    kind: "ollama",
    base_url: "http://localhost:11434",
    enabled: true,
    default_model: null,
    manual_models: [],
    discovered_models: [],
    auth_ref: null,
    capabilities: {
      thinking: false,
      effort: false,
      fast_mode: false,
      one_m_context: false,
      tools: true,
      vision: false,
    },
    context_window_default: 64_000,
    model_discovery: true,
    has_secret: false,
    ...overrides,
  };
}

describe("formatBackendError", () => {
  it("turns raw unreachable Ollama transport failures into an actionable local error", () => {
    const message = formatBackendError(
      "Failed to query Ollama: error sending request for url (http://localhost:11434/api/tags)",
      backend(),
    );

    expect(message).toBe(
      "Ollama is not reachable at http://localhost:11434. Start the service or update the Base URL.",
    );
  });

  it("preserves authentication errors without hiding the action", () => {
    const message = formatBackendError(
      "OpenAI API backend requires an API key in Settings -> Models",
      backend({ id: "openai-api", label: "OpenAI API", kind: "openai_api" }),
    );

    expect(message).toBe("OpenAI API backend requires an API key in Settings -> Models");
  });

  it("turns unreachable LM Studio into an actionable hint pointing at `lms server start`", () => {
    const message = formatBackendError(
      "Failed to query LM Studio: error sending request for url (http://localhost:1234/api/v0/models)",
      backend({
        id: "lm-studio",
        label: "LM Studio",
        kind: "lm_studio",
        base_url: "http://localhost:1234",
      }),
    );

    expect(message).toBe(
      "LM Studio is not reachable at http://localhost:1234. Run `lms server start` or update the Base URL.",
    );
  });

  it("does not invent a Base URL for native Codex CLI transport failures", () => {
    const message = formatBackendError(
      "error sending request for url",
      backend({
        id: "codex",
        label: "Codex",
        kind: "codex_native",
        base_url: null,
      }),
    );

    expect(message).toBe("Codex is not reachable. Check the provider and try again.");
  });
});
