tools/client/test_stale_contained_use.mjs (4016B)
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: 'stale-contained-use', 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 use = async (page, slot) => { 24 const source = cell(page, slot); 25 assert(source, 'native Use source is visible'); 26 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2, 'right'); 27 }; 28 const command = async slot => { 29 const sequence = game.sequence; 30 await use(107, slot); 31 await game.wait('actual contained Use request', () => game.events.some(event => event.sequence > sequence && 32 event.direction === 'framesent' && event.opcode === '52526858' && Buffer.from(event.hex, 'hex').readUInt32BE(53) === 107)); 33 const request = game.events.find(event => event.sequence > sequence && event.direction === 'framesent' && event.opcode === '52526858'); 34 return {sequence, reference: Buffer.from(request.hex, 'hex').readUInt32BE(33)}; 35 }; 36 await game.key('KeyI'); 37 await use(104, 48); 38 await game.wait('Briefcase source visible in first bag', () => cell(107, 65536)); 39 assert((await page.request.post(url + '/test-stale-view')).ok()); 40 evidence.before = await server(); 41 const first = await command(65536); 42 evidence.first = first; 43 await game.save(output + '/requested'); 44 await game.wait('stale Use receives exact failure completion', () => game.events.some(event => event.sequence > first.sequence && 45 event.direction === 'framereceived' && event.opcode === '52526858' && 46 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 2 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === first.reference)); 47 await game.wait('empty cached child row clears', () => !cell(107, 65536)); 48 assert.deepEqual(await server(), evidence.before); 49 await game.save(output + '/refused'); 50 await use(104, 48); 51 await game.wait('first bag closes', state => !state.grids.some(grid => grid.totalCells === 21)); 52 await use(104, 49); 53 await game.wait('real Briefcase destination opens', () => cell(107, 131072)); 54 const second = await command(131072); 55 evidence.second = second; 56 await game.wait('same-page retained Briefcase opens bank', () => game.events.some(event => event.sequence > second.sequence && 57 event.direction === 'framereceived' && event.opcode === '343c287f')); 58 assert.deepEqual(await server(), evidence.before); 59 await game.save(output + '/retried'); 60 await page.reload(); 61 await game.ready(); 62 assert.deepEqual(await server(), evidence.before); 63 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot]), [[1, 48], [2, 49]]); 64 assert.deepEqual(game.errors, []); 65 assert.equal(game.dropped, 0); 66 console.log('PASS: stale contained Use completes exactly, clears its empty cached row and permits same-page remote-bank Use'); 67 } finally { 68 try { 69 await fs.mkdir(output, {recursive: true}); 70 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 71 if (game) await game.save(output + '/final'); 72 } finally { await browser.close(); } 73 }