paymentSelection.helpers.test.mjs 5.3 KB
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { buildBoutiquePaymentSelectionPatch } from "../services/paymentSelection.helpers.js";

function createContext(overrides = {}) {
	return {
		configPayTypeList: [
			{ name: "门店收款", value: 2 },
			{ name: "微信支付", value: 1 },
			{ name: "POS机", value: 3 },
			{ name: "嘀加收款", value: 5 },
		],
		isErrorOrder: false,
		switchVlaue: "",
		selectOutTradeNoArr: [],
		paymentMethod: "",
		isAppointment: false,
		orderObj: {
			selectPayType: "",
			payeeType: "",
			isNewOrder: "Y",
		},
		...overrides,
	};
}

function assertPatchWithoutMutation(context, expected, message) {
	const snapshot = structuredClone(context);
	assert.deepEqual(buildBoutiquePaymentSelectionPatch(context), expected, message);
	assert.deepEqual(context, snapshot, `${message || "payment selection"}: input mutated`);
}

const djPayList = [
	{ name: "微信支付", value: 1 },
	{ name: "POS机", value: 3 },
	{ name: "对公转账", value: 6 },
	{ name: "其它收款", value: 7 },
];

const merchantPayList = [{ name: "门店收款", value: 2 }];

describe("paymentSelection.helpers", () => {
	it("IT-WO-PAYMENT-001: payment guide entries produce one complete and internally consistent patch", () => {
		const cases = [
			{
				name: "online guide",
				context: createContext({ paymentMethod: 1 }),
				expected: {
					djPayList,
					merchantPayList,
					showPayList: djPayList,
					"orderObj.selectPayType": 1,
					"orderObj.payeeType": 0,
				},
			},
			{
				name: "shop collection guide",
				context: createContext({ paymentMethod: 3 }),
				expected: {
					djPayList,
					merchantPayList,
					showPayList: merchantPayList,
					"orderObj.selectPayType": 2,
					"orderObj.payeeType": 1,
				},
			},
			{
				name: "bank transfer guide",
				context: createContext({ paymentMethod: 4 }),
				expected: {
					djPayList,
					merchantPayList,
					showPayList: djPayList,
					"orderObj.selectPayType": 6,
					"orderObj.payeeType": 0,
				},
			},
			{
				name: "other collection guide",
				context: createContext({ paymentMethod: 6 }),
				expected: {
					djPayList,
					merchantPayList,
					showPayList: djPayList,
					"orderObj.selectPayType": 7,
					"orderObj.payeeType": 0,
				},
			},
		];

		for (const { name, context, expected } of cases) {
			assertPatchWithoutMutation(context, expected, name);
		}
	});

	it("IT-WO-PAYMENT-002: correction and supplemental records retain the existing transaction-count matrix", () => {
		const cases = [
			{
				context: createContext({ isErrorOrder: true }),
				selected: 5,
			},
			{
				context: createContext({
					switchVlaue: 1,
					selectOutTradeNoArr: [{ outTradeNo: "trade-1" }],
				}),
				selected: 1,
			},
			{
				context: createContext({
					switchVlaue: 1,
					selectOutTradeNoArr: [{ outTradeNo: "trade-1" }, { outTradeNo: "trade-2" }],
				}),
				selected: 5,
			},
		];

		for (const { context, selected } of cases) {
			assertPatchWithoutMutation(context, {
				djPayList,
				merchantPayList,
				showPayList: djPayList,
				"orderObj.selectPayType": selected,
				"orderObj.payeeType": 0,
			});
		}
	});

	it("IT-WO-PAYMENT-003: a historical shop payment keeps the shop payee and list", () => {
		assertPatchWithoutMutation(
			createContext({
				orderObj: { selectPayType: 2, payeeType: 0, isNewOrder: "N" },
			}),
			{
				djPayList,
				merchantPayList,
				showPayList: merchantPayList,
				"orderObj.selectPayType": 2,
				"orderObj.payeeType": 1,
			},
		);
	});

	it("IT-WO-PAYMENT-004: a new order with no saved method selects the first configured online method", () => {
		assertPatchWithoutMutation(createContext(), {
			djPayList,
			merchantPayList,
			showPayList: djPayList,
			"orderObj.selectPayType": 1,
			"orderObj.payeeType": 0,
		});
	});

	it("IT-WO-PAYMENT-005: a historical order with no method falls back to shop collection", () => {
		assertPatchWithoutMutation(
			createContext({
				orderObj: { selectPayType: "", payeeType: "", isNewOrder: "N" },
			}),
			{
				djPayList,
				merchantPayList,
				showPayList: merchantPayList,
				"orderObj.selectPayType": 2,
				"orderObj.payeeType": 1,
			},
		);
	});

	it("IT-WO-PAYMENT-006: a saved payment that is no longer selectable is cleared atomically", () => {
		assertPatchWithoutMutation(
			createContext({
				orderObj: { selectPayType: 4, payeeType: 0, isNewOrder: "N" },
			}),
			{
				djPayList,
				merchantPayList,
				showPayList: [],
				"orderObj.selectPayType": "",
				"orderObj.payeeType": "",
			},
		);
	});

	it("IT-WO-PAYMENT-007: readonly payment entry preserves a saved shop selection in one complete patch", () => {
		assertPatchWithoutMutation(
			createContext({
				paymentMethod: 5,
				orderObj: { selectPayType: 2, payeeType: 1, isNewOrder: "N" },
			}),
			{
				djPayList,
				merchantPayList,
				showPayList: merchantPayList,
				"orderObj.selectPayType": 2,
				"orderObj.payeeType": 1,
			},
		);
	});

	it("IT-WO-PAYMENT-008: readonly payment entry preserves string-valued DJ fields without stale lists", () => {
		assertPatchWithoutMutation(
			createContext({
				paymentMethod: 5,
				orderObj: { selectPayType: "6", payeeType: "0", isNewOrder: "N" },
			}),
			{
				djPayList,
				merchantPayList,
				showPayList: djPayList,
				"orderObj.selectPayType": "6",
				"orderObj.payeeType": "0",
			},
		);
	});
});