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((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/)); });