|
| 1 | +import { test, expect, Page, Browser, BrowserContext } from '@playwright/test'; |
| 2 | + |
| 3 | +async function getRevealState(page: Page) { |
| 4 | + return await page.evaluate(() => { |
| 5 | + const reveal = (window as any).Reveal; |
| 6 | + return reveal ? reveal.getState() : null; |
| 7 | + }); |
| 8 | +} |
| 9 | + |
| 10 | +const SOCKET_PROPAGATION_DELAY = 500; |
| 11 | + |
| 12 | +async function createMultiplexPage(browser: Browser, url: string) { |
| 13 | + const context = await browser.newContext(); |
| 14 | + const page = await context.newPage(); |
| 15 | + await page.goto(url); |
| 16 | + return { context, page }; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Helper to create both master and client pages for multiplex testing |
| 21 | + */ |
| 22 | +async function createMultiplexPages(browser: Browser) { |
| 23 | + const master = await createMultiplexPage(browser, './revealjs/multiplex-speaker.html'); |
| 24 | + const client = await createMultiplexPage(browser, './revealjs/multiplex.html'); |
| 25 | + return { master, client }; |
| 26 | +} |
| 27 | + |
| 28 | +async function expectSlidePositions( |
| 29 | + master: Page, |
| 30 | + client: Page, |
| 31 | + expectedMasterH: number, |
| 32 | + expectedClientH: number, |
| 33 | + expectedMasterV?: number, |
| 34 | + expectedClientV?: number |
| 35 | +) { |
| 36 | + const masterState = await getRevealState(master); |
| 37 | + const clientState = await getRevealState(client); |
| 38 | + |
| 39 | + expect(masterState.indexh).toBe(expectedMasterH); |
| 40 | + expect(clientState.indexh).toBe(expectedClientH); |
| 41 | + |
| 42 | + if (expectedMasterV !== undefined) { |
| 43 | + expect(masterState.indexv).toBe(expectedMasterV); |
| 44 | + } |
| 45 | + if (expectedClientV !== undefined) { |
| 46 | + expect(clientState.indexv).toBe(expectedClientV); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +async function expectFragmentPositions( |
| 51 | + master: Page, |
| 52 | + client: Page, |
| 53 | + expectedMasterF: number, |
| 54 | + expectedClientF: number |
| 55 | +) { |
| 56 | + const masterState = await getRevealState(master); |
| 57 | + const clientState = await getRevealState(client); |
| 58 | + |
| 59 | + expect(masterState.indexf).toBe(expectedMasterF); |
| 60 | + expect(clientState.indexf).toBe(expectedClientF); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Test the multiplex feature where a master presentation controls client presentations. |
| 65 | + * Tests: slide synchronization, fragment synchronization, and unidirectional control. |
| 66 | + */ |
| 67 | +test('multiplex: master controls client, fragments sync, and client cannot control master', async ({ browser }) => { |
| 68 | + const { master, client } = await createMultiplexPages(browser); |
| 69 | + |
| 70 | + try { |
| 71 | + // Both should start on title slide |
| 72 | + await expectSlidePositions(master.page, client.page, 0, 0, 0, 0); |
| 73 | + |
| 74 | + // Test 1: Basic slide synchronization |
| 75 | + await master.page.keyboard.press('ArrowRight'); |
| 76 | + await master.page.waitForTimeout(SOCKET_PROPAGATION_DELAY); |
| 77 | + await expectSlidePositions(master.page, client.page, 1, 1); |
| 78 | + |
| 79 | + await master.page.keyboard.press('ArrowRight'); |
| 80 | + await master.page.waitForTimeout(SOCKET_PROPAGATION_DELAY); |
| 81 | + await expectSlidePositions(master.page, client.page, 2, 2); |
| 82 | + |
| 83 | + // Test 2: Fragment synchronization on slide 3 |
| 84 | + await master.page.keyboard.press('ArrowRight'); // slide 2 -> slide 3 |
| 85 | + await master.page.waitForTimeout(SOCKET_PROPAGATION_DELAY); |
| 86 | + await expectSlidePositions(master.page, client.page, 3, 3); |
| 87 | + |
| 88 | + // Show first fragment |
| 89 | + await master.page.keyboard.press('ArrowRight'); |
| 90 | + await master.page.waitForTimeout(SOCKET_PROPAGATION_DELAY); |
| 91 | + await expectFragmentPositions(master.page, client.page, 0, 0); |
| 92 | + |
| 93 | + // Show second fragment |
| 94 | + await master.page.keyboard.press('ArrowRight'); |
| 95 | + await master.page.waitForTimeout(SOCKET_PROPAGATION_DELAY); |
| 96 | + await expectFragmentPositions(master.page, client.page, 1, 1); |
| 97 | + |
| 98 | + // Test 3: Client cannot control master |
| 99 | + // Navigate back to title slide |
| 100 | + await master.page.goto('./revealjs/multiplex-speaker.html#/'); |
| 101 | + await master.page.waitForTimeout(SOCKET_PROPAGATION_DELAY); |
| 102 | + await expectSlidePositions(master.page, client.page, 0, 0); |
| 103 | + |
| 104 | + // Client tries to navigate (should not affect master) |
| 105 | + await client.page.keyboard.press('ArrowRight'); |
| 106 | + await client.page.waitForTimeout(SOCKET_PROPAGATION_DELAY); |
| 107 | + await expectSlidePositions(master.page, client.page, 0, 1); |
| 108 | + |
| 109 | + // Master overrides client's position |
| 110 | + await master.page.keyboard.press('ArrowRight'); |
| 111 | + await master.page.keyboard.press('ArrowRight'); |
| 112 | + await master.page.waitForTimeout(SOCKET_PROPAGATION_DELAY); |
| 113 | + await expectSlidePositions(master.page, client.page, 2, 2); |
| 114 | + |
| 115 | + } finally { |
| 116 | + await master.context.close(); |
| 117 | + await client.context.close(); |
| 118 | + } |
| 119 | +}); |
0 commit comments