|
| 1 | +using System.Reflection; |
| 2 | +using OpenClaw.Shared; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace OpenClaw.Connection.Tests; |
| 6 | + |
| 7 | +public sealed class GatewayClientFactoryTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void Create_BootstrapCredential_PairsAsNode() |
| 11 | + { |
| 12 | + var tempDir = Path.Combine(Path.GetTempPath(), "openclaw-gateway-factory-" + Guid.NewGuid().ToString("N")); |
| 13 | + Directory.CreateDirectory(tempDir); |
| 14 | + |
| 15 | + try |
| 16 | + { |
| 17 | + using var lifecycle = new GatewayClientFactory().Create( |
| 18 | + "ws://127.0.0.1:18789", |
| 19 | + new GatewayCredential("bootstrap-token", IsBootstrapToken: true, Source: "test"), |
| 20 | + tempDir, |
| 21 | + NullLogger.Instance); |
| 22 | + |
| 23 | + Assert.Equal("node", GetConnectRole(lifecycle.DataClient)); |
| 24 | + } |
| 25 | + finally |
| 26 | + { |
| 27 | + Directory.Delete(tempDir, recursive: true); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void Create_SharedCredential_PairsAsOperator() |
| 33 | + { |
| 34 | + var tempDir = Path.Combine(Path.GetTempPath(), "openclaw-gateway-factory-" + Guid.NewGuid().ToString("N")); |
| 35 | + Directory.CreateDirectory(tempDir); |
| 36 | + |
| 37 | + try |
| 38 | + { |
| 39 | + using var lifecycle = new GatewayClientFactory().Create( |
| 40 | + "ws://127.0.0.1:18789", |
| 41 | + new GatewayCredential("shared-token", IsBootstrapToken: false, Source: "test"), |
| 42 | + tempDir, |
| 43 | + NullLogger.Instance); |
| 44 | + |
| 45 | + Assert.Equal("operator", GetConnectRole(lifecycle.DataClient)); |
| 46 | + } |
| 47 | + finally |
| 48 | + { |
| 49 | + Directory.Delete(tempDir, recursive: true); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static string GetConnectRole(OpenClawGatewayClient client) |
| 54 | + { |
| 55 | + var method = typeof(OpenClawGatewayClient).GetMethod( |
| 56 | + "GetConnectRole", |
| 57 | + BindingFlags.Instance | BindingFlags.NonPublic); |
| 58 | + |
| 59 | + Assert.NotNull(method); |
| 60 | + return Assert.IsType<string>(method.Invoke(client, null)); |
| 61 | + } |
| 62 | +} |
0 commit comments