import { test, expect, Page } from '@playwright/test'; import { api, createRoom, enc, ensureUser, hsReachable, loginUI, openRoom, sendText, uniq, } from './localHs'; // Scheduled messages (MSC4140 delayed events). Each step is checked on the // server too, so this proves the delayed event really exists, fires and is // cancelled, not just that the tray looks right. type Delayed = { delayed_events?: { delay_id: string; room_id: string }[] }; const delayedOnServer = (token: string) => api('GET', '/_matrix/client/unstable/org.matrix.msc4140/delayed_events', token).then( (r) => r.delayed_events ?? [], ); // The tray remembers whether it is open; open it only if its rows are hidden. const openTray = async (page: Page) => { const rowButton = page.getByRole('button', { name: /^Cancel scheduled message: / }); if (await rowButton.count()) return; await page.getByRole('button', { name: /^\d+ scheduled messages?$/ }).click(); await expect(rowButton.first()).toBeVisible(); }; const schedule = async (page: Page, text: string) => { await page.getByRole('button', { name: 'Schedule message' }).click(); const dialog = page.getByRole('dialog', { name: 'Schedule message' }); await dialog.locator('#schedule-message-body').fill(text); // Two hours ahead, in the browser's own time zone. const { date, time } = await page.evaluate(() => { const d = new Date(Date.now() + 2 * 60 * 60 * 1000); const p = (n: number) => String(n).padStart(2, '0'); return { date: `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`, time: `${p(d.getHours())}:${p(d.getMinutes())}`, }; }); await dialog.locator('#schedule-date').fill(date); await dialog.locator('#schedule-time').fill(time); await dialog.getByRole('button', { name: 'Schedule', exact: true }).click(); await expect(dialog).toHaveCount(0); }; test.describe('scheduled messages', () => { test.beforeAll(async () => { test.skip(!(await hsReachable()), 'local homeserver not running'); }); test('schedule, send now, and cancel — the server agrees at each step', async ({ page }) => { test.setTimeout(120_000); const me = await ensureUser(uniq('sched')); const room = await createRoom(me, 'Schedule Room'); await sendText(me, room, 'room is ready'); await loginUI(page, me); await openRoom(page, room); // 1. Schedule: tray shows it, the server holds one delayed event. const first = uniq('later '); await schedule(page, first); await expect(page.getByRole('button', { name: '1 scheduled message' })).toBeVisible(); await expect .poll(async () => (await delayedOnServer(me.token)).length, { intervals: [1000] }) .toBe(1); expect((await delayedOnServer(me.token))[0].room_id).toBe(room); await expect(page.locator('[data-message-item]', { hasText: first })).toHaveCount(0); // 2. Send now: it arrives once, the delayed event is gone. await openTray(page); await page.getByRole('button', { name: /^Send scheduled message now: / }).click(); await expect(page.locator('[data-message-item]', { hasText: first })).toHaveCount(1, { timeout: 20_000, }); await expect .poll(async () => (await delayedOnServer(me.token)).length, { intervals: [1000] }) .toBe(0); // 3. Cancel: removed from the server and never sent. const second = uniq('never '); await schedule(page, second); await expect .poll(async () => (await delayedOnServer(me.token)).length, { intervals: [1000] }) .toBe(1); await openTray(page); await page.getByRole('button', { name: /^Cancel scheduled message: / }).click(); await expect .poll(async () => (await delayedOnServer(me.token)).length, { intervals: [1000] }) .toBe(0); await expect(page.getByRole('button', { name: /scheduled message/ })).toHaveCount(0); const msgs = await api<{ chunk: { content?: { body?: string } }[] }>( 'GET', `/_matrix/client/v3/rooms/${enc(room)}/messages?dir=b&limit=50`, me.token, ); expect(msgs.chunk.some((e) => e.content?.body === second)).toBe(false); }); });