tools/client/test_faction_insurance.mjs (9383B)
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 state = async () => (await games[0].page.request.get(url + '/test-state')).json(); 13 14 async function face(game, position) { 15 const deadline = Date.now() + 8000; 16 for (;;) { 17 await game.snapshot(); 18 const actor = game.local(); 19 const yaw = Math.atan2(position[0] - actor.position[0], position[2] - actor.position[2]); 20 const delta = yaw - 2 * Math.atan2(actor.rotation[1], actor.rotation[3]); 21 const angle = Math.atan2(Math.sin(delta), Math.cos(delta)); 22 if (Math.abs(angle) < 0.06) return; 23 assert(Date.now() < deadline, 'Native turn did not reach the interaction target'); 24 const sequence = game.sequence; 25 await game.page.keyboard.press(angle > 0 ? 'KeyD' : 'KeyA', 26 {delay: Math.min(150, Math.max(20, Math.abs(angle) / 3.5 * 1000))}); 27 await game.waitInput(sequence, 769); 28 } 29 } 30 31 async function frameEnemy(game, identity) { 32 const enemy = () => game.state.actors.find(actor => actor.type === 50000 && actor.id === identity); 33 if (enemy().screenPosition[1] >= 200 && enemy().screenPosition[1] <= 700) return; 34 const point = await game.point(512, 300); 35 let mouseY = point.y; 36 let sequence = game.sequence; 37 const deadline = Date.now() + 10000; 38 await game.page.mouse.move(point.x, mouseY); 39 await game.page.mouse.down({button: 'right'}); 40 try { 41 await game.waitInput(sequence, 1025); 42 for (;;) { 43 await game.snapshot(); 44 const y = enemy().screenPosition[1]; 45 if (y >= 200 && y <= 700) break; 46 assert(Date.now() < deadline, 'Encounter did not frame above the native toolbar'); 47 mouseY += y > 700 ? 20 : -20; 48 await game.page.mouse.move(point.x, mouseY, {steps: 2}); 49 } 50 } finally { 51 sequence = game.sequence; 52 await game.page.mouse.up({button: 'right'}); 53 await game.waitInput(sequence, 1026); 54 } 55 } 56 57 try { 58 for (const index of [0, 1]) { 59 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 60 await context.addCookies([{name: 'ao_client', value: 'faction-route-' + index, url}]); 61 const page = await context.newPage(), game = await GameTest.attach(page); 62 games.push(game); 63 await page.goto(url); 64 await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); 65 } 66 evidence.initial = await state(); 67 if (evidence.initial.phase === 'warm') { 68 for (const [index, game] of games.entries()) { 69 const config = evidence.initial.players[index]; 70 const [x, , z] = config.arrival; 71 await face(game, config.scannerPosition); 72 await game.click(...await game.pick(51005, config.scannerID), 'right'); 73 await game.wait('native insured message', () => game.events.some(event => event.direction === 'framereceived' && 74 event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 79)); 75 evidence['saved' + index] = (await state()).players[index]; 76 assert.equal(evidence['saved' + index].scanner, config.scannerID); 77 await game.save(output + '/saved-' + index); 78 79 // Real attack input starts retaliation; one server hit ends this controlled encounter. 80 const enemy = game.state.actors.find(actor => actor.type === 50000 && actor.id === config.enemy); 81 assert(enemy); 82 await face(game, enemy.position); 83 await frameEnemy(game, config.enemy); 84 await game.click(...await game.pick(50000, config.enemy)); 85 const sequence = game.sequence; 86 await game.key('KeyQ'); 87 await game.wait('native attack request', () => game.events.some(event => event.sequence > sequence && 88 event.direction === 'framesent' && event.opcode === '28494070')); 89 await game.wait('saved city resurrection', snapshot => game.local(snapshot)?.playfield === config.city && 90 game.local(snapshot)?.stats[27] === 1 && game.lifecycle.some(event => event.sequence > sequence && 91 event.direction === 'framereceived' && event.opcode === '445f2a0b'), 30000); 92 await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); 93 evidence['returned' + index] = (await state()).players[index]; 94 const returned = evidence['returned' + index]; 95 assert.equal(returned.health, 1); 96 assert.equal(returned.returnDoor, null); 97 assert.equal(returned.inventory.Items.length, 1); 98 assert.equal(returned.inventory.Items[0].Slot, 6); 99 assert.deepEqual(returned.inventory.Reclaim.map(entry => entry.Item), config.inventory.Items.slice(1)); 100 assert(Math.hypot(returned.position[0] - x, returned.position[2] - z) < 0.5); 101 assert.deepEqual(game.lifecycle.filter(event => event.sequence > sequence && event.actor?.[1] === config.id) 102 .map(event => [event.direction, event.opcode, Buffer.from(event.hex, 'hex').readUInt32BE(29)]), [ 103 ['framereceived', '5e477770', 99], ['framesent', '5e477770', 152], ['framereceived', '445f2a0b', 1], 104 ]); 105 await game.save(output + '/returned-' + index); 106 } 107 108 // Walk to each actual booth, letting the ordinary reclaim deadline expire. 109 for (const index of [1, 0]) { 110 const game = games[index]; 111 const config = evidence.initial.players[index]; 112 const streets = [[[487, 401]], [[299, 320], [320, 320], [318, 303]]][index]; 113 for (const [x, z] of streets) { 114 await game.walk(x, z); 115 assert.equal(game.local().playfield, config.city); 116 assert(Math.hypot(x - game.local().position[0], z - game.local().position[2]) < 0.35); 117 } 118 const returned = evidence['returned' + index]; 119 await game.wait('ordinary reclaim availability', () => returned.inventory.Reclaim.every(entry => 120 Date.now() >= Date.parse(entry.AvailableAt)), 90000); 121 await face(game, config.reclaimPosition); 122 await game.click(...await game.pick(51059, config.reclaimID), 'right'); 123 await game.wait('three reclaim sources', snapshot => snapshot.grids.flatMap(grid => grid.cells) 124 .filter(cell => cell.identity[0] === 106 && !cell.dummy).length === 3); 125 for (const [slot, item] of config.inventory.Items.slice(1).entries()) { 126 const cell = game.state.grids.flatMap(grid => grid.cells).find(cell => 127 cell.identity[0] === 106 && cell.identity[1] === slot && !cell.dummy); 128 assert(cell); 129 await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 130 await game.wait('exact instance reclaimed', snapshot => game.local(snapshot).inventory.some(proxy => 131 proxy.id === item.ID && proxy.count === item.Count && proxy.slot === 48 + slot)); 132 } 133 await game.click(170, 8); 134 await game.save(output + '/collected-' + index); 135 await game.page.reload(); 136 await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); 137 } 138 } 139 140 // Page replay and a fresh server must retain the same four instances per owner. 141 evidence.final = await state(); 142 for (const [index, game] of games.entries()) { 143 const player = evidence.final.players[index]; 144 assert.equal(player.field, player.city); 145 assert.equal(player.scanner, player.scannerID); 146 assert.equal(player.insured, 38650); 147 assert.equal(player.experience, 0); 148 assert.equal(player.stats.ImprovementPoints, 1500); 149 assert.equal(player.inventory.Credits, 37); 150 assert.equal(player.inventory.Reclaim, null); 151 assert.deepEqual(player.inventory.Items.map(item => [item.ID, item.Template, item.Quality, item.Slot, item.Count]), [ 152 [index * 4 + 1, 209283, 1, 6, 1], [index * 4 + 2, 42640, 1, 64, 1], 153 [index * 4 + 3, 25812, 1, 65, 50], [index * 4 + 4, 21609, 1, 66, 100], 154 ]); 155 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]).sort((a, b) => a[0] - b[0]), [ 156 [index * 4 + 1, 6, 1, 1], [index * 4 + 2, 48, 1, 1], 157 [index * 4 + 3, 49, 1, 50], [index * 4 + 4, 50, 1, 100], 158 ]); 159 assert.equal(game.lifecycleDropped, 0); 160 if (evidence.initial.phase === 'cold') { 161 await game.key('KeyI'); 162 await game.wait('retained inventory opens after cold loading', snapshot => snapshot.grids.some(grid => 163 grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))); 164 await game.save(output + '/inventory-' + index); 165 } 166 } 167 assert(games.every(game => game.errors.length === 0)); 168 console.log('PASS:', evidence.initial.phase, 'controlled faction save, death and exact reclaim ownership'); 169 } finally { 170 try { 171 evidence.final = await state(); 172 await fs.mkdir(output, {recursive: true}); 173 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 174 await Promise.all(games.map((game, index) => game.save(output + '/final-' + index))); 175 } finally { await browser.close(); } 176 }