Files
ad_trident/tests/test_vertexcontent.py
2026-10-22 22:11:39 +02:00

117 lines
4.3 KiB
Python

from pathlib import Path
import sys
import unittest
from unittest.mock import Mock
APPS = Path(__file__).resolve().parents[1] / "apps"
sys.path[:0] = [str(APPS), str(APPS / "ad_toolbox")]
from vertexcontent import VertexContent
class Harness(VertexContent):
def __init__(self, states, sources):
self.states = states
self.sources = sources
self.input_selected = "sensor.vertex2_input_selected"
self.has_signal = "binary_sensor.vertex2_has_input_signal"
self.output = "sensor.vertex2_current_content"
@property
def name(self):
return "vertex_content"
def get_state(self, entity, attribute=None):
data = self.states.get(entity)
if attribute == "all":
return data
return data.get("state") if data else None
APPLE = {
"AppleTV": {
"label": "Apple TV",
"platform": "Apple TV",
"entity": "media_player.apple_tv",
"pause_states": ["paused"],
"pause_supported_apps": ["Netflix"],
}
}
class VertexContentTests(unittest.TestCase):
def states(self, player_state="playing", attributes=None, signal="on"):
return {
"sensor.vertex2_input_selected": {"state": "AppleTV", "attributes": {}},
"binary_sensor.vertex2_has_input_signal": {"state": signal, "attributes": {}},
"media_player.apple_tv": {
"state": player_state,
"attributes": attributes or {},
},
}
def test_uses_title_before_application(self):
app = Harness(self.states(attributes={
"media_title": "Dune",
"app_name": "Netflix",
"media_content_type": "movie",
}), APPLE)
state, attributes = app.build_content()
self.assertEqual(state, "Dune")
self.assertEqual(attributes["application"], "Netflix")
self.assertEqual(attributes["content_type"], "movie")
self.assertFalse(attributes["is_paused"])
self.assertTrue(attributes["is_pause_supported"])
def test_falls_back_to_application(self):
app = Harness(self.states(attributes={"app_name": "Netflix"}), APPLE)
state, attributes = app.build_content()
self.assertEqual(state, "Netflix")
self.assertIsNone(attributes["title"])
def test_pause_requires_paused_state_and_supported_app(self):
supported = Harness(self.states("paused", {"app_name": "Netflix"}), APPLE)
unsupported = Harness(self.states("paused", {"app_name": "Photos"}), APPLE)
self.assertTrue(supported.build_content()[1]["is_paused"])
self.assertFalse(unsupported.build_content()[1]["is_paused"])
self.assertFalse(unsupported.build_content()[1]["is_pause_supported"])
def test_no_signal_is_idle(self):
app = Harness(self.states(signal="off"), APPLE)
state, attributes = app.build_content()
self.assertEqual(state, "idle")
self.assertFalse(attributes["available"])
self.assertFalse(attributes["is_paused"])
def test_console_uses_activity_state(self):
states = {
"sensor.vertex2_input_selected": {"state": "Playstation", "attributes": {}},
"binary_sensor.vertex2_has_input_signal": {"state": "on", "attributes": {}},
"sensor.ps5_136_activity": {"state": "Astro Bot", "attributes": {}},
}
sources = {"Playstation": {
"label": "PlayStation 5",
"platform": "PlayStation 5",
"entity": "sensor.ps5_136_activity",
"state_as_title": True,
"content_type": "game",
}}
state, attributes = Harness(states, sources).build_content()
self.assertEqual(state, "Astro Bot")
self.assertEqual(attributes["content_type"], "game")
self.assertFalse(attributes["is_pause_supported"])
def test_updates_through_created_entity_handle(self):
app = Harness(self.states(attributes={"app_name": "Netflix"}), APPLE)
app.output_entity = Mock()
app.output_entity.get_state.side_effect = ["old", {"attributes": {}}]
app.update_content()
app.output_entity.set_state.assert_called_once()
update = app.output_entity.set_state.call_args.kwargs
self.assertEqual(update["state"], "Netflix")
self.assertEqual(update["attributes"]["ad_app"], "vertex_content")
if __name__ == "__main__":
unittest.main()