Files
co-lite/solvway-admin-portal/app/lib/server/messages.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

156 lines
7.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn() }));
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
import { loadMessageCatalog, clearMessageCache, cachedCatalogEntryCount } from "./messages";
// The version-aware catalog cache (catalog-cache-invalidation spec): every load first fetches
// the cheap catalog version (admin -> /api/admin/messages/catalog-version) and caches the
// catalog under `locale@version`. A version bump makes the old key unreachable, so the next
// request refetches — immediate, cross-instance, no fixed-TTL wait. A version-fetch failure
// degrades to the last-known cached catalog (or today's plain-TTL behaviour when nothing is
// cached yet), never a blank catalog.
function okVersion(version: number) {
return { ok: true as const, data: { version } };
}
beforeEach(() => {
mocks.callMobileApi.mockReset();
clearMessageCache();
});
describe("loadMessageCatalog", () => {
it("fetches the catalog version then the catalog for a locale", async () => {
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(4))
.mockResolvedValueOnce({ ok: true, data: { WelcomeBack: "Welcome Back" } });
const cat = await loadMessageCatalog("en_US");
expect(cat).toEqual({ WelcomeBack: "Welcome Back" });
expect(mocks.callMobileApi).toHaveBeenNthCalledWith(1, "/api/admin/messages/catalog-version");
expect(mocks.callMobileApi).toHaveBeenNthCalledWith(2, "/api/admin/messages/catalog/en_US");
});
it("serves from cache within the same version (no second catalog call)", async () => {
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(4))
.mockResolvedValueOnce({ ok: true, data: { A: "a" } });
await loadMessageCatalog("zh_CN", 1_000);
mocks.callMobileApi.mockResolvedValueOnce(okVersion(4));
const cat = await loadMessageCatalog("zh_CN", 2_000);
expect(cat).toEqual({ A: "a" });
// the version was fetched again, but the catalog came from the cache
expect(mocks.callMobileApi).toHaveBeenCalledTimes(3);
expect(mocks.callMobileApi).toHaveBeenNthCalledWith(3, "/api/admin/messages/catalog-version");
});
it("refetches the catalog when the version bumps — visible next request, no TTL wait", async () => {
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(4))
.mockResolvedValueOnce({ ok: true, data: { A: "old" } });
await loadMessageCatalog("ms_MY", 0);
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(5))
.mockResolvedValueOnce({ ok: true, data: { A: "new" } });
const cat = await loadMessageCatalog("ms_MY", 1_000); // well inside the 15-min TTL
expect(cat).toEqual({ A: "new" });
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/messages/catalog/ms_MY");
});
it("refetches at the same version after the TTL expires (defence in depth)", async () => {
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(4))
.mockResolvedValueOnce({ ok: true, data: { A: "a" } });
await loadMessageCatalog("en_US", 0);
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(4))
.mockResolvedValueOnce({ ok: true, data: { A: "b" } });
const cat = await loadMessageCatalog("en_US", 16 * 60 * 1000);
expect(cat).toEqual({ A: "b" });
});
it("serves the last-known catalog when the version fetch fails", async () => {
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(4))
.mockResolvedValueOnce({ ok: true, data: { A: "a" } });
await loadMessageCatalog("en_US", 0);
mocks.callMobileApi.mockResolvedValueOnce({ ok: false, error: "version endpoint down" });
const cat = await loadMessageCatalog("en_US", 16 * 60 * 1000);
expect(cat).toEqual({ A: "a" });
// no catalog refetch after the version fetch failed — the last-known catalog is served
expect(mocks.callMobileApi).toHaveBeenCalledTimes(3);
});
it("serves the last-known catalog when the catalog fetch fails at a new version", async () => {
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(4))
.mockResolvedValueOnce({ ok: true, data: { A: "a" } });
await loadMessageCatalog("in_ID", 0);
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(5))
.mockResolvedValueOnce({ ok: false, error: "catalog endpoint down" });
const cat = await loadMessageCatalog("in_ID", 1_000);
expect(cat).toEqual({ A: "a" }); // stale data served, not blank
});
it("falls back to the plain-TTL path when the version fetch fails and nothing is cached yet", async () => {
// Version endpoint down + no last-known: behave like today (fetch the catalog directly).
mocks.callMobileApi
.mockResolvedValueOnce({ ok: false, error: "version endpoint down" })
.mockResolvedValueOnce({ ok: true, data: { A: "a" } });
const cat = await loadMessageCatalog("zh_TW", 0);
expect(cat).toEqual({ A: "a" });
// Second load within the TTL serves from the legacy cache without a catalog refetch.
mocks.callMobileApi.mockResolvedValueOnce({ ok: false, error: "version endpoint down" });
const again = await loadMessageCatalog("zh_TW", 1_000);
expect(again).toEqual({ A: "a" });
expect(mocks.callMobileApi).toHaveBeenCalledTimes(3);
});
it("returns an empty catalog when everything fails and nothing is cached", async () => {
mocks.callMobileApi
.mockResolvedValueOnce({ ok: false, error: "version endpoint down" })
.mockResolvedValueOnce({ ok: false, error: "catalog endpoint down" });
const cat = await loadMessageCatalog("in_ID");
expect(cat).toEqual({});
});
it("encodes the locale into the catalog URL path", async () => {
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(1))
.mockResolvedValueOnce({ ok: true, data: {} });
await loadMessageCatalog("zh_TW");
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/messages/catalog/zh_TW");
});
it("clearMessageCache empties the cache", async () => {
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(4))
.mockResolvedValueOnce({ ok: true, data: { A: "a" } });
await loadMessageCatalog("en_US", 0);
clearMessageCache();
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(4))
.mockResolvedValueOnce({ ok: true, data: { A: "a" } });
await loadMessageCatalog("en_US", 1_000);
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/messages/catalog/en_US");
});
it("keeps at most the current and previous version per locale after many bumps", async () => {
for (let v = 1; v <= 10; v++) {
mocks.callMobileApi
.mockResolvedValueOnce(okVersion(v))
.mockResolvedValueOnce({ ok: true, data: { A: `v${v}` } });
await loadMessageCatalog("en_US", 0);
}
// Bounded retention: the oldest versions were evicted, not kept forever.
expect(cachedCatalogEntryCount("en_US")).toBeLessThanOrEqual(2);
// The last-known fallback still works after the evictions.
mocks.callMobileApi.mockResolvedValueOnce({ ok: false, error: "version endpoint down" });
const cat = await loadMessageCatalog("en_US", 0);
expect(cat).toEqual({ A: "v10" });
});
});