-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_core.py
More file actions
208 lines (159 loc) · 6.47 KB
/
test_core.py
File metadata and controls
208 lines (159 loc) · 6.47 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
"""
OMEN Engine Core — Tests for Plugin Runtime and Event Bus
"""
import pytest
from engine.core.runtime import PluginRegistry, PluginRuntime, PluginManifest
from engine.core.event_bus import InProcessEventBus
# ---------------------------------------------------------------------------
# Test Fixtures
# ---------------------------------------------------------------------------
class MockPlugin:
plugin_id = "mock-plugin"
def __init__(self):
self.started = False
self.stopped = False
self.received_events = []
self.config = {}
def on_start(self, config):
self.started = True
self.config = config
def on_stop(self):
self.stopped = True
def on_event(self, event):
self.received_events.append(event)
return []
class ErrorPlugin:
plugin_id = "error-plugin"
def on_start(self, config):
pass
def on_stop(self):
pass
def on_event(self, event):
raise RuntimeError("Intentional test error")
def make_manifest(plugin_id="mock-plugin", capabilities=None):
return PluginManifest(
plugin_id=plugin_id,
name="Mock Plugin",
version="1.0.0",
description="Test plugin",
capabilities=capabilities or ["subscribe:test_event"],
)
@pytest.fixture
def registry():
return PluginRegistry()
@pytest.fixture
def event_bus():
return InProcessEventBus()
@pytest.fixture
def runtime(registry, event_bus):
return PluginRuntime(registry=registry, event_bus=event_bus)
# ---------------------------------------------------------------------------
# PluginRegistry Tests
# ---------------------------------------------------------------------------
class TestPluginRegistry:
def test_register_and_get(self, registry):
plugin = MockPlugin()
manifest = make_manifest()
registry.register(plugin, manifest)
assert registry.get("mock-plugin") is plugin
def test_duplicate_registration_raises(self, registry):
plugin = MockPlugin()
manifest = make_manifest()
registry.register(plugin, manifest)
with pytest.raises(ValueError):
registry.register(plugin, manifest)
def test_list_plugin_ids(self, registry):
registry.register(MockPlugin(), make_manifest("p1"))
registry.register(MockPlugin(), make_manifest("p2"))
assert set(registry.list_plugin_ids()) == {"p1", "p2"}
def test_unregister(self, registry):
registry.register(MockPlugin(), make_manifest())
registry.unregister("mock-plugin")
assert registry.get("mock-plugin") is None
def test_get_manifest(self, registry):
manifest = make_manifest()
registry.register(MockPlugin(), manifest)
assert registry.get_manifest("mock-plugin") is manifest
# ---------------------------------------------------------------------------
# PluginRuntime Tests
# ---------------------------------------------------------------------------
class TestPluginRuntime:
def test_start_plugin(self, runtime, registry):
plugin = MockPlugin()
registry.register(plugin, make_manifest())
runtime.start_plugin("mock-plugin", {"key": "value"})
assert plugin.started
assert plugin.config == {"key": "value"}
assert "mock-plugin" in runtime.running_plugins()
def test_stop_plugin(self, runtime, registry):
plugin = MockPlugin()
registry.register(plugin, make_manifest())
runtime.start_plugin("mock-plugin", {})
runtime.stop_plugin("mock-plugin")
assert plugin.stopped
assert "mock-plugin" not in runtime.running_plugins()
def test_dispatch_event(self, runtime, registry):
plugin = MockPlugin()
registry.register(plugin, make_manifest())
runtime.start_plugin("mock-plugin", {})
class FakeEvent:
event_type = "test_event"
event = FakeEvent()
runtime.dispatch_event(event)
assert event in plugin.received_events
def test_plugin_error_does_not_crash_dispatch(self, runtime, registry):
error_plugin = ErrorPlugin()
good_plugin = MockPlugin()
good_plugin.plugin_id = "good-plugin"
registry.register(error_plugin, make_manifest("error-plugin"))
registry.register(good_plugin, make_manifest("good-plugin"))
runtime.start_plugin("error-plugin", {})
runtime.start_plugin("good-plugin", {})
class FakeEvent:
event_type = "test_event"
# Should not raise even though error_plugin raises
runtime.dispatch_event(FakeEvent())
assert FakeEvent in [type(e) for e in good_plugin.received_events]
def test_start_unknown_plugin_raises(self, runtime):
with pytest.raises(KeyError):
runtime.start_plugin("nonexistent", {})
# ---------------------------------------------------------------------------
# InProcessEventBus Tests
# ---------------------------------------------------------------------------
class TestInProcessEventBus:
def test_subscribe_and_publish(self, event_bus):
received = []
event_bus.subscribe("test_topic", received.append)
event_bus.publish("test_topic", "hello")
assert received == ["hello"]
def test_unsubscribe(self, event_bus):
received = []
sub_id = event_bus.subscribe("test_topic", received.append)
event_bus.unsubscribe(sub_id)
event_bus.publish("test_topic", "hello")
assert received == []
def test_wildcard_subscription(self, event_bus):
received = []
event_bus.subscribe("track.*", received.append)
event_bus.publish("track_update", "event1")
event_bus.publish("track_delete", "event2")
event_bus.publish("other_event", "event3")
assert "event1" in received
assert "event2" in received
assert "event3" not in received
def test_multiple_subscribers(self, event_bus):
r1, r2 = [], []
event_bus.subscribe("topic", r1.append)
event_bus.subscribe("topic", r2.append)
event_bus.publish("topic", "x")
assert r1 == ["x"]
assert r2 == ["x"]
def test_handler_error_does_not_crash_bus(self, event_bus):
def bad_handler(event):
raise RuntimeError("oops")
good_received = []
event_bus.subscribe("topic", bad_handler)
event_bus.subscribe("topic", good_received.append)
# Should not raise
event_bus.publish("topic", "event")
assert "event" in good_received