tools/client/test_engineer_robot.mjs (6440B)
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, phase] = 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 = [], evidence = {}; 12 const actor = (game, id) => game.state.actors.find(actor => actor.type === 50000 && actor.id === id); 13 try { 14 for (let index = 0; index < 2; index++) { 15 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 16 await context.addCookies([{name: 'ao_client', value: 'pet-cast-' + index, url}]); 17 const page = await context.newPage(), game = await GameTest.attach(page); 18 games.push(game); 19 await page.goto(url); 20 await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); 21 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 22 await game.snapshot(); 23 } 24 const [owner] = games; 25 const state = async () => (await owner.page.request.get(url + '/test-state')).json(); 26 const command = async text => { 27 await owner.key('Enter'); 28 await owner.page.keyboard.type('/pet ' + text); 29 await owner.key('Enter'); 30 }; 31 if (phase === 'live') { 32 await owner.key('KeyI'); 33 await owner.wait('startup crystal cell', state => state.grids.some(grid => grid.cells.some(cell => 34 cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy))); 35 const useSlot = async () => { 36 const cell = owner.state.grids.flatMap(grid => grid.cells).find(cell => cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy); 37 assert(cell, 'owned item must have a visible inventory cell'); 38 await owner.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2, 'right'); 39 }; 40 await useSlot(); 41 await owner.wait('crystal consumed', state => !owner.local(state).inventory.some(item => item.templates[0] === 43329)); 42 await owner.page.keyboard.press('Control+8'); 43 await owner.click(987, 96); 44 await owner.wait('uploaded Feeble Automaton program', state => state.grids.some(grid => grid.cells.some(cell => 45 cell.identity[0] === 53019 && cell.identity[1] === 43325 && !cell.dummy))); 46 const cell = owner.state.grids.flatMap(grid => grid.cells).find(cell => cell.identity[0] === 53019 && cell.identity[1] === 43325 && !cell.dummy); 47 await owner.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2, 'right'); 48 await owner.wait('formula creates a shell', state => owner.local(state).inventory.some(item => item.templates[0] === 43328)); 49 evidence.shell = await state(); 50 assert.equal(evidence.shell.cash, 6); 51 assert.equal(evidence.shell.nano, 10); 52 assert.equal(evidence.shell.pets, null); 53 if (!owner.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))) await owner.key('KeyI'); 54 await owner.snapshot(); 55 await useSlot(); 56 await Promise.all(games.map(game => game.wait('owned robot appears', () => actor(game, 1000001) && actor(game, 1).stats[458] === 1))); 57 await owner.wait('shell consumed', state => !owner.local(state).inventory.some(item => item.templates[0] === 43328)); 58 evidence.activated = await state(); 59 assert.deepEqual(evidence.activated.pets.map(pet => [pet.id, pet.owner, pet.program, pet.health]), [[1000001, 1, 43325, 40]]); 60 await Promise.all(games.map((game, index) => game.save(output + '/activated-' + index))); 61 if (!owner.state.camera.firstPerson) await owner.key('F8'); 62 await owner.hold('KeyA', 'face enemy along open street', state => owner.local(state).rotation[1] < -0.69); 63 await owner.click(...await owner.pick(50000, 1000000)); 64 await owner.wait('native enemy selection', state => state.selected[0] === 50000 && state.selected[1] === 1000000); 65 await command('attack'); 66 await Promise.all(games.map(game => game.wait('robot attacks', () => actor(game, 1000001).combatTarget === 1000000))); 67 await owner.waitFor('robot reaches enemy and takes retaliation', async () => { 68 const current = await state(); 69 return current.targetHealth === 16 && current.pets[0].health < 40; 70 }); 71 await owner.click(...await owner.pick(50000, 1000001)); 72 await owner.wait('robot is visibly pickable', state => state.selected[1] === 1000001); 73 await Promise.all(games.map((game, index) => game.save(output + '/combat-' + index))); 74 await owner.waitFor('robot kill credits owner', async () => (await state()).experience === 100); 75 evidence.kill = await state(); 76 assert.equal(evidence.kill.targetHealth, 0); 77 assert(evidence.kill.pets[0].health < 40, 'robot must take retaliation'); 78 await Promise.all(games.map(async game => { await game.page.reload(); await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); })); 79 } 80 await Promise.all(games.map(game => game.wait('robot ownership recovers', () => actor(game, 1000001) && actor(game, 1).stats[458] === 1))); 81 evidence.recovered = await state(); 82 assert.equal(evidence.recovered.nano, 10); 83 assert.equal(evidence.recovered.cash, 6); 84 assert.equal(evidence.recovered.experience, 100); 85 assert.equal(evidence.recovered.pets[0].program, 43325); 86 assert.equal(evidence.recovered.pets[0].expires, '0001-01-01T00:00:00Z'); 87 if (phase === 'cold') { 88 await command('terminate'); 89 await Promise.all(games.map(game => game.wait('robot removed', () => !actor(game, 1000001) && actor(game, 1).stats[458] === 0))); 90 evidence.removed = await state(); 91 assert.equal(evidence.removed.pets, null); 92 } 93 assert(games.every(game => game.errors.length === 0)); 94 console.log('PASS:', phase, 'native crystal upload, shell creation/activation, robot combat/rewards, reconnect/cold recovery and removal'); 95 } finally { 96 try { 97 await fs.mkdir(output, {recursive: true}); 98 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 99 await Promise.all(games.map((game, index) => game.save(output + '/final-' + index))); 100 } finally { await browser.close(); } 101 }