CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/test/externalTests/chat.js
Views: 789
1
const assert = require('assert')
2
const { once } = 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 once(bot, 'chat')
31
assert.strictEqual(username, 'U9G')
32
assert.strictEqual(message, 'Hello World!')
33
assert.strictEqual(translate, 'chat.type.text')
34
assert.strictEqual(chatMessage.constructor.name, 'ChatMessage')
35
})
36
37
addTest('test addChatPattern', async (bot) => {
38
await once(bot, 'message') // => starting chat test chatAddPattern
39
bot.addChatPattern('theTest', /<.+> Hello World!!!!/, { repeat: false })
40
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello World!!!!"]}')
41
const [[match]] = await once(bot, 'chat:theTest')
42
assert.strictEqual(match, '<U9G> Hello World!!!!')
43
})
44
45
addTest('test parse', async (bot) => {
46
await once(bot, 'message') // => starting chat test chatAddPattern
47
bot.addChatPattern('theTest', /<.+> Hello World(!!!!)/, { repeat: false, parse: true })
48
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello World!!!!"]}')
49
const [[matches]] = await once(bot, 'chat:theTest')
50
assert.strictEqual(matches[0], '!!!!')
51
})
52
53
addTest('test addChatPatterns', async (bot) => {
54
await once(bot, 'message') // => starting chat test chatAddPattern
55
bot.addChatPatternSet('theTest', [/<.+> Hello/, /<.+> World/], { repeat: false })
56
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello"]}')
57
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "World"]}')
58
const [[partOne, partTwo]] = await once(bot, 'chat:theTest')
59
assert.strictEqual(partOne, '<U9G> Hello')
60
assert.strictEqual(partTwo, '<U9G> World')
61
})
62
63
addTest('test removeChatPattern', async (bot) => {
64
await once(bot, 'message') // => starting chat test removeChatPattern
65
bot.addChatPattern('test', /<.+> Hello/)
66
bot.removeChatPattern('test')
67
let triggered = false
68
const listener = () => { triggered = true }
69
bot.once('chat:test', listener)
70
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello"]}')
71
await once(bot, 'message')
72
assert.ok(triggered === false)
73
bot.off('chat:test', listener)
74
})
75
76
addTest('test awaitMessage', async (bot) => {
77
const p1 = bot.awaitMessage('<flatbot> hello')
78
bot.chat('hello')
79
await p1
80
const p2 = bot.awaitMessage(['<flatbot> hello', '<flatbot> world'])
81
bot.chat('world')
82
await p2
83
const p3 = bot.awaitMessage(/<.+> hello/)
84
bot.chat('hello')
85
await p3
86
const p4 = bot.awaitMessage([/<.+> hello/, /<.+> world/])
87
bot.chat('world')
88
await p4
89
})
90
91
addTest('test removechatpattern with a number input', async (bot) => {
92
const patternIndex = bot.addChatPattern('hello', /hello/)
93
bot.chat('hello')
94
await once(bot, 'chat:hello')
95
bot.removeChatPattern(patternIndex)
96
let listener
97
await new Promise((resolve, reject) => {
98
listener = (msg) => {
99
console.log('reacting to msg: ')
100
console.log(msg)
101
reject(new Error("Hello event shouldn't work after removing it"))
102
}
103
bot.on('chat:hello', listener)
104
bot.once('message', () => {
105
// wait half a second to make sure we aren't going to react to the msg
106
setTimeout(() => resolve(), 500)
107
})
108
bot.chat('hello')
109
})
110
bot.off('chat:hello', listener)
111
})
112
113
return tests
114
}
115
116