// 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 | null (undefined -> {}, invalid -> null) // walletCap(option: {walletTypeCode: string; max: number}, due: number, amounts: Record): number // isWalletRefusal(body: unknown): boolean // refusalWallet(body: unknown): string | null // walletAmountsKey(amounts: Record): 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 })); }); });