import { describe, expect, it } from "vitest";

import { __testing } from "./web-search.js";

const {
  inferPerplexityBaseUrlFromApiKey,
  resolvePerplexityBaseUrl,
  normalizeFreshness,
  normalizeBraveLang,
} = __testing;

describe("web_search perplexity baseUrl defaults", () => {
  it("detects a Perplexity key prefix", () => {
    expect(inferPerplexityBaseUrlFromApiKey("pplx-123")).toBe("direct");
  });

  it("detects an OpenRouter key prefix", () => {
    expect(inferPerplexityBaseUrlFromApiKey("sk-or-v1-123")).toBe("openrouter");
  });

  it("returns undefined for unknown key formats", () => {
    expect(inferPerplexityBaseUrlFromApiKey("unknown-key")).toBeUndefined();
  });

  it("prefers explicit baseUrl over key-based defaults", () => {
    expect(resolvePerplexityBaseUrl({ baseUrl: "https://example.com" }, "config", "pplx-123")).toBe(
      "https://example.com",
    );
  });

  it("defaults to direct when using PERPLEXITY_API_KEY", () => {
    expect(resolvePerplexityBaseUrl(undefined, "perplexity_env")).toBe("https://api.perplexity.ai");
  });

  it("defaults to OpenRouter when using OPENROUTER_API_KEY", () => {
    expect(resolvePerplexityBaseUrl(undefined, "openrouter_env")).toBe(
      "https://openrouter.ai/api/v1",
    );
  });

  it("defaults to direct when config key looks like Perplexity", () => {
    expect(resolvePerplexityBaseUrl(undefined, "config", "pplx-123")).toBe(
      "https://api.perplexity.ai",
    );
  });

  it("defaults to OpenRouter when config key looks like OpenRouter", () => {
    expect(resolvePerplexityBaseUrl(undefined, "config", "sk-or-v1-123")).toBe(
      "https://openrouter.ai/api/v1",
    );
  });

  it("defaults to OpenRouter for unknown config key formats", () => {
    expect(resolvePerplexityBaseUrl(undefined, "config", "weird-key")).toBe(
      "https://openrouter.ai/api/v1",
    );
  });
});

describe("normalizeBraveLang", () => {
  it("maps bare 'zh' to 'zh-hans'", () => {
    expect(normalizeBraveLang("zh")).toBe("zh-hans");
    expect(normalizeBraveLang("ZH")).toBe("zh-hans");
  });

  it("maps zh-cn / zh-sg to zh-hans", () => {
    expect(normalizeBraveLang("zh-cn")).toBe("zh-hans");
    expect(normalizeBraveLang("zh-sg")).toBe("zh-hans");
  });

  it("maps zh-tw / zh-hk to zh-hant", () => {
    expect(normalizeBraveLang("zh-tw")).toBe("zh-hant");
    expect(normalizeBraveLang("zh-hk")).toBe("zh-hant");
  });

  it("passes through already-correct codes", () => {
    expect(normalizeBraveLang("zh-hans")).toBe("zh-hans");
    expect(normalizeBraveLang("en")).toBe("en");
    expect(normalizeBraveLang("de")).toBe("de");
  });

  it("returns undefined for empty / undefined", () => {
    expect(normalizeBraveLang(undefined)).toBeUndefined();
    expect(normalizeBraveLang("")).toBeUndefined();
    expect(normalizeBraveLang("  ")).toBeUndefined();
  });
});

describe("web_search freshness normalization", () => {
  it("accepts Brave shortcut values", () => {
    expect(normalizeFreshness("pd")).toBe("pd");
    expect(normalizeFreshness("PW")).toBe("pw");
  });

  it("accepts valid date ranges", () => {
    expect(normalizeFreshness("2024-01-01to2024-01-31")).toBe("2024-01-01to2024-01-31");
  });

  it("rejects invalid date ranges", () => {
    expect(normalizeFreshness("2024-13-01to2024-01-31")).toBeUndefined();
    expect(normalizeFreshness("2024-02-30to2024-03-01")).toBeUndefined();
    expect(normalizeFreshness("2024-03-10to2024-03-01")).toBeUndefined();
  });
});
