tools/client/test_shop_empty_total.mjs (3978B)
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-empty-total', 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 = (state, page, slot) => state.grids.flatMap(grid => grid.cells).find(cell => 20 cell.identity[0] === page && cell.identity[1] === slot && !cell.dummy); 21 const clickCell = async source => game.click( 22 (source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 23 const waitServer = async (description, predicate) => { 24 const deadline = Date.now() + 5000; 25 while (!predicate(await server())) { 26 assert(Date.now() < deadline, description); 27 await game.snapshot(); 28 } 29 }; 30 await page.goto(url); 31 await game.ready(); 32 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 33 await game.snapshot(); 34 evidence.before = await server(); 35 assert.equal(game.local().stats[161], 38); 36 await game.key('KeyI'); 37 if (!game.state.camera.firstPerson) await game.key('F8'); 38 await game.click(...await game.pick(51035, 3221488799), 'right'); 39 await game.wait('tools catalog opens', state => cell(state, 111, 0)); 40 await clickCell(cell(game.state, 104, 48)); 41 await game.wait('axe picked', state => state.dragged[0] === 104 && state.dragged[1] === 48); 42 await game.click(860, 412); 43 await waitServer('axe offered to fund selection', state => state.sales === 1); 44 await clickCell(cell(game.state, 111, 0)); 45 await waitServer('Lock Pick selected', state => state.purchases === 1); 46 await game.save(output + '/selected'); 47 assert((await page.request.post(url + '/test-skill')).ok()); 48 await game.wait('controlled live Computer Literacy update', state => game.local(state).stats[161] === 48); 49 await game.save(output + '/skill-changed'); 50 await game.click(860, 328); 51 await waitServer('last purchase removed', state => state.purchases === 0); 52 await game.click(860, 412); 53 await waitServer('funding axe removed', state => state.sales === 0); 54 await game.wait('funding axe restored to inventory', state => game.local(state).inventory.some(item => item.id === 1)); 55 evidence.empty = await server(); 56 await game.save(output + '/empty'); 57 const sequence = game.sequence; 58 await game.click(873, 475); 59 await game.wait('empty zero-credit basket sends Accept', () => game.events.some(event => 60 event.sequence > sequence && event.direction === 'framesent' && event.opcode === '36284f6e' && 61 Buffer.from(event.hex, 'hex').readUInt32BE(33) === 1)); 62 await waitServer('empty basket accepted', state => !state.open); 63 evidence.after = await server(); 64 assert.deepEqual(evidence.after.inventory, evidence.before.inventory); 65 assert.equal(evidence.after.lastID, 1); 66 await page.reload(); 67 await game.ready(); 68 assert.deepEqual((await server()).inventory, evidence.before.inventory); 69 assert.deepEqual(game.errors, []); 70 console.log('PASS: last purchase removal accepts an empty zero-credit basket and retains the axe after reload'); 71 } finally { 72 try { 73 await fs.mkdir(output, {recursive: true}); 74 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 75 if (game) await game.save(output + '/final'); 76 } finally { await browser.close(); } 77 }