tools/client/test_named_key_trade.mjs (4979B)
1 import assert from 'node:assert/strict'; 2 import {createRequire} from 'node:module'; 3 import {GameTest} from './game_test.mjs'; 4 5 const {chromium} = createRequire(import.meta.url)('playwright'); 6 const [url, output] = process.argv.slice(2); 7 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', args: [ 8 '--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage', 9 '--disable-background-timer-throttling', '--disable-renderer-backgrounding']}); 10 const games = []; 11 const cell = (game, page, slot) => game.state.grids.flatMap(grid => grid.cells).find(cell => 12 cell.identity[0] === page && cell.identity[1] === slot && !cell.dummy); 13 const click = async (game, page, slot) => { 14 const source = cell(game, page, slot); 15 assert(source, `native source ${page}/${slot} must remain usable`); 16 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); 17 }; 18 async function inspect(game, page, slot, directory) { 19 await game.page.keyboard.down('Shift'); 20 try { await click(game, page, slot); } 21 finally { await game.page.keyboard.up('Shift'); } 22 await game.save(directory); 23 await game.key('Escape'); 24 } 25 try { 26 for (const index of [0, 1]) { 27 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 28 await context.addCookies([{name: 'ao_client', value: 'named-key-trade-' + index, url}]); 29 const page = await context.newPage(), game = await GameTest.attach(page); 30 games.push(game); 31 await page.goto(url); 32 await game.ready(); 33 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 34 await game.snapshot(); 35 } 36 const [owner, peer] = games; 37 const server = async () => (await owner.page.request.get(url + '/test-state')).json(); 38 await owner.key('KeyI'); 39 assert((await owner.page.request.post(url + '/test-name')).ok()); 40 await owner.wait('owner has the original live key', () => owner.events.some(event => 41 event.direction === 'framereceived' && event.opcode === '3b11256f' && event.actor[1] === 2)); 42 await inspect(owner, 104, 49, output + '/owner-info'); 43 if (!owner.state.camera.firstPerson) await owner.key('F8'); 44 for (const attempt of [0, 1]) { 45 await owner.click(...await owner.pick(50000, 2), 'right'); 46 await Promise.all(games.map(game => game.wait('player trade opens', state => state.regions.some(region => 47 region[0] === 832 && region[1] === 0 && region[2] === 1024 && region[3] === 247)))); 48 const sequence = peer.sequence; 49 await click(owner, 104, 49); 50 await owner.wait('key picked', state => state.dragged[0] === 104 && state.dragged[1] === 49); 51 await owner.click(864, 172); 52 await peer.wait('peer sees the key offer', () => cell(peer, 208, 0)); 53 await inspect(peer, 208, 0, output + '/peer-info-' + attempt); 54 await peer.wait('peer receives the exact live key', () => peer.events.some(event => event.sequence > sequence && 55 event.direction === 'framereceived' && event.opcode === '3b11256f' && event.actor[0] === 51053 && event.actor[1] === 2)); 56 if (attempt === 0) { 57 await owner.click(945, 228); 58 await owner.wait('cancelled source remains usable', () => cell(owner, 104, 49)); 59 await inspect(owner, 104, 49, output + '/cancelled-info'); 60 } 61 } 62 await owner.click(874, 228); 63 await peer.click(874, 228); 64 await peer.wait('recipient owns the same live key', state => peer.local(state).inventory.some(item => item.id === 2 && item.slot === 49)); 65 await peer.key('KeyI'); 66 await peer.wait('received source is usable', () => cell(peer, 104, 49)); 67 await inspect(peer, 104, 49, output + '/received-info'); 68 69 // A received live key must also work as the existing duplicator's target. 70 await click(peer, 104, 48); 71 await peer.wait('duplicator picked', state => state.dragged[0] === 104 && state.dragged[1] === 48); 72 const key = cell(peer, 104, 49); 73 await peer.page.keyboard.down('Shift'); 74 try { await peer.click((key.bounds[0] + key.bounds[2]) / 2, (key.bounds[1] + key.bounds[3]) / 2, 'right'); } 75 finally { await peer.page.keyboard.up('Shift'); } 76 await peer.wait('live received key is copied', state => peer.local(state).inventory.filter(item => item.templates[0] === 28577).length === 2 && cell(peer, 104, 50)); 77 await peer.key('Escape'); 78 await peer.wait('original and reusable tool remain usable', () => cell(peer, 104, 48) && cell(peer, 104, 49)); 79 await peer.save(output + '/copied'); 80 const result = await server(); 81 assert.deepEqual(result.owner.Items.map(item => item.ID), [1]); 82 assert.deepEqual(result.peer.Items.map(item => [item.ID, item.Slot]), [[2, 65], [3, 64], [4, 66]]); 83 for (const game of games) assert.deepEqual(game.errors, []); 84 console.log('PASS: live named key trade, repeated offers, native recipient copying and cold bindings'); 85 } finally { 86 try { for (const [index, game] of games.entries()) await game.save(output + '/final-' + index); } 87 finally { await browser.close(); } 88 }