summaryrefslogtreecommitdiff
path: root/e2e/tests/resilience.spec.ts
blob: 27e7a18095710068c43b488df977525021ef0622 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
 * E2E: resilience (PLAN.md Phase 7).
 *
 * * a tab that drops out (offline) resyncs on reconnect — no drift
 * * a SIGKILL'd server restarts mid-game: state AND seats survive, secret
 *   initiative stays masked for non-owners, the game continues
 * * a throttled (slow) tab converges on the same state as the fast tabs
 * * a double-tapped draw executes exactly once; only the sender gets the
 *   error toast
 *
 * Note on the initiative cell markup: during chooseInitiative the OWNER
 * renders an `<input>` (no `.init` div); every other connection renders the
 * `.init` div — `??` while the server masks the value, the number after the
 * draw.
 */
import type { Page } from '@playwright/test';
import { test, expect } from '../helpers/table.js';
import { startServer } from '../helpers/server.js';
import { Tab } from '../helpers/tab.js';

/** ~1 s one-way latency, moderate throughput (CDP network emulation). */
async function throttle(page: Page) {
	const cdp = await page.context().newCDPSession(page);
	await cdp.send('Network.emulateNetworkConditions', {
		offline: false,
		latency: 1000,
		downloadThroughput: 500_000,
		uploadThroughput: 500_000
	});
}

test.describe('Resilience', () => {
	test('a dropped tab resyncs when it comes back (no drift)', async ({
		table
	}) => {
		const p1 = (await table.newTab()).tab;
		const p2 = (await table.newTab()).tab;
		const specOpen = await table.newTab();
		const spec = specOpen.tab;
		const specContext = specOpen.context;
		await p1.open(table.server.url);
		await p1.addCharacter('Hatchet');
		await p1.selectScenario('#1 Roadside Ambush');
		await p2.open(table.server.url);
		await p2.addCharacter('Voidwarden');
		await spec.open(table.server.url);
		await spec.row('Hatchet').waitFor({ timeout: 15_000 });
		await spec.row('Voidwarden').waitFor({ timeout: 15_000 });

		// The spectator's network drops and its tab is lost. Closing the page
		// reliably severs the WebSocket (CDP offline emulation alone does not
		// tear down an already-established socket).
		await specContext.setOffline(true);
		await spec.closePage();

		// The game moves on without it.
		await p1.setInitiative('Hatchet', 4);
		await p2.setInitiative('Voidwarden', 2);
		await p1.clickDraw();
		await p1.waitForDrawn();
		await p2.waitForDrawn();

		// Back online: a fresh page in the same session (cookie) re-sends
		// `hello`, and the full `welcome` overwrites the stale snapshot.
		await specContext.setOffline(false);
		const spec2 = new Tab(await specContext.newPage());
		await spec2.open(table.server.url);
		await spec2.waitForDrawn();
		await expect(spec2.page.locator('.banner[role="status"]')).toBeHidden();
		await expect(spec2.initDisplay('Hatchet')).toHaveText('4');
		await expect(spec2.initDisplay('Voidwarden')).toHaveText('2');
		await expect(spec2.roundLabel()).toHaveText(/Round 1\//);
	});

	test('a SIGKILL’d server restarts mid-game: state and seats survive', async ({
		table
	}) => {
		const p1 = (await table.newTab()).tab;
		const p2 = (await table.newTab()).tab;
		const spec = (await table.newTab()).tab;
		await p1.open(table.server.url);
		await p1.addCharacter('Hatchet');
		await p1.selectScenario('#1 Roadside Ambush');
		await p2.open(table.server.url);
		await p2.addCharacter('Voidwarden');
		await spec.open(table.server.url);
		await spec.row('Hatchet').waitFor({ timeout: 15_000 });

		// Mid chooseInitiative: p1 has entered 4 (secret), p2 has not.
		await p1.setInitiative('Hatchet', 4);
		await expect(p2.initDisplay('Hatchet')).toHaveText('??');
		await expect(spec.initDisplay('Hatchet')).toHaveText('??');

		// Crash the server (SIGKILL — no graceful shutdown).
		await table.server.killAndWait();

		// Every tab notices the drop and starts reconnecting.
		await expect(p1.page.locator('.banner[role="status"]')).toBeVisible();
		await expect(spec.page.locator('.banner[role="status"]')).toBeVisible();

		// Restart the same server: same port, same saves dir.
		const restarted = await startServer({
			port: table.server.port,
			savesDir: table.server.savesDir
		});

		try {
			// All tabs auto-reconnect and resync.
			await p1.page
				.locator('.banner[role="status"]')
				.waitFor({ state: 'detached', timeout: 30_000 });
			await spec.page
				.locator('.banner[role="status"]')
				.waitFor({ state: 'detached', timeout: 30_000 });

			// State survived the crash…
			await expect(p1.roundLabel()).toHaveText(/Round 1\//);
			await expect(spec.roundLabel()).toHaveText(/Round 1\//);

			// …including the secret entry: p1 (the seat owner) still has the
			// editable input with the value; p2 and the spectator see the mask.
			// The seats broadcast lands a beat after the state, so auto-wait.
			await expect(
				p1.row('Hatchet').locator('input.init__input')
			).toHaveCount(1);
			await expect(p1.row('Hatchet').locator('input.init__input')).toHaveValue(
				'4'
			);
			await expect(p2.initDisplay('Hatchet')).toHaveText('??');
			await expect(spec.initDisplay('Hatchet')).toHaveText('??');

			// Seats survived too: p1 is still a player (can draw) and the
			// character is not claimable by p2.
			await expect(p1.page.locator('button.drawbtn')).toBeEnabled();
			await expect(p2.claimChip('Hatchet')).toHaveCount(0);

			// And the game continues exactly where it left off.
			await p2.setInitiative('Voidwarden', 2);
			await p2.clickDraw();
			await p1.waitForDrawn();
			await p2.waitForDrawn();
			await spec.waitForDrawn();
			await expect(p1.initDisplay('Hatchet')).toHaveText('4');
			await expect(spec.initDisplay('Voidwarden')).toHaveText('2');
		} finally {
			await restarted.kill();
		}
	});

	test('a throttled tab converges on the same state as the fast tabs', async ({
		table
	}) => {
		const p1 = (await table.newTab()).tab;
		const p2 = (await table.newTab()).tab;
		const specOpen = await table.newTab();
		const spec = specOpen.tab;

		// Slow the spectator down ~1 s one-way BEFORE the page (and its WS
		// connection) is established.
		await throttle(specOpen.page);
		await spec.open(table.server.url);

		// The fast tabs run a full round while the spectator lags behind.
		await p1.open(table.server.url);
		await p1.addCharacter('Hatchet');
		await p1.selectScenario('#1 Roadside Ambush');
		await p2.open(table.server.url);
		await p2.addCharacter('Voidwarden');
		await p1.setInitiative('Hatchet', 4);
		await p2.setInitiative('Voidwarden', 2);
		await p1.clickDraw();
		await p1.waitForDrawn();
		await p2.waitForDrawn();

		// The full-snapshot protocol means the slow tab lands on the final
		// state — no drift, no gaps.
		await spec.row('Hatchet').waitFor({ timeout: 30_000 });
		await spec.row('Voidwarden').waitFor({ timeout: 30_000 });
		await spec.waitForDrawn();
		await expect(spec.page.locator('.banner[role="status"]')).toBeHidden();
		await expect(spec.initDisplay('Hatchet')).toHaveText('4');
		await expect(spec.initDisplay('Voidwarden')).toHaveText('2');
		await expect(spec.roundLabel()).toHaveText(/Round 1\//);
	});

	test('a double-tapped draw runs once; only the sender sees the error', async ({
		table
	}) => {
		const p1Open = await table.newTab();
		const p1 = p1Open.tab;
		const p2 = (await table.newTab()).tab;
		const spec = (await table.newTab()).tab;
		await p1.open(table.server.url);
		await p1.addCharacter('Hatchet');
		await p1.selectScenario('#1 Roadside Ambush');
		await p2.open(table.server.url);
		await p2.addCharacter('Voidwarden');
		await spec.open(table.server.url);
		await spec.row('Hatchet').waitFor({ timeout: 15_000 });

		await p1.setInitiative('Hatchet', 4);
		await p2.setInitiative('Voidwarden', 2);

		// Throttle p1 so the first tap’s update cannot re-render the button
		// before the second tap lands — both taps send `draw`.
		await throttle(p1Open.page);
		await p1.page.locator('button.drawbtn').click({ clickCount: 2 });

		// The table drew exactly once…
		await p1.waitForDrawn();
		await p2.waitForDrawn();
		await spec.waitForDrawn();
		await expect(p2.roundLabel()).toHaveText(/Round 1\//);
		await expect(spec.roundLabel()).toHaveText(/Round 1\//);

		// …and the losing tap is reported to the sender only. (The other tabs
		// may still show the broadcast round-start scenario message — a
		// legitimate toast — but never the sender-only error.)
		await expect(p1.toast()).toContainText('already been drawn');
		await expect(
			p2.page.locator('.toast[role="status"]', {
				hasText: 'already been drawn'
			})
		).toHaveCount(0);
		await expect(
			spec.page.locator('.toast[role="status"]', {
				hasText: 'already been drawn'
			})
		).toHaveCount(0);
	});
});