tools/client/test_pending_stale_equipment.mjs (4728B)
1 import assert from 'node:assert/strict'; 2 import fs from 'node:fs/promises'; 3 import {createRequire} from 'node:module'; 4 import {GameTest} from './game_test.mjs'; 5 6 const {chromium} = createRequire(import.meta.url)('playwright'); 7 const [url, output] = process.argv.slice(2); 8 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', 9 args: ['--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage', 10 '--disable-background-timer-throttling', '--disable-renderer-backgrounding']}); 11 let game; 12 const evidence = {}; 13 try { 14 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 15 await context.addCookies([{name: 'ao_client', value: 'pending-stale-equipment', url}]); 16 const page = await context.newPage(); 17 game = await GameTest.attach(page); 18 const server = async () => (await page.request.get(url + '/test-state')).json(); 19 const cell = slot => game.state.grids.flatMap(grid => grid.cells).find(cell => 20 cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy); 21 const equip = async slot => { 22 const source = cell(slot); 23 assert(source, 'equipment source must be usable'); 24 const sequence = game.sequence; 25 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 26 await game.click(867, 206); 27 await game.wait('actual equipment request', () => game.events.some(event => event.sequence > sequence && 28 event.direction === 'framesent' && event.opcode === '47537a24' && 29 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === slot)); 30 return sequence; 31 }; 32 const idle = () => game.wait('native equip animation ends', state => 33 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 34 await page.goto(url); 35 await game.ready(); 36 await game.key('i'); 37 await page.keyboard.press('Control+1'); 38 await game.wait('both fixture weapons visible', () => cell(48) && cell(49)); 39 assert((await page.request.post(url + '/test-stale-view')).ok()); 40 await equip(48); 41 evidence.pending = await server(); 42 assert(evidence.pending.pending); 43 await idle(); 44 45 // The second native request addresses the old, now-empty carried row. 46 const sequence = await equip(49); 47 evidence.refused = await server(); 48 assert.deepEqual(evidence.refused, evidence.pending); 49 await game.save(output + '/requested'); 50 await game.wait('stale source cancelled and cleared during pending equipment', () => 51 !cell(49) && game.events.filter(event => event.sequence > sequence && 52 event.direction === 'framereceived' && event.opcode === '5e477770').length >= 2); 53 assert.deepEqual(game.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' && 54 event.opcode === '5e477770').map(event => { 55 const packet = Buffer.from(event.hex, 'hex'); 56 return [packet.readUInt32BE(29), packet.readUInt32BE(33), packet.readUInt32BE(37)]; 57 }), [[146, 104, 49], [112, 104, 49]]); 58 assert.deepEqual(game.state.dragged, [0, 0]); 59 await game.save(output + '/corrected'); 60 assert((await page.request.post(url + '/test-complete')).ok()); 61 await game.wait('original pending weapon completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); 62 evidence.completed = await server(); 63 assert.equal(evidence.completed.pending, null); 64 65 // Page recovery exposes the committed location; that exact weapon must equip. 66 await page.reload(); 67 await game.ready(); 68 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 69 await game.snapshot(); 70 if (!cell(50)) await game.key('i'); 71 await page.keyboard.press('Control+1'); 72 await game.wait('both exact retained weapons replay', () => cell(50) && game.local().inventory.some(item => item.id === 1 && item.slot === 6)); 73 await equip(50); 74 assert((await page.request.post(url + '/test-complete')).ok()); 75 await game.wait('recovered weapon replaces original', state => game.local(state).inventory.some(item => item.id === 2 && item.slot === 6)); 76 evidence.after = await server(); 77 assert.deepEqual(evidence.after.inventory.Items.map(item => [item.ID, item.Slot, item.Count]), [[1, 66, 1], [2, 6, 1]]); 78 await game.save(output + '/retained'); 79 assert.deepEqual(game.errors, []); 80 console.log('PASS: pending equip survives stale-source correction and both weapons remain usable after recovery'); 81 } finally { 82 try { 83 await fs.mkdir(output, {recursive: true}); 84 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 85 if (game) await game.save(output + '/final'); 86 } finally { await browser.close(); } 87 }