File size: 3,768 Bytes
3806142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f84efa
3806142
3da9c98
 
 
 
 
dbc2744
3da9c98
3806142
 
 
 
 
 
 
 
 
 
48a7b17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3da9c98
 
 
 
 
 
 
 
dbc2744
7f84efa
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pytest
from backend.app.providers.huggingface import (
    SpaceProviderError,
    classify_compatibility,
    parse_space_url,
)


def test_invalid_url_empty():
    with pytest.raises(SpaceProviderError) as ei:
        parse_space_url("")
    assert ei.value.code == "invalid_url"


def test_invalid_url_external():
    with pytest.raises(SpaceProviderError) as ei:
        parse_space_url("https://example.com/spaces/foo/bar")
    assert ei.value.code == "invalid_url"


def test_parse_full_url():
    assert parse_space_url("https://huggingface.co/spaces/gradio/hello_world") == (
        "gradio",
        "hello_world",
    )


def test_parse_repo_id():
    assert parse_space_url("gradio/hello_world") == ("gradio", "hello_world")


def test_classify_gpu_and_unknown():
    info = {
        "sdk": "unknown",
        "needs_gpu": True,
        "private": False,
        "repo_id": "x/y",
    }
    c = classify_compatibility(info)
    assert any("GPU" in w for w in c["warnings"])
    assert c["targets"][0]["id"] == "html_widget"
    stubs = [t for t in c["targets"] if t.get("stub")]
    assert [t["id"] for t in stubs] == ["windows_portable"]  # AppImage/PKGBUILD/MCP are real now
    assert all(not t["available"] for t in stubs)
    android = next(t for t in c["targets"] if t["id"] == "android")
    assert android["available"] is True
    assert android["stub"] is False
    assert android["mode"] == "hosted_wrapper"
    assert android["compat"]["project"] == "ok"
    assert android["compat"]["apk"] in {"ok", "warn"}
    assert android["compat"]["gpu_local"] == "no"  # needs_gpu True in this test


def test_classify_static_local_full():
    c = classify_compatibility({"sdk": "static", "needs_gpu": False, "private": False})
    assert c["local_offline_full"] is True


def test_classify_gradio_not_local_full():
    c = classify_compatibility({"sdk": "gradio", "needs_gpu": False, "private": False})
    assert c["local_offline_full"] is False


def test_normalize_hardware_dict():
    """HF returns runtime.hardware as an object — API must expose a string."""
    from backend.app.providers import huggingface as hf

    assert isinstance(
        # exercise the same logic via a tiny inline helper mirror
        (
            lambda hw_raw: (
                (hw_raw.get("current") or hw_raw.get("requested") or "unknown")
                if isinstance(hw_raw, dict)
                else (str(hw_raw) if hw_raw else "unknown")
            )
        )({"current": "cpu-basic", "requested": "cpu-basic"}),
        str,
    )
    # Also ensure needs_gpu scan works on stringified values
    info = {
        "sdk": "gradio",
        "needs_gpu": False,
        "private": False,
        "hardware": "cpu-basic",
    }
    c = classify_compatibility(info)
    assert c["hybrid_ok"] is True


def test_android_compat_no_gpu():
    c = classify_compatibility({"sdk": "gradio", "needs_gpu": False, "private": False})
    android = next(t for t in c["targets"] if t["id"] == "android")
    assert android["available"] is True
    assert android["mode"] == "hosted_wrapper"
    assert android["compat"]["gpu_local"] == "warn"
    assert "Hosted Wrapper" in android["reason"] or "project" in android["reason"].lower() or "APK" in android["reason"]


def test_new_targets_listed():
    c = classify_compatibility({"sdk": "gradio", "needs_gpu": False, "private": False})
    ids = [t["id"] for t in c["targets"]]
    for tid in ("linux_appimage", "arch_pkgbuild", "mcp"):
        assert tid in ids
        t = next(x for x in c["targets"] if x["id"] == tid)
        assert t["stub"] is False and t["mode"] == "hosted_wrapper"
    # new targets are listed above the remaining disabled stub
    assert ids.index("mcp") < ids.index("windows_portable")