|
| 1 | +import { expect, Page, BrowserContext } from "@playwright/test"; |
| 2 | +import { test as base, createBdd } from "playwright-bdd"; |
| 3 | + |
| 4 | +const test = base.extend<{ |
| 5 | + alicePage: Page; |
| 6 | + aliceContext: BrowserContext; |
| 7 | + bobPage: Page; |
| 8 | + bobContext: BrowserContext; |
| 9 | + tvPage: Page; |
| 10 | + tvContext: BrowserContext; |
| 11 | +}>({ |
| 12 | + aliceContext: async ({ browser }, use) => { |
| 13 | + const context = await browser.newContext(); |
| 14 | + await use(context); |
| 15 | + await context.close(); |
| 16 | + }, |
| 17 | + alicePage: async ({ aliceContext }, use) => { |
| 18 | + const page = await aliceContext.newPage(); |
| 19 | + await use(page); |
| 20 | + }, |
| 21 | + bobContext: async ({ browser }, use) => { |
| 22 | + const context = await browser.newContext(); |
| 23 | + await use(context); |
| 24 | + await context.close(); |
| 25 | + }, |
| 26 | + bobPage: async ({ bobContext }, use) => { |
| 27 | + const page = await bobContext.newPage(); |
| 28 | + await use(page); |
| 29 | + }, |
| 30 | + tvContext: async ({ browser }, use) => { |
| 31 | + const context = await browser.newContext(); |
| 32 | + await use(context); |
| 33 | + await context.close(); |
| 34 | + }, |
| 35 | + tvPage: async ({ tvContext }, use) => { |
| 36 | + const page = await tvContext.newPage(); |
| 37 | + await use(page); |
| 38 | + }, |
| 39 | +}); |
| 40 | + |
| 41 | +export { test }; |
| 42 | +const { Given, When, Then } = createBdd(test); |
| 43 | + |
| 44 | +const BASE_URL = "http://localhost:3000"; |
| 45 | + |
| 46 | +async function clearQueue(page: Page): Promise<void> { |
| 47 | + const response = await page.request.get(`${BASE_URL}/api/queue`); |
| 48 | + const data = await response.json(); |
| 49 | + if (data.queue) { |
| 50 | + for (const item of data.queue) { |
| 51 | + await page.request.delete( |
| 52 | + `${BASE_URL}/api/queue?queueItemId=${item.id}&userId=test` |
| 53 | + ); |
| 54 | + } |
| 55 | + } |
| 56 | + await page.request.put(`${BASE_URL}/api/queue`, { |
| 57 | + data: { action: "skip", userId: "test" }, |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +async function joinSession(page: Page, userName: string): Promise<void> { |
| 62 | + await page.goto(BASE_URL); |
| 63 | + const nameInput = page.getByTestId("username-input"); |
| 64 | + await nameInput.fill(userName); |
| 65 | + const joinButton = page.getByTestId("join-session-button"); |
| 66 | + await joinButton.click(); |
| 67 | + await page.waitForSelector('[data-testid="search-input"]', { |
| 68 | + timeout: 15000, |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +async function addSongToQueue(page: Page): Promise<void> { |
| 73 | + const artistItem = page.getByTestId("artist-item").first(); |
| 74 | + await artistItem.click(); |
| 75 | + const addButton = page.getByTestId("add-song-button").first(); |
| 76 | + await addButton.waitFor({ state: "visible", timeout: 30000 }); |
| 77 | + await addButton.click(); |
| 78 | + const dialog = page.locator("[data-testid='confirmation-dialog']"); |
| 79 | + await dialog.waitFor({ timeout: 10000 }); |
| 80 | + await dialog.locator("button[aria-label='Close']").click(); |
| 81 | + await dialog.waitFor({ state: "hidden", timeout: 5000 }); |
| 82 | +} |
| 83 | + |
| 84 | +Given( |
| 85 | + "{string} has joined the session on device {int}", |
| 86 | + async ({ alicePage, bobPage }, name: string, device: number) => { |
| 87 | + const page = device === 1 ? alicePage : bobPage; |
| 88 | + if (device === 1) await clearQueue(page); |
| 89 | + await joinSession(page, name); |
| 90 | + } |
| 91 | +); |
| 92 | + |
| 93 | +Given("the TV display is open", async ({ tvPage }) => { |
| 94 | + await tvPage.goto(`${BASE_URL}/tv`); |
| 95 | + await tvPage.waitForSelector('[data-testid="tv-interface"]', { |
| 96 | + timeout: 15000, |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +Given("a song is currently playing", async ({ alicePage }) => { |
| 101 | + await addSongToQueue(alicePage); |
| 102 | + await alicePage.waitForSelector('[data-testid="reactions-panel"]', { |
| 103 | + timeout: 30000, |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +Given("no songs are in the queue", async ({ alicePage }) => { |
| 108 | + await clearQueue(alicePage); |
| 109 | +}); |
| 110 | + |
| 111 | +async function openReactionsFab(page: Page): Promise<void> { |
| 112 | + const fab = page |
| 113 | + .getByTestId("reactions-panel") |
| 114 | + .locator("button[aria-label='Open reactions']"); |
| 115 | + await fab.click(); |
| 116 | +} |
| 117 | + |
| 118 | +When( |
| 119 | + "Alice sends a {string} reaction", |
| 120 | + async ({ alicePage }, emoji: string) => { |
| 121 | + await openReactionsFab(alicePage); |
| 122 | + const reactionButton = alicePage.getByTestId(`reaction-${emoji}`); |
| 123 | + await reactionButton.waitFor({ state: "visible", timeout: 10000 }); |
| 124 | + await reactionButton.click(); |
| 125 | + } |
| 126 | +); |
| 127 | + |
| 128 | +When("Bob sends a {string} reaction", async ({ bobPage }, emoji: string) => { |
| 129 | + await openReactionsFab(bobPage); |
| 130 | + const reactionButton = bobPage.getByTestId(`reaction-${emoji}`); |
| 131 | + await reactionButton.waitFor({ state: "visible", timeout: 10000 }); |
| 132 | + await reactionButton.click(); |
| 133 | +}); |
| 134 | + |
| 135 | +Then( |
| 136 | + "the TV display shows the {string} reaction floating across the screen", |
| 137 | + async ({ tvPage }, emoji: string) => { |
| 138 | + const reaction = tvPage.getByTestId("floating-reaction").filter({ |
| 139 | + hasText: emoji, |
| 140 | + }); |
| 141 | + await reaction.waitFor({ state: "visible", timeout: 10000 }); |
| 142 | + } |
| 143 | +); |
| 144 | + |
| 145 | +Then("the TV display shows both reactions", async ({ tvPage }) => { |
| 146 | + const reactions = tvPage.getByTestId("floating-reaction"); |
| 147 | + await expect(reactions).toHaveCount(2, { timeout: 10000 }); |
| 148 | +}); |
| 149 | + |
| 150 | +Then("Alice does not see the reactions panel", async ({ alicePage }) => { |
| 151 | + const panel = alicePage.getByTestId("reactions-panel"); |
| 152 | + await expect(panel).toHaveCount(0); |
| 153 | +}); |
| 154 | + |
| 155 | +Then( |
| 156 | + "Alice sees the reactions panel with emoji options", |
| 157 | + async ({ alicePage }) => { |
| 158 | + const panel = alicePage.getByTestId("reactions-panel"); |
| 159 | + await panel.waitFor({ state: "visible", timeout: 10000 }); |
| 160 | + } |
| 161 | +); |
| 162 | + |
| 163 | +Then( |
| 164 | + "the available reactions include {string}, {string}, {string}, {string}, {string}, {string}", |
| 165 | + async ( |
| 166 | + { alicePage }, |
| 167 | + e1: string, |
| 168 | + e2: string, |
| 169 | + e3: string, |
| 170 | + e4: string, |
| 171 | + e5: string, |
| 172 | + e6: string |
| 173 | + ) => { |
| 174 | + await openReactionsFab(alicePage); |
| 175 | + for (const emoji of [e1, e2, e3, e4, e5, e6]) { |
| 176 | + const button = alicePage.getByTestId(`reaction-${emoji}`); |
| 177 | + await expect(button).toBeVisible(); |
| 178 | + } |
| 179 | + } |
| 180 | +); |
0 commit comments