tools/client/test_city_doors.mjs (5083B)
1 import assert from 'node:assert/strict'; 2 import {createRequire} from 'node:module'; 3 import fs from 'node:fs/promises'; 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', args: [ 9 '--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 = []; 12 let expected; 13 const state = async () => (await games[0].page.request.get(url + '/test-state')).json(); 14 const door = game => game.state.actors.find(actor => actor.type === 51016 && actor.id === expected.door.ID)?.door; 15 async function pose(game, open) { 16 await game.wait(open ? 'shared door fully open' : 'shared door fully closed', () => { 17 const value = door(game); 18 return value && value.latched && !!(value.flags & 128) === open && 19 Math.abs(value.time - (open ? value.duration : 0)) < .001; 20 }); 21 } 22 async function save(label) { 23 await Promise.all(games.map((game, index) => game.save(`${output}/${label}-${index}`))); 24 await fs.writeFile(`${output}/${label}-server.json`, JSON.stringify(await state(), null, 2) + '\n'); 25 } 26 try { 27 for (let index = 0; index < 2; ++index) { 28 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 29 await context.addCookies([{name: 'ao_client', value: `city-door-${index}`, url}]); 30 const page = await context.newPage(), game = await GameTest.attach(page); 31 games.push(game); 32 await page.goto(url); 33 await game.ready(); 34 if (!game.state.camera.firstPerson) await game.key('F8'); 35 } 36 expected = await state(); 37 const [traveler, observer] = games; 38 await Promise.all(games.map(game => pose(game, false))); 39 assert(door(traveler).duration > 0, 'Selected route has no moving door; inspect its original model'); 40 await save('closed'); 41 42 // Approach through normal input, then replay an observer while the door is open. 43 for (const point of expected.approach) await traveler.walk(point[0], point[2]); 44 await Promise.all(games.map(game => pose(game, true))); 45 assert.equal((await state()).open, true); 46 await save('approach'); 47 await observer.page.reload(); 48 await observer.ready(); 49 if (!observer.state.camera.firstPerson) await observer.key('F8'); 50 await pose(observer, true); 51 await save('open-reconnect'); 52 53 // Withdrawal closes both replicas. Reconnect must preserve the closed pose too. 54 await traveler.walk(expected.start[0], expected.start[2]); 55 await Promise.all(games.map(game => pose(game, false))); 56 assert.equal((await state()).open, false); 57 await save('withdrawn'); 58 await observer.page.reload(); 59 await observer.ready(); 60 if (!observer.state.camera.firstPerson) await observer.key('F8'); 61 await pose(observer, false); 62 await save('closed-reconnect'); 63 64 // Cross the actual portal and compare native arrival against authoritative zoning. 65 for (const point of expected.approach) await traveler.walk(point[0], point[2]); 66 await Promise.all(games.map(game => pose(game, true))); 67 if (expected.players[0].playfield === 540) { 68 // The original Old Athen threshold also stops the native client without 69 // server corrections. Clear its raised lip with ordinary jump input. 70 const sequence = traveler.sequence; 71 await Promise.all([ 72 traveler.walk(expected.door.Position[0], expected.door.Position[2]), 73 (async () => { 74 await traveler.wait('walk forward into the Old Athen doorway', () => traveler.events.some(event => 75 event.sequence > sequence && event.kind === 'browser-input' && event.type === 'keydown' && event.code === 'KeyW')); 76 await traveler.key('Space'); 77 })(), 78 ]); 79 } else { 80 await traveler.walk(expected.door.Position[0], expected.door.Position[2]); 81 } 82 await traveler.wait('native destination after passage', () => traveler.local()?.playfield === expected.destination, 45000); 83 await traveler.ready(); 84 await pose(observer, false); 85 // Exterior door transforms are height anchors; wait for normal landing. 86 await traveler.waitFor('native and server arrival settle together', async () => { 87 await traveler.snapshot(); 88 const current = await state(); 89 return Math.hypot(...traveler.local().position.map((value, axis) => value - current.players[0].position[axis])) < .5; 90 }); 91 const arrived = await state(); 92 assert.equal(arrived.players[0].playfield, expected.destination); 93 assert.equal(arrived.players[1].playfield, expected.players[1].playfield); 94 assert.equal(arrived.open, false); 95 assert(Math.hypot(...traveler.local().position.map((value, axis) => value - arrived.players[0].position[axis])) < .5); 96 await save('passage'); 97 assert(games.every(game => game.errors.length === 0)); 98 console.log('PASS: shared approach, opening, withdrawal, reconnect and passage; native duration', door(observer).duration); 99 } finally { 100 try { if (games.length) await save('final'); } 101 finally { await browser.close(); } 102 }