File size: 1,983 Bytes
d82fef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# tests/test_app_smoke.py
"""
Smoke test that simulates a CPU-only Hugging Face Spaces import.
It *does not* start any server or UI. If no app module exists, the test is skipped.
"""

import sys
import pathlib
import importlib
import inspect
import pytest

def _import_app_module():
    root = pathlib.Path(__file__).resolve().parents[1]
    sys.path.insert(0, str(root))
    sys.path.insert(0, str(root / "src"))

    # Prefer real files if present; otherwise try a plain "app" import
    candidates = []
    if (root / "app.py").exists():
        candidates.append("app")
    if (root / "src" / "app.py").exists():
        candidates.append("app")
    if not candidates:
        candidates = ["app"]

    last_err = None
    for name in candidates:
        try:
            return importlib.import_module(name)
        except Exception as e:
            last_err = e
    pytest.skip(f"No importable app module found (last error: {last_err})")

def _try_tiny_call(module) -> bool:
    preferred = ("ping", "healthcheck", "health_check", "hello", "version", "get_version")
    fallback  = ("predict", "inference", "run", "main", "chord_bot")

    def try_call(fn):
        try:
            sig = inspect.signature(fn)
            if len(sig.parameters) == 0:
                fn()
                return True
            if len(sig.parameters) == 1:
                fn("test")
                return True
        except Exception:
            return False
        return False

    called = False
    for name in list(preferred) + list(fallback):
        if hasattr(module, name) and callable(getattr(module, name)):
            if try_call(getattr(module, name)):
                called = True
                break
    return called

def test_import_app_and_optionally_call():
    module = _import_app_module()
    assert module is not None  # Import alone verifies basic environment
    # Try a non-server, tiny function call if present; ok if none fits
    _try_tiny_call(module)