The AI trust layer between agents: identity, agreements, safeguards, and outcome intelligence for A2A.

Viewing files at commit 867f032d6ce0Commit detailsView latest

benchmarks/tau3/test_openclasp_client.py 68 lines · 2.3 KB · python
1
from __future__ import annotations
2
3
import json
4
import threading
5
import unittest
6
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
7
8
from openclasp_client import OpenClaspMcpClient, _split_policy
9
10
11
class Handler(BaseHTTPRequestHandler):
12
    authorization = ""
13
    tool_name = ""
14
15
    def do_POST(self):
16
        body = json.loads(self.rfile.read(int(self.headers["content-length"])))
17
        Handler.authorization = self.headers.get("authorization", "")
18
        Handler.tool_name = body["params"]["name"]
19
        value = {"caseId": "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"}
20
        rpc = {
21
            "jsonrpc": "2.0",
22
            "id": body["id"],
23
            "result": {
24
                "content": [{"type": "text", "text": json.dumps(value)}]
25
            },
26
        }
27
        payload = f"data: {json.dumps(rpc)}\n\n".encode()
28
        self.send_response(200)
29
        self.send_header("content-type", "text/event-stream")
30
        self.send_header("content-length", str(len(payload)))
31
        self.end_headers()
32
        self.wfile.write(payload)
33
34
    def log_message(self, *_args):
35
        return
36
37
38
class OpenClaspClientTests(unittest.TestCase):
39
    @classmethod
40
    def setUpClass(cls):
41
        cls.server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
42
        cls.thread = threading.Thread(target=cls.server.serve_forever, daemon=True)
43
        cls.thread.start()
44
45
    @classmethod
46
    def tearDownClass(cls):
47
        cls.server.shutdown()
48
        cls.thread.join()
49
50
    def test_calls_mcp_with_scoped_token_and_parses_sse(self):
51
        client = OpenClaspMcpClient(
52
            f"http://127.0.0.1:{self.server.server_port}",
53
            "oc_at_abcdefghijklmnop.abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG",
54
        )
55
        value = client.call_tool("openclasp_shield_list_cases")
56
        self.assertEqual(value["caseId"], "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa")
57
        self.assertTrue(Handler.authorization.startswith("Bearer oc_at_"))
58
        self.assertEqual(Handler.tool_name, "openclasp_shield_list_cases")
59
60
    def test_policy_chunks_fit_protocol_limits(self):
61
        chunks = _split_policy(("policy line\n" * 600).strip())
62
        self.assertTrue(chunks)
63
        self.assertTrue(all(len(chunk) <= 1900 for chunk in chunks))
64
        self.assertEqual("\n".join(chunks).replace("\n", ""), ("policy line" * 600))
65
66
67
if __name__ == "__main__":
68
    unittest.main()