summaryrefslogtreecommitdiff
path: root/e2e/helpers/tab.ts
blob: 616f7838e30b075d2c240b51496e4c7154473e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * 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<void> {
		await this.page.goto(url, { waitUntil: 'domcontentloaded' });
		await this.waitForConnected();
	}

	/** The "Connecting…" banner is visible until the first `welcome` lands. */
	async waitForConnected(): Promise<void> {
		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<void> {
		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<void> {
		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<void> {
		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<void> {
		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<void> {
		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<boolean> {
		return (await this.row(rowName).locator('input.init__input').count()) > 0;
	}

	// --- draw / turns ---------------------------------------------------------

	/** Taps Draw (chooseInitiative) or Next Round (playTurns). */
	async clickDraw(): Promise<void> {
		await this.page.locator('button.drawbtn').click();
	}

	/** Waits until the round has been drawn (the button reads Next Round). */
	async waitForDrawn(): Promise<void> {
		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<void> {
		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<void> {
		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<boolean> {
		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<Locator> {
		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<void> {
		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<void> {
		await this.page.close();
	}
}