Skip to content

Commit

Permalink
unittests/event_scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Oct 19, 2023
1 parent ffb031e commit 7c188a9
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
pip install -r requirements/tests.txt
pip install ./test/unittests/common_query/ovos_tskill_fakewiki
pip install ./test/end2end/session/skill-ovos-hello-world
pip install ./test/end2end/session/skill-ovos-schedule
pip install ./test/end2end/session/skill-ovos-fallback-unknown
pip install ./test/end2end/session/skill-ovos-fallback-unknownv1
pip install ./test/end2end/session/skill-converse_test
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ jobs:
pip install ./test/end2end/session/skill-ovos-fallback-unknown
pip install ./test/end2end/session/skill-ovos-fallback-unknownv1
pip install ./test/end2end/session/skill-converse_test
pip install ./test/end2end/session/skill-ovos-schedule
- name: Run unittests
run: |
pytest --cov=ovos_core --cov-report xml test/unittests
Expand Down
5 changes: 4 additions & 1 deletion test/end2end/session/minicroft.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from time import sleep

from ovos_bus_client.session import SessionManager, Session
from ovos_bus_client.util.scheduler import EventScheduler
from ovos_core.intent_services import IntentService
from ovos_core.skill_manager import SkillManager
from ovos_plugin_manager.skills import find_skill_plugins
Expand All @@ -15,6 +17,7 @@ def __init__(self, skill_ids, *args, **kwargs):
super().__init__(bus, *args, **kwargs)
self.skill_ids = skill_ids
self.intent_service = self._register_intent_services()
self.scheduler = EventScheduler(bus, schedule_file="/tmp/schetest.json")

def _register_intent_services(self):
"""Start up the all intent services and connect them as needed.
Expand Down Expand Up @@ -52,6 +55,7 @@ def run(self):

def stop(self):
super().stop()
self.scheduler.shutdown()
SessionManager.bus = None
SessionManager.sessions = {}
SessionManager.default_session = SessionManager.sessions["default"] = Session("default")
Expand All @@ -66,4 +70,3 @@ def get_minicroft(skill_id):
while croft1.status.state != ProcessState.READY:
sleep(0.2)
return croft1

7 changes: 7 additions & 0 deletions test/end2end/session/skill-ovos-schedule/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
recursive-include dialog *
recursive-include vocab *
recursive-include locale *
recursive-include res *
recursive-include ui *
include *.json
include *.txt
14 changes: 14 additions & 0 deletions test/end2end/session/skill-ovos-schedule/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from ovos_utils.intents import IntentBuilder
from ovos_workshop.decorators import intent_handler
from ovos_workshop.skills import OVOSSkill


class ScheduleSkill(OVOSSkill):

def handle_event(self, message):
self.speak_dialog("trigger")

@intent_handler(IntentBuilder("ScheduleIntent").require("Schedule"))
def handle_sched_intent(self, message):
self.speak_dialog("done")
self.schedule_event(self.handle_event, 3, name="my_event")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
it has been scheduled
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the event triggering
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
schedule event
45 changes: 45 additions & 0 deletions test/end2end/session/skill-ovos-schedule/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
from os import walk, path

from setuptools import setup

URL = "https://github.com/OpenVoiceOS/skill-ovos-schedule"
SKILL_CLAZZ = "ScheduleSkill" # needs to match __init__.py class name

# below derived from github url to ensure standard skill_id
SKILL_AUTHOR, SKILL_NAME = URL.split(".com/")[-1].split("/")
SKILL_PKG = SKILL_NAME.lower().replace('-', '_')
PLUGIN_ENTRY_POINT = f'{SKILL_NAME.lower()}.{SKILL_AUTHOR.lower()}={SKILL_PKG}:{SKILL_CLAZZ}'


# skill_id=package_name:SkillClass


def find_resource_files():
resource_base_dirs = ("locale", "ui", "vocab", "dialog", "regex", "skill")
base_dir = path.dirname(__file__)
package_data = ["*.json"]
for res in resource_base_dirs:
if path.isdir(path.join(base_dir, res)):
for (directory, _, files) in walk(path.join(base_dir, res)):
if files:
package_data.append(
path.join(directory.replace(base_dir, "").lstrip('/'),
'*'))
return package_data


setup(
name="ovos-skill-schedule",
version="0.0.0",
long_description="test",
description='OVOS schedule skill plugin',
author_email='[email protected]',
license='Apache-2.0',
package_dir={SKILL_PKG: ""},
package_data={SKILL_PKG: find_resource_files()},
packages=[SKILL_PKG],
include_package_data=True,
keywords='ovos skill plugin',
entry_points={'ovos.plugin.skill': PLUGIN_ENTRY_POINT}
)
3 changes: 3 additions & 0 deletions test/end2end/session/test_complete_failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def setUp(self):
self.skill_id = "skill-ovos-hello-world.openvoiceos"
self.core = get_minicroft(self.skill_id)

def tearDown(self) -> None:
self.core.stop()

def test_complete_failure(self):
SessionManager.sessions = {}
SessionManager.default_session = SessionManager.sessions["default"] = Session("default")
Expand Down
12 changes: 7 additions & 5 deletions test/end2end/session/test_converse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from time import sleep
from unittest import TestCase, skip
from unittest import TestCase

from ovos_bus_client.message import Message
from ovos_bus_client.session import SessionManager, Session
Expand All @@ -14,6 +14,9 @@ def setUp(self):
self.other_skill_id = "skill-ovos-hello-world.openvoiceos"
self.core = get_minicroft([self.skill_id, self.other_skill_id])

def tearDown(self) -> None:
self.core.stop()

def test_no_session(self):
SessionManager.sessions = {}
SessionManager.default_session = SessionManager.sessions["default"] = Session("default")
Expand Down Expand Up @@ -162,7 +165,7 @@ def hello_world():

# verify that "lang" is injected by converse.ping
# (missing in utterance message) and kept in all messages
self.assertEqual(messages[1].msg_type, f"{self.skill_id}.converse.ping")
self.assertEqual(messages[1].msg_type, f"{self.skill_id}.converse.ping")

# verify "pong" answer from converse test skill
self.assertEqual(messages[2].msg_type, "skill.converse.pong")
Expand Down Expand Up @@ -263,7 +266,7 @@ def skill_converse_yes():
# converse
self.assertEqual(messages[1].msg_type, f"{self.other_skill_id}.converse.ping")
self.assertEqual(messages[2].msg_type, "skill.converse.pong")
self.assertEqual(messages[2].data["skill_id"],messages[2].context["skill_id"])
self.assertEqual(messages[2].data["skill_id"], messages[2].context["skill_id"])
self.assertFalse(messages[2].data["can_handle"])
self.assertEqual(messages[3].msg_type, f"{self.skill_id}.converse.ping")
self.assertEqual(messages[4].msg_type, "skill.converse.pong")
Expand Down Expand Up @@ -356,7 +359,7 @@ def converse_capture():
# converse
self.assertEqual(messages[1].msg_type, f"{self.skill_id}.converse.ping")
self.assertEqual(messages[2].msg_type, "skill.converse.pong")
self.assertEqual(messages[2].data["skill_id"],messages[2].context["skill_id"])
self.assertEqual(messages[2].data["skill_id"], messages[2].context["skill_id"])
self.assertTrue(messages[2].data["can_handle"])
self.assertEqual(messages[3].msg_type, f"{self.other_skill_id}.converse.ping")
self.assertEqual(messages[4].msg_type, "skill.converse.pong")
Expand Down Expand Up @@ -480,4 +483,3 @@ def external_activate():
messages = []

external_activate()

3 changes: 3 additions & 0 deletions test/end2end/session/test_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def setUp(self):
self.skill_id = "skill-ovos-fallback-unknown.openvoiceos"
self.core = get_minicroft(self.skill_id)

def tearDown(self) -> None:
self.core.stop()

def test_fallback(self):
SessionManager.sessions = {}
SessionManager.default_session = SessionManager.sessions["default"] = Session("default")
Expand Down
4 changes: 3 additions & 1 deletion test/end2end/session/test_fallback_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def setUp(self):
self.skill_id = "skill-ovos-fallback-unknownv1.openvoiceos"
self.core = get_minicroft(self.skill_id)

def tearDown(self) -> None:
self.core.stop()

def test_fallback_v1(self):
SessionManager.sessions = {}
SessionManager.default_session = SessionManager.sessions["default"] = Session("default")
Expand Down Expand Up @@ -166,4 +169,3 @@ def wait_for_n_messages(n):
for m in messages[1:]:
self.assertEqual(m.context["session"]["session_id"], "default")
self.assertEqual(m.context["session"]["active_skills"][0][0], self.skill_id)

40 changes: 21 additions & 19 deletions test/end2end/session/test_get_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from ovos_bus_client.message import Message
from ovos_bus_client.session import SessionManager, Session
from ovos_utils.log import LOG
from .minicroft import get_minicroft


Expand All @@ -15,6 +14,9 @@ def setUp(self):
self.other_skill_id = "skill-ovos-hello-world.openvoiceos"
self.core = get_minicroft([self.skill_id, self.other_skill_id])

def tearDown(self) -> None:
self.core.stop()

def test_no_response(self):
SessionManager.sessions = {}
SessionManager.default_session = SessionManager.sessions["default"] = Session("default")
Expand Down Expand Up @@ -56,7 +58,7 @@ def on_speak(msg):
"recognizer_loop:utterance", # no session

# skill selected
"intent.service.skills.activated", # default session injected
"intent.service.skills.activated", # default session injected
f"{self.skill_id}.activate",
f"{self.skill_id}:test_get_response.intent",

Expand Down Expand Up @@ -95,9 +97,9 @@ def on_speak(msg):
for m in messages[1:]:
print(m.msg_type, m.context["session"]["session_id"])
self.assertEqual(m.context["session"]["session_id"], "default")
self.assertEqual(m.context["lang"], "en-us")
self.assertEqual(m.context["lang"], "en-us")

# verify skill is activated by intent service (intent pipeline matched)
# verify skill is activated by intent service (intent pipeline matched)
self.assertEqual(messages[1].msg_type, "intent.service.skills.activated")
self.assertEqual(messages[1].data["skill_id"], self.skill_id)
self.assertEqual(messages[2].msg_type, f"{self.skill_id}.activate")
Expand Down Expand Up @@ -694,7 +696,7 @@ def answer_get_response(msg):
"recognizer_loop:utterance", # no session

# skill selected
"intent.service.skills.activated", # default session injected
"intent.service.skills.activated", # default session injected
f"{self.skill_id}.activate",
f"{self.skill_id}:test_get_response_cascade.intent",

Expand Down Expand Up @@ -802,33 +804,33 @@ def answer_get_response(msg):
self.assertEqual(messages[6].data["utterance"], "give me items")
self.assertEqual(messages[6].data["meta"]["skill"], self.skill_id)

responses = ["A", "B", "C", "cancel"]
responses = ["A", "B", "C", "cancel"]
for response in responses:
i = 6 + responses.index(response) * 12
# enable get_response for this session
self.assertEqual(messages[i+1].msg_type, "skill.converse.get_response.enable")
self.assertEqual(messages[i+2].msg_type, "ovos.session.update_default")
self.assertEqual(messages[i + 1].msg_type, "skill.converse.get_response.enable")
self.assertEqual(messages[i + 2].msg_type, "ovos.session.update_default")

# 3 sound prompts (no dialog in this test)
self.assertEqual(messages[i+3].msg_type, "mycroft.mic.listen")
self.assertEqual(messages[i + 3].msg_type, "mycroft.mic.listen")

# check utterance goes through converse cycle
self.assertEqual(messages[i+4].msg_type, "recognizer_loop:utterance")
self.assertEqual(messages[i+5].msg_type, f"{self.skill_id}.converse.ping")
self.assertEqual(messages[i+6].msg_type, "skill.converse.pong")
self.assertEqual(messages[i + 4].msg_type, "recognizer_loop:utterance")
self.assertEqual(messages[i + 5].msg_type, f"{self.skill_id}.converse.ping")
self.assertEqual(messages[i + 6].msg_type, "skill.converse.pong")

# captured utterance sent to get_response handler that is waiting
self.assertEqual(messages[i+7].msg_type, f"{self.skill_id}.converse.get_response")
self.assertEqual(messages[i+7].data["utterances"], [response])
self.assertEqual(messages[i + 7].msg_type, f"{self.skill_id}.converse.get_response")
self.assertEqual(messages[i + 7].data["utterances"], [response])

# converse pipeline activates the skill last_used timestamp
self.assertEqual(messages[i+8].msg_type, "intent.service.skills.activated")
self.assertEqual(messages[i+9].msg_type, f"{self.skill_id}.activate")
self.assertEqual(messages[i+10].msg_type, "ovos.session.update_default")
self.assertEqual(messages[i + 8].msg_type, "intent.service.skills.activated")
self.assertEqual(messages[i + 9].msg_type, f"{self.skill_id}.activate")
self.assertEqual(messages[i + 10].msg_type, "ovos.session.update_default")

# disable get_response for this session
self.assertEqual(messages[i+11].msg_type, "skill.converse.get_response.disable")
self.assertEqual(messages[i+12].msg_type, "ovos.session.update_default")
self.assertEqual(messages[i + 11].msg_type, "skill.converse.get_response.disable")
self.assertEqual(messages[i + 12].msg_type, "ovos.session.update_default")

# intent return
self.assertEqual(messages[55].msg_type, "skill_items")
Expand Down
Loading

0 comments on commit 7c188a9

Please sign in to comment.