const assert = require('assert'); const { execFileSync } = require('child_process'); const http = require('http'); const mysql = process.env.MYSQL_BIN; const base = process.env.API_BASE || 'http://yjyf:9560/api'; function sql(query) { return execFileSync(mysql, ['-h127.0.0.1', '-u' + process.env.DB_USER, '-D' + process.env.DB_NAME, '-N', '-B', '-e', query], { encoding: 'utf8' }).trim(); } function request(path, data, token) { return new Promise((resolve, reject) => { const body = data && new URLSearchParams(data).toString(); const url = new URL(base + path); const req = http.request(url, { method: body ? 'POST' : 'GET', headers: { ...(body ? {'content-type': 'application/x-www-form-urlencoded', 'content-length': Buffer.byteLength(body)} : {}), ...(token ? {token} : {}) } }, res => { let text = ''; res.on('data', chunk => text += chunk); res.on('end', () => { try { resolve(JSON.parse(text)); } catch (e) { reject(e); } }); }); req.on('error', reject); if (body) req.write(body); req.end(); }); } (async () => { const login = await request('/auth/login', {mobile: process.env.TEST_MOBILE, password: process.env.TEST_PASSWORD}); assert.strictEqual(login.code, 1, 'login failed'); const {token, userinfo} = login.data; const uid = Number(userinfo.id); assert(Number.isInteger(uid) && uid > 0); for (const type of ['all', 'recharge', 'consume', 'refund']) { const filter = type === 'all' ? '' : ` AND type='${type}'`; const expected = sql(`SELECT id FROM fa_wallet_log WHERE user_type='user' AND user_id=${uid}${filter} ORDER BY id DESC`); const ids = expected ? expected.split(/\s+/).map(Number) : []; for (const page of [1, 2]) { const res = await request(`/user/walletLogs?type=${type}&page=${page}&limit=3`, null, token); assert.strictEqual(res.code, 1, `${type} page ${page} request failed`); assert.strictEqual(res.data.total, ids.length, `${type} total`); assert.deepStrictEqual(res.data.list.map(row => row.id), ids.slice((page - 1) * 3, page * 3), `${type} page ${page} must contain only matching user records`); assert.strictEqual(res.data.has_more, page * 3 < ids.length, `${type} has_more`); } } console.log('Wallet bill user, type, count and pagination checks passed'); })().catch(err => { console.error(err); process.exitCode = 1; });