"""Tests for the combined-provider serve entrypoint (ratatoskr.provider.serve_combined).

Only the env -> app seam is unit-tested; uvicorn.run is the untestable shell.
"""
from __future__ import annotations

import pytest

from ratatoskr.provider.serve_combined import build_combined_app_from_env

_ENV = {
    "RATATOSKR_HEIMDALL_KEY": "shared-secret",
    "RATATOSKR_MEMORY_EMBEDDING_DIM": "8",
    "RATATOSKR_AFFECT_DB": ":memory:",
    "RATATOSKR_MEMORY_DB": ":memory:",
}


def test_requires_heimdall_key():
    env = {k: v for k, v in _ENV.items() if k != "RATATOSKR_HEIMDALL_KEY"}
    with pytest.raises(RuntimeError):
        build_combined_app_from_env(env)


def test_requires_embedding_dim():
    env = {k: v for k, v in _ENV.items() if k != "RATATOSKR_MEMORY_EMBEDDING_DIM"}
    with pytest.raises(RuntimeError):
        build_combined_app_from_env(env)


def test_builds_app_with_all_routes():
    app = build_combined_app_from_env(dict(_ENV))
    paths = {getattr(r, "path", None) for r in app.routes}
    assert "/bifrost/handshake" in paths
    assert "/bifrost/memory-call" in paths
    assert "/bifrost/affect-call" in paths
    assert "/affect/state/{agent_id}" in paths


def test_opfeed_path_wraps_app(tmp_path):
    env = dict(_ENV)
    env["RATATOSKR_OPFEED_PATH"] = str(tmp_path / "ops.jsonl")
    app = build_combined_app_from_env(env)
    assert not hasattr(app, "routes")  # wrapped: a bare ASGI callable (plane='combined')
