Files
go-sms-V2/web/tests/view-cache-name.spec.ts
Tory eaecd83ab0
ci / test (push) Canceled after 0s
ci / web-test (push) Canceled after 0s
ci / build (push) Canceled after 0s
ci / deploy-dev (push) Canceled after 0s
Issue#2: 管理前端多标签导航+页签缓存(Gitea 工单:可独立关闭/一次全部关闭/切换后缓存)—— ①多标签导航=stores/tabs.ts(tab 身份=fullPath,参数页每实例一 tab 带 #id 后缀;home 固定不可关;登出 $reset 防跨角色残留;close 关当前回退相邻右邻优先/closeOthers/closeAll)+TagsBar.vue(纯原生元素守 G2 断言语义;✕独立关闭+中键关闭[auxclick button===1,@click.middle 对中键永不触发]+关闭其他+全部关闭;aurora token 风格 primary-soft 激活/圆角10px);②keep-alive 页签缓存=MainLayout router-view v-slot+include=cachedNames+key=fullPath;**29 个业务视图 defineOptions name=路由名**(28 新增+stats/profit 由 StatsProfitView 对齐——script setup 文件名推断名永不命中 include:15 个 index.vue 全叫 index/PascalCase≠kebab);ComingSoon(11 路由共用)刻意豁免(无本地状态,不缓存零副作用);③守门=view-cache-name.spec.ts 固化组件名=路由名约定(漏写=缓存静默失效,测试红而非线上)+verify-layout.mjs +5 项 tags-bar 断言(18→23)且凭据 env 化(SHOT_USER/SHOT_PASS 缺省 s2demo——admin 原密码首启打印一次不可知,硬编码 admin123 已不可登录 ISC:401);④门禁=eslint0/vue-tsc0/vitest 165(+17:tabs-store 7+守门 4+tags-bar 6)/build 9.99s chunk 剖面不变;verify-layout 23/23;Chrome 157 实机走查 17/17 含两项核心状态保留实证(结算页内 Tab 选中+筛选账号ID=2 切走切回均保留;截图 /tmp/issue2-*.png);验收单 issue2-frontend-tabs.md+CHANGELOG+memory-bank 回填
2026-11-16 12:35:42 +08:00

56 lines
2.7 KiB
TypeScript

// view-cache-name.spec.ts Issue#2 守门:keep-alive :include 按**组件名**匹配,
// 组件名必须=路由 name——新增业务页漏写 defineOptions 会让缓存**静默失效**
// (页面照常工作,仅切 tab 丢状态),此类回归必须在本测试红,而不是线上被发现。
// 豁免:ComingSoon 占位页(11 条路由共用,无本地状态可保,刻意不缓存)。
import { readFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { asyncRoutes } from '@/router/routes'
const webRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
// 从 routes.ts 源码提取 路由名 → 视图文件 映射(name 与 component 动态 import
// 相邻的行内模式;redirect 路由天然无 component 不会入表)。
const routesSrc = readFileSync(join(webRoot, 'src/router/routes.ts'), 'utf8')
const viewFile = new Map<string, string>()
for (const m of routesSrc.matchAll(/name:\s*'([\w-]+)',\s*component:\s*\(\)\s*=>\s*import\('([^']+)'\)/g)) {
viewFile.set(m[1]!, m[2]!)
}
describe('Issue#2 视图缓存命名守门(组件名=路由名)', () => {
it('路由→文件映射提取成功(≥40,防正则/结构漂移静默跳检)', () => {
expect(viewFile.size).toBeGreaterThanOrEqual(40)
})
it('每个业务视图 SFC 含 defineOptions({ name: <路由名> })', () => {
const children = asyncRoutes[0]!.children!
const business = children.filter(
(c) => c.name && c.meta?.title && c.meta?.group && viewFile.has(String(c.name)),
)
expect(business.length).toBeGreaterThanOrEqual(40)
const missing: string[] = []
for (const c of business) {
const name = String(c.name)
const rel = viewFile.get(name)!
// 豁免:ComingSoon 占位页(共用组件,刻意不参与缓存)
if (rel.endsWith('ComingSoon.vue')) continue
const sfc = readFileSync(join(webRoot, rel.replace('@/', 'src/')), 'utf8')
if (!sfc.includes(`defineOptions({ name: '${name}' })`)) missing.push(`${name} → ${rel}`)
}
expect(missing, `缺缓存命名(keep-alive 会静默失效):\n${missing.join('\n')}`).toEqual([])
})
it('豁免口径固化:ComingSoon 不写 defineOptions(每次进入重挂载,零状态可丢)', () => {
const sfc = readFileSync(join(webRoot, 'src/views/coming/ComingSoon.vue'), 'utf8')
expect(sfc).not.toContain('defineOptions')
})
it('MainLayout 已挂 keep-alive(include=tabs.cachedNames,key=fullPath)', () => {
const sfc = readFileSync(join(webRoot, 'src/layouts/MainLayout.vue'), 'utf8')
expect(sfc).toContain(':include="tabs.cachedNames"')
expect(sfc).toContain(':key="viewRoute.fullPath"')
expect(sfc).toContain('<TagsBar />')
})
})