Skip to content

Commit

Permalink
feat: Shell adapter no longer requires starting the message with the …
Browse files Browse the repository at this point in the history
…robot name.

BREAKING_CHANGE: When using the Shell adapter, it is no longer required to start with the robot name. When before you had to type `Hubot help`, now you can just type `help` and the help scripts will respond.
  • Loading branch information
joeyguerra committed May 30, 2024
1 parent 390d187 commit add95d3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/adapters/Shell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ class Shell extends Adapter {
if (stats.size > historySize) {
fs.unlinkSync(historyPath)
}

this.#rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
input: this.robot.stdin ?? process.stdin,
output: this.robot.stdout ?? process.stdout,
prompt: `${this.robot.name ?? this.robot.alias}> `,
completer
})
Expand Down Expand Up @@ -84,7 +85,11 @@ class Shell extends Adapter {
}
const userName = process.env.HUBOT_SHELL_USER_NAME || 'Shell'
const user = this.robot.brain.userForId(userId, { name: userName, room: 'Shell' })
await this.receive(new TextMessage(user, input, 'messageId'))
const message = new TextMessage(user, input, 'messageId')
if (!message.text.startsWith(this.robot.name) && !message.text.startsWith(this.robot.alias)) {
message.text = `${this.robot.name} ${message.text}`
}
await this.receive(message)
this.#rl.prompt()
})
this.#rl.on('history', async (history) => {
Expand Down
41 changes: 40 additions & 1 deletion test/Shell_test.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
'use strict'

import { describe, it, beforeEach } from 'node:test'
import { describe, it, beforeEach, afterEach } from 'node:test'
import assert from 'node:assert/strict'
import { Robot, TextMessage, User } from '../index.mjs'
import stream from 'node:stream'

describe('Shell Adapter Integration Test', () => {
let robot = null
beforeEach(async () => {
robot = new Robot('Shell', false, 'TestHubot')
robot.stdin = new stream.Readable()
robot.stdin._read = () => {}
await robot.loadAdapter()
await robot.run()
})
afterEach(() => {
robot.shutdown()
})
it('responds to a message that starts with the robot name', async () => {
let wasCalled = false
robot.respond(/helo/, async res => {
wasCalled = true
await res.reply('hello from the other side')
})
robot.stdin.push(robot.name + ' helo\n')
robot.stdin.push(null)
await new Promise(resolve => setTimeout(resolve, 60))
assert.deepEqual(wasCalled, true)
})
it('responds to a message without starting with the robot name', async () => {
let wasCalled = false
robot.respond(/helo/, async res => {
wasCalled = true
await res.reply('hello from the other side')
})
robot.stdin.push('helo\n')
robot.stdin.push(null)
await new Promise(resolve => setTimeout(resolve, 60))
assert.deepEqual(wasCalled, true)
})
})
describe('Shell Adapter', () => {
let robot = null
beforeEach(async () => {
robot = new Robot('Shell', false, 'TestHubot')
await robot.loadAdapter()
})
afterEach(() => {
robot.shutdown()
})

describe('Public API', () => {
let adapter = null
Expand Down

0 comments on commit add95d3

Please sign in to comment.