Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/test/externalTests/chat.js
9427 views
1
const assert = require('assert')
2
const { once, onceWithCleanup } = require('../../lib/promise_utils')
3
4
module.exports = () => {
5
async function runTest (bot, testFunction) {
6
await testFunction(bot)
7
}
8
9
const tests = []
10
11
function addTest (name, f) {
12
tests[name] = bot => runTest(bot, f)
13
}
14
15
addTest('start tests', async (bot) => {
16
await once(bot, 'message') // => <flatbot> starting chat test message event
17
})
18
19
addTest('test message event', async (bot) => {
20
await bot.test.wait(500)
21
bot.chat('/tellraw @p {"translate":"language.name"}')
22
const [json] = await once(bot, 'message')
23
const str = json.toString()
24
assert.strictEqual(str, 'English')
25
})
26
27
addTest('test chatAddPattern', async (bot) => {
28
await once(bot, 'message') // => starting chat test chatAddPattern
29
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello World!"]}')
30
const [username, message, translate, chatMessage] = await onceWithCleanup(bot, 'chat', {
31
timeout: 20000,
32
checkCondition: (username) => username === 'U9G'
33
})
34
assert.strictEqual(username, 'U9G')
35
assert.strictEqual(message, 'Hello World!')
36
assert.strictEqual(translate, 'chat.type.text')
37
assert.strictEqual(chatMessage.constructor.name, 'ChatMessage')
38
})
39
40
addTest('test addChatPattern', async (bot) => {
41
await once(bot, 'message') // => starting chat test chatAddPattern
42
bot.addChatPattern('theTest', /<.+> Hello World!!!!/, { repeat: false })
43
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello World!!!!"]}')
44
const [[match]] = await once(bot, 'chat:theTest')
45
assert.strictEqual(match, '<U9G> Hello World!!!!')
46
})
47
48
addTest('test parse', async (bot) => {
49
await once(bot, 'message') // => starting chat test chatAddPattern
50
bot.addChatPattern('theTest', /<.+> Hello World(!!!!)/, { repeat: false, parse: true })
51
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello World!!!!"]}')
52
const [[matches]] = await once(bot, 'chat:theTest')
53
assert.strictEqual(matches[0], '!!!!')
54
})
55
56
addTest('test addChatPatterns', async (bot) => {
57
await once(bot, 'message') // => starting chat test chatAddPattern
58
bot.addChatPatternSet('theTest', [/<.+> Hello/, /<.+> World/], { repeat: false })
59
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello"]}')
60
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "World"]}')
61
const [[partOne, partTwo]] = await once(bot, 'chat:theTest')
62
assert.strictEqual(partOne, '<U9G> Hello')
63
assert.strictEqual(partTwo, '<U9G> World')
64
})
65
66
addTest('test removeChatPattern', async (bot) => {
67
await once(bot, 'message') // => starting chat test removeChatPattern
68
bot.addChatPattern('test', /<.+> Hello/)
69
bot.removeChatPattern('test')
70
let triggered = false
71
const listener = () => { triggered = true }
72
bot.once('chat:test', listener)
73
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello"]}')
74
await once(bot, 'message')
75
assert.ok(triggered === false)
76
bot.off('chat:test', listener)
77
})
78
79
addTest('test awaitMessage', async (bot) => {
80
const p1 = bot.awaitMessage('<flatbot> hello')
81
bot.chat('hello')
82
await p1
83
const p2 = bot.awaitMessage(['<flatbot> hello', '<flatbot> world'])
84
bot.chat('world')
85
await p2
86
const p3 = bot.awaitMessage(/<.+> hello/)
87
bot.chat('hello')
88
await p3
89
const p4 = bot.awaitMessage([/<.+> hello/, /<.+> world/])
90
bot.chat('world')
91
await p4
92
})
93
94
addTest('test removechatpattern with a number input', async (bot) => {
95
const patternIndex = bot.addChatPattern('hello', /hello/)
96
bot.chat('hello')
97
await once(bot, 'chat:hello')
98
bot.removeChatPattern(patternIndex)
99
let listener
100
await new Promise((resolve, reject) => {
101
listener = (msg) => {
102
console.log('reacting to msg: ')
103
console.log(msg)
104
reject(new Error("Hello event shouldn't work after removing it"))
105
}
106
bot.on('chat:hello', listener)
107
bot.once('message', () => {
108
// wait half a second to make sure we aren't going to react to the msg
109
setTimeout(() => resolve(), 500)
110
})
111
bot.chat('hello')
112
})
113
bot.off('chat:hello', listener)
114
})
115
116
return tests
117
}
118
119