oweme/e2e/push.spec.ts
agent a78d855886 Add Playwright end-to-end tests
Two devices (EN + CS) sharing a group through sync, photos, rates, rewind,
offline and settle-up; plus push delivery to the service worker via CDP.
Can target a running deployment with E2E_BASE_URL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FYo6JSV1fDU1fm3LU77iF
2026-10-09 19:59:16 +00:00

49 lines
2.6 KiB
TypeScript

import { expect, test } from '@playwright/test';
// Delivers a push message straight to the service worker over CDP (no real
// push service involved) and checks that it decrypts the new events and
// shows a readable notification.
// The stripped-down headless shell always denies notifications; full
// Chromium in headless mode supports them.
test.use({ channel: 'chromium' });
test('service worker turns a push into a decrypted notification', async ({ browser, baseURL }) => {
const annaCtx = await browser.newContext({ locale: 'en-US' });
await annaCtx.grantPermissions(['clipboard-read', 'clipboard-write', 'notifications']);
const anna = await annaCtx.newPage();
await anna.goto('/new');
await anna.getByLabel('Group name').fill('Flat');
await anna.getByLabel('Your name').fill('Anna');
await anna.getByPlaceholder('Name').fill('Petr');
await anna.getByRole('button', { name: 'Create group' }).click();
await anna.getByRole('button', { name: 'Copy link' }).click();
const link = await anna.evaluate(() => navigator.clipboard.readText());
const groupId = /\/g\/([^#]+)#/.exec(link)![1];
await anna.evaluate(() => navigator.serviceWorker.ready);
// Petr adds an expense from another device.
const petr = await (await browser.newContext({ locale: 'en-US' })).newPage();
await petr.goto(link);
await petr.getByRole('button', { name: 'Petr' }).click();
await petr.getByRole('link', { name: 'Add expense' }).click();
await petr.getByLabel('Amount').fill('12.50');
await petr.getByLabel('What was it for?').fill('Pizza');
await petr.getByRole('button', { name: 'Save', exact: true }).click();
await expect(petr.getByRole('link', { name: /Pizza/ })).toBeVisible();
// Anna is on the home screen (not looking at the group) when the push arrives.
await anna.goto('/');
const cdp = await annaCtx.newCDPSession(anna);
const registrationId = await new Promise<string>((resolve) => {
cdp.on('ServiceWorker.workerRegistrationUpdated', ({ registrations }) => {
const r = registrations.find((x: { scopeURL: string; isDeleted: boolean }) => x.scopeURL.startsWith(baseURL!) && !x.isDeleted);
if (r) resolve(r.registrationId);
});
void cdp.send('ServiceWorker.enable');
});
await cdp.send('ServiceWorker.deliverPushMessage', { origin: new URL(baseURL!).origin, registrationId, data: JSON.stringify({ g: groupId }) });
await expect
.poll(async () => anna.evaluate(async () => (await (await navigator.serviceWorker.ready).getNotifications()).map((n) => `${n.title} | ${n.body}`)), { timeout: 15_000 })
.toContainEqual(expect.stringMatching(/Flat \| Petr: Pizza · \S*12\.50/));
});