|
9 | 9 |
|
10 | 10 | import com.github.copilot.sdk.json.CopilotClientOptions; |
11 | 11 | import com.github.copilot.sdk.json.PingResponse; |
| 12 | +import com.github.copilot.sdk.json.SessionLifecycleEvent; |
| 13 | +import com.github.copilot.sdk.json.SessionLifecycleEventTypes; |
12 | 14 |
|
13 | 15 | import java.io.BufferedReader; |
14 | 16 | import java.io.InputStreamReader; |
| 17 | +import java.lang.reflect.Field; |
15 | 18 | import java.nio.file.Path; |
16 | 19 | import java.nio.file.Paths; |
| 20 | +import java.util.ArrayList; |
17 | 21 |
|
18 | 22 | import static org.junit.jupiter.api.Assertions.*; |
19 | 23 |
|
@@ -218,4 +222,105 @@ void testUseLoggedInUserWithCliUrlThrows() { |
218 | 222 |
|
219 | 223 | assertThrows(IllegalArgumentException.class, () -> new CopilotClient(options)); |
220 | 224 | } |
| 225 | + |
| 226 | + // ===== onLifecycle tests ===== |
| 227 | + |
| 228 | + /** |
| 229 | + * Gets the internal LifecycleEventManager from a CopilotClient via reflection |
| 230 | + * so we can dispatch events for testing. |
| 231 | + */ |
| 232 | + private static LifecycleEventManager getLifecycleManager(CopilotClient client) throws Exception { |
| 233 | + Field f = CopilotClient.class.getDeclaredField("lifecycleManager"); |
| 234 | + f.setAccessible(true); |
| 235 | + return (LifecycleEventManager) f.get(client); |
| 236 | + } |
| 237 | + |
| 238 | + private static SessionLifecycleEvent lifecycleEvent(String type) { |
| 239 | + var e = new SessionLifecycleEvent(); |
| 240 | + e.setType(type); |
| 241 | + e.setSessionId("test-session-id"); |
| 242 | + return e; |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + void testOnLifecycleWildcardReceivesAllEvents() throws Exception { |
| 247 | + try (var client = new CopilotClient()) { |
| 248 | + var received = new ArrayList<SessionLifecycleEvent>(); |
| 249 | + client.onLifecycle(received::add); |
| 250 | + |
| 251 | + LifecycleEventManager mgr = getLifecycleManager(client); |
| 252 | + mgr.dispatch(lifecycleEvent(SessionLifecycleEventTypes.CREATED)); |
| 253 | + mgr.dispatch(lifecycleEvent(SessionLifecycleEventTypes.DELETED)); |
| 254 | + |
| 255 | + assertEquals(2, received.size()); |
| 256 | + assertEquals(SessionLifecycleEventTypes.CREATED, received.get(0).getType()); |
| 257 | + assertEquals(SessionLifecycleEventTypes.DELETED, received.get(1).getType()); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + void testOnLifecycleTypedReceivesOnlyMatchingEvents() throws Exception { |
| 263 | + try (var client = new CopilotClient()) { |
| 264 | + var received = new ArrayList<SessionLifecycleEvent>(); |
| 265 | + client.onLifecycle(SessionLifecycleEventTypes.CREATED, received::add); |
| 266 | + |
| 267 | + LifecycleEventManager mgr = getLifecycleManager(client); |
| 268 | + mgr.dispatch(lifecycleEvent(SessionLifecycleEventTypes.CREATED)); |
| 269 | + mgr.dispatch(lifecycleEvent(SessionLifecycleEventTypes.DELETED)); |
| 270 | + |
| 271 | + assertEquals(1, received.size()); |
| 272 | + assertEquals(SessionLifecycleEventTypes.CREATED, received.get(0).getType()); |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + @Test |
| 277 | + void testOnLifecycleUnsubscribeStopsDelivery() throws Exception { |
| 278 | + try (var client = new CopilotClient()) { |
| 279 | + var received = new ArrayList<SessionLifecycleEvent>(); |
| 280 | + AutoCloseable sub = client.onLifecycle(received::add); |
| 281 | + |
| 282 | + LifecycleEventManager mgr = getLifecycleManager(client); |
| 283 | + mgr.dispatch(lifecycleEvent(SessionLifecycleEventTypes.CREATED)); |
| 284 | + assertEquals(1, received.size()); |
| 285 | + |
| 286 | + sub.close(); |
| 287 | + |
| 288 | + mgr.dispatch(lifecycleEvent(SessionLifecycleEventTypes.DELETED)); |
| 289 | + assertEquals(1, received.size(), "Should not receive events after unsubscribe"); |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + @Test |
| 294 | + void testOnLifecycleTypedUnsubscribeStopsDelivery() throws Exception { |
| 295 | + try (var client = new CopilotClient()) { |
| 296 | + var received = new ArrayList<SessionLifecycleEvent>(); |
| 297 | + AutoCloseable sub = client.onLifecycle(SessionLifecycleEventTypes.UPDATED, received::add); |
| 298 | + |
| 299 | + LifecycleEventManager mgr = getLifecycleManager(client); |
| 300 | + mgr.dispatch(lifecycleEvent(SessionLifecycleEventTypes.UPDATED)); |
| 301 | + assertEquals(1, received.size()); |
| 302 | + |
| 303 | + sub.close(); |
| 304 | + |
| 305 | + mgr.dispatch(lifecycleEvent(SessionLifecycleEventTypes.UPDATED)); |
| 306 | + assertEquals(1, received.size(), "Should not receive events after unsubscribe"); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + @Test |
| 311 | + void testOnLifecycleMultipleHandlers() throws Exception { |
| 312 | + try (var client = new CopilotClient()) { |
| 313 | + var wildcard = new ArrayList<SessionLifecycleEvent>(); |
| 314 | + var typed = new ArrayList<SessionLifecycleEvent>(); |
| 315 | + |
| 316 | + client.onLifecycle(wildcard::add); |
| 317 | + client.onLifecycle(SessionLifecycleEventTypes.CREATED, typed::add); |
| 318 | + |
| 319 | + LifecycleEventManager mgr = getLifecycleManager(client); |
| 320 | + mgr.dispatch(lifecycleEvent(SessionLifecycleEventTypes.CREATED)); |
| 321 | + |
| 322 | + assertEquals(1, wildcard.size()); |
| 323 | + assertEquals(1, typed.size()); |
| 324 | + } |
| 325 | + } |
221 | 326 | } |
0 commit comments