tools/client/test_basic_shop_weapons.mjs (9000B)
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: 'basic-shop-weapons', 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 cell = (type, slot) => game.state.grids.flatMap(grid => grid.cells).find(cell => 20 cell.identity[0] === type && cell.identity[1] === slot && !cell.dummy); 21 const click = async (source, button = 'left') => { 22 assert(source, 'expected native inventory/catalog cell is present'); 23 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2, button); 24 }; 25 const waitServer = (label, predicate) => game.waitFor(label, async () => predicate(await server())); 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 evidence.before = await server(); 31 const seenCatalog = new Set(); 32 const catalogGrid = state => state.grids.find(grid => grid.rows === 4 && grid.cells.some(cell => cell.identity[0] === 111)); 33 const catalogRow = async index => { 34 while (!catalogGrid(game.state).cells.some(cell => cell.identity[0] === 111 && cell.identity[1] === index && !cell.dummy)) { 35 const grid = catalogGrid(game.state); 36 const source = grid.cells[0]; 37 const point = await game.point((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 38 await page.mouse.move(point.x, point.y); 39 await page.mouse.wheel(0, index < grid.firstRow * 3 ? -120 : 120); 40 await game.wait('weapon catalog scrolls', state => catalogGrid(state)?.firstRow !== grid.firstRow); 41 } 42 for (const source of catalogGrid(game.state).cells) { 43 if (source.identity[0] === 111 && !source.dummy) seenCatalog.add(source.identity[1]); 44 } 45 return catalogGrid(game.state).cells.find(cell => cell.identity[0] === 111 && cell.identity[1] === index && !cell.dummy); 46 }; 47 const open = async keyboard => { 48 await game.click(...await game.pick(51035, evidence.before.machine), keyboard ? 'left' : 'right'); 49 if (keyboard) await game.key('KeyE'); 50 await game.wait('basic weapon catalog opens', catalogGrid); 51 await catalogRow(0); 52 assert((await server()).open); 53 }; 54 await open(false); 55 await game.save(output + '/catalog'); 56 57 // Buy all six QL10 families, two bullet packages and energy ammo, selling loot. 58 for (const [count, index] of [0, 0, 1, 5, 7, 9, 11, 13, 15].entries()) { 59 await click(await catalogRow(index)); 60 await waitServer('purchase row selected', state => state.purchases === count + 1); 61 } 62 for (let index = 0; index < 16; index++) assert(seenCatalog.has(index)); 63 evidence.catalogRows = [...seenCatalog].sort((a, b) => a - b); 64 await game.save(output + '/catalog-bottom'); 65 await click(cell(104, 48)); 66 await game.wait('sale loot picked', state => state.dragged[0] === 104); 67 await game.click(860, 412); 68 await waitServer('loot offered', state => state.sales === 1); 69 await game.save(output + '/basket'); 70 await game.click(873, 475); 71 await waitServer('weapons committed', state => !state.open && state.inventory.Items.length === 9); 72 evidence.purchased = await server(); 73 assert.equal(evidence.purchased.inventory.Credits, evidence.before.playfield === 1183 ? 28293 : 26932); 74 assert.deepEqual(evidence.purchased.inventory.Items.map(item => [item.ID, item.Template, item.Quality, item.Count, item.Ammo]), [ 75 [2, 21605, 1, 100, 0], [3, 21605, 1, 100, 0], [4, 21609, 1, 100, 0], 76 [5, 123742, 10, 1, 0], [6, 122121, 10, 1, 0], [7, 124258, 10, 1, 0], 77 [8, 129026, 10, 1, 0], [9, 123976, 10, 1, 0], [10, 160476, 10, 1, 0], 78 ]); 79 80 // A cancelled QL5 basket leaves the completed transaction untouched. 81 await open(true); 82 await click(await catalogRow(12)); 83 await waitServer('cancel draft selected', state => state.purchases === 1); 84 await game.click(945, 475); 85 await waitServer('draft cancelled', state => !state.open); 86 assert.deepEqual((await server()).inventory, evidence.purchased.inventory); 87 await page.keyboard.press('Control+1'); 88 89 // Use Shift-wheel camera orbit to frame the nearby Leet. 90 const cameraPoint = await game.point(400, 200); 91 await page.mouse.move(cameraPoint.x, cameraPoint.y); 92 const deadline = Date.now() + 10000; 93 for (;;) { 94 await game.snapshot(); 95 const y = game.state.actors.find(actor => actor.type === 50000 && actor.id === evidence.before.target).screenPosition[1]; 96 if (y >= 500 && y <= 600) break; 97 assert(Date.now() < deadline, 'Camera did not frame the controlled Leet'); 98 const angles = game.state.camera.angles; 99 await page.keyboard.down('Shift'); 100 try { await page.mouse.wheel(0, y > 600 ? 40 : -40); } 101 finally { await page.keyboard.up('Shift'); } 102 await game.wait('Shift-wheel changes the camera', state => 103 state.camera.angles.some((angle, index) => angle !== angles[index])); 104 } 105 106 evidence.used = []; 107 for (const [id, reloads] of [ 108 [5, [[48, 20]]], [6, [[50, 20]]], [7, [[50, 40]]], [8, []], 109 [9, [[48, 8]]], [10, [[49, 100], [48, 120]]], 110 ]) { 111 await waitServer('previous weapon recharge finished', state => !state.recharging && state.fighting === 0); 112 await click(cell(104, game.local().inventory.find(item => item.id === id).slot), 'right'); 113 await game.wait('purchased weapon equipped', state => 114 game.local(state).inventory.some(item => item.id === id && item.slot === 6), 15000); 115 await waitServer('server agrees on equipped identity', state => state.inventory.Items.find(item => item.ID === id).Slot === 6); 116 for (const [ammoSlot, clip] of reloads) { 117 await click(cell(104, ammoSlot)); 118 await game.wait('ammunition picked', state => state.dragged[0] === 104 && state.dragged[1] === ammoSlot); 119 await game.click(867, 206, 'right'); 120 await waitServer('purchased weapon loaded', state => state.inventory.Items.find(item => item.ID === id).Ammo === clip); 121 await game.wait('native loaded clip agrees', state => game.local(state).weapons.some(weapon => weapon.slot === 6 && weapon.loadedAmmo === clip)); 122 await game.wait('native reload action finished', state => !game.local(state).actions.includes(6)); 123 await waitServer('reload cooldown finished', state => !state.recharging); 124 } 125 126 // One ordinary attack must damage the controlled target and consume one round. 127 const before = await server(); 128 await game.click(...await game.pick(50000, before.target)); 129 await game.key('KeyQ'); 130 await waitServer('purchased weapon deals damage', state => state.targetHealth < before.targetHealth); 131 await game.key('KeyQ'); 132 await waitServer('attack stopped', state => state.fighting === 0); 133 const used = await server(); 134 const expectedAmmo = reloads.length ? reloads.at(-1)[1] - 1 : 0; 135 assert.equal(used.inventory.Items.find(item => item.ID === id).Ammo, expectedAmmo); 136 await game.wait('native target damage and magazine agree', state => 137 state.actors.find(actor => actor.type === 50000 && actor.id === used.target)?.stats[27] === used.targetHealth && 138 (!reloads.length || game.local(state).weapons.some(weapon => weapon.slot === 6 && weapon.loadedAmmo === expectedAmmo))); 139 evidence.used.push(used); 140 await game.save(output + '/used-' + id); 141 } 142 const final = await server(); 143 assert.equal(final.inventory.Items.find(item => item.ID === 2).Count, 52); 144 assert.equal(final.inventory.Items.find(item => item.ID === 4).Count, 40); 145 assert(!final.inventory.Items.some(item => item.ID === 3)); 146 await page.reload(); 147 await game.ready(); 148 evidence.reconnected = await server(); 149 assert.deepEqual(evidence.reconnected.inventory, final.inventory); 150 for (const [id, slot] of [[5, 52], [6, 53], [7, 54], [8, 55], [9, 56], [10, 6]]) { 151 assert(game.local().inventory.some(item => item.id === id && item.slot === slot && item.quality === 10)); 152 } 153 assert.deepEqual(game.errors, []); 154 console.log('PASS:', evidence.before.machine, 'QL5/10 catalog, six purchased QL10 weapons used, buy/sell, E/cancel and reconnect'); 155 } finally { 156 try { 157 await fs.mkdir(output, {recursive: true}); 158 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 159 if (game) await game.save(output + '/final'); 160 } finally { await browser.close(); } 161 }