tools/client/test_first_aid.mjs (5772B)
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', headless: false, 9 args: ['--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 const server = async () => (await games[0].page.request.get(url + '/test-state')).json(); 13 const actor = (game, identity) => game.state.actors.find(actor => actor.type === 50000 && actor.id === identity); 14 const pending = (game, identity) => actor(game, identity)?.weapons.some(weapon => weapon.pending.length); 15 try { 16 for (let i = 0; i < 2; ++i) { 17 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 18 await context.addCookies([{name: 'ao_client', value: 'first-aid-' + i, url}]); 19 const page = await context.newPage(); 20 const game = await GameTest.attach(page); 21 games.push(game); 22 await page.goto(url); 23 await game.ready(); 24 assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); 25 } 26 const [owner, observer] = games; 27 const initial = await server(); 28 await Promise.all(games.map(game => game.wait('both clients and the Leet', () => 29 actor(game, initial.owner) && actor(game, initial.observer) && actor(game, initial.npc)))); 30 await owner.key('F1'); 31 await owner.key('KeyI'); 32 await owner.save(output + '/ready'); 33 assert.equal(owner.local().inventory[0].templates[0], initial.template); 34 assert.equal(owner.local().inventory[0].count, 3); 35 assert.equal(owner.local().stats[27], 200); 36 await owner.page.bringToFront(); 37 await owner.page.locator('#canvas').focus(); 38 const point = await owner.point(681, 65); 39 await owner.page.mouse.move(point.x, point.y); 40 41 // The fixture advances one ordinary Leet swing; all item actions use native browser input. 42 assert((await owner.page.request.post(url + '/test-attack')).ok()); 43 await Promise.all(games.map(game => game.wait('real incoming result before impact', () => pending(game, initial.npc)))); 44 evidence.pending = {server: await server(), clients: games.map(game => ({ 45 health: actor(game, initial.owner).stats[27], leet: actor(game, initial.npc)}))}; 46 assert(evidence.pending.server.health < 200); 47 assert(evidence.pending.clients.every(client => client.health === 200)); 48 const sequence = owner.sequence; 49 await Promise.all([ 50 owner.click(681, 65, 'right'), 51 ...games.map(async game => { 52 await game.wait('absolute heal while the real hit is pending', () => 53 actor(game, initial.owner).stats[27] !== 200 && pending(game, initial.npc)); 54 return {health: actor(game, initial.owner).stats[27], leet: actor(game, initial.npc)}; 55 }) 56 ]).then(([, ...clients]) => { evidence.healedBeforeImpact = clients; }); 57 await owner.wait('one First Aid charge', () => owner.local().inventory[0].count === 2); 58 const healed = await server(); 59 evidence.healed = healed; 60 const [low, high] = initial.template === 116629 ? [5, 7] : [10, 12]; 61 assert(healed.health >= evidence.pending.server.health + low && healed.health <= evidence.pending.server.health + high); 62 if (initial.template === 116629) { 63 assert(healed.nano >= initial.nano + 5 && healed.nano <= initial.nano + 7); 64 await owner.wait('startup kit restored native nano', () => owner.local().stats[214] === healed.nano); 65 } else assert.equal(healed.nano, initial.nano); 66 assert(evidence.healedBeforeImpact.every(client => client.health === healed.health)); 67 for (let i = 0; i < games.length; ++i) 68 assert.deepEqual(evidence.healedBeforeImpact[i].leet.weapons, evidence.pending.clients[i].leet.weapons); 69 assert.equal(healed.inventory.Items[0].Count, 2); 70 assert.notEqual(healed.lock, '0001-01-01T00:00:00Z'); 71 assert.equal(healed.fighting, initial.owner); 72 assert.equal(healed.nextAttack, evidence.pending.server.nextAttack); 73 assert.equal(healed.rechargeUntil, evidence.pending.server.rechargeUntil); 74 await Promise.all(games.map(game => game.wait('healed HP and drained real result', () => 75 actor(game, initial.owner).stats[27] === healed.health && !pending(game, initial.npc)))); 76 evidence.afterImpact = games.map(game => ({health: actor(game, initial.owner).stats[27], leet: actor(game, initial.npc)})); 77 for (let i = 0; i < games.length; ++i) { 78 assert.equal(evidence.afterImpact[i].leet.fighting, evidence.pending.clients[i].leet.fighting); 79 assert.equal(evidence.afterImpact[i].leet.combatTarget, evidence.pending.clients[i].leet.combatTarget); 80 } 81 const resultPackets = owner.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' && 82 ['46002f16', '5c654b28'].includes(event.opcode)); 83 assert.equal(resultPackets.length, 0, 'Healing invented another weapon result'); 84 await owner.click(681, 65, 'right'); 85 assert.equal((await server()).inventory.Items[0].Count, 2); 86 assert.equal(owner.local().inventory[0].count, 2); 87 await Promise.all(games.map((game, i) => game.save(output + '/verified-' + i))); 88 console.log('PASS:', initial.template, 'real pending hit, complete resource effects, one charge and lock, both HP snapshots agree after impact; no extra weapon results or attack-clock changes'); 89 } finally { 90 await fs.mkdir(output, {recursive: true}); 91 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2)); 92 try { await Promise.all(games.map((game, i) => game.save(output + '/final-' + i))); } 93 finally { await browser.close(); } 94 }