141 行
7.1 KiB
TypeScript
141 行
7.1 KiB
TypeScript
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
/**
|
|
* 资金闭环闸门:结算入口、退款回冲、组局同事务、分销封顶、支付后状态。
|
|
*/
|
|
describe('资金闭环闸门', () => {
|
|
const root = path.resolve(__dirname, '../../');
|
|
const read = (rel: string) => fs.readFileSync(path.join(root, rel), 'utf-8');
|
|
const repo = read('server/src/db/repository.ts');
|
|
const index = read('server/src/index.ts');
|
|
const list = read('pwuniapp/pages/order/list.vue');
|
|
|
|
const sliceFn = (code: string, name: string, len = 12000) => {
|
|
const start = code.indexOf(name);
|
|
expect(start).toBeGreaterThan(-1);
|
|
return code.slice(start, start + len);
|
|
};
|
|
|
|
it('完单结算只从待确认入账,退款回冲已入账佣金', () => {
|
|
const settle = sliceFn(repo, 'async completeOrderWithSettlement(');
|
|
expect(settle).toContain("String(order.status) !== '待确认'");
|
|
expect(settle).toContain('请先由大神申请完成并进入待确认');
|
|
expect(repo).toContain('async clawbackOrderSettlements(');
|
|
expect(repo).toContain("type = '结算回冲'");
|
|
});
|
|
|
|
it('退款按剩余可退,部分结算走平台抽成配置', () => {
|
|
const refund = sliceFn(repo, 'async adminRefundOrder(', 8000);
|
|
expect(refund).toContain('totalOrderAmt - alreadyRefunded');
|
|
expect(refund).toContain('loadPayoutSplit');
|
|
expect(refund).not.toContain('* 0.9');
|
|
const arb = sliceFn(repo, 'async handleArbitration(', 12000);
|
|
expect(arb).toContain('loadPayoutSplit');
|
|
expect(arb).toContain('clawbackOrderSettlements');
|
|
expect(arb).not.toContain('* 0.9');
|
|
});
|
|
|
|
it('组局核销、退席、解散同事务,收入记佣金且不可重复退', () => {
|
|
const verify = sliceFn(repo, 'async verifyCheckin(');
|
|
expect(verify).toContain('beginTransaction');
|
|
expect(verify).toContain('this.settleActivityCheckinAndReferral(');
|
|
const settle = sliceFn(repo, 'async settleActivityCheckinAndReferral(');
|
|
expect(settle).toContain('UPDATE users SET commission');
|
|
expect(repo).toContain('活动已解散,无法报名');
|
|
expect(repo).toContain('活动已解散,不可重复退款');
|
|
expect(repo).toContain("'活动核销回冲'");
|
|
});
|
|
|
|
it('分销从平台抽成列支,一二级比例之和不得超过抽成', () => {
|
|
expect(repo).toContain('splitFundedPayout');
|
|
expect(repo).toContain('分销从平台抽成中列支');
|
|
const cfg = sliceFn(repo, 'async updateFinanceConfig(');
|
|
expect(cfg).toContain('一级与二级分销比例之和不能超过平台抽成比例');
|
|
});
|
|
|
|
it('支付成功按是否指定大神写入待接单或履约中,超时与未支付取消补齐券和流水', () => {
|
|
const pay = sliceFn(repo, 'async applyPaymentSuccess(');
|
|
expect(pay).toContain("THEN '待接单'");
|
|
expect(pay).toContain("THEN '履约中'");
|
|
const timeout = sliceFn(repo, 'async processOrderTimeouts(');
|
|
expect(timeout).toContain('reverseUnpaidWechatLedger');
|
|
expect(timeout).toContain("status = '履约中'");
|
|
expect(timeout).toContain('claimed_by IS NULL OR claimed_by = 0');
|
|
expect(index).toContain('repo.adminCancelOrder(orderId)');
|
|
const route = index.indexOf("app.post('/api/orders/:id/pay'");
|
|
const sandbox = index.slice(route, route + 4200);
|
|
expect(sandbox).toContain("const paidStatus = isMarketplace ? '待接单' : '履约中'");
|
|
expect(sandbox).toContain("const allowedSandboxStatuses = ['履约中', '待接单']");
|
|
});
|
|
|
|
it('后台直接退款和仲裁只认管理员令牌', () => {
|
|
const refundRoute = index.indexOf("app.post('/api/orders/:id/admin-refund'");
|
|
const refundBlock = index.slice(refundRoute, refundRoute + 700);
|
|
expect(refundBlock).toContain('requireAdmin');
|
|
expect(refundBlock).toContain('isVerifiedAdminRequest(req)');
|
|
expect(refundBlock).not.toContain("req.headers['x-user-role']");
|
|
const arbRoute = index.indexOf("app.post('/api/arbitrations/:id/handle'");
|
|
const arbBlock = index.slice(arbRoute, arbRoute + 500);
|
|
expect(arbBlock).toContain('requireAdmin');
|
|
expect(arbBlock).toContain('isVerifiedAdminRequest(req)');
|
|
expect(arbBlock).not.toContain('operatorRole');
|
|
});
|
|
|
|
it('通用核销锁定具体票据并校验操作人', () => {
|
|
const verify = sliceFn(repo, 'async universalVerifyCheckin(');
|
|
expect(verify).toContain('a.creator_id = ?');
|
|
expect(verify).toContain('WHERE p.id = ? AND p.verify_code = ?');
|
|
expect(verify).toContain('FOR UPDATE');
|
|
expect(verify).toContain('无权核销该门票(非活动发起人或承接大神)');
|
|
expect(verify).toContain('核销码对应多张未核销门票,无法确定目标');
|
|
expect(verify).toContain("status IN ('履约中', '服务中')");
|
|
expect(verify).not.toContain('ORDER BY (a.creator_id = ?) DESC, p.created_at DESC\n LIMIT 1');
|
|
});
|
|
|
|
it('已结算订单不会把退款状态改回已完成', () => {
|
|
const settle = sliceFn(repo, 'async completeOrderWithSettlement(');
|
|
expect(settle).toContain('已阻止覆盖退款状态');
|
|
const settledAt = settle.indexOf('if (alreadySettled)');
|
|
const nextGate = settle.indexOf("String(order.status) !== '待确认'", settledAt);
|
|
const branch = settle.slice(settledAt, nextGate);
|
|
expect(branch).toContain("AND status = '待确认'");
|
|
expect(branch).not.toContain('WHERE id = ?", [order.id]');
|
|
});
|
|
|
|
it('退款记录先于微信请求落库,处理中占用可退额度', () => {
|
|
const fn = index.indexOf('setWeChatRefundFn(async');
|
|
const block = index.slice(fn, fn + 2800);
|
|
const saveAt = block.indexOf('先写入退款记录');
|
|
const createAt = block.indexOf('createRefund(');
|
|
expect(saveAt).toBeGreaterThan(-1);
|
|
expect(createAt).toBeGreaterThan(saveAt);
|
|
const pay = sliceFn(repo, 'private async occupyRefundSlot(');
|
|
expect(pay).toContain('GET_LOCK');
|
|
expect(pay).toContain('canReserveRefund');
|
|
expect(pay).toContain('已有退款正在处理或超出可退金额');
|
|
const done = sliceFn(repo, 'async completeAcceptedWechatRefund(');
|
|
expect(done).toContain('退款记录尚未落库,拒绝确认该回调');
|
|
expect(done).toContain('部分退款剩余结算');
|
|
const settle = sliceFn(repo, 'async completeOrderWithSettlement(');
|
|
expect(settle).toContain('退款处理中,暂不可结算');
|
|
});
|
|
|
|
it('微信退款只有 SUCCESS 才结案,PROCESSING 等回调补记', () => {
|
|
expect(index).toContain('pending: true');
|
|
expect(index).toContain('completeAcceptedWechatRefund');
|
|
const pay = sliceFn(repo, 'private async refundOrderPayment(');
|
|
expect(pay).toContain("wxStatus === 'SUCCESS'");
|
|
expect(pay).toContain('r.pending');
|
|
expect(pay).toContain('到账确认前不会把订单标成已退款');
|
|
});
|
|
|
|
it('小程序确认验收只在待确认出现,列表支付不再自带履约中', () => {
|
|
expect(list).toContain("order.rawStatus === '待确认'");
|
|
const payStart = list.indexOf('const payOrder = async');
|
|
const pay = list.slice(payStart, payStart + 1400);
|
|
expect(pay).not.toContain("targetStatus: '履约中'");
|
|
expect(index).toContain('确认验收仅买家本人或平台管理员可操作,大神请先申请完成服务');
|
|
});
|
|
});
|