import json
import tempfile
import unittest

from evennia_tools.telemetry import append_event, event_log_path, excerpt


class TestEvenniaTelemetry(unittest.TestCase):
    def test_event_log_path_contains_session_id(self):
        path = event_log_path("session_abc", "/tmp/evennia-test")
        self.assertEqual(path.name, "session_abc.jsonl")

    def test_append_event_writes_jsonl(self):
        with tempfile.TemporaryDirectory() as td:
            path = append_event("session_xyz", {"event": "look", "room": "Gate"}, td)
            self.assertTrue(path.exists())
            line = path.read_text(encoding="utf-8").strip()
            data = json.loads(line)
            self.assertEqual(data["event"], "look")
            self.assertEqual(data["room"], "Gate")
            self.assertIn("timestamp", data)

    def test_excerpt_compacts_whitespace(self):
        self.assertEqual(excerpt("a  b\n c"), "a b c")


if __name__ == "__main__":
    unittest.main()
