import pytest from agent_code import build_inline_agent_code, resolve_agent_code def test_space_and_configured_public_urls_are_supported(tmp_path): assert ( resolve_agent_code( space_id="owner/agent", configured_url=None, allow_inline=False, root=tmp_path, ) == "https://huggingface.co/spaces/owner/agent/tree/main" ) assert ( resolve_agent_code( space_id=None, configured_url="https://github.com/owner/agent", allow_inline=False, root=tmp_path, ) == "https://github.com/owner/agent" ) def test_invalid_or_missing_local_code_identity_fails_closed(tmp_path): with pytest.raises(ValueError, match="absolute HTTP"): resolve_agent_code( space_id=None, configured_url="C:/local/project", allow_inline=False, root=tmp_path, ) with pytest.raises(RuntimeError, match="Local submission needs"): resolve_agent_code( space_id=None, configured_url=None, allow_inline=False, root=tmp_path ) def test_explicit_inline_mode_bundles_only_declared_sources(tmp_path, monkeypatch): (tmp_path / "agent.py").write_text("class Agent: pass\n", encoding="utf-8") monkeypatch.setattr("agent_code.SOURCE_FILES", ("agent.py",)) (tmp_path / ".env").write_text("HF_TOKEN=secret", encoding="utf-8") bundled = build_inline_agent_code(tmp_path) assert "class Agent" in bundled assert "secret" not in bundled assert ( resolve_agent_code( space_id=None, configured_url=None, allow_inline=True, root=tmp_path ) == bundled )