1import { readFile, readdir } from 'node:fs/promises'2import { dirname, join } from 'node:path'3import { fileURLToPath } from 'node:url'4import { canonicalizeSvg, materializeSvg, supportedAppearances } from '../../../shared/svg/index.js'5import { httpRendererUrlFor } from '../src/components/DiagramExample/rendererRouting.mjs'67const site = join(dirname(fileURLToPath(import.meta.url)), '..')8const examplesDir = join(site, 'static', 'examples')9const names = (await readdir(examplesDir)).filter((name) => name.endsWith('.json')).sort()1011async function check(name) {12 const example = JSON.parse(await readFile(join(examplesDir, name), 'utf8'))13 const response = await fetch(httpRendererUrlFor(example.engine), {14 method: 'POST',15 headers: { Accept: 'image/svg+xml', 'Content-Type': 'application/json' },16 body: JSON.stringify({17 source: example.source,18 format: 'svg',19 options: {},20 metadata: {},21 presentation: { background: '', padding: 24, frame: false },22 }),23 })24 const body = await response.text()25 if (!response.ok || !body.includes('<svg')) {26 throw new Error(`${example.engine}: HTTP ${response.status} ${body.replace(/\s+/g, ' ').slice(0, 240)}`)27 }28 const version = [29 response.headers.get('X-Diagram-Engine-Version'),30 response.headers.get('X-Renderer-Build'),31 ].filter(Boolean).join(' ')32 const canonical = canonicalizeSvg(body, {}, example.engine, version)33 const conformance = canonical.match(/data-dz-conformance="([^"]+)"/)?.[1]34 if (conformance !== 'presentation-only' && !/data-dz-(?:fill|stroke)=/.test(canonical)) {35 throw new Error(`${example.engine}: normalization profile produced no semantic paint roles`)36 }37 const appearances = supportedAppearances(canonical)38 const appearance = appearances.includes('auto-transparent')39 ? 'auto-transparent'40 : appearances.includes('auto-framed') ? 'auto-framed' : 'raw'41 materializeSvg(canonical, appearance)42 return example.engine43}4445const failures = []46for (let index = 0; index < names.length; index += 5) {47 const group = names.slice(index, index + 5)48 const results = await Promise.allSettled(group.map(check))49 results.forEach((result) => {50 if (result.status === 'rejected') failures.push(result.reason.message)51 else console.log(`Checked ${result.value}.`)52 })53}5455if (failures.length) {56 console.error(failures.map((failure) => `ERROR: ${failure}`).join('\n'))57 process.exitCode = 158} else {59 console.log(`The live renderer accepted all ${names.length} examples.`)60}