tools/client/test_fallback_placement.mjs (3928B)
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 state = async () => (await games[0].page.request.get(url+'/test-state')).json(); 13 const actor = (game, id) => game.state.actors.find(actor => actor.type === 50000 && actor.id === id); 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: 'fallback-'+index, url}]); 18 const page = await context.newPage(); 19 const game = await GameTest.attach(page); 20 games.push(game); 21 await page.goto(url); 22 await game.ready(); 23 } 24 const [owner] = games; 25 evidence.initial = await state(); 26 assert.equal(evidence.initial.health, 20); 27 if (!owner.state.camera.firstPerson) await owner.key('F8'); 28 29 // Tilt the real camera down the shore before asking the native picker to select. 30 const cameraPoint = await owner.point(512, 300); 31 let mouseY = cameraPoint.y, sequence = owner.sequence; 32 await owner.page.mouse.move(cameraPoint.x, mouseY); 33 await owner.page.mouse.down({button: 'right'}); 34 try { 35 await owner.waitInput(sequence, 1025); 36 const deadline = Date.now()+10000; 37 for (;;) { 38 await owner.snapshot(); 39 const screenY = actor(owner, evidence.initial.enemy).screenPosition[1]; 40 if (screenY >= 350 && screenY <= 540) break; 41 assert(Date.now() < deadline, 'Camera did not frame the Leet above chat'); 42 mouseY += screenY > 540 ? 20 : -20; 43 await owner.page.mouse.move(cameraPoint.x, mouseY, {steps: 2}); 44 } 45 } finally { 46 sequence = owner.sequence; 47 await owner.page.mouse.up({button: 'right'}); 48 } 49 await owner.waitInput(sequence, 1026); 50 const pixel = await owner.pick(50000, evidence.initial.enemy); 51 await owner.click(...pixel); 52 await owner.wait('ordinary target selection', snapshot => snapshot.selected[1] === evidence.initial.enemy); 53 await owner.walk(...evidence.initial.near); 54 evidence.approach = {server: await state(), native: owner.local().position}; 55 assert(Math.hypot(...evidence.approach.server.ownerPosition.map((value, axis) => value-evidence.approach.native[axis])) < .5); 56 await owner.save(output+'/approach'); 57 await owner.key('KeyQ'); 58 const deadline = Date.now()+12000; 59 for (;;) { 60 evidence.hit = await state(); 61 if (evidence.hit.health < 20) break; 62 assert(Date.now() < deadline, 'Ordinary melee did not damage the fallback'); 63 await owner.snapshot(); 64 } 65 await owner.key('KeyQ'); 66 await owner.wait('ordinary stop attack', () => owner.local().combatTarget === 0); 67 evidence.stopped = await state(); 68 await Promise.all(games.map(game => game.wait('both native replicas retain the melee hit', () => 69 actor(game, evidence.initial.enemy)?.stats[27] === evidence.stopped.health))); 70 evidence.clients = games.map(game => ({position: actor(game, evidence.initial.enemy).position, 71 health: actor(game, evidence.initial.enemy).stats[27]})); 72 assert(games.every(game => game.errors.length === 0)); 73 console.log('PASS: real walking, native picking and ordinary melee reach the grounded generated fallback on both clients'); 74 } finally { 75 try { 76 await fs.mkdir(output, {recursive: true}); 77 await fs.writeFile(output+'/verification.json', JSON.stringify(evidence, null, 2)); 78 await Promise.all(games.map((game, index) => game.save(output+'/final-'+index))); 79 } finally { await browser.close(); } 80 }