From 735cfbb8a00b1cf20b2dbc28ed0548d56da4cbe1 Mon Sep 17 00:00:00 2001
From: Chris Pastl <chris@crispybits.app>
Date: Fri, 3 May 2024 15:14:51 +0200
Subject: [PATCH] refactor: init PlayoutStatusResponse with core reponse string
 and compare enum cases

---
 src/aura_engine/core/channels.py | 19 ++++++++++++++-----
 tests/core_client_mock.py        | 29 +++++++++--------------------
 tests/test_core_channels.py      | 17 ++++++++---------
 tests/test_core_mixer.py         | 10 +---------
 4 files changed, 32 insertions(+), 43 deletions(-)

diff --git a/src/aura_engine/core/channels.py b/src/aura_engine/core/channels.py
index a60ddfd7..78fdf229 100644
--- a/src/aura_engine/core/channels.py
+++ b/src/aura_engine/core/channels.py
@@ -306,7 +306,7 @@ class GenericChannel:
         response = self.mixer.client.exec(self.name, "set_track_metadata", json_metadata)
         msg = f"Response for '{self.name}.set_track_metadata': {response}"
         self.logger.info(SU.pink(msg))
-        if response not in PlayoutStatusResponse.SUCCESS.value:
+        if PlayoutStatusResponse(response) != PlayoutStatusResponse.SUCCESS:
             msg = f"Error while setting metadata on {self.name} to:\n{json_metadata}"
             self.logger.error(SU.red(msg))
             return False
@@ -485,7 +485,7 @@ class StreamChannel(GenericChannel):
         response = self.mixer.client.exec(self.name, "status")
         msg = f"{self.name}.status result: {response}"
         self.logger.info(SU.pink(msg))
-        if not response.startswith(PlayoutStatusResponse.STREAM_STATUS_CONNECTED.value):
+        if PlayoutStatusResponse(response) != PlayoutStatusResponse.STREAM_STATUS_CONNECTED:
             return False
 
         lqs_url = response.split(" ")[1]
@@ -509,7 +509,7 @@ class StreamChannel(GenericChannel):
         Stop the stream.
         """
         response = self.mixer.client.exec(self.name, "stop")
-        if response not in PlayoutStatusResponse.SUCCESS.value:
+        if PlayoutStatusResponse(response) != PlayoutStatusResponse.SUCCESS:
             self.logger.error(SU.red(f"{self.name}.stop result: {response}"))
             raise LoadSourceException("Error while stopping stream!")
         return response
@@ -520,7 +520,7 @@ class StreamChannel(GenericChannel):
         Set the stream URL.
         """
         response = self.mixer.client.exec(self.name, "url", url)
-        if response not in PlayoutStatusResponse.SUCCESS.value:
+        if PlayoutStatusResponse(response) != PlayoutStatusResponse.SUCCESS:
             self.logger.error(SU.red(f"{self.name}.url result: {response}"))
             raise LoadSourceException("Error while setting stream URL!")
         return response
@@ -619,10 +619,19 @@ class PlayoutStatusResponse(str, Enum):
     Response values indicating some status.
     """
 
-    SUCCESS = ["OK", "Done", "Done!", "Donee!"]
+    SUCCESS = "OK"
     STREAM_STATUS_POLLING = "polling"
     STREAM_STATUS_STOPPED = "stopped"
     STREAM_STATUS_CONNECTED = "connected"
+    INVALID = "invalid"
+
+    @classmethod
+    def _missing_(cls, value):
+        if value in ["Done", "Done!", "Donee!"]:
+            return cls.SUCCESS
+        if value.startswith(cls.STREAM_STATUS_CONNECTED):
+            return cls.STREAM_STATUS_CONNECTED
+        return cls.INVALID
 
 
 class LoadSourceException(Exception):
diff --git a/tests/core_client_mock.py b/tests/core_client_mock.py
index 13098b66..105869a0 100644
--- a/tests/core_client_mock.py
+++ b/tests/core_client_mock.py
@@ -67,15 +67,6 @@ class ChannelAction(StrEnum):
     SET_TRACK_METADATA = auto()
 
 
-class Response(StrEnum):
-    """
-    Using custom reponse type since PlayoutStatusResponse.SUCCESS is list instead of single string.
-    """
-
-    SUCCESS = "OK"  # PlayoutStatusResponse.SUCCESS
-    INVALID_URL = "Invalid URL"  # some arbitrary string which is not "OK" signaling failure
-
-
 class CoreClientMock(CoreClient):
     """
     Suppress unwanted behavior by subclassing and overriding necessary functions.
@@ -170,7 +161,7 @@ class CoreClientMock(CoreClient):
                         self.selections[chn] = sel
                         response = f"volume={vol}% remaining=0 selected={sel}"
                     case MixerAction.ACTIVATE:
-                        response = Response.SUCCESS
+                        response = PlayoutStatusResponse.SUCCESS
             case Namespace.CHANNEL:
                 act = ChannelAction(action)
                 if act is None:
@@ -187,26 +178,24 @@ class CoreClientMock(CoreClient):
                     case ChannelAction.URL:
                         if self._validate_url(args):
                             self.stream_url = args
-                            response = Response.SUCCESS
+                            response = PlayoutStatusResponse.SUCCESS
                         else:
-                            response = Response.INVALID_URL
+                            response = ""
                     case ChannelAction.STATUS:
                         response = (
-                            PlayoutStatusResponse.STREAM_STATUS_CONNECTED.value
-                            + " "
-                            + self.stream_url
+                            PlayoutStatusResponse.STREAM_STATUS_CONNECTED + " " + self.stream_url
                         )
                     case ChannelAction.START:
                         sleep(1)
-                        response = Response.SUCCESS
+                        response = PlayoutStatusResponse.SUCCESS
                     case ChannelAction.STOP:
-                        response = Response.SUCCESS
+                        response = PlayoutStatusResponse.SUCCESS
                     case ChannelAction.CLEAR:
-                        response = Response.SUCCESS
+                        response = PlayoutStatusResponse.SUCCESS
                     case ChannelAction.ROLL:
-                        response = Response.SUCCESS
+                        response = PlayoutStatusResponse.SUCCESS
                     case ChannelAction.SET_TRACK_METADATA:
-                        response = Response.SUCCESS
+                        response = PlayoutStatusResponse.SUCCESS
 
         if response is not None:
             log(f"Core mock reponse '{response}'")
diff --git a/tests/test_core_channels.py b/tests/test_core_channels.py
index c225604a..4e28081f 100644
--- a/tests/test_core_channels.py
+++ b/tests/test_core_channels.py
@@ -133,7 +133,10 @@ class TestPlayoutStatusResponse(unittest.TestCase):
     def test_enum(self):
         print(self._testMethodName)
 
-        # test string initializer
+        self.assertIs(PlayoutStatusResponse("OK"), PlayoutStatusResponse.SUCCESS)
+        self.assertIs(PlayoutStatusResponse("Done"), PlayoutStatusResponse.SUCCESS)
+        self.assertIs(PlayoutStatusResponse("Done!"), PlayoutStatusResponse.SUCCESS)
+        self.assertIs(PlayoutStatusResponse("Donee!"), PlayoutStatusResponse.SUCCESS)
         self.assertIs(
             PlayoutStatusResponse("polling"), PlayoutStatusResponse.STREAM_STATUS_POLLING
         )
@@ -141,15 +144,11 @@ class TestPlayoutStatusResponse(unittest.TestCase):
             PlayoutStatusResponse("stopped"), PlayoutStatusResponse.STREAM_STATUS_STOPPED
         )
         self.assertIs(
-            PlayoutStatusResponse("connected"), PlayoutStatusResponse.STREAM_STATUS_CONNECTED
+            PlayoutStatusResponse("connected <additional data>"),
+            PlayoutStatusResponse.STREAM_STATUS_CONNECTED,
         )
-
-        # testing vice-versa due to list type for SUCCESS case
-        self.assertTrue("OK" in PlayoutStatusResponse.SUCCESS.value)
-        self.assertTrue("Done" in PlayoutStatusResponse.SUCCESS.value)
-        self.assertTrue("Done!" in PlayoutStatusResponse.SUCCESS.value)
-        self.assertTrue("Donee!" in PlayoutStatusResponse.SUCCESS.value)
-        self.assertTrue("mkay" not in PlayoutStatusResponse.SUCCESS.value)
+        self.assertIs(PlayoutStatusResponse("mkay"), PlayoutStatusResponse.INVALID)
+        self.assertIs(PlayoutStatusResponse(""), PlayoutStatusResponse.INVALID)
 
 
 if __name__ == "__main__":
diff --git a/tests/test_core_mixer.py b/tests/test_core_mixer.py
index f568a6c1..11b95cb7 100644
--- a/tests/test_core_mixer.py
+++ b/tests/test_core_mixer.py
@@ -22,15 +22,7 @@ import unittest
 
 from aura_engine.base.config import AuraConfig
 from aura_engine.base.logger import AuraLogger
-from aura_engine.core.channels import (
-    ChannelFactory,
-    ChannelName,
-    ChannelType,
-    LineChannel,
-    PlayoutStatusResponse,
-    QueueChannel,
-    StreamChannel,
-)
+from aura_engine.core.channels import ChannelType, QueueChannel
 from aura_engine.core.mixer import Mixer
 from aura_engine.events import EngineEventDispatcher
 
-- 
GitLab