tools/client/test_pending_bag_stack.mjs (4670B)
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-bag-stack', 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 cell = (page, slot) => game.state.grids.flatMap(grid => grid.cells).find(cell => 22 cell.identity[0] === page && cell.identity[1] === slot && !cell.dummy); 23 const click = async (page, slot) => { 24 const source = cell(page, slot); 25 assert(source, 'the exact inventory source must be visible'); 26 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 27 }; 28 await game.key('i'); 29 await game.click(735, 45, 'right'); 30 await game.wait('owned backpack opens', () => cell(107, 65536) && cell(107, 65537)); 31 await page.keyboard.press('Control+1'); 32 await click(104, 48); 33 await game.click(867, 206); 34 await game.wait('admitted weapon move', () => game.events.some(event => 35 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104)); 36 const deadline = Date.now() + 10000; 37 do { 38 evidence.pending = await server(); 39 if (evidence.pending.pending) break; 40 await game.snapshot(); 41 } while (Date.now() < deadline); 42 assert(evidence.pending.pending); 43 await game.wait('local equipment animation ends', state => 44 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 45 const join = async () => { 46 const sequence = game.sequence; 47 await click(107, 65536); 48 await click(107, 65537); 49 await game.wait('actual native join request', () => game.events.some(event => event.sequence > sequence && 50 event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53)); 51 }; 52 await join(); 53 evidence.refused = await server(); 54 assert.deepEqual(evidence.refused, evidence.pending); 55 await game.save(output + '/refused'); 56 await game.wait('both refused stack icons remain usable', () => cell(107, 65536) && cell(107, 65537)); 57 evidence.pendingWeaponVisible = Boolean(cell(104, 48)); 58 assert.deepEqual(game.state.dragged, [0, 0]); 59 assert((await page.request.post(url + '/test-complete')).ok()); 60 await game.wait('weapon completes unchanged', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); 61 await join(); 62 await game.wait('normal backpack join retry acknowledged', () => game.events.some(event => event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53)); 63 evidence.after = await server(); 64 assert.equal(evidence.after.pending, null); 65 assert.deepEqual(evidence.after.inventory.Items, [ 66 {...evidence.pending.inventory.Items[0], Slot: 6}, evidence.pending.inventory.Items[1]]); 67 assert.deepEqual(evidence.after.inventory.Containers[2], [{...evidence.pending.inventory.Containers[2][1], Count: 6}]); 68 await game.save(output + '/retried'); 69 await page.reload(); 70 await game.ready(); 71 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 72 await game.snapshot(); 73 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.count]).sort((a, b) => a[0] - b[0]), 74 [[1, 6, 1], [2, 49, 1]]); 75 await game.key('i'); 76 await game.wait('carried backpack visible', () => cell(104, 49)); 77 await game.click(735, 45, 'right'); 78 await game.wait('retained backpack opens', () => cell(107, 65537)); 79 assert(!cell(107, 65536)); 80 assert.deepEqual(game.errors, []); 81 console.log('PASS: pending equipment backpack join refusal preserves usable stacks and permits six-charge retry'); 82 } finally { 83 try { 84 await fs.mkdir(output, {recursive: true}); 85 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 86 if (game) await game.save(output + '/final'); 87 } finally { await browser.close(); } 88 }