tools/client/test_pending_equipment.mjs (9826B)
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-equipment', url}]); 16 const page = await context.newPage(); 17 game = await GameTest.attach(page); 18 await page.goto(url); 19 await game.ready(); 20 const server = async () => (await page.request.get(url + '/test-state')).json(); 21 await game.key('i'); 22 await page.keyboard.press('Control+1'); 23 const equip = async slot => { 24 const cell = game.state.grids.flatMap(grid => grid.cells).find(cell => 25 cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy); 26 assert(cell, 'equipment source is visible'); 27 const sequence = game.sequence; 28 await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 29 await game.click(867, 206); 30 await game.wait('actual equipment request', () => game.events.some(event => event.sequence > sequence && 31 event.direction === 'framesent' && event.opcode === '47537a24' && 32 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === slot)); 33 return sequence; 34 }; 35 await equip(48); 36 evidence.pending = await server(); 37 assert(evidence.pending.pending); 38 await game.wait('local animation finishes while server move remains pending', state => 39 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 40 41 // Refusing an unrelated worn drag must retain its usable icon and wear effects. 42 const armor = [5, 5, 1, 1, 5, 5, 1, 5]; 43 assert.deepEqual(game.local().effectiveArmor, armor); 44 await game.click(953, 53); 45 const worn = game.sequence; 46 await game.click(927, 148); 47 await game.wait('actual worn vest picked during pending equip', state => state.dragged[0] === 102 && state.dragged[1] === 21); 48 const empty = game.state.grids.find(grid => grid.totalCells === 30).cells[2]; 49 assert.equal(empty.identity[0], 0); 50 await game.click((empty.bounds[0] + empty.bounds[2]) / 2, (empty.bounds[1] + empty.bounds[3]) / 2); 51 await game.wait('actual worn removal refused during pending equip', () => game.events.some(event => event.sequence > worn && 52 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 102) && 53 game.events.some(event => event.sequence > worn && event.direction === 'framereceived' && ['47537a24', '5e477770'].includes(event.opcode))); 54 evidence.wornRefused = await server(); 55 assert.deepEqual(evidence.wornRefused, evidence.pending); 56 assert.deepEqual(game.events.filter(event => event.sequence > worn && event.direction === 'framereceived' && 57 event.opcode === '5e477770').map(event => Array.from({length: 5}, (_, index) => Buffer.from(event.hex, 'hex').readUInt32BE(29 + index * 4))), 58 [[146, 102, 21, 0, 0]]); 59 assert.deepEqual(game.local().effectiveArmor, armor); 60 await game.save(output + '/worn-refused'); 61 await game.wait('local refused removal action ended', state => !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 62 await game.click(927, 148); 63 await game.wait('corrected worn icon can actually be picked again', state => state.dragged[0] === 102 && state.dragged[1] === 21); 64 await game.click(927, 148); 65 await game.wait('corrected worn pickup returns to its original slot', state => state.dragged[0] === 0 && state.dragged[1] === 0); 66 assert.deepEqual(await server(), evidence.pending); 67 assert.deepEqual(game.local().effectiveArmor, armor); 68 await game.save(output + '/worn-usable'); 69 await game.click(867, 53); 70 const sequence = await equip(49); 71 evidence.refused = await server(); 72 assert.deepEqual(evidence.refused, evidence.pending); 73 await game.save(output + '/pending'); 74 await game.wait('second equipment source restored', state => state.grids.some(grid => grid.cells.some(cell => 75 cell.identity[0] === 104 && cell.identity[1] === 49 && !cell.dummy)) && 76 game.events.some(event => event.sequence > sequence && event.direction === 'framereceived' && 77 (event.opcode === '47537a24' || event.opcode === '5e477770'))); 78 assert.deepEqual(game.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' && 79 event.opcode === '47537a24').map(event => { 80 const packet = Buffer.from(event.hex, 'hex'); 81 return [packet.readUInt32BE(29), packet.readUInt32BE(33), packet.readUInt32BE(45)]; 82 }), [[104, 49, 48], [104, 48, 49]]); 83 assert.deepEqual(game.state.dragged, [0, 0]); 84 await game.save(output + '/refused'); 85 assert((await page.request.post(url + '/test-complete')).ok()); 86 await game.wait('original equipment completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); 87 await game.wait('local second animation has ended', state => 88 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 89 await equip(49); 90 evidence.retry = await server(); 91 assert(evidence.retry.pending); 92 assert.deepEqual(evidence.retry.inventory.Items, [ 93 {...evidence.pending.inventory.Items[0], Slot: 6}, evidence.pending.inventory.Items[1], evidence.pending.inventory.Items[2]]); 94 assert((await page.request.post(url + '/test-complete')).ok()); 95 await game.wait('retry replaces the original weapon', state => game.local(state).inventory.some(item => item.id === 2 && item.slot === 6)); 96 evidence.after = await server(); 97 assert.equal(evidence.after.pending, null); 98 assert.deepEqual(evidence.after.inventory.Items, [ 99 {...evidence.pending.inventory.Items[0], Slot: 65}, {...evidence.pending.inventory.Items[1], Slot: 6}, evidence.pending.inventory.Items[2]]); 100 assert.equal(game.events.filter(event => event.direction === 'framesent' && event.opcode === '47537a24' && 101 Buffer.from(event.hex, 'hex').readUInt32BE(29) !== 102).length, 3); 102 await game.save(output + '/retried'); 103 104 // Removing the occupied hand must not interrupt a pending replacement. 105 await game.wait('local retry action finishes', state => 106 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 107 await equip(49); 108 evidence.replacement = await server(); 109 assert.equal(evidence.replacement.pending.replacedID, 2); 110 await game.wait('local replacement animation finishes', state => 111 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 112 const hand = game.sequence; 113 await game.click(867, 206, 'right'); 114 await game.wait('occupied hand refusal acknowledged', () => game.events.some(event => 115 event.sequence > hand && event.direction === 'framereceived' && event.opcode === '5e477770')); 116 evidence.handRefused = await server(); 117 assert.deepEqual(evidence.handRefused, evidence.replacement); 118 assert.deepEqual(game.events.filter(event => event.sequence > hand && event.direction === 'framereceived' && 119 event.opcode === '5e477770').map(event => { 120 const packet = Buffer.from(event.hex, 'hex'); 121 return Array.from({length: 5}, (_, index) => packet.readUInt32BE(29 + index * 4)); 122 }), [[146, 101, 6, 0, 0]]); 123 assert.deepEqual(game.state.dragged, [0, 0]); 124 await game.save(output + '/occupied-hand-refused'); 125 assert((await page.request.post(url + '/test-complete')).ok()); 126 await game.wait('original replacement completes after hand refusal', state => 127 game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); 128 129 // Restore the retained layout through another ordinary native replacement. 130 await game.wait('local hand action finishes', state => 131 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 132 await equip(49); 133 assert((await page.request.post(url + '/test-complete')).ok()); 134 await game.wait('both instances remain usable', state => 135 game.local(state).inventory.some(item => item.id === 2 && item.slot === 6)); 136 assert.equal(game.events.filter(event => event.direction === 'framesent' && event.opcode === '47537a24' && 137 Buffer.from(event.hex, 'hex').readUInt32BE(29) !== 102).length, 6); 138 await game.save(output + '/occupied-hand-retained'); 139 await page.reload(); 140 await game.ready(); 141 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 142 await game.snapshot(); 143 if (!game.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))) await game.key('i'); 144 await game.wait('retained weapon visible after page replay', state => state.grids.some(grid => grid.cells.some(cell => 145 cell.identity[0] === 104 && cell.identity[1] === 49 && !cell.dummy))); 146 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]).sort((a, b) => a[0] - b[0]), 147 [[1, 49, 1, 1], [2, 6, 1, 1], [3, 21, 1, 1]]); 148 assert.deepEqual(game.local().effectiveArmor, armor); 149 assert.equal(game.errors.length, 0); 150 console.log('PASS: worn drag refusal restores a real second pickup; pending replacements retain both weapons and the worn vest through page/cold recovery'); 151 } finally { 152 try { 153 await fs.mkdir(output, {recursive: true}); 154 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 155 if (game) await game.save(output + '/final'); 156 } finally { await browser.close(); } 157 }