-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests & fixes for story change mid session bug. (#27)
Tests & fixes for story change mid session bug.
- Loading branch information
1 parent
bcdfce5
commit 9ce04e7
Showing
10 changed files
with
275 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
mutters-ink-bot/src/test/ink/sessionrestore/story_new_option.ink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
VAR conversation_has_completed_at_least_once = false | ||
|
||
-> start | ||
|
||
|
||
=== start ==== | ||
-> all_intents | ||
|
||
= all_intents | ||
+ [OneIntent] -> option_one | ||
+ [NewIntent] -> option_new | ||
+ [TwoIntent] -> option_two | ||
+ [ThreeIntent] -> option_three | ||
|
||
= option_one | ||
You chose option one | ||
-> restart_conversation | ||
|
||
= option_two | ||
You chose option two | ||
+ [OneIntent] -> option_one | ||
+ [TwoIntent] -> option_two | ||
-> restart_conversation | ||
|
||
= option_new | ||
You chose option new | ||
+ [OneIntent] -> option_one | ||
+ [ThreeIntent] -> option_three | ||
-> restart_conversation | ||
|
||
= option_three | ||
You chose option three | ||
-> restart_conversation | ||
|
||
|
||
=== restart_conversation === | ||
~ conversation_has_completed_at_least_once = true | ||
-> start | ||
|
31 changes: 31 additions & 0 deletions
31
mutters-ink-bot/src/test/ink/sessionrestore/story_three_options.ink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
VAR conversation_has_completed_at_least_once = false | ||
|
||
-> start | ||
|
||
|
||
=== start ==== | ||
-> all_intents | ||
|
||
= all_intents | ||
+ [OneIntent] -> option_one | ||
+ [TwoIntent] -> option_two | ||
+ [ThreeIntent] -> option_three | ||
|
||
= option_one | ||
You chose option one | ||
-> restart_conversation | ||
|
||
= option_two | ||
You chose option two | ||
+ [OneIntent] -> option_one | ||
+ [TwoIntent] -> option_two | ||
-> restart_conversation | ||
|
||
= option_three | ||
You chose option three | ||
-> restart_conversation | ||
|
||
|
||
=== restart_conversation === | ||
~ conversation_has_completed_at_least_once = true | ||
-> start |
12 changes: 12 additions & 0 deletions
12
mutters-ink-bot/src/test/java/com/rabidgremlin/mutters/bot/ink/SessionRestoreTestBot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.rabidgremlin.mutters.bot.ink; | ||
|
||
public class SessionRestoreTestBot | ||
extends InkBot<SessionRestoreTestBotConfiguration> | ||
{ | ||
|
||
public SessionRestoreTestBot(SessionRestoreTestBotConfiguration config) | ||
{ | ||
super(config); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...ot/src/test/java/com/rabidgremlin/mutters/bot/ink/SessionRestoreTestBotConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.rabidgremlin.mutters.bot.ink; | ||
|
||
import java.util.List; | ||
|
||
import com.rabidgremlin.mutters.core.IntentMatcher; | ||
import com.rabidgremlin.mutters.templated.SimpleTokenizer; | ||
import com.rabidgremlin.mutters.templated.TemplatedIntent; | ||
import com.rabidgremlin.mutters.templated.TemplatedIntentMatcher; | ||
|
||
public class SessionRestoreTestBotConfiguration implements InkBotConfiguration | ||
{ | ||
private String inkJsonFileName; | ||
|
||
public SessionRestoreTestBotConfiguration(String inkJsonFileName) | ||
{ | ||
this.inkJsonFileName = inkJsonFileName; | ||
} | ||
|
||
@Override | ||
public IntentMatcher getIntentMatcher() | ||
{ | ||
SimpleTokenizer tokenizer = new SimpleTokenizer(); | ||
|
||
TemplatedIntentMatcher matcher = new TemplatedIntentMatcher(tokenizer); | ||
|
||
TemplatedIntent oneIntent = matcher.addIntent("OneIntent"); | ||
oneIntent.addUtterance("one"); | ||
|
||
TemplatedIntent twoIntent = matcher.addIntent("TwoIntent"); | ||
twoIntent.addUtterance("two"); | ||
|
||
TemplatedIntent threeIntent = matcher.addIntent("ThreeIntent"); | ||
threeIntent.addUtterance("three"); | ||
|
||
TemplatedIntent newIntent = matcher.addIntent("NewIntent"); | ||
newIntent.addUtterance("new"); | ||
|
||
return matcher; | ||
} | ||
|
||
@Override | ||
public String getStoryJson() | ||
{ | ||
return StoryUtils.loadStoryJsonFromClassPath(inkJsonFileName); | ||
} | ||
|
||
@Override | ||
public List<InkBotFunction> getInkFunctions() | ||
{ | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<GlobalIntent> getGlobalIntents() | ||
{ | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public ConfusedKnot getConfusedKnot() | ||
{ | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<String> getDefaultResponses() | ||
{ | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
mutters-ink-bot/src/test/java/com/rabidgremlin/mutters/bot/ink/TestSessionRestore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.rabidgremlin.mutters.bot.ink; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import com.rabidgremlin.mutters.core.Context; | ||
import com.rabidgremlin.mutters.core.bot.BotException; | ||
import com.rabidgremlin.mutters.core.bot.BotResponse; | ||
import com.rabidgremlin.mutters.core.session.Session; | ||
|
||
/** Test session restores when story changes (which can happen if sessions are stored outside of the bot for instance in a external cache). | ||
* | ||
* Ink tries to gracefully handle story state restores when a story structure has changed. It is supposed to raise warnings and errors that can | ||
* be checked when this occurs, but this is not happening. There is also a case where restore causes wrong path to be followed and incorrect output | ||
* to be generated. See testSessionRestoreWhenStoryOptionsChange() | ||
* | ||
* Current 'hack' is treat any change of story as an incompatible state change and raise a BadInkStoryState. | ||
* | ||
*/ | ||
public class TestSessionRestore | ||
{ | ||
private static SessionRestoreTestBot storyThreeBot; | ||
private static SessionRestoreTestBot storyNewBot; | ||
|
||
@BeforeClass | ||
public static void setUpBot() | ||
{ | ||
storyThreeBot = new SessionRestoreTestBot(new SessionRestoreTestBotConfiguration("story_three_options.ink.json")); | ||
storyNewBot = new SessionRestoreTestBot(new SessionRestoreTestBotConfiguration("story_new_option.ink.json")); | ||
} | ||
|
||
@Test | ||
public void testSessionRestoreWhenStoryOptionsChange() throws BotException | ||
{ | ||
Session session = new Session(); | ||
Context context = new Context(); | ||
|
||
BotResponse response = storyThreeBot.respond(session, context, "Three"); | ||
assertThat(response.getResponse(), is("You chose option three")); | ||
|
||
response = storyThreeBot.respond(session, context, "Three"); | ||
assertThat(response.getResponse(), is("You chose option three")); | ||
|
||
response = storyThreeBot.respond(session, context, "Three"); | ||
assertThat(response.getResponse(), is("You chose option three")); | ||
|
||
response = storyThreeBot.respond(session, context, "Three"); | ||
assertThat(response.getResponse(), is("You chose option three")); | ||
|
||
// switch to new story with more options and change of options order | ||
// Response should be "You chose option three" but actually land up "You chose option two" due to bug in ink | ||
// so short term, we will just spit out a BadInkStoryState exception | ||
try | ||
{ | ||
response = storyNewBot.respond(session, context, "Three"); | ||
//assertThat(response.getResponse(), is("You chose option three")); | ||
fail("Code should not reach here. Expected exception to be thrown"); | ||
} | ||
catch(Exception e) | ||
{ | ||
if (!e.getCause().getClass().equals(BadInkStoryState.class)) | ||
{ | ||
fail("Was expecting cause to be BadInkStoryState exception."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"inkVersion":19,"root":[[{"->":"start"},["done",{"#f":7,"#n":"g-0"}],null],"done",{"start":[{"->":".^.all_intents"},{"all_intents":[["ev","str","^OneIntent","/str","/ev",{"*":".^.c-0","flg":4},"ev","str","^NewIntent","/str","/ev",{"*":".^.c-1","flg":4},"ev","str","^TwoIntent","/str","/ev",{"*":".^.c-2","flg":4},"ev","str","^ThreeIntent","/str","/ev",{"*":".^.c-3","flg":4},{"c-0":["^ ",{"->":"start.option_one"},"\n",{"#f":7}],"c-1":["^ ",{"->":"start.option_new"},"\n",{"#f":7}],"c-2":["^ ",{"->":"start.option_two"},"\n",{"#f":7}],"c-3":["^ ",{"->":"start.option_three"},"\n",{"#f":7}]}],{"#f":3}],"option_one":["^You chose option one","\n",{"->":"restart_conversation"},{"#f":3}],"option_two":[["^You chose option two","\n","ev","str","^OneIntent","/str","/ev",{"*":".^.c-0","flg":4},"ev","str","^TwoIntent","/str","/ev",{"*":".^.c-1","flg":4},{"c-0":["^ ",{"->":"start.option_one"},"\n",{"#f":7}],"c-1":["^ ",{"->":".^.^.^"},"\n",{"->":"restart_conversation"},{"#f":7}]}],{"#f":3}],"option_new":[["^You chose option new","\n","ev","str","^OneIntent","/str","/ev",{"*":".^.c-0","flg":4},"ev","str","^ThreeIntent","/str","/ev",{"*":".^.c-1","flg":4},{"c-0":["^ ",{"->":"start.option_one"},"\n",{"#f":7}],"c-1":["^ ",{"->":"start.option_three"},"\n",{"->":"restart_conversation"},{"#f":7}]}],{"#f":3}],"option_three":["^You chose option three","\n",{"->":"restart_conversation"},{"#f":3}],"#f":3}],"restart_conversation":["ev",1,"/ev",{"temp=":"conversation_has_completed_at_least_once","re":true},{"->":"start"},{"#f":3}],"global decl":["ev",0,{"VAR=":"conversation_has_completed_at_least_once"},"/ev","end",null],"#f":3}],"listDefs":{}} |
1 change: 1 addition & 0 deletions
1
mutters-ink-bot/src/test/resources/story_three_options.ink.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"inkVersion":19,"root":[[{"->":"start"},["done",{"#f":7,"#n":"g-0"}],null],"done",{"start":[{"->":".^.all_intents"},{"all_intents":[["ev","str","^OneIntent","/str","/ev",{"*":".^.c-0","flg":4},"ev","str","^TwoIntent","/str","/ev",{"*":".^.c-1","flg":4},"ev","str","^ThreeIntent","/str","/ev",{"*":".^.c-2","flg":4},{"c-0":["^ ",{"->":"start.option_one"},"\n",{"#f":7}],"c-1":["^ ",{"->":"start.option_two"},"\n",{"#f":7}],"c-2":["^ ",{"->":"start.option_three"},"\n",{"#f":7}]}],{"#f":3}],"option_one":["^You chose option one","\n",{"->":"restart_conversation"},{"#f":3}],"option_two":[["^You chose option two","\n","ev","str","^OneIntent","/str","/ev",{"*":".^.c-0","flg":4},"ev","str","^TwoIntent","/str","/ev",{"*":".^.c-1","flg":4},{"c-0":["^ ",{"->":"start.option_one"},"\n",{"#f":7}],"c-1":["^ ",{"->":".^.^.^"},"\n",{"->":"restart_conversation"},{"#f":7}]}],{"#f":3}],"option_three":["^You chose option three","\n",{"->":"restart_conversation"},{"#f":3}],"#f":3}],"restart_conversation":["ev",1,"/ev",{"temp=":"conversation_has_completed_at_least_once","re":true},{"->":"start"},{"#f":3}],"global decl":["ev",0,{"VAR=":"conversation_has_completed_at_least_once"},"/ev","end",null],"#f":3}],"listDefs":{}} |