import { describe, expect, it } from "vitest";
import { buildHreflangSet } from "./hreflang.js";

describe("buildHreflangSet", () => {
  const LANGUAGES = ["ru", "en", "es", "fr", "it", "ja", "ko", "zh", "de"] as const;

  it("returns entries for all 9 languages plus x-default", () => {
    const result = buildHreflangSet({
      canonicalOrigin: "https://www.aeroflot.ru",
      pathWithoutLocale: "/onlineboard/flight/SU100-2025-01-15",
    });

    expect(result).toHaveLength(10); // 9 languages + x-default
  });

  it("includes all 9 languages", () => {
    const result = buildHreflangSet({
      canonicalOrigin: "https://www.aeroflot.ru",
      pathWithoutLocale: "/smoke",
    });

    const langs = result.map((entry) => entry.lang);
    for (const lang of LANGUAGES) {
      expect(langs).toContain(lang);
    }
  });

  it("x-default points to the ru variant (BCP-47)", () => {
    const result = buildHreflangSet({
      canonicalOrigin: "https://www.aeroflot.ru",
      pathWithoutLocale: "/smoke",
    });

    const xDefault = result.find((entry) => entry.lang === "x-default");
    expect(xDefault).toBeDefined();
    expect(xDefault?.href).toBe("https://www.aeroflot.ru/ru-ru/smoke");
  });

  it("builds correct href for each language using BCP-47 URL codes", () => {
    const result = buildHreflangSet({
      canonicalOrigin: "https://www.aeroflot.ru",
      pathWithoutLocale: "/onlineboard",
    });

    const en = result.find((entry) => entry.lang === "en");
    expect(en?.href).toBe("https://www.aeroflot.ru/en-en/onlineboard");

    const ja = result.find((entry) => entry.lang === "ja");
    expect(ja?.href).toBe("https://www.aeroflot.ru/ja-ja/onlineboard");
  });

  it("preserves paths with nested segments", () => {
    const result = buildHreflangSet({
      canonicalOrigin: "https://www.aeroflot.ru",
      pathWithoutLocale: "/onlineboard/flight/SU100-2025-01-15",
    });

    const fr = result.find((entry) => entry.lang === "fr");
    expect(fr?.href).toBe("https://www.aeroflot.ru/fr-fr/onlineboard/flight/SU100-2025-01-15");
  });

  it("handles root path", () => {
    const result = buildHreflangSet({
      canonicalOrigin: "https://www.aeroflot.ru",
      pathWithoutLocale: "",
    });

    const ru = result.find((entry) => entry.lang === "ru");
    expect(ru?.href).toBe("https://www.aeroflot.ru/ru-ru");
  });
});
