rusticai-testing 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,417 @@
1
+ import time
2
+ from abc import ABC, abstractmethod
3
+ from typing import List, Tuple, Union
4
+
5
+ import pytest
6
+
7
+ from rustic_ai.core.messaging.client import SimpleClient
8
+ from rustic_ai.core.messaging.core import Client, Message, MessagingInterface
9
+ from rustic_ai.core.messaging.core.message import AgentTag, MessageConstants
10
+ from rustic_ai.core.messaging.core.messaging_config import MessagingConfig
11
+ from rustic_ai.core.utils.gemstone_id import GemstoneGenerator
12
+ from rustic_ai.core.utils.priority import Priority
13
+
14
+
15
+ class BaseTestMessagingABC(ABC):
16
+ @pytest.fixture
17
+ @abstractmethod
18
+ def messaging_config(self) -> MessagingConfig:
19
+ """
20
+ Fixture that returns an instance of the storage implementation being tested.
21
+ This should be overridden in subclasses that test specific storage implementations.
22
+ """
23
+ raise NotImplementedError("This fixture should be overridden in subclasses.")
24
+
25
+ @pytest.fixture
26
+ @abstractmethod
27
+ def namespace(self):
28
+ """
29
+ Fixture that returns the namespace for the messaging interface.
30
+ """
31
+ raise NotImplementedError("This fixture should be overridden in subclasses.")
32
+
33
+ @pytest.fixture
34
+ def generator(self):
35
+ """
36
+ Fixture that returns a GemstoneGenerator instance with a seed of 1.
37
+ """
38
+ return GemstoneGenerator(1)
39
+
40
+ @pytest.fixture
41
+ def messaging(self, namespace, messaging_config):
42
+ """
43
+ Fixture that returns an instance of the Messaging Interface initialized with the given storage.
44
+ """
45
+ mbus = MessagingInterface(namespace, messaging_config)
46
+ yield mbus
47
+ mbus.shutdown()
48
+
49
+ @pytest.fixture
50
+ def alt_messaging(self, namespace, messaging_config):
51
+ """
52
+ Fixture that returns an instance of the Messaging Interface initialized with the given storage.
53
+ """
54
+ mbus = MessagingInterface(f"{namespace}_alt", messaging_config)
55
+ yield mbus
56
+ mbus.shutdown()
57
+
58
+ @pytest.fixture
59
+ def simple_client(self) -> Tuple[Client, List[Message]]:
60
+ messages_received: List[Message] = []
61
+
62
+ def message_handler(message: Message):
63
+ messages_received.append(message)
64
+
65
+ client = SimpleClient("client1", "Simple Client", message_handler)
66
+ return client, messages_received
67
+
68
+ @pytest.fixture
69
+ def simple_client_2(self) -> Tuple[Client, List[Message]]:
70
+ messages_received: List[Message] = []
71
+
72
+ def message_handler(message: Message):
73
+ messages_received.append(message)
74
+
75
+ client = SimpleClient("client2", "Simple Client", message_handler)
76
+ return client, messages_received
77
+
78
+ # A pytest fixure for a simple client that can send messages to the message bus
79
+ @pytest.fixture
80
+ def message_publisher(self) -> Tuple[Client, List[Message]]:
81
+ messages_received: List[Message] = []
82
+
83
+ class TestClient(SimpleClient):
84
+ def __init__(self, client_id: str, name: str):
85
+ super().__init__(client_id, name, self._handle_message)
86
+
87
+ def _handle_message(self, message: Message):
88
+ messages_received.append(message)
89
+
90
+ def send_message(
91
+ self,
92
+ topic: Union[str, List[str]],
93
+ format: str,
94
+ payload: dict,
95
+ message_id,
96
+ ):
97
+ message = Message(
98
+ topics=topic,
99
+ sender=AgentTag(id=self.id, name=self.name),
100
+ format=format,
101
+ payload=payload,
102
+ id_obj=message_id,
103
+ )
104
+ self.publish(message)
105
+ return message
106
+
107
+ return TestClient("sender1", "Simple Publishing Client"), messages_received
108
+
109
+ def test_register_unregister_client(self, messaging, simple_client):
110
+ """
111
+ Test registering and unregistering a client.
112
+ """
113
+ client, _ = simple_client
114
+ messaging.register_client(client)
115
+ assert "client1" in messaging.clients
116
+
117
+ messaging.unregister_client(client)
118
+ assert "client1" not in messaging.clients
119
+
120
+ def test_publish_and_subscribe(self, messaging, message_publisher, simple_client, generator):
121
+ """
122
+ Test publishing a message and subscribing a client to a topic.
123
+ """
124
+ client, messages_received = simple_client
125
+ messaging.register_client(client)
126
+ messaging.subscribe("topic1", client)
127
+
128
+ sender, sender_messages_received = message_publisher
129
+ messaging.register_client(sender)
130
+ messaging.subscribe("topic1", sender)
131
+
132
+ message_id = generator.get_id(Priority.NORMAL)
133
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value"}, message_id)
134
+
135
+ time.sleep(1) # Give the message time to be processed
136
+
137
+ assert len(messages_received) == 1
138
+ assert messages_received[0].payload == {"data": "value"}
139
+ assert messages_received[0].id == message_id.to_int()
140
+
141
+ assert len(sender_messages_received) == 0
142
+
143
+ def test_publish_and_subscribe_multiple_topics(self, messaging, message_publisher, simple_client, generator):
144
+ """
145
+ Test publishing a message and subscribing a client to multiple topics.
146
+ """
147
+ client, messages_received = simple_client
148
+ messaging.register_client(client)
149
+ messaging.subscribe("topic1", client)
150
+ messaging.subscribe("topic2", client)
151
+
152
+ sender, _ = message_publisher
153
+ messaging.register_client(sender)
154
+
155
+ message_id1 = generator.get_id(Priority.NORMAL)
156
+ sender.send_message(
157
+ ["topic1", "topic2"],
158
+ MessageConstants.RAW_JSON_FORMAT,
159
+ {"data": "value1"},
160
+ message_id1,
161
+ )
162
+
163
+ time.sleep(1)
164
+ assert len(messages_received) == 2
165
+
166
+ assert messages_received[0].payload == {"data": "value1"}
167
+ assert messages_received[0].id == message_id1.to_int()
168
+
169
+ assert messages_received[1].payload == {"data": "value1"}
170
+ assert messages_received[1].id == message_id1.to_int()
171
+
172
+ topics = [message.topic_published_to for message in messages_received]
173
+
174
+ assert "topic1" in topics
175
+ assert "topic2" in topics
176
+
177
+ def test_unsubscribe(self, messaging, message_publisher, simple_client, generator):
178
+ """
179
+ Test unsubscribing a client from a topic.
180
+ """
181
+ client, messages_received = simple_client
182
+ messaging.register_client(client)
183
+ messaging.subscribe("topic1", client)
184
+ messaging.unsubscribe("topic1", client)
185
+
186
+ sender, _ = message_publisher
187
+ messaging.register_client(sender)
188
+ messaging.subscribe("topic1", sender)
189
+
190
+ message_id = generator.get_id(Priority.NORMAL)
191
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value"}, message_id)
192
+
193
+ # Since client1 is unsubscribed, it should not receive the message
194
+ assert not messages_received
195
+
196
+ def test_multiple_subscribers_to_topic(
197
+ self, message_publisher, simple_client, simple_client_2, messaging, generator
198
+ ):
199
+ """
200
+ Test that multiple subscribers to a topic all receive the published message.
201
+ """
202
+ client1, messages_received1 = simple_client
203
+ client2, messages_received2 = simple_client_2
204
+
205
+ messaging.register_client(client1)
206
+ messaging.register_client(client2)
207
+
208
+ messaging.subscribe("topic1", client1)
209
+ messaging.subscribe("topic1", client2)
210
+
211
+ sender, _ = message_publisher
212
+ messaging.register_client(sender)
213
+ messaging.subscribe("topic1", sender)
214
+
215
+ message_id = generator.get_id(Priority.NORMAL)
216
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value"}, message_id)
217
+
218
+ time.sleep(1) # Give the message time to be processed
219
+
220
+ assert len(messages_received1) == 1
221
+ assert len(messages_received2) == 1
222
+
223
+ def test_multiple_topics_subscription(self, message_publisher, messaging, simple_client, generator):
224
+ """
225
+ Test that a client subscribed to multiple topics receives messages for each topic.
226
+ """
227
+ client, messages_received = simple_client
228
+ messaging.register_client(client)
229
+ messaging.subscribe("topic1", client)
230
+ messaging.subscribe("topic2", client)
231
+
232
+ sender, _ = message_publisher
233
+ messaging.register_client(sender)
234
+ messaging.subscribe("topic1", sender)
235
+ messaging.subscribe("topic2", sender)
236
+
237
+ message_id1 = generator.get_id(Priority.NORMAL)
238
+ message_id2 = generator.get_id(Priority.NORMAL)
239
+
240
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value1"}, message_id1)
241
+ sender.send_message("topic2", MessageConstants.RAW_JSON_FORMAT, {"data": "value2"}, message_id2)
242
+
243
+ time.sleep(1) # Give the message time to be processed
244
+
245
+ assert len(messages_received) == 2
246
+ assert messages_received[0].payload == {"data": "value1"}
247
+ assert messages_received[1].payload == {"data": "value2"}
248
+
249
+ def test_message_retrieval_since_id(self, message_publisher, messaging, simple_client, generator):
250
+ """
251
+ Test retrieving messages for a topic since a specific message ID.
252
+ """
253
+ client, _ = simple_client
254
+ messaging.register_client(client)
255
+ messaging.subscribe("topic1", client)
256
+
257
+ sender, _ = message_publisher
258
+ messaging.register_client(sender)
259
+ messaging.subscribe("topic1", sender)
260
+
261
+ old_id = generator.get_id(Priority.NORMAL)
262
+ time.sleep(0.001)
263
+ new_id = generator.get_id(Priority.NORMAL)
264
+
265
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "old_value"}, old_id)
266
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "new_value"}, new_id)
267
+
268
+ messages = messaging.get_messages_for_topic_since("topic1", old_id.to_int())
269
+ assert len(messages) == 1
270
+ assert messages[0].payload == {"data": "new_value"}
271
+
272
+ def test_unsubscribe_non_subscribed_client(self, messaging, simple_client):
273
+ """
274
+ Test unsubscribing a client that isn't subscribed to a topic.
275
+ """
276
+ client, _ = simple_client
277
+ messaging.register_client(client)
278
+ # This should not raise an error
279
+ messaging.unsubscribe("topic1", client)
280
+
281
+ def test_register_already_registered_client(self, messaging, simple_client):
282
+ """
283
+ Test registering a client that's already registered.
284
+ """
285
+ client, _ = simple_client
286
+ messaging.register_client(client)
287
+ # Registering the same client again should not raise an error
288
+ messaging.register_client(client)
289
+
290
+ def test_unregister_non_registered_client(self, messaging, simple_client):
291
+ """
292
+ Test unregistering a client that isn't registered.
293
+ """
294
+ client, _ = simple_client
295
+ # This should not raise an error
296
+ messaging.unregister_client(client)
297
+
298
+ # Test get_messages method
299
+ def test_get_messages(self, message_publisher, messaging, simple_client, generator):
300
+ """
301
+ Test retrieving all messages for a topic.
302
+ """
303
+ client, _ = simple_client
304
+ messaging.register_client(client)
305
+ messaging.subscribe("topic1", client)
306
+
307
+ sender, _ = message_publisher
308
+ messaging.register_client(sender)
309
+ messaging.subscribe("topic1", sender)
310
+
311
+ sender.send_message(
312
+ "topic1",
313
+ MessageConstants.RAW_JSON_FORMAT,
314
+ {"data": "value1"},
315
+ generator.get_id(Priority.NORMAL),
316
+ )
317
+ sender.send_message(
318
+ "topic1",
319
+ MessageConstants.RAW_JSON_FORMAT,
320
+ {"data": "value2"},
321
+ generator.get_id(Priority.NORMAL),
322
+ )
323
+
324
+ messages = messaging.get_messages("topic1")
325
+ assert len(messages) == 2
326
+ assert messages[0].payload == {"data": "value1"}
327
+ assert messages[1].payload == {"data": "value2"}
328
+
329
+ messages = messaging.get_messages("topic1")
330
+ assert len(messages) == 2
331
+ assert messages[0].payload == {"data": "value1"}
332
+ assert messages[1].payload == {"data": "value2"}
333
+
334
+ # Test get_next_message_for_topic method
335
+ def test_get_next_message_for_topic(self, message_publisher, messaging, simple_client, generator):
336
+ """
337
+ Test retrieving the next message for a topic.
338
+ """
339
+ client, _ = simple_client
340
+ messaging.register_client(client)
341
+ messaging.subscribe("topic1", client)
342
+
343
+ sender, _ = message_publisher
344
+ messaging.register_client(sender)
345
+ messaging.subscribe("topic1", sender)
346
+
347
+ message_id1 = generator.get_id(Priority.NORMAL)
348
+ time.sleep(0.001)
349
+ message_id2 = generator.get_id(Priority.NORMAL)
350
+
351
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value1"}, message_id1)
352
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value2"}, message_id2)
353
+
354
+ message = messaging.get_next_message_for_topic("topic1", 0)
355
+ assert message.payload == {"data": "value1"}
356
+
357
+ message = messaging.get_next_message_for_topic("topic1", message.id)
358
+ assert message.payload == {"data": "value2"}
359
+
360
+ # Test get_next_message_for_client method
361
+ def test_get_next_message_for_client(self, message_publisher, messaging, simple_client, generator):
362
+ """
363
+ Test retrieving the next message for a client.
364
+ """
365
+ client, _ = simple_client
366
+ messaging.register_client(client)
367
+ messaging.subscribe("topic1", client)
368
+
369
+ sender, _ = message_publisher
370
+ messaging.register_client(sender)
371
+ messaging.subscribe("topic1", sender)
372
+
373
+ message_id1 = generator.get_id(Priority.NORMAL)
374
+ time.sleep(0.001)
375
+ message_id2 = generator.get_id(Priority.NORMAL)
376
+
377
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value1"}, message_id1)
378
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value2"}, message_id2)
379
+
380
+ message = messaging.get_next_message_for_client(client.id, 0)
381
+ assert message.payload == {"data": "value1"}
382
+
383
+ message = messaging.get_next_message_for_client(client.id, message.id)
384
+ assert message.payload == {"data": "value2"}
385
+
386
+ # Test messaging isolation between different namespaces
387
+ def test_messaging_isolation(
388
+ self,
389
+ messaging,
390
+ alt_messaging,
391
+ message_publisher,
392
+ simple_client,
393
+ simple_client_2,
394
+ generator,
395
+ ):
396
+ """
397
+ Test that messages are isolated between different messaging interfaces with different namespaces.
398
+ """
399
+ client, messages_received = simple_client
400
+ messaging.register_client(client)
401
+ messaging.subscribe("topic1", client)
402
+
403
+ sender, _ = message_publisher
404
+ messaging.register_client(sender)
405
+ messaging.subscribe("topic1", sender)
406
+
407
+ alt_client, alt_messages_received = simple_client_2
408
+ alt_messaging.register_client(alt_client)
409
+ alt_messaging.subscribe("topic1", alt_client)
410
+
411
+ message_id = generator.get_id(Priority.NORMAL)
412
+ sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value"}, message_id)
413
+
414
+ time.sleep(1) # Give the message time to be processed
415
+
416
+ assert len(messages_received) == 1
417
+ assert len(alt_messages_received) == 0
@@ -0,0 +1,37 @@
1
+ Metadata-Version: 2.1
2
+ Name: rusticai-testing
3
+ Version: 0.0.1
4
+ Summary: Test Utilities for Rustic AI Framework
5
+ Home-page: https://www.rustic.ai/
6
+ License: Apache-2.0
7
+ Author: Dragonscale Industries Inc.
8
+ Author-email: dev@dragonscale.ai
9
+ Requires-Python: >=3.12,<4.0
10
+ Classifier: License :: OSI Approved :: Apache Software License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Requires-Dist: pytest (>=8.3.5,<9.0.0)
15
+ Requires-Dist: rusticai-core (>=0.0.5,<0.1.0)
16
+ Project-URL: Repository, https://github.com/rustic-ai/python-framework
17
+ Project-URL: Rustic AI Core, https://pypi.org/project/rusticai-core/
18
+ Description-Content-Type: text/markdown
19
+
20
+ # Rustic AI Testing
21
+
22
+ Rustic AI Testing is a collection of test utilities that can be leveraged to verify integration with Rustic AI Framework.
23
+ **This should only be used for testing purposes and should not be used in production.**
24
+
25
+ ## Installing
26
+
27
+ ```shell
28
+ pip install rusticai-testing
29
+ ```
30
+
31
+ ## Building from Source
32
+
33
+ ```shell
34
+ poetry install --with dev
35
+ poetry build
36
+ ```
37
+
@@ -0,0 +1,16 @@
1
+ rustic_ai/testing/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ rustic_ai/testing/agent_ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ rustic_ai/testing/agent_ext/base_test_kvstore.py,sha256=fSm2bEL2mmabcuBGDroZ53iIosJvvF9SXnQh0in3TWU,8425
4
+ rustic_ai/testing/agents/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ rustic_ai/testing/agents/sample_agents.py,sha256=oO8_M7SoMLju8ATxwEJfHDumjSBPXZiyYOCR5GHEAK0,2343
6
+ rustic_ai/testing/agents/simple_agent.py,sha256=-dtXO2rsZPFijII7x1IwwVJ6PDG8kNrhoKHHAyuvJv4,1262
7
+ rustic_ai/testing/execution/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ rustic_ai/testing/execution/base_test_integration.py,sha256=KrfhExZYUvA6oW8gR7s6CI7M9XgnTOBs4msyQtvbFv8,5614
9
+ rustic_ai/testing/execution/integration_agents.py,sha256=ocAUhF24z1fvHGUXUKVlxQPaXsgHWhNlt7S0x1Ua0Ds,2441
10
+ rustic_ai/testing/helpers.py,sha256=SKoi4gz17JB60cf8OUcGdhvLj937fi89YsUb_clmEds,3343
11
+ rustic_ai/testing/messaging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ rustic_ai/testing/messaging/base_test_backend.py,sha256=mBgSm_UXWQ6N1Z-_XX5ZUZ1CnlCRqX0vWSFV1e2Lo-Y,13308
13
+ rustic_ai/testing/messaging/base_test_messaging.py,sha256=JVnSC2rplEu2MQ4ln3o6Mriyq6B9X_-EhjsF688d0LE,15576
14
+ rusticai_testing-0.0.1.dist-info/METADATA,sha256=92bAL90Dwq-h1KRacm7rING4smCVZ7nXdSUMD55rJLM,1108
15
+ rusticai_testing-0.0.1.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
16
+ rusticai_testing-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 1.9.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any