tools/client/test_pending_stale_drop.mjs (5894B)
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-drop', 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 if (!game.state.camera.firstPerson) await game.key('F8'); 40 const point = await game.point(500, 150); 41 await page.mouse.move(point.x, point.y); 42 const press = game.sequence; 43 await page.mouse.down({button: 'right'}); 44 try { 45 await game.waitInput(press, 1025); 46 await page.mouse.move(point.x, point.y + 400, {steps: 8}); 47 await game.wait('ground view below the character', state => state.camera.angles[1] >= 0.55); 48 } finally { 49 const release = game.sequence; 50 await page.mouse.up({button: 'right'}); 51 await game.waitInput(release, 1026); 52 } 53 assert((await page.request.post(url + '/test-stale-view')).ok()); 54 await equip(48); 55 evidence.pending = await server(); 56 assert(evidence.pending.pending); 57 await idle(); 58 59 // Real ground-drop input addresses the old, now-empty carried row. 60 const source = cell(49); 61 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 62 await game.wait('stale source picked', state => state.dragged[0] === 104 && state.dragged[1] === 49); 63 const sequence = game.sequence; 64 await game.click(500, 580); 65 await game.wait('actual stale ground-drop request', () => game.events.some(event => event.sequence > sequence && 66 event.direction === 'framesent' && event.opcode === '3a243f41' && 67 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === 49)); 68 evidence.refused = await server(); 69 assert.deepEqual(evidence.refused, evidence.pending); 70 await game.save(output + '/requested'); 71 await game.wait('stale source cancelled and cleared during pending equipment', () => 72 !cell(49) && game.events.filter(event => event.sequence > sequence && 73 event.direction === 'framereceived' && event.opcode === '5e477770').length >= 2); 74 assert.deepEqual(game.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' && 75 event.opcode === '5e477770').map(event => { 76 const packet = Buffer.from(event.hex, 'hex'); 77 return [packet.readUInt32BE(29), packet.readUInt32BE(33), packet.readUInt32BE(37)]; 78 }), [[146, 104, 49], [112, 104, 49]]); 79 assert.deepEqual(game.state.dragged, [0, 0]); 80 await game.save(output + '/corrected'); 81 assert((await page.request.post(url + '/test-complete')).ok()); 82 await game.wait('original pending weapon completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); 83 evidence.completed = await server(); 84 assert.equal(evidence.completed.pending, null); 85 86 // Page recovery exposes the committed location; that exact weapon must equip. 87 await page.reload(); 88 await game.ready(); 89 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 90 await game.snapshot(); 91 if (!cell(50)) await game.key('i'); 92 await page.keyboard.press('Control+1'); 93 await game.wait('both exact retained weapons replay', () => cell(50) && game.local().inventory.some(item => item.id === 1 && item.slot === 6)); 94 await equip(50); 95 assert((await page.request.post(url + '/test-complete')).ok()); 96 await game.wait('recovered weapon replaces original', state => game.local(state).inventory.some(item => item.id === 2 && item.slot === 6)); 97 evidence.after = await server(); 98 assert.deepEqual(evidence.after.inventory.Items.map(item => [item.ID, item.Slot, item.Count]), [[1, 66, 1], [2, 6, 1]]); 99 await game.save(output + '/retained'); 100 assert.deepEqual(game.errors, []); 101 assert.equal(game.dropped, 0); 102 console.log('PASS: pending equip survives stale ground-drop correction and both weapons remain usable after recovery'); 103 } finally { 104 try { 105 await fs.mkdir(output, {recursive: true}); 106 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 107 if (game) await game.save(output + '/final'); 108 } finally { await browser.close(); } 109 }