feat(e2e): autologin patch (dev) + 登录态路由测试脚本

- util_web devAutologin: localStorage[_test_token]=user|pass -> 登录API写Hive+dispatch currentUser
- 延迟3s执行等 Flutter/Hive ready (否则 httpPost hang)
- main.dart 调用 devAutologin
- run_auth.js: 注入token遍历登录态路由
- 已验证: autologin API 200 OK
This commit is contained in:
2026-08-03 01:50:12 +08:00
parent 901e42a41b
commit 33b54fe454
10 changed files with 221 additions and 0 deletions

86
tools/e2e/run_auth.js Normal file
View File

@@ -0,0 +1,86 @@
// 登录态路由测试:注入 _test_tokenautologin 后遍历需登录路由
const { chromium } = require('playwright');
const BASE = 'http://127.0.0.1:8099';
const USER = '6477108200';
const PASS = '8818';
// 需登录路由(动态参数用合理默认)
const AUTH_ROUTES = [
'/me',
'/orders',
'/user-profile',
'/my-cards',
'/my-addresses/310',
'/coupons/310',
'/my-support/310',
'/new-ticket/310',
'/change-password',
'/checkout/310',
];
(async () => {
const browser = await chromium.launch({ headless: true });
const ctx = await browser.newContext({ viewport: { width: 1280, height: 900 } });
await ctx.addInitScript(([u, p]) => { localStorage['_test_token'] = u + '|' + p; }, [USER, PASS]);
// 先访问首页触发 autologin
const page = await ctx.newPage();
let autologinOk = false;
page.on('console', m => { if (m.text().includes('CK_AUTOLOGIN_OK')) autologinOk = true; });
console.log('触发 autologin...');
await page.goto(BASE + '/', { waitUntil: 'load', timeout: 60000 });
await page.waitForSelector('canvas', { timeout: 90000 });
await page.reload({ waitUntil: 'load' });
await page.waitForTimeout(8000);
console.log('autologin:', autologinOk ? '✓ 成功' : '✗ 未确认\n');
const results = [];
for (const route of AUTH_ROUTES) {
const errors = [];
const stacks = [];
const pageErrs = [];
page.removeAllListeners('console');
page.removeAllListeners('pageerror');
page.on('console', m => {
const t = m.text();
if (t.startsWith('CK_ERROR:')) errors.push(t.slice(0, 120));
if (t.startsWith('CK_STACK:')) {
const proj = t.match(/flutter_wisetronic\/[^\s]+:\d+/g);
if (proj) stacks.push([...new Set(proj)].slice(0, 2));
}
});
page.on('pageerror', e => pageErrs.push(e.message.slice(0, 120)));
try {
await page.goto(BASE + route, { waitUntil: 'load', timeout: 30000 });
await page.waitForTimeout(6000);
} catch (e) {
errors.push('GOTO: ' + e.message.slice(0, 80));
}
const allErr = [...errors, ...pageErrs];
const status = allErr.length === 0 ? '✓ OK' : `${allErr.length}`;
console.log(`${status.padEnd(10)} ${route}`);
if (allErr.length) {
console.log(` ${allErr[0].slice(0, 120)}`);
if (stacks.length) console.log(` 位置: ${JSON.stringify(stacks[0])}`);
}
results.push({ route, errors: allErr, stacks });
}
const failed = results.filter(r => r.errors.length > 0);
console.log(`\n========== 汇总 ==========`);
console.log(`${results.length} 个登录态路由,${failed.length} 个有错误`);
if (failed.length) {
const locMap = {};
for (const f of failed) {
const loc = f.stacks[0] ? f.stacks[0][0] : f.errors[0].slice(0, 40);
locMap[loc] = locMap[loc] || [];
locMap[loc].push(f.route);
}
for (const [loc, routes] of Object.entries(locMap)) {
console.log(` ${loc}\n${routes.join(', ')}`);
}
}
await browser.close();
})();