from __future__ import annotations
import ast
import json
import re
from pathlib import Path
ROOT = Path(__file__).parents[1]
def _app_functions(*names: str):
source = (ROOT / "app.py").read_text(encoding="utf-8")
module = ast.parse(source)
selected = [
node
for node in module.body
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in names
]
namespace = {
"json": json,
"_LAYOUT_RE": re.compile(
r"(\d+)\s+(\d+)\s+(\d+)\s+(\d+)"
),
"_META_RE": re.compile(r"(\{.*?\})"),
}
helper_module = ast.Module(body=selected, type_ignores=[])
exec(compile(helper_module, "app.py", "exec"), namespace)
return namespace
def test_layout_parser_accepts_only_valid_complete_boxes() -> None:
functions = _app_functions("parse_layout_boxes")
parse = functions["parse_layout_boxes"]
text = (
"10 20 300 400"
"300 20 10 400"
"1 2 3"
)
assert parse(text) == [(10, 20, 300, 400)]
def test_branch_parser_extracts_metadata_and_content() -> None:
functions = _app_functions("parse_branch_text")
parse = functions["parse_branch_text"]
category, content = parse('{"category":"title"}Quarterly report')
assert category == "title"
assert content == "Quarterly report"
def test_space_is_parallel_only_and_streaming() -> None:
source = (ROOT / "app.py").read_text(encoding="utf-8")
assert '@spaces.GPU(duration=120, size="large")' in source
assert 'execution_mode="parallel"' in source
assert 'os.environ.get("SPACES_ZERO_GPU") == "1"' in source
assert "yield (" in source
assert "gr.Radio" not in source