-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_integration.py
More file actions
219 lines (197 loc) · 6.91 KB
/
Copy pathtest_integration.py
File metadata and controls
219 lines (197 loc) · 6.91 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""Integration tests — end-to-end bus → service → store → bus pipeline.
Tests verify that real services react correctly when wired through the
Runtime and events flow through the full pipeline.
"""
from __future__ import annotations
from typing import Any, cast
from underwrite.__config__ import Configuration
from underwrite.__events__ import Event, EventType
from underwrite.__runtime__ import Runtime
from underwrite.__store__ import MemoryStore
def memory_runtime() -> Runtime:
"""Returns a Runtime backed by MemoryStore for test isolation."""
cfg = Configuration.default()
cfg.store.backend = "memory"
cfg.metrics.enabled = False
cfg.authz.enabled = False
return Runtime(config=cfg)
class TestRuntimeIntegration:
"""End-to-end tests using the full Runtime."""
def test_start_stop_services(self) -> None:
cfg = Configuration.default()
cfg.authz.enabled = False
rt = Runtime(config=cfg)
rt.register("mechanism")
rt.wire("mechanism")
rt.start(["mechanism"])
svc = rt.get("mechanism")
assert svc is not None
assert svc.is_running
rt.stop()
svc = rt.get("mechanism")
assert svc is not None
assert svc.is_running is False
def test_mechanism_emits_seed_added(self) -> None:
rt = memory_runtime()
bus = rt.bus
received: list[Event] = []
bus.subscribe("*", lambda e: received.append(e))
rt.register("mechanism")
rt.wire("mechanism")
bus.start()
rt.start(["mechanism"])
svc = rt.get("mechanism")
assert svc is not None
svc.handle(
Event(
event_type="mechanism",
source="test",
payload={"command": "add_seed", "user": "bank", "base_budget": 100000},
)
)
assert any(e.event_type == EventType.SEED_ADDED for e in received)
def test_store_persists_across_start_stop(self) -> None:
cfg = Configuration.default()
cfg.authz.enabled = False
rt = Runtime(config=cfg)
rt.register("mechanism")
rt.wire("mechanism")
rt.start(["mechanism"])
svc = rt.get("mechanism")
assert svc is not None
svc.handle(
Event(
event_type="mechanism",
source="test",
payload={"command": "add_seed", "user": "bank", "base_budget": 100000},
)
)
orig_state = rt.store.get("protocol:state")
rt.stop()
assert orig_state is not None
assert "bank" in orig_state["seeds"]
def test_bus_delivers_to_subscribed_service(self) -> None:
cfg = Configuration.default()
cfg.authz.enabled = False
rt = Runtime(config=cfg)
bus = rt.bus
rt.register("audit")
rt.wire("audit")
bus.start()
rt.start(["audit"])
bus.publish(
Event(
event_type=EventType.LOAN_ORIGINATED, source="test", payload={"borrower": "alice", "principal": 10000}
)
)
audit = cast(Any, rt.get("audit"))
assert audit is not None
assert len(audit._ledger) >= 1
assert audit._ledger[0]["event_type"] == EventType.LOAN_ORIGINATED
def test_mechanism_rejects_with_bus(self) -> None:
cfg = Configuration.default()
cfg.authz.enabled = False
rt = Runtime(config=cfg)
bus = rt.bus
received: list[Event] = []
bus.subscribe("mechanism.rejected", lambda e: received.append(e))
rt.register("mechanism")
rt.wire("mechanism")
bus.start()
rt.start(["mechanism"])
svc = rt.get("mechanism")
assert svc is not None
svc.handle(
Event(
event_type="mechanism", source="test", payload={"command": "add_seed", "user": "bank", "base_budget": 0}
)
)
assert len(received) == 1
assert received[0].payload["reason"] is not None
def test_full_loan_lifecycle(self) -> None:
rt = memory_runtime()
bus = rt.bus
all_events: list[Event] = []
bus.subscribe("*", lambda e: all_events.append(e))
rt.register("mechanism")
rt.register("audit")
rt.wire("mechanism")
rt.wire("audit")
bus.start()
rt.start(["mechanism", "audit"])
svc = rt.get("mechanism")
assert svc is not None
svc.handle(
Event(
event_type="mechanism",
source="test",
payload={"command": "add_seed", "user": "bank", "base_budget": 1_000_000},
)
)
svc.handle(
Event(
event_type="mechanism",
source="test",
payload={"command": "add_user", "sponsor": "bank", "user": "alice", "delegation_amount": 500_000},
)
)
svc.handle(
Event(
event_type="mechanism",
source="test",
payload={
"command": "originate",
"borrower": "alice",
"principal": 100000,
"term": 12,
"default_probability": 0.02,
"protocol_rate": 0.10,
"max_delegation_rate": 0.05,
},
)
)
emitted = {e.event_type for e in all_events}
assert EventType.SEED_ADDED in emitted
assert EventType.USER_ADDED in emitted
assert EventType.LOAN_ORIGINATED in emitted
audit = rt.get("audit")
assert audit is not None
assert len(audit._ledger) >= 3 # type: ignore[attr-defined]
rt.stop()
class TestStoreIntegration:
def test_memory_store_round_trip(self) -> None:
store = MemoryStore()
store.set("key", {"nested": [1, 2, 3]})
assert store.get("key") == {"nested": [1, 2, 3]}
assert store.exists("key")
assert store.delete("key")
assert store.get("key") is None
def test_keys_pattern(self) -> None:
store = MemoryStore()
store.set("a:1", 1)
store.set("a:2", 2)
store.set("b:1", 3)
assert len(store.keys("a:")) == 2
class TestCrossServiceCommunication:
def test_two_services_share_store(self) -> None:
rt = memory_runtime()
bus = rt.bus
bus.start()
rt.register("mechanism")
rt.register("audit")
rt.wire("mechanism")
rt.wire("audit")
rt.start(["mechanism", "audit"])
svc = rt.get("mechanism")
assert svc is not None
svc.handle(
Event(
event_type="mechanism",
source="test",
payload={"command": "add_seed", "user": "bank", "base_budget": 100000},
)
)
state = rt.store.get("protocol:state")
assert state is not None
assert "bank" in state["seeds"]
rt.stop()