tools/client/test_mission_key_copy.mjs (5794B)
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 14 try { 15 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 16 await context.addCookies([{name: 'ao_client', value: 'mission-key-copy', url}]); 17 const page = await context.newPage(); 18 game = await GameTest.attach(page); 19 const server = async () => (await page.request.get(url + '/test-state')).json(); 20 const cell = slot => game.state.grids.flatMap(grid => grid.cells).find(cell => 21 cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy); 22 const click = async (slot, button = 'left') => { 23 const source = cell(slot); 24 assert(source, `ordinary slot ${slot} must be usable`); 25 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2, button); 26 }; 27 await page.goto(url); 28 await game.ready(); 29 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 30 await game.snapshot(); 31 await game.key('i'); 32 await game.wait('original key visible', () => cell(49)); 33 evidence.initial = await server(); 34 await game.save(output + '/initial'); 35 36 // Buy the original tool from the actual Tools terminal, then use that same item. 37 if (!game.state.camera.firstPerson) await game.key('F8'); 38 await game.click(...await game.pick(51035, 3221488799), 'right'); 39 await game.wait('duplicator catalog entry visible', state => state.grids.flatMap(grid => grid.cells).some(cell => 40 cell.identity[0] === 111 && cell.identity[1] === 1 && !cell.dummy)); 41 const tool = game.state.grids.flatMap(grid => grid.cells).find(cell => cell.identity[0] === 111 && cell.identity[1] === 1); 42 await game.click((tool.bounds[0] + tool.bounds[2]) / 2, (tool.bounds[1] + tool.bounds[3]) / 2); 43 await game.wait('duplicator displayed in the BUY basket', state => state.grids.some(grid => 44 grid.position[1] === 303 && grid.cells.some(cell => !cell.dummy))); 45 await game.save(output + '/selected'); 46 await game.click(873, 475); 47 await game.wait('purchased duplicator delivered', state => game.local(state).inventory.some(item => 48 item.templates[0] === 28564 && item.slot === 50) && cell(50)); 49 evidence.purchased = await server(); 50 assert.deepEqual(evidence.purchased.bindings, evidence.initial.bindings); 51 assert.deepEqual(evidence.purchased.inventory.Items.slice(0, 2), evidence.initial.inventory.Items); 52 assert.equal(evidence.purchased.inventory.Credits, 80); 53 assert.deepEqual(evidence.purchased.inventory.Items[2], 54 {ID: 3, Type: 51054, Template: 28564, Quality: 1, Slot: 66, Count: 1, Ammo: 0}); 55 await game.save(output + '/purchased'); 56 57 // Native item combination uses the picked tool and Shift-right-click of the key. 58 const start = game.sequence; 59 await click(50); 60 await game.wait('duplicator held', state => state.dragged[0] === 104 && state.dragged[1] === 50); 61 await page.keyboard.down('Shift'); 62 try { await click(49, 'right'); } 63 finally { await page.keyboard.up('Shift'); } 64 await game.wait('actual native key-copy request', () => game.events.some(event => event.sequence > start && 65 event.direction === 'framesent' && event.opcode === '5e477770' && 66 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 81)); 67 const request = game.events.find(event => event.sequence > start && event.direction === 'framesent' && 68 event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 81); 69 assert.deepEqual(Array.from({length: 5}, (_, index) => Buffer.from(request.hex, 'hex').readUInt32BE(29 + index * 4)), 70 [81, 104, 50, 104, 49]); 71 await game.save(output + '/requested'); 72 await game.wait('new key delivered to the ordinary inventory', state => 73 game.local(state).inventory.filter(item => item.templates[0] === 28577).length === 2 && cell(51)); 74 evidence.copied = await server(); 75 assert.deepEqual(evidence.copied.bindings, {'2': 2, '4': 2}); 76 assert.deepEqual(evidence.copied.inventory.Items.slice(0, 3), evidence.purchased.inventory.Items); 77 assert.deepEqual(evidence.copied.inventory.Items[3], 78 {ID: 4, Type: 51053, Template: 28577, Quality: 1, Slot: 67, Count: 1, Ammo: 0}); 79 assert.equal(evidence.copied.inventory.Credits, evidence.purchased.inventory.Credits); 80 await game.key('Escape'); 81 await game.wait('both reusable inputs remain usable', state => state.dragged[0] === 0 && cell(49) && cell(50)); 82 await game.save(output + '/copied'); 83 84 // A page replay must preserve both distinct key instances and the reusable tool. 85 await page.reload(); 86 await game.ready(); 87 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 88 await game.snapshot(); 89 assert.deepEqual(game.local().inventory.map(item => [item.id, item.templates[0], item.slot]), 90 [[1, 209283, 48], [2, 28577, 49], [3, 28564, 50], [4, 28577, 51]]); 91 evidence.replayed = await server(); 92 assert.deepEqual(evidence.replayed, evidence.copied); 93 assert.deepEqual(game.errors, []); 94 console.log('PASS: native Tools purchase, immediate key copy, reusable inputs, exact debit and page/cold persistence'); 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 }