tools/client/test_stale_player_offer.mjs (5066B)
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 const games = [], evidence = {}; 12 try { 13 for (const index of [0, 1]) { 14 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 15 await context.addCookies([{name: 'ao_client', value: 'stale-player-offer-' + index, url}]); 16 const page = await context.newPage(), game = await GameTest.attach(page); 17 games.push(game); 18 await page.goto(url); 19 await game.ready(); 20 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 21 await game.snapshot(); 22 } 23 const [owner, peer] = games; 24 const server = async () => (await owner.page.request.get(url + '/test-state')).json(); 25 const cell = slot => owner.state.grids.flatMap(grid => grid.cells).find(cell => 26 cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy); 27 const click = async slot => { 28 const source = cell(slot); 29 assert(source, `ordinary slot ${slot} must be usable`); 30 await owner.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 31 }; 32 const openTrade = async () => { 33 await owner.click(...await owner.pick(50000, 2), 'right'); 34 await Promise.all(games.map(game => game.wait('both player trade windows open', state => 35 state.regions.some(([left, top, right, bottom]) => left === 832 && top === 0 && right === 1024 && bottom === 247)))); 36 }; 37 const closed = state => !state.regions.some(([left, top, right, bottom]) => 38 left === 832 && top === 0 && right === 1024 && bottom === 247); 39 await owner.key('KeyI'); 40 if (!owner.state.camera.firstPerson) await owner.key('F8'); 41 await openTrade(); 42 assert((await owner.page.request.post(url + '/test-stale-view')).ok()); 43 evidence.merged = await server(); 44 await owner.save(output + '/stale'); 45 await click(48); 46 await owner.wait('consumed source picked', state => state.dragged[0] === 104 && state.dragged[1] === 48); 47 const sequence = owner.sequence; 48 await owner.click(864, 172); 49 await owner.wait('actual stale player offer request', () => owner.events.some(event => event.sequence > sequence && 50 event.direction === 'framesent' && event.opcode === '36284f6e' && 51 Buffer.from(event.hex, 'hex').readUInt32BE(33) === 4)); 52 await Promise.all(games.map(game => game.wait('invalid trade cancels for both participants', closed))); 53 await owner.wait('ghost removed and current counts restored', state => !cell(48) && cell(49) && cell(50) && 54 owner.local(state).inventory.find(item => item.slot === 49)?.count === 8); 55 evidence.refused = await server(); 56 assert.deepEqual(evidence.refused, evidence.merged); 57 await owner.save(output + '/refused'); 58 59 // Retry the retained source in a new native trade on the same page. 60 await openTrade(); 61 await click(49); 62 await owner.wait('survivor picked', state => state.dragged[0] === 104 && state.dragged[1] === 49); 63 await owner.click(864, 172); 64 await peer.wait('current stack visible to peer', state => state.grids.some(grid => 65 grid.cells.some(cell => cell.identity[0] === 208 && !cell.dummy))); 66 await peer.save(output + '/retry-peer'); 67 await owner.click(945, 228); 68 await Promise.all(games.map(game => game.wait('retry cancelled for both participants', closed))); 69 await owner.wait('retained stack remains usable', state => cell(48) && cell(50) && 70 owner.local(state).inventory.some(item => item.slot === 48 && item.count === 8)); 71 evidence.after = await server(); 72 assert.deepEqual(evidence.after.owner.Items.map(item => [item.ID, item.Slot, item.Count]), [[2, 64, 8], [3, 66, 7]]); 73 assert.equal(evidence.after.peer.Items?.length ?? 0, 0); 74 for (const game of games) { 75 await game.page.reload(); 76 await game.ready(); 77 await game.page.waitForFunction(() => touchRegion(500, 400)?.world === true); 78 await game.snapshot(); 79 assert.deepEqual(game.errors, []); 80 } 81 assert.deepEqual(owner.local().inventory.map(item => [item.id, item.slot, item.quality, item.count, ...item.templates]), 82 [[2, 48, 2, 8, 25812, 25811], [3, 50, 2, 7, 25812, 25811]]); 83 assert.deepEqual(peer.local().inventory, []); 84 assert.deepEqual(await server(), evidence.after); 85 console.log('PASS: both-player cancellation, authoritative stacks, real retry and page/cold ownership retain fifteen charges'); 86 } finally { 87 try { 88 await fs.mkdir(output, {recursive: true}); 89 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 90 for (const [index, game] of games.entries()) await game.save(output + '/final-' + index); 91 } finally { await browser.close(); } 92 }