|
5 | 5 | using System.Text.Json; |
6 | 6 | using Xunit; |
7 | 7 | using OpenClaw.Shared; |
| 8 | +using OpenClaw.TestSupport; |
8 | 9 |
|
9 | 10 | namespace OpenClaw.Shared.Tests; |
10 | 11 |
|
@@ -487,11 +488,189 @@ public List<string> CaptureAuthenticationFailedEvents() |
487 | 488 | _client.AuthenticationFailed += (_, msg) => events.Add(msg); |
488 | 489 | return events; |
489 | 490 | } |
| 491 | + |
| 492 | + public (int WizardResponses, int RequestMethods) GetPendingRequestCounts() |
| 493 | + { |
| 494 | + var wizardField = typeof(OpenClawGatewayClient).GetField( |
| 495 | + "_pendingWizardResponses", |
| 496 | + System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); |
| 497 | + var wizardResponses = |
| 498 | + (System.Collections.Concurrent.ConcurrentDictionary<string, TaskCompletionSource<JsonElement>>)wizardField!.GetValue(_client)!; |
| 499 | + |
| 500 | + var methodsField = typeof(OpenClawGatewayClient).GetField( |
| 501 | + "_pendingRequestMethods", |
| 502 | + System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); |
| 503 | + var requestMethods = (Dictionary<string, string>)methodsField!.GetValue(_client)!; |
| 504 | + |
| 505 | + return (wizardResponses.Count, requestMethods.Count); |
| 506 | + } |
490 | 507 | } |
491 | 508 |
|
492 | 509 | private static string CreateTempIdentityPath() => |
493 | 510 | Path.Combine(Path.GetTempPath(), "OpenClawGatewayClientTests", Guid.NewGuid().ToString("N")); |
494 | 511 |
|
| 512 | + [Fact] |
| 513 | + public async Task SendWizardRequestAsync_ResponseBeforeDispose_ReturnsPayloadAndCleansTracking() |
| 514 | + { |
| 515 | + using var server = new LoopbackWebSocketServer(); |
| 516 | + using var identity = new TempDirectory("wizard-request-"); |
| 517 | + await server.StartAsync(); |
| 518 | + var helper = new GatewayClientTestHelper( |
| 519 | + gatewayUrl: server.WebSocketUrl, |
| 520 | + identityPath: identity.Path); |
| 521 | + using var client = helper.Client; |
| 522 | + await client.ConnectAsync(); |
| 523 | + |
| 524 | + var responseTask = client.SendWizardRequestAsync("wizard.start", timeoutMs: 10_000); |
| 525 | + var request = await server.ReceiveTextAsync().WaitAsync(TimeSpan.FromSeconds(2)); |
| 526 | + var requestId = ReadRequestId(request); |
| 527 | + |
| 528 | + await server.SendTextAsync( |
| 529 | + JsonSerializer.Serialize(new |
| 530 | + { |
| 531 | + type = "res", |
| 532 | + id = requestId, |
| 533 | + ok = true, |
| 534 | + payload = new { stepId = "welcome" } |
| 535 | + })); |
| 536 | + |
| 537 | + var payload = await responseTask.WaitAsync(TimeSpan.FromSeconds(2)); |
| 538 | + |
| 539 | + Assert.Equal("welcome", payload.GetProperty("stepId").GetString()); |
| 540 | + Assert.Equal((0, 0), helper.GetPendingRequestCounts()); |
| 541 | + |
| 542 | + client.Dispose(); |
| 543 | + Assert.Equal((0, 0), helper.GetPendingRequestCounts()); |
| 544 | + } |
| 545 | + |
| 546 | + [Fact] |
| 547 | + public async Task SendWizardRequestAsync_GatewayError_PropagatesUnchangedAndCleansTracking() |
| 548 | + { |
| 549 | + using var server = new LoopbackWebSocketServer(); |
| 550 | + using var identity = new TempDirectory("wizard-request-"); |
| 551 | + await server.StartAsync(); |
| 552 | + var helper = new GatewayClientTestHelper( |
| 553 | + gatewayUrl: server.WebSocketUrl, |
| 554 | + identityPath: identity.Path); |
| 555 | + using var client = helper.Client; |
| 556 | + await client.ConnectAsync(); |
| 557 | + |
| 558 | + var responseTask = client.SendWizardRequestAsync("wizard.next", timeoutMs: 10_000); |
| 559 | + var request = await server.ReceiveTextAsync().WaitAsync(TimeSpan.FromSeconds(2)); |
| 560 | + var requestId = ReadRequestId(request); |
| 561 | + |
| 562 | + await server.SendTextAsync( |
| 563 | + JsonSerializer.Serialize(new |
| 564 | + { |
| 565 | + type = "res", |
| 566 | + id = requestId, |
| 567 | + ok = false, |
| 568 | + error = new { message = "wizard rejected" } |
| 569 | + })); |
| 570 | + |
| 571 | + var exception = await Assert.ThrowsAsync<InvalidOperationException>( |
| 572 | + async () => await responseTask.WaitAsync(TimeSpan.FromSeconds(2))); |
| 573 | + |
| 574 | + Assert.Equal("wizard rejected", exception.Message); |
| 575 | + Assert.Equal((0, 0), helper.GetPendingRequestCounts()); |
| 576 | + } |
| 577 | + |
| 578 | + [Fact] |
| 579 | + public async Task SendWizardRequestAsync_DeadlineExpires_ThrowsTimeoutAndCleansTracking() |
| 580 | + { |
| 581 | + using var server = new LoopbackWebSocketServer(); |
| 582 | + using var identity = new TempDirectory("wizard-request-"); |
| 583 | + await server.StartAsync(); |
| 584 | + var helper = new GatewayClientTestHelper( |
| 585 | + gatewayUrl: server.WebSocketUrl, |
| 586 | + identityPath: identity.Path); |
| 587 | + using var client = helper.Client; |
| 588 | + await client.ConnectAsync(); |
| 589 | + |
| 590 | + var responseTask = client.SendWizardRequestAsync("wizard.status", timeoutMs: 250); |
| 591 | + await server.ReceiveTextAsync().WaitAsync(TimeSpan.FromSeconds(2)); |
| 592 | + |
| 593 | + var exception = await Assert.ThrowsAsync<TimeoutException>(async () => await responseTask); |
| 594 | + |
| 595 | + Assert.Equal("Timed out waiting for wizard.status response", exception.Message); |
| 596 | + Assert.Equal((0, 0), helper.GetPendingRequestCounts()); |
| 597 | + } |
| 598 | + |
| 599 | + [Fact] |
| 600 | + public async Task SendWizardRequestAsync_LateResponseAfterTimeout_DoesNotChangeOutcomeOrTracking() |
| 601 | + { |
| 602 | + using var server = new LoopbackWebSocketServer(); |
| 603 | + using var identity = new TempDirectory("wizard-request-"); |
| 604 | + await server.StartAsync(); |
| 605 | + var helper = new GatewayClientTestHelper( |
| 606 | + gatewayUrl: server.WebSocketUrl, |
| 607 | + identityPath: identity.Path); |
| 608 | + using var client = helper.Client; |
| 609 | + await client.ConnectAsync(); |
| 610 | + |
| 611 | + var timedOutTask = client.SendWizardRequestAsync("wizard.status", timeoutMs: 250); |
| 612 | + var timedOutRequest = await server.ReceiveTextAsync().WaitAsync(TimeSpan.FromSeconds(2)); |
| 613 | + var timedOutRequestId = ReadRequestId(timedOutRequest); |
| 614 | + var firstTimeout = await Assert.ThrowsAsync<TimeoutException>(async () => await timedOutTask); |
| 615 | + |
| 616 | + await server.SendTextAsync( |
| 617 | + JsonSerializer.Serialize(new |
| 618 | + { |
| 619 | + type = "res", |
| 620 | + id = timedOutRequestId, |
| 621 | + ok = true, |
| 622 | + payload = new { stepId = "too-late" } |
| 623 | + })); |
| 624 | + |
| 625 | + var probeTask = client.SendWizardRequestAsync("wizard.status", timeoutMs: 10_000); |
| 626 | + var probeRequest = await server.ReceiveTextAsync().WaitAsync(TimeSpan.FromSeconds(2)); |
| 627 | + var probeRequestId = ReadRequestId(probeRequest); |
| 628 | + await server.SendTextAsync( |
| 629 | + JsonSerializer.Serialize(new |
| 630 | + { |
| 631 | + type = "res", |
| 632 | + id = probeRequestId, |
| 633 | + ok = true, |
| 634 | + payload = new { stepId = "probe" } |
| 635 | + })); |
| 636 | + |
| 637 | + var probePayload = await probeTask.WaitAsync(TimeSpan.FromSeconds(2)); |
| 638 | + var repeatedTimeout = await Assert.ThrowsAsync<TimeoutException>(async () => await timedOutTask); |
| 639 | + |
| 640 | + Assert.Equal("probe", probePayload.GetProperty("stepId").GetString()); |
| 641 | + Assert.Same(firstTimeout, repeatedTimeout); |
| 642 | + Assert.Equal((0, 0), helper.GetPendingRequestCounts()); |
| 643 | + } |
| 644 | + |
| 645 | + [Fact] |
| 646 | + public async Task SendWizardRequestAsync_DisposeBeforeResponse_ThrowsCancellationAndCleansTracking() |
| 647 | + { |
| 648 | + using var server = new LoopbackWebSocketServer(); |
| 649 | + using var identity = new TempDirectory("wizard-request-"); |
| 650 | + await server.StartAsync(); |
| 651 | + var helper = new GatewayClientTestHelper( |
| 652 | + gatewayUrl: server.WebSocketUrl, |
| 653 | + identityPath: identity.Path); |
| 654 | + using var client = helper.Client; |
| 655 | + await client.ConnectAsync(); |
| 656 | + |
| 657 | + var responseTask = client.SendWizardRequestAsync("wizard.cancel", timeoutMs: 10_000); |
| 658 | + await server.ReceiveTextAsync().WaitAsync(TimeSpan.FromSeconds(2)); |
| 659 | + |
| 660 | + client.Dispose(); |
| 661 | + |
| 662 | + await Assert.ThrowsAnyAsync<OperationCanceledException>( |
| 663 | + async () => await responseTask.WaitAsync(TimeSpan.FromSeconds(2))); |
| 664 | + Assert.Equal((0, 0), helper.GetPendingRequestCounts()); |
| 665 | + } |
| 666 | + |
| 667 | + private static string ReadRequestId(string request) |
| 668 | + { |
| 669 | + using var document = JsonDocument.Parse(request); |
| 670 | + return document.RootElement.GetProperty("id").GetString() |
| 671 | + ?? throw new InvalidOperationException("Gateway request did not include an id."); |
| 672 | + } |
| 673 | + |
495 | 674 | [Fact] |
496 | 675 | public void ExtractChatTimestampMs_FallsBackFromInvalidTimestampToTs() |
497 | 676 | { |
|
0 commit comments