hubot-slack
Version:
A Slack adapter for hubot
221 lines (182 loc) • 9.22 kB
text/coffeescript
should = require 'should'
SlackMention = require('../src/mention')
describe 'buildText()', ->
it 'Should decode entities', ->
message =
message.rawMessage.text = 'foo > & < >&<'
message.buildText , () ->
message.text.should.equal('foo > & < >&<')
it 'Should remove formatting around <http> links', ->
message =
message.rawMessage.text = 'foo <http://www.example.com> bar'
message.buildText , () ->
message.text.should.equal 'foo http://www.example.com bar'
it 'Should remove formatting around <https> links', ->
message =
message.rawMessage.text = 'foo <https://www.example.com> bar'
message.buildText , () ->
message.text.should.equal 'foo https://www.example.com bar'
it 'Should remove formatting around <skype> links', ->
message =
message.rawMessage.text = 'foo <skype:echo123?call> bar'
message.buildText , () ->
message.text.should.equal 'foo skype:echo123?call bar'
it 'Should remove formatting around <https> links with a label', ->
message =
message.rawMessage.text = 'foo <https://www.example.com|label> bar'
message.buildText , () ->
message.text.should.equal 'foo label (https://www.example.com) bar'
it 'Should remove formatting around <https> links with a substring label', ->
message =
message.rawMessage.text = 'foo <https://www.example.com|example.com> bar'
message.buildText , () ->
message.text.should.equal 'foo https://www.example.com bar'
it 'Should remove formatting around <https> links with a label containing entities', ->
message =
message.rawMessage.text = 'foo <https://www.example.com|label > & <> bar'
message.buildText , () ->
message.text.should.equal 'foo label > & < (https://www.example.com) bar'
it 'Should remove formatting around <mailto> links', ->
message =
message.rawMessage.text = 'foo <mailto:name@example.com> bar'
message.buildText , () ->
message.text.should.equal 'foo name@example.com bar'
it 'Should remove formatting around <mailto> links with an email label', ->
message =
message.rawMessage.text = 'foo <mailto:name@example.com|name@example.com> bar'
message.buildText , () ->
message.text.should.equal 'foo name@example.com bar'
it 'Should handle empty text with attachments', ->
message =
message.rawMessage.text = undefined
message.rawMessage.attachments = [
{ fallback: 'first' },
]
message.buildText , () ->
message.text.should.equal '\nfirst'
it 'Should handle an empty set of attachments', ->
message =
message.rawMessage.text = 'foo'
message.rawMessage.attachments = []
message.buildText , () ->
message.text.should.equal 'foo'
it 'Should change multiple links at once', ->
message =
message.rawMessage.text = 'foo <@U123|label> bar <#C123> <!channel> <https://www.example.com|label>'
message.buildText , () ->
message.text.should.equal 'foo @label bar #general @channel label (https://www.example.com)'
it 'Should populate mentions with simple SlackMention object', ->
message =
message.rawMessage.text = 'foo <@U123> bar'
message.buildText , () ->
message.mentions.length.should.equal 1
message.mentions[0].type.should.equal 'user'
message.mentions[0].id.should.equal 'U123'
should.equal (message.mentions[0] instanceof SlackMention), true
it 'Should populate mentions with simple SlackMention object with label', ->
message =
message.rawMessage.text = 'foo <@U123|label> bar'
message.buildText , () ->
message.mentions.length.should.equal 1
message.mentions[0].type.should.equal 'user'
message.mentions[0].id.should.equal 'U123'
should.equal message.mentions[0].info, undefined
should.equal (message.mentions[0] instanceof SlackMention), true
it 'Should populate mentions with multiple SlackMention objects', ->
message =
message.rawMessage.text = 'foo <@U123> bar <#C123> baz <@U123|label> qux'
message.buildText , () ->
message.mentions.length.should.equal 3
should.equal (message.mentions[0] instanceof SlackMention), true
should.equal (message.mentions[1] instanceof SlackMention), true
should.equal (message.mentions[2] instanceof SlackMention), true
it 'Should populate mentions with simple SlackMention object if user in brain', ->
.updateUserInBrain(.user)
message =
message.rawMessage.text = 'foo <@U123> bar'
message.buildText , () ->
message.mentions.length.should.equal 1
message.mentions[0].type.should.equal 'user'
message.mentions[0].id.should.equal 'U123'
should.equal (message.mentions[0] instanceof SlackMention), true
it 'Should add conversation to cache', ->
message =
client =
message.rawMessage.text = 'foo bar'
message.buildText , () ->
message.text.should.equal('foo bar')
client.channelData.should.have.key('C123')
it 'Should not modify conversation if it is not expired', ->
message =
client =
client.channelData[.channel.id] = {
channel: {id: .channel.id, name: 'baz'},
updated: Date.now()
}
message.rawMessage.text = 'foo bar'
message.buildText , () ->
message.text.should.equal('foo bar')
client.channelData.should.have.key('C123')
client.channelData['C123'].channel.name.should.equal 'baz'
it 'Should handle conversation errors', ->
message =
client =
message.rawMessage.text = 'foo bar'
message.buildText , () ->
client.robot.logger.logs?.error.length.should.equal 1
it 'Should flatten attachments', ->
message =
client =
message.rawMessage.text = 'foo bar'
message.rawMessage.attachments = [
{ fallback: 'first' },
{ fallback: 'second' }
]
message.buildText , () ->
message.text.should.equal 'foo bar\nfirst\nsecond'
describe 'replaceLinks()', ->
it 'Should change <@U123> links to @name', ->
.replaceLinks , 'foo <@U123> bar'
.then((text) -> text.should.equal 'foo @name bar')
it 'Should change <@U123|label> links to @label', ->
.replaceLinks , 'foo <@U123|label> bar'
.then((text) -> text.should.equal 'foo @label bar')
it 'Should handle invalid User ID gracefully', ->
.replaceLinks , 'foo <@U555> bar'
.then((text) -> text.should.equal 'foo <@U555> bar')
it 'Should handle empty User API response', ->
.replaceLinks , 'foo <@U789> bar'
.then((text) -> text.should.equal 'foo <@U789> bar')
it 'Should change <#C123> links to #general', ->
.replaceLinks , 'foo <#C123> bar'
.then((text) -> text.should.equal 'foo #general bar')
it 'Should change <#C123|label> links to #label', ->
.replaceLinks , 'foo <#C123|label> bar'
.then((text) -> text.should.equal 'foo #label bar')
it 'Should handle invalid Conversation ID gracefully', ->
.replaceLinks , 'foo <#C555> bar'
.then((text) -> text.should.equal 'foo <#C555> bar')
it 'Should handle empty Conversation API response', ->
.replaceLinks , 'foo <#C789> bar'
.then((text) -> text.should.equal 'foo <#C789> bar')
it 'Should change <!everyone> links to @everyone', ->
.replaceLinks , 'foo <!everyone> bar'
.then((text) -> text.should.equal 'foo @everyone bar')
it 'Should change <!channel> links to @channel', ->
.replaceLinks , 'foo <!channel> bar'
.then((text) -> text.should.equal 'foo @channel bar')
it 'Should change <!group> links to @group', ->
.replaceLinks , 'foo <!group> bar'
.then((text) -> text.should.equal 'foo @group bar')
it 'Should change <!here> links to @here', ->
.replaceLinks , 'foo <!here> bar'
.then((text) -> text.should.equal 'foo @here bar')
it 'Should change <!subteam^S123|@subteam> links to @subteam', ->
.replaceLinks , 'foo <!subteam^S123|@subteam> bar'
.then((text) -> text.should.equal 'foo @subteam bar')
it 'Should change <!foobar|hello> links to hello', ->
.replaceLinks , 'foo <!foobar|hello> bar'
.then((text) -> text.should.equal 'foo hello bar')
it 'Should leave <!foobar> links as-is when no label is provided', ->
.replaceLinks , 'foo <!foobar> bar'
.then((text) -> text.should.equal 'foo <!foobar> bar')