Skip to content

Commit 131a7ea

Browse files
kaduvertrom1504
andauthored
Extended useChests.js tests (#3132)
* added click-type-independant randomized testing * fix lint * remove dependency * fix * fix 2 * prevent giving the bot air * test ci * hopefully fix for all versions * remove ci testing file * optimize factors for maximal testing * small optimization --------- Co-authored-by: Romain Beaumont <[email protected]>
1 parent a315653 commit 131a7ea

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test/externalTests/useChests.js

+56
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { Vec3 } = require('vec3')
22
const assert = require('assert')
33
const { once } = require('events')
4+
const { onceWithCleanup } = require('../../lib/promise_utils')
45

56
module.exports = () => async (bot) => {
67
const Item = require('prismarine-item')(bot.registry)
@@ -126,4 +127,59 @@ module.exports = () => async (bot) => {
126127

127128
await withdrawBones(smallTrappedChestLocation, 1)
128129
await withdrawBones(largeTrappedChestLocations[0], 2)
130+
131+
const itemsWithStackSize = {
132+
64: ['stone', 'grass'],
133+
16: ['ender_pearl', 'egg'],
134+
1: ['fishing_rod', 'bow']
135+
}
136+
137+
function getRandomStackableItem () {
138+
if (Math.random() < 0.75) {
139+
return itemsWithStackSize[64][~~(Math.random() * itemsWithStackSize[64].length)]
140+
} else {
141+
if (Math.random() < 0.5) {
142+
return itemsWithStackSize[16][~~(Math.random() * itemsWithStackSize[16].length)]
143+
} else {
144+
return itemsWithStackSize[1][~~(Math.random() * itemsWithStackSize[1].length)]
145+
}
146+
}
147+
}
148+
149+
async function createRandomLayout (window, slotPopulationFactor) {
150+
await bot.test.becomeCreative()
151+
152+
for (let slot = 0; slot < window.inventoryStart; slot++) {
153+
if (Math.random() < slotPopulationFactor) {
154+
const item = bot.registry.itemsByName[getRandomStackableItem()]
155+
bot.chat(`/give ${bot.username} ${item.name} ${Math.ceil(Math.random() * item.stackSize)}`)
156+
await onceWithCleanup(window, 'updateSlot', { checkCondition: (slot, oldItem, newItem) => slot === window.hotbarStart && newItem?.name === item.name })
157+
158+
// await bot.clickWindow(slot, 0, 2)
159+
await bot.moveSlotItem(window.hotbarStart, slot)
160+
}
161+
}
162+
163+
await bot.test.becomeSurvival()
164+
}
165+
166+
async function testMouseClick (window, clicks) {
167+
let iterations = 0
168+
while (iterations++ < clicks) {
169+
await bot.clickWindow(~~(Math.random() * window.inventoryStart), 0, 0)
170+
}
171+
}
172+
173+
function clearLargeChest () {
174+
bot.chat(`/setblock ${largeChestLocations[0].x} ${largeChestLocations[0].y} ${largeChestLocations[0].z} chest`)
175+
bot.chat(`/setblock ${largeChestLocations[1].x} ${largeChestLocations[1].y} ${largeChestLocations[1].z} chest`)
176+
}
177+
178+
const window = await bot.openContainer(bot.blockAt(largeChestLocations[0]))
179+
await createRandomLayout(window, 0.95)
180+
181+
await testMouseClick(window, 250)
182+
183+
window.close()
184+
clearLargeChest()
129185
}

0 commit comments

Comments
 (0)