Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/test/diggingDeathTest.js
9427 views
1
/* eslint-env mocha */
2
3
const EventEmitter = require('events')
4
const assert = require('assert')
5
const inject = require('../lib/plugins/digging')
6
7
describe('digging plugin death handler', () => {
8
function createMockBot () {
9
const bot = new EventEmitter()
10
// The digging plugin assigns these on the bot
11
bot.targetDigBlock = null
12
bot.targetDigFace = null
13
bot.lastDigTime = null
14
// stopDigging is set by the plugin, but starts as the noop at bottom of digging.js
15
// We don't pre-set it so the plugin can assign it
16
// Provide minimal _client stub for stopDigging path (write is called during cancel)
17
bot._client = { write: () => {} }
18
// entity stub needed if canDigBlock or digTime are called
19
bot.entity = {
20
position: { x: 0, y: 0, z: 0, offset: () => ({ x: 0, y: 0, z: 0, distanceTo: () => 0 }) },
21
isInWater: false,
22
onGround: true,
23
eyeHeight: 1.62,
24
effects: {}
25
}
26
bot.heldItem = null
27
bot.game = { gameMode: 'survival' }
28
bot.inventory = { slots: [] }
29
bot.getEquipmentDestSlot = () => 5
30
bot.swingArm = () => {}
31
bot.world = { raycast: () => null }
32
return bot
33
}
34
35
it('should not throw when death is emitted and no digging is in progress', () => {
36
const bot = createMockBot()
37
inject(bot)
38
// Simply emitting death should not throw
39
assert.doesNotThrow(() => {
40
bot.emit('death')
41
})
42
})
43
44
it('should not throw when death is emitted and stopDigging throws', () => {
45
const bot = createMockBot()
46
inject(bot)
47
// Simulate a broken stopDigging that throws
48
bot.stopDigging = () => {
49
throw new Error('unexpected state error')
50
}
51
// The try/catch in the death handler should swallow this
52
assert.doesNotThrow(() => {
53
bot.emit('death')
54
})
55
})
56
57
it('should not throw when death is emitted with problematic event listeners', () => {
58
const bot = createMockBot()
59
inject(bot)
60
// Add a listener for diggingAborted that would be removed
61
bot.on('diggingAborted', () => {})
62
bot.on('diggingCompleted', () => {})
63
// Override removeAllListeners to throw (simulating the crash scenario)
64
const origRemoveAll = bot.removeAllListeners.bind(bot)
65
bot.removeAllListeners = (event) => {
66
if (event === 'diggingAborted') {
67
throw new Error('Cannot read properties of undefined')
68
}
69
return origRemoveAll(event)
70
}
71
// The try/catch should protect against this
72
assert.doesNotThrow(() => {
73
bot.emit('death')
74
})
75
})
76
77
it('should clean up digging listeners on death when digging is active', () => {
78
const bot = createMockBot()
79
inject(bot)
80
// Simulate active listeners
81
bot.on('diggingAborted', () => {})
82
bot.on('diggingCompleted', () => {})
83
assert.ok(bot.listenerCount('diggingAborted') > 0)
84
assert.ok(bot.listenerCount('diggingCompleted') > 0)
85
bot.emit('death')
86
// After death, digging listeners should be removed
87
assert.strictEqual(bot.listenerCount('diggingAborted'), 0)
88
assert.strictEqual(bot.listenerCount('diggingCompleted'), 0)
89
})
90
})
91
92