monitoringDonutLegend.test.js 2.7 KB

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