@@ -87,46 +87,47 @@ async def test_remove_nonexistent_root_raises():
87
87
88
88
@pytest .mark .asyncio
89
89
async def test_initialize_adds_default_roots (monkeypatch ):
90
- # Monkeypatch default_roots in settings
90
+ # Pretend the app was configured with two default roots
91
91
monkeypatch .setattr (settings , "default_roots" , ["http://a.com" , "local/path" ])
92
+
92
93
service = RootService ()
93
94
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
97
101
assert "file://local/path" in uris
98
102
99
103
await service .shutdown ()
100
104
101
105
106
+
102
107
@pytest .mark .asyncio
103
108
async def test_subscribe_changes_receives_events ():
104
109
service = RootService ()
105
- # Start subscription
106
110
events = []
107
111
108
112
async def subscriber ():
109
113
async for ev in service .subscribe_changes ():
110
114
events .append (ev )
111
- if len (events ) >= 2 :
115
+ if len (events ) >= 2 : # expect "added" then "removed"
112
116
break
113
117
118
+ # Start subscription and give the event-loop one tick so the queue
119
+ # is fully registered before we emit any events.
114
120
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.
116
124
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
119
128
await asyncio .wait_for (task , timeout = 1.0 )
120
129
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
+
132
133
await service .shutdown ()
0 commit comments