tools/client/test_mission_cash.mjs (5724B)
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: 'mission-cash', 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 const waitServer = (label, predicate) => game.waitFor(label, async () => predicate(await server())); 20 const cell = (type, slot) => game.state.grids.flatMap(grid => grid.cells).find(cell => 21 cell.identity[0] === type && cell.identity[1] === slot && !cell.dummy); 22 const click = async source => { 23 assert(source, 'native item cell is visible'); 24 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 25 }; 26 await page.goto(url); 27 await game.ready(); 28 if (!game.state.camera.firstPerson) await game.key('F8'); 29 await game.key('KeyI'); 30 const initial = evidence.initial = await server(); 31 if (initial.phase === 'hand-in') { 32 assert.equal(initial.inventory.Credits, 0); 33 assert.equal(initial.completed, false); 34 // Inspect the accepted contract before collecting its payable reward. 35 await page.keyboard.press('Control+4'); 36 await game.wait('accepted mission icon visible', state => state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 56003))); 37 const mission = game.state.grids.flatMap(grid => grid.cells).find(cell => cell.identity[0] === 56003); 38 await page.keyboard.down('Shift'); 39 try { await click(mission); } 40 finally { await page.keyboard.up('Shift'); } 41 await game.wait('mission description opens', state => state.regions.some(region => 42 region[0] === 760 && region[1] === 620 && region[2] === 1024 && region[3] === 768)); 43 // Move and expand the original information window to expose its rewards. 44 for (const [startX, startY, endX, endY] of [[870, 628, 450, 148], [602, 286, 1000, 730]]) { 45 const start = await game.point(startX, startY), end = await game.point(endX, endY); 46 await page.mouse.move(start.x, start.y); 47 await page.mouse.down(); 48 try { await page.mouse.move(end.x, end.y, {steps: 10}); } 49 finally { 50 const sequence = game.sequence; 51 await page.mouse.up(); 52 await game.waitInput(sequence, 1026); 53 } 54 await game.snapshot(); 55 } 56 await game.wait('mission reward text fits the window', state => state.regions.some(region => 57 region[0] === 340 && region[1] === 140 && region[2] > 900 && region[3] > 600)); 58 await game.save(output + '/reward-promise'); 59 await game.key('Escape'); 60 await page.keyboard.press('Control+4'); 61 const book = initial.inventory.Items.find(item => item.Template === 40822); 62 assert(book); 63 await click(cell(104, book.Slot - 16)); 64 await game.wait('original mission book picked', state => state.dragged[0] === 104); 65 await game.click(...await game.pick(56001, initial.booth), 'right'); 66 await waitServer('mission cash paid', state => state.completed && state.inventory.Credits === 500); 67 await game.wait('native completion cash received', state => game.local(state).stats[61] === 500); 68 evidence.completed = await server(); 69 assert.deepEqual(evidence.completed.inventory.Items.map(item => [item.ID, item.Template]), [[1, 209283], [4, 85596]]); 70 await game.save(output + '/completed'); 71 } else { 72 assert.equal(initial.inventory.Credits, 500); 73 assert.equal(initial.completed, true); 74 await game.click(...await game.pick(51035, initial.machine), 'right'); 75 await game.wait('original recovery and ammunition catalog', () => [0, 1, 2, 4].every(row => cell(111, row))); 76 for (const row of [0, 1, 2, 4]) await click(cell(111, row)); 77 await waitServer('four funded supply packages selected', state => state.purchases === 4); 78 await game.save(output + '/funded-basket'); 79 await game.click(873, 475); 80 await waitServer('only completion cash funds the supplies', state => !state.open && state.inventory.Credits === 500 - initial.supplyCost); 81 await game.wait('native purchase balance agrees', state => game.local(state).stats[61] === 500 - initial.supplyCost); 82 evidence.bought = await server(); 83 assert.deepEqual(evidence.bought.inventory.Items.map(item => [item.ID, item.Template, item.Quality, item.Count]), 84 [[1, 209283, 1, 1], [4, 85596, 1, 1], [5, 25812, 1, 50], [6, 85366, 1, 25], [7, 25820, 1, 50], [8, 21609, 1, 100]]); 85 await game.save(output + '/resupplied'); 86 } 87 const beforeReload = (await server()).inventory; 88 await page.reload(); 89 await game.ready(); 90 evidence.reconnected = await server(); 91 assert.deepEqual(evidence.reconnected.inventory, beforeReload); 92 assert.equal(game.local().stats[61], beforeReload.Credits); 93 assert.deepEqual(game.errors, []); 94 console.log('PASS:', initial.booth, initial.phase, 'native cash/items and page recovery'); 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 if (game) await game.save(output + '/final'); 100 } finally { await browser.close(); } 101 }