Skip to content

Commit 6ef039a

Browse files
committed
fix test_root_service.py
Signed-off-by: Mihai Criveti <[email protected]>
1 parent e52d1b8 commit 6ef039a

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

tests/unit/mcpgateway/services/test_root_service.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,46 +87,47 @@ async def test_remove_nonexistent_root_raises():
8787

8888
@pytest.mark.asyncio
8989
async def test_initialize_adds_default_roots(monkeypatch):
90-
# Monkeypatch default_roots in settings
90+
# Pretend the app was configured with two default roots
9191
monkeypatch.setattr(settings, "default_roots", ["http://a.com", "local/path"])
92+
9293
service = RootService()
9394
await service.initialize()
94-
roots = await service.list_roots()
95-
uris = {r.uri for r in roots}
96-
assert "http://a.com" in uris
95+
96+
# Cast the FileUrl objects to plain strings for comparison
97+
uris = {str(r.uri) for r in await service.list_roots()}
98+
99+
# FileUrl normalises the HTTP URI to include a trailing slash
100+
assert "http://a.com/" in uris
97101
assert "file://local/path" in uris
98102

99103
await service.shutdown()
100104

101105

106+
102107
@pytest.mark.asyncio
103108
async def test_subscribe_changes_receives_events():
104109
service = RootService()
105-
# Start subscription
106110
events = []
107111

108112
async def subscriber():
109113
async for ev in service.subscribe_changes():
110114
events.append(ev)
111-
if len(events) >= 2:
115+
if len(events) >= 2: # expect "added" then "removed"
112116
break
113117

118+
# Start subscription and give the event-loop one tick so the queue
119+
# is fully registered before we emit any events.
114120
task = asyncio.create_task(subscriber())
115-
# Add and remove a root
121+
await asyncio.sleep(0)
122+
123+
# Add a root, then remove it again.
116124
r = await service.add_root("subscriber-test")
117-
await service.remove_root(r.uri)
118-
# Wait for subscriber to collect both events
125+
await service.remove_root(str(r.uri).rstrip("/")) # match stored key
126+
127+
# Collect both events or time-out
119128
await asyncio.wait_for(task, timeout=1.0)
120129

121-
# Validate the events
122-
assert events[0] == {
123-
"type": "root_added",
124-
"data": {"uri": r.uri, "name": r.name},
125-
}
126-
assert events[1] == {
127-
"type": "root_removed",
128-
"data": {"uri": r.uri},
129-
}
130-
131-
# After breaking, the subscription should be cleaned up without error
130+
assert events[0] == {"type": "root_added", "data": {"uri": r.uri, "name": r.name}}
131+
assert events[1] == {"type": "root_removed", "data": {"uri": r.uri}}
132+
132133
await service.shutdown()

0 commit comments

Comments
 (0)