tools/client/test_bulk_reclaim.mjs (6442B)
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 const games = []; 12 const evidence = {}; 13 const state = async game => (await game.page.request.get(url + '/test-state')).json(); 14 const source = snapshot => snapshot.grids.flatMap(grid => grid.cells).filter(cell => 15 cell.identity[0] === 106 && !cell.dummy); 16 const compare = async game => { 17 const server = await state(game); 18 const expected = server.items.map(([id, type, slot, quality, count, low, high]) => 19 [id, type, slot >= 64 ? slot - 16 : slot, quality, count, low, high]).sort((a, b) => a[0] - b[0]); 20 const actual = game.local().inventory.map(item => 21 [item.id, item.type, item.slot, item.quality, item.count, ...item.templates]).sort((a, b) => a[0] - b[0]); 22 assert.deepEqual(actual, expected); 23 assert.equal(game.local().stats[61], server.inventory.Credits); 24 assert.equal(server.nextItem, 33); 25 return server; 26 }; 27 try { 28 for (const name of ['Bulkowner', 'Bulkwatch']) { 29 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 30 await context.addCookies([{name: 'ao_client', value: name, url}]); 31 const page = await context.newPage(); 32 const game = await GameTest.attach(page); 33 games.push(game); 34 await page.goto(url); 35 await game.ready(); 36 evidence[name] = await compare(game); 37 } 38 const [owner, observer] = games; 39 const initial = evidence.Bulkowner.inventory; 40 if (phase === 'collect') { 41 assert.equal(initial.Reclaim.length, 24); 42 assert.equal(initial.Items.length, 2); 43 const [x, y] = await owner.pick(51059, 3221881537); 44 await owner.click(x, y, 'right'); 45 await owner.wait('24-entry private reclaim opens', snapshot => source(snapshot).length > 0); 46 await owner.save(output + '/opened'); 47 const pending = new Map(initial.Reclaim.map((entry, slot) => [slot, entry.Item])); 48 evidence.collected = []; 49 const firstRow = owner.state.grids.find(grid => grid.cells.some(cell => cell.identity[0] === 106)).firstRow; 50 assert(firstRow > 0); 51 const point = await owner.point(80, 70); 52 await owner.page.mouse.move(point.x, point.y); 53 await owner.page.mouse.wheel(0, -100); 54 await owner.wait('native reclaim scrolls to earlier rows', snapshot => snapshot.grids.some(grid => 55 grid.cells.some(cell => cell.identity[0] === 106) && grid.firstRow < firstRow)); 56 evidence.scrolls = 1; 57 let lastCell; 58 for (let index = 0; index < 24; ++index) { 59 let cell = source(owner.state).find(cell => pending.has(cell.identity[1])); 60 if (!cell) { 61 // Advance upward if the remaining fixed slots are outside the visible rows. 62 const previous = source(owner.state).map(cell => cell.identity[1]).join(','); 63 const point = await owner.point(80, 70); 64 await owner.page.mouse.move(point.x, point.y); 65 await owner.page.mouse.wheel(0, -100); 66 ++evidence.scrolls; 67 await owner.wait('earlier reclaim rows become visible', snapshot => source(snapshot).some(cell => 68 pending.has(cell.identity[1])) && source(snapshot).map(cell => cell.identity[1]).join(',') !== previous); 69 cell = source(owner.state).find(cell => pending.has(cell.identity[1])); 70 } 71 assert(cell); 72 const slot = cell.identity[1]; 73 const item = pending.get(slot); 74 await owner.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 75 await owner.wait('exact reclaim instance arrives', snapshot => owner.local(snapshot).inventory.some(proxy => 76 proxy.id === item.ID && proxy.type === item.Type)); 77 const after = await state(owner); 78 const taken = after.inventory.Items.find(candidate => candidate.ID === item.ID); 79 assert.deepEqual({...taken, Slot: item.Slot}, item); 80 assert.equal(after.inventory.Reclaim?.length ?? 0, 23 - index); 81 pending.delete(slot); 82 evidence.collected.push({slot, item: taken}); 83 lastCell = cell; 84 } 85 assert(evidence.scrolls > 0, 'bulk reclaim must exercise its native scrollable window'); 86 evidence.after = await compare(owner); 87 assert.equal(evidence.after.inventory.Reclaim, null); 88 for (let index = 0; index < 2; ++index) { 89 await owner.click((lastCell.bounds[0] + lastCell.bounds[2]) / 2, (lastCell.bounds[1] + lastCell.bounds[3]) / 2); 90 assert.deepEqual(await state(owner), evidence.after); 91 } 92 await owner.click(170, 8); 93 await owner.click(x, y, 'right'); 94 await owner.wait('empty reclaim window reopens', snapshot => snapshot.regions.some(region => 95 region[0] === 0 && region[1] === 0 && region[2] === 178 && region[3] === 128)); 96 assert.equal(source(owner.state).length, 0); 97 assert.deepEqual(await state(owner), evidence.after); 98 await owner.save(output + '/collected'); 99 await owner.page.reload(); 100 await owner.ready(); 101 evidence.reconnected = await compare(owner); 102 assert.deepEqual(evidence.reconnected, evidence.after); 103 } else { 104 assert.equal(initial.Reclaim, null); 105 assert.equal(initial.Items.length, 26); 106 evidence.after = await compare(owner); 107 } 108 assert.deepEqual(await compare(observer), evidence.Bulkwatch); 109 assert(!observer.events.some(event => event.opcode === '52213420')); 110 assert.equal(evidence.after.inventory.Items.find(item => item.ID === 3).Ammo, 13); 111 assert.equal(evidence.after.inventory.Items.find(item => item.ID === 4).Count, 100); 112 assert.equal(evidence.after.inventory.Items.find(item => item.ID === 5).Count, 17); 113 for (const game of games) assert.deepEqual(game.errors, []); 114 console.log('PASS:', phase, '24 exact reclaim identities, charges, loaded rounds, private observer and fresh-client recovery'); 115 } finally { 116 try { 117 await fs.mkdir(output, {recursive: true}); 118 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 119 for (let index = 0; index < games.length; ++index) await games[index].save(output + '/final-' + index); 120 } finally { await browser.close(); } 121 }