106 lines
4.9 KiB
JavaScript
106 lines
4.9 KiB
JavaScript
// End-to-end check of Back and Forward navigation history, UI buttons, and mouse buttons 3 & 4.
|
|
const { app, BrowserWindow } = require('electron');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
const fs = require('fs');
|
|
|
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ink-e2e-nav-'));
|
|
const vault = path.join(tmp, 'Vault');
|
|
fs.mkdirSync(vault);
|
|
fs.writeFileSync(path.join(vault, 'Alpha.md'), 'Content of Alpha\n');
|
|
fs.writeFileSync(path.join(vault, 'Beta.md'), 'Content of Beta\n');
|
|
fs.writeFileSync(path.join(vault, 'Gamma.md'), 'Content of Gamma\n');
|
|
|
|
app.setPath('userData', tmp);
|
|
fs.writeFileSync(path.join(tmp, 'settings.json'), JSON.stringify({ vault, lastPage: 'Alpha', bounds: { width: 1280, height: 860 } }));
|
|
require('../electron/main.cjs');
|
|
|
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
const failures = [];
|
|
const check = (name, ok, extra = '') => {
|
|
console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${extra ? ' — ' + extra : ''}`);
|
|
if (!ok) failures.push(name);
|
|
};
|
|
|
|
app.whenReady().then(async () => {
|
|
let win;
|
|
for (let i = 0; i < 80 && !(win = BrowserWindow.getAllWindows()[0]); i++) await sleep(100);
|
|
const wc = win.webContents;
|
|
if (wc.isLoading()) await new Promise((r) => wc.once('did-finish-load', r));
|
|
const js = (code) => wc.executeJavaScript(`(async () => { ${code} })()`);
|
|
|
|
try {
|
|
await sleep(2000);
|
|
const title = () => js(`return document.querySelector('.title')?.textContent?.trim()`);
|
|
const backDisabled = () => js(`return !!document.querySelector('.nav-btn[aria-label="Back"]')?.disabled`);
|
|
const forwardDisabled = () => js(`return !!document.querySelector('.nav-btn[aria-label="Forward"]')?.disabled`);
|
|
|
|
check('starts on Alpha', (await title()) === 'Alpha', await title());
|
|
check('back is disabled initially', (await backDisabled()) === true);
|
|
check('forward is disabled initially', (await forwardDisabled()) === true);
|
|
|
|
// Navigate to Beta
|
|
await js(`document.querySelector('.row[data-id="Beta"]')?.click()`);
|
|
await sleep(600);
|
|
check('navigates to Beta', (await title()) === 'Beta', await title());
|
|
check('back is now enabled', (await backDisabled()) === false);
|
|
check('forward remains disabled', (await forwardDisabled()) === true);
|
|
|
|
// Navigate to Gamma
|
|
await js(`document.querySelector('.row[data-id="Gamma"]')?.click()`);
|
|
await sleep(600);
|
|
check('navigates to Gamma', (await title()) === 'Gamma', await title());
|
|
|
|
// Click Topbar Back button
|
|
await js(`document.querySelector('.nav-btn[aria-label="Back"]')?.click()`);
|
|
await sleep(600);
|
|
check('topbar Back button goes to Beta', (await title()) === 'Beta', await title());
|
|
check('forward is now enabled', (await forwardDisabled()) === false);
|
|
|
|
// Mouse button 3 (Back)
|
|
await js(`window.dispatchEvent(new MouseEvent('mouseup', { button: 3, bubbles: true, cancelable: true }))`);
|
|
await sleep(600);
|
|
check('mouse Back button (button 3) goes to Alpha', (await title()) === 'Alpha', await title());
|
|
check('back is now disabled at start of history', (await backDisabled()) === true);
|
|
check('forward is enabled', (await forwardDisabled()) === false);
|
|
|
|
// Mouse button 4 (Forward)
|
|
await js(`window.dispatchEvent(new MouseEvent('mouseup', { button: 4, bubbles: true, cancelable: true }))`);
|
|
await sleep(600);
|
|
check('mouse Forward button (button 4) goes to Beta', (await title()) === 'Beta', await title());
|
|
|
|
// Topbar Forward button
|
|
await js(`document.querySelector('.nav-btn[aria-label="Forward"]')?.click()`);
|
|
await sleep(600);
|
|
check('topbar Forward button goes to Gamma', (await title()) === 'Gamma', await title());
|
|
check('forward is disabled at end of history', (await forwardDisabled()) === true);
|
|
|
|
// Keyboard Alt+ArrowLeft
|
|
await js(`window.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowLeft', altKey: true, bubbles: true }))`);
|
|
await sleep(600);
|
|
check('Alt+ArrowLeft goes back to Beta', (await title()) === 'Beta', await title());
|
|
|
|
// Keyboard Alt+ArrowRight
|
|
await js(`window.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight', altKey: true, bubbles: true }))`);
|
|
await sleep(600);
|
|
check('Alt+ArrowRight goes forward to Gamma', (await title()) === 'Gamma', await title());
|
|
|
|
// Electron app-command browser-backward
|
|
win.emit('app-command', { preventDefault() {} }, 'browser-backward');
|
|
await sleep(600);
|
|
check('Electron app-command browser-backward goes to Beta', (await title()) === 'Beta', await title());
|
|
|
|
// Electron app-command browser-forward
|
|
win.emit('app-command', { preventDefault() {} }, 'browser-forward');
|
|
await sleep(600);
|
|
check('Electron app-command browser-forward goes to Gamma', (await title()) === 'Gamma', await title());
|
|
|
|
} catch (err) {
|
|
check('harness', false, err.stack);
|
|
}
|
|
|
|
fs.rmSync(tmp, { recursive: true, force: true });
|
|
console.log(failures.length ? `\n${failures.length} check(s) failed` : '\nnavigation e2e passed!');
|
|
app.exit(failures.length ? 1 : 0);
|
|
});
|