1 | {SlackTextMessage, SlackRawMessage, SlackBotMessage, SlackRawListener, SlackBotListener} = require '../index'
|
2 |
|
3 | should = require 'should'
|
4 |
|
5 | class ClientMessage
|
6 | ts = 0
|
7 | constructor: (fields = {}) ->
|
8 | @type = 'message'
|
9 | @ts = (ts++).toString()
|
10 | @[k] = val for own k, val of fields
|
11 |
|
12 | getBody: ->
|
13 |
|
14 | text = ""
|
15 | text += @text if @text
|
16 | if @attachments
|
17 | text += "\n" if @text
|
18 | for k, attach of @attachments
|
19 | text += "\n" if k > 0
|
20 | text += attach.fallback
|
21 | text
|
22 |
|
23 | getChannelType: ->
|
24 |
|
25 | 'Channel'
|
26 |
|
27 | describe 'Receiving a Slack message', ->
|
28 | beforeEach ->
|
29 | @makeMessage = (fields = {}) =>
|
30 | msg = new ClientMessage fields
|
31 | msg.channel = @stubs.channel.id unless 'channel' of fields
|
32 | msg
|
33 |
|
34 | it 'should produce a SlackTextMessage', ->
|
35 | @slackbot.message(@makeMessage {
|
36 | user: @stubs.user.id
|
37 | text: "Hello world"
|
38 | }).then =>
|
39 | @stubs.robot.received.should.have.length 1
|
40 | msg = @stubs.robot.received[0]
|
41 | msg.should.be.an.instanceOf SlackTextMessage
|
42 | msg.text.should.equal "Hello world"
|
43 |
|
44 | it 'should parse the text in the SlackTextMessage', ->
|
45 | @slackbot.message(@makeMessage {
|
46 | user: @stubs.user.id
|
47 | text: "Foo <@U123> bar <http://slack.com>"
|
48 | }).then =>
|
49 | @stubs.robot.received.should.have.length 1
|
50 | msg = @stubs.robot.received[0]
|
51 | msg.should.be.an.instanceOf SlackTextMessage
|
52 | msg.text.should.equal "Foo @name bar http://slack.com"
|
53 | msg.rawText.should.equal "Foo <@U123> bar <http://slack.com>"
|
54 |
|
55 | it 'should include attachments in the SlackTextMessage text', ->
|
56 | @slackbot.message(@makeMessage {
|
57 | user: @stubs.user.id
|
58 | text: "Hello world"
|
59 | attachments: [
|
60 | { fallback: "attachment fallback" }
|
61 | { fallback: "second attachment fallback" }
|
62 | ]
|
63 | }).then =>
|
64 | @stubs.robot.received.should.have.length 1
|
65 | msg = @stubs.robot.received[0]
|
66 | msg.should.be.an.instanceOf SlackTextMessage
|
67 | msg.text.should.equal "Hello world\nattachment fallback\nsecond attachment fallback"
|
68 |
|
69 | it 'should save the raw message in the SlackTextMessage', ->
|
70 | @slackbot.message(rawMsg = @makeMessage {
|
71 | subtype: 'file_share'
|
72 | user: @stubs.user.id
|
73 | text: "Hello world"
|
74 | file:
|
75 | name: "file.txt"
|
76 | }).then =>
|
77 | @stubs.robot.received.should.have.length 1
|
78 | msg = @stubs.robot.received[0]
|
79 | msg.should.be.an.instanceOf SlackTextMessage
|
80 | msg.rawMessage.should.equal rawMsg
|
81 |
|
82 | it 'should produce a SlackBotMessage when the subtype is bot_message', ->
|
83 | @slackbot.message(rawMsg = @makeMessage {
|
84 | subtype: 'bot_message'
|
85 | username: 'bot'
|
86 | text: 'Hello world'
|
87 | attachments: [{
|
88 | fallback: 'attachment'
|
89 | }]
|
90 | }).then =>
|
91 | @stubs.robot.received.should.have.length 1
|
92 | msg = @stubs.robot.received[0]
|
93 | msg.should.be.an.instanceOf SlackBotMessage
|
94 | msg.text.should.equal "Hello world\nattachment"
|
95 | msg.rawMessage.should.equal rawMsg
|
96 |
|
97 | it 'should produce a SlackRawMessage when the user is nil', ->
|
98 | @slackbot.message(rawMsg = @makeMessage {
|
99 | text: 'Hello world'
|
100 | }).then =>
|
101 | @stubs.robot.received.should.have.length 1
|
102 | msg = @stubs.robot.received[0]
|
103 | msg.should.be.an.instanceOf SlackRawMessage
|
104 | msg.text.should.equal 'Hello world'
|
105 | msg.rawMessage.should.equal rawMsg
|
106 |
|
107 | it 'should produce a SlackRawMessage when the message is hidden', ->
|
108 | @slackbot.message(@makeMessage {
|
109 | hidden: true
|
110 | user: @stubs.user.id
|
111 | text: 'Hello world'
|
112 | }).then =>
|
113 | @stubs.robot.received.should.have.length 1
|
114 | msg = @stubs.robot.received[0]
|
115 | msg.should.be.an.instanceOf SlackRawMessage
|
116 |
|
117 | it 'should produce a SlackRawMessage when the message has no body', ->
|
118 | @slackbot.message(@makeMessage {
|
119 | user: @stubs.user.id
|
120 | }).then =>
|
121 | @stubs.robot.received.should.have.length 1
|
122 | msg = @stubs.robot.received[0]
|
123 | msg.should.be.an.instanceOf SlackRawMessage
|
124 |
|
125 | it 'should produce a SlackRawMessage when the message has no channel', ->
|
126 | @slackbot.message(new ClientMessage {
|
127 | type: 'message'
|
128 | user: @stubs.user.id
|
129 | text: 'Hello world'
|
130 | ts: "1234"
|
131 | }).then =>
|
132 | @stubs.robot.received.should.have.length 1
|
133 | msg = @stubs.robot.received[0]
|
134 | msg.should.be.an.instanceOf SlackRawMessage
|
135 |
|
136 | describe 'should handle SlackRawMessage inheritance properly when brobbot', ->
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | beforeEach ->
|
142 | mods = (require.resolve name for name in ['brobbot', '../src/message'])
|
143 | @saved = []
|
144 |
|
145 | require path for path in mods
|
146 | @saved = (require.cache[path] for path in mods)
|
147 | delete require.cache[path] for path in mods
|
148 |
|
149 | afterEach ->
|
150 |
|
151 | for mod in @saved
|
152 | require.cache[mod.filename] = mod
|
153 | delete @saved
|
154 |
|
155 | it 'does export Message', ->
|
156 | if not require('brobbot').Message
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | `function Message() {}`
|
162 | require('brobbot').Message = Message
|
163 | {SlackRawMessage: rawMessage} = require '../src/message'
|
164 | rawMessage::constructor.__super__.constructor.name.should.equal 'Message'
|