tools/client/test_ground_drop.mjs (12215B)
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, phase] = 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: 'ground-drop', 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, type, slot) => state.grids.flatMap(grid => grid.cells).find(cell => 20 cell.identity[0] === type && cell.identity[1] === slot && !cell.dummy); 21 const clickCell = async item => game.click((item.bounds[0] + item.bounds[2]) / 2, (item.bounds[1] + item.bounds[3]) / 2); 22 const inventory = async () => { 23 if (!game.state.grids.some(grid => grid.totalCells === 30)) await game.key('KeyI'); 24 }; 25 await page.goto(url); 26 await game.ready(); 27 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 28 await game.snapshot(); 29 await inventory(); 30 if (phase === 'stale-contained') { 31 const bag = cell(game.state, 104, 49); 32 await game.click((bag.bounds[0] + bag.bounds[2]) / 2, (bag.bounds[1] + bag.bounds[3]) / 2, 'right'); 33 await game.wait('backpack opened for contained source', state => state.grids.some(grid => grid.totalCells === 21)); 34 await clickCell(cell(game.state, 104, 48)); 35 await game.wait('pistol picked for contained source', state => state.dragged[0] === 104 && state.dragged[1] === 48); 36 await clickCell(game.state.grids.find(grid => grid.totalCells === 21).cells[0]); 37 await game.wait('pistol deposited for contained source', state => cell(state, 107, 65536)); 38 } 39 if (phase === 'stale' || phase === 'stale-contained' || phase === 'refusal') { 40 if (!game.state.camera.firstPerson) await game.key('F8'); 41 const point = await game.point(500, 150); 42 await page.mouse.move(point.x, point.y); 43 const press = game.sequence; 44 await page.mouse.down({button: 'right'}); 45 try { 46 await game.waitInput(press, 1025); 47 await page.mouse.move(point.x, point.y + 400, {steps: 8}); 48 await game.wait('ground view below the character', state => state.camera.angles[1] >= 0.55); 49 } finally { 50 const release = game.sequence; 51 await page.mouse.up({button: 'right'}); 52 await game.waitInput(release, 1026); 53 } 54 } 55 if (phase === 'stale-contained') { 56 assert((await page.request.post(url + '/test-stale-contained-source')).ok()); 57 evidence.transferred = await server(); 58 assert.deepEqual(evidence.transferred.inventory.Containers[2], [ 59 {ID: 1, Type: 51018, Template: 123742, Quality: 2, Slot: 1, Count: 1, Ammo: 3}]); 60 await game.save(output + '/stale'); 61 await clickCell(cell(game.state, 107, 65536)); 62 await game.wait('stale contained source picked', state => state.dragged[0] === 107 && state.dragged[1] === 65536); 63 const sequence = game.sequence; 64 await game.click(500, 580); 65 await game.wait('actual stale contained ground-drop request', () => game.events.some(event => event.sequence > sequence && 66 event.direction === 'framesent' && event.opcode === '3a243f41' && 67 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 107 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === 65536)); 68 evidence.afterRequest = await server(); 69 assert.deepEqual(evidence.afterRequest, evidence.transferred); 70 await game.wait('stale contained source cancelled and refreshed', state => !cell(state, 107, 65536) && 71 cell(state, 107, 65537) && state.dragged[0] === 0 && game.events.some(event => event.sequence > sequence && 72 event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146)); 73 await game.save(output + '/refreshed'); 74 await clickCell(cell(game.state, 107, 65537)); 75 await game.wait('actual contained source picked', state => state.dragged[0] === 107 && state.dragged[1] === 65537); 76 await clickCell(game.state.grids.find(grid => grid.totalCells === 30).cells.find(item => item.identity[0] === 0)); 77 await game.wait('exact contained item withdrawn', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 48)); 78 evidence.withdrawn = await server(); 79 assert.equal(evidence.withdrawn.inventory.Containers, null); 80 assert.equal(evidence.withdrawn.inventory.Items.find(item => item.ID === 1).Ammo, 3); 81 await game.save(output + '/withdrawn'); 82 await page.reload(); 83 await game.ready(); 84 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 85 await game.snapshot(); 86 assert.deepEqual(await server(), evidence.withdrawn); 87 await inventory(); 88 } 89 if (phase === 'stale') { 90 assert((await page.request.post(url + '/test-stale-source')).ok()); 91 evidence.transferred = await server(); 92 assert.deepEqual(evidence.transferred.inventory.Containers[2], [ 93 {ID: 1, Type: 51018, Template: 123742, Quality: 2, Slot: 0, Count: 1, Ammo: 3}]); 94 assert(!evidence.transferred.inventory.Items.some(item => item.ID === 1)); 95 await game.save(output + '/stale'); 96 await clickCell(cell(game.state, 104, 48)); 97 await game.wait('stale drop source picked', state => state.dragged[0] === 104 && state.dragged[1] === 48); 98 const sequence = game.sequence; 99 await game.click(500, 580); 100 await game.wait('actual stale ground-drop request', () => game.events.some(event => event.sequence > sequence && 101 event.direction === 'framesent' && event.opcode === '3a243f41' && 102 Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === 48)); 103 evidence.afterRequest = await server(); 104 assert.deepEqual(evidence.afterRequest, evidence.transferred); 105 await game.wait('stale ground-drop source cancelled and cleared', state => !cell(state, 104, 48) && 106 state.dragged[0] === 0 && game.events.some(event => event.sequence > sequence && 107 event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 146) && 108 game.events.some(event => event.sequence > sequence && event.direction === 'framereceived' && 109 event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 112)); 110 await game.save(output + '/cleared'); 111 112 // The same physical source must remain available through its actual bag. 113 const bag = cell(game.state, 104, 49); 114 await game.click((bag.bounds[0] + bag.bounds[2]) / 2, (bag.bounds[1] + bag.bounds[3]) / 2, 'right'); 115 await game.wait('actual retained source opens', state => cell(state, 107, 65536)); 116 await clickCell(cell(game.state, 107, 65536)); 117 await game.wait('retained source picked', state => state.dragged[0] === 107 && state.dragged[1] === 65536); 118 await clickCell(game.state.grids.find(grid => grid.totalCells === 30).cells.find(item => item.identity[0] === 0)); 119 await game.wait('same physical source withdrawn', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 48)); 120 evidence.withdrawn = await server(); 121 assert.equal(evidence.withdrawn.inventory.Items.find(item => item.ID === 1).Ammo, 3); 122 assert.equal(evidence.withdrawn.inventory.Containers, null); 123 await game.save(output + '/withdrawn'); 124 await page.reload(); 125 await game.ready(); 126 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 127 await game.snapshot(); 128 assert.deepEqual(await server(), evidence.withdrawn); 129 await inventory(); 130 } 131 if (phase === 'refusal') { 132 evidence.before = await server(); 133 await game.save(output + '/before'); 134 const drop = async (type, slot) => { 135 await clickCell(cell(game.state, type, slot)); 136 await game.wait('owned drop source picked', state => state.dragged[0] === type && state.dragged[1] === slot); 137 const sequence = game.sequence; 138 await game.click(500, 580); 139 await game.wait('actual ground-drop request', () => game.events.some(event => event.sequence > sequence && 140 event.direction === 'framesent' && event.opcode === '3a243f41' && 141 Buffer.from(event.hex, 'hex').readUInt32BE(29) === type && Buffer.from(event.hex, 'hex').readUInt32BE(33) === slot)); 142 await game.wait('refused ground-drop source restored', state => cell(state, type, slot) && game.events.some(event => 143 event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '5f4b442a' && 144 Buffer.from(event.hex, 'hex').toString('ascii', 33, 68) === 'You are not able to drop this item.')); 145 assert.deepEqual(game.state.dragged, [0, 0]); 146 }; 147 await drop(104, 48); 148 evidence.refused = await server(); 149 assert.deepEqual(evidence.refused, evidence.before); 150 await game.save(output + '/refused'); 151 await drop(104, 48); 152 assert.deepEqual(await server(), evidence.before); 153 154 // Move the same pistol into its own bag and refuse its packed drop source. 155 const bag = cell(game.state, 104, 49); 156 await game.click((bag.bounds[0] + bag.bounds[2]) / 2, (bag.bounds[1] + bag.bounds[3]) / 2, 'right'); 157 await game.wait('carried backpack opens', state => state.grids.some(grid => grid.totalCells === 21)); 158 await clickCell(cell(game.state, 104, 48)); 159 await game.wait('pistol picked for deposit', state => state.dragged[0] === 104 && state.dragged[1] === 48); 160 await clickCell(game.state.grids.find(grid => grid.totalCells === 21).cells[0]); 161 await game.wait('same pistol deposited', state => cell(state, 107, 65536) && !game.local(state).inventory.some(item => item.id === 1)); 162 evidence.bagged = await server(); 163 assert.deepEqual(evidence.bagged.inventory.Containers[2], [{ID: 1, Type: 51018, Template: 123742, Quality: 2, Slot: 0, Count: 1, Ammo: 3}]); 164 await drop(107, 65536); 165 evidence.bagRefused = await server(); 166 assert.deepEqual(evidence.bagRefused, evidence.bagged); 167 await game.save(output + '/bag-refused'); 168 await clickCell(cell(game.state, 107, 65536)); 169 await game.wait('retained pistol picked for withdrawal', state => state.dragged[0] === 107 && state.dragged[1] === 65536); 170 await clickCell(game.state.grids.find(grid => grid.totalCells === 30).cells.find(item => item.identity[0] === 0)); 171 await game.wait('refused pistol remains normally withdrawable', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 48)); 172 evidence.after = await server(); 173 assert.deepEqual(evidence.after.inventory.Items.toSorted((a, b) => a.ID - b.ID), evidence.before.inventory.Items); 174 assert.equal(evidence.after.worldItems, 5); 175 assert.equal(evidence.after.lastID, 8); 176 await game.save(output + '/withdrawn'); 177 await page.reload(); 178 await game.ready(); 179 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 180 await game.snapshot(); 181 assert.deepEqual(await server(), evidence.after); 182 await inventory(); 183 } 184 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.templates, item.quality, item.count]), [ 185 [1, 48, [123742, 123743], 2, 1], [2, 49, [99228, 99228], 1, 1], [3, 50, [25812, 25811], 2, 3]]); 186 evidence.final = await server(); 187 assert.equal(evidence.final.inventory.Items.find(item => item.ID === 1).Ammo, 3); 188 assert.deepEqual(game.errors, []); 189 console.log('PASS: carried/backpack ground-drop refusals restore sources; repeated input and page/cold retain IDs, selected qualities, charges and loaded rounds'); 190 } finally { 191 try { 192 await fs.mkdir(output, {recursive: true}); 193 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 194 if (game) await game.save(output + '/final'); 195 } finally { await browser.close(); } 196 }