from __future__ import annotations import json from server import _document_uses_moving, _merge_class_names, _merge_documents from tiny_trigger.automation import AutomationDocument, load_automation_text def test_merge_documents_replaces_same_named_rule() -> None: existing = load_automation_text( json.dumps( { "rules": [ { "name": "monitor-lights", "when": {"all": [{"present": {"label": "person"}}]}, "then": [{"type": "simulate", "name": "old action"}], } ] } ) ) compiled = load_automation_text( json.dumps( { "rules": [ { "name": "monitor-lights", "when": {"all": [{"present": {"label": "monitor"}}]}, "then": [{"type": "simulate", "name": "new action"}], } ] } ) ) merged = _merge_documents(existing, compiled) assert isinstance(merged, AutomationDocument) assert [rule.name for rule in merged.rules] == ["monitor-lights"] assert merged.rules[0].then[0].name == "new action" def test_merge_class_names_adds_rule_labels_without_duplicates() -> None: assert _merge_class_names(["person", "Monitor"], ["monitor", "guitar"]) == [ "person", "Monitor", "guitar", ] def test_document_uses_moving_only_for_motion_rules() -> None: presence = load_automation_text( json.dumps( { "rules": [ { "name": "person-visible", "when": {"all": [{"present": {"label": "person"}}]}, "then": [{"type": "simulate", "name": "notify"}], } ] } ) ) moving = load_automation_text( json.dumps( { "rules": [ { "name": "car-moving", "when": {"all": [{"moving": {"label": "car"}}]}, "then": [{"type": "simulate", "name": "notify"}], } ] } ) ) assert not _document_uses_moving(presence) assert _document_uses_moving(moving)