| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import assert from 'node:assert/strict'
- import { readFileSync } from 'node:fs'
- import test from 'node:test'
- /**
- * 「设备类型状态分布」图例排版守卫(维护者 2026-12-02)。
- *
- * 症状:图一(donutGridBlock)的图例标签全被截成「窨井盖···343」「可燃···32」,
- * 且行与行对不齐;图二(stackedBlock)两列、标签完整、色点+名称+计数对齐。
- *
- * 根因:donutGrid 的图例是 flex-wrap,且 `.panel-donut__label` 被固定成 4em
- * (`--donut-label-ch` 默认 4,而 donutGridBlock 没算这个变量),长标签必被截断。
- * 修法:让 donut-grid 图例复用 stacked 的 grid 两列 + flex 行排版,
- * 标签列自然占满列宽,不再写死 4em。
- */
- function styleOf(file) {
- const source = readFileSync(new URL(`../${file}`, import.meta.url), 'utf8')
- const match = source.match(/<style[^>]*>([\s\S]*?)<\/style>/)
- assert.ok(match, `${file} 应有样式块`)
- return match[1]
- }
- /** 取一条选择器的规则体(按声明块花括号配对),并剥离注释避免误判。 */
- function ruleBody(css, selector) {
- const index = css.indexOf(selector)
- assert.ok(index >= 0, `找不到规则:${selector}`)
- const open = css.indexOf('{', index)
- let depth = 0
- for (let i = open; i < css.length; i += 1) {
- if (css[i] === '{') depth += 1
- else if (css[i] === '}') {
- depth -= 1
- if (depth === 0) return css.slice(open + 1, i).replace(/\/\*[\s\S]*?\*\//g, '')
- }
- }
- throw new Error(`规则花括号不配对:${selector}`)
- }
- const css = styleOf('src/modules/cockpit/components/PanelBlocks.vue')
- test('donut-grid 图例用与 stacked 参考一致的 auto-fill 两列栅格,不再 flex-wrap', () => {
- const body = ruleBody(css, '.panel-donuts__legend {')
- assert.match(body, /display:\s*grid/, '图例容器应为 grid')
- // 与图二 stackedBlock 同源:auto-fill + minmax(96u,1fr),列数随面板宽度自适应,
- // 每列至少 96u 保证标签有足够宽度,不再被压成 4 字。
- assert.match(body, /grid-template-columns:\s*repeat\(\s*auto-fill\s*,\s*minmax\(calc\(96 \* var\(--u, 1px\)\), 1fr\)\)/)
- assert.doesNotMatch(body, /flex-wrap/, '不能再回到 flex-wrap')
- })
- test('donut-grid 图例行改为 flex(色点+名称+计数),不再用固定 4em 标签列', () => {
- const row = ruleBody(css, '.panel-donuts__legend .panel-donut__row {')
- assert.match(row, /display:\s*flex/, '图例行应为 flex')
- const label = ruleBody(css, '.panel-donuts__legend .panel-donut__label {')
- assert.match(label, /width:\s*auto/, '标签列应自动占满剩余宽度')
- assert.doesNotMatch(label, /--donut-label-ch/, '不能再把标签宽度写死成字符数')
- })
|