"""Tests for app.gateway.services — run lifecycle service layer.""" from __future__ import annotations import json def test_format_sse_basic(): from app.gateway.services import format_sse frame = format_sse("metadata", {"run_id": "abc"}) assert frame.startswith("event: metadata\n") assert "data: " in frame parsed = json.loads(frame.split("data: ")[1].split("\n")[0]) assert parsed["run_id"] == "abc" def test_format_sse_with_event_id(): from app.gateway.services import format_sse frame = format_sse("metadata", {"run_id": "abc"}, event_id="123-0") assert "id: 123-0" in frame def test_format_sse_end_event_null(): from app.gateway.services import format_sse frame = format_sse("end", None) assert "data: null" in frame def test_format_sse_no_event_id(): from app.gateway.services import format_sse frame = format_sse("values", {"x": 1}) assert "id:" not in frame def test_normalize_stream_modes_none(): from app.gateway.services import normalize_stream_modes assert normalize_stream_modes(None) == ["values"] def test_normalize_stream_modes_string(): from app.gateway.services import normalize_stream_modes assert normalize_stream_modes("messages-tuple") == ["messages-tuple"] def test_normalize_stream_modes_list(): from app.gateway.services import normalize_stream_modes assert normalize_stream_modes(["values", "messages-tuple"]) == ["values", "messages-tuple"] def test_normalize_stream_modes_empty_list(): from app.gateway.services import normalize_stream_modes assert normalize_stream_modes([]) == ["values"] def test_normalize_input_none(): from app.gateway.services import normalize_input assert normalize_input(None) == {} def test_normalize_input_with_messages(): from app.gateway.services import normalize_input result = normalize_input({"messages": [{"role": "user", "content": "hi"}]}) assert len(result["messages"]) == 1 assert result["messages"][0].content == "hi" def test_normalize_input_passthrough(): from app.gateway.services import normalize_input result = normalize_input({"custom_key": "value"}) assert result == {"custom_key": "value"} def test_build_run_config_basic(): from app.gateway.services import build_run_config config = build_run_config("thread-1", None, None) assert config["configurable"]["thread_id"] == "thread-1" assert config["recursion_limit"] == 100 def test_build_run_config_with_overrides(): from app.gateway.services import build_run_config config = build_run_config( "thread-1", {"configurable": {"model_name": "gpt-4"}, "tags": ["test"]}, {"user": "alice"}, ) assert config["configurable"]["model_name"] == "gpt-4" assert config["tags"] == ["test"] assert config["metadata"]["user"] == "alice"