| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- /**
- * 回归守护单测 —— 成长页六宫格三列布局容差
- *
- * 溯源:
- * fix: 3e93a9372「fix(cfc-frontend): 成长页六宫格三列布局容差修复 —
- * calc(33.333% - 8rpx)→calc(33.333% - 12rpx),余量 0→1.73% 防临界折行」
- * 被测:cfc-frontend/pages/growth-main/index.vue .progress-grid / .progress-tile
- *
- * 缺陷本质(本用例守护的规则):
- * 三列 flex-wrap 布局,每格 width = calc(33.333% - Nrpx),容器 gap = Grpx。
- * 一行总宽 = 3 × (33.333% - N) + 2 × G = 99.999% - 3N + 2G。
- * 余量 slack = 100% - 上式 = 0.001% × 容器宽 + 3N - 2G。
- * 当 N = G/1.5(原 8rpx / gap 12rpx → slack ≈ 0)时,任何亚像素/字体缩放误差
- * 都会让第三格折行 → 六宫格变「2+2+2」两列。修复把 N 提到 12rpx,slack 恒正。
- *
- * 断言口径(静态契约,不依赖渲染引擎):
- * 1) .progress-grid 确为 flex-wrap 且 gap = 12rpx(布局前提)
- * 2) .progress-tile 宽度形如 calc(33.333% - Nrpx)
- * 3) slack(单位 rpx)≥ 4 —— 留出亚像素容差,禁止回归到「刚好 100%」
- * 本文件只读源码,不改任何业务代码。
- */
- import fs from 'fs'
- import path from 'path'
- const PAGE_PATH = path.join(__dirname, '../../../pages/growth-main/index.vue')
- const SRC = fs.readFileSync(PAGE_PATH, 'utf8')
- /** 截取某个 CSS 类选择器的声明块({} 内文本),返回 null 表示不存在 */
- function cssBlock(src, selector) {
- const idx = src.indexOf('\n' + selector + ' {')
- const altIdx = src.indexOf(selector + ' {')
- const start = idx >= 0 ? idx : altIdx
- if (start < 0) return null
- const open = src.indexOf('{', start)
- const close = src.indexOf('}', open)
- if (open < 0 || close < 0) return null
- return src.slice(open + 1, close)
- }
- describe('成长页六宫格 · 三列布局容差(3e93a9372 回归守护)', () => {
- test('TC-GRID-01 .progress-grid 为 flex-wrap 三列容器且 gap=12rpx', () => {
- const b = cssBlock(SRC, '.progress-grid')
- expect(b).not.toBeNull()
- expect(b).toMatch(/display:\s*flex/)
- expect(b).toMatch(/flex-wrap:\s*wrap/)
- const gap = /gap:\s*(\d+)rpx/.exec(b)
- expect(gap).not.toBeNull()
- expect(Number(gap[1])).toBe(12)
- })
- test('TC-GRID-02 .progress-tile 宽度为 calc(33.333% - Nrpx) 形式', () => {
- const b = cssBlock(SRC, '.progress-tile')
- expect(b).not.toBeNull()
- const m = /width:\s*calc\(\s*33\.333%\s*-\s*(\d+)rpx\s*\)/.exec(b)
- expect(m).not.toBeNull()
- expect(Number(m[1])).toBeGreaterThan(0)
- })
- test('TC-GRID-03 一行三格总宽留有余量(slack ≥ 4rpx),不得回归为按 100% 铺满', () => {
- const grid = cssBlock(SRC, '.progress-grid')
- const tile = cssBlock(SRC, '.progress-tile')
- const g = Number(/gap:\s*(\d+)rpx/.exec(grid)[1])
- const n = Number(/width:\s*calc\(\s*33\.333%\s*-\s*(\d+)rpx\s*\)/.exec(tile)[1])
- // 3 × (33.333% - n) + 2 × g = 99.999% - 3n + 2g
- // slack(rpx) = 0.001% 容器宽 + 3n - 2g;0.001% 项按 750rpx 设计宽 ≈ 0.0075rpx,忽略
- const slackRpx = 3 * n - 2 * g
- expect(slackRpx).toBeGreaterThanOrEqual(4)
- })
- test('TC-GRID-04 三格等宽(同一 width 声明,不允许个别格单独覆盖宽度)', () => {
- // .progress-tile 的 width 声明只出现一次;若出现多个宽度声明即为布局漂移
- const tile = cssBlock(SRC, '.progress-tile')
- const widths = tile.match(/(^|\n)\s*width:\s*[^;]+;/g) || []
- expect(widths.length).toBe(1)
- })
- })
|