Files
co-lite/solvway-admin-portal/app/lib/messageContent.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

97 lines
3.4 KiB
TypeScript

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("");
});
});