File size: 3,076 Bytes
948a05a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
"""Test harness: chat, tools, skills, stats."""

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from splitbit_llm.harness.harness import SplitBitHarness
from splitbit_llm.harness.tools import ToolRegistry, get_default_tools, tool_loop, parse_tool_calls


def test_harness_chat():
    """Test harness chat functionality."""
    harness = SplitBitHarness()
    result = harness.chat("Hello there", channel="cli")

    assert "response" in result, "No response in result"
    assert "elapsed_s" in result, "No elapsed time"
    assert "stats" in result, "No stats"
    print(f"  Response: {result['response'][:60]}")
    print(f"  Elapsed: {result['elapsed_s']}s")


def test_harness_stats():
    """Test harness stats aggregation."""
    harness = SplitBitHarness()
    stats = harness.get_stats()

    assert "model" in stats, "No model stats"
    assert "skills" in stats, "No skills stats"
    assert "recursive_links" in stats, "No link stats"
    assert "auto_sizer" in stats, "No auto_sizer stats"
    print(f"  Model params: {stats['model']['param_count']:,}")
    print(f"  Tier: {stats['auto_sizer']['tier']}")


def test_tool_registry():
    """Test tool registry and execution."""
    registry = ToolRegistry()
    for tool in get_default_tools():
        registry.register(tool)

    assert len(registry.list_tools()) == 9, f"Wrong tool count: {len(registry.list_tools())}"
    print(f"  Tools: {len(registry.list_tools())}")

    # Test calculate tool
    result = registry.execute("calculate", "2 + 2")
    assert result.success, f"Calculate failed: {result.error}"
    assert "4" in result.output, f"Wrong result: {result.output}"
    print(f"  calculate(2+2) = {result.output}")


def test_tool_loop():
    """Test tool execution loop."""
    registry = ToolRegistry()
    for tool in get_default_tools():
        registry.register(tool)

    text = "Let me calculate: [TOOL: calculate(3 * 7)]"
    final_text, results = tool_loop(text, registry)

    assert len(results) == 1, f"Expected 1 result, got {len(results)}"
    assert results[0].success, "Tool execution failed"
    assert "21" in results[0].output, f"Wrong output: {results[0].output}"
    print(f"  Tool loop: {len(results)} calls, output: {results[0].output}")


def test_parse_tool_calls():
    """Test parsing tool calls from text."""
    text = "I will [TOOL: calculate(1 + 1)] and then [TOOL: calculate(2 + 2)]"
    calls = parse_tool_calls(text)
    assert len(calls) == 2, f"Expected 2 calls, got {len(calls)}"
    assert calls[0][0] == "calculate", f"Wrong tool name: {calls[0][0]}"
    print(f"  Parsed {len(calls)} tool calls")


if __name__ == "__main__":
    print("Running harness tests...")
    test_harness_chat()
    print("  ✓ test_harness_chat")
    test_harness_stats()
    print("  ✓ test_harness_stats")
    test_tool_registry()
    print("  ✓ test_tool_registry")
    test_tool_loop()
    print("  ✓ test_tool_loop")
    test_parse_tool_calls()
    print("  ✓ test_parse_tool_calls")
    print("\nAll harness tests passed!")