tools/client/test_reclaim_weapon.mjs (6801B)
1 import assert from 'node:assert/strict'; 2 import {createRequire} from 'node:module'; 3 import {GameTest} from './game_test.mjs'; 4 5 const {chromium} = createRequire(import.meta.url)('playwright'); 6 const [url, output, phase] = process.argv.slice(2); 7 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', 8 args: ['--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage', 9 '--disable-background-timer-throttling', '--disable-renderer-backgrounding']}); 10 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 11 await context.addCookies([{name: 'ao_client', value: 'reclaim-weapon', url}]); 12 const page = await context.newPage(); 13 const game = await GameTest.attach(page); 14 const state = async () => (await page.request.get(url + '/test-state')).json(); 15 try { 16 await page.goto(url); 17 await game.ready(); 18 assert(game.local().inventory.some(item => item.id === 1 && item.slot === 6)); 19 assert.equal((await state()).psychic, 15); 20 if (phase === 'live') { 21 await game.key('KeyI'); 22 await page.keyboard.press('Control+1'); 23 await game.click(867, 206, 'right'); 24 await game.wait('instantiated weapon returns to inventory', snapshot => game.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 49)); 25 assert.equal((await state()).psychic, 0); 26 await game.save(output + '/removed'); 27 28 const sequence = game.sequence; 29 assert.equal((await page.request.post(url + '/test-fatal-hit')).status(), 204); 30 await game.wait('same-playfield death return', snapshot => snapshot.localId === 1 && game.local(snapshot)?.stats[27] === 1 && 31 game.local(snapshot)?.inventory.length === 0 && game.lifecycle.some(event => event.sequence > sequence && 32 event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 152), 30000); 33 await game.ready(); 34 const dead = await state(); 35 assert.equal(dead.playfield, 705); 36 assert.equal(dead.inventory.Reclaim.length, 2); 37 assert.equal(dead.inventory.Reclaim.find(entry => entry.Item.ID === 1).Item.Ammo, 13); 38 assert(dead.shock > 4500 && dead.shock <= 5000); 39 assert(dead.effective_psychic >= 101 && dead.effective_psychic < 115); 40 assert(game.local().stats[247] > 4500 && game.local().stats[247] <= 5000); 41 assert.equal(game.local().stats[21], 200); 42 assert.equal((await page.request.post(url + '/test-ready-reclaim')).status(), 204); 43 const [x, y] = await game.pick(51059, 3221881537); 44 await game.click(x, y, 'right'); 45 await game.wait('native reclaim window', snapshot => snapshot.regions.some(region => 46 region[0] === 0 && region[1] === 0 && region[2] === 178 && region[3] === 128)); 47 await game.click(30, 45); 48 await game.wait('same weapon reclaimed', snapshot => game.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 48)); 49 await game.click(84, 45); 50 await game.wait('same energy stack reclaimed', snapshot => game.local(snapshot).inventory.some(item => item.id === 2 && item.count === 100)); 51 await game.click(170, 8); 52 if (!game.state.regions.some(region => region[0] === 654 && region[1] === 0 && region[2] === 832 && region[3] === 182)) await game.key('KeyI'); 53 await game.save(output + '/collected'); 54 const attempt = game.sequence; 55 await game.click(681, 45, 'right'); 56 await game.save(output + '/shock-rejected'); 57 assert(!game.events.some(event => event.sequence > attempt && event.direction === 'framesent' && event.opcode === '47537a24')); 58 assert.equal((await state()).inventory.Items.find(item => item.ID === 1).Slot, 64); 59 await game.key('KeyU'); 60 await game.save(output + '/shock-skills'); 61 62 // Replay a controlled last-tick checkpoint, then observe native/server expiry. 63 assert.equal((await page.request.post(url + '/test-last-shock-tick')).status(), 204); 64 await page.reload(); 65 await game.ready(); 66 await game.wait('native shock recovers', snapshot => game.local(snapshot).stats[247] === 0); 67 await game.waitFor('server shock expires', async () => (await state()).shock === 0); 68 await game.key('KeyI'); 69 await game.click(681, 45, 'right'); 70 await game.wait('reclaimed native weapon equips again', snapshot => game.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 6)); 71 } 72 const final = await state(); 73 assert.equal(final.psychic, 15); 74 assert.equal(final.shock, 0); 75 assert.equal(final.effective_psychic, 215); 76 assert.equal(final.inventory.Items.find(item => item.ID === 1).Ammo, 13); 77 assert.equal(final.inventory.Items.find(item => item.ID === 2).Count, 100); 78 assert.equal(final.inventory.Reclaim, null); 79 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, ...item.templates, item.count]).sort((a, b) => a[0] - b[0]), [ 80 [1, 6, 75, 124260, 124261, 1], [2, 49, 1, 21609, 21609, 100], 81 ]); 82 await game.wait('loaded reclaimed weapon delivered', () => game.events.some(event => 83 event.direction === 'framereceived' && event.opcode === '3b1d2268' && event.actor[1] === 1)); 84 await game.wait('derived resource maxima restored', snapshot => game.local(snapshot).stats[1] === final.maximum_health && 85 game.local(snapshot).stats[221] === final.maximum_nano); 86 const event = game.events.findLast(event => event.direction === 'framereceived' && event.opcode === '3b1d2268' && event.actor[1] === 1); 87 const packet = Buffer.from(event.hex, 'hex'); 88 const stats = new Map(); 89 for (let offset = 59; offset + 8 <= packet.length - 4; offset += 8) stats.set(packet.readUInt32BE(offset), packet.readUInt32BE(offset + 4)); 90 assert.equal(stats.get(26), 13); 91 await game.key('KeyU'); 92 await game.save(output + '/psychic'); 93 if (phase === 'live') { 94 const lifecycle = game.lifecycle.filter(event => event.actor?.[1] === 1); 95 assert.deepEqual(lifecycle.map(event => [event.direction, event.opcode, 96 Buffer.from(event.hex, 'hex').readUInt32BE(29)]), [ 97 ['framereceived', '5e477770', 99], ['framesent', '5e477770', 152], ['framereceived', '445f2a0b', 1], 98 ]); 99 100 // Controlled trace load isolates rollover from another gameplay replay. 101 for (let index = 0; index < 2050; ++index) game.record({kind: 'fixture-replay', index}); 102 assert.equal(game.events.length, 2048); 103 assert(game.events.every(event => event.sequence > lifecycle[2].sequence)); 104 assert.deepEqual(game.lifecycle, lifecycle); 105 assert.equal(game.lifecycleDropped, 0); 106 await game.save(output + '/lifecycle-rollover'); 107 } 108 assert.equal(game.errors.length, 0); 109 console.log('PASS:', phase, 'reclaimed weapon, ammunition, recovered stats and exactly-once Psychic bonus'); 110 } finally { 111 try { await game.save(output + '/final'); } 112 finally { await browser.close(); } 113 }