|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | +import puppeteer from 'puppeteer'; |
| 7 | +import { processSVGWithUniqueIds } from '../../src/util/processSVG.js'; |
| 8 | + |
| 9 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 10 | +const failedDir = path.join(__dirname, 'failed'); |
| 11 | +const processedDir = path.join(__dirname, 'processed'); |
| 12 | + |
| 13 | +// delete failed directory before running tests |
| 14 | +if (fs.existsSync(failedDir)) { |
| 15 | + fs.rmSync(failedDir, { recursive: true }); |
| 16 | +} |
| 17 | + |
| 18 | +// Get all SVG files in the current directory |
| 19 | +const svgFiles = fs.readdirSync(__dirname).filter(file => file.endsWith('.svg')); |
| 20 | + |
| 21 | +async function renderSvgToBuffer(browser, svgContent) { |
| 22 | + const page = await browser.newPage(); |
| 23 | + await page.setViewport({ width: 600, height: 400 }); |
| 24 | + |
| 25 | + const html = ` |
| 26 | + <!DOCTYPE html> |
| 27 | + <html> |
| 28 | + <head> |
| 29 | + <style> |
| 30 | + body { margin: 0; padding: 0; background: white; } |
| 31 | + </style> |
| 32 | + </head> |
| 33 | + <body>${svgContent}</body> |
| 34 | + </html> |
| 35 | + `; |
| 36 | + |
| 37 | + await page.setContent(html, { waitUntil: 'networkidle0' }); |
| 38 | + const screenshot = await page.screenshot({ type: 'png' }); |
| 39 | + await page.close(); |
| 40 | + return screenshot; |
| 41 | +} |
| 42 | + |
| 43 | +function buffersAreEqual(buf1, buf2) { |
| 44 | + if (buf1.length !== buf2.length) return false; |
| 45 | + return buf1.equals(buf2); |
| 46 | +} |
| 47 | + |
| 48 | +for (const svgFile of svgFiles) { |
| 49 | + test(`processSVGWithUniqueIds visual regression - ${svgFile} should render identically`, async () => { |
| 50 | + const svgPath = path.join(__dirname, svgFile); |
| 51 | + const originalSvg = fs.readFileSync(svgPath, 'utf-8'); |
| 52 | + const processedSvg = processSVGWithUniqueIds(originalSvg); |
| 53 | + |
| 54 | + const browser = await puppeteer.launch({ headless: true }); |
| 55 | + |
| 56 | + try { |
| 57 | + const originalScreenshot = await renderSvgToBuffer(browser, originalSvg); |
| 58 | + const processedScreenshot = await renderSvgToBuffer(browser, processedSvg); |
| 59 | + |
| 60 | + const isEqual = buffersAreEqual(originalScreenshot, processedScreenshot); |
| 61 | + |
| 62 | + if (!isEqual) { |
| 63 | + if (!fs.existsSync(failedDir)) { |
| 64 | + fs.mkdirSync(failedDir, { recursive: true }); |
| 65 | + } |
| 66 | + const processedPath = path.join(failedDir, `processed_${svgFile}`); |
| 67 | + fs.writeFileSync(processedPath, processedSvg); |
| 68 | + console.log(`Saved processed SVG to: ${processedPath}`); |
| 69 | + } |
| 70 | + |
| 71 | + assert.ok( |
| 72 | + isEqual, |
| 73 | + `Processed SVG (${svgFile}) should render identically to the original SVG`, |
| 74 | + ); |
| 75 | + } finally { |
| 76 | + await browser.close(); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + test(`processSVGWithUniqueIds idempotency - ${svgFile} should render identically after double processing`, async () => { |
| 81 | + const svgPath = path.join(__dirname, svgFile); |
| 82 | + const originalSvg = fs.readFileSync(svgPath, 'utf-8'); |
| 83 | + const processedOnce = processSVGWithUniqueIds(originalSvg); |
| 84 | + const processedTwice = processSVGWithUniqueIds(processedOnce); |
| 85 | + |
| 86 | + const browser = await puppeteer.launch({ headless: true }); |
| 87 | + |
| 88 | + try { |
| 89 | + const originalScreenshot = await renderSvgToBuffer(browser, originalSvg); |
| 90 | + const doubleProcessedScreenshot = await renderSvgToBuffer(browser, processedTwice); |
| 91 | + |
| 92 | + const isEqual = buffersAreEqual(originalScreenshot, doubleProcessedScreenshot); |
| 93 | + |
| 94 | + if (!isEqual) { |
| 95 | + if (!fs.existsSync(failedDir)) { |
| 96 | + fs.mkdirSync(failedDir, { recursive: true }); |
| 97 | + } |
| 98 | + const processedPath = path.join(failedDir, `double_processed_${svgFile}`); |
| 99 | + fs.writeFileSync(processedPath, processedTwice); |
| 100 | + console.log(`Saved double-processed SVG to: ${processedPath}`); |
| 101 | + } |
| 102 | + |
| 103 | + assert.ok( |
| 104 | + isEqual, |
| 105 | + `Double-processed SVG (${svgFile}) should render identically to the original SVG`, |
| 106 | + ); |
| 107 | + } finally { |
| 108 | + await browser.close(); |
| 109 | + } |
| 110 | + }); |
| 111 | +} |
0 commit comments