Spaces:
Sleeping
Sleeping
| """ | |
| Smoke tests that DO NOT hit the network. | |
| Run with: pytest apps/backend/tests | |
| """ | |
| from __future__ import annotations | |
| import importlib | |
| import os | |
| import sys | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) | |
| def test_imports(): | |
| """Every backend module imports cleanly.""" | |
| for mod in ("backend.app", "backend.agent", "backend.intent", | |
| "backend.llm_router", "backend.executor"): | |
| importlib.import_module(mod) | |
| def test_intent_heuristic_chat(): | |
| from backend.intent import heuristic_detect | |
| r = heuristic_detect("hello there") | |
| assert r is not None and r.needs_sandbox is False | |
| def test_intent_heuristic_exec(): | |
| from backend.intent import heuristic_detect | |
| r = heuristic_detect("Create proof.txt with the current unix timestamp and write it") | |
| assert r is not None and r.needs_sandbox is True | |
| def test_intent_code_fence(): | |
| from backend.intent import heuristic_detect | |
| r = heuristic_detect("```python\nprint('hi')\n```") | |
| assert r is not None and r.needs_sandbox is True | |
| def test_extract_code_blocks(): | |
| from backend.agent import extract_code_blocks, pick_runnable | |
| blocks = extract_code_blocks("Here is code:\n```python\nprint(1)\n```\nand shell:\n```bash\nls\n```") | |
| assert len(blocks) == 2 | |
| chosen = pick_runnable(blocks) | |
| assert chosen is not None | |
| assert chosen.language in ("python", "py") | |
| def test_provider_order_engineering(): | |
| from backend.llm_router import provider_order | |
| order = provider_order("Write a python function to sort a list") | |
| assert order[0] == "sambanova" | |
| def test_provider_order_short_chat(): | |
| from backend.llm_router import provider_order | |
| order = provider_order("hi") | |
| assert order[0] == "gemini" | |
| def test_pool_empty_when_no_env(): | |
| from backend.llm_router import KeyPool | |
| p = KeyPool([]) | |
| assert not p | |
| assert p.pick() is None | |
| def test_pool_rotation_and_cooldown(): | |
| from backend.llm_router import KeyPool, MAX_FAILURES_BEFORE_COOLDOWN | |
| p = KeyPool(["a", "b", "c"]) | |
| seen = set() | |
| for _ in range(3): | |
| ks = p.pick() | |
| assert ks is not None | |
| seen.add(ks.key) | |
| assert seen == {"a", "b", "c"} | |
| # Mark one failed enough times to cool down | |
| ks = p.pick() | |
| for _ in range(MAX_FAILURES_BEFORE_COOLDOWN): | |
| KeyPool.mark_failure(ks) | |
| assert ks.cooldown_until > 0 | |