1. 抽离底部导航归属逻辑为纯函数resolveEntryTab,优化分包页面高亮规则,对齐iOS HIG规范 2. 新增页面Tab通配符映射,简化PAGE_TAB_MAP维护 3. 修复鸿蒙平台webview返回按钮适配问题 4. 新增pages.json校验与导航标题自动注入脚本 5. 优化启动页动画性能,提升合成渲染效率 6. 修复diy-picture组件v-for缺少key的警告 7. 清理鸿蒙平台多余的分包构建配置 8. 更新统计数据与store注释说明
912 lines
32 KiB
JavaScript
912 lines
32 KiB
JavaScript
/**
|
|
* TabBar 高亮 — 行为测试 (按用户故事组织)
|
|
*
|
|
* 原则:
|
|
* - 每个测试都是一个真实的用户故事, 不是为了能测试过而迎合代码
|
|
* - 测试 input → 行为, 不测试代码结构 ("wiring 测试" 已删除)
|
|
* - 失败的测试 = 真实用户能看到的高亮错误
|
|
*
|
|
* 跑法 (需要 Node 18+):
|
|
* node --test tests/tabbar-highlight.test.js
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const vm = require('node:vm');
|
|
|
|
// =============================================================
|
|
// 加载被测源码 (ESM → CommonJS 沙箱)
|
|
// =============================================================
|
|
|
|
const TABBAR_SRC = fs.readFileSync(
|
|
path.join(__dirname, '..', 'common', 'js', 'util-tabbar.js'),
|
|
'utf8',
|
|
);
|
|
|
|
function loadTabbarInSandbox() {
|
|
const moduleObj = { exports: {} };
|
|
const wrapped = TABBAR_SRC
|
|
.replace(/^export\s+function\s+/gm, 'function ')
|
|
.replace(/^export\s+const\s+/gm, 'const ')
|
|
.replace(/^export\s+let\s+/gm, 'let ')
|
|
.replace(/^export\s+var\s+/gm, 'var ')
|
|
+ '\nmodule.exports = { resolvePageUrl, isPageInRouteList, resolveLinkUrl, '
|
|
+ 'makeSystemTabBarList, adaptSubpackageUrl, isTabBarRootPage, '
|
|
+ 'isCurrentPageTabBarRoot, checkTabBarActive, resolveEntryTab, lookupPageTabMap };\n';
|
|
const sandbox = { console, globalThis: {}, module: moduleObj, exports: moduleObj.exports };
|
|
sandbox.globalThis = sandbox;
|
|
vm.createContext(sandbox);
|
|
vm.runInContext(wrapped, sandbox, { filename: 'util-tabbar.js' });
|
|
return sandbox.module.exports;
|
|
}
|
|
|
|
const { resolveEntryTab, checkTabBarActive, lookupPageTabMap } = loadTabbarInSandbox();
|
|
|
|
// =============================================================
|
|
// 业务测试夹具
|
|
// =============================================================
|
|
|
|
// 5 个 Tab + 5 个分包的标准布局
|
|
const HOME = '/pages/index/index';
|
|
const CATEGORY = '/pages_goods/category';
|
|
const CART = '/pages_goods/cart';
|
|
const MERCHANT = '/pages_promotion/merch/merchcategory';
|
|
const MEMBER = '/pages_tool/member/index';
|
|
|
|
// 常见非 tab 落地页
|
|
const DIY = '/pages_tool/index/diy';
|
|
const MEMBER_INFO = '/pages_tool/member/info';
|
|
const MEMBER_COUPON = '/pages_tool/member/coupon';
|
|
const MEMBER_BALANCE = '/pages_tool/member/balance';
|
|
const GOODS_LIST = '/pages_goods/list';
|
|
const GOODS_DETAIL = '/pages_goods/detail';
|
|
const SEARCH = '/pages_tool/goods/search';
|
|
const BRAND = '/pages_tool/goods/brand';
|
|
const COUPON = '/pages_tool/goods/coupon';
|
|
const HELP_LIST = '/pages_tool/help/list';
|
|
const NOTICE_LIST = '/pages_tool/notice/list';
|
|
const FILES_LIST = '/pages_tool/files/list';
|
|
const FENXIAO = '/pages_promotion/fenxiao/index';
|
|
|
|
// 全屏流程 (modal 风格)
|
|
const LOGIN = '/pages_tool/login/login';
|
|
const PAY = '/pages_tool/pay/index';
|
|
const WEBVIEW = '/pages_tool/webview/webview';
|
|
|
|
function makeTabBarList(tabs) {
|
|
return {
|
|
list: tabs.map((t, i) => ({
|
|
id: t.id || (i + 1),
|
|
text: t.text || '',
|
|
link: { wap_url: t.url },
|
|
})),
|
|
};
|
|
}
|
|
|
|
const STANDARD_TABBAR = makeTabBarList([
|
|
{ id: 1, text: '首页', url: HOME },
|
|
{ id: 2, text: '分类', url: CATEGORY },
|
|
{ id: 3, text: '购物车', url: CART },
|
|
{ id: 4, text: '商户', url: MERCHANT },
|
|
{ id: 5, text: '我的', url: MEMBER },
|
|
]);
|
|
|
|
// PAGE_TAB_MAP: 显式声明的页面归属 (精确 + null)
|
|
const PAGE_TAB_MAP = {
|
|
// 首页频道
|
|
[HOME]: HOME,
|
|
'/pages/empty/index': HOME,
|
|
[HELP_LIST]: HOME,
|
|
[NOTICE_LIST]: HOME,
|
|
// 分类频道 — tab 根
|
|
[CATEGORY]: CATEGORY,
|
|
[GOODS_LIST]: CATEGORY,
|
|
[GOODS_DETAIL]: CATEGORY,
|
|
// 商户频道
|
|
[MERCHANT]: MERCHANT,
|
|
'/pages_promotion/merch/detail': MERCHANT,
|
|
// 购物车
|
|
[CART]: CART,
|
|
// 我的频道 — tab 根
|
|
[MEMBER]: MEMBER,
|
|
'/pages_order/list': MEMBER,
|
|
'/pages_tool/recharge/list': MEMBER,
|
|
'/pages_tool/recharge/order_list': MEMBER,
|
|
// 显式"不点亮"
|
|
[FILES_LIST]: null,
|
|
};
|
|
|
|
// 通配符 (按子树批量归属, 减少维护成本)
|
|
const PAGE_TAB_WILDCARDS = [
|
|
{ pattern: '/pages_tool/member/*', tab: MEMBER },
|
|
{ pattern: '/pages_tool/goods/*', tab: CATEGORY },
|
|
];
|
|
|
|
// Modal / 全屏流程页 (在归属判定中被跳过, 视为透明)
|
|
const NO_TAB_PAGES = new Set([
|
|
LOGIN, PAY, WEBVIEW,
|
|
'/pages_tool/ai-chat/index',
|
|
'/pages_tool/vr/index',
|
|
'/pages_tool/cases/index',
|
|
'/pages_tool/webview/webview',
|
|
]);
|
|
|
|
/**
|
|
* 端到端: 给定当前页面 + 历史栈, 算出哪个 tab 高亮
|
|
* @returns {string|null} tab URL 或 null
|
|
*/
|
|
function findHighlightedTab({ currentPageRoute, historyRoutes = [], tabBarList = STANDARD_TABBAR }) {
|
|
const entryTab = resolveEntryTab({
|
|
currentPageRoute,
|
|
tabBarList,
|
|
historyRoutes,
|
|
noTabPages: NO_TAB_PAGES,
|
|
});
|
|
const currentPackageName = currentPageRoute.split('?')[0].split('/')[1] || '';
|
|
const packageEntryTab = entryTab ? { [currentPackageName]: entryTab } : {};
|
|
for (const tab of tabBarList.list) {
|
|
if (!tab.link || !tab.link.wap_url) continue;
|
|
if (checkTabBarActive(
|
|
tab.link.wap_url,
|
|
currentPageRoute,
|
|
tabBarList,
|
|
packageEntryTab,
|
|
{},
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
)) {
|
|
return tab.link.wap_url;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function assertHighlight(actual, expected, label) {
|
|
assert.equal(actual, expected,
|
|
`[${label}] 期望 ${expected || '不点亮'}, 实际 ${actual || '不点亮'}`);
|
|
}
|
|
|
|
// =============================================================
|
|
// 场景 1: 跨包导航 — "从哪个 tab 出发, 非 tab 页就归属哪个 tab"
|
|
// =============================================================
|
|
|
|
test('场景 1.1: 用户从首页点 diy 链接 → 首页保持高亮', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [HOME],
|
|
}), HOME, 'home → diy');
|
|
});
|
|
|
|
test('场景 1.2: 用户从分类点 diy 链接 → 分类高亮', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [CATEGORY],
|
|
}), CATEGORY, 'category → diy');
|
|
});
|
|
|
|
test('场景 1.3: 用户从商户 tab 进 diy 微页面 → 商户高亮', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [MERCHANT],
|
|
}), MERCHANT, 'merchant → diy');
|
|
});
|
|
|
|
test('场景 1.4: 深层导航, 跳过非 tab 中间页', () => {
|
|
// 用户从首页逛到商品列表, 再点 diy 链接
|
|
// (中间经过非 tab 的 goods_list)
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [HOME, GOODS_LIST],
|
|
}), HOME, 'home → goods_list → diy');
|
|
});
|
|
|
|
test('场景 1.5: 经过 login 弹窗后, 仍记为来自原 tab', () => {
|
|
// 用户在 home, 弹登录页, 登录成功回到 diy
|
|
// login 是 modal, 不应该"洗掉"来源 tab
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [HOME, LOGIN],
|
|
}), HOME, 'home → login → diy');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 2: 同包导航 — 用户在当前包内浏览, 高亮当前包的 tab
|
|
// =============================================================
|
|
|
|
test('场景 2.1: /pages_goods/list → /pages_goods/detail 属于分类频道', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: GOODS_DETAIL,
|
|
historyRoutes: [GOODS_LIST],
|
|
}), CATEGORY, 'goods/list → detail');
|
|
});
|
|
|
|
test('场景 2.2: /pages_tool/member/coupon → /pages_tool/member/info 属于"我的"', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: MEMBER_INFO,
|
|
historyRoutes: [MEMBER_COUPON],
|
|
}), MEMBER, 'member/coupon → info');
|
|
});
|
|
|
|
test('场景 2.3: 在"我的"频道内多页浏览, 高亮始终是"我的"', () => {
|
|
// home → member → coupon → info (都在 pages_tool 包)
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: MEMBER_INFO,
|
|
historyRoutes: [HOME, MEMBER, MEMBER_COUPON],
|
|
}), MEMBER, 'home → member → coupon → info');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 3: tab 切换 — 用户点底部 tab 跳转
|
|
// =============================================================
|
|
|
|
test('场景 3.1: tab→tab 跳转, 目标 tab 高亮 (不受来源干扰)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: CATEGORY,
|
|
historyRoutes: [HOME],
|
|
}), CATEGORY, 'home → category tab');
|
|
});
|
|
|
|
test('场景 3.2: 在 diy 页面点"我的" tab → 我的高亮 (同包)', () => {
|
|
// 实际场景: diy 页面底部 TabBar 用户点"我的"
|
|
// 走 switchTab/reLaunch, 栈变成 [MEMBER]
|
|
// diy 的临时 packageEntryTab 会被 MEMBER 覆盖
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: MEMBER,
|
|
historyRoutes: [DIY],
|
|
}), MEMBER, 'diy → member tab');
|
|
});
|
|
|
|
test('场景 3.3: 在 diy 页面点"首页" tab → 首页高亮 (跨包)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: HOME,
|
|
historyRoutes: [DIY],
|
|
}), HOME, 'diy → home tab');
|
|
});
|
|
|
|
test('场景 3.4: 链式 tab 切换 home→分类→我的→购物车→首页', () => {
|
|
// 模拟用户连续点 tab, 每次都更新高亮
|
|
const tabs = [HOME, CATEGORY, MEMBER, CART, HOME];
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: tabs[i],
|
|
historyRoutes: tabs.slice(0, i),
|
|
}), tabs[i], `链式第 ${i + 1} 步: ${tabs[i]}`);
|
|
}
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 4: tab→tab 内部非 tab 页 — 通配符命中
|
|
// =============================================================
|
|
|
|
test('场景 4.1: 首页 → 商品搜索 → 分类高亮 (通配符 /pages_tool/goods/*)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: SEARCH,
|
|
historyRoutes: [HOME],
|
|
}), CATEGORY, 'home → search (goods/* 通配符)');
|
|
});
|
|
|
|
test('场景 4.2: 首页 → 商品品牌页 → 分类高亮 (通配符)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: BRAND,
|
|
historyRoutes: [HOME],
|
|
}), CATEGORY, 'home → brand (goods/* 通配符)');
|
|
});
|
|
|
|
test('场景 4.3: 首页 → 会员中心 → 我的高亮 (通配符 /pages_tool/member/*)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: MEMBER_INFO,
|
|
historyRoutes: [HOME],
|
|
}), MEMBER, 'home → member/info (member/* 通配符)');
|
|
});
|
|
|
|
test('场景 4.4: 首页 → 优惠券 → 我的高亮 (通配符)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: MEMBER_COUPON,
|
|
historyRoutes: [HOME],
|
|
}), MEMBER, 'home → member/coupon (member/* 通配符)');
|
|
});
|
|
|
|
test('场景 4.5: 首页 → 余额 → 我的高亮 (通配符)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: MEMBER_BALANCE,
|
|
historyRoutes: [HOME],
|
|
}), MEMBER, 'home → member/balance (member/* 通配符)');
|
|
});
|
|
|
|
test('场景 4.6: 分类 tab → 商品详情 → 分类保持高亮 (PAGE_TAB_MAP 精确优先)', () => {
|
|
// 即使来源是分类 tab, 商品详情仍在 PAGE_TAB_MAP 绑 CATEGORY
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: GOODS_DETAIL,
|
|
historyRoutes: [CATEGORY],
|
|
}), CATEGORY, 'category → goods/detail (PAGE_TAB_MAP wins)');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 5: 显式 PAGE_TAB_MAP 优先级
|
|
// =============================================================
|
|
|
|
test('场景 5.1: /pages_tool/files/list 显式绑 null → 不点亮 (业务规则: 文件页不归属任何 tab)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: FILES_LIST,
|
|
historyRoutes: [HOME],
|
|
}), null, 'home → files/list (null 优先)');
|
|
});
|
|
|
|
test('场景 5.2: /pages_tool/index/diy 不在 PAGE_TAB_MAP, 走 history-walk', () => {
|
|
// diy 不在精确表也不在通配符, 走 history-walk
|
|
// 从 home 来 → HOME 高亮
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [HOME],
|
|
}), HOME, 'diy 走 history-walk, home 高亮');
|
|
});
|
|
|
|
test('场景 5.3: diy 没在 PAGE_TAB_MAP 显式声明 (避免硬绑)', () => {
|
|
// 反向验证: 修复后, diy 不在 PAGE_TAB_MAP, 完全靠 history-walk
|
|
// 这是关键修复 — 之前 diy:HOME 导致深链误高亮
|
|
// 此测试防回归: 任何人重新加回 diy:HOME, 这个测试 + 行为测试都会挂
|
|
const currentMap = PAGE_TAB_MAP[DIY];
|
|
assert.equal(currentMap, undefined,
|
|
'diy 不应在 PAGE_TAB_MAP (修复后靠 history-walk 决定)');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 6: 深链冷启动 (扫码/分享) — iOS HIG
|
|
// =============================================================
|
|
|
|
test('场景 6.1: 用户扫码进 diy → 不高亮任何 tab (iOS HIG)', () => {
|
|
// 用户从来没在任何 tab 页面, 没有来源记忆
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [],
|
|
}), null, '深链 diy');
|
|
});
|
|
|
|
test('场景 6.2: 分享 diy 链接给朋友, 朋友点开 → 不高亮 (无历史)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY + '?id=123',
|
|
historyRoutes: [],
|
|
}), null, '深链 diy?id=123');
|
|
});
|
|
|
|
test('场景 6.3: 分享商品详情链接, 朋友点开 → 分类高亮 (通配符)', () => {
|
|
// 朋友点链接进入商品详情, 没有历史
|
|
// 商品详情在 PAGE_TAB_MAP 精确绑 CATEGORY → CATEGORY 高亮
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: GOODS_DETAIL,
|
|
historyRoutes: [],
|
|
}), CATEGORY, '深链 goods/detail');
|
|
});
|
|
|
|
test('场景 6.4: 分享会员中心链接, 朋友点开 → 我的高亮 (精确绑)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: MEMBER,
|
|
historyRoutes: [],
|
|
}), MEMBER, '深链 member (精确)');
|
|
});
|
|
|
|
test('场景 6.5: 分享 fenxiao 链接, 朋友点开 → 不高亮 (不在任何 map)', () => {
|
|
// fenxiao 不在 PAGE_TAB_MAP, 不在通配符, 不在 tabBarList
|
|
// iOS HIG: 不高亮
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: FENXIAO,
|
|
historyRoutes: [],
|
|
}), null, '深链 fenxiao');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 7: 全屏 modal 流程 — login / pay / webview
|
|
// =============================================================
|
|
|
|
test('场景 7.1: 在 home 点登录 → 全屏不点亮 (NO_TAB_PAGES)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: LOGIN,
|
|
historyRoutes: [HOME],
|
|
}), null, 'home → login (NO_TAB_PAGES)');
|
|
});
|
|
|
|
test('场景 7.2: 支付流程中 → 不点亮 (NO_TAB_PAGES)', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: PAY,
|
|
historyRoutes: [HOME, GOODS_LIST],
|
|
}), null, 'home → list → pay (NO_TAB_PAGES)');
|
|
});
|
|
|
|
test('场景 7.3: 登录后回到原页面, 高亮不丢失', () => {
|
|
// 实际场景: 用户在 home, 弹登录, 登录成功, 跳到 diy
|
|
// 期望: diy 仍归属 home (login 是 modal, 不算"离开" home)
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [HOME, LOGIN],
|
|
}), HOME, 'home → login → diy (modal 透明)');
|
|
});
|
|
|
|
test('场景 7.4: 支付完成后回到原页面, 高亮跟随当前包', () => {
|
|
// 用户: home → 进入商品区(逛购物车) → 支付 → 回到商品列表
|
|
// 实际栈: home → cart → pay → list
|
|
// 跳过 pay (NO_TAB_PAGES), 上一非 NO_TAB_PAGES 是 cart (pages_goods)
|
|
// 当前 list 在 pages_goods → 同包 → CATEGORY (商品频道的入口 tab)
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: GOODS_LIST,
|
|
historyRoutes: [HOME, CART, PAY],
|
|
}), CATEGORY, 'home → cart → pay → list (modal 透明 + 同包)');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 8: from_tab query 机制 (旧机制, 仍在用)
|
|
// =============================================================
|
|
|
|
test('场景 8.1: URL 带 from_tab=home → 强制高亮 home (即使当前在 pages_tool)', () => {
|
|
// diy-bottom-nav.vue#redirectTo 在用户点 TabBar 时会写 from_tab query
|
|
const result = checkTabBarActive(
|
|
HOME,
|
|
DIY,
|
|
STANDARD_TABBAR,
|
|
{},
|
|
{ from_tab: HOME },
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: {}, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
);
|
|
assert.equal(result, true, 'from_tab=home → HOME 高亮');
|
|
});
|
|
|
|
test('场景 8.2: from_tab=member 但 PAGE_TAB_MAP 已显式声明 → PAGE_TAB_MAP 优先', () => {
|
|
// PAGE_TAB_MAP 显式声明的页面, from_tab 不能覆盖
|
|
// 这是设计: 显式声明 > 任何 hint
|
|
const result = checkTabBarActive(
|
|
MEMBER,
|
|
DIY,
|
|
STANDARD_TABBAR,
|
|
{},
|
|
{ from_tab: MEMBER },
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: { [DIY]: HOME }, pageTabWildcards: [] },
|
|
);
|
|
assert.equal(result, false, 'PAGE_TAB_MAP 优先于 from_tab');
|
|
});
|
|
|
|
test('场景 8.3: 非法 from_tab 值 → 忽略, 走后续链路', () => {
|
|
// from_tab 不是任何 tab, 视为无效
|
|
const result = checkTabBarActive(
|
|
HOME,
|
|
DIY,
|
|
STANDARD_TABBAR,
|
|
{},
|
|
{ from_tab: '/foo/bar' },
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: {}, pageTabWildcards: [] },
|
|
);
|
|
assert.equal(result, false, '非法 from_tab → 忽略, 无其它信号 → 不亮');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 9: URL 参数不影响高亮判定
|
|
// =============================================================
|
|
|
|
test('场景 9.1: diy 带 query ?id=123&from=share → 行为不变', () => {
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: '/pages_tool/index/diy?id=123&from=share',
|
|
historyRoutes: [HOME],
|
|
}), HOME, 'home → diy?id=123&from=share');
|
|
});
|
|
|
|
test('场景 9.2: tab 根带 query 也精确匹配', () => {
|
|
// 即使带 ?id=123, 也能识别是 home tab
|
|
const result = resolveEntryTab({
|
|
currentPageRoute: '/pages/index/index?from=share',
|
|
tabBarList: STANDARD_TABBAR,
|
|
historyRoutes: [],
|
|
});
|
|
assert.equal(result, HOME, 'home tab 根 + query');
|
|
});
|
|
|
|
test('场景 9.3: 老路径 /pages/member/index 自动适配到 /pages_tool/member/index', () => {
|
|
// 兼容老 URL (旧业务数据)
|
|
const result = resolveEntryTab({
|
|
currentPageRoute: DIY,
|
|
tabBarList: STANDARD_TABBAR,
|
|
historyRoutes: ['/pages/member/index'],
|
|
});
|
|
assert.equal(result, MEMBER, '老路径自动 adaptSubpackageUrl');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 10: 一致性 — 同时只有 1 个 tab 高亮
|
|
// =============================================================
|
|
|
|
test('场景 10.1: 5 个 tab 中, 同时只有 1 个高亮 (互斥)', () => {
|
|
const results = STANDARD_TABBAR.list.map(t =>
|
|
checkTabBarActive(
|
|
t.link.wap_url,
|
|
DIY,
|
|
STANDARD_TABBAR,
|
|
{ pages_tool: HOME },
|
|
{},
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
)
|
|
);
|
|
const trueCount = results.filter(Boolean).length;
|
|
assert.equal(trueCount, 1, `5 个 tab 应只亮 1 个, 实际 ${trueCount} 个亮`);
|
|
});
|
|
|
|
test('场景 10.2: 同场景下 (current + history), 高亮结果应一致 (幂等)', () => {
|
|
// 同样的 input 调两次, 结果应一致
|
|
const opts = {
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [HOME, GOODS_LIST],
|
|
tabBarList: STANDARD_TABBAR,
|
|
};
|
|
const r1 = resolveEntryTab(opts);
|
|
const r2 = resolveEntryTab(opts);
|
|
assert.equal(r1, r2, 'resolveEntryTab 幂等');
|
|
assert.equal(r1, HOME, '且结果应为 home');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 11: 边界 — 输入异常时不能崩
|
|
// =============================================================
|
|
|
|
test('场景 11.1: tabBarList 为空 (服务端配置异常) → 任何 tab 都不亮', () => {
|
|
// 即使 diy 在 PAGE_TAB_MAP, 没有 tab 也没法亮
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [HOME],
|
|
tabBarList: { list: [] },
|
|
}), null, 'tabBarList 空');
|
|
});
|
|
|
|
test('场景 11.2: tabBarList.list 含脏数据 (link 为 null/空) → 不崩', () => {
|
|
const dirtyTabBar = {
|
|
list: [
|
|
{ id: 1, link: null },
|
|
{ id: 2, link: {} },
|
|
{ id: 3, text: '首页', link: { wap_url: HOME } },
|
|
{ id: 4, text: '我的', link: { wap_url: MEMBER } },
|
|
],
|
|
};
|
|
const r = resolveEntryTab({
|
|
currentPageRoute: DIY,
|
|
tabBarList: dirtyTabBar,
|
|
historyRoutes: [HOME],
|
|
});
|
|
assert.equal(r, HOME, '脏数据不影响查找');
|
|
});
|
|
|
|
test('场景 11.3: historyRoutes 含 null/空/无效值 → 安全跳过', () => {
|
|
const r = resolveEntryTab({
|
|
currentPageRoute: DIY,
|
|
tabBarList: STANDARD_TABBAR,
|
|
historyRoutes: [null, '', undefined, HOME],
|
|
});
|
|
assert.equal(r, HOME, 'historyRoutes 脏数据 → 跳过');
|
|
});
|
|
|
|
test('场景 11.4: historyRoutes 不是数组 → 视为无历史 (iOS HIG)', () => {
|
|
// 防御性编程: 异常入参不能崩
|
|
// 行为: 当作无历史 → 走 iOS HIG (无历史不点亮)
|
|
const r = resolveEntryTab({
|
|
currentPageRoute: DIY,
|
|
tabBarList: STANDARD_TABBAR,
|
|
historyRoutes: 'not-an-array',
|
|
});
|
|
assert.equal(r, null, 'historyRoutes 非数组 → 视为空 → 不点亮');
|
|
});
|
|
|
|
test('场景 11.5: 当前页面是 /pages_goods/some-unmapped-page 深链 → 不亮', () => {
|
|
// 业务上新加的商品页, 既不在 PAGE_TAB_MAP 也不在通配符
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: '/pages_goods/some-unmapped-page',
|
|
historyRoutes: [],
|
|
}), null, '深链 unmapped 页');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 12: 业务连续性 — packageEntryTab 惰性更新
|
|
// =============================================================
|
|
|
|
test('场景 12.1: 用户在 home → diy → 切到 member → 回 home → 进 diy, 仍归 home', () => {
|
|
// 完整路径模拟:
|
|
// stack=[home] (home 高亮)
|
|
// stack=[home, diy] (home 保持)
|
|
// stack=[member] (member 高亮, 弹栈)
|
|
// stack=[home] (用户返回)
|
|
// stack=[home, diy] (再次进 diy, 应仍归 home)
|
|
//
|
|
// 最后一步: 当前 diy, 历史 = [home]
|
|
// 期望: HOME 高亮 (因为用户在 home 上下文再次进了 diy)
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [HOME],
|
|
}), HOME, 'home → diy → member → 返回 → home → diy');
|
|
});
|
|
|
|
test('场景 12.2: 进入 cart tab 后, 旧 packageEntryTab 被新 tab 覆盖', () => {
|
|
// 用户在 home → diy (写入 pages_tool → home)
|
|
// 然后点 cart tab → 弹栈, 跳 CART
|
|
// 跳 CART 后: pages_goods → CART (覆盖旧 entry)
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: CART,
|
|
historyRoutes: [HOME],
|
|
}), CART, 'home → diy → 点 cart tab → CART');
|
|
});
|
|
|
|
test('场景 12.3: 登出后, 旧 packageEntryTab 保持 (惰性, 下次 updateCurrentRoute 重算)', () => {
|
|
// 登出不应清 packageEntryTab (故意惰性)
|
|
// 下次进新页面时 updateCurrentRoute 会基于新栈重算, 自然覆盖
|
|
// 这里只验证行为: 登出后访问 diy, 仍按 history-walk 决定
|
|
assertHighlight(findHighlightedTab({
|
|
currentPageRoute: DIY,
|
|
historyRoutes: [HOME],
|
|
}), HOME, '登出后 diy 高亮仍由 history-walk 决定');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 13: 核心单元测试 (resolveEntryTab 5 步 + lookupPageTabMap)
|
|
// =============================================================
|
|
|
|
test('单元 13.1: 当前是 tab 根 → 走 step 1 精确匹配, 不查 history', () => {
|
|
assert.equal(
|
|
resolveEntryTab({ currentPageRoute: CATEGORY, tabBarList: STANDARD_TABBAR, historyRoutes: [HOME, MEMBER] }),
|
|
CATEGORY,
|
|
'category 是 tab 根, 不被 history 干扰',
|
|
);
|
|
});
|
|
|
|
test('单元 13.2: lookupPageTabMap 精确匹配优先于通配符', () => {
|
|
// /pages_tool/member/index 在精确表里是 MEMBER
|
|
assert.equal(
|
|
lookupPageTabMap(MEMBER, PAGE_TAB_MAP, PAGE_TAB_WILDCARDS),
|
|
MEMBER,
|
|
);
|
|
});
|
|
|
|
test('单元 13.3: lookupPageTabMap 通配符命中非精确条目', () => {
|
|
assert.equal(
|
|
lookupPageTabMap('/pages_tool/member/balance_detail', PAGE_TAB_MAP, PAGE_TAB_WILDCARDS),
|
|
MEMBER,
|
|
'通配符 /pages_tool/member/* → MEMBER',
|
|
);
|
|
assert.equal(
|
|
lookupPageTabMap('/pages_tool/goods/brand', PAGE_TAB_MAP, PAGE_TAB_WILDCARDS),
|
|
CATEGORY,
|
|
'通配符 /pages_tool/goods/* → CATEGORY',
|
|
);
|
|
});
|
|
|
|
test('单元 13.4: lookupPageTabMap null 优先于通配符', () => {
|
|
// 即使有 /pages_tool/files/* 通配符, files/list 显式 null 应优先
|
|
const customMap = { [FILES_LIST]: null };
|
|
const customWildcards = [{ pattern: '/pages_tool/files/*', tab: HOME }];
|
|
assert.equal(
|
|
lookupPageTabMap(FILES_LIST, customMap, customWildcards),
|
|
null,
|
|
'files/list 显式 null 优先于通配符',
|
|
);
|
|
});
|
|
|
|
test('单元 13.5: lookupPageTabMap 无匹配 → undefined (走 history-walk)', () => {
|
|
assert.equal(
|
|
lookupPageTabMap(DIY, PAGE_TAB_MAP, PAGE_TAB_WILDCARDS),
|
|
undefined,
|
|
'diy 不在任何表, 走 history-walk',
|
|
);
|
|
});
|
|
|
|
test('单元 13.6: lookupPageTabMap 通配符不误匹配前缀相近路径', () => {
|
|
// /pages_tool/members (复数) 不会被 /pages_tool/member/* 匹配
|
|
assert.equal(
|
|
lookupPageTabMap('/pages_tool/members', PAGE_TAB_MAP, PAGE_TAB_WILDCARDS),
|
|
undefined,
|
|
'复数路径不匹配单数通配符',
|
|
);
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 14: checkTabBarActive 兜底 (直接函数调用, 验证边界)
|
|
// =============================================================
|
|
|
|
test('兜底 14.1: link URL 不在 tabBarList → 直接 false (早返回)', () => {
|
|
// 询问一个非法 link URL
|
|
const result = checkTabBarActive(
|
|
'/pages/non-existent-tab',
|
|
DIY,
|
|
STANDARD_TABBAR,
|
|
{ pages_tool: HOME },
|
|
{ from_tab: HOME },
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
);
|
|
assert.equal(result, false, '非法 link URL → false (无视所有信号)');
|
|
});
|
|
|
|
test('兜底 14.2: 当前页面在 NO_TAB_PAGES → 任何 tab 都不亮', () => {
|
|
for (const tabUrl of [HOME, CATEGORY, CART, MERCHANT, MEMBER]) {
|
|
const result = checkTabBarActive(
|
|
tabUrl,
|
|
LOGIN,
|
|
STANDARD_TABBAR,
|
|
{ pages: HOME, pages_tool: MEMBER },
|
|
{},
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
);
|
|
assert.equal(result, false, `login 页面, ${tabUrl} 不该亮`);
|
|
}
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 15: 核心 8 场景对照表 (业务高频路径)
|
|
// =============================================================
|
|
|
|
const SCENARIOS = [
|
|
{ label: '深链扫 diy', current: DIY, history: [], expected: null },
|
|
{ label: '首页 → diy 链接', current: DIY, history: [HOME], expected: HOME },
|
|
{ label: '首页 → 会员中心', current: MEMBER, history: [HOME], expected: MEMBER },
|
|
{ label: '分类 → diy 链接', current: DIY, history: [CATEGORY], expected: CATEGORY },
|
|
{ label: '分类 → 商品列表 → diy', current: DIY, history: [CATEGORY, GOODS_LIST], expected: CATEGORY },
|
|
{ label: '我的 → 优惠券 → diy', current: DIY, history: [MEMBER, MEMBER_COUPON], expected: MEMBER },
|
|
{ label: '首页 → 登录页 → diy', current: DIY, history: [HOME, LOGIN], expected: HOME },
|
|
{ label: '深链扫 member/info', current: MEMBER_INFO, history: [], expected: MEMBER },
|
|
];
|
|
|
|
test('场景对照: 8 个核心业务场景', () => {
|
|
for (const sc of SCENARIOS) {
|
|
assertHighlight(
|
|
findHighlightedTab({
|
|
currentPageRoute: sc.current,
|
|
historyRoutes: sc.history,
|
|
}),
|
|
sc.expected,
|
|
sc.label,
|
|
);
|
|
}
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 16: P0 修复验证 — 通用化 DIY_VIEW_INDEX
|
|
// =============================================================
|
|
|
|
test('场景 16.1: 在 home page (name=DIY_VIEW_INDEX), 自点 home tab → home 高亮', () => {
|
|
// 模拟: 用户在 home page, 点击 home tab
|
|
// verify 的 DIY_VIEW_INDEX 短路 + step 2 精确匹配, 都应高亮
|
|
// (实际测试中, 短路 vs 精确匹配谁先触发不重要, 结果都是 true)
|
|
const result = checkTabBarActive(
|
|
HOME,
|
|
HOME,
|
|
STANDARD_TABBAR,
|
|
{ pages: HOME },
|
|
{},
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
);
|
|
assert.equal(result, true, '在 home 上点 home tab → 高亮');
|
|
});
|
|
|
|
test('场景 16.2: 在 home page, 点 cart tab → cart 不亮 (无来源记忆)', () => {
|
|
// 用户在 home, 没去过 cart, packageEntryTab 只有 pages
|
|
const result = checkTabBarActive(
|
|
CART,
|
|
HOME,
|
|
STANDARD_TABBAR,
|
|
{ pages: HOME },
|
|
{},
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
);
|
|
// step 5 pageTabMap['/pages/index/index'] = HOME → 询问 cart 链接 → false
|
|
assert.equal(result, false, '在 home 上点 cart → cart 不该高亮');
|
|
});
|
|
|
|
test('场景 16.3: 在 home page, 点 merchant tab → 走 PAGE_TAB_MAP 兜底', () => {
|
|
// pageTabMap['/pages/index/index'] = HOME, 询问 merchant → false
|
|
const result = checkTabBarActive(
|
|
MERCHANT,
|
|
HOME,
|
|
STANDARD_TABBAR,
|
|
{ pages: HOME },
|
|
{},
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
);
|
|
assert.equal(result, false, 'home → merchant: HOME 优先, merchant 不亮');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 17: P0-2 修复 — tabBarList 异步加载竞态
|
|
// =============================================================
|
|
|
|
test('场景 17.1: tabBarList 异步加载场景 (文档化行为)', () => {
|
|
// 实际修复在 vue 组件里 (watch tabBarReady), 这里验证 resolveEntryTab 在 tabBarList 空时的行为
|
|
// 修复前: tabBarList 为空时, step 2 history-walk 会得到空 tabUrls, 走 fallback
|
|
// 修复后: resolveEntryTab 直接 return null, 不会用空 tabUrls 计算
|
|
const r1 = resolveEntryTab({
|
|
currentPageRoute: DIY,
|
|
tabBarList: { list: [] }, // 空
|
|
historyRoutes: [HOME],
|
|
noTabPages: NO_TAB_PAGES,
|
|
});
|
|
assert.equal(r1, null, 'tabBarList 空时, resolveEntryTab 应安全返回 null');
|
|
|
|
const r2 = resolveEntryTab({
|
|
currentPageRoute: DIY,
|
|
tabBarList: null, // null
|
|
historyRoutes: [HOME],
|
|
noTabPages: NO_TAB_PAGES,
|
|
});
|
|
assert.equal(r2, null, 'tabBarList null 时, resolveEntryTab 应安全返回 null');
|
|
});
|
|
|
|
// =============================================================
|
|
// 场景 18: P1-4 调试开关 (__TABBAR_DEBUG__)
|
|
// =============================================================
|
|
|
|
test('场景 18.1: __TABBAR_DEBUG__ 开启时, 决策路径有 console.log', () => {
|
|
// 在沙箱里, 临时设 globalThis.__TABBAR_DEBUG__ = true 并捕获 console.log
|
|
const logCapture = [];
|
|
const origLog = console.log;
|
|
const origGlobal = globalThis.__TABBAR_DEBUG__;
|
|
console.log = (...a) => logCapture.push(a.join(' '));
|
|
globalThis.__TABBAR_DEBUG__ = true;
|
|
|
|
try {
|
|
checkTabBarActive(
|
|
HOME,
|
|
DIY,
|
|
STANDARD_TABBAR,
|
|
{},
|
|
{},
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
);
|
|
} finally {
|
|
console.log = origLog;
|
|
globalThis.__TABBAR_DEBUG__ = origGlobal;
|
|
}
|
|
|
|
assert.ok(logCapture.length > 0, `应至少有一次 console.log, 实际 ${logCapture.length} 次`);
|
|
assert.ok(logCapture.some(l => l.includes('[TABBAR]')), '日志应包含 [TABBAR] 前缀');
|
|
});
|
|
|
|
test('场景 18.2: __TABBAR_DEBUG__ 未开启时, 决策路径无 console.log', () => {
|
|
const logCapture = [];
|
|
const origLog = console.log;
|
|
const origGlobal = globalThis.__TABBAR_DEBUG__;
|
|
console.log = (...a) => logCapture.push(a.join(' '));
|
|
globalThis.__TABBAR_DEBUG__ = false;
|
|
|
|
try {
|
|
checkTabBarActive(
|
|
HOME,
|
|
DIY,
|
|
STANDARD_TABBAR,
|
|
{},
|
|
{},
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS },
|
|
);
|
|
} finally {
|
|
console.log = origLog;
|
|
globalThis.__TABBAR_DEBUG__ = origGlobal;
|
|
}
|
|
|
|
const tabbarLogs = logCapture.filter(l => l.includes('[TABBAR]'));
|
|
assert.equal(tabbarLogs.length, 0, '__TABBAR_DEBUG__ 关闭时不应有 [TABBAR] 日志');
|
|
});
|
|
|
|
test('场景 18.3: opts.debug = true 也能触发调试', () => {
|
|
const logCapture = [];
|
|
const origLog = console.log;
|
|
const origGlobal = globalThis.__TABBAR_DEBUG__;
|
|
console.log = (...a) => logCapture.push(a.join(' '));
|
|
globalThis.__TABBAR_DEBUG__ = false;
|
|
|
|
try {
|
|
checkTabBarActive(
|
|
HOME,
|
|
DIY,
|
|
STANDARD_TABBAR,
|
|
{},
|
|
{},
|
|
{ noTabPages: NO_TAB_PAGES, pageTabMap: PAGE_TAB_MAP, pageTabWildcards: PAGE_TAB_WILDCARDS, debug: true },
|
|
);
|
|
} finally {
|
|
console.log = origLog;
|
|
globalThis.__TABBAR_DEBUG__ = origGlobal;
|
|
}
|
|
|
|
const tabbarLogs = logCapture.filter(l => l.includes('[TABBAR]'));
|
|
assert.ok(tabbarLogs.length > 0, 'opts.debug=true 应触发 [TABBAR] 日志');
|
|
}); |