|
| 1 | +using OpenClaw.Connection; |
| 2 | +using OpenClaw.Shared; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace OpenClaw.Connection.Tests; |
| 6 | + |
| 7 | +public class OperatorScopeHelperTests |
| 8 | +{ |
| 9 | + // ─── CanApproveDevices ─── |
| 10 | + |
| 11 | + [Fact] |
| 12 | + public void CanApproveDevices_ReturnsFalse_ForEmptyList() |
| 13 | + { |
| 14 | + Assert.False(OperatorScopeHelper.CanApproveDevices([])); |
| 15 | + } |
| 16 | + |
| 17 | + [Theory] |
| 18 | + [InlineData("operator.admin")] |
| 19 | + [InlineData("operator.pairing")] |
| 20 | + [InlineData("OPERATOR.ADMIN")] |
| 21 | + [InlineData("OPERATOR.PAIRING")] |
| 22 | + [InlineData("Operator.Admin")] |
| 23 | + [InlineData("Operator.Pairing")] |
| 24 | + public void CanApproveDevices_ReturnsTrue_ForApprovalScope(string scope) |
| 25 | + { |
| 26 | + Assert.True(OperatorScopeHelper.CanApproveDevices([scope])); |
| 27 | + } |
| 28 | + |
| 29 | + [Theory] |
| 30 | + [InlineData("operator.read")] |
| 31 | + [InlineData("node.invoke")] |
| 32 | + [InlineData("admin")] |
| 33 | + [InlineData("pairing")] |
| 34 | + [InlineData("operator")] |
| 35 | + [InlineData("")] |
| 36 | + public void CanApproveDevices_ReturnsFalse_ForNonApprovalScope(string scope) |
| 37 | + { |
| 38 | + Assert.False(OperatorScopeHelper.CanApproveDevices([scope])); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void CanApproveDevices_ReturnsTrue_WhenAdminIsAmongMultipleScopes() |
| 43 | + { |
| 44 | + Assert.True(OperatorScopeHelper.CanApproveDevices(["operator.read", "operator.admin", "node.invoke"])); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void CanApproveDevices_ReturnsTrue_WhenPairingIsAmongMultipleScopes() |
| 49 | + { |
| 50 | + Assert.True(OperatorScopeHelper.CanApproveDevices(["operator.read", "operator.pairing"])); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void CanApproveDevices_ReturnsFalse_WhenNoApprovalScopePresent() |
| 55 | + { |
| 56 | + Assert.False(OperatorScopeHelper.CanApproveDevices(["operator.read", "node.invoke", "user.profile"])); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +public class ChatNavigationReadinessTests |
| 61 | +{ |
| 62 | + // ─── IsOperatorHandshakeReady ─── |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void IsOperatorHandshakeReady_ReturnsTrue_WhenManagerIsNull() |
| 66 | + { |
| 67 | + Assert.True(ChatNavigationReadiness.IsOperatorHandshakeReady(null)); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void IsOperatorHandshakeReady_ReturnsTrue_WhenOperatorIsConnected() |
| 72 | + { |
| 73 | + var mgr = new StubConnectionManager(RoleConnectionState.Connected); |
| 74 | + Assert.True(ChatNavigationReadiness.IsOperatorHandshakeReady(mgr)); |
| 75 | + } |
| 76 | + |
| 77 | + [Theory] |
| 78 | + [InlineData(RoleConnectionState.Idle)] |
| 79 | + [InlineData(RoleConnectionState.Connecting)] |
| 80 | + [InlineData(RoleConnectionState.Error)] |
| 81 | + [InlineData(RoleConnectionState.PairingRequired)] |
| 82 | + public void IsOperatorHandshakeReady_ReturnsFalse_WhenOperatorIsNotConnected(RoleConnectionState state) |
| 83 | + { |
| 84 | + var mgr = new StubConnectionManager(state); |
| 85 | + Assert.False(ChatNavigationReadiness.IsOperatorHandshakeReady(mgr)); |
| 86 | + } |
| 87 | + |
| 88 | + // ─── WaitForOperatorHandshakeAsync ─── |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public async Task WaitForOperatorHandshakeAsync_ReturnsTrue_WhenAlreadyConnected() |
| 92 | + { |
| 93 | + var mgr = new StubConnectionManager(RoleConnectionState.Connected); |
| 94 | + var result = await ChatNavigationReadiness.WaitForOperatorHandshakeAsync(mgr, TimeSpan.FromMilliseconds(100)); |
| 95 | + Assert.True(result); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public async Task WaitForOperatorHandshakeAsync_ReturnsTrue_WhenNullManager() |
| 100 | + { |
| 101 | + var result = await ChatNavigationReadiness.WaitForOperatorHandshakeAsync(null, TimeSpan.FromMilliseconds(100)); |
| 102 | + Assert.True(result); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public async Task WaitForOperatorHandshakeAsync_ReturnsFalse_OnTimeout() |
| 107 | + { |
| 108 | + var mgr = new StubConnectionManager(RoleConnectionState.Connecting); |
| 109 | + var result = await ChatNavigationReadiness.WaitForOperatorHandshakeAsync(mgr, TimeSpan.FromMilliseconds(50)); |
| 110 | + Assert.False(result); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public async Task WaitForOperatorHandshakeAsync_ReturnsTrue_WhenStateEventFires() |
| 115 | + { |
| 116 | + var mgr = new StubConnectionManager(RoleConnectionState.Connecting); |
| 117 | + |
| 118 | + // Fire the Connected state event after a short delay |
| 119 | + _ = Task.Run(async () => |
| 120 | + { |
| 121 | + await Task.Delay(20); |
| 122 | + mgr.SimulateConnected(); |
| 123 | + }); |
| 124 | + |
| 125 | + var result = await ChatNavigationReadiness.WaitForOperatorHandshakeAsync(mgr, TimeSpan.FromMilliseconds(500)); |
| 126 | + Assert.True(result); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public async Task WaitForOperatorHandshakeAsync_ThrowsOperationCanceled_WhenTokenCancelled() |
| 131 | + { |
| 132 | + var mgr = new StubConnectionManager(RoleConnectionState.Connecting); |
| 133 | + using var cts = new CancellationTokenSource(); |
| 134 | + cts.CancelAfter(30); |
| 135 | + |
| 136 | + await Assert.ThrowsAsync<OperationCanceledException>(() => |
| 137 | + ChatNavigationReadiness.WaitForOperatorHandshakeAsync(mgr, TimeSpan.FromSeconds(10), cts.Token)); |
| 138 | + } |
| 139 | + |
| 140 | + // ─── Stub ─── |
| 141 | + |
| 142 | + private sealed class StubConnectionManager : IGatewayConnectionManager |
| 143 | + { |
| 144 | + private RoleConnectionState _operatorState; |
| 145 | + |
| 146 | + public StubConnectionManager(RoleConnectionState operatorState) |
| 147 | + { |
| 148 | + _operatorState = operatorState; |
| 149 | + CurrentSnapshot = BuildSnapshot(operatorState); |
| 150 | + } |
| 151 | + |
| 152 | + public GatewayConnectionSnapshot CurrentSnapshot { get; private set; } |
| 153 | + public string? ActiveGatewayUrl => null; |
| 154 | + public IOperatorGatewayClient? OperatorClient => null; |
| 155 | + public ConnectionDiagnostics Diagnostics { get; } = new(); |
| 156 | + |
| 157 | + public event EventHandler<GatewayConnectionSnapshot>? StateChanged; |
| 158 | +#pragma warning disable CS0067 |
| 159 | + public event EventHandler<ConnectionDiagnosticEvent>? DiagnosticEvent; |
| 160 | + public event EventHandler<OperatorClientChangedEventArgs>? OperatorClientChanged; |
| 161 | +#pragma warning restore CS0067 |
| 162 | + |
| 163 | + public void SimulateConnected() |
| 164 | + { |
| 165 | + _operatorState = RoleConnectionState.Connected; |
| 166 | + CurrentSnapshot = BuildSnapshot(RoleConnectionState.Connected); |
| 167 | + StateChanged?.Invoke(this, CurrentSnapshot); |
| 168 | + } |
| 169 | + |
| 170 | + private static GatewayConnectionSnapshot BuildSnapshot(RoleConnectionState op) => new() |
| 171 | + { |
| 172 | + OperatorState = op, |
| 173 | + NodeState = RoleConnectionState.Idle, |
| 174 | + OverallState = OverallConnectionState.Idle, |
| 175 | + NodePairingStatus = PairingStatus.Unknown |
| 176 | + }; |
| 177 | + |
| 178 | + public Task ConnectAsync(string? gatewayId = null) => Task.CompletedTask; |
| 179 | + public Task DisconnectAsync() => Task.CompletedTask; |
| 180 | + public Task ReconnectAsync() => Task.CompletedTask; |
| 181 | + public Task SwitchGatewayAsync(string gatewayId) => Task.CompletedTask; |
| 182 | + public Task EnsureNodeConnectedAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; |
| 183 | + public Task<SetupCodeResult> ApplySetupCodeAsync(string setupCode) => Task.FromResult(new SetupCodeResult(SetupCodeOutcome.InvalidCode)); |
| 184 | + public Task<SetupCodeResult> ConnectWithSharedTokenAsync(string gatewayUrl, string token, SshTunnelConfig? sshTunnel = null) => Task.FromResult(new SetupCodeResult(SetupCodeOutcome.InvalidCode)); |
| 185 | + public void Dispose() { } |
| 186 | + } |
| 187 | +} |
0 commit comments