Skip to content

Commit

Permalink
fix(branch): Bug that caused consecutive direct branches not to match
Browse files Browse the repository at this point in the history
  • Loading branch information
timkinnane committed Sep 9, 2018
1 parent ec85334 commit cebfb1e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/lib/branch.spec.ts
Expand Up @@ -241,6 +241,16 @@ describe('[branch]', () => {
message: new bot.TextMessage(user, `${bot.settings.get('name')} foo`)
}), middleware)
})
it('.process returns match on consecutive direct branch', async () => {
const directFoo = new bot.TextDirectBranch(/foo/, () => null)
const directBar = new bot.TextDirectBranch(/bar/, () => null)
const b = new bot.State({
message: new bot.TextMessage(user, `${bot.settings.get('name')} bar`)
})
await directFoo.process(b, middleware)
await directBar.process(b, middleware)
expect(b.branches).to.eql([directBar])
})
it('.process adds condition match results to state', () => {
const conditions = [{ starts: 'foo' }, { ends: 'bar' }]
const branch = new bot.TextDirectBranch(conditions, (b) => {
Expand Down
9 changes: 6 additions & 3 deletions src/lib/branch.ts
Expand Up @@ -165,13 +165,16 @@ export class TextBranch extends Branch {

/**
* Text Direct Branch pre-matches the text for bot name prefix.
* Once matched, it removes the direct pattern from the message text.
* If matched on the direct pattern (name prefix) it runs the branch matcher on
* a clone of the message with the prefix removed, this allows conditions like
* `is` to operate on the body of the message, without failing due to a prefix.
*/
export class TextDirectBranch extends TextBranch {
async matcher (message: bot.TextMessage) {
if (directPattern().exec(message.toString())) {
message.text = message.text.replace(directPattern(), '')
return super.matcher(message)
const indirectMessage = message.clone()
indirectMessage.text = message.text.replace(directPattern(), '')
return super.matcher(indirectMessage)
} else {
return false
}
Expand Down

0 comments on commit cebfb1e

Please sign in to comment.