import { describe, it, expect } from "vitest"; import { buildContentBlob, displayLocaleName, parseContentBlob } from "./messageContent"; const LOCALES = [ { code: "en_US", label: "English" }, { code: "zh_CN", label: "简体中文" }, { code: "zh_TW", label: "繁體中文" }, { code: "in_ID", label: "Bahasa Indonesia" }, { code: "ms_MY", label: "Bahasa Melayu" }, ]; describe("buildContentBlob", () => { it("assembles only the filled locales into the content JSON blob", () => { const blob = buildContentBlob( { en_US: "Welcome", zh_CN: " ", zh_TW: "", in_ID: "Selamat datang", // ms_MY omitted entirely }, LOCALES, ); expect(JSON.parse(blob)).toEqual({ en_US: "Welcome", in_ID: "Selamat datang" }); }); it("trims each locale's text before storing it", () => { const blob = buildContentBlob({ en_US: " Welcome " }, LOCALES); expect(JSON.parse(blob)).toEqual({ en_US: "Welcome" }); }); it("ignores any key that is not in the runtime locale list", () => { const blob = buildContentBlob({ en_US: "Welcome", fr_FR: "Bienvenue" }, LOCALES); expect(JSON.parse(blob)).toEqual({ en_US: "Welcome" }); }); it("iterates the runtime locale list, not a hardcoded set", () => { // A registry-driven locale list (e.g. a staged th_TH) drives the blob, exactly like the // /messages per-locale inputs do. const runtime = [...LOCALES, { code: "th_TH", label: "ภาษาไทย" }]; const blob = buildContentBlob({ en_US: "Welcome", th_TH: "ยินดีต้อนรับ" }, runtime); expect(JSON.parse(blob)).toEqual({ en_US: "Welcome", th_TH: "ยินดีต้อนรับ" }); }); it("produces an empty object blob when no locale is filled", () => { expect(buildContentBlob({}, LOCALES)).toBe("{}"); }); }); describe("parseContentBlob", () => { it("parses a valid JSON blob into a locale->text record", () => { expect(parseContentBlob('{"en_US":"Welcome","zh_CN":"HuanYing"}')).toEqual({ en_US: "Welcome", zh_CN: "HuanYing", }); }); it("returns an empty record for null/undefined/empty content", () => { expect(parseContentBlob(undefined)).toEqual({}); expect(parseContentBlob(null)).toEqual({}); expect(parseContentBlob("")).toEqual({}); }); it("returns an empty record rather than throwing on malformed JSON", () => { expect(parseContentBlob("not json")).toEqual({}); }); it("returns an empty record when the parsed JSON is not an object", () => { expect(parseContentBlob("42")).toEqual({}); expect(parseContentBlob('"just a string"')).toEqual({}); }); }); describe("displayLocaleName", () => { it("returns the en_US value when the value is a locale-JSON map containing en_US", () => { expect(displayLocaleName('{"en_US":"Glycocare Botanical Beverage"}')).toBe( "Glycocare Botanical Beverage", ); }); it("falls back to the first locale's value when en_US is absent", () => { expect(displayLocaleName('{"ms_MY":"Minuman Botani Glycocare"}')).toBe( "Minuman Botani Glycocare", ); }); it("returns a plain string value unchanged", () => { expect(displayLocaleName("Ultimate Youth")).toBe("Ultimate Youth"); }); it("returns an empty string for null/undefined/empty values", () => { expect(displayLocaleName(null)).toBe(""); expect(displayLocaleName(undefined)).toBe(""); expect(displayLocaleName("")).toBe(""); }); });