"""Extension routing tests.""" import pytest from sandboxer.extensions.registry import load_extension from sandboxer.models import Profile, RouteSpec, RouteStrategy from sandboxer.routing.resolver import resolve_extension def _burst_profile() -> Profile: return Profile.model_validate( { "id": "profile.burst-sandbox", "version": "1.0.0", "extension": "ext.compose-ssh", "route": { "strategy": "prefer-self-hosted", "extensions": [ "ext.compose-ssh", "ext.e2b", "ext.modal", "ext.saas-stub", ], }, } ) def test_explicit_uses_profile_extension() -> None: profile = Profile.model_validate( { "id": "profile.compose-e2e", "version": "1.0.0", "extension": "ext.compose-ssh", } ) ext = resolve_extension(profile, {}, host_override="coulombcore") assert ext.id == "ext.compose-ssh" def test_prefer_self_hosted_when_host_set(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("SANDBOXER_HOST", "coulombcore") ext = resolve_extension(_burst_profile(), {"repo": "/tmp/x"}, host_override=None) assert ext.id == "ext.compose-ssh" def test_prefer_self_hosted_falls_back_to_saas(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delenv("SANDBOXER_HOST", raising=False) monkeypatch.delenv("E2B_API_KEY", raising=False) monkeypatch.delenv("MODAL_TOKEN_ID", raising=False) monkeypatch.setenv("SANDBOXER_FORCE_SAAS", "1") ext = resolve_extension(_burst_profile(), {}, host_override=None) assert ext.id == "ext.saas-stub" assert ext.capabilities.pricing_model == "metered" def test_prefer_self_hosted_falls_back_to_e2b_when_credentialed( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setenv("SANDBOXER_FORCE_SAAS", "1") monkeypatch.setenv("E2B_API_KEY", "test-key") ext = resolve_extension(_burst_profile(), {}, host_override=None) assert ext.id == "ext.e2b" def test_lowest_cost_picks_metered_when_forced(monkeypatch: pytest.MonkeyPatch) -> None: profile = _burst_profile() profile.route = RouteSpec( strategy=RouteStrategy.LOWEST_COST, extensions=["ext.compose-ssh", "ext.saas-stub"], ) monkeypatch.setenv("SANDBOXER_FORCE_SAAS", "1") ext = resolve_extension(profile, {}, host_override=None) assert load_extension(ext.id).capabilities.pricing_model == "metered"