File size: 2,388 Bytes
46258b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
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