// Smoke test for the Empty Link Fixer content script.
//
// Runs the actual config.js + content.js against jsdom pages and verifies that
// empty anchors are filled — statically, on dynamically added content, emptied
// text, late href attributes and inside (open) shadow DOM — while links that
// have text or media content are left alone.
//
// Usage: node tests/smoke.mjs (needs `npm install --no-save jsdom`)
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import assert from 'node:assert/strict';
import { JSDOM } from 'jsdom';
const dir = fileURLToPath(new URL('..', import.meta.url));
const configJs = readFileSync(`${dir}config.js`, 'utf8');
const contentJs = readFileSync(`${dir}content.js`, 'utf8');
const tick = (ms = 40) => new Promise((r) => setTimeout(r, ms));
function boot(html, { url = 'https://example.com/', hosts = ['example.com'] } = {}) {
const dom = new JSDOM(html, {
url,
runScripts: 'outside-only',
pretendToBeVisual: true,
});
const { window } = dom;
window.eval(configJs);
window.eval(`globalThis.LINK_FIX_CONFIG = ${JSON.stringify({
hosts,
includeSubdomains: true,
rescanIntervalMs: 0, // disable the safety-net timer during tests
debug: false,
})};`);
window.eval(contentJs);
return dom;
}
const $ = (dom, sel) => dom.window.document.querySelector(sel);
async function run() {
// ---------------------------------------------------------------- static
{
const dom = boot(`
Already has text
`);
await tick();
assert.equal($(dom, '#e1').textContent, 'https://example.com/a', 'empty anchor filled with href');
assert.equal($(dom, '#e2').textContent.trim(), 'Already has text', 'text anchor untouched');
assert.equal($(dom, '#e3').textContent, '', 'image anchor untouched');
assert.ok($(dom, '#e3').querySelector('img'), 'image preserved inside anchor');
assert.equal($(dom, '#e4').textContent, '', 'svg anchor untouched');
assert.ok($(dom, '#e4').querySelector('svg'), 'svg preserved inside anchor');
assert.equal($(dom, '#e5').textContent.trim(), '/ws', 'whitespace-only anchor filled');
assert.equal($(dom, '#e6').textContent, '', 'placeholder href="#" not filled');
assert.equal($(dom, '#e7').textContent, '', 'javascript: href not filled');
assert.equal($(dom, '#e8').textContent.trim(), '/hidden', 'empty anchor filled regardless of visibility');
dom.window.close();
}
// ------------------------------------------------- dynamically inserted
{
const dom = boot('