Skip to content

Commit 4750021

Browse files
authored
fix: messages not getting handled when linking to a local module for development (#1814)
* fix: when doing local development and linking to a local module, the instanceof check results in skipped messages because TextMessage from different sources don't pass that check * implement copilot review suggestions
1 parent 5a0ce04 commit 4750021

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/Listener.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
import { inspect } from 'node:util'
4-
import { TextMessage } from './Message.mjs'
54
import Middleware from './Middleware.mjs'
65

76
class Listener {
@@ -97,7 +96,7 @@ class TextListener extends Listener {
9796
// callback - A Function that is triggered if the incoming message matches.
9897
constructor (robot, regex, options, callback) {
9998
function matcher (message) {
100-
if (message instanceof TextMessage) {
99+
if (typeof message.match === 'function') {
101100
return message.match(regex)
102101
}
103102
}

test/Listener_test.mjs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,32 @@ describe('Listener', () => {
290290
it('does not match EnterMessages', () => {
291291
const callback = async () => {}
292292
const testMessage = new EnterMessage(user)
293-
testMessage.match = () => {
294-
assert.fail('match should not be called')
295-
}
296293
const testRegex = /test/
297294

298295
const testListener = new TextListener(robot, testRegex, callback)
299296
const result = testListener.matcher(testMessage)
300297

301298
assert.deepEqual(result, undefined)
302299
})
300+
301+
it('matches non-TextMessage objects with a match function (duck-typing regression test)', () => {
302+
const callback = async () => {}
303+
const testRegex = /test/
304+
// Simulate a message from a linked module that isn't an instanceof TextMessage
305+
const nonTextMessage = {
306+
user: 'testuser',
307+
text: 'test message',
308+
match (regex) {
309+
assert.deepEqual(regex, testRegex)
310+
return true
311+
}
312+
}
313+
314+
const testListener = new TextListener(robot, testRegex, callback)
315+
const result = testListener.matcher(nonTextMessage)
316+
317+
assert.ok(result)
318+
})
303319
})
304320
)
305321
})

0 commit comments

Comments
 (0)