From 83a7f32c5cb7ac1db40372ddbfe7b5fdaf60292e Mon Sep 17 00:00:00 2001
From: David Trattnig <david@subsquare.at>
Date: Fri, 13 Oct 2023 12:14:25 +0200
Subject: [PATCH] test: methods to queue timeslots

---
 tests/test_scheduling_scheduler.py | 106 ++++++++++++++++++++++++-----
 1 file changed, 90 insertions(+), 16 deletions(-)

diff --git a/tests/test_scheduling_scheduler.py b/tests/test_scheduling_scheduler.py
index 89da4ea7..526346af 100644
--- a/tests/test_scheduling_scheduler.py
+++ b/tests/test_scheduling_scheduler.py
@@ -28,7 +28,6 @@ from aura_engine.core.channels import GenericChannel
 from aura_engine.engine import Engine
 from aura_engine.scheduling.domain import Playlist, PlaylistItem, Timeslot
 from aura_engine.scheduling.scheduler import AuraScheduler
-from aura_engine.scheduling.timetable import TimetableService
 from aura_engine.scheduling.utils import TimetableRenderer
 
 
@@ -215,10 +214,9 @@ class TestSchedulingScheduler(unittest.TestCase):
     #
 
     def build_timetable(
-        self, scheduler: AuraScheduler, current_start, current_end, source_type="file://"
+        self, scheduler: AuraScheduler, current_start, current_end, st="file://"
     ) -> [Timeslot]:
         # Delete the timetable file to isolate test case
-        cache_location = self.config.get("cache_dir")
         f = scheduler.timetable.timetable_file
         scheduler.timetable.timetable_file = f.replace("timetable.json", "timetable-test.json")
         scheduler.timetable.delete_timetable_file()
@@ -256,24 +254,35 @@ class TestSchedulingScheduler(unittest.TestCase):
             id=4, repetition_id=None, start=now + 540, end=now + 720, show=None, episode=None
         )
 
-        pl = Playlist("9999", "Playlist XYZ", False)
-        alpha = PlaylistItem(source_type + "alpha.flac", 100, 100, None)
-        beta = PlaylistItem(
-            source_type + "beta.flac", current_end - current_start - 200, 100, None
-        )
-        gamma = PlaylistItem(source_type + "gamma.flac", 100, 100, None)
-        pl.add(alpha)
-        pl.add(beta)
-        pl.add(gamma)
-
-        ts2.set_playlists(None, pl, None)
+        # Playlist for timeslot 2
+        pl2 = Playlist("9999", "Playlist XYZ", False)
+        alpha = PlaylistItem(st + "alpha.flac", 100, 100, None)
+        beta = PlaylistItem(st + "beta.flac", current_end - current_start - 200, 100, None)
+        gamma = PlaylistItem(st + "gamma.flac", 50, 100, None)
+        delta = PlaylistItem(st + "delta.flac", 50, 100, None)
+        pl2.add(alpha)
+        pl2.add(beta)
+        pl2.add(gamma)
+        pl2.add(delta)
+
+        # Playlist for timeslot 3
+        pl3 = Playlist("7777", "Colours", False)
+        magenta = PlaylistItem(st + "magenta.flac", 100, 100, None)
+        black = PlaylistItem(st + "black.flac", current_end - current_start - 200, 100, None)
+        taupe = PlaylistItem(st + "taupe.flac", 100, 100, None)
+        pl3.add(magenta)
+        pl3.add(black)
+        pl3.add(taupe)
+
+        ts2.set_playlists(None, pl2, None)
+        ts3.set_playlists(pl3, None, None)
         return [ts1, ts2, ts3, ts4]
 
     #
     # Test Cases
     #
 
-    def xxx_test_start_and_terminate_scheduler(self):
+    def test_start_and_terminate_scheduler(self):
         print(self._testMethodName)
 
         # Boot the scheduler
@@ -324,7 +333,7 @@ class TestSchedulingScheduler(unittest.TestCase):
         scheduler.play_active_item()
         cmds = CommandRegistry.get(self._testMethodName)
 
-        # Expecting at a timeslot and a play command
+        # Expecting at least one timeslot and a play command
         self.assertEqual(2, len(cmds))
         self.assertTrue(isinstance(cmds[0], MockedTimeslotCommand))
         self.assertTrue(isinstance(cmds[1], MockedPlayCommand))
@@ -370,6 +379,71 @@ class TestSchedulingScheduler(unittest.TestCase):
         self.assertEqual(1, len(cmds))
         self.assertTrue(isinstance(cmds[0], MockedTimeslotCommand))
 
+    def test_queue_programme_startup_items(self):
+        print(self._testMethodName)
+
+        # Construct the scheduler
+        scheduler = AuraScheduler(MockedEngine())
+        scheduler.timetable.delete_timetable_file()
+
+        # Build some timetable
+        scheduler.timetable.timetable = self.build_timetable(scheduler, -400, +400)
+
+        scheduler.queue_startup_items()
+        cmds = CommandRegistry.get("queue_startup_items")
+
+        # Expecting only one play command, is the timeslot command was created
+        # when scheduling the active item already
+        self.assertEqual(1, len(cmds))
+        self.assertTrue(isinstance(cmds[0], MockedPlayCommand))
+
+        play_cmd: MockedPlayCommand = cmds[0]
+        self.assertEqual(2, len(play_cmd.items))
+        channel: GenericChannel = play_cmd.items[0].play_channel
+        self.assertEqual("main channel", channel.name)
+
+    def test_queue_programme_nothing_next_in_window(self):
+        print(self._testMethodName)
+
+        # Construct the scheduler
+        scheduler = AuraScheduler(MockedEngine())
+        scheduler.timetable.delete_timetable_file()
+
+        # Build some timetable
+        scheduler.timetable.timetable = self.build_timetable(scheduler, -400, +400)
+
+        scheduler.queue_programme()
+        cmds = CommandRegistry.get(self._testMethodName)
+
+        # Expecting no command as everything is out of the scheduling window
+        self.assertIsNone(cmds)
+
+    def test_queue_programme_valid_next_in_window(self):
+        print(self._testMethodName)
+
+        # Construct the scheduler
+        scheduler = AuraScheduler(MockedEngine())
+        scheduler.timetable.delete_timetable_file()
+
+        # Build some timetable
+        scheduler.timetable.timetable = self.build_timetable(scheduler, -400, +30)
+
+        scheduler.queue_programme()
+        cmds = CommandRegistry.get(self._testMethodName)
+        cmds += CommandRegistry.get("queue_programme")
+
+        # Expecting one timeslot and one play command for the next timeslot
+        self.assertEqual(2, len(cmds))
+        self.assertTrue(isinstance(cmds[0], MockedTimeslotCommand))
+        self.assertTrue(isinstance(cmds[1], MockedPlayCommand))
+
+        timeslot_cmd: MockedTimeslotCommand = cmds[0]
+        play_cmd: MockedPlayCommand = cmds[1]
+        self.assertEqual(3, timeslot_cmd.timeslot.get_id())
+        self.assertEqual(3, len(play_cmd.items))
+        channel: GenericChannel = play_cmd.items[0].play_channel
+        self.assertEqual("main channel", channel.name)
+
 
 if __name__ == "__main__":
     unittest.main()
-- 
GitLab