~ 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
141 lines · 6.1 KB Raw History
// What plays goes on as carol moves about the workspace: her liked tracks
// (world.mjs's seedTracks, audio on her PDS, played through HUD's blob
// proxy) play in the workspace's one <audio>, apart from the tile, through
// scrolling it far away, switching tabs, refreshing it and closing it; the
// mini player shows the track, plays, pauses and skips, and goes back to
// the tile while it is open.

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

test.describe.configure({ mode: 'serial' });

/** @type {import('@playwright/test').BrowserContext} */
let context;
/** @type {import('@playwright/test').Page} */
let page;

test.beforeAll(async ({ browser }) => {
	context = await browser.newContext();
	page = await context.newPage();
});
test.afterAll(() => context?.close());

const strip = () => page.getByRole('main', { name: 'Columns' });
const tabs = () => page.getByRole('tablist', { name: 'Canvases' }).getByRole('tab');
const mini = () => page.getByRole('banner').getByRole('region', { name: 'Now playing' });
const audio = () => page.locator('audio');
/** The audio's state: whether it plays, and how far in. */
const state = () => audio().evaluate((a) => ({ paused: a.paused, time: a.currentTime, src: a.getAttribute('src') }));

/** That the audio plays on: not paused, and further in a moment later. */
async function playsOn() {
	await expect.poll(async () => (await state()).paused).toBe(false);
	const before = (await state()).time;
	await expect.poll(async () => (await state()).time).toBeGreaterThan(before);
}

let likes = '';

test('carol’s liked tracks play, and the mini player shows the track', async () => {
	await signIn(page, 'carol');
	const canvas = await tabs().first().getAttribute('data-tab');
	const token = await mcpToken(context, 'carol');
	const added = await mcpCall(token, 'canvas_add', {
		canvas,
		tool: 'list_records',
		arguments: { actor: who.carol.did, collection: 'fm.plyr.like' },
		title: 'Liked tracks',
	});
	expect(added.isError ?? false, JSON.stringify(added)).toBe(false);
	const tile = strip().getByRole('article', { name: 'Liked tracks', exact: true });
	await expect(tile.getByRole('button', { name: 'Play Track one' })).toBeVisible();
	likes = await tile.getAttribute('data-tile');
	// One <audio>, the workspace's, loading nothing until played.
	await expect(audio()).toHaveCount(1);
	expect((await state()).src).toBeNull();

	await tile.getByRole('button', { name: 'Play Track one' }).click();
	await playsOn();
	expect((await state()).src).toContain('/blob');
	await expect(mini()).toContainText('Track one');
	await expect(mini()).toContainText('carol');
	await expect(tile.getByRole('button', { name: 'Play Track one' })).toHaveAttribute('aria-current', 'true');
});

test('its controls pause, play and skip', async () => {
	await mini().getByRole('button', { name: 'Pause' }).click();
	await expect.poll(async () => (await state()).paused).toBe(true);
	await mini().getByRole('button', { name: 'Play' }).click();
	await playsOn();
	await mini().getByRole('button', { name: 'Next' }).click();
	await expect(mini()).toContainText('Track two');
	await playsOn();
});

test('it plays on with its tile scrolled far away, and Go to brings the tile back', async () => {
	const token = await mcpToken(context, 'carol');
	const canvas = await tabs().first().getAttribute('data-tab');
	for (let i = 0; i < 4; i++) {
		const added = await mcpCall(token, 'canvas_add', {
			canvas,
			tool: 'list_records',
			// Each a call of its own: the same call again adds nothing.
			arguments: { actor: who.alice.did, collection: 'app.bsky.feed.post', limit: 10 + i },
			title: `Posts ${i + 1}`,
			width: 'full',
		});
		expect(added.isError ?? false, JSON.stringify(added)).toBe(false);
	}
	await expect(strip().locator('article[data-tile]')).toHaveCount(5);
	const tile = strip().locator(`article[data-tile="${likes}"]`);
	await expect(tile.locator('.player')).toHaveCount(1);
	await strip().evaluate((main) => {
		const box = [main, ...main.querySelectorAll('*')].find((e) => e.scrollWidth > e.clientWidth + 1 && getComputedStyle(e).overflowX !== 'visible');
		box?.scrollTo({ left: box.scrollWidth, behavior: 'instant' });
	});
	// Far away, the tile draws nothing: its player is gone, the music is not.
	await expect(tile.locator('.player')).toHaveCount(0);
	await playsOn();

	await mini().getByRole('button', { name: 'Go to Track two' }).click();
	await expect(tile).toBeInViewport({ ratio: 0.5 });
	await expect(tile.getByRole('button', { name: 'Play Track two' })).toHaveAttribute('aria-current', 'true');
});

test('it plays on in another tab', async () => {
	await page.getByRole('banner').locator('summary[aria-label="Canvases"]').click();
	await page.getByRole('menuitem', { name: /New canvas/ }).click();
	await expect(tabs()).toHaveCount(2);
	await page.keyboard.press('Enter');
	await expect(tabs().nth(1)).toHaveAttribute('aria-selected', 'true');
	await playsOn();
	await expect(mini()).toContainText('Track two');
	// Go to switches back to the tile's tab.
	await mini().getByRole('button', { name: 'Go to Track two' }).click();
	await expect(tabs().first()).toHaveAttribute('aria-selected', 'true');
	await expect(strip().locator(`article[data-tile="${likes}"]`)).toBeInViewport({ ratio: 0.5 });
});

test('it plays on as its tile refreshes, and as it closes', async () => {
	const tile = strip().locator(`article[data-tile="${likes}"]`);
	await tile.hover();
	await tile.getByRole('button', { name: 'Refresh' }).click();
	await expect(tile.getByRole('button', { name: 'Play Track two' })).toHaveAttribute('aria-current', 'true');
	await playsOn();

	await tile.hover();
	await tile.getByRole('button', { name: 'Close' }).click();
	await expect(tile).toHaveCount(0);
	await playsOn();
	// Its tile gone, nothing to go to.
	await expect(mini()).toContainText('Track two');
	await expect(mini().getByRole('button', { name: /^Go to / })).toHaveCount(0);
});

test('stopping from the mini player stops the music and lets go of it', async () => {
	await mini().getByRole('button', { name: 'Stop' }).click();
	await expect(mini()).toHaveCount(0);
	const after = await state();
	expect(after.paused).toBe(true);
	expect(after.src).toBeNull();
});