-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathHubot_test.mjs
More file actions
122 lines (117 loc) · 4.2 KB
/
Copy pathHubot_test.mjs
File metadata and controls
122 lines (117 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict'
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import path from 'node:path'
import { spawn } from 'node:child_process'
import { TextMessage, User } from '../index.mjs'
import { fileURLToPath } from 'node:url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const root = __dirname.replace(/test$/, '')
describe('Running bin/Hubot.mjs', () => {
it('should load adapter from HUBOT_FILE environment variable', async () => {
process.env.HUBOT_HTTPD = 'false'
process.env.HUBOT_FILE = path.resolve(root, 'test', 'fixtures', 'MockAdapter.mjs')
const hubot = (await import('../bin/Hubot.mjs')).default
await hubot.loadFile(path.resolve(root, 'test', 'fixtures'), 'TestScript.mjs')
while (!hubot.adapter) {
await new Promise(resolve => setTimeout(resolve, 100))
}
hubot.adapter.on('reply', (envelope, ...strings) => {
assert.equal(strings[0], 'test response from .mjs script')
delete process.env.HUBOT_FILE
delete process.env.HUBOT_HTTPD
hubot.shutdown()
})
try {
await hubot.receive(new TextMessage(new User('mocha', { room: '#mocha' }), '@Hubot test'))
assert.deepEqual(hubot.hasLoadedTestMjsScript, true)
assert.equal(hubot.name, 'Hubot')
} finally {
hubot.shutdown()
}
})
it('should output a help message when run with --help', (t, done) => {
const hubot = process.platform === 'win32' ? spawn('node', ['./bin/Hubot.mjs', '--help']) : spawn('./bin/hubot', ['--help'])
const expected = `Usage: hubot [options]
-a, --adapter HUBOT_ADAPTER
-f, --file HUBOT_FILE
-c, --create HUBOT_CREATE
-d, --disable-httpd HUBOT_HTTPD
-s, --httpdserver HUBOT_HTTPD_SERVER
-h, --help
-l, --alias HUBOT_ALIAS
-n, --name HUBOT_NAME
-r, --require PATH
-t, --config-check
-v, --version
-e, --execute
`
let actual = ''
hubot.stdout.on('data', (data) => {
actual += data.toString()
})
hubot.stderr.on('data', (data) => {
actual += data.toString()
})
hubot.on('close', (code) => {
assert.deepEqual(actual, expected)
done()
})
})
it('should execute the command when run with --execute or -e', (t, done) => {
const expected = "HELO World! I'm Hubot."
const commandText = 'helo'
const env = Object.assign({}, process.env, { NOLOG: 'off' })
const hubot = process.platform === 'win32'
? spawn('node', ['./bin/Hubot.mjs', '-d', '--execute', commandText, '-r', 'test/scripts'], { env })
: spawn('./bin/hubot', ['-d', '--execute', commandText, '-r', 'test/scripts'], { env })
let actual = ''
hubot.stdout.on('data', (data) => {
actual += data.toString()
})
hubot.stderr.on('data', (data) => {
actual += data.toString()
})
hubot.on('close', (code) => {
assert.ok(actual.includes(expected))
done()
})
})
})
describe('Running hubot with args', () => {
it('should not start web service when --disable-httpd is passed', (t, done) => {
const hubot = process.platform === 'win32' ? spawn('node', ['./bin/Hubot.mjs', '--disable-httpd']) : spawn('./bin/hubot', ['--disable-httpd'])
let actual = {}
const logMessages = []
hubot.stdout.on('data', (data) => {
console.log(data.toString())
logMessages.push(data.toString())
})
hubot.stderr.on('data', (data) => {
console.log(data.toString())
logMessages.push(data.toString())
})
const interval = setInterval(async () => {
if (logMessages.some(m => m.includes('EADDRINUSE'))) {
clearInterval(interval)
assert.fail('Web service started when --disable-httpd was passed')
done()
}
if (logMessages.some(m => m.includes('No external-scripts.json found. Skipping'))) {
clearInterval(interval)
try {
const response = await fetch('http://localhost:8080')
actual = await response.text()
} catch (e) {
actual = e
} finally {
hubot.kill()
}
assert.ok(actual instanceof TypeError)
assert.deepEqual(actual.message, 'fetch failed', 'this is an expected failure since the web service should not be running')
done()
}
}, 60)
})
})