|
18 | 18 | import java.nio.file.Path; |
19 | 19 | import java.nio.file.Paths; |
20 | 20 | import java.util.ArrayList; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | +import java.util.concurrent.ExecutionException; |
21 | 23 |
|
22 | 24 | import static org.junit.jupiter.api.Assertions.*; |
23 | 25 |
|
@@ -323,4 +325,128 @@ void testOnLifecycleMultipleHandlers() throws Exception { |
323 | 325 | assertEquals(1, typed.size()); |
324 | 326 | } |
325 | 327 | } |
| 328 | + |
| 329 | + // ===== getState() coverage ===== |
| 330 | + |
| 331 | + @Test |
| 332 | + void testGetStateErrorAfterFailedStart() throws Exception { |
| 333 | + // Use a non-existent CLI path to trigger a startup failure |
| 334 | + var options = new CopilotClientOptions().setCliPath("/nonexistent/path/to/cli").setAutoStart(false); |
| 335 | + |
| 336 | + try (var client = new CopilotClient(options)) { |
| 337 | + // Manually start to trigger the error |
| 338 | + CompletableFuture<Void> startFuture = client.start(); |
| 339 | + |
| 340 | + // Wait for the start to fail |
| 341 | + try { |
| 342 | + startFuture.get(); |
| 343 | + } catch (ExecutionException e) { |
| 344 | + // Expected |
| 345 | + } |
| 346 | + |
| 347 | + assertEquals(ConnectionState.ERROR, client.getState()); |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + @Test |
| 352 | + void testGetStateConnectingDuringStart() throws Exception { |
| 353 | + // Use a non-existent CLI path; the future won't complete immediately |
| 354 | + var options = new CopilotClientOptions().setCliPath("/nonexistent/path/to/cli").setAutoStart(false); |
| 355 | + |
| 356 | + try (var client = new CopilotClient(options)) { |
| 357 | + // Start is async - grab state before completion |
| 358 | + client.start(); |
| 359 | + |
| 360 | + // The state should be either CONNECTING or ERROR depending on timing |
| 361 | + ConnectionState state = client.getState(); |
| 362 | + assertTrue(state == ConnectionState.CONNECTING || state == ConnectionState.ERROR, |
| 363 | + "State should be CONNECTING or ERROR, was: " + state); |
| 364 | + } |
| 365 | + } |
| 366 | + |
| 367 | + // ===== ensureConnected throws when autoStart=false and not connected ===== |
| 368 | + |
| 369 | + @Test |
| 370 | + void testEnsureConnectedThrowsWhenNotStartedAndAutoStartDisabled() { |
| 371 | + var options = new CopilotClientOptions().setAutoStart(false); |
| 372 | + |
| 373 | + try (var client = new CopilotClient(options)) { |
| 374 | + // Calling ping (which calls ensureConnected) without start() should throw |
| 375 | + assertThrows(IllegalStateException.class, () -> client.ping("test")); |
| 376 | + } |
| 377 | + } |
| 378 | + |
| 379 | + // ===== close() idempotency ===== |
| 380 | + |
| 381 | + @Test |
| 382 | + void testCloseIsIdempotent() { |
| 383 | + var client = new CopilotClient(); |
| 384 | + |
| 385 | + // First close |
| 386 | + client.close(); |
| 387 | + // Second close should not throw |
| 388 | + assertDoesNotThrow(() -> client.close()); |
| 389 | + } |
| 390 | + |
| 391 | + @Test |
| 392 | + void testCloseAfterFailedStart() throws Exception { |
| 393 | + var options = new CopilotClientOptions().setCliPath("/nonexistent/path/to/cli").setAutoStart(false); |
| 394 | + var client = new CopilotClient(options); |
| 395 | + |
| 396 | + CompletableFuture<Void> startFuture = client.start(); |
| 397 | + try { |
| 398 | + startFuture.get(); |
| 399 | + } catch (ExecutionException e) { |
| 400 | + // Expected |
| 401 | + } |
| 402 | + |
| 403 | + // close() after a failed start should not throw |
| 404 | + assertDoesNotThrow(() -> client.close()); |
| 405 | + } |
| 406 | + |
| 407 | + // ===== stop() with no connection ===== |
| 408 | + |
| 409 | + @Test |
| 410 | + void testStopWithNoConnectionCompletes() throws Exception { |
| 411 | + try (var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false))) { |
| 412 | + // stop() without start() should complete without error |
| 413 | + client.stop().get(); |
| 414 | + assertEquals(ConnectionState.DISCONNECTED, client.getState()); |
| 415 | + } |
| 416 | + } |
| 417 | + |
| 418 | + @Test |
| 419 | + void testForceStopWithNoConnectionCompletes() throws Exception { |
| 420 | + try (var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false))) { |
| 421 | + // forceStop() without start() should complete without error |
| 422 | + client.forceStop().get(); |
| 423 | + assertEquals(ConnectionState.DISCONNECTED, client.getState()); |
| 424 | + } |
| 425 | + } |
| 426 | + |
| 427 | + // ===== start() idempotency ===== |
| 428 | + |
| 429 | + @Test |
| 430 | + void testStartIsIdempotentSingleConnectionAttempt() throws Exception { |
| 431 | + var options = new CopilotClientOptions().setCliPath("/nonexistent/path/to/cli").setAutoStart(false); |
| 432 | + |
| 433 | + try (var client = new CopilotClient(options)) { |
| 434 | + client.start(); |
| 435 | + client.start(); |
| 436 | + |
| 437 | + // Both calls should result in the same state (single connection attempt) |
| 438 | + ConnectionState state = client.getState(); |
| 439 | + assertTrue(state == ConnectionState.CONNECTING || state == ConnectionState.ERROR, |
| 440 | + "State should be CONNECTING or ERROR after start(), was: " + state); |
| 441 | + } |
| 442 | + } |
| 443 | + |
| 444 | + // ===== null options defaulting ===== |
| 445 | + |
| 446 | + @Test |
| 447 | + void testNullOptionsDefaultsToEmpty() { |
| 448 | + try (var client = new CopilotClient(null)) { |
| 449 | + assertEquals(ConnectionState.DISCONNECTED, client.getState()); |
| 450 | + } |
| 451 | + } |
326 | 452 | } |
0 commit comments