/** * One browser tab on the table (PLAN.md Phase 7): drives the real UI — * menus, character rows, the initiative widget, the draw button — and * asserts on the DOM each connection actually receives (the per-connection * masked state, PLAN.md section 3.4). */ import type { Locator, Page } from '@playwright/test'; export class Tab { constructor(public readonly page: Page) {} /** Loads the game page and waits for the WS handshake to complete. */ async open(url: string): Promise { await this.page.goto(url, { waitUntil: 'domcontentloaded' }); await this.waitForConnected(); } /** The "Connecting…" banner is visible until the first `welcome` lands. */ async waitForConnected(): Promise { await this.page.waitForSelector('.topbar'); await this.page .locator('.banner[role="status"]') .waitFor({ state: 'detached', timeout: 30_000 }); } // --- main list ----------------------------------------------------------- /** The character row whose display name is [name]. */ row(name: string): Locator { return this.page .locator('.row--character') .filter({ has: this.page.locator('.row__name', { hasText: name }) }); } /** The monster row whose name is [name]. */ monsterRow(name: string): Locator { return this.page .locator('.row--monster') .filter({ has: this.page.locator('.row__name', { hasText: name }) }); } // --- menus --------------------------------------------------------------- /** Opens the main drawer and picks the [label] entry (a menu dialog). */ async openMenu(label: string): Promise { await this.page.locator('.topbar__menu').click(); await this.page .locator('nav[aria-label="Main menu"]') .getByRole('button', { name: label, exact: true }) .click(); await this.page.locator('.dialog__overlay').first().waitFor(); } /** The open dialog titled [title]. */ dialog(title: string): Locator { return this.page .locator('.dialog__overlay') .filter({ has: this.page.locator('.dialog__title', { hasText: title }) }); } /** * Adds a character via the Add Character menu (level defaults to 1). * Resolves once the row appears — i.e. the server's update arrived. */ async addCharacter(name: string, level = 1): Promise { await this.openMenu('Add Character'); const dlg = this.dialog('Add Character'); const up = dlg.locator('.stepbtn').nth(1); for (let i = 1; i < level; i++) await up.click(); await dlg.locator('.menulist__item').filter({ hasText: name }).click(); await this.row(name).waitFor({ timeout: 15_000 }); } /** Picks [scenarioName] (optionally switching to [campaign] first). */ async selectScenario(scenarioName: string, campaign?: string): Promise { await this.openMenu('Set Scenario'); const dlg = this.dialog('Set Scenario'); if (campaign) { await dlg.locator('.campaignbtns button').filter({ hasText: campaign }).click(); } await dlg .locator('.menulist__item') .filter({ hasText: scenarioName }) .first() .click(); await this.page.locator('.dialog__overlay').waitFor({ state: 'detached' }); } /** Adds a monster via the Add Monsters menu. */ async addMonster(name: string, level = 1): Promise { await this.openMenu('Add Monsters'); const dlg = this.dialog('Add Monsters'); const up = dlg.locator('.stepbtn').nth(1); for (let i = 1; i < level; i++) await up.click(); await dlg.locator('.menulist__item').filter({ hasText: name }).first().click(); await this.monsterRow(name).waitFor({ timeout: 15_000 }); } // --- initiative (the server-gated secret) -------------------------------- /** Enters [value] in this tab's initiative input for [rowName]. */ async setInitiative(rowName: string, value: number): Promise { const input = this.row(rowName).locator('input.init__input'); await input.waitFor({ timeout: 15_000 }); await input.fill(String(value)); } /** The initiative value shown on [rowName] in this tab (the `.init` cell). */ initDisplay(rowName: string): Locator { return this.row(rowName).locator('.init'); } /** True when [rowName] shows an editable initiative input on this tab. */ async hasInitiativeInput(rowName: string): Promise { return (await this.row(rowName).locator('input.init__input').count()) > 0; } // --- draw / turns --------------------------------------------------------- /** Taps Draw (chooseInitiative) or Next Round (playTurns). */ async clickDraw(): Promise { await this.page.locator('button.drawbtn').click(); } /** Waits until the round has been drawn (the button reads Next Round). */ async waitForDrawn(): Promise { await this.page .getByRole('button', { name: /Next Round/ }) .waitFor({ timeout: 15_000 }); } /** Waits until a new round is being chosen (the button reads Draw). */ async waitForRoundChosen(): Promise { await this.page.getByRole('button', { name: /Draw/ }).waitFor({ timeout: 15_000 }); } /** Taps the right-hand turn-done zone of [rowName] (playTurns). */ async turnDone(rowName: string): Promise { await this.row(rowName).locator('div[aria-label="Turn done"]').click(); } /** The round counter under the draw button (e.g. "Round 1/1"). */ roundLabel(): Locator { return this.page.locator('.drawbtn__round'); } /** The draw/next-round button's enabled state (players only). */ async drawEnabled(): Promise { return this.page.locator('button.drawbtn').isEnabled(); } // --- seats (claim / re-claim) -------------------------------------------- /** * Opens the figure's status (stat) menu by tapping the row's name, and * returns the open dialog (titled with the figure's display name). */ async openStatusMenu(rowName: string): Promise { await this.row(rowName).locator('.row__name').click(); const dlg = this.dialog(rowName); await dlg.waitFor({ timeout: 15_000 }); return dlg; } /** The "Claim" chip on [rowName] (visible while the character is unclaimed). */ claimChip(rowName: string): Locator { return this.row(rowName).locator('button.chip--claim'); } /** Clicks the Claim chip on [rowName]. */ async claim(rowName: string): Promise { await this.claimChip(rowName).click(); } // --- misc ----------------------------------------------------------------- /** The current toast text (server errors / scenario events), if any. */ toast(): Locator { return this.page.locator('.toast[role="status"]'); } /** Closes this tab's page (the server sees the connection drop). */ async closePage(): Promise { await this.page.close(); } }