uagent-testkit 0.1.0__tar.gz → 0.1.2__tar.gz

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.
Files changed (34) hide show
  1. uagent_testkit-0.1.2/.github/workflows/ci.yml +33 -0
  2. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/.gitignore +2 -0
  3. uagent_testkit-0.1.2/CHANGELOG.md +89 -0
  4. uagent_testkit-0.1.2/LICENSE +202 -0
  5. uagent_testkit-0.1.2/PKG-INFO +348 -0
  6. uagent_testkit-0.1.0/PKG-INFO → uagent_testkit-0.1.2/README.md +101 -40
  7. uagent_testkit-0.1.2/pyproject.toml +58 -0
  8. uagent_testkit-0.1.2/tests/test_chain.py +673 -0
  9. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/tests/test_harness.py +130 -4
  10. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/tests/test_network.py +28 -0
  11. uagent_testkit-0.1.2/tests/test_plugin.py +127 -0
  12. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/uagent_testkit/__init__.py +11 -1
  13. uagent_testkit-0.1.2/uagent_testkit/_loop.py +83 -0
  14. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/uagent_testkit/_recording.py +21 -3
  15. uagent_testkit-0.1.2/uagent_testkit/_refusals.py +79 -0
  16. uagent_testkit-0.1.2/uagent_testkit/chain.py +495 -0
  17. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/uagent_testkit/harness.py +25 -10
  18. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/uagent_testkit/network.py +21 -12
  19. uagent_testkit-0.1.2/uagent_testkit/plugin.py +107 -0
  20. uagent_testkit-0.1.0/.github/workflows/ci.yml +0 -21
  21. uagent_testkit-0.1.0/LICENSE +0 -15
  22. uagent_testkit-0.1.0/README.md +0 -228
  23. uagent_testkit-0.1.0/pyproject.toml +0 -49
  24. uagent_testkit-0.1.0/uagent_testkit/_loop.py +0 -30
  25. uagent_testkit-0.1.0/uagent_testkit/plugin.py +0 -57
  26. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/examples/__init__.py +0 -0
  27. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/examples/quote_agents.py +0 -0
  28. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/examples/test_quote_agents.py +0 -0
  29. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/tests/__init__.py +0 -0
  30. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/tests/conftest.py +0 -0
  31. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/tests/test_chat_protocol.py +0 -0
  32. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/tests/test_no_network.py +0 -0
  33. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/tests/test_verification.py +0 -0
  34. {uagent_testkit-0.1.0 → uagent_testkit-0.1.2}/uagent_testkit/_storage.py +0 -0
@@ -0,0 +1,33 @@
1
+ name: tests
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ python-version: ["3.10", "3.11", "3.12", "3.13"]
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - uses: actions/setup-python@v5
18
+ with:
19
+ python-version: ${{ matrix.python-version }}
20
+ - run: pip install -e ".[test]"
21
+ - run: pytest -q
22
+
23
+ oldest-uagents:
24
+ # The floor pyproject.toml declares, so a wrong floor fails here rather than
25
+ # for a user pinned to it. uagents 0.23.0 requires uagents-core>=0.4.0.
26
+ runs-on: ubuntu-latest
27
+ steps:
28
+ - uses: actions/checkout@v4
29
+ - uses: actions/setup-python@v5
30
+ with:
31
+ python-version: "3.12"
32
+ - run: pip install -e ".[test]" "uagents==0.23.0" "uagents-core==0.4.0"
33
+ - run: pytest -q
@@ -10,3 +10,5 @@ agent_storage.json
10
10
  grant-application.md
11
11
  .env
12
12
  .env.*
13
+ .mcp.json
14
+ PASTE-SHEET.md
@@ -0,0 +1,89 @@
1
+ # Changelog
2
+
3
+ ## 0.1.2 (unreleased)
4
+
5
+ Fixes from a bug hunt on 2026-10-05, run against uagents 0.26.0 (released 2026-10-02)
6
+ and web3 7.16 and 8.0. Each code fix comes with a test that fails on 0.1.1.
7
+
8
+ - **ChainDouble works with web3.py.** 0.1.x said it did and never had: every
9
+ `Web3.HTTPProvider` call failed with `TypeError: '_Response' object does not support
10
+ the context manager protocol`, every `AsyncHTTPProvider` call with
11
+ `AttributeError: '_Response' object has no attribute 'read'`, and httpx's
12
+ `raise_for_status()` raised `RuntimeError`. The double now answers with real
13
+ `requests` and `httpx` responses and a complete aiohttp one, and the suite drives both
14
+ web3.py providers.
15
+ - **No unclosed event loops.** The pytest plugin loads into every suite in an
16
+ environment where the package is installed. It made a new event loop for every test
17
+ and closed none, so any suite with async tests run under `-W error` failed on
18
+ `ResourceWarning: unclosed event loop`, even one that never imports uAgents. It now
19
+ keeps one loop for the session and closes it at the end.
20
+ - **Agents build in any fixture.** A module- or session-scoped fixture that builds an
21
+ `Agent` failed with `RuntimeError: There is no current event loop` when it was set up
22
+ after an async test (pytest-asyncio 1.x unsets its loop after each one), and an agent
23
+ built at import time in a `conftest.py` warned, which `-W error` turns into a failed
24
+ conftest. The plugin now puts a loop in place before every test's fixtures and before
25
+ conftests load.
26
+ - **A refusal the handler catches still fails the delivery.** Agents wrap RPC reads in
27
+ `try/except`, which turned a missing stub back into a silent pass. `deliver()`,
28
+ `tick()`, `startup()` and `shutdown()` now re-raise any refusal the handler's own work
29
+ made, in the handler, in tasks it starts or in a thread; `chain.refusals` lists them
30
+ all. A refusal is matched to its delivery by context, so the agent's own manifest
31
+ upload and a delivery running alongside are not blamed on the handler.
32
+ - **ChainDouble blocks what it says it blocks.** Eight request paths reached the
33
+ network: requests' `Session.send()` and adapters, httpx's `Client.stream()`,
34
+ `Client.send()` and `AsyncClient.send()`, and urllib. requests, httpx, aiohttp and
35
+ urllib are now patched where every request through them passes. A JSON body that is
36
+ not JSON-RPC, such as an LLM or webhook call, raised
37
+ `UnstubbedCall: handler called None` instead of `NetworkCallBlocked`. `http.client`,
38
+ `urllib3` used directly and raw sockets are not covered, and the README says so.
39
+ - **Dependency floor.** `uagents>=0.22` was wrong: on every 0.22.x the first
40
+ `deliver()` raises `TypeError`, because `AgentRepresentation` takes no `prefix` before
41
+ 0.23. The floor is now `uagents>=0.23`, and CI tests it.
42
+ - **Reply contracts match the framework.** An `ErrorMessage` answer honours any
43
+ `replies=` declaration, as the framework's `validate_replies()` allows; the harness
44
+ used to fail it. And the harness now runs `validate_replies()` after each handler, as
45
+ `Agent._handle_message` does, so a missing reply logs the same error as in production
46
+ and `assert_no_errors()` fails on it.
47
+ - **AgentNetwork keeps one session per conversation.** Each hop got a fresh
48
+ `ctx.session`; in production a reply carries the session of the message it answers.
49
+ `send()` also takes `session=`.
50
+ - **Sending to an agent outside the network raises `KeyError`.** It returned an empty
51
+ transcript that passed `assert_all_delivered()`.
52
+ - **Checksummed calldata.** A mixed-case holder or selector in `eth_call` data raised
53
+ `UnstubbedCall` ("which the test never set") for a balance the test had set.
54
+ - **`raise_errors=False` no longer swallows cancellation.** A timeout around
55
+ `deliver()` returned a `Delivery` holding `CancelledError` instead of timing out.
56
+ - **`ctx.send_and_receive()` fails loudly.** It returned `None` after no wait at all;
57
+ it now raises `NotImplementedError` and says why, and fails the delivery even when the
58
+ handler catches it.
59
+ - **Docs.** The README's "Asserting on failure" example raised `IndexError`.
60
+ `h.tick()` also runs `@on_schedule` handlers on uagents 0.26+. `include(proto,
61
+ publish_manifest=True)` uploads a manifest to Agentverse whenever the agent's loop
62
+ runs, harness or not. `test_storage_does_not_touch_disk` could not see the file it
63
+ guards against.
64
+ - `LICENSE` holds the full Apache-2.0 text. It held only the short notice, and GitHub
65
+ listed the licence as "Other".
66
+
67
+ ### Behaviour changes that can fail a test that passed on 0.1.1
68
+
69
+ In each case the old pass was hiding something:
70
+
71
+ - A handler that catches an `UnstubbedCall` or `NetworkCallBlocked` now fails its
72
+ delivery. It passed on data the test never set up.
73
+ - `assert_no_errors()` fails when a handler sends none of its declared replies, which
74
+ production logs as an error.
75
+ - `AgentNetwork.send()` to an agent never added raises `KeyError`. Nothing was
76
+ delivered, and the empty transcript passed `assert_all_delivered()`.
77
+ - `ctx.send_and_receive()` raises `NotImplementedError`, caught or not. The handler got
78
+ `None` even when the peer answered, so only its no-reply path ever ran.
79
+ - Requests through urllib, httpx's `stream()`/`send()` and requests' `Session.send()`
80
+ are answered or refused under `ChainDouble.install()`. They reached the network.
81
+
82
+ ## 0.1.1 (2026-07-28)
83
+
84
+ - `ChainDouble`, for agents that read BNB Chain.
85
+
86
+ ## 0.1.0 (2026-07-28)
87
+
88
+ - First release: the single-agent harness, `AgentNetwork`, Protocol-based agents and
89
+ the shared chat protocol, and the pytest fixtures.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,348 @@
1
+ Metadata-Version: 2.5
2
+ Name: uagent-testkit
3
+ Version: 0.1.2
4
+ Summary: Test Fetch.ai uAgents offline — handlers, agent-to-agent flows, and BNB Chain reads.
5
+ Project-URL: Homepage, https://github.com/pgalyen1987/uagent-testkit
6
+ Project-URL: Repository, https://github.com/pgalyen1987/uagent-testkit
7
+ Project-URL: Issues, https://github.com/pgalyen1987/uagent-testkit/issues
8
+ Project-URL: Changelog, https://github.com/pgalyen1987/uagent-testkit/blob/main/CHANGELOG.md
9
+ Author-email: Patrick Galyen <pgalyen87@gmail.com>
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: agent-launch,agentverse,asi,bnb-chain,bsc,fetchai,pytest,testing,uagents,web3
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Framework :: Pytest
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: Apache Software License
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Software Development :: Testing
22
+ Requires-Python: >=3.10
23
+ Requires-Dist: uagents>=0.23
24
+ Provides-Extra: test
25
+ Requires-Dist: aiohttp>=3.9; extra == 'test'
26
+ Requires-Dist: httpx>=0.27; extra == 'test'
27
+ Requires-Dist: pytest-asyncio>=0.24; extra == 'test'
28
+ Requires-Dist: pytest>=8.0; extra == 'test'
29
+ Requires-Dist: requests>=2.31; extra == 'test'
30
+ Requires-Dist: web3>=7; extra == 'test'
31
+ Description-Content-Type: text/markdown
32
+
33
+ # uagent-testkit
34
+
35
+ [![PyPI](https://img.shields.io/pypi/v/uagent-testkit.svg)](https://pypi.org/project/uagent-testkit/)
36
+ [![CI](https://github.com/pgalyen1987/uagent-testkit/actions/workflows/ci.yml/badge.svg)](https://github.com/pgalyen1987/uagent-testkit/actions/workflows/ci.yml)
37
+
38
+ Test [Fetch.ai uAgents](https://github.com/fetchai/uAgents) without a network, a wallet, or a `sleep()`.
39
+
40
+ ```python
41
+ from uagent_testkit import harness
42
+
43
+ async def test_echo():
44
+ h = harness(my_agent)
45
+ result = await h.deliver(Ping(text="hi"))
46
+
47
+ assert result.reply(Pong).text == "pong:hi"
48
+ assert h.storage["seen"] == 1
49
+ ```
50
+
51
+ ## Why
52
+
53
+ The uAgents framework has no testing story. To exercise a handler today you generally
54
+ have to stand the agent up for real — which means Almanac registration, endpoint
55
+ resolution, a funded wallet on some paths, and `asyncio.sleep()` calls sprinkled through
56
+ the test to wait for messages that may never arrive. That is slow, flaky, and awkward to
57
+ run in CI.
58
+
59
+ There is also a subtler problem. `Agent._handle_message` catches every exception a
60
+ handler raises and writes it to a log. That is the right call at runtime — an agent
61
+ processes messages from peers it does not control, and one malformed message must not
62
+ take the process down. But the same containment applies under pytest, and there is no
63
+ strict mode to turn it off, so a handler that crashes on every message still looks
64
+ like a green test.
65
+
66
+ `uagent-testkit` routes messages straight to the registered handler with a context that
67
+ records instead of transmitting. No sockets, no registration, no waiting.
68
+
69
+ - **Handler exceptions are raised** rather than contained into a log.
70
+ - **Storage is in-memory**, so state doesn't leak between tests via the JSON file the
71
+ stock `KeyValueStore` writes to your working directory.
72
+ - **Reply contracts are checked** — if `@on_message(replies=Pong)` sends neither a
73
+ `Pong` nor an `ErrorMessage`, `assert_reply_contract()` fails, and the error the
74
+ framework would log lands in `result.logs`, where `assert_no_errors()` sees it.
75
+ - **Interval and lifecycle handlers run on demand**, so an `@on_interval(period=3600)`
76
+ is testable without waiting an hour.
77
+ - **Multi-agent conversations replay deterministically**, with a loop guard instead of
78
+ a hung test.
79
+ - **Agents that read BNB Chain are testable offline** — `ChainDouble` answers BEP-20
80
+ and balance queries from state your test sets, and blocks any other request made
81
+ through requests, httpx, aiohttp or urllib.
82
+
83
+ ## Install
84
+
85
+ ```bash
86
+ pip install uagent-testkit
87
+ ```
88
+
89
+ Requires Python 3.10+ and `uagents>=0.23`. The pytest fixtures register automatically.
90
+
91
+ ## Testing one agent
92
+
93
+ ```python
94
+ from uagents import Agent, Context, Model
95
+ from uagent_testkit import harness
96
+
97
+ class Ping(Model):
98
+ text: str
99
+
100
+ class Pong(Model):
101
+ text: str
102
+
103
+ agent = Agent(name="echo", seed="echo seed")
104
+
105
+ @agent.on_message(model=Ping, replies=Pong)
106
+ async def on_ping(ctx: Context, sender: str, msg: Ping):
107
+ ctx.storage.set("seen", (ctx.storage.get("seen") or 0) + 1)
108
+ await ctx.send(sender, Pong(text=f"pong:{msg.text}"))
109
+ ```
110
+
111
+ ```python
112
+ async def test_ping():
113
+ h = harness(agent)
114
+ result = await h.deliver(Ping(text="hi"), sender="agent1qexample")
115
+
116
+ result.assert_replied_with(Pong)
117
+ result.assert_reply_contract() # honoured its replies= declaration
118
+ assert result.reply(Pong).text == "pong:hi"
119
+ assert result.sent[0].destination == "agent1qexample"
120
+ assert h.storage["seen"] == 1
121
+ ```
122
+
123
+ ### Asserting on failure
124
+
125
+ ```python
126
+ async def test_bad_input_is_handled():
127
+ result = await h.deliver(Ping(text=""), raise_errors=False)
128
+ assert isinstance(result.error, ValueError)
129
+ assert "empty" in str(result.error)
130
+ ```
131
+
132
+ By default `deliver()` re-raises whatever the handler raised, so a broken handler fails
133
+ the test loudly. Pass `raise_errors=False` when the failure is the thing under test.
134
+ Cancellation and `KeyboardInterrupt` are not handler failures, so they always propagate.
135
+
136
+ ### Sender verification
137
+
138
+ Handlers without `allow_unverified=True` are only reachable from a verified agent
139
+ address. The harness enforces that, so a test can't pass for a flow production
140
+ rejects:
141
+
142
+ ```python
143
+ from uagent_testkit import UnverifiedSender, user_sender
144
+
145
+ async def test_strict_handler_refuses_users():
146
+ with pytest.raises(UnverifiedSender):
147
+ await h.deliver(Ping(text="hi"), sender=user_sender())
148
+ ```
149
+
150
+ ### Intervals and lifecycle
151
+
152
+ ```python
153
+ await h.startup() # runs @on_event("startup") handlers
154
+ result = await h.tick() # runs @on_interval handlers once, ignoring period
155
+ # (and @on_schedule ones, on uagents 0.26+)
156
+ result = await h.tick(only="heartbeat") # just one, by function name
157
+ await h.shutdown()
158
+ ```
159
+
160
+ ## Testing agents against each other
161
+
162
+ ```python
163
+ from uagent_testkit import AgentNetwork
164
+
165
+ async def test_conversation():
166
+ net = AgentNetwork(alice, bob)
167
+ transcript = await net.send(Ping(text="hello"), to=bob, sender=alice)
168
+
169
+ transcript.assert_delivered(Pong, to=alice.address)
170
+ assert transcript.of(Pong)[0].text == "pong:hello"
171
+ assert net.harness(alice).storage["last_pong"] == "pong:hello"
172
+ ```
173
+
174
+ Every reply addressed to another agent in the network is delivered in turn until the
175
+ conversation goes quiet. As on a live network, a reply carries the session of the message
176
+ it answers, so the whole conversation runs in one `ctx.session` (pass `session=` to choose
177
+ it). Sending to an agent you never added raises `KeyError`. Messages a handler sends to
178
+ addresses outside the network are collected on `transcript.undelivered` rather than
179
+ silently dropped:
180
+
181
+ ```python
182
+ transcript.assert_all_delivered() # fails if anything went to an unknown address
183
+ ```
184
+
185
+ Agents that answer each other forever raise `ConversationTooLong` after `max_rounds`
186
+ deliveries instead of hanging the suite. This catches echo loops, which are easy to
187
+ write and expensive to discover in production:
188
+
189
+ ```python
190
+ # a keyword responder whose answer contains its own trigger word
191
+ if "menu" in msg.text().lower():
192
+ await ctx.send(sender, chat("here is the menu")) # -> contains "menu"
193
+ ```
194
+
195
+ Two agents like that will talk to each other indefinitely. On a live network that is
196
+ two agents spamming each other; here it fails in milliseconds with the transcript
197
+ so far attached.
198
+
199
+ ## Protocols and the chat protocol
200
+
201
+ Agents assembled from `Protocol` objects work the same way, including the shared chat
202
+ protocol from `uagents_core` that Agentverse and Agent Launch agents use:
203
+
204
+ ```python
205
+ chat = Protocol(spec=chat_protocol_spec)
206
+
207
+ @chat.on_message(ChatMessage)
208
+ async def on_chat(ctx: Context, sender: str, msg: ChatMessage):
209
+ ...
210
+
211
+ agent.include(chat)
212
+ ```
213
+
214
+ ```python
215
+ async def test_chat():
216
+ h = harness(agent)
217
+ result = await h.deliver(
218
+ ChatMessage(
219
+ timestamp=datetime.now(timezone.utc),
220
+ msg_id=uuid4(),
221
+ content=[TextContent(type="text", text="show me the menu")],
222
+ )
223
+ )
224
+ assert result.replies(ChatAcknowledgement)
225
+ assert result.replies(ChatMessage)[0].text() == "here is what we have"
226
+ ```
227
+
228
+ ## Agents that read BNB Chain
229
+
230
+ The harness intercepts `ctx.send`, which covers agent-to-agent messaging. It does not
231
+ cover a handler that reaches out to an RPC endpoint — an agent reading a BEP-20 balance
232
+ still talks to the network during a test. `ChainDouble` closes that gap:
233
+
234
+ ```python
235
+ from uagent_testkit import ChainDouble, FET_BSC, harness
236
+
237
+ async def test_treasury_agent():
238
+ chain = ChainDouble()
239
+ chain.add_token(FET_BSC, symbol="FET", decimals=18,
240
+ balances={wallet: 5_500_000_000_000_000_000})
241
+
242
+ h = harness(treasury_agent)
243
+ with chain.install():
244
+ result = await h.deliver(CheckBalance(wallet=wallet))
245
+
246
+ assert result.reply(BalanceReport).amount == 5.5
247
+ chain.assert_called("eth_call")
248
+ ```
249
+
250
+ It patches `requests`, `httpx` and `aiohttp` where they are importable, plus `urllib`,
251
+ at the call every request through them passes, so it works whether the agent uses
252
+ web3.py or raw HTTP; the test suite drives web3.py's `HTTPProvider` and
253
+ `AsyncHTTPProvider` through it. It understands web3.py's JSON-encoded `data=` body as
254
+ well as `json=`. It does not cover `http.client` or `urllib3` used directly, or sockets
255
+ a handler opens itself.
256
+
257
+ Supported: `eth_call` (BEP-20 `balanceOf`, `symbol`, `name`, `decimals`, `totalSupply`),
258
+ `eth_getBalance`, `eth_getCode`, `eth_blockNumber`, `eth_chainId`, `net_version`,
259
+ `eth_getTransactionCount`, batched requests. Two of these answer without being told:
260
+ `eth_getCode` returns `0x` (no code) for any address not registered with `add_token()`,
261
+ and `eth_getTransactionCount` always returns 0.
262
+
263
+ Two deliberate refusals, both of which turn a silent pass into a loud failure:
264
+
265
+ ```python
266
+ # reading state the test never set up
267
+ with pytest.raises(UnstubbedCall):
268
+ ... # rather than quietly returning zero
269
+
270
+ # any other HTTP from inside a handler: a price API, an LLM, a webhook
271
+ with pytest.raises(NetworkCallBlocked):
272
+ ... # rather than hitting a real API in CI
273
+ ```
274
+
275
+ Agents wrap RPC reads in `try/except` so one bad call can't crash them, and that habit
276
+ would swallow a refusal and turn it back into a silent pass. So `deliver()` (and
277
+ `tick()`, `startup()`, `shutdown()`) re-raises any refusal the handler's own work made,
278
+ caught or not: in the handler, in the tasks it starts, and in `asyncio.to_thread()`.
279
+ Work the handler did not start is not charged to it, such as a delivery running
280
+ alongside it or the manifest upload that `include(proto, publish_manifest=True)` queues.
281
+ A refusal from a thread that carries no context (`loop.run_in_executor()`, a plain
282
+ `Thread`) fails every delivery running at that moment. `chain.refusals` keeps every
283
+ refusal either way.
284
+
285
+ There's also a `bsc` fixture that gives you an installed double directly.
286
+
287
+ ## pytest fixtures
288
+
289
+ Installing the package registers two factory fixtures:
290
+
291
+ ```python
292
+ def test_with_fixture(agent_harness, agent_network):
293
+ h = agent_harness(my_agent)
294
+ net = agent_network(alice, bob)
295
+ ```
296
+
297
+ ## API
298
+
299
+ | | |
300
+ |---|---|
301
+ | `harness(agent)` | Wrap an agent. Swaps in in-memory storage and merges the internal protocol. |
302
+ | `h.deliver(msg, sender=…, raise_errors=True)` | Route a message to its handler. Returns `Delivery`. |
303
+ | `h.tick(only=None)` | Run `@on_interval` (and `@on_schedule`) handlers once. |
304
+ | `h.startup()` / `h.shutdown()` | Run `@on_event` handlers. |
305
+ | `h.storage` | In-memory store, subscriptable. |
306
+ | `h.handles(Model)` | Whether a handler is registered for a type. |
307
+ | `Delivery.sent` | List of `Sent(destination, schema_digest, body)`. |
308
+ | `Delivery.reply(Model)` | The single reply of that type; raises otherwise. |
309
+ | `Delivery.replies(Model, to=…)` | Replies of that type to the sender; `to=ANY` for any destination. |
310
+ | `user_sender()` | A user (non-agent) address, for `allow_unverified` paths. |
311
+ | `Delivery.error` / `.logs` | Handler exception (or a refusal it caught) and captured log records. |
312
+ | `Delivery.assert_replied_with` / `assert_silent` / `assert_no_errors` / `assert_reply_contract` | Chainable assertions. |
313
+ | `AgentNetwork(*agents)` | Multi-agent network. |
314
+ | `net.send(msg, to=…, sender=…, max_rounds=20, session=None)` | Play out a conversation in one session. Returns `Transcript`. |
315
+ | `Transcript.of(Model)` / `.to(addr)` / `.undelivered` | Inspect what was exchanged. |
316
+
317
+ ## Scope
318
+
319
+ This exercises your handler logic: routing, replies, storage, protocol contracts, and
320
+ inter-agent flow. It deliberately does not simulate Almanac resolution, envelope
321
+ signing, or on-chain settlement — those are integration concerns and mocking them would
322
+ give you false confidence. Keep a thin integration test on a real testnet for those, and
323
+ use this for everything else.
324
+
325
+ `ctx.send_and_receive()` is not supported. Sends are recorded rather than delivered, so
326
+ no answer can reach the handler that is waiting for one; the harness raises
327
+ `NotImplementedError` rather than hand it `None`, and a handler that catches it still
328
+ fails its delivery.
329
+
330
+ The harness can't stop work an agent schedules on its own event loop.
331
+ `include(proto, publish_manifest=True)` queues an upload of the protocol's manifest to
332
+ Agentverse, and it runs whenever that loop runs, with or without the harness: for an
333
+ agent built inside an async test, as soon as the test yields to the loop. Pass
334
+ `publish_manifest=False` when you build agents for tests.
335
+
336
+ The [changelog](https://github.com/pgalyen1987/uagent-testkit/blob/main/CHANGELOG.md)
337
+ lists what each release fixed, and which fixes can fail a test that used to pass.
338
+
339
+ ## Running the tests
340
+
341
+ ```bash
342
+ python -m venv .venv && .venv/bin/pip install -e ".[test]"
343
+ .venv/bin/python -m pytest
344
+ ```
345
+
346
+ ## License
347
+
348
+ Apache-2.0