tools/client/test_pending_backpack.mjs (10612B)
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-backpack', 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 source = state => state.grids.flatMap(grid => grid.cells).find(cell => 22 cell.identity[0] === 107 && cell.identity[1] === (1 << 16 | 5) && !cell.dummy); 23 const clickCell = async cell => game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 24 await game.key('i'); 25 await game.click(735, 45, 'right'); 26 await game.wait('open backpack source', source); 27 await page.keyboard.press('Control+1'); 28 const weapon = game.state.grids.flatMap(grid => grid.cells).find(cell => 29 cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy); 30 assert(weapon); 31 const equipSequence = game.sequence; 32 await clickCell(weapon); 33 await game.click(867, 206); 34 await game.wait('actual equipment request', () => game.events.some(event => event.sequence > equipSequence && 35 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104)); 36 evidence.pending = await server(); 37 assert(evidence.pending.pending); 38 await game.wait('local equip ends while server completion remains pending', state => 39 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 40 const withdraw = async () => { 41 const sequence = game.sequence; 42 await clickCell(source(game.state)); 43 await game.wait('bag source picked', state => state.dragged[0] === 107 && state.dragged[1] === (1 << 16 | 5)); 44 await game.click(789, 45); 45 await game.wait('actual backpack withdrawal', () => game.events.some(event => event.sequence > sequence && 46 event.direction === 'framesent' && event.opcode === '47537a24' && 47 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 107)); 48 return sequence; 49 }; 50 const sequence = await withdraw(); 51 evidence.refused = await server(); 52 assert.deepEqual(evidence.refused, evidence.pending); 53 await game.save(output + '/pending'); 54 await game.wait('backpack withdrawal explicitly cancelled', () => game.events.some(event => event.sequence > sequence && 55 event.direction === 'framereceived' && event.opcode === '5e477770' && 56 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146)); 57 await game.wait('same backpack source restored', source); 58 assert.deepEqual(game.state.dragged, [0, 0]); 59 await game.save(output + '/refused'); 60 assert((await page.request.post(url + '/test-complete')).ok()); 61 await game.wait('original weapon completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); 62 await withdraw(); 63 await game.wait('same-window withdrawal retry succeeds', state => game.local(state).inventory.some(item => item.id === 3 && item.slot === 48)); 64 evidence.after = await server(); 65 assert.equal(evidence.after.pending, null); 66 assert.equal(evidence.after.inventory.Containers, null); 67 assert.deepEqual(evidence.after.inventory.Items, [ 68 {...evidence.pending.inventory.Items[0], Slot: 6}, evidence.pending.inventory.Items[1], 69 {...evidence.pending.inventory.Containers[2][0], Slot: 64}]); 70 assert.equal(game.events.filter(event => event.sequence > sequence && event.direction === 'framesent' && 71 event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 107).length, 2); 72 await game.save(output + '/picked'); 73 74 // Exercise the matching deposit after a second real equipment request. 75 await game.wait('local equip action ended', state => !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 76 const removal = game.sequence; 77 await game.click(867, 206, 'right'); 78 await game.wait('actual weapon removal request', () => game.events.some(event => event.sequence > removal && 79 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 101)); 80 evidence.depositPending = await server(); 81 assert(evidence.depositPending.pending); 82 await game.wait('local removal ends while server completion remains pending', state => 83 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 84 const deposit = async () => { 85 const sequence = game.sequence; 86 const part = game.state.grids.flatMap(grid => grid.cells).find(cell => 87 cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy); 88 assert(part, 'carried part source is visible'); 89 await clickCell(part); 90 await game.wait('carried part picked', state => state.dragged[0] === 104 && state.dragged[1] === 48); 91 await game.click(30, 45); 92 await game.wait('actual backpack deposit', () => game.events.some(event => event.sequence > sequence && 93 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(37) === 51017)); 94 return sequence; 95 }; 96 const depositSequence = await deposit(); 97 evidence.depositRefused = await server(); 98 assert.deepEqual(evidence.depositRefused, evidence.depositPending); 99 await game.save(output + '/deposit-pending'); 100 await game.wait('refused deposit source restored', state => state.grids.some(grid => grid.cells.some(cell => 101 cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy)) && 102 game.events.some(event => event.sequence > depositSequence && event.direction === 'framereceived' && event.opcode === '47537a24')); 103 assert.deepEqual(game.state.dragged, [0, 0]); 104 await game.save(output + '/deposit-refused'); 105 assert((await page.request.post(url + '/test-complete')).ok()); 106 await game.wait('original weapon removal completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 50)); 107 await deposit(); 108 await game.wait('same-window deposit retry succeeds', state => state.grids.some(grid => grid.cells.some(cell => 109 cell.identity[0] === 107 && cell.identity[1] === (1 << 16) && !cell.dummy))); 110 evidence.depositAfter = await server(); 111 assert.equal(evidence.depositAfter.pending, null); 112 assert.deepEqual(evidence.depositAfter.inventory.Items, [ 113 {...evidence.depositPending.inventory.Items[0], Slot: 66}, evidence.depositPending.inventory.Items[1]]); 114 assert.deepEqual(evidence.depositAfter.inventory.Containers, {2: [{...evidence.depositPending.inventory.Items[2], Slot: 0}]}); 115 await game.save(output + '/deposited'); 116 117 // A late request from a known closed source must restore the cached icon. 118 assert((await page.request.post(url + '/test-close-source')).ok()); 119 evidence.closedBefore = await server(); 120 const closedSequence = game.sequence; 121 const contained = state => state.grids.flatMap(grid => grid.cells).find(cell => 122 cell.identity[0] === 107 && cell.identity[1] === (1 << 16) && !cell.dummy); 123 await clickCell(contained(game.state)); 124 await game.wait('closed-source item picked', state => state.dragged[0] === 107 && state.dragged[1] === (1 << 16)); 125 await clickCell(game.state.grids.find(grid => grid.totalCells === 30).cells.find(cell => cell.identity[0] === 0)); 126 await game.wait('actual closed-source withdrawal', () => game.events.some(event => event.sequence > closedSequence && 127 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 107)); 128 await game.wait('closed-source withdrawal explicitly cancelled', state => contained(state) && game.events.some(event => 129 event.sequence > closedSequence && event.direction === 'framereceived' && event.opcode === '5e477770' && 130 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146)); 131 evidence.closedRefused = await server(); 132 assert.deepEqual(evidence.closedRefused, evidence.closedBefore); 133 assert.deepEqual(game.state.dragged, [0, 0]); 134 await game.save(output + '/closed-refused'); 135 await game.click(735, 45, 'right'); 136 await game.wait('real Use reopens the source', () => game.events.some(event => event.sequence > closedSequence && 137 event.direction === 'framereceived' && event.opcode === '4e536976')); 138 await game.wait('reopened contained source', contained); 139 await clickCell(contained(game.state)); 140 await game.wait('reopened item picked', state => state.dragged[0] === 107 && state.dragged[1] === (1 << 16)); 141 await clickCell(game.state.grids.find(grid => grid.totalCells === 30).cells.find(cell => cell.identity[0] === 0)); 142 await game.wait('normal reopen permits withdrawal', state => game.local(state).inventory.some(item => item.id === 3 && item.slot === 48)); 143 await deposit(); 144 await game.wait('part returned after closed-source retry', contained); 145 assert.deepEqual((await server()).inventory, evidence.depositAfter.inventory); 146 await game.save(output + '/closed-retried'); 147 await page.reload(); 148 await game.ready(); 149 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]).sort((a, b) => a[0] - b[0]), 150 [[1, 50, 1, 1], [2, 49, 1, 1]]); 151 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 152 await game.snapshot(); 153 if (!game.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))) await game.key('i'); 154 await game.click(735, 45, 'right'); 155 await game.wait('deposited part retained after page replay', state => state.grids.some(grid => grid.cells.some(cell => 156 cell.identity[0] === 107 && cell.identity[1] === (1 << 16) && !cell.dummy))); 157 assert.equal(game.errors.length, 0); 158 console.log('PASS: backpack refusals restore sources through pending equipment, ordinary reopening, retry/page/cold recovery'); 159 } finally { 160 try { 161 await fs.mkdir(output, {recursive: true}); 162 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 163 if (game) await game.save(output + '/final'); 164 } finally { await browser.close(); } 165 }