This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91f7573
commit b572dd8
Showing
2 changed files
with
139 additions
and
8 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
115 changes: 115 additions & 0 deletions
115
src/test/java/io/spokestack/spokestack/rasa/RasaDialoguePolicyTest.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,115 @@ | ||
package io.spokestack.spokestack.rasa; | ||
|
||
import io.spokestack.spokestack.SpeechConfig; | ||
import io.spokestack.spokestack.dialogue.ConversationData; | ||
import io.spokestack.spokestack.dialogue.DialogueDispatcher; | ||
import io.spokestack.spokestack.dialogue.DialogueEvent; | ||
import io.spokestack.spokestack.dialogue.DialogueListener; | ||
import io.spokestack.spokestack.dialogue.InMemoryConversationData; | ||
import io.spokestack.spokestack.dialogue.Prompt; | ||
import io.spokestack.spokestack.nlu.NLUResult; | ||
import io.spokestack.spokestack.util.EventTracer; | ||
import junit.framework.TestListener; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class RasaDialoguePolicyTest { | ||
|
||
@Test | ||
public void unusedMethods() { | ||
RasaDialoguePolicy policy = new RasaDialoguePolicy(testConfig()); | ||
ConversationData dataStore = new InMemoryConversationData(); | ||
dataStore.set("key", "val"); | ||
assertEquals("", policy.dump(dataStore)); | ||
policy.load("state", dataStore); | ||
} | ||
|
||
@Test | ||
public void testEvents() throws InterruptedException { | ||
RasaDialoguePolicy policy = new RasaDialoguePolicy(testConfig()); | ||
ConversationData dataStore = new InMemoryConversationData(); | ||
TestListener listener = new TestListener(); | ||
DialogueDispatcher dispatcher = testDispatcher(listener); | ||
// null response throws an error | ||
String response = null; | ||
NLUResult result = rasaResult(response); | ||
policy.handleTurn(result, dataStore, dispatcher); | ||
DialogueEvent event = listener.events.poll(500, TimeUnit.MILLISECONDS); | ||
assertNotNull(event); | ||
assertEquals(DialogueEvent.Type.ERROR, event.type); | ||
|
||
// empty response throws an error | ||
response = "{}"; | ||
result = rasaResult(response); | ||
policy.handleTurn(result, dataStore, dispatcher); | ||
event = listener.events.poll(500, TimeUnit.MILLISECONDS); | ||
assertNotNull(event); | ||
assertEquals(DialogueEvent.Type.ERROR, event.type); | ||
|
||
String prompt = "hi"; | ||
String imageURL = "https://example.com"; | ||
response = "[" + | ||
"{\"recipient_id\": \"id\", \"text\": \"" + prompt + "\"}," + | ||
"{\"recipient_id\": \"id\", \"image\": \"" + imageURL + "\"}," + | ||
"]"; | ||
result = rasaResult(response); | ||
policy.handleTurn(result, dataStore, dispatcher); | ||
List<DialogueEvent> events = new ArrayList<>(); | ||
listener.events.drainTo(events); | ||
event = events.get(0); | ||
assertEquals(DialogueEvent.Type.PROMPT, event.type); | ||
String receivedPrompt = event.state.getPrompt().getText(dataStore); | ||
assertEquals(prompt, receivedPrompt); | ||
event = events.get(1); | ||
assertEquals(DialogueEvent.Type.ACTION, event.type); | ||
String receivedURL = event.state.getPrompt().getText(dataStore); | ||
assertEquals(imageURL, receivedURL); | ||
} | ||
|
||
private SpeechConfig testConfig() { | ||
SpeechConfig config = new SpeechConfig(); | ||
config.put("sample-rate", 16000); | ||
config.put("frame-width", 20); | ||
config.put("buffer-width", 300); | ||
return config; | ||
} | ||
|
||
private DialogueDispatcher testDispatcher(DialogueListener listener) { | ||
int level = EventTracer.Level.INFO.value(); | ||
List<DialogueListener> listeners = new ArrayList<>(); | ||
listeners.add(listener); | ||
return new DialogueDispatcher(level, listeners); | ||
} | ||
|
||
private NLUResult rasaResult(String response) { | ||
HashMap<String, Object> rasaContext = new HashMap<>(); | ||
rasaContext.put(RasaOpenSourceNLU.RESPONSE_KEY, response); | ||
return new NLUResult.Builder("test utterance") | ||
.withIntent(RasaOpenSourceNLU.RASA_INTENT) | ||
.withContext(rasaContext) | ||
.build(); | ||
} | ||
|
||
static class TestListener implements DialogueListener { | ||
LinkedBlockingQueue<DialogueEvent> events = new LinkedBlockingQueue<>(); | ||
|
||
@Override | ||
public void onDialogueEvent(@NotNull DialogueEvent event) { | ||
events.add(event); | ||
} | ||
|
||
@Override | ||
public void onTrace(@NotNull EventTracer.Level level, | ||
@NotNull String message) { | ||
// no-op | ||
} | ||
} | ||
} |