tools/client/test_vektor.mjs (7020B)
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 games = []; 11 12 async function bagClick(game, id, button = 'right') { 13 await game.wait('native equipment action finishes', state => 14 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 15 const item = game.local().inventory.find(item => item.id === id); 16 assert(item && item.slot >= 48 && item.slot < 57); 17 const cell = item.slot - 48; 18 await game.click(681 + cell % 3 * 54, 45 + Math.floor(cell / 3) * 54, button); 19 } 20 21 try { 22 for (const token of ['vektor-owner', 'vektor-watch']) { 23 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 24 await context.addCookies([{name: 'ao_client', value: token, url}]); 25 const page = await context.newPage(); 26 const game = await GameTest.attach(page); 27 games.push(game); 28 await page.goto(url); 29 await game.ready(); 30 } 31 const [owner, observer] = games; 32 const state = async () => (await owner.page.request.get(url + '/test-state')).json(); 33 if (phase === 'live') { 34 assert.equal(owner.local().stats[115], 300); 35 assert.equal(owner.local().stats[21], 200); 36 const nanoMaximum = owner.local().stats[221]; 37 await owner.key('KeyI'); 38 await owner.page.keyboard.press('Control+1'); 39 for (const [id, psychic] of [[1, 200], [2, 200], [3, 215]]) { 40 const sequence = observer.sequence; 41 await bagClick(owner, id); 42 await owner.wait('selected Vektor worn', snapshot => owner.local(snapshot).inventory.some(item => item.id === id && item.slot === 6)); 43 await observer.wait('observer receives selected Vektor', () => observer.events.some(event => { 44 if (event.sequence <= sequence || event.direction !== 'framereceived' || event.opcode !== '3b1d2268') return false; 45 return event.actor[0] === 51018 && event.actor[1] === id; 46 })); 47 assert.equal(owner.local().stats[21], 200); 48 assert.equal(owner.local().stats[221], nanoMaximum + (id === 3 ? 9 : 0)); 49 assert.equal((await state()).psychic, psychic - 200); 50 await owner.save(output + '/worn-' + id); 51 52 if (id === 2) { 53 // Ammunition-on-weapon uses actual inventory clicks and the native use request. 54 await bagClick(owner, 5, 'left'); 55 await owner.click(867, 206, 'right'); 56 await owner.wait('energy stack debited', snapshot => owner.local(snapshot).inventory.some(item => item.id === 5 && item.count === 60)); 57 const reloaded = await state(); 58 assert.equal(reloaded.inventory.Items.find(item => item.ID === 2).Ammo, 40); 59 await owner.wait('native reload completes', snapshot => !owner.local(snapshot).actions.includes(5)); 60 const before = await state(); 61 const deadline = Date.now() + 10000; 62 while (owner.state.selected[0] !== 50000 || owner.state.selected[1] !== before.target) { 63 assert(Date.now() < deadline, 'Native Tab did not select the controlled Leet'); 64 const selected = owner.state.selected; 65 await owner.key('Tab'); 66 await owner.wait('native target cycle advances', snapshot => 67 snapshot.selected[0] !== selected[0] || snapshot.selected[1] !== selected[1]); 68 } 69 const attack = owner.sequence; 70 await owner.key('KeyQ'); 71 await owner.wait('one real shot', () => owner.events.some(event => event.sequence > attack && 72 event.direction === 'framereceived' && event.opcode === '46002f16' && event.actor[1] === owner.local().id)); 73 await owner.key('KeyQ'); 74 await Promise.all(games.map(game => game.wait('native shot impact', snapshot => 75 snapshot.actors.some(actor => actor.id === before.target && actor.stats[27] === 9832)))); 76 const shot = await state(); 77 assert.equal(shot.health, 9832); 78 assert.equal(shot.inventory.Items.find(item => item.ID === 2).Ammo, 39); 79 assert.equal(shot.inventory.Items.find(item => item.ID === 5).Count, 60); 80 assert.equal(shot.fighting, 0); 81 await owner.wait('server shot recharge completes', () => Date.now() >= Date.parse(shot.recharge)); 82 await owner.save(output + '/shot'); 83 await observer.save(output + '/observer-shot'); 84 // Reconnect ends the fixture encounter; active-combat swaps are a separate TODO. 85 await owner.page.reload(); 86 await owner.ready(); 87 await owner.key('KeyI'); 88 await owner.page.keyboard.press('Control+1'); 89 } 90 await owner.click(867, 206, 'right'); 91 await owner.wait('same Vektor removed', snapshot => owner.local(snapshot).inventory.some(item => item.id === id && item.slot >= 48)); 92 assert.equal(owner.local().stats[21], 200); 93 assert.equal(owner.local().stats[221], nanoMaximum); 94 assert.equal((await state()).psychic, 0); 95 } 96 97 // QL125 fails locally; the retained QL75 bonus must apply exactly once again. 98 const before = await state(); 99 const sequence = owner.sequence; 100 await bagClick(owner, 4); 101 await owner.save(output + '/rejected-125'); 102 assert(!owner.events.some(event => event.sequence > sequence && event.direction === 'framesent' && event.opcode === '47537a24')); 103 assert.deepEqual((await state()).inventory, before.inventory); 104 await bagClick(owner, 3); 105 await owner.wait('same LIZARD re-worn', snapshot => owner.local(snapshot).inventory.some(item => item.id === 3 && item.slot === 6)); 106 assert.equal(owner.local().stats[21], 200); 107 assert.equal(owner.local().stats[221], nanoMaximum + 9); 108 assert.equal((await state()).psychic, 15); 109 await owner.key('KeyU'); 110 await owner.save(output + '/psychic-bonus'); 111 await owner.key('KeyU'); 112 await owner.page.reload(); 113 await owner.ready(); 114 } 115 const inventory = owner.local().inventory; 116 assert.deepEqual(inventory.map(item => [item.id, item.quality, ...item.templates, item.count]).sort((a, b) => a[0] - b[0]), [ 117 [1, 2, 124256, 124257, 1], [2, 37, 124258, 124259, 1], 118 [3, 75, 124260, 124261, 1], [4, 125, 124260, 124261, 1], 119 [5, 1, 21609, 21609, 60], [6, 1, 21605, 21605, 100], [7, 1, 21613, 21613, 100], 120 ]); 121 assert.equal(inventory.find(item => item.id === 3).slot, 6); 122 assert.equal(owner.local().stats[21], 200); 123 assert.deepEqual(observer.local().inventory, []); 124 assert(games.every(game => game.errors.length === 0)); 125 console.log('PASS:', phase, 'Vektor selected qualities, exact identities and LIZARD bonus retained'); 126 } finally { 127 try { await Promise.all(games.map((game, id) => game.save(output + '/final-' + id))); } 128 finally { await browser.close(); } 129 }