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
139 lines
5.1 KiB
TypeScript
139 lines
5.1 KiB
TypeScript
// walletAmounts contract (free-wallet-split Task 5), fixed here for the implementer. This is the admin
|
|
// portal's own copy of the helpers (the two portals share no code); no next/* imports.
|
|
// WALLET_ORDER: readonly ["GIFT","ACORN","PRODUCT","APPLE"]
|
|
// parseWalletAmounts(raw: unknown): Record<string, number> | null (undefined -> {}, invalid -> null)
|
|
// walletCap(option: {walletTypeCode: string; max: number}, due: number, amounts: Record<string, number>): number
|
|
// isWalletRefusal(body: unknown): boolean
|
|
// refusalWallet(body: unknown): string | null
|
|
// walletAmountsKey(amounts: Record<string, number>): string (stable, order-independent, zero entries ignored)
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
WALLET_ORDER,
|
|
isWalletRefusal,
|
|
parseWalletAmounts,
|
|
refusalWallet,
|
|
walletAmountsKey,
|
|
walletCap,
|
|
} from "./walletAmounts";
|
|
|
|
describe("WALLET_ORDER", () => {
|
|
it("lists Gift card, Acorns, Product, Apples in spend order", () => {
|
|
expect([...WALLET_ORDER]).toEqual(["GIFT", "ACORN", "PRODUCT", "APPLE"]);
|
|
});
|
|
});
|
|
|
|
describe("parseWalletAmounts", () => {
|
|
it("accepts per-wallet RM amounts", () => {
|
|
expect(parseWalletAmounts({ PRODUCT: 120, ACORN: 30 })).toEqual({ PRODUCT: 120, ACORN: 30 });
|
|
});
|
|
|
|
it("treats an empty object and an absent value as no wallet", () => {
|
|
expect(parseWalletAmounts({})).toEqual({});
|
|
expect(parseWalletAmounts(undefined)).toEqual({});
|
|
});
|
|
|
|
it("accepts every real wallet code and two decimals", () => {
|
|
expect(parseWalletAmounts({ GIFT: 1, ACORN: 2, PRODUCT: 3.25, APPLE: 0 })).toEqual({
|
|
GIFT: 1, ACORN: 2, PRODUCT: 3.25, APPLE: 0,
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
["an unknown wallet code", { GIFT_CARD: 1 }],
|
|
["a negative amount", { PRODUCT: -1 }],
|
|
["more than 2 decimals", { PRODUCT: 1.005 }],
|
|
["a string amount", { PRODUCT: "1" }],
|
|
["an array", []],
|
|
["a null entry", { PRODUCT: null }],
|
|
["NaN", { PRODUCT: Number.NaN }],
|
|
["Infinity", { PRODUCT: Number.POSITIVE_INFINITY }],
|
|
["null", null],
|
|
["a string", "PRODUCT"],
|
|
["a number", 5],
|
|
])("rejects %s", (_name, raw) => {
|
|
expect(parseWalletAmounts(raw)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("walletCap", () => {
|
|
const product = { walletTypeCode: "PRODUCT", max: 200 };
|
|
|
|
it("subtracts what the other wallets already cover from the due", () => {
|
|
expect(walletCap(product, 100, { ACORN: 30 })).toBe(70);
|
|
});
|
|
|
|
it("never exceeds the wallet's own max", () => {
|
|
expect(walletCap({ walletTypeCode: "PRODUCT", max: 40 }, 100, {})).toBe(40);
|
|
});
|
|
|
|
it("is 0 when the other wallets cover or exceed the due", () => {
|
|
expect(walletCap(product, 100, { ACORN: 100 })).toBe(0);
|
|
expect(walletCap(product, 100, { ACORN: 80, APPLE: 40 })).toBe(0);
|
|
});
|
|
|
|
it("does not count the wallet's own amount as 'others'", () => {
|
|
expect(walletCap(product, 200, { PRODUCT: 80, ACORN: 150 })).toBe(50);
|
|
});
|
|
|
|
it("works to 2 decimals without float drift", () => {
|
|
expect(walletCap({ walletTypeCode: "PRODUCT", max: 500 }, 100.1, { ACORN: 33.33 })).toBe(66.77);
|
|
});
|
|
});
|
|
|
|
describe("isWalletRefusal", () => {
|
|
it.each([
|
|
"WalletAmountOverBalance",
|
|
"AcornsOverYellowLabel",
|
|
"WalletAmountsOverDue",
|
|
"WalletAmountInvalid",
|
|
"WalletLegsDisabled",
|
|
])("is true for %s", (code) => {
|
|
expect(isWalletRefusal({ code })).toBe(true);
|
|
});
|
|
|
|
it("is false for other codes and non-objects", () => {
|
|
expect(isWalletRefusal({ code: "SomethingElse" })).toBe(false);
|
|
expect(isWalletRefusal({ error: "boom" })).toBe(false);
|
|
expect(isWalletRefusal(null)).toBe(false);
|
|
expect(isWalletRefusal("WalletAmountInvalid")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("refusalWallet", () => {
|
|
it("reads details.walletTypeCode", () => {
|
|
expect(refusalWallet({ code: "AcornsOverYellowLabel", details: { walletTypeCode: "ACORN" } })).toBe("ACORN");
|
|
});
|
|
|
|
it("falls back to a top-level walletTypeCode", () => {
|
|
expect(refusalWallet({ code: "WalletAmountOverBalance", walletTypeCode: "PRODUCT" })).toBe("PRODUCT");
|
|
});
|
|
|
|
it("is null when the refusal names no wallet", () => {
|
|
expect(refusalWallet({ code: "WalletAmountsOverDue", details: {} })).toBeNull();
|
|
expect(refusalWallet(null)).toBeNull();
|
|
expect(refusalWallet("x")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("walletAmountsKey", () => {
|
|
it("is the same for the same amounts in any key order", () => {
|
|
expect(walletAmountsKey({ PRODUCT: 50, ACORN: 20 })).toBe(walletAmountsKey({ ACORN: 20, PRODUCT: 50 }));
|
|
});
|
|
|
|
it("changes when any wallet amount changes", () => {
|
|
const base = walletAmountsKey({ PRODUCT: 50, ACORN: 20 });
|
|
expect(walletAmountsKey({ PRODUCT: 50, ACORN: 21 })).not.toBe(base);
|
|
expect(walletAmountsKey({ PRODUCT: 51, ACORN: 20 })).not.toBe(base);
|
|
expect(walletAmountsKey({ PRODUCT: 50, ACORN: 20, APPLE: 1 })).not.toBe(base);
|
|
});
|
|
|
|
it("ignores wallets set to 0 and treats no wallets as the empty key", () => {
|
|
expect(walletAmountsKey({ PRODUCT: 50, ACORN: 0 })).toBe(walletAmountsKey({ PRODUCT: 50 }));
|
|
expect(walletAmountsKey({})).toBe(walletAmountsKey({ APPLE: 0 }));
|
|
});
|
|
|
|
it("does not confuse which wallet holds an amount", () => {
|
|
expect(walletAmountsKey({ PRODUCT: 50 })).not.toBe(walletAmountsKey({ ACORN: 50 }));
|
|
});
|
|
});
|