Skip to content

Commit

Permalink
test with session
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Sep 30, 2023
1 parent 76fe6ec commit c31bb3c
Showing 1 changed file with 104 additions and 1 deletion.
105 changes: 104 additions & 1 deletion test/end2end/session/test_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def wait_for_n_messages(n):

utt = Message("recognizer_loop:utterance",
{"utterances": ["invalid"]},
{"session": SessionManager.default_session.serialize()})
{"session": SessionManager.default_session.serialize(), # explicit default sess
"x": "xx"})
self.core.bus.emit(utt)

# confirm all expected messages are sent
Expand Down Expand Up @@ -69,6 +70,7 @@ def wait_for_n_messages(n):
# verify that contexts are kept around
for m in messages:
self.assertEqual(m.context["session"]["session_id"], "default")
self.assertEqual(m.context["x"], "xx")

# verify converse ping/pong answer from skill
self.assertEqual(messages[1].msg_type, "skill.converse.ping")
Expand Down Expand Up @@ -105,3 +107,104 @@ def wait_for_n_messages(n):
# verify default session is now updated
self.assertEqual(messages[12].msg_type, "ovos.session.update_default")
self.assertEqual(messages[12].data["session_data"]["session_id"], "default")

def test_fallback_with_session(self):
SessionManager.sessions = {}
SessionManager.default_session = SessionManager.sessions["default"] = Session("default")
SessionManager.default_session.lang = "en-us"
messages = []

sess = Session()

def new_msg(msg):
nonlocal messages
m = Message.deserialize(msg)
if m.msg_type in ["ovos.skills.settings_changed"]:
return # skip these, only happen in 1st run
messages.append(m)
print(len(messages), msg)

def wait_for_n_messages(n):
nonlocal messages
while len(messages) < n:
sleep(0.1)

self.core.bus.on("message", new_msg)

utt = Message("recognizer_loop:utterance",
{"utterances": ["invalid"]},
{"session": sess.serialize(), # explicit sess
"x": "xx"})
self.core.bus.emit(utt)

# confirm all expected messages are sent
expected_messages = [
"recognizer_loop:utterance",
# Converse
"skill.converse.ping",
"skill.converse.pong",
# FallbackV2
"ovos.skills.fallback.ping",
"ovos.skills.fallback.pong",
# skill executing
f"ovos.skills.fallback.{self.skill_id}.request",
f"ovos.skills.fallback.{self.skill_id}.start",
"enclosure.active_skill",
"speak",
f"ovos.skills.fallback.{self.skill_id}.response",
# intent service post fallback
"intent.service.skills.activated",
f"{self.skill_id}.activate"
]
wait_for_n_messages(len(expected_messages))

self.assertEqual(len(expected_messages), len(messages))

mtypes = [m.msg_type for m in messages]
for m in expected_messages:
self.assertTrue(m in mtypes)

# verify that contexts are kept around
for m in messages:
self.assertEqual(m.context["session"]["session_id"], sess.session_id)
self.assertEqual(m.context["x"], "xx")

# verify converse ping/pong answer from skill
self.assertEqual(messages[1].msg_type, "skill.converse.ping")
self.assertEqual(messages[2].msg_type, "skill.converse.pong")
self.assertEqual(messages[2].data["skill_id"], self.skill_id)
self.assertEqual(messages[2].context["skill_id"], self.skill_id)
self.assertFalse(messages[2].data["can_handle"])

# verify fallback ping/pong answer from skill
self.assertEqual(messages[3].msg_type, "ovos.skills.fallback.ping")
self.assertEqual(messages[4].msg_type, "ovos.skills.fallback.pong")
self.assertEqual(messages[4].data["skill_id"], self.skill_id)
self.assertEqual(messages[4].context["skill_id"], self.skill_id)
self.assertTrue(messages[4].data["can_handle"])

# verify skill executes
self.assertEqual(messages[5].msg_type, f"ovos.skills.fallback.{self.skill_id}.request")
self.assertEqual(messages[5].data["skill_id"], self.skill_id)
self.assertEqual(messages[6].msg_type, f"ovos.skills.fallback.{self.skill_id}.start")
self.assertEqual(messages[7].msg_type, "enclosure.active_skill")
self.assertEqual(messages[7].data["skill_id"], self.skill_id)
self.assertEqual(messages[8].msg_type, "speak")
self.assertEqual(messages[8].data["meta"]["dialog"], "unknown")
self.assertEqual(messages[8].data["meta"]["skill"], self.skill_id)
self.assertEqual(messages[9].msg_type, f"ovos.skills.fallback.{self.skill_id}.response")
self.assertTrue(messages[9].data["result"])
self.assertEqual(messages[9].data["fallback_handler"], "UnknownSkill.handle_fallback")

# verify skill is activated
self.assertEqual(messages[10].msg_type, "intent.service.skills.activated")
self.assertEqual(messages[10].data["skill_id"], self.skill_id)
self.assertEqual(messages[11].msg_type, f"{self.skill_id}.activate")

# test that active skills list has been updated
sess = SessionManager.sessions[sess.session_id]
self.assertEqual(sess.active_skills[0][0], self.skill_id)
# test that default session remains unchanged
self.assertEqual(SessionManager.default_session.active_skills, [])


0 comments on commit c31bb3c

Please sign in to comment.