117 lines
6.3 KiB
JavaScript
117 lines
6.3 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { writeFile } from 'node:fs/promises';
|
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
|
|
const [port, scenario, artifact, title] = process.argv.slice(2);
|
|
let socket;
|
|
const pending = new Map();
|
|
let sequence = 0;
|
|
|
|
async function waitFor(callback, label) {
|
|
for (let i = 0; i < 150; ++i) {
|
|
const result = await callback();
|
|
if (result) return result;
|
|
await delay(100);
|
|
}
|
|
throw new Error('Timed out waiting for ' + label);
|
|
}
|
|
|
|
function command(method, params = {}) {
|
|
const id = ++sequence;
|
|
return new Promise((resolve, reject) => {
|
|
const timer = setTimeout(() => {
|
|
pending.delete(id);
|
|
reject(new Error('DevTools timed out on ' + method));
|
|
}, 5000);
|
|
pending.set(id, reply => {
|
|
clearTimeout(timer);
|
|
if (reply.error) reject(new Error(reply.error.message));
|
|
else resolve(reply.result);
|
|
});
|
|
socket.send(JSON.stringify({ id, method, params }));
|
|
});
|
|
}
|
|
|
|
async function evaluate(expression) {
|
|
const reply = await command('Runtime.evaluate', { expression, returnByValue: true });
|
|
if (reply.exceptionDetails) throw new Error(reply.exceptionDetails.exception.description);
|
|
return reply.result.value;
|
|
}
|
|
|
|
const target = await waitFor(async () => {
|
|
const response = await fetch('http://127.0.0.1:' + port + '/json/list').catch(() => null);
|
|
if (!response?.ok) return;
|
|
return (await response.json()).find(item => item.url === 'https://kswitcher-markdown.invalid/markdown.html' &&
|
|
(!title || item.title === title));
|
|
}, 'the Markdown page in WebView2');
|
|
socket = new WebSocket(target.webSocketDebuggerUrl);
|
|
socket.addEventListener('message', event => {
|
|
const reply = JSON.parse(event.data);
|
|
const callback = pending.get(reply.id);
|
|
if (callback) {
|
|
pending.delete(reply.id);
|
|
callback(reply);
|
|
}
|
|
});
|
|
await new Promise((resolve, reject) => {
|
|
socket.addEventListener('open', resolve, { once: true });
|
|
socket.addEventListener('error', reject, { once: true });
|
|
});
|
|
|
|
try {
|
|
if (scenario === 'render') {
|
|
await waitFor(() => evaluate(`document.documentElement?.classList.contains('rendered') &&
|
|
!!document.querySelector('figure svg') &&
|
|
document.querySelector('img')?.naturalWidth === 1`), 'rendered Mermaid SVG and relative image');
|
|
assert.equal(await evaluate(`document.querySelector('h1').textContent`), 'Перегляд Markdown');
|
|
assert.equal(await evaluate(`[...document.querySelectorAll('nav button')].every(button => {
|
|
const style = getComputedStyle(button);
|
|
return style.borderTopWidth === '0px' && style.backgroundColor === 'rgba(0, 0, 0, 0)';
|
|
})`), true);
|
|
assert.equal(await evaluate(`document.querySelector('td').textContent`), 'Cell');
|
|
assert.equal(await evaluate(`document.querySelector('figure svg').getBoundingClientRect().height > 0`), true);
|
|
assert.equal(await evaluate(`document.querySelector('figure svg').textContent.includes('Render target')`), true);
|
|
assert.equal(await evaluate(`!!document.querySelector('pre code.language-mermaid') &&
|
|
document.querySelector('main').textContent.includes('Invalid Mermaid diagram')`), true);
|
|
assert.equal(await evaluate(`!!document.querySelector('pre code.language-js') && !document.querySelector('iframe')`), true);
|
|
assert.equal(await evaluate(`performance.getEntriesByType('resource')
|
|
.every(entry => entry.name.startsWith(location.origin + '/') || entry.name.startsWith('data:'))`), true);
|
|
await waitFor(() => evaluate(`document.querySelector('img[alt="Missing image"]')?.title === 'Image unavailable'`),
|
|
'missing image feedback');
|
|
assert.deepEqual(await evaluate(`[...document.querySelectorAll('input')].map(box => box.checked)`), [false, true]);
|
|
assert.deepEqual(await evaluate(`[...document.querySelectorAll('input')].map(box => { box.click(); return box.checked; })`), [true, false]);
|
|
assert.deepEqual(await evaluate(`[...document.querySelectorAll('input')].map(box => { box.click(); return box.checked; })`), [false, true]);
|
|
if (artifact) {
|
|
const { data } = await command('Page.captureScreenshot');
|
|
await writeFile(artifact, Buffer.from(data, 'base64'));
|
|
}
|
|
} else if (scenario === 'edit') {
|
|
await command('Input.dispatchKeyEvent', { type: 'keyDown', key: 'F6', code: 'F6', windowsVirtualKeyCode: 117 });
|
|
} else if (['follow', 'shift-follow', 'ctrl-follow'].includes(scenario)) {
|
|
await evaluate(`document.querySelector('a[href^="child/"]').dispatchEvent(new MouseEvent('click', {
|
|
bubbles: true, cancelable: true, shiftKey: ${scenario === 'shift-follow'}, ctrlKey: ${scenario === 'ctrl-follow'}
|
|
}))`);
|
|
} else if (scenario === 'mouse-back' || scenario === 'mouse-forward') {
|
|
const button = scenario === 'mouse-back' ? 'back' : 'forward';
|
|
await waitFor(() => evaluate(`!document.getElementById('${button}').disabled`), button);
|
|
await command('Input.dispatchMouseEvent', {type:'mousePressed', x:15, y:80, button, buttons:button === 'back' ? 8 : 16});
|
|
await command('Input.dispatchMouseEvent', {type:'mouseReleased', x:15, y:80, button, buttons:0});
|
|
} else if (scenario === 'back' || scenario === 'forward') {
|
|
await waitFor(() => evaluate(`!document.getElementById('${scenario}').disabled`), scenario);
|
|
await evaluate(`document.getElementById('${scenario}').click()`);
|
|
} else if (scenario === 'cycle') {
|
|
await command('Input.dispatchKeyEvent', {type:'keyDown', key:'Tab', code:'Tab', modifiers:2, windowsVirtualKeyCode:9});
|
|
} else if (scenario === 'close') {
|
|
await command('Input.dispatchKeyEvent', {type:'keyDown', key:'w', code:'KeyW', modifiers:2, windowsVirtualKeyCode:87});
|
|
} else {
|
|
await waitFor(() => evaluate('document.documentElement?.classList.contains("rendered") && ' +
|
|
'document.querySelector("h1")?.textContent === ' + JSON.stringify(scenario)), scenario);
|
|
if (scenario === 'Linked Markdown') {
|
|
await waitFor(() => evaluate('scrollY > 0 && document.getElementById("target-heading").getBoundingClientRect().top < innerHeight'),
|
|
'the relative heading fragment to scroll into view');
|
|
}
|
|
}
|
|
} finally {
|
|
socket.close();
|
|
}
|