|
| 1 | +import Foundation |
| 2 | +import OpenClawDiscovery |
| 3 | +import Testing |
| 4 | +@testable import OpenClaw |
| 5 | + |
| 6 | +@Suite(.serialized) |
| 7 | +@MainActor |
| 8 | +struct GatewayDiscoverySelectionSupportTests { |
| 9 | + private func makeGateway( |
| 10 | + serviceHost: String?, |
| 11 | + servicePort: Int?, |
| 12 | + tailnetDns: String? = nil, |
| 13 | + sshPort: Int = 22, |
| 14 | + stableID: String) -> GatewayDiscoveryModel.DiscoveredGateway |
| 15 | + { |
| 16 | + GatewayDiscoveryModel.DiscoveredGateway( |
| 17 | + displayName: "Gateway", |
| 18 | + serviceHost: serviceHost, |
| 19 | + servicePort: servicePort, |
| 20 | + lanHost: nil, |
| 21 | + tailnetDns: tailnetDns, |
| 22 | + sshPort: sshPort, |
| 23 | + gatewayPort: servicePort, |
| 24 | + cliPath: nil, |
| 25 | + stableID: stableID, |
| 26 | + debugID: UUID().uuidString, |
| 27 | + isLocal: false) |
| 28 | + } |
| 29 | + |
| 30 | + @Test func `selecting tailscale serve gateway switches to direct transport`() async { |
| 31 | + let tailnetHost = "gateway-host.tailnet-example.ts.net" |
| 32 | + let configPath = TestIsolation.tempConfigPath() |
| 33 | + await TestIsolation.withEnvValues(["OPENCLAW_CONFIG_PATH": configPath]) { |
| 34 | + let state = AppState(preview: true) |
| 35 | + state.remoteTransport = .ssh |
| 36 | + state.remoteTarget = "user@old-host" |
| 37 | + |
| 38 | + GatewayDiscoverySelectionSupport.applyRemoteSelection( |
| 39 | + gateway: self.makeGateway( |
| 40 | + serviceHost: tailnetHost, |
| 41 | + servicePort: 443, |
| 42 | + tailnetDns: tailnetHost, |
| 43 | + stableID: "tailscale-serve|\(tailnetHost)"), |
| 44 | + state: state) |
| 45 | + |
| 46 | + #expect(state.remoteTransport == .direct) |
| 47 | + #expect(state.remoteUrl == "wss://\(tailnetHost)") |
| 48 | + #expect(CommandResolver.parseSSHTarget(state.remoteTarget)?.host == tailnetHost) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test func `selecting merged tailnet gateway still switches to direct transport`() async { |
| 53 | + let tailnetHost = "gateway-host.tailnet-example.ts.net" |
| 54 | + let configPath = TestIsolation.tempConfigPath() |
| 55 | + await TestIsolation.withEnvValues(["OPENCLAW_CONFIG_PATH": configPath]) { |
| 56 | + let state = AppState(preview: true) |
| 57 | + state.remoteTransport = .ssh |
| 58 | + |
| 59 | + GatewayDiscoverySelectionSupport.applyRemoteSelection( |
| 60 | + gateway: self.makeGateway( |
| 61 | + serviceHost: tailnetHost, |
| 62 | + servicePort: 443, |
| 63 | + tailnetDns: tailnetHost, |
| 64 | + stableID: "wide-area|openclaw.internal.|gateway-host"), |
| 65 | + state: state) |
| 66 | + |
| 67 | + #expect(state.remoteTransport == .direct) |
| 68 | + #expect(state.remoteUrl == "wss://\(tailnetHost)") |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test func `selecting nearby lan gateway keeps ssh transport`() async { |
| 73 | + let configPath = TestIsolation.tempConfigPath() |
| 74 | + await TestIsolation.withEnvValues(["OPENCLAW_CONFIG_PATH": configPath]) { |
| 75 | + let state = AppState(preview: true) |
| 76 | + state.remoteTransport = .ssh |
| 77 | + state.remoteTarget = "user@old-host" |
| 78 | + |
| 79 | + GatewayDiscoverySelectionSupport.applyRemoteSelection( |
| 80 | + gateway: self.makeGateway( |
| 81 | + serviceHost: "nearby-gateway.local", |
| 82 | + servicePort: 18789, |
| 83 | + stableID: "bonjour|nearby-gateway"), |
| 84 | + state: state) |
| 85 | + |
| 86 | + #expect(state.remoteTransport == .ssh) |
| 87 | + #expect(CommandResolver.parseSSHTarget(state.remoteTarget)?.host == "nearby-gateway.local") |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments