-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_protocol.py
More file actions
executable file
·116 lines (85 loc) · 2.84 KB
/
test_protocol.py
File metadata and controls
executable file
·116 lines (85 loc) · 2.84 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
"""
Test the protocol without running full agents.
Shows the basic flow works.
"""
import os
import tempfile
import threading
import time
# Use temp dir for test
TEST_DIR = tempfile.mkdtemp(prefix="protocol_test_")
os.environ["AGENT_PROTOCOL_DIR"] = TEST_DIR
os.environ["AGENT_POLL_INTERVAL"] = "0.1" # Fast polling for test
from protocol import AgentProtocol, ControllerProtocol
print(f"Test directory: {TEST_DIR}")
def test_basic_flow():
"""Test basic request/response flow."""
print("\n=== Test: Basic Flow ===")
agent = AgentProtocol("test_agent")
ctrl = ControllerProtocol()
# Agent sends request in background
result = {"answer": None}
def agent_ask():
result["answer"] = agent.confirm("Deploy to production?", default=False)
t = threading.Thread(target=agent_ask)
t.start()
# Wait for request to appear
time.sleep(0.2)
# Controller responds
pending = ctrl.get_pending_requests()
assert len(pending) == 1, f"Expected 1 pending, got {len(pending)}"
print(f"Pending request: {pending[0]}")
ctrl.respond(pending[0]["id"], "yes")
print("Sent response: yes")
# Wait for agent to receive
t.join(timeout=2)
assert result["answer"] == True, f"Expected True, got {result['answer']}"
print(f"Agent received: {result['answer']}")
print("PASS")
def test_commands():
"""Test command sending."""
print("\n=== Test: Commands ===")
agent = AgentProtocol("test_agent2")
ctrl = ControllerProtocol()
# Send a task
ctrl.send_task("Fix the bug in auth.py")
print("Sent task")
# Agent receives
cmd = agent.check_commands()
assert cmd is not None, "Expected command"
assert cmd["type"] == "task"
assert cmd["task"] == "Fix the bug in auth.py"
print(f"Agent received: {cmd}")
print("PASS")
def test_status():
"""Test status updates."""
print("\n=== Test: Status ===")
agent = AgentProtocol("test_agent3")
ctrl = ControllerProtocol()
agent.set_status("working", task="Writing tests", detail="test_foo.py")
status = ctrl.get_status()
assert status["state"] == "working"
assert status["task"] == "Writing tests"
print(f"Status: {status}")
print("PASS")
def test_logging():
"""Test activity logging."""
print("\n=== Test: Logging ===")
agent = AgentProtocol("test_agent4")
ctrl = ControllerProtocol()
agent.log("info", "Starting task")
agent.log("debug", "Reading file", file="foo.py")
agent.log("error", "File not found")
logs = ctrl.get_log()
assert len(logs) >= 3
print(f"Logs: {len(logs)} entries")
for log in logs[-3:]:
print(f" [{log['level']}] {log['message']}")
print("PASS")
if __name__ == "__main__":
test_basic_flow()
test_commands()
test_status()
test_logging()
print("\n=== All tests passed! ===")