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

69 lines
3.7 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { DEFAULT_MESSAGES } from "@/app/lib/i18n/defaultMessages";
import { m, tCode } from "@/test/i18nCatalog";
import { CATALOG_TABLES } from "./catalogTables";
import { SORT_LABELS, RULE_FIELD_LABELS, emptyCollection, validateCollection } from "./collections";
import { emptyPrice, emptySalesItem, emptySku, emptySkuVariant, validatePrices, validateSalesItem, validateSku, variantLabel } from "./catalogSetup";
// localise-admin-catalogue: the catalogue helpers hold message codes or translate through the caller's t,
// so no English is baked into module state. tCode renders every code as «Code», so English left over fails.
const isCode = (code: string | undefined) => code !== undefined && Object.prototype.hasOwnProperty.call(DEFAULT_MESSAGES, code);
describe("registries hold codes", () => {
it("CATALOG_TABLES labels, nouns and column labels are message codes", () => {
for (const table of CATALOG_TABLES) {
expect(isCode(table.label), table.label).toBe(true);
expect(isCode(table.noun), `${table.key} noun ${table.noun}`).toBe(true);
for (const column of table.columns) expect(isCode(column.label), column.label).toBe(true);
}
});
it("collection sort and rule labels are message codes", () => {
for (const code of [...Object.values(SORT_LABELS), ...Object.values(RULE_FIELD_LABELS)]) expect(isCode(code), code).toBe(true);
});
});
describe("validators translate through t", () => {
it("validateSku", () => {
const errors = validateSku({ ...emptySku(), variants: [] }, tCode);
expect(errors["sku.code"]).toBe(m("SkuErrCodeRequired"));
expect(errors["sku.name"]).toBe(m("SkuErrNameRequired"));
expect(errors["sku.variations"]).toBe(m("SkuErrAddVariant"));
});
it("validateSku interpolates the repeated barcode", () => {
const row = (barcode: string) => ({ barcode, batchNumber: "", expiryDate: "", status: "Y" as const, source: "MANUAL" as const });
const sku = { ...emptySku(), code: "A", name: "A", variants: [{ ...emptySkuVariant(), code: "A-1", weight: "1", barcodes: [row("ABC1234"), row("abc1234")] }] };
expect(validateSku(sku, tCode)["sku.variations[0].barcodes[1].barcode"]).toBe(m("SkuErrBarcodeRepeated", "abc1234"));
});
it("validateSalesItem", () => {
const errors = validateSalesItem({ ...emptySalesItem(), status: "Y" }, tCode);
expect(errors["salesItem.code"]).toBe(m("SalesItemErrCodeRequired"));
expect(errors["salesItem.brand"]).toBe(m("SalesItemErrBrandRequired"));
expect(errors["salesItem.images"]).toBe(m("SalesItemErrImageRequired"));
});
it("validatePrices", () => {
const errors = validatePrices([{ ...emptyPrice("missing"), priceType: "", price: "" }], emptySalesItem(), tCode, "2026-10-21");
expect(errors["pricing.rows[0].variationId"]).toBe(m("PricingErrPickVariation"));
expect(errors["pricing.rows[0].priceType"]).toBe(m("PricingErrPriceTypeRequired"));
expect(errors["pricing.rows[0].price"]).toBe(m("PricingErrPriceMin"));
});
it("validateCollection interpolates its limits", () => {
const draft = { ...emptyCollection("MANUAL"), title: "x".repeat(121), slug: "all" };
const errors = validateCollection(draft, tCode);
expect(errors.title).toBe(m("CollErrTitleMax", 120));
expect(errors.slug).toBe(m("CollErrSlugReserved", "all"));
expect(validateCollection(emptyCollection("MANUAL"), tCode).title).toBe(m("CollErrTitleRequired"));
});
});
describe("labels translate through t", () => {
it("variantLabel names the default variant from the catalog", () => {
expect(variantLabel("DEFAULT", "DEFAULT", tCode)).toBe(m("SkuVariantDefault"));
expect(variantLabel("Colour", "Blue", tCode)).toBe("Blue");
});
});