Files

64 lines
2.4 KiB
JavaScript

// Shared handles for the chat dock checks: the dock's own storage key, its
// landmarks, and the pointer gestures that pin it.
const assert = require('node:assert/strict');
const KEY = 'archie.chat.dock.v2';
const dockOf = (page) => page.getByRole('complementary', { name: 'Chat with Archie' });
const composerOf = (page) => page.getByRole('combobox', { name: 'Message Archie', exact: true });
const hideOf = (page) => page.getByRole('button', { name: 'Hide conversation' });
const dividerOf = (page) => page.getByRole('separator', { name: 'Resize chat' });
const stored = (page) => page.evaluate((key) => localStorage.getItem(key), KEY);
const saved = async (page) => JSON.parse(await stored(page));
// Loads /events with the given stored layout (a string, or null for none).
async function load(page, go, layout = null) {
await go('/events');
await page.evaluate(([key, value]) => (value === null ? localStorage.removeItem(key) : localStorage.setItem(key, value)), [KEY, layout]);
await go('/events');
}
// Focusing the composer is how the operator opens the conversation.
async function expand(page) {
await composerOf(page).focus();
await hideOf(page).waitFor();
}
// Retries an assertion until it holds: the dock settles over a few frames.
async function until(check, message, timeout = 5000) {
const end = Date.now() + timeout;
let last;
for (;;) {
try {
const ok = await check();
if (ok) return;
last = ok;
} catch (e) {
last = e;
}
if (Date.now() > end) throw new Error(`${message}${last instanceof Error ? `: ${last.message}` : ''}`);
await new Promise((r) => setTimeout(r, 50));
}
}
const near = (a, b, message, tolerance = 1) => assert.ok(Math.abs(a - b) <= tolerance, `${message}: ${a} is not within ${tolerance} of ${b}`);
// The empty strip at the top of the header, where a drag starts.
async function grip(page) {
const box = await dockOf(page).boundingBox();
return { x: box.x + box.width / 2, y: box.y + 8 };
}
async function dragTo(page, x, steps = 10) {
const from = await grip(page);
await page.mouse.move(from.x, from.y);
await page.mouse.down();
await page.mouse.move(x, from.y, { steps });
return from;
}
const mainRect = (page) => page.locator('#main-content').boundingBox();
module.exports = { KEY, dockOf, composerOf, hideOf, dividerOf, stored, saved, load, expand, until, near, grip, dragTo, mainRect };