-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile-playtest.ts
More file actions
272 lines (236 loc) · 8.88 KB
/
Copy pathmobile-playtest.ts
File metadata and controls
272 lines (236 loc) · 8.88 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env tsx
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2025-2026 Matthew Kissinger
/**
* Mobile Playtest Harness
*
* Automated playtesting on a physical Android device connected via USB ADB.
* Uses CDP multi-touch injection for trusted PointerEvents + ADB for
* orientation control and device screenshots.
*
* Prerequisites:
* - Phone connected via USB with ADB debugging authorized
* - Chrome open on the phone
* - Dev server accessible from phone (--host flag)
*
* Usage:
* npm run playtest:mobile # full matrix
* npm run playtest:mobile -- --landscape-only
* npm run playtest:mobile -- --flow-only
* npm run playtest:mobile -- --mode open_frontier
*/
import { execSync, spawn, type ChildProcess } from 'child_process';
import { join } from 'path';
import { ADBController } from './mobile-playtest/adb-controller';
import { CDPBridge } from './mobile-playtest/cdp-bridge';
import { TouchInjector } from './mobile-playtest/touch-injector';
import { ScreenNavigator } from './mobile-playtest/screen-navigator';
import { GameplayScenarios, type ScenarioResult } from './mobile-playtest/gameplay-scenarios';
import {
createResultsDir,
writeConsoleLog,
writeErrorLog,
writeReport,
type PlaytestReport,
} from './mobile-playtest/report-writer';
// ---- Config ----
const CDP_PORT = 9222;
const DEV_PORT = 9200;
const PC_IP = '192.168.1.100';
const DEFAULT_MODE = 'zone_control';
type ViewportConfig = {
id: string;
label: string;
orientation: 'portrait' | 'landscape';
fullscreen: boolean;
};
const FULL_MATRIX: ViewportConfig[] = [
{ id: 'portrait', label: 'Portrait (no fullscreen)', orientation: 'portrait', fullscreen: false },
{ id: 'portrait-fs', label: 'Portrait (fullscreen)', orientation: 'portrait', fullscreen: true },
{ id: 'landscape', label: 'Landscape (no fullscreen)', orientation: 'landscape', fullscreen: false },
{ id: 'landscape-fs', label: 'Landscape (fullscreen)', orientation: 'landscape', fullscreen: true },
];
const LANDSCAPE_ONLY: ViewportConfig[] = [
{ id: 'landscape-fs', label: 'Landscape (fullscreen)', orientation: 'landscape', fullscreen: true },
];
// ---- CLI Args ----
const args = process.argv.slice(2);
const landscapeOnly = args.includes('--landscape-only');
const flowOnly = args.includes('--flow-only');
const modeArg = args.find((_, i, a) => a[i - 1] === '--mode');
const gameMode = modeArg || DEFAULT_MODE;
const configs = landscapeOnly ? LANDSCAPE_ONLY : FULL_MATRIX;
// ---- Dev Server ----
async function startDevServer(): Promise<ChildProcess> {
console.log(`Starting dev server on port ${DEV_PORT}...`);
const child = spawn('npm', ['run', 'dev', '--', '--port', String(DEV_PORT), '--host'], {
cwd: process.cwd(),
stdio: 'pipe',
shell: true,
});
await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Dev server start timeout')), 30_000);
const onData = (chunk: Buffer) => {
const text = chunk.toString();
if (text.includes('Local:') || text.includes('localhost')) {
clearTimeout(timeout);
child.stdout?.off('data', onData);
resolve();
}
};
child.stdout?.on('data', onData);
child.stderr?.on('data', (chunk: Buffer) => {
const text = chunk.toString();
if (text.includes('error')) console.error(' dev-server stderr:', text.trim());
});
child.on('error', (err) => { clearTimeout(timeout); reject(err); });
child.on('exit', (code) => {
if (code) { clearTimeout(timeout); reject(new Error(`Dev server exited with code ${code}`)); }
});
});
console.log(`Dev server ready at http://${PC_IP}:${DEV_PORT}`);
return child;
}
// ---- Main ----
async function main(): Promise<void> {
const startTime = Date.now();
const resultsDir = createResultsDir();
console.log(`Results: ${resultsDir}`);
// 1. Setup ADB
const adb = new ADBController();
adb.keepAwake();
adb.wake();
// 2. Forward CDP
adb.launchChrome();
await sleep(1000);
adb.forwardCDP(CDP_PORT);
// 3. Start dev server
let devServer: ChildProcess | null = null;
try {
devServer = await startDevServer();
} catch (e) {
console.error('Failed to start dev server:', e);
process.exit(1);
}
const reports: PlaytestReport[] = [];
try {
for (const config of configs) {
console.log(`\n=== ${config.label} ===`);
const configStart = Date.now();
const screenshotDir = join(resultsDir, 'screenshots');
let stepNum = 0;
// Set orientation
if (config.orientation === 'portrait') {
adb.setPortrait();
} else {
adb.setLandscape();
}
await sleep(1000);
// Connect CDP
const bridge = new CDPBridge();
await bridge.connect(CDP_PORT);
const touch = new TouchInjector(bridge.cdp);
const nav = new ScreenNavigator(bridge, touch, adb);
const scenarios = new GameplayScenarios(bridge, touch, nav, adb);
// Screenshot helper: captures both CDP and ADB screenshots
const snap = async (label: string): Promise<void> => {
stepNum++;
const prefix = String(stepNum).padStart(2, '0');
const baseName = `${config.id}-${prefix}-${label}`;
try {
await bridge.screenshot(join(screenshotDir, `${baseName}-cdp.png`));
} catch (e) {
console.warn(` CDP screenshot failed: ${e}`);
}
try {
adb.screencap(join(screenshotDir, `${baseName}-adb.png`));
} catch (e) {
console.warn(` ADB screencap failed: ${e}`);
}
};
// Navigate to game (perf=1 exposes __engine/__renderer globals for state queries)
await bridge.navigate(`http://${PC_IP}:${DEV_PORT}?perf=1`);
await sleep(2000);
// Run screen flow
let flowPassed = true;
try {
await nav.runFullFlow(gameMode, snap);
if (config.fullscreen) {
await nav.requestFullscreen();
await sleep(1000);
await snap('fullscreen-active');
}
} catch (e) {
console.error(` Screen flow failed: ${e}`);
flowPassed = false;
await snap('flow-error');
}
// Run gameplay scenarios (only if flow succeeded and not flow-only mode)
let scenarioResults: ScenarioResult[] = [];
if (flowPassed && !flowOnly) {
console.log(' Running gameplay scenarios...');
try {
scenarioResults = await scenarios.runAll(snap);
for (const r of scenarioResults) {
console.log(` ${r.passed ? 'PASS' : 'FAIL'}: ${r.name} - ${r.details.slice(0, 60)}`);
}
} catch (e) {
console.error(` Gameplay scenarios crashed: ${e}`);
await snap('scenarios-error');
}
}
const configDuration = ((Date.now() - configStart) / 1000).toFixed(1);
reports.push({
timestamp: new Date().toISOString(),
device: 'Samsung Galaxy S24 Ultra (SM-S926U)',
config: config.label,
duration: `${configDuration}s`,
screenFlowPassed: flowPassed,
scenarioResults,
consoleErrorCount: bridge.getErrors().length,
pageErrorCount: bridge.getErrors().filter(e => e.type === 'pageerror').length,
});
// Write per-config console logs
writeConsoleLog(resultsDir, bridge.getConsoleLog());
writeErrorLog(resultsDir, bridge.getErrors());
await bridge.disconnect();
}
} finally {
// Cleanup
adb.resetOrientation();
if (devServer) {
devServer.kill();
// Force-kill on Windows
try {
execSync(`powershell -Command "Get-Process -Id ${devServer.pid} -ErrorAction SilentlyContinue | Stop-Process -Force"`, { stdio: 'ignore' });
} catch { /* ok */ }
}
}
// Write final report
writeReport(resultsDir, reports);
const totalDuration = ((Date.now() - startTime) / 1000).toFixed(1);
console.log(`\n=== Playtest complete (${totalDuration}s) ===`);
console.log(`Results: ${resultsDir}`);
// Summary
const totalScenarios = reports.reduce((sum, r) => sum + r.scenarioResults.length, 0);
const passedScenarios = reports.reduce((sum, r) => sum + r.scenarioResults.filter(s => s.passed).length, 0);
const totalErrors = reports.reduce((sum, r) => sum + r.consoleErrorCount, 0);
console.log(`Flow: ${reports.filter(r => r.screenFlowPassed).length}/${reports.length} configs passed`);
if (totalScenarios > 0) {
console.log(`Scenarios: ${passedScenarios}/${totalScenarios} passed`);
}
if (totalErrors > 0) {
console.log(`Console errors: ${totalErrors} (see errors.log)`);
}
// Exit with failure if anything failed
const allPassed = reports.every(r => r.screenFlowPassed) &&
reports.every(r => r.scenarioResults.every(s => s.passed));
process.exit(allPassed ? 0 : 1);
}
function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
main().catch(err => {
console.error('Playtest harness crashed:', err);
process.exit(1);
});