tools/client/test_training_corpses.mjs (5537B)
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', 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 let game; 12 const evidence = {}; 13 try { 14 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 15 await context.addCookies([{name: 'ao_client', value: 'training-corpse', url}]); 16 const page = await context.newPage(); 17 game = await GameTest.attach(page); 18 const server = async () => (await page.request.get(url + '/test-state')).json(); 19 await page.goto(url); 20 await game.ready(); 21 if (!game.state.camera.firstPerson) await game.key('F8'); 22 evidence.before = await server(); 23 async function frame(type, lower, upper) { 24 await game.wait("target appears", state => state.actors.some(actor => actor.type === type && actor.id === evidence.before.target)); 25 const cameraPoint = await game.point(512, 300); 26 let mouseY = cameraPoint.y, sequence = game.sequence; 27 await game.page.mouse.move(cameraPoint.x, mouseY); 28 await game.page.mouse.down({button: 'right'}); 29 try { 30 await game.waitInput(sequence, 1025); 31 const deadline = Date.now() + 10000; 32 for (;;) { 33 await game.snapshot(); 34 const y = game.state.actors.find(actor => actor.type === type && actor.id === evidence.before.target).screenPosition[1]; 35 if (y >= lower && y <= upper) break; 36 assert(Date.now() < deadline, 'Camera did not frame the corpse above chat'); 37 mouseY += y > upper ? 10 : -10; 38 await game.page.mouse.move(cameraPoint.x, mouseY, {steps: 2}); 39 // Recenter the drag before it leaves the canvas on steep viewpoints. 40 if (mouseY < 100 || mouseY > 600) { 41 sequence = game.sequence; 42 await game.page.mouse.up({button: 'right'}); 43 await game.waitInput(sequence, 1026); 44 mouseY = cameraPoint.y; 45 await game.page.mouse.move(cameraPoint.x, mouseY); 46 sequence = game.sequence; 47 await game.page.mouse.down({button: 'right'}); 48 await game.waitInput(sequence, 1025); 49 } 50 } 51 } finally { 52 sequence = game.sequence; 53 await game.page.mouse.up({button: 'right'}); 54 } 55 await game.waitInput(sequence, 1026); 56 } 57 if (evidence.before.health > 0) { 58 await frame(50000, 500, 550); 59 await game.save(output + '/living'); 60 await game.click(...await game.pick(50000, evidence.before.target)); 61 await game.wait('human opponent selected', state => state.selected[0] === 50000 && state.selected[1] === evidence.before.target); 62 await game.key('q'); 63 await game.waitFor('native attack killed human opponent', async () => (await server()).health === 0); 64 evidence.killed = await server(); 65 assert(evidence.killed.experience > evidence.before.experience, 'Human kill granted no XP'); 66 evidence.before = evidence.killed; 67 await game.wait('human corpse appears', state => state.actors.some(actor => actor.type === 51050 && actor.id === evidence.before.target)); 68 await frame(51050, 400, 450); 69 await game.save(output + '/warm-corpse'); 70 await page.reload(); 71 await game.ready(); 72 if (!game.state.camera.firstPerson) await game.key('F8'); 73 } 74 // Cold replay must retain both a visible body and its original loot. 75 await frame(51050, 400, 450); 76 await game.save(output + '/corpse'); 77 await game.click(...await game.pick(51050, evidence.before.target), 'right'); 78 await game.wait('original training loot appears', state => state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 107 && !cell.dummy))); 79 await game.save(output + '/loot'); 80 for (const item of evidence.before.items) { 81 await game.wait('exact corpse source icon', state => state.grids.some(grid => grid.cells.some(cell => 82 cell.identity[0] === 107 && cell.identity[1] === item.Slot && !cell.dummy))); 83 const cell = game.state.grids.flatMap(grid => grid.cells).find(cell => 84 cell.identity[0] === 107 && cell.identity[1] === item.Slot && !cell.dummy); 85 await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 86 await game.waitFor('exact source transferred', async () => !(await server()).items.some(source => source.ID === item.ID)); 87 } 88 await game.waitFor('exact original loot collected', async () => (await server()).items.length === 0); 89 evidence.collected = await server(); 90 assert.deepEqual(evidence.collected.inventory.Items, evidence.before.items.map((item, index) => ({...item, Slot: 64 + index}))); 91 assert.equal(evidence.collected.inventory.Credits, evidence.before.credits); 92 await page.reload(); 93 await game.ready(); 94 evidence.reconnected = await server(); 95 assert.deepEqual(evidence.reconnected.inventory, evidence.collected.inventory); 96 assert.deepEqual(game.errors, []); 97 console.log('PASS:', evidence.before.route, 'corpse picked and retained exact loot through reconnect'); 98 } finally { 99 try { 100 await fs.mkdir(output, {recursive: true}); 101 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 102 if (game) await game.save(output + '/final'); 103 } finally { await browser.close(); } 104 }