import { describe, expect, it } from "vitest";
import { ensureCatalog, getCatalog, isCatalogLoaded, translate } from "./messages";

describe("locale catalog loading", () => {
  it("ships the default locale and fetches others on demand", async () => {
    expect(isCatalogLoaded("zh-TW")).toBe(true);
    expect(isCatalogLoaded("en")).toBe(false);
    // 還沒載到英文字典時，先用預設語系頂著而不是吐出 key
    expect(translate("en", "app.name")).toBe(translate("zh-TW", "app.name"));

    await ensureCatalog("en");

    expect(isCatalogLoaded("en")).toBe(true);
    expect(getCatalog("en")["app.name"]).toBe("Lapras");
    expect(translate("en", "app.name")).toBe("Lapras");
  });

  it("shares one in-flight request across concurrent callers", async () => {
    const [a, b] = await Promise.all([ensureCatalog("en"), ensureCatalog("en")]);
    expect(a).toBe(b);
  });
});
