Files
RuneStone/tests/smoke/links/backlinks.smoke.mjs
2026-11-16 04:21:21 -07:00

84 lines
5.5 KiB
JavaScript

// Backlinks panel in isolation: both themes, narrow panels, long lists, keyboard use, empty, busy, and error states, and text contrast.
import { writeFile } from "node:fs/promises";
import assert from "node:assert/strict";
import path from "node:path";
import { outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("links/backlinks", async ({ page }) => {
const output = await outputDir("links/backlinks");
await page.route("**/__backlinks__", route => route.fulfill({ contentType: "text/html", body: `<!doctype html><html><head><meta charset="UTF-8"><title>Backlinks check</title></head><body>
<aside id="fixture" style="width:320px;height:500px;display:flex;flex-direction:column;background:var(--rs-bg-elevated);margin:24px"></aside>
<script type="module">
import '/src/app.css';
import {mount,unmount} from '/node_modules/.vite/deps/svelte.js';
import Backlinks from '/src/lib/layout/Backlinks.svelte';
import {applyAppearance} from '/src/lib/theme/appearance.ts';
let component;
window.renderBacklinks = async ({mode='dark',width=320,entries=[],busy=false,error=null}={}) => {
if(component) await unmount(component);
applyAppearance({theme:'default',mode});
document.querySelector('#fixture').style.width=width+'px';
component=mount(Backlinks,{target:document.querySelector('#fixture'),props:{entries,busy,error,onOpen:entry=>window.openedBacklink=entry,onRebuild:()=>window.rebuildCount=(window.rebuildCount||0)+1}});
};
await window.renderBacklinks();
window.backlinksReady=true;
</script></body></html>` }));
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 = [];
await page.goto(`${smokeUrl}/__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` });
await writeFile(path.join(output, "measurements.json"), JSON.stringify(measurements,null,2));
console.log("Backlinks smoke passed: themes, narrow panels, long lists, keyboard, empty, busy, error, and contrast.");
}, { viewport: { width: 720, height: 640 }, timeout: 30000 });