From cebfb1e92d70dbe04b64d3cc95a3e558a4901987 Mon Sep 17 00:00:00 2001 From: Tim Kinnane Date: Sun, 9 Sep 2018 12:32:07 +1000 Subject: [PATCH] fix(branch): Bug that caused consecutive direct branches not to match --- src/lib/branch.spec.ts | 10 ++++++++++ src/lib/branch.ts | 9 ++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/lib/branch.spec.ts b/src/lib/branch.spec.ts index 4f8d3bf..44d6871 100644 --- a/src/lib/branch.spec.ts +++ b/src/lib/branch.spec.ts @@ -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) => { diff --git a/src/lib/branch.ts b/src/lib/branch.ts index 58b7d5f..87e7146 100644 --- a/src/lib/branch.ts +++ b/src/lib/branch.ts @@ -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 }