|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import fs from 'node:fs/promises' |
| 3 | +import os from 'node:os' |
| 4 | +import path from 'node:path' |
| 5 | +import test from 'node:test' |
| 6 | + |
| 7 | +import * as marked from 'marked' |
| 8 | +import playwright from 'playwright' |
| 9 | + |
| 10 | +import * as coverageBadge from '../../../scripts/coverage-badge.mts' |
| 11 | + |
| 12 | +test('project and social badges render in two rows before and after refresh', async t => { |
| 13 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'readme-layout-')) |
| 14 | + t.after(() => fs.rm(dir, { force: true, recursive: true })) |
| 15 | + const readmePath = path.join(dir, 'README.md') |
| 16 | + await fs.writeFile(readmePath, await fs.readFile('README.md')) |
| 17 | + const coveragePath = path.join(dir, 'coverage.json') |
| 18 | + await fs.writeFile(coveragePath, '{}') |
| 19 | + |
| 20 | + const browser = await playwright.chromium.launch({ channel: 'chrome' }) |
| 21 | + t.after(() => browser.close()) |
| 22 | + const page = await browser.newPage({ viewport: { width: 960, height: 300 } }) |
| 23 | + await page.route('**/*', async route => { |
| 24 | + const url = new URL(route.request().url()) |
| 25 | + const social = ['badge-follow-bluesky.svg', 'badge-follow-x.svg'].find( |
| 26 | + name => url.pathname === '/assets/fleet/' + name, |
| 27 | + ) |
| 28 | + const body = social |
| 29 | + ? await fs.readFile(path.join('assets/fleet', social)) |
| 30 | + : '<svg xmlns="http://www.w3.org/2000/svg" width="94" height="20"/>' |
| 31 | + await route.fulfill({ contentType: 'image/svg+xml', body }) |
| 32 | + }) |
| 33 | + |
| 34 | + async function verifyRows() { |
| 35 | + const header = (await fs.readFile(readmePath, 'utf8')).split(/^## /m)[0] |
| 36 | + await page.setContent('<base href="https://readme.invalid/">' + marked.parse(header)) |
| 37 | + // GitHub strips comments after Markdown has established paragraph boundaries. |
| 38 | + await page.evaluate(() => { |
| 39 | + const walker = globalThis.document.createTreeWalker(globalThis.document.body, globalThis.NodeFilter.SHOW_COMMENT) |
| 40 | + const comments = [] |
| 41 | + while (walker.nextNode()) { |
| 42 | + comments.push(walker.currentNode) |
| 43 | + } |
| 44 | + for (const comment of comments) { |
| 45 | + comment.remove() |
| 46 | + } |
| 47 | + }) |
| 48 | + const names = ['Socket Badge', 'Coverage', 'Follow @SocketSecurity', 'Follow @socket.dev on Bluesky'] |
| 49 | + const boxes = await Promise.all(names.map(async name => { |
| 50 | + const image = page.getByRole('img', { name, exact: true }) |
| 51 | + await image.evaluate(element => element.decode()) |
| 52 | + return image.boundingBox() |
| 53 | + })) |
| 54 | + const [socket, coverage, x, bluesky] = boxes |
| 55 | + for (const [left, right] of [[socket, coverage], [x, bluesky]]) { |
| 56 | + assert.ok(left.x + left.width <= right.x, 'badges appear in order') |
| 57 | + assert.ok(Math.max(left.y, right.y) < Math.min(left.y + left.height, right.y + right.height), 'badges share a row') |
| 58 | + } |
| 59 | + assert.ok(Math.max(socket.y + socket.height, coverage.y + coverage.height) <= Math.min(x.y, bluesky.y), 'social badges are below project badges') |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + await verifyRows() |
| 64 | + await coverageBadge.updateCoverageBadge({ readmePath, unitCoveragePath: coveragePath, e2eCoveragePath: coveragePath }) |
| 65 | + await verifyRows() |
| 66 | + } catch (error) { |
| 67 | + await page.screenshot({ path: 'readme-badge-layout-failure.png' }) |
| 68 | + throw error |
| 69 | + } |
| 70 | +}) |
0 commit comments