def test_handler_without_type_hint(self): class ModuleWithMultipleTypes(BaseModule): def boot(self) -> None: self.subscribe(self.handle, types=[EventTypeA, EventTypeB]) def handle(self, event): pass ai = Core() ai.add_module(ModuleWithMultipleTypes()) ai.boot()
def test_module_receives_all_events(self): ai = Core() module = AllEventsModule() ai.add_module(module) ai.boot() ai.publish(EventTypeA(content="a")) ai.publish(EventTypeB(content="b")) self.assertEqual(['a', 'b'], module.received_events)
def test_publishing_non_class(self): class BadPublisher(BaseModule): def boot(self): pass ai = Core() module = BadPublisher() ai.add_module(module) ai.boot() self.assertRaises(ModulePublishedBadEventError, lambda: module.publish("not an event"))
def test_module_should_not_subscribe_after_boot(self): class ModuleSubscribingAfterBoot(BaseModule): def boot(self) -> None: self.subscribe(self.handle, EventTypeA) def handle(self, event: EventTypeA) -> None: self.subscribe(lambda event: None, EventTypeB) ai = Core() ai.add_module(ModuleSubscribingAfterBoot()) ai.boot() self.assertRaises(ModuleSubscribedAfterBoot, lambda: ai.publish(EventTypeA('foobar')))
def test_add_task(self): e = [] class ModuleWithTask(BaseModule): def boot(self) -> None: self.add_task(self.sleep()) async def sleep(self) -> None: await asyncio.sleep(0.001) e.append('done') ai = Core() ai.add_module(ModuleWithTask()) ai.boot() self.assertEqual(['done'], e)
def test_handle_metadata(self): received_ids = [] class MetadataModule(BaseModule): def boot(self): self.subscribe(self.handler) def handler(self, event: BaseEvent, metadata: Metadata): received_ids.append(metadata.id) ai = Core(metadata_provider=lambda: Metadata(id=test_uuid)) ai.add_module(MetadataModule()) ai.boot() ai.publish(TestEvent('hello')) self.assertEqual([test_uuid], received_ids)
def test_subscribing_non_class(self): class BadSubscriber(BaseModule): def boot(self): self.subscribe(lambda: None, types='MyEvent') ai = Core() module = BadSubscriber() ai.add_module(module) self.assertRaises(ModuleSubscribedToNonClassError, lambda: ai.boot())
def test_metadata_id_is_same_for_all_handlers(self): received_ids = [] class MetadataModule(BaseModule): def boot(self): self.subscribe( lambda metadata: received_ids.append(metadata.id)) self.subscribe( lambda metadata: received_ids.append(metadata.id)) ai = Core() ai.add_module(MetadataModule()) ai.boot() ai.publish(TestEvent('hello')) self.assertEqual(2, len(received_ids)) self.assertEqual(received_ids[0], received_ids[1])
def test_module_receives_only_events_it_subscribed_to(self): class AEventsModule(BaseModule): received_events = [] def boot(self): self.subscribe(self.handle_a, types=EventTypeA) def handle_a(self, event: EventTypeA): self.received_events.append(event.content) ai = Core() foobar = AEventsModule() ai.add_module(foobar) ai.boot() ai.publish(EventTypeA(content="a")) ai.publish(EventTypeB(content="b")) self.assertEqual(['a'], foobar.received_events)
def test_output_expression_result(self): class TestHelperBot(BaseModule): events: List[BaseEvent] def boot(self) -> None: self.events = [] self.subscribe(lambda event: self.events.append(event)) helper = TestHelperBot() bot = MathBot() ai = Core() ai.add_module(bot) ai.add_module(helper) ai.boot() ai.publish(TextInput('10+2')) self.assertIn(MathParsed(Addition(Constant(10), Constant(2))), helper.events) self.assertIn(TextOutput('The result is: 12.0'), helper.events)
def test_handler_type_matches_subscribe(self): class ModuleWithTypeMismatch(BaseModule): def boot(self) -> None: self.subscribe(self.handle, types=EventTypeA) def handle(self, event: EventTypeB): pass ai = Core() ai.add_module(ModuleWithTypeMismatch()) self.assertRaises(ModuleError, lambda: ai.boot())
def test_handler_arguments(self): class FooModule(BaseModule): def boot(self): self.subscribe(self.handle) def handle(self, message): pass ai = Core() ai.add_module(FooModule()) self.assertRaises(ModuleError, lambda: ai.boot())
import logging from core.core import Core from modules.CommandHelper.module import CommandHelper from modules.EchoBot.module import EchoBot from modules.EventLoggingBot.module import EventLoggingBot from modules.MathBot.module import MathBot from modules.ReminderBot.module import ReminderBot from modules.TimeBot.module import TimeBot from modules.ConsoleModule.module import ConsoleModule if __name__ == '__main__': logging.basicConfig(format='%(asctime)s: %(message)s', level=logging.INFO, datefmt='%H:%M:%S') ai = Core() ai.add_module(EchoBot()) ai.add_module(TimeBot()) ai.add_module(EventLoggingBot('ai.log')) ai.add_module(ReminderBot()) ai.add_module(MathBot()) ai.add_module(ConsoleModule()) ai.add_module(CommandHelper()) ai.boot()