tools/client/test_shop_availability.mjs (4431B)
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: 'shop-availability', 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 shopVisible = state => state.regions.some(([left, top, right, bottom]) => 21 left === 832 && top === 0 && right === 1024 && bottom === 494); 22 await page.goto(url); 23 await game.ready(); 24 await game.key('KeyI'); 25 const initial = evidence.initial = await server(); 26 const open = async keyboard => { 27 if (!game.state.camera.firstPerson) await game.key('F8'); 28 const sequence = game.sequence; 29 await game.click(...await game.pick(51035, initial.machine), keyboard ? 'left' : 'right'); 30 if (keyboard) await game.key('KeyE'); 31 await game.wait('empty shop opens with an explicit purchase limitation', state => shopVisible(state) && 32 game.events.some(event => event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '5f4b442a')); 33 const messages = game.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '5f4b442a'); 34 assert.equal(messages.length, 1); 35 const packet = Buffer.from(messages[0].hex, 'hex'); 36 assert.equal(packet.subarray(33, 33 + packet.readUInt32BE(29)).toString(), 37 initial.name + ': Purchases are not yet supported in this alpha. You can still sell items here.'); 38 assert(!game.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 111 && !cell.dummy))); 39 await waitServer('server retains the sale transaction', state => state.open); 40 }; 41 const offer = async () => { 42 const cell = game.state.grids.flatMap(grid => grid.cells).find(cell => cell.identity[0] === 104 && cell.identity[1] === 48); 43 assert(cell, 'owned sale item is present'); 44 await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 45 await game.wait('sale item picked', state => state.dragged[0] === 104); 46 await game.click(860, 412); 47 await waitServer('sale item offered', state => state.sales === 1); 48 }; 49 await open(false); 50 await game.save(output + '/limitation'); 51 await offer(); 52 await game.click(945, 475); 53 await waitServer('cancel restores ownership', state => !state.open); 54 await game.wait('cancel closes native shop', state => !shopVisible(state)); 55 assert.deepEqual((await server()).inventory, initial.inventory); 56 await open(true); 57 await offer(); 58 await game.click(873, 475); 59 await waitServer('sale committed once', state => !state.open && state.inventory.Items.length === 0); 60 evidence.sold = await server(); 61 assert.equal(evidence.sold.inventory.Credits, initial.creditsAfterSale); 62 await game.wait('native cash agrees with sale', state => game.local(state).stats[61] === initial.creditsAfterSale); 63 await page.reload(); 64 await game.ready(); 65 evidence.reconnected = await server(); 66 assert.deepEqual(evidence.reconnected.inventory, evidence.sold.inventory); 67 assert.equal(game.local().stats[61], initial.creditsAfterSale); 68 await open(false); 69 await game.save(output + '/reconnected-limitation'); 70 await game.click(945, 475); 71 await waitServer('final cancellation completed', state => !state.open); 72 assert.deepEqual(game.errors, []); 73 console.log('PASS:', initial.machine, 'visible purchase limitation, sell/cancel/retry and reconnect'); 74 } finally { 75 try { 76 await fs.mkdir(output, {recursive: true}); 77 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 78 if (game) await game.save(output + '/final'); 79 } finally { await browser.close(); } 80 }