File size: 1,821 Bytes
8e8a34f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"<SP_LAYOUT>(\d+)\s+(\d+)\s+(\d+)\s+(\d+)</SP_LAYOUT>"
        ),
        "_META_RE": re.compile(r"<SP_META>(\{.*?\})</SP_META>"),
    }
    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 = (
        "<SP_LAYOUT>10 20 300 400</SP_LAYOUT>"
        "<SP_LAYOUT>300 20 10 400</SP_LAYOUT>"
        "<SP_LAYOUT>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('<SP_META>{"category":"title"}</SP_META>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