import { describe, expect, it } from "vitest";
import {
  isCloudinarySportIconUrl,
  isLegacySportIconFilename,
  parseSportIconUrlInput,
  resolveSportIconUrl,
} from "../sport-icon-url";

describe("resolveSportIconUrl", () => {
  it("returns null for empty values", () => {
    expect(resolveSportIconUrl(null)).toBeNull();
    expect(resolveSportIconUrl(undefined)).toBeNull();
  });

  it("keeps remote and absolute URLs unchanged", () => {
    expect(resolveSportIconUrl("https://res.cloudinary.com/demo/image/upload/v1/sports-icons/nfl.svg")).toBe(
      "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/nfl.svg"
    );
    expect(resolveSportIconUrl("/custom/icon.svg")).toBe("/custom/icon.svg");
  });

  it("resolves legacy filenames from public sports-icons", () => {
    expect(resolveSportIconUrl("nfl.svg")).toBe("/sports-icons/nfl.svg");
  });
});

describe("sport icon URL validation", () => {
  it("accepts Cloudinary sports icon SVG URLs", () => {
    const url = "https://res.cloudinary.com/demo/image/upload/v123/sports-icons/nfl.svg";
    expect(isCloudinarySportIconUrl(url)).toBe(true);
    expect(parseSportIconUrlInput(url)).toBe(url);
  });

  it("accepts legacy SVG filenames", () => {
    expect(isLegacySportIconFilename("nfl.svg")).toBe(true);
    expect(parseSportIconUrlInput("nfl.svg")).toBe("nfl.svg");
  });

  it("rejects arbitrary URLs and non-SVG files", () => {
    expect(parseSportIconUrlInput("https://example.com/icon.svg")).toBeUndefined();
    expect(parseSportIconUrlInput("https://res.cloudinary.com/demo/image/upload/v123/avatars/nfl.svg")).toBeUndefined();
    expect(parseSportIconUrlInput("nfl.png")).toBeUndefined();
    expect(parseSportIconUrlInput("../nfl.svg")).toBeUndefined();
    expect(parseSportIconUrlInput("/sports-icons/nfl.svg")).toBeUndefined();
  });
});
