import { describe, expect, it } from "vitest";
import {
  homePathForLang,
  isHomePathname,
  languageForHomePathname,
  localizedHomeRoutes,
} from "./languageRoutes";

const expectedRoutes = [
  ["/cn", "zh-CN"],
  ["/ja", "ja"],
  ["/ko", "ko"],
  ["/vi", "vi"],
  ["/id", "id"],
  ["/ms", "ms"],
] as const;

describe("language home routes", () => {
  it("uses short ISO-style language path codes except default English root", () => {
    expect(localizedHomeRoutes.map(({ path, lang }) => [path, lang])).toEqual(
      expectedRoutes,
    );
  });

  it("maps localized home paths to language codes", () => {
    for (const [path, lang] of expectedRoutes) {
      expect(languageForHomePathname(path)).toBe(lang);
      expect(languageForHomePathname(`${path}/`)).toBe(lang);
    }
    expect(languageForHomePathname("/english")).toBeNull();
    expect(languageForHomePathname("/unknown")).toBeNull();
  });

  it("treats root and localized language paths as home pages", () => {
    expect(isHomePathname("/")).toBe(true);
    expect(isHomePathname("/cn")).toBe(true);
    expect(isHomePathname("/browse")).toBe(false);
  });

  it("links each active language back to its localized home", () => {
    expect(homePathForLang("en")).toBe("/");
    expect(homePathForLang("zh-CN")).toBe("/cn");
    expect(homePathForLang("ja")).toBe("/ja");
    expect(homePathForLang("ko")).toBe("/ko");
    expect(homePathForLang("vi")).toBe("/vi");
    expect(homePathForLang("id")).toBe("/id");
    expect(homePathForLang("ms")).toBe("/ms");
  });
});
