tools/client/test_basic_shop_nanos.mjs (7969B)
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', 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: 'basic-shop-nanos', 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 waitServer = (label, predicate) => game.waitFor(label, async () => predicate(await server())); 20 const cell = (type, slot) => game.state.grids.flatMap(grid => grid.cells).find(cell => 21 cell.identity[0] === type && cell.identity[1] === slot && !cell.dummy); 22 const click = async (source, button = 'left') => { 23 assert(source, 'expected native cell is present'); 24 await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2, button); 25 }; 26 await page.goto(url); 27 await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); 28 if (!game.state.camera.firstPerson) await game.key('F8'); 29 await game.key('KeyI'); 30 const initial = evidence.initial = await server(); 31 const open = async keyboard => { 32 await game.click(...await game.pick(51035, initial.machine), keyboard ? 'left' : 'right'); 33 if (keyboard) await game.key('KeyE'); 34 await game.wait('requested crystal visible in the catalog', () => cell(111, initial.row)); 35 }; 36 await open(false); 37 await game.save(output + '/catalog'); 38 await click(cell(111, initial.row)); 39 await waitServer('crystal in purchase basket', state => state.purchases === 1); 40 await click(cell(104, 48)); 41 await game.wait('sale loot picked', state => state.dragged[0] === 104); 42 await game.click(860, 412); 43 await waitServer('loot in sale basket', state => state.sales === 1); 44 await game.click(873, 475); 45 await waitServer('crystal purchase committed', state => !state.open && state.inventory.Items.length === 1 && state.inventory.Items[0].Template === initial.crystal); 46 const bought = evidence.bought = await server(); 47 assert.equal(bought.inventory.Credits, initial.creditsAfterPurchase); 48 assert.deepEqual(bought.inventory.Items.map(item => [item.ID, item.Type, item.Template, item.Quality, item.Count]), 49 [[2, 51005, initial.crystal, initial.quality, 1]]); 50 await game.wait("native purchase cash and crystal arrived", state => game.local(state).stats[61] === bought.inventory.Credits && game.local(state).inventory.some(item => item.quality === initial.quality)); 51 52 // Cancel and reopen the same terminal before consuming the exact purchase. 53 await open(true); 54 await click(cell(111, initial.row)); 55 await waitServer('cancel basket selected', state => state.purchases === 1); 56 await game.click(945, 475); 57 await waitServer('cancel completed', state => !state.open); 58 assert.deepEqual((await server()).inventory, bought.inventory); 59 await click(cell(104, bought.inventory.Items[0].Slot - 16), 'right'); 60 await waitServer('purchased crystal uploaded once', state => 61 state.inventory.Items.length === 0 && state.inventory.LearnedNanos?.includes(initial.program)); 62 await page.keyboard.press('Control+8'); 63 // Find the uploaded program among the five native school tabs. 64 for (const [x, y] of [[865, 76], [974, 76], [865, 96], [927, 96], [987, 96]]) { 65 if (cell(53019, initial.program)) break; 66 await game.click(x, y); 67 await game.snapshot(); 68 } 69 assert(cell(53019, initial.program), 'purchased program visible in its native school'); 70 if (initial.hostile) { 71 await game.key('Tab'); 72 await game.wait('native Tab selects controlled target', state => state.selected[0] === 50000 && state.selected[1] === initial.target); 73 } else { 74 await game.key('F1'); 75 } 76 const beforeCast = evidence.beforeCast = await server(); 77 const nativeArmor = initial.stat >= 90 && initial.stat <= 97 ? game.local().effectiveArmor[initial.stat - 90] : null; 78 const castSequence = game.sequence; 79 await click(cell(53019, initial.program), 'right'); 80 await waitServer('purchased nano cast completed', state => state.nano < beforeCast.nano && state.reserved === 0); 81 if (initial.minimum) { 82 await waitServer('purchased nano changes target health', state => state.health !== beforeCast.health); 83 } 84 const cast = evidence.cast = await server(); 85 if (initial.stat) { 86 assert.equal(cast.appliedBonus, initial.bonus); 87 await game.wait('original buff delivered to the client', () => game.events.some(event => { 88 if (event.sequence <= castSequence || event.direction !== 'framereceived' || event.opcode !== '5e477770') return false; 89 const packet = Buffer.from(event.hex, 'hex'); 90 return packet.readUInt32BE(29) === 177 && packet.readUInt32BE(37) === initial.program; 91 })); 92 if (nativeArmor !== null) await game.wait('native armor agrees with server', state => 93 game.local(state).effectiveArmor[initial.stat - 90] === nativeArmor + initial.bonus); 94 } 95 if (initial.program === 55992) assert(cast.rooted); 96 if (initial.minimum) { 97 const amount = initial.hostile ? beforeCast.health - cast.health : cast.health - beforeCast.health; 98 assert(amount >= initial.minimum && amount <= initial.maximum, 'authored health effect'); 99 await game.wait('native health agrees with server result', state => 100 state.actors.find(actor => actor.type === 50000 && actor.id === initial.target)?.stats[27] === cast.health); 101 } 102 if (initial.petLevel) { 103 await game.wait('purchased pet appears', state => state.actors.some(actor => actor.id === cast.pet && actor.stats[54] === initial.petLevel)); 104 await game.key('Tab'); 105 await game.wait('native enemy selection', state => state.selected[0] === 50000 && state.selected[1] === initial.enemy); 106 await game.key('Enter'); 107 await page.keyboard.type('/pet attack'); 108 await game.key('Enter'); 109 await waitServer('purchased pet kills target with owner loot rights', state => state.enemyHealth === 0 && state.looter === 1); 110 assert.equal((await server()).experience, 0, 'level-ten owner receives no XP from this level-one fixture'); 111 evidence.petKill = await server(); 112 } 113 await game.save(output + '/cast'); 114 await page.reload(); 115 await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); 116 evidence.reconnected = await server(); 117 assert.deepEqual(evidence.reconnected.inventory, cast.inventory); 118 if (initial.stat) { 119 assert.equal(evidence.reconnected.appliedBonus, initial.bonus); 120 if (nativeArmor !== null) assert.equal(game.local().effectiveArmor[initial.stat - 90], nativeArmor + initial.bonus); 121 } 122 if (initial.petLevel) { 123 await game.wait('upgraded pet reappears after reconnect', state => state.actors.some(actor => actor.id === cast.pet && actor.stats[54] === initial.petLevel)); 124 assert.equal(evidence.reconnected.experience, 0); 125 assert.equal(evidence.reconnected.enemyHealth, 0); 126 assert.equal(evidence.reconnected.looter, 1); 127 } 128 assert.equal(game.local().stats[61], cast.inventory.Credits); 129 assert.deepEqual(game.errors, []); 130 console.log('PASS:', initial.machine, 'crystal', initial.crystal, 'native buy/sell, cancel, learn/cast and reconnect'); 131 } finally { 132 try { 133 await fs.mkdir(output, {recursive: true}); 134 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 135 if (game) await game.save(output + '/final'); 136 } finally { await browser.close(); } 137 }