import { createRequire } from "node:module"; import { mkdir, writeFile } from "node:fs/promises"; import assert from "node:assert/strict"; const require = createRequire(import.meta.url); const { chromium } = require(process.env.PLAYWRIGHT_MODULE || "playwright"); const browser = await chromium.launch({ headless: true, ...(process.env.CHROME_PATH ? { executablePath: process.env.CHROME_PATH } : {}) }); const page = await browser.newPage({ viewport: { width: 720, height: 640 } }); const errors = []; page.on("pageerror", error => errors.push(error.message)); const output = ".audit/backlinks"; await mkdir(output, { recursive: true }); await page.route("**/__backlinks__", route => route.fulfill({ contentType: "text/html", body: `Backlinks check ` })); const entries = Array.from({ length: 28 }, (_, index) => ({ sourcePath: `research/long-folder-name/project-${index + 1}/source-note-with-a-long-readable-title.md`, line: index + 3, from: index * 100, to: index * 100 + 10, text: "This source note links to [[Target|the selected note]]. Its visible excerpt can wrap within the panel.", kind: "wiki", target: "Target", })); const measurements = []; try { await page.goto(`${process.env.SMOKE_URL || "http://127.0.0.1:1420"}/__backlinks__`); await page.waitForFunction(() => window.backlinksReady); const panel = page.getByRole("region", { name: "Backlinks", exact: true }); await panel.getByText("No other notes link to this file.").waitFor(); await page.screenshot({ path: `${output}/empty-dark.png` }); for (const mode of ["light", "dark"]) { for (const width of [320, 180]) { await page.evaluate(args => window.renderBacklinks(args), { mode, width, entries }); const toggle = panel.getByRole("button", { name: "Backlinks 28", exact: true }); await toggle.focus(); await page.keyboard.press("Enter"); assert.equal(await toggle.getAttribute("aria-expanded"), "false"); assert.equal(await panel.locator(".source").first().isVisible(), false); await page.keyboard.press("Space"); assert.equal(await toggle.getAttribute("aria-expanded"), "true"); await page.keyboard.press("Tab"); await page.keyboard.press("Enter"); assert.ok(await page.evaluate(() => window.rebuildCount > 0)); await page.keyboard.press("Tab"); await page.keyboard.press("Enter"); assert.equal(await page.evaluate(() => window.openedBacklink.sourcePath), entries[0].sourcePath); const metrics = await panel.evaluate(node => { const source = node.querySelector(".source"), title = node.querySelector(".source-title"), content = node.querySelector(".content"); const style = getComputedStyle(source); const color = value => value.match(/[\d.]+/g).slice(0, 3).map(Number); const luminance = rgb => rgb.map(value => value / 255).map(value => value <= .04045 ? value / 12.92 : ((value + .055) / 1.055) ** 2.4).reduce((sum, value, index) => sum + value * [.2126,.7152,.0722][index], 0); const contrast = (a,b) => { const l1=luminance(color(a)),l2=luminance(color(b));return (Math.max(l1,l2)+.05)/(Math.min(l1,l2)+.05); }; const background = getComputedStyle(document.querySelector("#fixture")).backgroundColor; return { text: style.color, accent: getComputedStyle(title).color, background, textContrast: contrast(style.color,background), linkContrast:contrast(getComputedStyle(title).color,background), width:node.clientWidth,scrollWidth:node.scrollWidth,scrollable:content.scrollHeight>content.clientHeight, focusStyle:style.outlineStyle,focusColor:style.outlineColor,opacity:style.opacity }; }); assert.ok(metrics.textContrast >= 4.5, `Text contrast: ${mode} ${width}`); assert.ok(metrics.linkContrast >= 4.5, `Link contrast: ${mode} ${width}`); assert.ok(metrics.scrollWidth <= metrics.width, "The panel must not scroll horizontally"); assert.ok(metrics.scrollable, "Long lists must scroll inside the panel"); assert.equal(metrics.focusStyle, "solid"); assert.equal(metrics.opacity, "1"); measurements.push({ mode, width, ...metrics }); await page.screenshot({ path: `${output}/${mode}-${width}-focus.png` }); } } await page.evaluate(() => window.renderBacklinks({ busy:true, error:"Could not read one note. Rebuild the index to try again." })); assert.equal(await panel.getByRole("button", { name:"Rebuild note index", exact:true }).isDisabled(), true); await panel.getByText("Reading workspace links…").waitFor(); await page.screenshot({ path:`${output}/busy-error-dark.png` }); assert.deepEqual(errors, []); await writeFile(`${output}/measurements.json`, JSON.stringify(measurements,null,2)); console.log("Backlinks smoke passed: themes, narrow panels, long lists, keyboard, empty, busy, error, and contrast."); } finally { await browser.close(); }