2026-05-29 12:26:34 +02:00
|
|
|
"""Tests for EventEmitter."""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from ha_diag.event_emitter import EventEmitter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_emit_creates_json_file(tmp_events_dir: Path, emitter: EventEmitter):
|
|
|
|
|
event_id = emitter.emit(
|
|
|
|
|
event_type="ha_websocket_dead",
|
|
|
|
|
severity="error",
|
|
|
|
|
service="homeassistant",
|
|
|
|
|
message="HA unreachable",
|
|
|
|
|
payload={"error": "timeout"},
|
|
|
|
|
)
|
|
|
|
|
files = list(tmp_events_dir.glob("*.json"))
|
|
|
|
|
assert len(files) == 1
|
|
|
|
|
assert files[0].name == f"{event_id}.json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_emit_event_schema(tmp_events_dir: Path, emitter: EventEmitter):
|
|
|
|
|
event_id = emitter.emit(
|
|
|
|
|
event_type="ha_websocket_dead",
|
|
|
|
|
severity="error",
|
|
|
|
|
service="homeassistant",
|
|
|
|
|
message="HA unreachable",
|
|
|
|
|
payload={"error": "timeout"},
|
|
|
|
|
)
|
|
|
|
|
data = json.loads((tmp_events_dir / f"{event_id}.json").read_text())
|
|
|
|
|
assert data["id"] == event_id
|
|
|
|
|
assert data["type"] == "ha_websocket_dead"
|
|
|
|
|
assert data["severity"] == "error"
|
|
|
|
|
assert data["node"] == "test-node"
|
|
|
|
|
assert data["service"] == "homeassistant"
|
|
|
|
|
assert data["message"] == "HA unreachable"
|
|
|
|
|
assert data["payload"] == {"error": "timeout"}
|
|
|
|
|
assert "timestamp" in data
|
|
|
|
|
assert "date" in data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_emit_multiple_events_unique_files(tmp_events_dir: Path, emitter: EventEmitter):
|
|
|
|
|
ids = [
|
|
|
|
|
emitter.emit("ha_websocket_dead", "error", "homeassistant", f"msg {i}")
|
|
|
|
|
for i in range(3)
|
|
|
|
|
]
|
|
|
|
|
assert len(set(ids)) == 3
|
|
|
|
|
assert len(list(tmp_events_dir.glob("*.json"))) == 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_emit_no_tmp_file_left(tmp_events_dir: Path, emitter: EventEmitter):
|
|
|
|
|
emitter.emit("ha_websocket_dead", "error", "homeassistant", "msg")
|
|
|
|
|
assert not list(tmp_events_dir.glob("*.tmp"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_emitter_creates_events_dir(tmp_path: Path):
|
|
|
|
|
new_dir = tmp_path / "nested" / "events"
|
|
|
|
|
emitter = EventEmitter(new_dir, "my-node")
|
|
|
|
|
assert new_dir.exists()
|
2026-05-29 12:56:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_location_tag_included_in_payload(tmp_events_dir: Path):
|
|
|
|
|
emitter = EventEmitter(tmp_events_dir, node_name="piha", location_tag="ken")
|
|
|
|
|
event_id = emitter.emit("ha_websocket_dead", "error", "homeassistant", "msg")
|
|
|
|
|
data = json.loads((tmp_events_dir / f"{event_id}.json").read_text())
|
|
|
|
|
assert data["payload"]["location_tag"] == "ken"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_location_tag_empty_not_in_payload(tmp_events_dir: Path):
|
|
|
|
|
emitter = EventEmitter(tmp_events_dir, node_name="piha", location_tag="")
|
|
|
|
|
event_id = emitter.emit("ha_websocket_dead", "error", "homeassistant", "msg")
|
|
|
|
|
data = json.loads((tmp_events_dir / f"{event_id}.json").read_text())
|
|
|
|
|
assert "location_tag" not in data["payload"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_location_tag_does_not_override_explicit_payload_key(tmp_events_dir: Path):
|
|
|
|
|
emitter = EventEmitter(tmp_events_dir, node_name="piha", location_tag="ken")
|
|
|
|
|
event_id = emitter.emit(
|
|
|
|
|
"ha_websocket_dead", "error", "homeassistant", "msg",
|
|
|
|
|
payload={"location_tag": "override", "other": "value"},
|
|
|
|
|
)
|
|
|
|
|
data = json.loads((tmp_events_dir / f"{event_id}.json").read_text())
|
|
|
|
|
# Explicit payload key wins over the emitter's location_tag
|
|
|
|
|
assert data["payload"]["location_tag"] == "override"
|
|
|
|
|
assert data["payload"]["other"] == "value"
|