tools/client/test_npc_recovery.mjs (5238B)
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 const pending = game => actor(game, 1)?.weapons.some(weapon => weapon.pending.length); 14 try { 15 for (let index = 0; index < 2; index++) { 16 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 17 await context.addCookies([{name: 'ao_client', value: 'npc-recovery-' + index, url}]); 18 const page = await context.newPage(), game = await GameTest.attach(page); 19 games.push(game); 20 await page.goto(url); 21 await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); 22 } 23 const [owner] = games; 24 const server = async () => (await owner.page.request.get(url + '/test-state')).json(); 25 evidence.initial = await server(); 26 const npc = evidence.initial.npc; 27 await Promise.all(games.map(game => game.wait('both players and the injured Leet', () => actor(game, 1) && actor(game, 2) && actor(game, npc)))); 28 const [x, y] = await owner.pick(50000, npc); 29 await owner.click(x, y); 30 assert((await owner.page.request.post(url + '/test-engage')).ok()); 31 await Promise.all(games.map(game => game.wait('combat baseline before any weapon outcome', () => 32 actor(game, npc).combatTarget === 1 && actor(game, npc).stats[27] === 10))); 33 await owner.key('q'); 34 await owner.wait('real native attack request', () => owner.events.some(event => 35 event.direction === 'framesent' && event.opcode === '28494070')); 36 assert((await owner.page.request.post(url + '/test-swing')).ok()); 37 await Promise.all(games.map(game => game.wait('first real weapon result pending', () => pending(game)))); 38 evidence.pending = {server: await server(), clients: games.map(game => ({health: actor(game, npc).stats[27], weapons: actor(game, 1).weapons}))}; 39 assert.equal(evidence.pending.server.health, 5); 40 assert(evidence.pending.clients.every(client => client.health === 10)); 41 42 // A controlled phase advances the ordinary three-point combat recovery once. 43 assert((await owner.page.request.post(url + '/test-recover')).ok()); 44 evidence.recovered = await server(); 45 assert.deepEqual(evidence.recovered, {...evidence.pending.server, health: 8, counter: 60}); 46 await Promise.all(games.map(game => game.wait('recovery snapshot or first impact', () => 47 actor(game, npc).stats[27] === 8 || !pending(game)))); 48 evidence.included = games.map(game => ({health: actor(game, npc).stats[27], weapons: actor(game, 1).weapons})); 49 await Promise.all(games.map(game => game.wait('first impact consumed', () => !pending(game)))); 50 evidence.first = {server: await server(), clients: games.map(game => ({health: actor(game, npc).stats[27], weapons: actor(game, 1).weapons}))}; 51 await Promise.all(games.map((game, index) => game.save(output + '/first-' + index))); 52 53 assert((await owner.page.request.post(url + '/test-swing')).ok()); 54 await Promise.all(games.map(game => game.wait('second real weapon result pending', () => pending(game)))); 55 await Promise.all(games.map(game => game.wait('second impact consumed', () => !pending(game)))); 56 evidence.second = {server: await server(), clients: games.map(game => ({health: actor(game, npc).stats[27], weapons: actor(game, 1).weapons}))}; 57 await Promise.all(games.map((game, index) => game.save(output + '/second-' + index))); 58 assert.equal(evidence.second.server.health, 3); 59 assert(evidence.second.clients.every(client => client.health === 3), 'Native NPC HP reached zero before the real death because combat recovery was withheld'); 60 assert(evidence.first.clients.every(client => client.health === 8)); 61 assert(evidence.included.every(client => client.health === 8)); 62 evidence.included.forEach((client, index) => assert.deepEqual(client.weapons, evidence.pending.clients[index].weapons)); 63 assert.deepEqual(evidence.first.server, evidence.recovered); 64 65 assert((await owner.page.request.post(url + '/test-swing')).ok()); 66 await Promise.all(games.map(game => game.wait('real death creates the corpse', state => 67 state.actors.some(actor => actor.type === 51050 && actor.id === npc)))); 68 evidence.death = await server(); 69 assert.equal(evidence.death.health, 0); 70 assert(games.every(game => game.errors.length === 0)); 71 console.log('PASS: authoritative NPC recovery survives queued impacts, both clients keep three HP until the real final death'); 72 } finally { 73 try { 74 await fs.mkdir(output, {recursive: true}); 75 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 76 await Promise.all(games.map((game, index) => game.save(output + '/final-' + index))); 77 } finally { await browser.close(); } 78 }