"""Unit tests for lifecycle hook helpers."""

import pytest

from src.services.instance.lifecycle_hooks import _derive_title, _should_notify


class TestDeriveTitle:
    """Tests for _derive_title."""

    def test_known_event_types(self) -> None:
        assert _derive_title("instance.created") == "Container created"
        assert _derive_title("instance.started") == "Container started"
        assert _derive_title("instance.stopped") == "Container stopped"
        assert _derive_title("instance.restarted") == "Container restarted"
        assert _derive_title("instance.deleted") == "Container deleted"
        assert _derive_title("instance.error") == "Container error"
        assert _derive_title("instance.health_changed") == "Container ready"

    def test_unknown_event_type(self) -> None:
        assert _derive_title("instance.custom_event") == "Custom Event"


class TestShouldNotify:
    """Tests for _should_notify filtering."""

    def test_error_events_are_notified(self) -> None:
        assert _should_notify("instance.error", "error") is True
        assert _should_notify("instance.error", None) is True

    def test_health_changed_running_is_notified(self) -> None:
        assert _should_notify("instance.health_changed", "running") is True

    def test_created_started_stopped_restarted_deleted_filtered(self) -> None:
        for event in [
            "instance.created",
            "instance.started",
            "instance.stopped",
            "instance.restarted",
            "instance.deleted",
        ]:
            assert _should_notify(event, "pending") is False
            assert _should_notify(event, "running") is False
            assert _should_notify(event, None) is False

    def test_health_changed_non_running_filtered(self) -> None:
        assert _should_notify("instance.health_changed", "unhealthy") is False
        assert _should_notify("instance.health_changed", "starting") is False
        assert _should_notify("instance.health_changed", None) is False
