tools/client/test_traded_weapon.mjs (8562B)
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', headless: false, 9 args: ['--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage', 10 '--disable-background-timer-throttling', '--disable-renderer-backgrounding']}); 11 const games = [], evidence = {}; 12 async function inventoryClick(game, slot, button = 'right') { 13 await game.wait('equipment action finishes', state => 14 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 15 const cell = game.state.grids.flatMap(grid => grid.cells).find(cell => 16 cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy); 17 assert(cell, 'ordinary inventory cell is visible'); 18 await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2, button); 19 } 20 try { 21 for (const index of [0, 1]) { 22 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 23 await context.addCookies([{name: 'ao_client', value: 'traded-weapon-' + index, url}]); 24 const page = await context.newPage(), game = await GameTest.attach(page); 25 games.push(game); 26 await page.goto(url); 27 await game.ready(); 28 } 29 const [owner, recipient] = games; 30 const state = async () => (await owner.page.request.get(url + '/test-state')).json(); 31 evidence.initial = await state(); 32 if (evidence.initial.owner.Items.length === 2) { 33 assert.equal(evidence.initial.health, 20); 34 // Instantiate the loaded weapon in both native worlds before offering it. 35 await owner.key('KeyI'); 36 await owner.page.keyboard.press('Control+1'); 37 await inventoryClick(owner, 48); 38 await owner.wait('original owner wears the physical weapon', snapshot => 39 owner.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 6)); 40 assert.equal((await state()).ownerSense, 302); 41 await owner.click(867, 206, 'right'); 42 await owner.wait('same physical weapon removed', snapshot => 43 owner.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 48)); 44 assert.equal((await state()).ownerSense, 300); 45 await owner.page.keyboard.press('Control+1'); 46 if (!owner.state.camera.firstPerson) await owner.key('F8'); 47 48 // Both native participants see cancellation, then consent to the same gift. 49 for (let attempt = 0; attempt < 2; ++attempt) { 50 await owner.click(...await owner.pick(50000, 2), 'right'); 51 await Promise.all(games.map(game => game.wait('player trade opens', snapshot => 52 snapshot.regions.some(([left, top, right, bottom]) => 53 left === 832 && top === 0 && right === 1024 && bottom === 247)))); 54 await inventoryClick(owner, 48, 'left'); 55 const sequence = owner.sequence; 56 await owner.click(864, 172); 57 await owner.wait('physical weapon offered', () => owner.events.some(event => event.sequence > sequence && 58 event.direction === 'framesent' && event.opcode === '36284f6e' && 59 Buffer.from(event.hex, 'hex').readUInt32BE(33) === 4)); 60 await recipient.wait('peer weapon offer visible', snapshot => snapshot.grids.some(grid => 61 grid.cells.some(cell => cell.identity[0] === 208 && !cell.dummy))); 62 evidence['offered' + attempt] = await state(); 63 assert.equal(evidence['offered' + attempt].owner.Items[0].Ammo, 13); 64 assert.deepEqual(evidence['offered' + attempt].recipient.Items, null); 65 await recipient.save(output + '/offer-' + attempt); 66 if (attempt === 0) { 67 await owner.click(945, 228); 68 await owner.wait('cancelled weapon restored with its identity', snapshot => 69 owner.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 48)); 70 assert.equal((await state()).owner.Items[0].Ammo, 13); 71 } 72 } 73 await owner.click(874, 228); 74 await recipient.click(874, 228); 75 await recipient.wait('gift appears in recipient inventory', snapshot => 76 recipient.local(snapshot).inventory.some(item => item.slot === 48 && item.quality === 50)); 77 evidence.transferred = await state(); 78 assert.equal(evidence.transferred.owner.Items.length, 1); 79 assert.equal(evidence.transferred.recipient.Items[0].ID, 1); 80 assert.equal(evidence.transferred.recipient.Items[0].Ammo, 13); 81 await recipient.save(output + '/received'); 82 83 // Use the received row immediately, without reconnecting to repair it. 84 if (!recipient.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))) 85 await recipient.key('KeyI'); 86 await recipient.page.keyboard.press('Control+1'); 87 await inventoryClick(recipient, 48); 88 await recipient.wait('recipient wears the same physical weapon', snapshot => 89 recipient.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 6)); 90 assert.equal((await state()).recipientSense, 302); 91 await recipient.click(867, 206, 'right'); 92 await recipient.wait('recipient removes the same weapon', snapshot => 93 recipient.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 48)); 94 assert.equal((await state()).recipientSense, 300); 95 await inventoryClick(recipient, 48); 96 await recipient.wait('recipient rewields the same weapon', snapshot => 97 recipient.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 6)); 98 99 const deadline = Date.now() + 10000; 100 while (recipient.state.selected[0] !== 50000 || recipient.state.selected[1] !== evidence.initial.target) { 101 assert(Date.now() < deadline, 'Tab did not select the controlled Leet'); 102 const selected = recipient.state.selected; 103 await recipient.key('Tab'); 104 await recipient.wait('target cycle advances', snapshot => 105 snapshot.selected[0] !== selected[0] || snapshot.selected[1] !== selected[1]); 106 } 107 const sequence = recipient.sequence; 108 await recipient.key('KeyQ'); 109 await recipient.wait('recipient fires retained ammunition', () => recipient.events.some(event => 110 event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '46002f16' && event.actor[1] === 2)); 111 await recipient.key('KeyQ'); 112 await Promise.all(games.map(game => game.wait('both clients see the impact', snapshot => 113 snapshot.actors.some(actor => actor.id === evidence.initial.target && actor.stats[27] === 18)))); 114 evidence.hit = await state(); 115 assert.equal(evidence.hit.health, 18); 116 assert.equal(evidence.hit.recipient.Items[0].Ammo, 12); 117 assert.equal(evidence.hit.owner.Items[0].Count, 100); 118 await recipient.save(output + '/hit'); 119 for (const game of games) { await game.page.reload(); await game.ready(); } 120 } 121 assert.deepEqual(owner.local().inventory.map(item => [item.id, item.slot, item.quality, item.count]), [[2, 49, 1, 100]]); 122 assert.deepEqual(recipient.local().inventory.map(item => [item.id, item.slot, item.quality, ...item.templates]), 123 [[1, 6, 50, 160476, 160477]]); 124 evidence.final = await state(); 125 assert.equal(evidence.final.ownerSense, 300); 126 assert.equal(evidence.final.recipientSense, 302); 127 assert.equal(evidence.final.recipient.Items[0].Ammo, 12); 128 if (!owner.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))) { 129 await owner.key('KeyI'); 130 await owner.wait('retained ammunition inventory opens', snapshot => snapshot.grids.some(grid => 131 grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))); 132 } 133 if (!recipient.state.regions.some(([left, top, right, bottom]) => 134 left === 832 && top === 0 && right === 1024 && bottom === 360)) 135 await recipient.page.keyboard.press('Control+1'); 136 await recipient.wait('retained weapon panel opens', snapshot => snapshot.regions.some(([left, top, right, bottom]) => 137 left === 832 && top === 0 && right === 1024 && bottom === 360)); 138 assert(games.every(game => game.errors.length === 0)); 139 console.log('PASS: instantiated loaded weapon survives cancelled/accepted trade, immediate recipient use and page/cold recovery'); 140 } finally { 141 try { 142 await fs.mkdir(output, {recursive: true}); 143 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2)); 144 await Promise.all(games.map((game, index) => game.save(output + '/final-' + index))); 145 } finally { await browser.close(); } 146 }