tools/client/test_kevlar_armor.mjs (10739B)
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 = []; 12 async function bagClick(game, id) { 13 await game.wait('inventory actions finish', state => 14 !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); 15 const item = game.local().inventory.find(item => item.id === id); 16 assert(item && item.slot >= 48 && item.slot < 57); 17 const cell = item.slot - 48; 18 await game.click(681 + cell % 3 * 54, 45 + Math.floor(cell / 3) * 54, 'right'); 19 } 20 try { 21 for (const token of ['kevlar-owner', 'kevlar-watch']) { 22 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 23 await context.addCookies([{name: 'ao_client', value: token, url}]); 24 const page = await context.newPage(); 25 const game = await GameTest.attach(page); 26 games.push(game); 27 await page.goto(url); 28 await game.ready(); 29 await page.waitForFunction(() => touchRegion(500, 400)?.world === true); 30 await game.snapshot(); 31 } 32 const [owner, observer] = games; 33 if (!observer.state.camera.firstPerson) await observer.key('F8'); 34 await observer.wait('armor wearer is visible to the observer', state => state.actors.some(actor => actor.id === 1 && 35 actor.screenPosition?.[0] > 0 && actor.screenPosition[0] < 1024 && actor.screenPosition[1] > 0 && 36 actor.screenPosition[1] < 748 && actor.screenPosition[2] >= 0 && actor.screenPosition[2] <= 1)); 37 const state = async () => (await owner.page.request.get(url + '/test-state')).json(); 38 const baseline = await state(); 39 assert.equal(baseline.inventory.Items.length, 10); 40 assert.deepEqual(baseline.armor, [25, 25, 24, 21, 8, 21, 21, 8]); 41 await owner.key('KeyI'); 42 await owner.page.keyboard.press('Control+1'); 43 await owner.click(953, 53); 44 await owner.page.keyboard.press('Control+9'); 45 await owner.save(output + '/initial'); 46 47 // Every piece uses native removal and wear, with explicit left-arm placement. 48 for (const [id, slot, x, y, armor] of [ 49 [1, 27, 927, 262, [6, 6, 6, 5, 2, 5, 5, 2]], 50 [2, 30, 927, 316, [3, 3, 3, 2, 1, 2, 2, 1]], 51 [3, 23, 867, 206, [2, 2, 2, 2, 1, 2, 2, 1]], 52 [4, 25, 987, 206, [2, 2, 2, 2, 1, 2, 2, 1]], 53 [5, 24, 927, 206, [2, 2, 2, 2, 1, 2, 2, 1]], 54 [6, 21, 927, 148, [10, 10, 9, 8, 2, 8, 8, 2]], 55 ]) { 56 const sequence = observer.sequence; 57 await owner.click(x, y, 'right'); 58 await owner.wait('exact piece removed', state => owner.local(state).inventory.some(item => item.id === id && item.slot >= 48)); 59 assert.deepEqual((await state()).armor, baseline.armor.map((value, index) => value - armor[index])); 60 if (slot === 23) { 61 await owner.wait('arm action finishes', state => !owner.local(state).actions.includes(3)); 62 const cell = owner.local().inventory.find(item => item.id === id).slot - 48; 63 await owner.click(681 + cell % 3 * 54, 45 + Math.floor(cell / 3) * 54); 64 await owner.click(x, y); 65 } else await bagClick(owner, id); 66 await owner.wait('exact piece worn', state => owner.local(state).inventory.some(item => item.id === id && item.slot === slot)); 67 await observer.wait('observer receives wear', () => observer.events.some(event => { 68 if (event.sequence <= sequence || event.direction !== 'framereceived' || event.opcode !== '35505644' || event.actor[1] !== 1) return false; 69 const packet = Buffer.from(event.hex, 'hex'); 70 return packet.readUInt32BE(45) === 6 && packet.readUInt32BE(53) === slot; 71 })); 72 assert.deepEqual((await state()).armor, baseline.armor); 73 } 74 await owner.save(output + '/all-pieces'); 75 await observer.save(output + '/observer-all-pieces'); 76 77 // The alternate vest must retain its distinct appearance and AC contract. 78 await owner.click(927, 148, 'right'); 79 await owner.wait('body armor retained', state => owner.local(state).inventory.some(item => item.id === 6 && item.slot >= 48)); 80 const vestSequence = observer.sequence; 81 await bagClick(owner, 7); 82 await owner.wait('alternate vest worn', state => owner.local(state).inventory.some(item => item.id === 7 && item.slot === 21)); 83 await observer.wait('observer receives alternate vest', () => observer.events.some(event => { 84 if (event.sequence <= vestSequence || event.direction !== 'framereceived' || event.opcode !== '35505644') return false; 85 const packet = Buffer.from(event.hex, 'hex'); 86 return packet.readUInt32BE(29) === 85691 && packet.readUInt32BE(45) === 6 && packet.readUInt32BE(53) === 21; 87 })); 88 assert.deepEqual((await state()).armor, [24, 24, 24, 20, 8, 20, 20, 8]); 89 await owner.save(output + '/vest-style'); 90 await observer.save(output + '/observer-vest-style'); 91 await owner.click(927, 148, 'right'); 92 await owner.wait('alternate vest retained', state => owner.local(state).inventory.some(item => item.id === 7 && item.slot >= 48)); 93 await bagClick(owner, 6); 94 await owner.wait('body armor restored', state => owner.local(state).inventory.some(item => item.id === 6 && item.slot === 21)); 95 assert.deepEqual((await state()).armor, baseline.armor); 96 97 // The high stored endpoint must still deliver the selected QL100 contract. 98 await owner.click(927, 206, 'right'); 99 await owner.wait('QL2 gloves retained', state => owner.local(state).inventory.some(item => item.id === 5 && item.slot >= 48)); 100 await bagClick(owner, 8); 101 await owner.wait('QL100 gloves worn', state => owner.local(state).inventory.some(item => item.id === 8 && item.slot === 24)); 102 assert.deepEqual((await state()).armor, [148, 148, 131, 97, 32, 97, 97, 32]); 103 await owner.save(output + '/quality-100'); 104 await owner.click(927, 206, 'right'); 105 await owner.wait('QL100 gloves retained', state => owner.local(state).inventory.some(item => item.id === 8 && item.slot >= 48)); 106 const before = await state(), sequence = owner.sequence; 107 await bagClick(owner, 9); 108 await owner.save(output + '/quality-101-rejected'); 109 assert(!owner.events.some(event => event.sequence > sequence && event.direction === 'framesent' && event.opcode === '47537a24')); 110 assert.deepEqual((await state()).inventory, before.inventory); 111 assert.equal((await state()).pending, false); 112 113 // Train one Strength and Agility point through the original ability form. 114 await owner.page.keyboard.press('Control+1'); 115 await owner.page.keyboard.press('Control+9'); 116 await owner.key('KeyU'); 117 await owner.click(334, 256); 118 await owner.click(835, 166); 119 await owner.click(835, 196); 120 await owner.click(445, 622); 121 await owner.wait('trained strict thresholds', state => owner.local(state).stats[16] === 244 && 122 owner.local(state).stats[17] === 269 && owner.local(state).stats[53] === 978); 123 await bagClick(owner, 9); 124 await owner.wait('QL101 gloves worn', state => owner.local(state).inventory.some(item => item.id === 9 && item.slot === 24)); 125 assert.deepEqual((await state()).armor, [149, 149, 132, 98, 33, 98, 98, 33]); 126 const high = await state(), highSequence = owner.sequence; 127 await bagClick(owner, 10); 128 await owner.save(output + '/quality-200-rejected'); 129 assert(!owner.events.some(event => event.sequence > highSequence && event.direction === 'framesent' && event.opcode === '47537a24')); 130 assert.deepEqual((await state()).inventory, high.inventory); 131 132 // Cancel, then accept one selected-quality sale at the original armor machine. 133 if (!owner.state.camera.firstPerson) await owner.key('F8'); 134 let offered = await state(); 135 for (let attempt = 0; attempt < 2; ++attempt) { 136 const [x, y] = await owner.pick(51035, 3221226655); 137 await owner.click(x, y, 'right'); 138 await owner.wait('armor machine open', state => state.regions.some(([left, top, right, bottom]) => 139 left === 832 && top === 0 && right === 1024 && bottom === 494)); 140 const cell = owner.local().inventory.find(item => item.id === 8).slot - 48; 141 await owner.click(681 + cell % 3 * 54, 45 + Math.floor(cell / 3) * 54); 142 const sequence = owner.sequence; 143 await owner.click(864, 416); 144 await owner.wait('sale offered', () => owner.events.some(event => event.sequence > sequence && 145 event.direction === 'framesent' && event.opcode === '36284f6e')); 146 assert.deepEqual((await state()).inventory, offered.inventory); 147 await owner.save(output + '/sale-offer-' + attempt); 148 if (attempt === 0) { 149 await owner.click(945, 475); 150 await owner.wait('cancelled glove restored', state => owner.local(state).inventory.some(item => item.id === 8 && item.slot === 48)); 151 assert.deepEqual((await state()).inventory, offered.inventory); 152 offered = await state(); 153 } 154 } 155 await owner.click(874, 475); 156 await owner.wait('exact selected-quality sale', state => owner.local(state).stats[61] === 5062); 157 const final = await state(); 158 const slots = {1: 27, 2: 30, 3: 23, 4: 25, 5: 68, 6: 21, 7: 67, 9: 24, 10: 66}; 159 const expected = baseline.inventory.Items.filter(item => item.ID !== 8).map(item => ({...item, Slot: slots[item.ID]})); 160 assert.deepEqual(final.inventory.Items, expected); 161 assert.deepEqual(final.observer, baseline.observer); 162 await fs.writeFile(output + '/ledger.json', JSON.stringify(final, null, 2)); 163 await owner.page.reload(); 164 await owner.ready(); 165 await owner.page.waitForFunction(() => touchRegion(500, 400)?.world === true); 166 await owner.snapshot(); 167 const pairs = {85691: [85691, 22004], 85516: [85516, 22301], 85641: [85641, 22116], 168 85745: [85745, 21929], 85598: [85598, 22178], 85692: [85692, 22026]}; 169 for (const item of expected) { 170 const pair = pairs[item.Template]; 171 assert.deepEqual(owner.local().inventory.find(actual => actual.id === item.ID), { 172 slot: item.Slot >= 64 ? item.Slot - 16 : item.Slot, type: item.Type, id: item.ID, 173 quality: item.Quality, templates: item.Quality === 200 ? [pair[1], pair[1]] : pair, count: 1}); 174 } 175 await owner.key('KeyI'); 176 await owner.page.keyboard.press('Control+1'); 177 await owner.click(953, 53); 178 await owner.page.keyboard.press('Control+9'); 179 await owner.save(output + '/reconnected'); 180 assert(games.every(game => game.errors.length === 0)); 181 console.log('PASS: Kevlar pieces, both sleeves, QL100/101 native gates/training, exact sale and page recovery'); 182 } finally { 183 try { await Promise.all(games.map((game, index) => game.save(output + '/final-' + index))); } 184 finally { await browser.close(); } 185 }