Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/test/externalTests/chat.js
Views: 789
const assert = require('assert')1const { once } = require('../../lib/promise_utils')23module.exports = () => {4async function runTest (bot, testFunction) {5await testFunction(bot)6}78const tests = []910function addTest (name, f) {11tests[name] = bot => runTest(bot, f)12}1314addTest('start tests', async (bot) => {15await once(bot, 'message') // => <flatbot> starting chat test message event16})1718addTest('test message event', async (bot) => {19await bot.test.wait(500)20bot.chat('/tellraw @p {"translate":"language.name"}')21const [json] = await once(bot, 'message')22const str = json.toString()23assert.strictEqual(str, 'English')24})2526addTest('test chatAddPattern', async (bot) => {27await once(bot, 'message') // => starting chat test chatAddPattern28bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello World!"]}')29const [username, message, translate, chatMessage] = await once(bot, 'chat')30assert.strictEqual(username, 'U9G')31assert.strictEqual(message, 'Hello World!')32assert.strictEqual(translate, 'chat.type.text')33assert.strictEqual(chatMessage.constructor.name, 'ChatMessage')34})3536addTest('test addChatPattern', async (bot) => {37await once(bot, 'message') // => starting chat test chatAddPattern38bot.addChatPattern('theTest', /<.+> Hello World!!!!/, { repeat: false })39bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello World!!!!"]}')40const [[match]] = await once(bot, 'chat:theTest')41assert.strictEqual(match, '<U9G> Hello World!!!!')42})4344addTest('test parse', async (bot) => {45await once(bot, 'message') // => starting chat test chatAddPattern46bot.addChatPattern('theTest', /<.+> Hello World(!!!!)/, { repeat: false, parse: true })47bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello World!!!!"]}')48const [[matches]] = await once(bot, 'chat:theTest')49assert.strictEqual(matches[0], '!!!!')50})5152addTest('test addChatPatterns', async (bot) => {53await once(bot, 'message') // => starting chat test chatAddPattern54bot.addChatPatternSet('theTest', [/<.+> Hello/, /<.+> World/], { repeat: false })55bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello"]}')56bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "World"]}')57const [[partOne, partTwo]] = await once(bot, 'chat:theTest')58assert.strictEqual(partOne, '<U9G> Hello')59assert.strictEqual(partTwo, '<U9G> World')60})6162addTest('test removeChatPattern', async (bot) => {63await once(bot, 'message') // => starting chat test removeChatPattern64bot.addChatPattern('test', /<.+> Hello/)65bot.removeChatPattern('test')66let triggered = false67const listener = () => { triggered = true }68bot.once('chat:test', listener)69bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello"]}')70await once(bot, 'message')71assert.ok(triggered === false)72bot.off('chat:test', listener)73})7475addTest('test awaitMessage', async (bot) => {76const p1 = bot.awaitMessage('<flatbot> hello')77bot.chat('hello')78await p179const p2 = bot.awaitMessage(['<flatbot> hello', '<flatbot> world'])80bot.chat('world')81await p282const p3 = bot.awaitMessage(/<.+> hello/)83bot.chat('hello')84await p385const p4 = bot.awaitMessage([/<.+> hello/, /<.+> world/])86bot.chat('world')87await p488})8990addTest('test removechatpattern with a number input', async (bot) => {91const patternIndex = bot.addChatPattern('hello', /hello/)92bot.chat('hello')93await once(bot, 'chat:hello')94bot.removeChatPattern(patternIndex)95let listener96await new Promise((resolve, reject) => {97listener = (msg) => {98console.log('reacting to msg: ')99console.log(msg)100reject(new Error("Hello event shouldn't work after removing it"))101}102bot.on('chat:hello', listener)103bot.once('message', () => {104// wait half a second to make sure we aren't going to react to the msg105setTimeout(() => resolve(), 500)106})107bot.chat('hello')108})109bot.off('chat:hello', listener)110})111112return tests113}114115116