From 52fea2a18a3882c13dbb9c0154bbac7c50f21475 Mon Sep 17 00:00:00 2001 From: Alexander Jayaraj Date: Thu, 2 Nov 2017 13:48:39 +0530 Subject: [PATCH 1/2] Resolving issue #375. During conversation the search should remain in the current topic --- src/bot/db/import.js | 1 + src/bot/db/models/gambit.js | 3 +++ src/bot/getReply/index.js | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/src/bot/db/import.js b/src/bot/db/import.js index a419e819..7c82f7d3 100755 --- a/src/bot/db/import.js +++ b/src/bot/db/import.js @@ -21,6 +21,7 @@ const rawToGambitData = function rawToGambitData(gambitId, gambit) { filter: gambit.trigger.filter, trigger: gambit.trigger.clean, input: gambit.trigger.raw, + topic: gambit.topic }; // Conditional rolled up triggers will not have a flags diff --git a/src/bot/db/models/gambit.js b/src/bot/db/models/gambit.js index 2ab7d4cc..8a42161a 100644 --- a/src/bot/db/models/gambit.js +++ b/src/bot/db/models/gambit.js @@ -57,6 +57,9 @@ const createGambitModel = function createGambitModel(db, factSystem) { // TODO, change the type to a ID and reference another gambit directly // this will save us a lookup down the road (and improve performace.) redirect: { type: String, default: '' }, + + // topic field added to fix issue#375 + topic: { type: String, default: 'random'} }); gambitSchema.pre('save', function (next) { diff --git a/src/bot/getReply/index.js b/src/bot/getReply/index.js index 0619a797..3030d802 100644 --- a/src/bot/getReply/index.js +++ b/src/bot/getReply/index.js @@ -72,6 +72,18 @@ const findMatches = async function findMatches(pendingTopics, messageObject, opt debug.info(`Replies: ${match.gambit.replies.map(reply => reply.reply).join('\n')}`); }); + // If there are multiple matches, then select the one from topic. Issue #375 + function isTopicMatched(match) { + return match.gambit.topic === options.user.getTopic(); + } + var topicIndex = unfilteredMatches.findIndex(isTopicMatched); + + if (unfilteredMatches.length > 1 && topicIndex > 0) { + var tmp = unfilteredMatches[0]; + unfilteredMatches[0] = unfilteredMatches[topicIndex]; + unfilteredMatches[topicIndex] = tmp; + } + for (let j = 0; j < unfilteredMatches.length && !stopSearching; ++j) { const match = unfilteredMatches[j]; const reply = await matchItorHandle(match, messageObject, options); From 6960aae4ad7bf89b765f25791009ddacabf1dada Mon Sep 17 00:00:00 2001 From: Alexander Jayaraj Date: Thu, 2 Nov 2017 14:23:20 +0530 Subject: [PATCH 2/2] Added test cases for issue #375 --- test/fixtures/topicconversation/main.ss | 34 ++++++++++++++ test/topicconversation.js | 62 +++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 test/fixtures/topicconversation/main.ss create mode 100644 test/topicconversation.js diff --git a/test/fixtures/topicconversation/main.ss b/test/fixtures/topicconversation/main.ss new file mode 100644 index 00000000..848f61a0 --- /dev/null +++ b/test/fixtures/topicconversation/main.ss @@ -0,0 +1,34 @@ ++ I love animals +- ^topicRedirect("animals", "__favorite__") + ++ I love pets +- ^topicRedirect("pets", "__favorite__") + + +> topic animals {keep} + + __favorite__ + - I don't like pets, what is your favorite? + + + Mine is cat + - Cats are scary, what is your favorite? + + whatever + % * what is your favorite * + -Cats are scary + + + i do love animals + - Yes, I do as well +< topic + +> topic pets {keep} + + __favorite__ + - Me too, what is your favorite? + + + Mine is cat + - I love it too, what is your favorite ? + + whatever + % * what is your favorite * + -Cats are fun + + + i do love animals + - No, I like only cats +< topic \ No newline at end of file diff --git a/test/topicconversation.js b/test/topicconversation.js new file mode 100644 index 00000000..60cc4e81 --- /dev/null +++ b/test/topicconversation.js @@ -0,0 +1,62 @@ +/* global describe, it, before, after */ + +import mocha from 'mocha'; +import should from 'should/as-function'; +import helpers from './helpers'; +import { doesMatch, doesMatchTopic } from '../src/bot/getReply/helpers'; + + +// Testing topics that include and mixin other topics. +describe('SuperScript TopicsConversation', () => { + before(helpers.before('topicconversation')); + + describe('TopicConversationPets', () => { + it('Conversation begin', (done) => { + helpers.getBot().reply('tc_user', 'i love pets', (err, reply) => { + should('Me too, what is your favorite?').eql(reply.string); + done(); + }); + }); + + it('Inside the pets topic', (done) => { + helpers.getBot().reply('tc_user', 'mine is cat', (err, reply) => { + should(reply.string).eql('I love it too, what is your favorite ?'); + done(); + }); + }); + + it('Should remain in the pets topic', (done) => { + helpers.getBot().reply('tc_user', 'whatever', (err, reply) => { + should(reply.string).eql('Cats are fun'); + done(); + }); + }); + + }); + + describe('TopicConversationAnimals', () => { + it('Conversation begin', (done) => { + helpers.getBot().reply('tc_user', 'i love animals', (err, reply) => { + should('I don\'t like pets, what is your favorite?').eql(reply.string); + done(); + }); + }); + + it('Inside the pets topic', (done) => { + helpers.getBot().reply('tc_user', 'mine is cat', (err, reply) => { + should(reply.string).eql('Cats are scary, what is your favorite?'); + done(); + }); + }); + + it('Should remain in the pets topic', (done) => { + helpers.getBot().reply('tc_user', 'whatever', (err, reply) => { + should(reply.string).eql('Cats are scary'); + done(); + }); + }); + + }); + + after(helpers.after); +});