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
142 lines
5.9 KiB
TypeScript
142 lines
5.9 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/counter/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/counter/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 (redirectUrl) and calls the COUNTER split path", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: true,
|
|
data: { success: true, uuid: "o-1", redirectUrl: "/checkout/return/o-1" },
|
|
});
|
|
const res = await POST(
|
|
postReq({ buyerType: "MEMBER", memberCode: "MY0001", lines: [], walletAmounts: { PRODUCT: 10 }, cashAmount: 0, gatewayAmount: 20, candidatePin: "1234", clientKey: "CK" }),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json();
|
|
expect(body.redirectUrl).toBe("/checkout/return/o-1");
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith(
|
|
"/api/admin/counter/split/sale",
|
|
expect.objectContaining({ method: "POST", token: "jwt-admin" }),
|
|
);
|
|
});
|
|
|
|
it("forwards a structured logical failure (wrong PIN) verbatim as 200", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "Request failed",
|
|
extra: { success: false, pinVerifyStatus: "FAIL", messages: ["PIN incorrect"] },
|
|
});
|
|
const res = await POST(postReq({ memberCode: "MY0001", walletAmounts: { PRODUCT: 10 }, candidatePin: "0000" }));
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json();
|
|
expect(body.success).toBe(false);
|
|
expect(body.pinVerifyStatus).toBe("FAIL");
|
|
});
|
|
|
|
it("surfaces counter rejection messages (no envelope) as 400", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "Request failed",
|
|
extra: { messages: ["Invalid split allocation"] },
|
|
});
|
|
const res = await POST(postReq({ memberCode: "MY0001", walletAmounts: {} }));
|
|
expect(res.status).toBe(400);
|
|
const body = await res.json();
|
|
expect(body.error).toBe("Invalid split allocation");
|
|
expect(body.error).not.toBe("Request failed");
|
|
});
|
|
|
|
it("forwards a real HTTP error (no extra) with its status", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-admin");
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "Insufficient wallet balance to complete this sale.",
|
|
status: 402,
|
|
});
|
|
const res = await POST(postReq({ memberCode: "MY0001", walletAmounts: { PRODUCT: 10 }, candidatePin: "1234" }));
|
|
expect(res.status).toBe(402);
|
|
expect((await res.json()).error).toBe("Insufficient wallet balance to complete this sale.");
|
|
});
|
|
});
|
|
|
|
describe("POST /api/counter/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" },
|
|
});
|
|
});
|
|
});
|