import asyncio import zipfile from pathlib import Path import pytest from backend.app.converters.base import BuildContext from backend.app.converters.firefox_ext import FirefoxExtConverter from backend.app.converters.html_widget import HtmlWidgetConverter from backend.app.converters.pwa import PwaConverter from backend.app.converters.stubs import WindowsPortableStub from backend.app.converters.tauri import TauriConverter SAMPLE = { "repo_id": "gradio/hello_world", "owner": "gradio", "name": "hello_world", "title": "Hello World", "emoji": "👋", "short_description": "demo", "sdk": "gradio", "embed_url": "https://gradio-hello-world.hf.space", "space_url": "https://huggingface.co/spaces/gradio/hello_world", } def _ctx(tmp_path: Path) -> BuildContext: return BuildContext(build_id="test", work_dir=tmp_path, space_info=SAMPLE) @pytest.mark.asyncio async def test_html_widget(tmp_path: Path): r = await HtmlWidgetConverter().convert(_ctx(tmp_path)) assert r.ok and r.artifact assert r.artifact.mode == "hybrid" assert zipfile.is_zipfile(r.artifact.path) with zipfile.ZipFile(r.artifact.path) as zf: names = zf.namelist() assert any(n.endswith("index.html") for n in names) assert any("spaceforge.json" in n for n in names) @pytest.mark.asyncio async def test_pwa(tmp_path: Path): r = await PwaConverter().convert(_ctx(tmp_path)) assert r.ok and r.artifact assert r.artifact.mode == "hybrid" with zipfile.ZipFile(r.artifact.path) as zf: data = zf.read([n for n in zf.namelist() if n.endswith("manifest.webmanifest")][0]) assert b"Hello World" in data or b"hello" in data.lower() @pytest.mark.asyncio async def test_firefox(tmp_path: Path): r = await FirefoxExtConverter().convert(_ctx(tmp_path)) assert r.ok and r.artifact assert r.artifact.filename.endswith(".xpi") assert r.artifact.mode == "hybrid" with zipfile.ZipFile(r.artifact.path) as zf: man = zf.read("manifest.json") assert b"manifest_version" in man @pytest.mark.asyncio async def test_tauri_project_only(tmp_path: Path): r = await TauriConverter().convert(_ctx(tmp_path)) assert r.ok and r.artifact assert r.artifact.mode == "local" assert "project" in r.artifact.filename with zipfile.ZipFile(r.artifact.path) as zf: assert any("tauri.conf.json" in n for n in zf.namelist()) meta = zf.read([n for n in zf.namelist() if n.endswith("spaceforge.json")][0]) assert b'"binary_included": false' in meta.replace(b" ", b"") or b'"binary_included": false' in meta @pytest.mark.asyncio async def test_stub_windows_portable(tmp_path: Path): r = await WindowsPortableStub().convert(_ctx(tmp_path)) assert not r.ok assert r.project_only assert "not available" in (r.error or "").lower() or "HF" in (r.error or "") @pytest.mark.asyncio async def test_android_capacitor_project(tmp_path: Path, monkeypatch): # Unit tests force project-zip path (no SDK assemble in CI) monkeypatch.setenv("SPACEFORGE_ANDROID_SDK", "0") monkeypatch.delenv("ANDROID_HOME", raising=False) monkeypatch.delenv("ANDROID_SDK_ROOT", raising=False) from backend.app.converters.mobile.android import AndroidConverter r = await AndroidConverter().convert(_ctx(tmp_path)) assert r.ok and r.artifact assert r.project_only assert r.artifact.mode == "hosted_wrapper" assert "project" in r.artifact.filename assert r.artifact.filename.endswith(".zip") assert "FALLBACK" in (r.artifact.notes or "") or "project zip" in (r.artifact.notes or "").lower() with zipfile.ZipFile(r.artifact.path) as zf: names = zf.namelist() assert any("capacitor.config.json" in n for n in names) assert any(n.endswith("MainActivity.java") for n in names) assert any("build-android.sh" in n for n in names) assert any(n.endswith("ANDROID.md") for n in names) assert any(n.endswith("AndroidManifest.xml") for n in names) meta_name = [n for n in names if n.endswith("spaceforge.json")][0] meta = zf.read(meta_name) assert b'"binary_included": false' in meta or b'"binary_included":false' in meta.replace(b" ", b"") assert b'"apk_included": false' in meta or b'"apk_included":false' in meta.replace(b" ", b"") assert b"hosted_wrapper" in meta assert b'"artifact_kind": "project-zip"' in meta or b"project-zip" in meta # Never claim Local APK in labeling assert b"Local APK" in meta # listed under never_claim cfg = zf.read([n for n in names if n.endswith("capacitor.config.json") and "/android/" not in n and "assets" not in n][0]) assert b"hf.space" in cfg or b"server" in cfg @pytest.mark.asyncio async def test_android_gpu_notes(tmp_path: Path, monkeypatch): monkeypatch.setenv("SPACEFORGE_ANDROID_SDK", "0") monkeypatch.delenv("ANDROID_HOME", raising=False) monkeypatch.delenv("ANDROID_SDK_ROOT", raising=False) from backend.app.converters.mobile.android import AndroidConverter info = dict(SAMPLE) info["needs_gpu"] = True ctx = BuildContext(build_id="test", work_dir=tmp_path, space_info=info) r = await AndroidConverter().convert(ctx) assert r.ok and r.artifact with zipfile.ZipFile(r.artifact.path) as zf: meta = zf.read([n for n in zf.namelist() if n.endswith("spaceforge.json")][0]) assert b'"gpu_local": "no"' in meta or b'"gpu_local":"no"' in meta.replace(b" ", b"")