Files
mgm-daylight-mcrm-mobile-ios/tests/publicEndpointsMigration.test.mjs
2026-09-16 10:43:59 +08:00

49 lines
2.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import vm from 'node:vm';
const root = new URL('../', import.meta.url);
const app = fs.readFileSync(new URL('App.vue', root), 'utf8');
const method = app.slice(app.indexOf('async writeLog('), app.indexOf('async logout(){'));
test('actual APP log method routes by token and preserves debugMode gate', async () => {
for (const token of ['', null, undefined, 'valid-token']) {
const calls = [];
const scope = {
uni: {getStorageSync: key => key === 'token' ? token : {debugMode: true}},
writeDebugLog: async value => calls.push(['private', value]),
writePublicDebugLog: async value => calls.push(['public', value]), console,
};
const {writeLog} = vm.runInNewContext(`({${method}})`, scope);
await writeLog('80001', 'LOGIN', 'txn', 'description', null, null, 1, false);
assert.equal(calls.length, 1);
assert.equal(calls[0][0], token ? 'private' : 'public');
assert.equal(calls[0][1].eventCode, 'LOGIN');
scope.uni.getStorageSync = key => key === 'token' ? token : {debugMode: false};
await writeLog('80001', 'LOGIN', 'txn', 'description', null, null, 1, false);
assert.equal(calls.length, 1);
}
});
test('API wrappers use public bootstrap routes and preserve refresh protocol', async () => {
const calls = [];
const scope = {uni: {$u: {http: {post: (...args) => calls.push(args)}}}};
function load(file, names) {
const source = fs.readFileSync(new URL(file, root), 'utf8').replaceAll('export const ', 'const ');
return vm.runInNewContext(`${source}\n;({${names.join(',')}})`, scope);
}
const common = load('api/commonApi.js', ['getSettings', 'writeDebugLog', 'writePublicDebugLog', 'refreshToken']);
const config = load('api/configApi.js', ['getCfgSettingByKey']);
const params = {keyName: 'MACAU_STRING'};
const options = {custom: {test: true}};
common.getSettings(params, options); common.writeDebugLog(params, options);
common.writePublicDebugLog(params, options); config.getCfgSettingByKey(params, options);
common.refreshToken(params, options);
assert.deepEqual(calls.map(call => call[0]), [
'/system/sys_setting/get_public_login_settings', '/system/write_log', '/system/write_public_log',
'/web/setting/get_public_setting_by_key', '/app/refresh_token?isRenewRefreshToken=false',
]);
for (const call of calls) { assert.equal(call[1], params); assert.equal(call[2], options); }
});