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
110 lines
4.6 KiB
TypeScript
110 lines
4.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
|
|
|
import { POST } from "./route";
|
|
|
|
function postReq(body: unknown) {
|
|
return new Request("http://localhost/api/pos/split/sale", {
|
|
method: "POST",
|
|
body: JSON.stringify(body),
|
|
}) as unknown as Parameters<typeof POST>[0];
|
|
}
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset();
|
|
});
|
|
|
|
describe("POST /api/pos/split/sale", () => {
|
|
it("returns 401 when no session token", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
const res = await POST(postReq({}));
|
|
expect(res.status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns the success body and calls the POS split path", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { success: true, uuid: "o-9" } });
|
|
const res = await POST(
|
|
postReq({ buyerType: "MEMBER", memberCode: "MY0001", eventId: "77", lines: [], walletAmounts: {}, cashAmount: 5, gatewayAmount: 5, clientKey: "CK" }),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
expect((await res.json()).uuid).toBe("o-9");
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith(
|
|
"/api/admin/pos/split/sale",
|
|
expect.objectContaining({ method: "POST", token: "jwt-admin" }),
|
|
);
|
|
});
|
|
|
|
it("forwards a structured pin failure verbatim as 200", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "Request failed",
|
|
extra: { success: false, pinVerifyStatus: "LOCKED", messages: ["PIN locked"] },
|
|
});
|
|
const res = await POST(postReq({ memberCode: "MY0001", walletAmounts: { PRODUCT: 10 }, candidatePin: "0000" }));
|
|
expect(res.status).toBe(200);
|
|
expect((await res.json()).pinVerifyStatus).toBe("LOCKED");
|
|
});
|
|
});
|
|
|
|
describe("POST /api/pos/split/sale - per-wallet amounts (free-wallet-split)", () => {
|
|
const base = { memberCode: "MY0001", lines: [{ pPricingId: "1", qty: 1 }], candidatePin: "1234" };
|
|
const forwarded = () => mocks.callMobileApi.mock.calls[0][1].body as Record<string, unknown>;
|
|
|
|
it("forwards walletAmounts to mobile-api unchanged", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { success: true } });
|
|
const res = await POST(postReq({ ...base, walletAmounts: { PRODUCT: 50, ACORN: 20.5 } }));
|
|
expect(res.status).toBe(200);
|
|
expect(forwarded()).toMatchObject({ memberCode: "MY0001", walletAmounts: { PRODUCT: 50, ACORN: 20.5 } });
|
|
});
|
|
|
|
it("never forwards the retired election, walletAmount or splitWalletAmount", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { success: true } });
|
|
await POST(postReq({ ...base, walletAmounts: { PRODUCT: 5 }, election: "APPLE", walletAmount: 5, splitWalletAmount: 5 }));
|
|
const sent = forwarded();
|
|
expect(sent).not.toHaveProperty("election");
|
|
expect(sent).not.toHaveProperty("walletAmount");
|
|
expect(sent).not.toHaveProperty("splitWalletAmount");
|
|
});
|
|
|
|
it.each([
|
|
["an unknown wallet code", { BOGUS: 5 }],
|
|
["a negative amount", { PRODUCT: -1 }],
|
|
["more than 2 decimals", { PRODUCT: 1.005 }],
|
|
["a non-numeric amount", { PRODUCT: "5" }],
|
|
["an array", [5]],
|
|
])("rejects %s with 400 WalletAmountInvalid and never calls upstream", async (_name, walletAmounts) => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
const res = await POST(postReq({ ...base, walletAmounts }));
|
|
expect(res.status).toBe(400);
|
|
expect((await res.json()).code).toBe("WalletAmountInvalid");
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("relays an upstream wallet refusal as 400 with error, code and details", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "Acorns can only pay for yellow-label items.",
|
|
status: 400,
|
|
code: "AcornsOverYellowLabel",
|
|
details: { walletTypeCode: "ACORN" },
|
|
});
|
|
const res = await POST(postReq({ ...base, walletAmounts: { ACORN: 99 } }));
|
|
expect(res.status).toBe(400);
|
|
expect(await res.json()).toMatchObject({
|
|
error: "Acorns can only pay for yellow-label items.",
|
|
code: "AcornsOverYellowLabel",
|
|
details: { walletTypeCode: "ACORN" },
|
|
});
|
|
});
|
|
});
|