Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

30.0 KB
Raw Blame
"""Tests for config.py — manifest loading and service discovery."""

import json
import logging
from pathlib import Path

import pytest

import config
from config import (
    _apply_external_llm_service_override,
    _apply_host_native_llm_service_override,
    _detect_container_default_gateway,
    load_extension_manifests,
    _read_manifest_file,
)


VALID_MANIFEST = """\
schema_version: ods.services.v1
service:
  id: test-service
  name: Test Service
  port: 8080
  health: /health
  gpu_backends: [amd, nvidia]
  external_port_default: 8080
features:
  - id: test-feature
    name: Test Feature
    icon: Zap
    category: inference
    gpu_backends: [amd, nvidia]
"""


def test_apple_native_probes_follow_configured_endpoint_not_container_port():
    services = {"llama-server": {"host": "host.docker.internal", "port": 8080,
                                 "external_port": 8081}}
    _apply_host_native_llm_service_override(services, "apple", {
        "OLLAMA_URL": "http://host.docker.internal:8081",
        "LLM_API_URL": "http://model-router:9099",
    })
    assert services["llama-server"] == {
        "host": "host.docker.internal", "port": 8081, "external_port": 8081,
    }


@pytest.mark.parametrize("url", ["", "http://host:bad", "http://host:0", "https://host:8081",
                                  "file:///tmp/model", "http://user:secret@host:8081",
                                  "http://host:8081?token=x"])
def test_apple_native_probes_reject_invalid_endpoint(url):
    services = {"llama-server": {"host": "original", "port": 8080}}
    _apply_host_native_llm_service_override(services, "apple", {"OLLAMA_URL": url})
    assert services["llama-server"] == {"host": "original", "port": 8080}


def test_apple_native_probe_override_leaves_external_backend_to_its_resolver():
    services = {"llama-server": {"host": "original", "port": 8080}}
    _apply_host_native_llm_service_override(services, "apple", {
        "LLM_BACKEND": "external", "OLLAMA_URL": "http://host.docker.internal:8081",
    })
    assert services["llama-server"] == {"host": "original", "port": 8080}


def test_bundled_llama_server_is_discoverable_on_cpu_fallback():
    manifest_path = Path(__file__).resolve().parents[2] / "llama-server" / "manifest.yaml"
    manifest = config.yaml.safe_load(manifest_path.read_text(encoding="utf-8"))

    assert "cpu" in manifest["service"]["gpu_backends"]
    assert all("cpu" in feature["gpu_backends"] for feature in manifest["features"])


def test_aider_library_extension_is_discoverable_on_cpu_fallback(tmp_path):
    manifest_path = (
        Path(__file__).resolve().parents[3]
        / "library"
        / "services"
        / "aider"
        / "manifest.yaml"
    )
    manifest = config.yaml.safe_load(manifest_path.read_text(encoding="utf-8"))

    assert "cpu" in manifest["service"]["gpu_backends"]
    assert "none" in manifest["service"]["gpu_backends"]
    assert all("cpu" in feature["gpu_backends"] for feature in manifest["features"])
    catalog_path = Path(__file__).resolve().parents[4] / "config" / "extensions-catalog.json"
    catalog = json.loads(catalog_path.read_text(encoding="utf-8"))
    aider = next(ext for ext in catalog["extensions"] if ext["id"] == "aider")
    assert {"cpu", "none"}.issubset(aider["gpu_backends"])

    installed = tmp_path / "aider"
    installed.mkdir()
    (installed / "manifest.yaml").write_text(manifest_path.read_text(encoding="utf-8"))
    (installed / "compose.yaml").write_text("services:\n  aider:\n    image: test/aider\n")
    services, features, errors = load_extension_manifests(tmp_path, "cpu")
    assert errors == []
    assert "aider" in services
    assert any(feature["id"] == "ai-pair-programming" for feature in features)


def test_manifest_loader_rejects_pathological_nesting(tmp_path):
    nested = "value: leaf\n"
    for _ in range(config.MAX_MANIFEST_DEPTH + 2):
        nested = "value:\n  " + nested.replace("\n", "\n  ")
    manifest = tmp_path / "deep.yaml"
    manifest.write_text(nested, encoding="utf-8")

    with pytest.raises(ValueError, match="nesting exceeds"):
        _read_manifest_file(manifest)


@pytest.mark.parametrize(
    ("value", "expected"),
    [
        (None, "unknown"),
        ("", "unknown"),
        (" LOCAL ", "local"),
        ("cloud", "cloud"),
        ("HYBRID", "hybrid"),
        # The retired managed-AMD mode reads as local for one release.
        ("lemonade", "local"),
        (" Lemonade ", "local"),
        ("core", "unknown"),
    ],
)
def test_normalize_ods_mode(value, expected):
    assert config.normalize_ods_mode(value) == expected


def test_live_env_value_prefers_last_persisted_value(monkeypatch, tmp_path):
    (tmp_path / ".env").write_text(
        "LLM_MODEL=old-model\nLLM_MODEL=new-model\n",
        encoding="utf-8",
    )
    monkeypatch.setattr(config, "INSTALL_DIR", str(tmp_path))
    monkeypatch.setenv("LLM_MODEL", "stale-process-model")

    assert config.read_live_env_value("LLM_MODEL") == "new-model"


def test_live_env_value_preserves_explicit_empty_value(monkeypatch, tmp_path):
    (tmp_path / ".env").write_text("LEMONADE_MODEL=\n", encoding="utf-8")
    monkeypatch.setattr(config, "INSTALL_DIR", str(tmp_path))
    monkeypatch.setenv("LEMONADE_MODEL", "stale-process-model")

    assert config.read_live_env_value("LEMONADE_MODEL", "fallback") == ""


def test_live_env_value_strips_one_pair_and_preserves_unmatched_quotes(monkeypatch, tmp_path):
    (tmp_path / ".env").write_text(
        "PAIRED='model-v2'\n"
        "UNMATCHED=model-v2'\n"
        "REPEATED=''model-v2''\n",
        encoding="utf-8",
    )
    monkeypatch.setattr(config, "INSTALL_DIR", str(tmp_path))

    assert config.read_live_env_value("PAIRED") == "model-v2"
    assert config.read_live_env_value("UNMATCHED") == "model-v2'"
    assert config.read_live_env_value("REPEATED") == "'model-v2'"


class TestReadManifestFile:

    def test_reads_yaml(self, tmp_path):
        f = tmp_path / "manifest.yaml"
        f.write_text(VALID_MANIFEST)
        data = _read_manifest_file(f)
        assert data["schema_version"] == "ods.services.v1"
        assert data["service"]["id"] == "test-service"

    def test_reads_json(self, tmp_path):
        import json
        f = tmp_path / "manifest.json"
        f.write_text(json.dumps({
            "schema_version": "ods.services.v1",
            "service": {"id": "json-svc", "name": "JSON", "port": 9090},
        }))
        data = _read_manifest_file(f)
        assert data["service"]["id"] == "json-svc"

    def test_rejects_non_dict_root(self, tmp_path):
        f = tmp_path / "manifest.yaml"
        f.write_text("- just\n- a\n- list\n")
        with pytest.raises(ValueError, match="object"):
            _read_manifest_file(f)


class TestHostAgentResolution:

    def test_detect_container_default_gateway_reads_little_endian_route(self, tmp_path):
        route = tmp_path / "route"
        route.write_text(
            "Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n"
            "eth0\t00000000\t010012AC\t0003\t0\t0\t0\t00000000\t0\t0\t0\n",
            encoding="utf-8",
        )

        assert _detect_container_default_gateway(str(route)) == "172.18.0.1"

    def test_detect_container_default_gateway_requires_gateway_flag(self, tmp_path):
        route = tmp_path / "route"
        route.write_text(
            "Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n"
            "eth0\t00000000\t010012AC\t0001\t0\t0\t0\t00000000\t0\t0\t0\n",
            encoding="utf-8",
        )

        assert _detect_container_default_gateway(str(route)) == ""

    def test_resolve_agent_host_honors_explicit_env(self, monkeypatch):
        monkeypatch.setenv("ODS_AGENT_HOST", "  10.9.8.7  ")
        monkeypatch.setattr(config, "_running_inside_container", lambda: True)
        monkeypatch.setattr(config, "_detect_container_default_gateway", lambda: "172.18.0.1")

        assert config._resolve_agent_host() == "10.9.8.7"

    def test_resolve_agent_host_uses_gateway_inside_container(self, monkeypatch):
        monkeypatch.delenv("ODS_AGENT_HOST", raising=False)
        monkeypatch.setattr(config, "_running_inside_container", lambda: True)
        monkeypatch.setattr(config, "_running_under_wsl", lambda: False)
        monkeypatch.setattr(config, "_detect_container_default_gateway", lambda: "172.18.0.1")

        assert config._resolve_agent_host() == "172.18.0.1"

    def test_resolve_agent_host_uses_desktop_route_under_wsl(self, monkeypatch):
        monkeypatch.delenv("ODS_AGENT_HOST", raising=False)
        monkeypatch.setattr(config, "_running_inside_container", lambda: True)
        monkeypatch.setattr(config, "_running_under_wsl", lambda: True)
        monkeypatch.setattr(
            config,
            "_detect_container_default_gateway",
            lambda: (_ for _ in ()).throw(
                AssertionError("WSL must not select Docker Desktop's compose gateway")
            ),
        )

        assert config._resolve_agent_host() == "host.docker.internal"

    def test_resolve_agent_host_falls_back_outside_container(self, monkeypatch):
        monkeypatch.delenv("ODS_AGENT_HOST", raising=False)
        monkeypatch.setattr(config, "_running_inside_container", lambda: False)
        monkeypatch.setattr(config, "_detect_container_default_gateway", lambda: "192.168.1.1")

        assert config._resolve_agent_host() == "host.docker.internal"


class TestHostNativeLlmResolution:

    @pytest.mark.parametrize(("backend", "url_key"), [
        ("llama-server", "NATIVE_LLM_CONTAINER_BASE_URL"),
        # The origin's one-release legacy name, in an unmigrated .env.
        ("lemonade", "LEMONADE_CONTAINER_BASE_URL"),
    ])
    def test_wsl_cpu_surface_probes_the_windows_llama_server(self, backend, url_key):
        services = {"llama-server": {"host": "llama-server", "port": 8080}}
        _apply_host_native_llm_service_override(services, "cpu", {
            "LLM_BACKEND": backend,
            "AMD_INFERENCE_LOCATION": "host",
            url_key: "http://192.168.50.1:8181/v1",
            "OLLAMA_URL": "http://litellm:4000",
        })
        assert services["llama-server"] == {"host": "192.168.50.1", "port": 8181, "external_link": False}

    def test_host_side_url_and_gateway_are_never_probed(self):
        services = {"llama-server": {"host": "llama-server", "port": 8080}}
        _apply_host_native_llm_service_override(services, "amd", {
            "LLM_BACKEND": "llama-server",
            "AMD_INFERENCE_LOCATION": "host",
            "AMD_INFERENCE_PORT": "18080",
            # Windows loopback is not this container's loopback.
            "NATIVE_LLM_BASE_URL": "http://localhost:18080",
            "LEMONADE_BASE_URL": "http://localhost:13305",
            "LLM_API_URL": "http://litellm:4000",
        })
        assert services["llama-server"] == {
            "host": "host.docker.internal", "port": 18080, "external_link": False,
        }

    def test_container_runtime_does_not_use_host_endpoint(self):
        services = {"llama-server": {"host": "llama-server", "port": 8080}}
        _apply_host_native_llm_service_override(services, "cpu", {
            "LLM_BACKEND": "llama-server",
            "AMD_INFERENCE_LOCATION": "container",
            "NATIVE_LLM_CONTAINER_BASE_URL": "http://192.168.50.1:8181",
        })
        assert services["llama-server"] == {"host": "llama-server", "port": 8080}

    def test_routes_windows_amd_host_runtime_to_ollama_url(self):
        services = {"llama-server": {"host": "llama-server", "port": 8080}}
        env = {
            "AMD_INFERENCE_LOCATION": "host",
            "AMD_INFERENCE_PORT": "9090",
            "OLLAMA_URL": "http://host.docker.internal:9090/v1",
        }

        _apply_host_native_llm_service_override(services, "amd", env)

        assert services["llama-server"]["host"] == "host.docker.internal"
        assert services["llama-server"]["port"] == 9090
        # The container is off on this route; a sidebar link to its port opened an error page.
        assert services["llama-server"]["external_link"] is False

    @pytest.mark.parametrize(
        ("backend", "location"),
        [("nvidia", "host"), ("amd", "container"), ("amd", "external")],
    )
    def test_leaves_non_host_native_services_unchanged(self, backend, location):
        services = {"llama-server": {"host": "llama-server", "port": 8080}}

        _apply_host_native_llm_service_override(
            services,
            backend,
            {"AMD_INFERENCE_LOCATION": location, "OLLAMA_URL": "http://host.docker.internal:9090"},
        )

        assert services["llama-server"] == {"host": "llama-server", "port": 8080}


class TestExternalLlmResolution:

    @pytest.mark.parametrize(
        ("provider", "url", "expected_port", "expected_health", "expected_name"),
        [
            ("ollama", "http://host.docker.internal:11434", 11434, "/api/tags", "Ollama (External LLM)"),
            ("lmstudio", "http://host.docker.internal:1234", 1234, "/v1/models", "LM Studio (External LLM)"),
            ("", "https://llm.example.test", 443, "/v1/models", "External LLM"),
        ],
    )
    def test_routes_external_runtime_to_configured_endpoint(
        self, provider, url, expected_port, expected_health, expected_name,
    ):
        services = {"llama-server": {"host": "llama-server", "port": 8080, "health": "/health"}}

        _apply_external_llm_service_override(
            services,
            {
                "LLM_BACKEND": "external",
                "EXTERNAL_LLM_PROVIDER": provider,
                "EXTERNAL_LLM_CONTAINER_URL": url,
            },
        )

        assert services["llama-server"] == {
            "host": "host.docker.internal" if "host.docker.internal" in url else "llm.example.test",
            "port": expected_port,
            "scheme": url.split("://")[0],
            "external_api": True,
            "health": expected_health,
            "name": expected_name,
        }

    @pytest.mark.parametrize(
        "environment",
        [
            {},
            {"LLM_BACKEND": "llama-server", "EXTERNAL_LLM_CONTAINER_URL": "http://host.docker.internal:11434"},
            {"LLM_BACKEND": "external", "EXTERNAL_LLM_CONTAINER_URL": "not-a-url"},
        ],
    )
    def test_leaves_service_unchanged_without_valid_external_contract(self, environment):
        services = {"llama-server": {"host": "llama-server", "port": 8080, "health": "/health"}}

        _apply_external_llm_service_override(services, environment)

        assert services["llama-server"] == {
            "host": "llama-server",
            "port": 8080,
            "health": "/health",
        }


class TestLoadExtensionManifests:

    def test_loads_valid_manifest(self, tmp_path):
        svc_dir = tmp_path / "test-service"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(VALID_MANIFEST)

        services, features, _ = load_extension_manifests(tmp_path, "nvidia")
        assert "test-service" in services
        assert services["test-service"]["port"] == 8080
        assert services["test-service"]["name"] == "Test Service"
        assert services["test-service"]["health"] == "/health"
        assert len(features) == 1
        assert features[0]["id"] == "test-feature"

    def test_normalizes_llm_contract(self, tmp_path):
        svc_dir = tmp_path / "llm-app"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n"
            "  id: llm-app\n"
            "  name: LLM App\n"
            "  port: 8080\n"
            "  health: /health\n"
            "  llm:\n"
            "    consumes: true\n"
            "    route: direct\n"
            "    pinning: none\n"
            "    min_context: 65536\n"
            "    probe:\n"
            "      kind: chat\n"
            "      path: /v1/chat/completions\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "nvidia")

        llm = services["llm-app"]["llm"]
        assert llm["consumes"] is True
        assert llm["route"] == "direct"
        assert llm["pinning"] == "none"
        assert llm["min_context"] == 65536
        assert llm["probe"]["kind"] == "chat"
        assert llm["swap_safe"] is False
        assert llm["badge"] == "not-swap-safe"

    def test_skips_docker_service_when_declared_compose_file_is_absent(self, tmp_path):
        svc_dir = tmp_path / "sample-agent"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n"
            "  id: sample-agent\n"
            "  name: Sample Agent\n"
            "  type: docker\n"
            "  compose_file: compose.yaml\n"
            "  port: 18789\n"
            "  health: /\n"
            "  llm:\n"
            "    consumes: true\n"
            "    route: direct\n"
            "    pinning: none\n"
            "features:\n"
            "  - id: sample-agent-feature\n"
            "    name: Sample Agent Feature\n"
        )

        services, features, _ = load_extension_manifests(tmp_path, "nvidia")

        assert "sample-agent" not in services
        assert features == []

    def test_loads_docker_service_when_declared_compose_file_exists(self, tmp_path):
        svc_dir = tmp_path / "sample-agent"
        svc_dir.mkdir()
        (svc_dir / "compose.yaml").write_text("services:\n  sample-agent:\n    image: test\n")
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n"
            "  id: sample-agent\n"
            "  name: Sample Agent\n"
            "  type: docker\n"
            "  compose_file: compose.yaml\n"
            "  port: 18789\n"
            "  health: /\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "nvidia")

        assert "sample-agent" in services

    def test_builtin_llm_probe_paths_match_live_service_routes(self):
        services_dir = Path(__file__).resolve().parents[2]

        services, _, _ = load_extension_manifests(services_dir, "nvidia")

        assert services["open-webui"]["llm"]["probe"]["path"] == "/openai/v1/chat/completions"
        assert services["perplexica"]["llm"]["probe"]["path"] == "/api/search"
        assert services["privacy-shield"]["llm"]["probe"]["path"] == "/v1/chat/completions"

    def test_open_webui_core_consumer_is_discoverable_on_cpu_backend(self):
        """CPU/external-LLM installs still run Open WebUI through the gateway."""
        services_dir = Path(__file__).resolve().parents[2]

        services, _, errors = load_extension_manifests(services_dir, "cpu")

        assert errors == []
        assert services["open-webui"]["category"] == "core"
        assert services["open-webui"]["llm"]["consumes"] is True
        assert services["open-webui"]["llm"]["route"] == "gateway"

    def test_external_port_default_zero_disables_external_port_fallback(self, tmp_path):
        svc_dir = tmp_path / "internal-service"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n"
            "  id: internal-service\n"
            "  name: Internal Service\n"
            "  port: 9119\n"
            "  external_port_default: 0\n"
            "  health: /api/status\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "nvidia")

        assert services["internal-service"]["port"] == 9119
        assert services["internal-service"]["external_port"] == 0

    def test_loads_public_url_from_service_env_convention(self, tmp_path, monkeypatch):
        monkeypatch.setenv("TEST_SERVICE_PUBLIC_URL", "https://test.example/ui/")
        svc_dir = tmp_path / "test-service"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(VALID_MANIFEST)

        services, _, _ = load_extension_manifests(tmp_path, "nvidia")

        assert services["test-service"]["public_url"] == "https://test.example/ui"

    def test_loads_public_url_from_json_map(self, tmp_path, monkeypatch):
        monkeypatch.setenv("ODS_SERVICE_PUBLIC_URLS", '{"test-service":"https://svc.example"}')
        svc_dir = tmp_path / "test-service"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(VALID_MANIFEST)

        services, _, _ = load_extension_manifests(tmp_path, "nvidia")

        assert services["test-service"]["public_url"] == "https://svc.example"

    def test_rejects_unsafe_public_url(self, tmp_path, monkeypatch):
        monkeypatch.setenv("TEST_SERVICE_PUBLIC_URL", "javascript:alert(1)")
        svc_dir = tmp_path / "test-service"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(VALID_MANIFEST)

        services, _, _ = load_extension_manifests(tmp_path, "nvidia")

        assert services["test-service"]["public_url"] == ""

    def test_reads_public_url_map_from_utf8_env_file(self, tmp_path, monkeypatch):
        install_dir = tmp_path / "ods"
        install_dir.mkdir()
        (install_dir / ".env").write_text(
            "# operator note: public URLs -> browser routes 你好\n"
            'ODS_SERVICE_PUBLIC_URLS={"test-service":"https://svc.example"}\n',
            encoding="utf-8",
        )
        monkeypatch.delenv("ODS_SERVICE_PUBLIC_URLS", raising=False)
        monkeypatch.setattr(config, "INSTALL_DIR", str(install_dir))

        assert config._read_env_value("ODS_SERVICE_PUBLIC_URLS") == '{"test-service":"https://svc.example"}'

    def test_preserves_host_network_flag(self, tmp_path):
        svc_dir = tmp_path / "host-network-service"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n"
            "  id: host-network-service\n"
            "  name: Host Network Service\n"
            "  host_network: true\n"
            "  port: 0\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "nvidia")

        assert services["host-network-service"]["host_network"] is True

    def test_skips_wrong_schema_version(self, tmp_path):
        svc_dir = tmp_path / "old-service"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v0\nservice:\n  id: old\n  port: 80\n"
        )

        services, features, errors = load_extension_manifests(tmp_path, "nvidia")
        assert len(services) == 0
        assert len(errors) == 1
        assert "Unsupported schema_version" in errors[0]["error"]

    def test_filters_by_gpu_backend(self, tmp_path):
        svc_dir = tmp_path / "nvidia-only"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: nvidia-only\n  name: NVIDIA Only\n  port: 80\n"
            "  gpu_backends: [nvidia]\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "amd")
        assert len(services) == 0

        services, _, _ = load_extension_manifests(tmp_path, "nvidia")
        assert "nvidia-only" in services

    def test_empty_directory(self, tmp_path):
        services, features, _ = load_extension_manifests(tmp_path, "nvidia")
        assert services == {}
        assert features == []

    def test_nonexistent_directory(self, tmp_path):
        missing = tmp_path / "does-not-exist"
        services, features, _ = load_extension_manifests(missing, "nvidia")
        assert services == {}
        assert features == []

    def test_features_filtered_by_gpu(self, tmp_path):
        svc_dir = tmp_path / "mixed"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: mixed\n  name: Mixed\n  port: 80\n"
            "  gpu_backends: [amd, nvidia]\n"
            "features:\n"
            "  - id: amd-feat\n    name: AMD Feature\n    gpu_backends: [amd]\n"
            "  - id: both-feat\n    name: Both Feature\n    gpu_backends: [amd, nvidia]\n"
        )

        _, features, _ = load_extension_manifests(tmp_path, "nvidia")
        feature_ids = [f["id"] for f in features]
        assert "both-feat" in feature_ids
        assert "amd-feat" not in feature_ids

    def test_apple_backend_discovers_services_without_explicit_list(self, tmp_path):
        """Services with no gpu_backends key default to [amd, nvidia, apple]."""
        svc_dir = tmp_path / "generic-svc"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: generic-svc\n  name: Generic\n  port: 80\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "apple")
        assert "generic-svc" in services

    def test_apple_backend_filtered_by_explicit_nvidia_amd_list(self, tmp_path):
        """Docker service explicitly listing [amd, nvidia] is still loaded for apple backend."""
        svc_dir = tmp_path / "gpu-only-svc"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: gpu-only-svc\n  name: GPU Only\n  port: 80\n"
            "  gpu_backends: [amd, nvidia]\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "apple")
        assert "gpu-only-svc" in services

    def test_apple_backend_discovers_service_explicitly_listing_apple(self, tmp_path):
        """Service that lists apple in gpu_backends is discovered for apple backend."""
        svc_dir = tmp_path / "apple-svc"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: apple-svc\n  name: Apple Svc\n  port: 80\n"
            "  gpu_backends: [amd, nvidia, apple]\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "apple")
        assert "apple-svc" in services

    def test_apple_backend_feature_default_discovered(self, tmp_path):
        """Features with no gpu_backends key default to include apple."""
        svc_dir = tmp_path / "svc-with-feature"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: svc\n  name: Svc\n  port: 80\n"
            "features:\n"
            "  - id: default-feat\n    name: Default Feature\n"
        )

        _, features, _ = load_extension_manifests(tmp_path, "apple")
        assert any(f["id"] == "default-feat" for f in features)

    def test_apple_backend_excludes_host_systemd(self, tmp_path):
        """Services with type: host-systemd are excluded on apple backend."""
        svc_dir = tmp_path / "systemd-svc"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: systemd-svc\n  name: Systemd Svc\n  port: 80\n"
            "  type: host-systemd\n"
            "  gpu_backends: [amd, nvidia]\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "apple")
        assert "systemd-svc" not in services

    def test_apple_backend_includes_explicitly_supported_host_service(self, tmp_path):
        """A cross-platform host service may declare a macOS LaunchAgent."""
        svc_dir = tmp_path / "host-svc"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: host-svc\n  name: Host Svc\n  port: 3003\n"
            "  type: host-systemd\n"
            "  macos_host_supported: true\n"
            "  gpu_backends: [all]\n"
        )

        services, _, _ = load_extension_manifests(tmp_path, "apple")
        assert services["host-svc"]["macos_host_supported"] is True

    def test_apple_backend_loads_all_features(self, tmp_path):
        """Features with gpu_backends: [amd, nvidia] are loaded for apple backend."""
        svc_dir = tmp_path / "svc-with-gpu-feature"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: svc\n  name: Svc\n  port: 80\n"
            "features:\n"
            "  - id: gpu-feat\n    name: GPU Feature\n    gpu_backends: [amd, nvidia]\n"
        )

        _, features, _ = load_extension_manifests(tmp_path, "apple")
        assert any(f["id"] == "gpu-feat" for f in features)

    def test_warns_on_missing_optional_feature_fields(self, tmp_path, caplog):
        """A feature missing optional fields is loaded but a warning is logged."""
        svc_dir = tmp_path / "sparse-svc"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  id: sparse-svc\n  name: Sparse\n  port: 80\n"
            "features:\n"
            "  - id: sparse-feat\n    name: Sparse Feature\n"
        )

        with caplog.at_level(logging.WARNING, logger="config"):
            _, features, _ = load_extension_manifests(tmp_path, "nvidia")

        assert any(f["id"] == "sparse-feat" for f in features)
        warning_msgs = [r.message for r in caplog.records if "missing optional fields" in r.message]
        assert len(warning_msgs) == 1
        assert "sparse-feat" in warning_msgs[0]
        for field in ("description", "icon", "category", "setup_time", "priority"):
            assert field in warning_msgs[0]

    def test_collects_parse_errors(self, tmp_path):
        svc_dir = tmp_path / "broken-svc"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text("invalid: yaml: [unterminated")

        services, features, errors = load_extension_manifests(tmp_path, "nvidia")
        assert len(errors) == 1
        assert "file" in errors[0]
        assert "error" in errors[0]
        assert "broken-svc" in errors[0]["file"]
        assert services == {}
        assert features == []

    def test_collects_error_for_missing_service_id(self, tmp_path):
        """A manifest with a valid schema but no service.id collects an error."""
        svc_dir = tmp_path / "no-id-svc"
        svc_dir.mkdir()
        (svc_dir / "manifest.yaml").write_text(
            "schema_version: ods.services.v1\n"
            "service:\n  name: No ID\n  port: 80\n"
        )

        services, features, errors = load_extension_manifests(tmp_path, "nvidia")
        assert len(errors) == 1
        assert "service.id is required" in errors[0]["error"]
        assert "no-id-svc" in errors[0]["file"]
        assert services == {}