import { describe, expect, it } from "vitest";
import {
  isValidProviderBaseUrl,
  normalizeProviderBaseUrl,
} from "../src/dimsdk/pages/utils/provider-base-url.ts";

describe("provider base URL normalization", () => {
  it("keeps OpenAI-compatible root URLs unchanged", () => {
    expect(normalizeProviderBaseUrl("openai", "https://api.openai.com")).toBe("https://api.openai.com");
    expect(normalizeProviderBaseUrl("openai-responses", "https://proxy.example.com/")).toBe("https://proxy.example.com/");
  });

  it("strips endpoint URLs to API roots for OpenAI-compatible adapters", () => {
    expect(normalizeProviderBaseUrl("openai", "https://api.openai.com/chat/completions")).toBe("https://api.openai.com/");
    expect(normalizeProviderBaseUrl("openai", "https://api.openai.com/v1/responses")).toBe("https://api.openai.com/v1");
    expect(normalizeProviderBaseUrl("openai", "https://proxy.example.com/openai/v1/chat/completions")).toBe(
      "https://proxy.example.com/openai/v1",
    );
  });

  it("keeps non-openai adapter URLs unchanged except trimming", () => {
    expect(normalizeProviderBaseUrl("gemini", " https://generativelanguage.googleapis.com ")).toBe(
      "https://generativelanguage.googleapis.com",
    );
    expect(normalizeProviderBaseUrl("anthropic", "https://api.anthropic.com")).toBe("https://api.anthropic.com");
  });

  it("validates only absolute http(s) URLs", () => {
    expect(isValidProviderBaseUrl("https://api.openai.com/v1")).toBe(true);
    expect(isValidProviderBaseUrl("http://localhost:11434/v1")).toBe(true);
    expect(isValidProviderBaseUrl("api.openai.com/v1")).toBe(false);
    expect(isValidProviderBaseUrl("ftp://example.com/v1")).toBe(false);
  });
});
