From 500883775baea3698531fc9e4a25fe2e4ee1d81b Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 2 Mar 2026 09:39:34 +0000 Subject: [PATCH] refactor(tests): dedupe ios defaults and setup-code helpers --- apps/ios/Tests/DeepLinkParserTests.swift | 48 +++++-------------- .../GatewayConnectionControllerTests.swift | 25 ---------- apps/ios/Tests/NodeAppModelInvokeTests.swift | 25 ---------- apps/ios/Tests/TestDefaultsSupport.swift | 26 ++++++++++ 4 files changed, 39 insertions(+), 85 deletions(-) create mode 100644 apps/ios/Tests/TestDefaultsSupport.swift diff --git a/apps/ios/Tests/DeepLinkParserTests.swift b/apps/ios/Tests/DeepLinkParserTests.swift index 51ef9547a10..faaf0518d30 100644 --- a/apps/ios/Tests/DeepLinkParserTests.swift +++ b/apps/ios/Tests/DeepLinkParserTests.swift @@ -2,6 +2,14 @@ import OpenClawKit import Foundation import Testing +private func setupCode(from payload: String) -> String { + Data(payload.utf8) + .base64EncodedString() + .replacingOccurrences(of: "+", with: "-") + .replacingOccurrences(of: "/", with: "_") + .replacingOccurrences(of: "=", with: "") +} + @Suite struct DeepLinkParserTests { @Test func parseRejectsUnknownHost() { let url = URL(string: "openclaw://nope?message=hi")! @@ -99,13 +107,7 @@ import Testing @Test func parseGatewaySetupCodeParsesBase64UrlPayload() { let payload = #"{"url":"wss://gateway.example.com:443","token":"tok","password":"pw"}"# - let encoded = Data(payload.utf8) - .base64EncodedString() - .replacingOccurrences(of: "+", with: "-") - .replacingOccurrences(of: "/", with: "_") - .replacingOccurrences(of: "=", with: "") - - let link = GatewayConnectDeepLink.fromSetupCode(encoded) + let link = GatewayConnectDeepLink.fromSetupCode(setupCode(from: payload)) #expect(link == .init( host: "gateway.example.com", @@ -121,13 +123,7 @@ import Testing @Test func parseGatewaySetupCodeDefaultsTo443ForWssWithoutPort() { let payload = #"{"url":"wss://gateway.example.com","token":"tok"}"# - let encoded = Data(payload.utf8) - .base64EncodedString() - .replacingOccurrences(of: "+", with: "-") - .replacingOccurrences(of: "/", with: "_") - .replacingOccurrences(of: "=", with: "") - - let link = GatewayConnectDeepLink.fromSetupCode(encoded) + let link = GatewayConnectDeepLink.fromSetupCode(setupCode(from: payload)) #expect(link == .init( host: "gateway.example.com", @@ -139,37 +135,19 @@ import Testing @Test func parseGatewaySetupCodeRejectsInsecureNonLoopbackWs() { let payload = #"{"url":"ws://attacker.example:18789","token":"tok"}"# - let encoded = Data(payload.utf8) - .base64EncodedString() - .replacingOccurrences(of: "+", with: "-") - .replacingOccurrences(of: "/", with: "_") - .replacingOccurrences(of: "=", with: "") - - let link = GatewayConnectDeepLink.fromSetupCode(encoded) + let link = GatewayConnectDeepLink.fromSetupCode(setupCode(from: payload)) #expect(link == nil) } @Test func parseGatewaySetupCodeRejectsInsecurePrefixBypassHost() { let payload = #"{"url":"ws://127.attacker.example:18789","token":"tok"}"# - let encoded = Data(payload.utf8) - .base64EncodedString() - .replacingOccurrences(of: "+", with: "-") - .replacingOccurrences(of: "/", with: "_") - .replacingOccurrences(of: "=", with: "") - - let link = GatewayConnectDeepLink.fromSetupCode(encoded) + let link = GatewayConnectDeepLink.fromSetupCode(setupCode(from: payload)) #expect(link == nil) } @Test func parseGatewaySetupCodeAllowsLoopbackWs() { let payload = #"{"url":"ws://127.0.0.1:18789","token":"tok"}"# - let encoded = Data(payload.utf8) - .base64EncodedString() - .replacingOccurrences(of: "+", with: "-") - .replacingOccurrences(of: "/", with: "_") - .replacingOccurrences(of: "=", with: "") - - let link = GatewayConnectDeepLink.fromSetupCode(encoded) + let link = GatewayConnectDeepLink.fromSetupCode(setupCode(from: payload)) #expect(link == .init( host: "127.0.0.1", diff --git a/apps/ios/Tests/GatewayConnectionControllerTests.swift b/apps/ios/Tests/GatewayConnectionControllerTests.swift index 27e7aed7aea..5559e42086e 100644 --- a/apps/ios/Tests/GatewayConnectionControllerTests.swift +++ b/apps/ios/Tests/GatewayConnectionControllerTests.swift @@ -4,31 +4,6 @@ import Testing import UIKit @testable import OpenClaw -private func withUserDefaults(_ updates: [String: Any?], _ body: () throws -> T) rethrows -> T { - let defaults = UserDefaults.standard - var snapshot: [String: Any?] = [:] - for key in updates.keys { - snapshot[key] = defaults.object(forKey: key) - } - for (key, value) in updates { - if let value { - defaults.set(value, forKey: key) - } else { - defaults.removeObject(forKey: key) - } - } - defer { - for (key, value) in snapshot { - if let value { - defaults.set(value, forKey: key) - } else { - defaults.removeObject(forKey: key) - } - } - } - return try body() -} - @Suite(.serialized) struct GatewayConnectionControllerTests { @Test @MainActor func resolvedDisplayNameSetsDefaultWhenMissing() { let defaults = UserDefaults.standard diff --git a/apps/ios/Tests/NodeAppModelInvokeTests.swift b/apps/ios/Tests/NodeAppModelInvokeTests.swift index dbeee118a4a..c12c9727874 100644 --- a/apps/ios/Tests/NodeAppModelInvokeTests.swift +++ b/apps/ios/Tests/NodeAppModelInvokeTests.swift @@ -4,31 +4,6 @@ import Testing import UIKit @testable import OpenClaw -private func withUserDefaults(_ updates: [String: Any?], _ body: () throws -> T) rethrows -> T { - let defaults = UserDefaults.standard - var snapshot: [String: Any?] = [:] - for key in updates.keys { - snapshot[key] = defaults.object(forKey: key) - } - for (key, value) in updates { - if let value { - defaults.set(value, forKey: key) - } else { - defaults.removeObject(forKey: key) - } - } - defer { - for (key, value) in snapshot { - if let value { - defaults.set(value, forKey: key) - } else { - defaults.removeObject(forKey: key) - } - } - } - return try body() -} - private func makeAgentDeepLinkURL( message: String, deliver: Bool = false, diff --git a/apps/ios/Tests/TestDefaultsSupport.swift b/apps/ios/Tests/TestDefaultsSupport.swift new file mode 100644 index 00000000000..75fd2344aa3 --- /dev/null +++ b/apps/ios/Tests/TestDefaultsSupport.swift @@ -0,0 +1,26 @@ +import Foundation + +func withUserDefaults(_ updates: [String: Any?], _ body: () throws -> T) rethrows -> T { + let defaults = UserDefaults.standard + var snapshot: [String: Any?] = [:] + for key in updates.keys { + snapshot[key] = defaults.object(forKey: key) + } + for (key, value) in updates { + if let value { + defaults.set(value, forKey: key) + } else { + defaults.removeObject(forKey: key) + } + } + defer { + for (key, value) in snapshot { + if let value { + defaults.set(value, forKey: key) + } else { + defaults.removeObject(forKey: key) + } + } + } + return try body() +}