File size: 1,723 Bytes
c641d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
    )