@@ -61,3 +61,100 @@ async def test_async_daemon_not_running():
6161 client = toq .connect_async ("http://127.0.0.1:19999" )
6262 with pytest .raises (toq .ToqError , match = "not running" ):
6363 await client .status ()
64+
65+
66+ # --- Client method tests with mocked HTTP ---
67+
68+
69+ class MockResponse :
70+ def __init__ (self , status_code = 200 , json_data = None , text_data = "ok" ):
71+ self .status_code = status_code
72+ self ._json = json_data or {}
73+ self ._text = text_data
74+
75+ def json (self ):
76+ return self ._json
77+
78+ @property
79+ def text (self ):
80+ return self ._text
81+
82+ def raise_for_status (self ):
83+ if self .status_code >= 400 :
84+ raise Exception (f"HTTP { self .status_code } " )
85+
86+
87+ def test_sync_send (monkeypatch ):
88+ client = toq .connect ("http://localhost:9010" )
89+ resp = MockResponse (json_data = {"id" : "m1" , "status" : "delivered" , "thread_id" : "t1" , "timestamp" : "now" })
90+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
91+ result = client .send ("toq://host/agent" , "hello" )
92+ assert result ["status" ] == "delivered"
93+
94+
95+ def test_sync_peers (monkeypatch ):
96+ client = toq .connect ("http://localhost:9010" )
97+ resp = MockResponse (json_data = {"peers" : [{"public_key" : "k1" , "address" : "a1" , "status" : "connected" , "last_seen" : "now" }]})
98+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
99+ result = client .peers ()
100+ assert len (result ) == 1
101+ assert result [0 ]["public_key" ] == "k1"
102+
103+
104+ def test_sync_block (monkeypatch ):
105+ client = toq .connect ("http://localhost:9010" )
106+ resp = MockResponse ()
107+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
108+ client .block ("ed25519:abc" ) # should not raise
109+
110+
111+ def test_sync_unblock (monkeypatch ):
112+ client = toq .connect ("http://localhost:9010" )
113+ resp = MockResponse ()
114+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
115+ client .unblock ("ed25519:abc" ) # should not raise
116+
117+
118+ def test_sync_approvals (monkeypatch ):
119+ client = toq .connect ("http://localhost:9010" )
120+ resp = MockResponse (json_data = {"approvals" : [{"id" : "k1" , "public_key" : "k1" , "address" : "a1" , "requested_at" : "now" }]})
121+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
122+ result = client .approvals ()
123+ assert len (result ) == 1
124+
125+
126+ def test_sync_approve (monkeypatch ):
127+ client = toq .connect ("http://localhost:9010" )
128+ resp = MockResponse ()
129+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
130+ client .approve ("k1" ) # should not raise
131+
132+
133+ def test_sync_deny (monkeypatch ):
134+ client = toq .connect ("http://localhost:9010" )
135+ resp = MockResponse ()
136+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
137+ client .deny ("k1" ) # should not raise
138+
139+
140+ def test_sync_health (monkeypatch ):
141+ client = toq .connect ("http://localhost:9010" )
142+ resp = MockResponse (text_data = "ok" )
143+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
144+ result = client .health ()
145+ assert result == "ok"
146+
147+
148+ def test_sync_status (monkeypatch ):
149+ client = toq .connect ("http://localhost:9010" )
150+ resp = MockResponse (json_data = {"status" : "running" , "address" : "toq://localhost/agent" })
151+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
152+ result = client .status ()
153+ assert result ["status" ] == "running"
154+
155+
156+ def test_sync_shutdown (monkeypatch ):
157+ client = toq .connect ("http://localhost:9010" )
158+ resp = MockResponse ()
159+ monkeypatch .setattr (client ._http , "request" , lambda * a , ** kw : resp )
160+ client .shutdown () # should not raise
0 commit comments