|
@@ -3,7 +3,7 @@ const { readFileSync } = require('node:fs');
|
|
|
const { test } = require('node:test');
|
|
const { test } = require('node:test');
|
|
|
const vm = require('node:vm');
|
|
const vm = require('node:vm');
|
|
|
|
|
|
|
|
-function fixture({ result, prepareFails } = {}) {
|
|
|
|
|
|
|
+function fixture({ result, prepareFails, kind = 'recharge' } = {}) {
|
|
|
let definition;
|
|
let definition;
|
|
|
const calls = [];
|
|
const calls = [];
|
|
|
const opener = {
|
|
const opener = {
|
|
@@ -11,15 +11,19 @@ function fixture({ result, prepareFails } = {}) {
|
|
|
returnFromNativePayment: (order, outcome) => calls.push(['return', order.orderId, outcome]),
|
|
returnFromNativePayment: (order, outcome) => calls.push(['return', order.orderId, outcome]),
|
|
|
};
|
|
};
|
|
|
const app = { Parse: {
|
|
const app = { Parse: {
|
|
|
- User: { current: () => ({ id: 'fixture-user' }) },
|
|
|
|
|
- Cloud: { run: async (name, input) => {
|
|
|
|
|
- calls.push([name, input.orderId, input.loginCode]);
|
|
|
|
|
- if (prepareFails) throw { code: prepareFails === true ? 209 : prepareFails };
|
|
|
|
|
- return { timeStamp: '1796000000', nonceStr: 'fixture', package: 'prepay_id=fixture', paySign: 'stub', signType: 'MD5' };
|
|
|
|
|
- } },
|
|
|
|
|
|
|
+ User: { current: () => ({ id: 'fixture-user', getSessionToken: () => 'fixture-token' }) },
|
|
|
} };
|
|
} };
|
|
|
const wx = {
|
|
const wx = {
|
|
|
login: ({ success }) => success({ code: 'fixture-code' }),
|
|
login: ({ success }) => success({ code: 'fixture-code' }),
|
|
|
|
|
+ request: ({ url, data, success }) => {
|
|
|
|
|
+ assert.equal(url,'https://server.fmode.cn/api/functions');
|
|
|
|
|
+ assert.equal(data.id,'uw9AcFYAxb');
|
|
|
|
|
+ assert.equal(data._SessionToken,'fixture-token');
|
|
|
|
|
+ calls.push([data.action, data.orderId, data.loginCode, data.kind]);
|
|
|
|
|
+ if (prepareFails) success({statusCode:200,data:{code:prepareFails === true ? 209 : prepareFails}});
|
|
|
|
|
+ else success({statusCode:200,data:{code:200,success:true,data:{ timeStamp: '1796000000', nonceStr: 'fixture',
|
|
|
|
|
+ package: 'prepay_id=fixture', paySign: 'stub', signType: 'MD5' }}});
|
|
|
|
|
+ },
|
|
|
requestPayment: ({ package: payPackage, success, fail }) => {
|
|
requestPayment: ({ package: payPackage, success, fail }) => {
|
|
|
calls.push(['cashier', payPackage]);
|
|
calls.push(['cashier', payPackage]);
|
|
|
if (result) fail({ errMsg: result });
|
|
if (result) fail({ errMsg: result });
|
|
@@ -31,19 +35,21 @@ function fixture({ result, prepareFails } = {}) {
|
|
|
Page: x => { definition = x; }, getApp: () => app, getCurrentPages: () => [opener, {}], wx, setTimeout, clearTimeout,
|
|
Page: x => { definition = x; }, getApp: () => app, getCurrentPages: () => [opener, {}], wx, setTimeout, clearTimeout,
|
|
|
});
|
|
});
|
|
|
const page = { ...definition, data: { ...definition.data }, setData(patch) { Object.assign(this.data, patch); } };
|
|
const page = { ...definition, data: { ...definition.data }, setData(patch) { Object.assign(this.data, patch); } };
|
|
|
- page.onLoad({ orderId: 'testOrder1', kind: 'recharge', fen: '9900' });
|
|
|
|
|
|
|
+ page.onLoad({ orderId: 'testOrder1', kind, fen: '9900' });
|
|
|
return { page, calls };
|
|
return { page, calls };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-test('native cashier prepares the same order once and reports submitted, not settled', async () => {
|
|
|
|
|
- const { page, calls } = fixture();
|
|
|
|
|
- await Promise.all([page.pay(), page.pay()]);
|
|
|
|
|
- page.onUnload();
|
|
|
|
|
- assert.deepEqual(calls, [
|
|
|
|
|
- ['prepareYubanMiniPayment', 'testOrder1', 'fixture-code'], ['cashier', 'prepay_id=fixture'],
|
|
|
|
|
- ['return', 'testOrder1', 'submitted'], ['back'],
|
|
|
|
|
- ]);
|
|
|
|
|
-});
|
|
|
|
|
|
|
+for (const kind of ['membership','recharge']) {
|
|
|
|
|
+ test(kind + ' native cashier prepares once and reports submitted, not settled', async () => {
|
|
|
|
|
+ const { page, calls } = fixture({kind});
|
|
|
|
|
+ await Promise.all([page.pay(), page.pay()]);
|
|
|
|
|
+ page.onUnload();
|
|
|
|
|
+ assert.deepEqual(calls, [
|
|
|
|
|
+ ['prepare', 'testOrder1', 'fixture-code', kind], ['cashier', 'prepay_id=fixture'],
|
|
|
|
|
+ ['return', 'testOrder1', 'submitted'], ['back'],
|
|
|
|
|
+ ]);
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
test('native cancellation returns the order without marking it paid', async () => {
|
|
test('native cancellation returns the order without marking it paid', async () => {
|
|
|
const { page, calls } = fixture({ result: 'requestPayment:fail cancel' });
|
|
const { page, calls } = fixture({ result: 'requestPayment:fail cancel' });
|