import { type Page, expect, test } from '@playwright/test';
type EventLog = { type: string; detail: unknown };
test.beforeEach(async ({ page }) => {
await page.addInitScript(() => {
const w = window as unknown as { __events: EventLog[] };
w.__events = [];
const push = (e: Event) => w.__events.push({ type: e.type, detail: (e as CustomEvent).detail });
window.addEventListener('physarum:ready', push);
window.addEventListener('physarum:presetchange', push);
window.addEventListener('physarum:error', push);
});
});
const types = (page: Page) =>
page.evaluate(() => (window as unknown as { __events: EventLog[] }).__events.map((e) => e.type));
const changedIds = (page: Page) =>
page.evaluate(() =>
(window as unknown as { __events: EventLog[] }).__events
.filter((e) => e.type === 'physarum:presetchange')
.map((e) => (e.detail as { id: string }).id),
);
test('player.html boots a built-in preset and emits ready', async ({ page }) => {
await page.goto('/player.html?preset=builtin:aurora&controls=always');
await expect.poll(() => types(page), { timeout: 15_000 }).toContain('physarum:ready');
await expect.poll(() => changedIds(page)).toContain('aurora');
});
test('playlist next advances to the next preset', async ({ page }) => {
await page.goto(
'/player.html?playlist=builtin:aurora,builtin:mandala&controls=always&interval=999',
);
await expect.poll(() => types(page), { timeout: 15_000 }).toContain('physarum:ready');
await page.locator('physarum-player').getByRole('button', { name: 'Next preset' }).click();
await expect.poll(() => changedIds(page), { timeout: 15_000 }).toContain('mandala');
});