import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { describe, expect, it } from 'vitest' // 移动端适配结构性回归测试:不渲染组件,仅检查 SFC 源码。 // 固定列宽列表合计约 69.5rem,窄屏(≤1023px:手机横竖屏 / 平板竖屏 / 半屏小窗口) // 下必然超出视口:题头与数据行置于同一横向滚动容器(.audit-list)整体左右滑动, // 列对齐关系与桌面一致;筛选栏窄屏 2 列(≤480px 手机竖屏单列)、分页条允许换行; // ≥1024px 维持桌面布局不变。 // 特别注意:VEntity 是多根 Fragment 组件(tr + 可选 footer tr),其 拿不到 // 本组件的 scoped 作用域 ID,所有锚定 .audit-row / 行内元素 :deep 的 scoped 规则 // 永远失配(历史上「隐藏题头 + 数据行纵向堆叠」方案因此整批失效——窄屏下字段 // 仍按固定列宽横排并被视口截断),行内样式必须锚定自身元素 .audit-list 穿透, // 本测试防止该失效方案被重新引入。 const sfc = readFileSync(fileURLToPath(new URL('./AuditLogList.vue', import.meta.url)), 'utf-8') const MOBILE_QUERY = '@media (max-width: 1023px)' /** 提取 max-width: 1023px 媒体查询块全文(大括号配平)。 */ function mobileBlock(): string { const start = sfc.indexOf(MOBILE_QUERY) expect(start, `缺少 ${MOBILE_QUERY} 媒体查询`).toBeGreaterThan(-1) let depth = 0 let end = -1 for (let i = sfc.indexOf('{', start); i < sfc.length; i++) { if (sfc[i] === '{') depth++ else if (sfc[i] === '}') { depth-- if (depth === 0) { end = i break } } } return sfc.slice(start, end) } /** 在指定文本块内取某选择器的规则体(自 from 起首个匹配)。 */ function ruleIn(block: string, selector: string, from = 0): string { const start = block.indexOf(`${selector} {`, from) expect(start, `块中缺少 ${selector} 规则`).toBeGreaterThan(-1) const end = block.indexOf('}', start) return block.slice(start, end) } /** 取全局(非媒体查询)样式中某选择器的规则体。 */ function styleRule(selector: string): string { const start = sfc.indexOf(`${selector} {`) expect(start, `缺少 ${selector} 样式规则`).toBeGreaterThan(-1) const end = sfc.indexOf('}', start) return sfc.slice(start, end) } describe('AuditLogList 移动端适配', () => { it('存在 ≤1023px 窄屏断点,且位于桌面规则之后(同特异性按顺序覆盖才生效)', () => { const mediaStart = sfc.indexOf(MOBILE_QUERY) const desktopGrid = sfc.indexOf('.filter-grid {') expect(desktopGrid).toBeGreaterThan(-1) expect(mediaStart).toBeGreaterThan(desktopGrid) }) it('窄屏筛选栏改为 2 列,操作按钮独占整行并拉伸铺满', () => { const grid = ruleIn(mobileBlock(), '.filter-grid') expect(grid).toMatch(/grid-template-columns:\s*repeat\(2,\s*minmax\(0,\s*1fr\)\)/) const actions = ruleIn(mobileBlock(), '.filter-actions') expect(actions).toMatch(/grid-column:\s*1\s*\/\s*-1/) const stretch = ruleIn(mobileBlock(), '.filter-actions > :deep(*)') expect(stretch).toMatch(/flex:\s*1/) }) it('手机竖屏(≤480px)筛选栏单列,datetime-local 不被压扁', () => { const start = sfc.indexOf('@media (max-width: 480px)') expect(start, '缺少 ≤480px 单列断点').toBeGreaterThan(-1) expect(sfc.slice(start)).toMatch( /grid-template-columns:\s*minmax\(0,\s*1fr\)/, ) }) it('题头与数据行置于同一横向滚动容器(.audit-list overflow-x: auto)', () => { // 模板顺序:.audit-list 包裹 .audit-header 与全部 VEntity 数据行, // 滑动时题头与数据列保持对齐 const listStart = sfc.indexOf('
') expect(listStart, '缺少 .audit-list 滚动容器').toBeGreaterThan(-1) const headerStart = sfc.indexOf('
') const entityStart = sfc.indexOf('\n ', listStart) expect(headerStart).toBeGreaterThan(listStart) expect(entityStart).toBeGreaterThan(headerStart) expect(listEnd).toBeGreaterThan(entityStart) const rule = styleRule('.audit-list') expect(rule).toMatch(/overflow-x:\s*auto/) }) it('窄屏不隐藏列题头(题头随容器滑动,列对齐关系不变)', () => { // 历史方案曾在窄屏 display: none 隐藏题头,导致用户失去列语义,防止回归 const headerRule = styleRule('.audit-header') expect(headerRule).not.toMatch(/display:\s*none/) expect(mobileBlock()).not.toContain('.audit-header') }) it('题头在滚动容器内随内容延伸且列宽不收缩(滑动后下划线不断裂、不与数据列错位)', () => { const header = styleRule('.audit-header') expect(header).toMatch(/min-width:\s*max-content/) const col = styleRule('.header-col') expect(col).toMatch(/flex-shrink:\s*0/) }) it('数据行保持固定列宽表格形态,无失效的行内堆叠样式', () => { // VEntity 的 拿不到本组件 scoped 作用域 ID,锚定 .audit-row 的规则永远失配; // 窄屏块内不得出现任何 entity 行内 :deep 规则,全文件不得再有 .audit-row 选择器 // (选择器须位于行首或上一规则之后,注释中的历史说明不受限) expect(mobileBlock()).not.toContain('entity-') expect(sfc).not.toMatch(/(?:^|[},;])\s*\.audit-row[\s:{]/) }) it('行悬停手型经自身元素 .audit-list 穿透到 VEntity 行', () => { const rule = styleRule('.audit-list :deep(.entity-wrapper)') expect(rule).toMatch(/cursor:\s*pointer/) }) it('窄屏分页条允许换行(总数与翻页控制器不横向溢出)', () => { const rule = ruleIn( mobileBlock(), '.audit-pagination :deep(.pagination),\n .audit-pagination :deep(.pagination__controller)', ) expect(rule).toMatch(/flex-wrap:\s*wrap/) }) })