tools/client/test_replaced_stack_split.mjs (4312B)
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: 'replaced-stack-split', 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 await game.key('KeyI'); 22 evidence.initial = await server(); 23 assert((await page.request.post(url + '/test-replace-source')).ok()); 24 evidence.replaced = await server(); 25 assert.equal(evidence.replaced.inventory.Items[0].ID, 2); 26 const sequence = game.sequence; 27 await page.keyboard.down('Control'); 28 try { await game.click(681, 45); } finally { await page.keyboard.up('Control'); } 29 await game.wait('actual stale-source split request', () => game.events.some(event => event.sequence > sequence && 30 event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 52)); 31 evidence.requested = await server(); 32 await game.save(output + '/requested'); 33 await game.wait('only the authoritative replacement remains carried', snapshot => game.local(snapshot).inventory.length === 1 && 34 game.local(snapshot).inventory.some(item => item.id === 2 && item.type === 51018 && item.slot === 48 && item.count === 1)); 35 assert.deepEqual(await server(), evidence.replaced); 36 if (game.state.dragged[0] !== 0) { 37 await game.click(735, 45); 38 await game.wait('replacement can be placed normally', snapshot => snapshot.dragged[0] === 0); 39 } 40 await game.wait('replacement icon is usable', snapshot => snapshot.grids.some(grid => grid.cells.some(cell => 41 cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy))); 42 assert.deepEqual(game.local().inventory[0].templates, [123742, 123743]); 43 await game.save(output + '/corrected'); 44 45 // Use the corrected gun before reconnecting, then complete its admitted delay. 46 const source = game.state.grids.flatMap(grid => grid.cells).find(cell => 47 cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy); 48 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2, 'right'); 49 await game.waitFor('same replacement admitted for wear', async () => (await server()).pending); 50 assert((await page.request.post(url + '/test-complete')).ok()); 51 await game.wait('same gun equipped without reload', snapshot => game.local(snapshot).inventory.some(item => 52 item.id === 2 && item.slot === 6) && game.local(snapshot).weapons.some(weapon => weapon.slot === 6 && weapon.loadedAmmo === 3)); 53 evidence.worn = await server(); 54 assert.equal(evidence.worn.inventory.Items[0].Ammo, 3); 55 assert.deepEqual(evidence.worn.inventory.Bank, evidence.replaced.inventory.Bank); 56 await game.save(output + '/worn'); 57 await page.reload(); 58 await game.ready(); 59 await game.wait('loaded rounds replay', snapshot => game.local(snapshot).weapons.some(weapon => weapon.slot === 6 && weapon.loadedAmmo === 3)); 60 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]), [[2, 6, 2, 1]]); 61 assert.deepEqual(game.local().inventory[0].templates, [123742, 123743]); 62 assert.deepEqual(await server(), evidence.worn); 63 assert.deepEqual(game.errors, []); 64 console.log('PASS: stale nonstackable-source split removes phantom charges, restores the loaded replacement and permits same-page wear with page/cold ownership'); 65 } finally { 66 try { 67 await fs.mkdir(output, {recursive: true}); 68 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 69 if (game) await game.save(output + '/final'); 70 } finally { await browser.close(); } 71 }