tools/client/test_player_recovery.mjs (5228B)
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', 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 actor = (game, id) => game.state.actors.find(actor => actor.type === 50000 && actor.id === id); 13 try { 14 for (let index = 0; index < 2; index++) { 15 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 16 await context.addCookies([{name: 'ao_client', value: 'player-recovery-' + index, url}]); 17 const page = await context.newPage(), game = await GameTest.attach(page); 18 games.push(game); 19 await page.goto(url); 20 await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); 21 } 22 const [owner, observer] = games; 23 const server = async () => (await owner.page.request.get(url + '/test-state')).json(); 24 evidence.initial = await server(); 25 const npc = evidence.initial.npc; 26 const pending = game => actor(game, npc)?.weapons.some(weapon => weapon.pending.length); 27 const clients = () => games.map(game => ({health: actor(game, 1).stats[27], weapons: actor(game, npc).weapons})); 28 await Promise.all(games.map(game => game.wait('both players and Leet', () => actor(game, 1) && actor(game, 2) && actor(game, npc)))); 29 await owner.key('F1'); 30 const [x, y] = await observer.pick(50000, 1); 31 await observer.click(x, y); 32 assert((await owner.page.request.post(url + '/test-engage')).ok()); 33 await Promise.all(games.map(game => game.wait('incoming combat baseline', () => 34 actor(game, npc).combatTarget === 1 && actor(game, 1).stats[27] === 16))); 35 assert((await owner.page.request.post(url + '/test-swing')).ok()); 36 await Promise.all(games.map(game => game.wait('first real hit pending', () => pending(game)))); 37 evidence.pending = {server: await server(), clients: clients()}; 38 assert.equal(evidence.pending.server.health, 8); 39 assert(evidence.pending.clients.every(client => client.health === 16)); 40 41 assert((await owner.page.request.post(url + '/test-recover')).ok()); 42 evidence.recovered = await server(); 43 assert.deepEqual(evidence.recovered, {...evidence.pending.server, health: 11, 44 counter: evidence.initial.bodyDevelopment === 80 ? 56 : 58}); 45 await Promise.all(games.map(game => game.wait('recovery snapshot or first impact', () => actor(game, 1).stats[27] === 11 || !pending(game)))); 46 evidence.included = clients(); 47 await Promise.all(games.map(game => game.wait('first impact consumed', () => !pending(game)))); 48 evidence.first = {server: await server(), clients: clients()}; 49 assert((await owner.page.request.post(url + '/test-swing')).ok()); 50 await Promise.all(games.map(game => game.wait('second hit pending', () => pending(game)))); 51 await Promise.all(games.map(game => game.wait('second impact consumed', () => !pending(game)))); 52 evidence.second = {server: await server(), clients: clients()}; 53 await Promise.all(games.map((game, index) => game.save(output + '/second-' + index))); 54 assert.equal(evidence.second.server.health, 3); 55 assert(evidence.second.clients.every(client => client.health === 3), 'Native player HP reached zero while authoritative health remained three'); 56 assert(evidence.first.clients.every(client => client.health === 11)); 57 assert.deepEqual(evidence.first.server, evidence.recovered); 58 assert(evidence.included.every(client => client.health === 11)); 59 evidence.included.forEach((client, index) => assert.deepEqual(client.weapons, evidence.pending.clients[index].weapons)); 60 61 assert((await owner.page.request.post(url + '/test-stop')).ok()); 62 await Promise.all(games.map(game => game.wait('stopped attacker and retained health', () => 63 actor(game, npc).combatTarget === 0 && actor(game, 1).stats[27] === 3))); 64 evidence.stopped = await server(); 65 assert.equal(evidence.stopped.target, 0); 66 await Promise.all(games.map(async game => { await game.page.reload(); await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); })); 67 await Promise.all(games.map(game => game.wait('replayed owner health', () => actor(game, 1)?.stats[27] === 3))); 68 await owner.page.waitForFunction(() => touchRegion(500, 400)?.world === true); 69 evidence.replayed = {server: await server(), clients: clients()}; 70 assert.equal(evidence.replayed.server.health, 3); 71 assert(games.every(game => game.errors.length === 0)); 72 console.log('PASS: player recovery survives queued incoming impacts, both clients retain three HP through stop and page/cold replay'); 73 } finally { 74 try { 75 await fs.mkdir(output, {recursive: true}); 76 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 77 await Promise.all(games.map((game, index) => game.save(output + '/final-' + index))); 78 } finally { await browser.close(); } 79 }