~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
105 lines · 4.8 KB Raw History
// The strip as a swipe right moves it: carol's canvas of ten columns,
// their tiles drawing as they come near, swiped with the wheel over a
// header, sideways but now and then more up than sideways (as a trackpad's
// swipe goes), just after a column was gone to (shown and flashed), with a
// tile added as it goes. The page scrolls the strip none of itself, and
// the strip only goes right; then a column gone to is brought into view.
// The browser's own snapping is turned off while it is swiped: Chromium
// snaps each of Playwright's wheels, which are not a trackpad's, back to a
// column's start. The canvas is deleted after, leaving carol's world as
// it was.

import { expect, signIn, test, who } from './pages.mjs';

test('a swipe right is never taken back: the page scrolls the strip none of itself', async ({ browser }) => {
	const context = await browser.newContext();
	const page = await context.newPage();
	const errors = [];
	page.on('pageerror', (error) => errors.push(error.message));
	await signIn(page, 'carol');
	const procedure = (method, body) =>
		page.evaluate(
			async ([method, body]) => {
				const res = await fetch(`/xrpc/place.hud.${method}`, {
					method: 'POST',
					headers: { 'Content-Type': 'application/json', 'X-HUD': '1' },
					body: JSON.stringify(body),
				});
				return res.json();
			},
			[method, body],
		);
	const posts = (limit) => ['list_records', { actor: who.alice.did, collection: 'app.bsky.feed.post', limit }];
	const calls = [posts(1), posts(2), posts(3), posts(4), posts(5), posts(6), posts(7), ['describe_repo', { actor: who.alice.did }], ['describe_repo', { actor: who.bob.did }], ['describe_repo', { actor: who.carol.did }]];
	const { canvas } = await procedure('createCanvas', { name: 'Swiped' });
	try {
		for (const [i, [tool, args]] of calls.entries()) {
			const { tile } = await procedure('addTile', { canvas: canvas.key, tool, arguments: args, title: `Column ${i + 1}`, width: 'third' });
			expect(tile?.key).toBeTruthy();
		}
		await page.reload();
		await page.locator(`[role="tab"][data-tab="${canvas.key}"]`).click();
		const strip = page.getByRole('main', { name: 'Columns' });
		await expect(strip.locator('article[data-tile]')).toHaveCount(calls.length);
		await expect(strip.locator('[aria-busy="true"]')).toHaveCount(0);

		// Gone to, as the palette goes to an open tile: shown and flashed.
		const go = async (title) => {
			await page.keyboard.press('k');
			await page.getByRole('searchbox', { name: 'Search everything' }).fill(title);
			await page.keyboard.press('Enter');
			await expect(strip.getByRole('article', { name: title, exact: true })).toBeInViewport({ ratio: 0.9 });
		};
		await go('Column 1');

		await page.addStyleTag({ content: '.strip { scroll-snap-type: none !important; }' });
		await strip.evaluate((main) => {
			const writes = (window.__writes = []);
			for (const name of ['scrollTo', 'scroll', 'scrollBy']) {
				main[name] = function (...args) {
					writes.push(`${name}(${JSON.stringify(args)})`);
					return Element.prototype[name].apply(this, args);
				};
			}
			const left = Object.getOwnPropertyDescriptor(Element.prototype, 'scrollLeft');
			Object.defineProperty(main, 'scrollLeft', {
				get: left.get,
				set(v) {
					writes.push(`scrollLeft = ${v}`);
					left.set.call(this, v);
				},
			});
			const xs = (window.__xs = []);
			const sample = () => {
				xs.push(left.get.call(main));
				if (!window.__done) requestAnimationFrame(sample);
			};
			requestAnimationFrame(sample);
		});
		const head = await strip.locator('article header').first().boundingBox();
		await page.mouse.move(head.x + head.width / 2, head.y + head.height / 2);
		let added;
		for (let i = 0; i < 48; i++) {
			const up = i % 8 === 7;
			await page.mouse.wheel(up ? 4 : 40, up ? 60 : 2);
			if (i === 16) added = procedure('addTile', { canvas: canvas.key, tool: 'list_records', arguments: { actor: who.carol.did, collection: 'fm.plyr.like' }, title: 'Arrived', width: 'third' });
			await page.waitForTimeout(16);
		}
		expect((await added)?.tile?.key).toBeTruthy();
		await expect(strip.getByRole('article', { name: 'Arrived', exact: true })).toHaveCount(1);
		await page.waitForTimeout(500);
		const { writes, xs } = await page.evaluate(() => ((window.__done = true), { writes: window.__writes, xs: window.__xs }));
		expect(writes, 'the page scrolled the strip itself as it was swiped').toEqual([]);
		const back = xs.flatMap((x, i) => (i > 0 && x < xs[i - 1] - 0.5 ? [`${xs[i - 1]} → ${x}`] : []));
		expect(back, 'the strip went back left as it was swiped right').toEqual([]);
		expect(xs.at(-1)).toBeGreaterThan(xs[0] + 1000);
		expect(errors).toEqual([]);

		// Asked, it still goes there.
		await go('Column 1');
		await go('Arrived');
	} finally {
		await procedure('deleteCanvas', { canvas: canvas.key });
		await context.close();
	}
});