Files
docs/悦/货代SAAS平台后台/tests/verify-customer-recharge-application.cjs
T
2026-08-21 09:20:26 +08:00

84 lines
4.8 KiB
JavaScript

const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const root = path.resolve(__dirname, '..');
const moduleSource = fs.readFileSync(path.join(root, 'prototype-src/modules/08-finance.html'), 'utf8');
const overlaysSource = fs.readFileSync(path.join(root, 'prototype-src/overlays.html'), 'utf8');
const appSource = fs.readFileSync(path.join(root, 'prototype-src/app.js'), 'utf8');
assert.match(moduleSource, /充值申请记录/);
assert.match(moduleSource, /showCustomerRechargeApplication\(/);
assert.match(overlaysSource, /id="modal-customer-recharge-application"/);
assert.match(overlaysSource, /id="modal-customer-recharge-review"/);
assert.match(overlaysSource, /customer-recharge-qr/);
assert.match(overlaysSource, /id="customer-recharge-vouchers"[^>]*multiple[^>]*accept="\.pdf,\.jpg,\.jpeg,\.png"/);
assert.match(appSource, /customerRechargeApplications/);
assert.match(appSource, /function submitCustomerRechargeApplication\(/);
assert.match(appSource, /function reviewCustomerRechargeApplication\(/);
assert.match(appSource, /function approveCustomerRechargeApplication\(/);
assert.doesNotMatch(moduleSource, /id="modal-customer-recharge"/);
assert.doesNotMatch(appSource, /function showCustomerRecharge\(/);
assert.doesNotMatch(appSource, /function saveCustomerRecharge\(/);
const rechargeDataStart = appSource.indexOf('const customerRechargeApplications=');
const rechargeDataEnd = appSource.indexOf('const integrationData', rechargeDataStart);
const rechargeSource = appSource.slice(rechargeDataStart, rechargeDataEnd);
assert.match(rechargeSource, /referenceNo:/);
assert.match(rechargeSource, /app\.referenceNo/);
assert.doesNotMatch(rechargeSource, /\breference:/);
assert.doesNotMatch(rechargeSource, /app\.reference\b/);
const closeSource = extract('closeCustomerRechargeApplication');
for (const id of ['customer-recharge-application-id', 'customer-recharge-customer', 'customer-recharge-current', 'customer-recharge-amount', 'customer-recharge-date', 'customer-recharge-ref', 'customer-recharge-remark', 'customer-recharge-vouchers']) {
assert.match(closeSource, new RegExp(id), `close must clear ${id}`);
}
const reviewSource = extract('reviewCustomerRechargeApplication');
assert.match(reviewSource, /未找到充值申请/);
const handleSource = extract('handleCustomerRechargeReview');
assert.match(handleSource, /未找到充值申请/);
assert.match(handleSource, /未找到客户账户/);
assert.ok(handleSource.indexOf("customerAccountData.find") < handleSource.indexOf("customerAccountTransactions[app.accountId]"), 'account must be validated before transaction storage access');
function extract(name) {
const match = appSource.match(new RegExp(`function ${name}\\([^]*?\\n\\}`));
assert.ok(match, `missing ${name}`);
return match[0];
}
const normalizeSource = extract('normalizeCustomerRechargeAmount');
const normalize = new Function(`${normalizeSource}; return normalizeCustomerRechargeAmount;`)();
assert.equal(normalize('12'), 12);
assert.equal(normalize('12.3'), 12.3);
assert.equal(normalize('12.345'), null);
assert.equal(normalize('-1'), null);
assert.equal(normalize(''), null);
const approveSource = extract('approveCustomerRechargeApplication');
const approve = new Function(`${approveSource}; return approveCustomerRechargeApplication;`)();
const account = { id: 'CA-X', balance: 100, frozen: 0, status: '正常', lastChange: '' };
const application = { id: 'a1', accountId: 'CA-X', amount: 25.5, arrivalDate: '2026-08-20', status: '待审核', vouchers: [] };
const transactions = [];
const first = approve(application, account, transactions, { decision: '通过', now: '2026/8/20 10:00', operator: '财务' });
assert.equal(first.credited, true);
assert.equal(first.processed, true);
assert.match(first.transaction.no, /^AC20260820\d{8}$/);
assert.equal(account.balance, 125.5);
assert.equal(transactions.length, 1);
const second = approve(application, account, transactions, { decision: '通过', now: '2026-08-20 10:01', operator: '财务' });
assert.equal(second.credited, false);
assert.equal(second.processed, false);
assert.equal(account.balance, 125.5);
assert.equal(transactions.length, 1);
const rejected = { id: 'a2', accountId: 'CA-X', amount: 10, status: '待审核', vouchers: [] };
const firstReject = approve(rejected, account, transactions, { decision: '驳回', remark: '金额不符', now: '2026-08-20 10:02', operator: '财务' });
assert.equal(firstReject.processed, true);
assert.equal(firstReject.credited, false);
const repeatedReject = approve(rejected, account, transactions, { decision: '驳回', remark: '再次驳回', now: '2026-08-20 10:03', operator: '财务' });
assert.equal(repeatedReject.processed, false);
assert.equal(transactions.length, 1);
console.log('客户充值申请与审核回归校验通过');