// Regenerate README diagrams from the checked-in Mermaid sources: // node utils/diagrams/render-test-architecture.mjs // Uses the existing Playwright dependency and local Chrome, without networking. import fs from 'node:fs/promises' import path from 'node:path' import { fileURLToPath } from 'node:url' import { chromium } from 'playwright' const root = fileURLToPath(new URL('../../', import.meta.url)) const directory = path.join(root, 'docs/public/diagrams') const width = 1600, height = 1020 const layout = { A: { x: 600, y: 78, w: 400, h: 64, color: 'root' }, B: { x: 280, y: 220, w: 400, h: 64, color: 'blue' }, C: { x: 960, y: 220, w: 260, h: 64, color: 'purple' }, D: { x: 1280, y: 220, w: 280, h: 64, color: 'green' }, E: { x: 100, y: 360, w: 340, h: 72, color: 'blue' }, F: { x: 520, y: 360, w: 340, h: 72, color: 'blue' }, G: { x: 100, y: 500, w: 340, h: 70, color: 'blue' }, H: { x: 100, y: 600, w: 340, h: 70, color: 'blue' }, I: { x: 100, y: 700, w: 340, h: 70, color: 'blue' }, J: { x: 100, y: 814, w: 340, h: 90, color: 'orange' }, K: { x: 520, y: 500, w: 340, h: 100, color: 'blue' }, L: { x: 520, y: 700, w: 340, h: 130, color: 'blue' }, M: { x: 960, y: 500, w: 260, h: 160, color: 'purple' }, N: { x: 1280, y: 500, w: 280, h: 160, color: 'green' }, } const palette = { root: ['#153462', '#153462', '#ffffff'], blue: ['#ffffff', '#a8c6ec', '#163e70'], purple: ['#ffffff', '#c7b7ed', '#593a91'], green: ['#ffffff', '#9dd5c0', '#17674d'], orange: ['#fff7e7', '#e6bc6c', '#80530b'], } const xml = value => value.replace(/[&<>"']/g, character => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[character]) function parse(source) { const labels = new Map(), edges = [] for (const line of source.trim().split(/\r?\n/).slice(1)) { const match = line.trim().match(/^(\w+)(?:\[([^\]]+)\])?\s*-->\s*(\w+)(?:\[([^\]]+)\])?$/) if (!match) throw new Error(`Unsupported Mermaid line: ${line}`) const [, from, fromLabel, to, toLabel] = match for (const [id, label] of [[from, fromLabel], [to, toLabel]]) { if (!layout[id]) throw new Error(`Missing node layout: ${id}`) if (label) labels.set(id, label) } edges.push([from, to]) } if (labels.size !== Object.keys(layout).length) throw new Error('Incomplete diagram node labels') return { labels, edges } } function connection(from, to) { const a = layout[from], b = layout[to] const x = a.x + a.w / 2, y = a.y + a.h, target = b.x + b.w / 2 if (from === 'A') return `M ${x} ${y} V 178 H ${target} V ${b.y - 4}` if (from === 'B') return `M ${x} ${y} V 322 H ${target} V ${b.y - 4}` if (from === 'E') return `M ${x} ${y} V 462 H 68 V ${b.y + b.h / 2} H ${b.x - 4}` return `M ${x} ${y} V ${(y + b.y) / 2} H ${target} V ${b.y - 4}` } function draw({ labels, edges }, wrapped, english) { const nodes = [...labels].map(([id, label]) => { const box = layout[id], [fill, stroke, text] = palette[box.color] const lines = wrapped[id], lineHeight = id === 'A' ? 29 : 26 if (lines.length * lineHeight > box.h - 12) throw new Error(`Label too tall: ${label}`) const firstY = box.y + box.h / 2 - (lines.length - 1) * lineHeight / 2 return ` ${lines.map((line, index) => `${xml(line)}`).join('')}` }).join('\n') return ` ${english ? 'Common UI Plus test architecture' : 'Common UI Plus 测试架构'} ${xml([...labels.values()].join('; '))} COMMON UI PLUS / ${english ? 'TEST ARCHITECTURE' : '测试架构'} Vitest · Vue · Chrome ${edges.map(([from, to]) => ``).join('')} ${nodes} tests/types/ tests/browser/ ${english ? 'Source: vitest.config.ts · playwright.config.ts · tests/types/tsconfig.json' : '配置来源:vitest.config.ts · playwright.config.ts · tests/types/tsconfig.json'} \n` } const browser = await chromium.launch({ channel: 'chrome', headless: true }) try { const page = await browser.newPage({ viewport: { width, height }, deviceScaleFactor: 2 }) await page.route('**/*', route => route.abort()) for (const english of [false, true]) { const name = `test-architecture${english ? '.en' : ''}` const graph = parse(await fs.readFile(path.join(directory, `${name}.mmd`), 'utf8')) const wrapped = await page.evaluate(async ({ labels, layout }) => { await document.fonts.load('20px "Microsoft YaHei"') const canvas = document.createElement('canvas'), context = canvas.getContext('2d') if (!context) throw new Error('Canvas text measurement unavailable') return Object.fromEntries(labels.map(([id, label]) => { context.font = `${id === 'A' ? 25 : 20}px "Segoe UI", "Microsoft YaHei", Arial, sans-serif` const lines = [], limit = layout[id].w - 42 let current = '' const units = /[\u3400-\u9fff]/u.test(label) ? Array.from(label) : label.split(/(?<=\s)/u) for (const unit of units) { if (current && context.measureText(current + unit).width > limit) { lines.push(current.trim()); current = unit.trimStart() } else current += unit } if (current) lines.push(current.trim()) if (lines.length > 1 && /^[\u3400-\u9fff]$/u.test(lines.at(-1))) { const previous = lines.at(-2) lines[lines.length - 2] = previous.slice(0, -2) lines[lines.length - 1] = previous.slice(-2) + lines.at(-1) } return [id, lines] })) }, { labels: [...graph.labels], layout }) const svg = draw(graph, wrapped, english) await fs.writeFile(path.join(directory, `${name}.svg`), svg) await page.setContent(`${svg}`) await page.evaluate(() => document.fonts.ready) const overflowing = await page.locator('g[data-node]').evaluateAll(nodes => nodes.filter(node => { const rect = node.querySelector('rect').getBBox(), text = node.querySelector('text').getBBox() return text.x < rect.x + 8 || text.y < rect.y + 5 || text.x + text.width > rect.x + rect.width - 8 || text.y + text.height > rect.y + rect.height - 5 }).map(node => node.getAttribute('data-node'))) if (overflowing.length) throw new Error(`Overflowing labels: ${overflowing.join(', ')}`) await page.locator('svg').screenshot({ path: path.join(directory, `${name}.png`) }) console.log(`Generated ${name}.svg and ${name}.png (${width * 2}×${height * 2}); ${graph.labels.size} nodes, ${graph.edges.length} edges, no label overflow.`) } } finally { await browser.close() }