tools/client/test_pending_bag_split.mjs (6031B)
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', args: [ 9 '--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 source => { 24 assert(source, 'the exact native source must be usable'); 25 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 26 }; 27 await game.key('KeyI'); 28 await game.click(735, 45, 'right'); 29 await game.wait('two three-charge stacks open', () => cell(107, 65536) && cell(107, 65537)); 30 await page.keyboard.press('Control+1'); 31 await click(cell(104, 48)); 32 await game.click(867, 206); 33 await game.wait('real carried weapon move sent', () => game.events.some(event => 34 event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104)); 35 const deadline = Date.now() + 10000; 36 do { 37 evidence.pending = await server(); 38 if (evidence.pending.pending) break; 39 await game.snapshot(); 40 } while (Date.now() < deadline); 41 assert(evidence.pending.pending); 42 await game.wait('local equipment bar ends before held server completion', state => 43 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 44 const split = async () => { 45 const sequence = game.sequence; 46 await page.keyboard.down('Control'); 47 try { await click(cell(107, 65536)); } finally { await page.keyboard.up('Control'); } 48 await game.wait('actual native split request', () => game.events.some(event => event.sequence > sequence && 49 event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 52)); 50 return sequence; 51 }; 52 await split(); 53 evidence.requested = await server(); 54 await game.save(output + '/requested'); 55 await game.wait('refused optimistic clone removed', () => !cell(107, 65538)); 56 if (game.state.dragged[0] !== 0) { 57 await game.click(789, 45); 58 await game.wait('refused selected source placement clears', state => state.dragged[0] === 0); 59 } 60 evidence.refused = await server(); 61 assert.deepEqual(evidence.refused, evidence.pending); 62 await game.save(output + '/refused'); 63 64 // Complete the unchanged move, then normally reopen and split/join the source. 65 assert((await page.request.post(url + '/test-complete')).ok()); 66 await game.wait('original weapon completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); 67 if (!cell(107, 65536)) await game.click(735, 45, 'right'); 68 await game.wait('authoritative source stacks usable', () => cell(107, 65536) && cell(107, 65537) && !cell(107, 65538)); 69 const retry = await split(); 70 await game.wait('normal split acknowledged', () => game.events.some(event => event.sequence > retry && 71 event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 52)); 72 evidence.split = await server(); 73 assert.deepEqual(evidence.split.inventory.Containers[2].map(item => [item.ID, item.Slot, item.Count]), [[3, 0, 1], [4, 1, 3], [5, 2, 2]]); 74 if (game.state.dragged[0] !== 0) { 75 const grid = game.state.grids.find(grid => grid.cells.some(cell => cell.identity[0] === 107)); 76 await click(grid.cells.find(source => source.identity[0] === 0 && source.index > 2)); 77 await game.wait('normal split source placed', state => state.dragged[0] === 0); 78 } 79 const join = game.sequence; 80 await click(cell(107, 65538)); 81 await click(cell(107, 65536)); 82 await game.wait('normal join acknowledged', () => game.events.some(event => event.sequence > join && 83 event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53)); 84 evidence.after = await server(); 85 assert.equal(evidence.after.pending, null); 86 assert.equal(evidence.after.inventory.LastID, 5); 87 assert.deepEqual(evidence.after.inventory.Containers, evidence.pending.inventory.Containers); 88 assert.deepEqual(evidence.after.inventory.Items, [ 89 {...evidence.pending.inventory.Items[0], Slot: 6}, evidence.pending.inventory.Items[1]]); 90 await game.save(output + '/retried'); 91 await page.reload(); 92 await game.ready(); 93 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.count]).sort((a, b) => a[0] - b[0]), 94 [[1, 6, 1], [2, 49, 1]]); 95 await game.key('KeyI'); 96 await game.click(735, 45, 'right'); 97 await game.wait('page recovery retains both original stacks', () => cell(107, 65536) && cell(107, 65537) && !cell(107, 65538)); 98 assert.deepEqual((await server()).inventory, evidence.after.inventory); 99 assert.deepEqual(game.errors, []); 100 console.log('PASS: pending split refusal preserves equipment, two original stacks and six charges; real retry/page/cold retain exact ownership with one admitted allocation'); 101 } finally { 102 try { 103 await fs.mkdir(output, {recursive: true}); 104 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 105 if (game) await game.save(output + '/final'); 106 } finally { await browser.close(); } 107 }