tools/client/test_pending_bank.mjs (13569B)
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 const evidence = {}; 12 let game; 13 try { 14 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 15 await context.addCookies([{name: 'ao_client', value: 'pending-bank', 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 const admitted = async () => { 22 const deadline = Date.now() + 10000; 23 do { 24 const state = await server(); 25 if (state.pending) return state; 26 await game.snapshot(); 27 } while (Date.now() < deadline); 28 assert.fail('server did not admit the native equipment request'); 29 }; 30 const carried = state => state.grids.flatMap(grid => grid.cells).find(cell => 31 cell.identity[0] === 104 && cell.identity[1] === 49 && !cell.dummy); 32 const clickCell = async cell => game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 33 await game.key('i'); 34 if (!game.state.camera.firstPerson) await game.key('F8'); 35 await game.click(...await game.pick(51005, 3221357727), 'right'); 36 await game.wait('bank opens', state => state.grids.some(grid => grid.totalCells === 102)); 37 await page.keyboard.press('Control+1'); 38 const weapon = game.state.grids.flatMap(grid => grid.cells).find(cell => 39 cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy); 40 await clickCell(weapon); 41 await game.click(867, 206); 42 evidence.pending = await admitted(); 43 assert(evidence.pending.pending); 44 assert(evidence.pending.bank); 45 await game.wait('local equip ends while server completion remains pending', state => 46 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 47 const deposit = async () => { 48 const sequence = game.sequence; 49 await clickCell(carried(game.state)); 50 await game.wait('carried part picked', state => state.dragged[0] === 104 && state.dragged[1] === 49); 51 await game.click(30, 45); 52 await game.wait('actual bank deposit', () => game.events.some(event => event.sequence > sequence && 53 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(37) === 57005)); 54 return sequence; 55 }; 56 const sequence = await deposit(); 57 evidence.refused = await server(); 58 assert.deepEqual(evidence.refused, evidence.pending); 59 await game.save(output + '/pending'); 60 await game.wait('refused carried source restored', state => carried(state) && game.events.some(event => 61 event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '47537a24')); 62 assert.deepEqual(game.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' && 63 event.opcode === '47537a24').map(event => { 64 const packet = Buffer.from(event.hex, 'hex'); 65 return [packet.readUInt32BE(29), packet.readUInt32BE(33), packet.readUInt32BE(45)]; 66 }), [[104, 49, 48], [104, 48, 49]]); 67 assert(game.state.grids.some(grid => grid.totalCells === 102)); 68 assert.deepEqual(game.state.dragged, [0, 0]); 69 await game.save(output + '/refused'); 70 assert((await page.request.post(url + '/test-complete')).ok()); 71 await game.wait('original weapon completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); 72 await deposit(); 73 await game.wait('same-window bank retry succeeds', state => !game.local(state).inventory.some(item => item.id === 2) && 74 state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 105 && cell.identity[1] === 0 && !cell.dummy))); 75 evidence.after = await server(); 76 assert.equal(evidence.after.pending, null); 77 assert.deepEqual(evidence.after.bank, evidence.pending.bank); 78 assert.deepEqual(evidence.after.inventory.Items, [ 79 {...evidence.pending.inventory.Items[0], Slot: 6}, evidence.pending.inventory.Items[2]]); 80 assert.deepEqual(evidence.after.inventory.Bank, [{...evidence.pending.inventory.Items[1], Slot: 0}]); 81 await game.save(output + '/deposited'); 82 83 // Move the bank window aside using consumed native mouse input. 84 const title = await game.point(80, 8), destination = await game.point(280, 8); 85 await page.mouse.move(title.x, title.y); 86 await game.snapshot(); 87 let mouseSequence = game.sequence; 88 await page.mouse.down(); 89 try { 90 await game.waitInput(mouseSequence, 1025); 91 await page.mouse.move(destination.x, destination.y, {steps: 8}); 92 await game.wait('bank window moved aside', state => state.grids.some(grid => 93 grid.totalCells === 102 && grid.cells[0].bounds[0] >= 200)); 94 } finally { 95 mouseSequence = game.sequence; 96 await page.mouse.up(); 97 } 98 await game.waitInput(mouseSequence, 1026); 99 await game.click(789, 45, 'right'); 100 const contained = state => state.grids.flatMap(grid => grid.cells).find(cell => 101 cell.identity[0] === 107 && cell.identity[1] === (1 << 16 | 5) && !cell.dummy); 102 await game.wait('owned backpack opens beside bank', contained); 103 const removal = game.sequence; 104 await game.click(867, 206, 'right'); 105 await game.wait('native weapon removal requested', () => game.events.some(event => event.sequence > removal && 106 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 101)); 107 evidence.containedPending = await admitted(); 108 assert(evidence.containedPending.pending); 109 await game.wait('local removal ends before server completion', state => 110 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 111 const depositContained = async () => { 112 const sequence = game.sequence; 113 await clickCell(contained(game.state)); 114 await game.wait('contained part picked', state => state.dragged[0] === 107 && state.dragged[1] === (1 << 16 | 5)); 115 const bankCell = game.state.grids.find(grid => grid.totalCells === 102).cells[1]; 116 await clickCell(bankCell); 117 await game.wait('actual contained bank deposit', () => game.events.some(event => event.sequence > sequence && 118 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 107)); 119 return sequence; 120 }; 121 const containedSequence = await depositContained(); 122 evidence.containedRefused = await server(); 123 assert.deepEqual(evidence.containedRefused, evidence.containedPending); 124 await game.save(output + '/contained-pending'); 125 await game.wait('contained deposit cancelled', () => game.events.some(event => event.sequence > containedSequence && 126 event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146)); 127 await game.wait('contained bank source restored', contained); 128 assert.deepEqual(game.state.dragged, [0, 0]); 129 await game.save(output + '/contained-refused'); 130 assert((await page.request.post(url + '/test-complete')).ok()); 131 await game.wait('original removal completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 48)); 132 await depositContained(); 133 await game.wait('same-window contained bank retry succeeds', state => state.grids.some(grid => grid.cells.some(cell => 134 cell.identity[0] === 105 && cell.identity[1] === 1 && !cell.dummy))); 135 evidence.containedAfter = await server(); 136 assert.equal(evidence.containedAfter.pending, null); 137 assert.deepEqual(evidence.containedAfter.bank, evidence.pending.bank); 138 assert.equal(evidence.containedAfter.inventory.Containers, null); 139 assert.deepEqual(evidence.containedAfter.inventory.Items, [ 140 {...evidence.containedPending.inventory.Items[0], Slot: 64}, evidence.containedPending.inventory.Items[1]]); 141 assert.deepEqual(evidence.containedAfter.inventory.Bank, [ 142 evidence.after.inventory.Bank[0], {...evidence.containedPending.inventory.Containers[3][0], Slot: 1}]); 143 await game.save(output + '/contained-deposited'); 144 145 // Withdraw a banked part while another native weapon move awaits completion. 146 await game.click(681, 45); 147 await game.click(867, 206); 148 evidence.withdrawPending = await admitted(); 149 await game.wait('local equip ends before bank withdrawal', state => 150 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 151 const withdraw = async () => { 152 const bankCell = game.state.grids.find(grid => grid.totalCells === 102).cells[0]; 153 await clickCell(bankCell); 154 }; 155 const withdrawal = game.sequence; 156 await withdraw(); 157 await game.wait('actual bank withdrawal reaches server', () => game.events.some(event => event.sequence > withdrawal && 158 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 105)); 159 await game.wait('native bank withdrawal cancelled', () => game.events.some(event => event.sequence > withdrawal && 160 event.direction === 'framereceived' && event.opcode === '5e477770' && 161 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === 105 && 162 Buffer.from(event.hex, 'hex').readUInt32BE(37) === 0)); 163 await game.wait('bank source restored after refused withdrawal', state => state.grids.some(grid => grid.cells.some(cell => 164 cell.identity[0] === 105 && cell.identity[1] === 0 && !cell.dummy))); 165 evidence.withdrawRefused = await server(); 166 assert.deepEqual(evidence.withdrawRefused, evidence.withdrawPending); 167 assert.deepEqual(game.state.dragged, [0, 0]); 168 await game.save(output + '/withdraw-refused'); 169 170 // Deliver a late request after the server has processed the bank close. 171 assert((await page.request.post(url + '/test-close-bank')).ok()); 172 evidence.closedPending = await server(); 173 assert.equal(evidence.closedPending.bank, null); 174 assert.deepEqual(evidence.closedPending.pending, evidence.withdrawPending.pending); 175 const closed = game.sequence; 176 await withdraw(); 177 await game.wait('late withdrawal reaches server', () => game.events.some(event => event.sequence > closed && 178 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 105)); 179 await game.wait('late closed-bank withdrawal cancelled', () => game.events.some(event => event.sequence > closed && 180 event.direction === 'framereceived' && event.opcode === '5e477770' && 181 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === 105 && 182 Buffer.from(event.hex, 'hex').readUInt32BE(37) === 0)); 183 await game.wait('late closed-bank source restored', state => state.grids.some(grid => grid.cells.some(cell => 184 cell.identity[0] === 105 && cell.identity[1] === 0 && !cell.dummy))); 185 evidence.closedRefused = await server(); 186 assert.deepEqual(evidence.closedRefused, evidence.closedPending); 187 await game.save(output + '/closed-refused'); 188 assert((await page.request.post(url + '/test-complete')).ok()); 189 await game.wait('original axe equip completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); 190 await game.click(...await game.pick(51005, 3221357727), 'right'); 191 await game.wait('bank reopens after the admitted move completes', () => game.events.some(event => event.sequence > closed && 192 event.direction === 'framereceived' && event.opcode === '343c287f')); 193 await withdraw(); 194 await game.wait('withdrawal retry succeeds after reopening', state => game.local(state).inventory.some(item => item.id === 2 && item.slot === 48)); 195 evidence.withdrawAfter = await server(); 196 assert.equal(evidence.withdrawAfter.pending, null); 197 assert.deepEqual(evidence.withdrawAfter.bank, evidence.withdrawPending.bank); 198 assert.deepEqual(evidence.withdrawAfter.inventory.Items, [ 199 {...evidence.containedAfter.inventory.Items[0], Slot: 6}, evidence.containedAfter.inventory.Items[1], 200 {...evidence.containedAfter.inventory.Bank[0], Slot: 64}]); 201 assert.deepEqual(evidence.withdrawAfter.inventory.Bank, [evidence.containedAfter.inventory.Bank[1]]); 202 await game.save(output + '/withdraw-retried'); 203 await page.reload(); 204 await game.ready(); 205 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]), [[1, 6, 1, 1], [2, 48, 1, 1], [3, 50, 1, 1]]); 206 if (!game.state.camera.firstPerson) await game.key('F8'); 207 await game.click(...await game.pick(51005, 3221357727), 'right'); 208 await game.wait('banked part retained after page replay', state => state.grids.some(grid => grid.cells.some(cell => 209 cell.identity[0] === 105 && cell.identity[1] === 1 && !cell.dummy))); 210 assert.deepEqual((await server()).inventory, evidence.withdrawAfter.inventory); 211 assert.equal(game.errors.length, 0); 212 console.log('PASS: pending bank deposits and withdrawal restore sources, preserve bank/moves and retry with exact page/cold recovery'); 213 } finally { 214 try { 215 await fs.mkdir(output, {recursive: true}); 216 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 217 if (game) await game.save(output + '/final'); 218 } finally { await browser.close(); } 219 }