tools/client/test_corpse_peer_pickup.mjs (3970B)
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 games = []; 12 const evidence = {}; 13 try { 14 for (let id = 1; id <= 2; ++id) { 15 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 16 await context.addCookies([{name: 'ao_client', value: 'corpse-peer-' + id, url}]); 17 const page = await context.newPage(); 18 const game = await GameTest.attach(page); 19 games.push(game); 20 await page.goto(url); 21 await game.ready(); 22 await game.click(...await game.pick(51050, 1000000), 'right'); 23 await game.wait('shared corpse source', state => state.grids.some(grid => grid.cells.some(cell => 24 cell.identity[0] === 107 && cell.identity[1] === 0 && !cell.dummy))); 25 } 26 const [first, second] = games; 27 const server = async () => (await first.page.request.get(url + '/test-state')).json(); 28 evidence.before = await server(); 29 assert.equal(evidence.before.items.length, 1); 30 31 // The other native client commits pickup during this real mouse gesture. 32 const point = await first.point(30, 45); 33 await first.page.bringToFront(); 34 await first.page.mouse.move(point.x, point.y); 35 const sequence = first.sequence; 36 await first.page.mouse.down(); 37 try { 38 await first.waitInput(sequence, 1025); 39 await second.click(30, 45); 40 await second.wait('recipient owns the item', state => second.local(state).inventory.length === 1); 41 await first.wait('other open window receives committed contents', () => first.events.some(event => 42 event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '4e536976' && 43 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 1009)); 44 } finally { 45 const release = first.sequence; 46 await first.page.mouse.up(); 47 await first.waitInput(release, 1026); 48 } 49 50 // An empty refresh must remove the selectable source, not leave a phantom. 51 await first.click(30, 45); 52 await first.snapshot(); 53 evidence.staleRequests = first.events.filter(event => event.sequence > sequence && 54 event.direction === 'framesent' && event.opcode === '47537a24').length; 55 evidence.after = await server(); 56 await first.save(output + '/empty-peer-window'); 57 assert.equal(evidence.staleRequests, 0, 'The collected source must no longer issue pickup requests'); 58 assert.equal(first.local().inventory.length, 0); 59 assert(first.state.regions.some(region => region[0] === 0 && region[1] === 0 && region[2] === 178 && region[3] === 128)); 60 assert.deepEqual(evidence.after.items, []); 61 assert.equal(evidence.after.first.Credits, 1); 62 assert.equal(evidence.after.second.Credits, 0); 63 assert.deepEqual(evidence.after.second.Items.map(item => item.ID), evidence.before.items.map(item => item.ID)); 64 65 for (const game of games) { 66 await game.page.reload(); 67 await game.ready(); 68 assert.deepEqual(game.local().inventory.map(item => 69 [item.type, item.id, item.slot, item.quality, item.templates, item.count]), 70 game === second ? [[51005, 1, 48, 1, [42640, 42640], 1]] : []); 71 assert.deepEqual(game.errors, []); 72 } 73 console.log('PASS: peer pickup clears a selectable corpse source during a native mouse gesture; page/cold ownership stays exact'); 74 } finally { 75 try { 76 await fs.mkdir(output, {recursive: true}); 77 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 78 for (let index = 0; index < games.length; ++index) await games[index].save(output + '/final-' + (index + 1)); 79 } finally { await browser.close(); } 80 }