tools/client/game_smoke.js (3405B)
1 // Execute this source directly in the Computer Use Node REPL. 2 globalThis.gameSmoke = await (async () => { 3 // verify_game.py owns the bounded client lifetime. 4 const fs = await import('node:fs/promises'); 5 const path = await import('node:path'); 6 const {setTimeout} = await import('node:timers/promises'); 7 8 const directory = (await import('node:url')).pathToFileURL(path.resolve('local/client/game-14.9.1') + path.sep); 9 let report; 10 let window; 11 let events; 12 13 async function start() { 14 report = JSON.parse(await fs.readFile(new URL('verification.json', directory), 'utf8')); 15 if (report.client_exited) throw new Error('Start verify_game.py first.'); 16 await setTimeout(Math.max(0, (report.started_at + 35) * 1000 - Date.now())); 17 const windows = (await globalThis.sky.list_windows()).filter(candidate => 18 candidate.app.toLowerCase() === 'process:' + report.executable.toLowerCase()); 19 if (windows.length !== 1) throw new Error(`Expected one game window, found ${windows.length}.`); 20 window = await globalThis.sky.get_window({id: windows[0].id, app: windows[0].app}); 21 await globalThis.sky.activate_window({window}); 22 window = (await globalThis.sky.get_window_state({ 23 window, include_screenshot: false, include_text: true})).window; 24 events = []; 25 return report; 26 } 27 28 async function key(key) { 29 await globalThis.sky.press_key({window, key}); 30 window = (await globalThis.sky.get_window_state({ 31 window, include_screenshot: false, include_text: true})).window; 32 events.push({key, elapsed_seconds: Date.now() / 1000 - report.started_at}); 33 await save(); 34 } 35 36 async function save() { 37 await fs.writeFile(new URL(`smoke-${report.pid}.json`, directory), 38 JSON.stringify({run: report, events}, null, 2)); 39 } 40 41 async function capture(label) { 42 const screenshots = path.join(report.client, 'Screenshots'); 43 const before = new Set(await fs.readdir(screenshots)); 44 await key('F12'); 45 let files = []; 46 for (let attempt = 0; attempt < 10 && files.length === 0; attempt++) { 47 await setTimeout(500); 48 files = (await fs.readdir(screenshots)).filter(name => !before.has(name) && name.endsWith('.png')); 49 } 50 if (files.length !== 1) throw new Error(`Expected one native screenshot, found ${files.length}.`); 51 await setTimeout(500); 52 const filename = `smoke-${report.pid}-${label}.png`; 53 await fs.copyFile(path.join(screenshots, files[0]), new URL(filename, directory)); 54 events.push({capture: filename, elapsed_seconds: Date.now() / 1000 - report.started_at}); 55 await save(); 56 return filename; 57 } 58 59 // Keep FPS sampling separate from movement and menu rendering. 60 async function sampleFps() { 61 const files = []; 62 for (let sample = 1; sample <= 5; sample++) { 63 await setTimeout(5000); 64 files.push(await capture(`fps-${sample}`)); 65 } 66 return files; 67 } 68 69 async function smoke() { 70 const files = []; 71 for (const [name, shortcut] of [['inventory', 'i'], ['map', 'p']]) { 72 await key(shortcut); 73 await setTimeout(1000); 74 files.push(await capture(`${name}-open`)); 75 await key(shortcut); 76 await setTimeout(1000); 77 files.push(await capture(`${name}-closed`)); 78 } 79 await key('KP_0'); 80 await setTimeout(3000); 81 await key('w'); 82 files.push(await capture('moved')); 83 return files; 84 } 85 86 return {start, key, capture, sampleFps, smoke}; 87 })();