// 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 `<g data-node="${id}"><rect x="${box.x}" y="${box.y}" width="${box.w}" height="${box.h}" rx="14" fill="${fill}" stroke="${stroke}" stroke-width="2"/>
<text x="${box.x + box.w / 2}" y="${firstY}" text-anchor="middle" dominant-baseline="central" fill="${text}" font-size="${id === 'A' ? 25 : 20}" font-weight="${['A', 'B', 'C', 'D', 'E', 'F', 'J'].includes(id) ? 650 : 500}">${lines.map((line, index) => `<tspan x="${box.x + box.w / 2}" dy="${index ? lineHeight : 0}">${xml(line)}</tspan>`).join('')}</text></g>`
}).join('\n')
return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" role="img" aria-labelledby="title description">
<title id="title">${english ? 'Common UI Plus test architecture' : 'Common UI Plus 测试架构'}</title>
<desc id="description">${xml([...labels.values()].join('; '))}</desc>
<defs><marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" fill="#8296b0"/></marker></defs>
<rect width="${width}" height="${height}" rx="22" fill="#ffffff"/>
<g font-family="'Segoe UI', 'Microsoft YaHei', Arial, sans-serif">
<text x="42" y="43" font-size="17" font-weight="650" letter-spacing="1.6" fill="#506785">COMMON UI PLUS / ${english ? 'TEST ARCHITECTURE' : '测试架构'}</text>
<text x="1558" y="43" text-anchor="end" font-size="16" fill="#65758c">Vitest · Vue · Chrome</text>
<rect x="32" y="198" width="888" height="732" rx="20" fill="#f1f6fd"/>
<rect x="944" y="198" width="300" height="732" rx="20" fill="#f7f3fd"/>
<rect x="1260" y="198" width="308" height="732" rx="20" fill="#eff8f4"/>
<g fill="none" stroke="#8296b0" stroke-width="2.4" stroke-linejoin="round" stroke-linecap="round" marker-end="url(#arrow)">${edges.map(([from, to]) => `<path data-edge="${from}-${to}" d="${connection(from, to)}"/>`).join('')}</g>
${nodes}
<text x="1090" y="727" text-anchor="middle" font-size="17" fill="#705997">tests/types/</text>
<text x="1420" y="727" text-anchor="middle" font-size="17" fill="#3b7d65">tests/browser/</text>
<path d="M 42 952 H 1558" stroke="#dce4ef"/>
<text x="42" y="989" font-size="18" fill="#536780">${english ? 'Source: vitest.config.ts · playwright.config.ts · tests/types/tsconfig.json' : '配置来源:vitest.config.ts · playwright.config.ts · tests/types/tsconfig.json'}</text>
</g>
</svg>\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(`<html><head><meta charset="utf-8"/><style>html,body{margin:0;background:white}svg{display:block}</style></head><body>${svg}</body></html>`)
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()
}