test(iOS): update gateway tests for Keychain-backed storage

(cherry picked from commit d08960aef8)
This commit is contained in:
Rocuts
2026-03-02 12:31:27 -05:00
committed by mbelinky
parent 0022732c70
commit 40f54bcdcd
2 changed files with 52 additions and 27 deletions

View File

@@ -71,18 +71,37 @@ import UIKit
} }
@Test @MainActor func loadLastConnectionReadsSavedValues() { @Test @MainActor func loadLastConnectionReadsSavedValues() {
withUserDefaults([:]) { let prior = KeychainStore.loadString(service: "ai.openclaw.gateway", account: "lastConnection")
GatewaySettingsStore.saveLastGatewayConnectionManual( defer {
host: "gateway.example.com", if let prior {
port: 443, _ = KeychainStore.saveString(prior, service: "ai.openclaw.gateway", account: "lastConnection")
useTLS: true, } else {
stableID: "manual|gateway.example.com|443") _ = KeychainStore.delete(service: "ai.openclaw.gateway", account: "lastConnection")
let loaded = GatewaySettingsStore.loadLastGatewayConnection() }
#expect(loaded == .manual(host: "gateway.example.com", port: 443, useTLS: true, stableID: "manual|gateway.example.com|443"))
} }
_ = KeychainStore.delete(service: "ai.openclaw.gateway", account: "lastConnection")
GatewaySettingsStore.saveLastGatewayConnectionManual(
host: "gateway.example.com",
port: 443,
useTLS: true,
stableID: "manual|gateway.example.com|443")
let loaded = GatewaySettingsStore.loadLastGatewayConnection()
#expect(loaded == .manual(host: "gateway.example.com", port: 443, useTLS: true, stableID: "manual|gateway.example.com|443"))
} }
@Test @MainActor func loadLastConnectionReturnsNilForInvalidData() { @Test @MainActor func loadLastConnectionReturnsNilForInvalidData() {
let prior = KeychainStore.loadString(service: "ai.openclaw.gateway", account: "lastConnection")
defer {
if let prior {
_ = KeychainStore.saveString(prior, service: "ai.openclaw.gateway", account: "lastConnection")
} else {
_ = KeychainStore.delete(service: "ai.openclaw.gateway", account: "lastConnection")
}
}
_ = KeychainStore.delete(service: "ai.openclaw.gateway", account: "lastConnection")
// Plant legacy UserDefaults with invalid host/port to exercise migration + validation.
withUserDefaults([ withUserDefaults([
"gateway.last.kind": "manual", "gateway.last.kind": "manual",
"gateway.last.host": "", "gateway.last.host": "",

View File

@@ -27,6 +27,7 @@ private let lastGatewayDefaultsKeys = [
"gateway.last.tls", "gateway.last.tls",
"gateway.last.stableID", "gateway.last.stableID",
] ]
private let lastGatewayKeychainEntry = KeychainEntry(service: gatewayService, account: "lastConnection")
private func snapshotDefaults(_ keys: [String]) -> [String: Any?] { private func snapshotDefaults(_ keys: [String]) -> [String: Any?] {
let defaults = UserDefaults.standard let defaults = UserDefaults.standard
@@ -84,9 +85,13 @@ private func withBootstrapSnapshots(_ body: () -> Void) {
body() body()
} }
private func withLastGatewayDefaultsSnapshot(_ body: () -> Void) { private func withLastGatewaySnapshot(_ body: () -> Void) {
let snapshot = snapshotDefaults(lastGatewayDefaultsKeys) let defaultsSnapshot = snapshotDefaults(lastGatewayDefaultsKeys)
defer { restoreDefaults(snapshot) } let keychainSnapshot = snapshotKeychain([lastGatewayKeychainEntry])
defer {
restoreDefaults(defaultsSnapshot)
restoreKeychain(keychainSnapshot)
}
body() body()
} }
@@ -135,7 +140,7 @@ private func withLastGatewayDefaultsSnapshot(_ body: () -> Void) {
} }
@Test func lastGateway_manualRoundTrip() { @Test func lastGateway_manualRoundTrip() {
withLastGatewayDefaultsSnapshot { withLastGatewaySnapshot {
GatewaySettingsStore.saveLastGatewayConnectionManual( GatewaySettingsStore.saveLastGatewayConnectionManual(
host: "example.com", host: "example.com",
port: 443, port: 443,
@@ -147,28 +152,24 @@ private func withLastGatewayDefaultsSnapshot(_ body: () -> Void) {
} }
} }
@Test func lastGateway_discoveredDoesNotPersistResolvedHostPort() { @Test func lastGateway_discoveredOverwritesManual() {
withLastGatewayDefaultsSnapshot { withLastGatewaySnapshot {
// Simulate a prior manual record that included host/port. GatewaySettingsStore.saveLastGatewayConnectionManual(
applyDefaults([ host: "10.0.0.99",
"gateway.last.host": "10.0.0.99", port: 18789,
"gateway.last.port": 18789, useTLS: true,
"gateway.last.tls": true, stableID: "manual|10.0.0.99|18789")
"gateway.last.stableID": "manual|10.0.0.99|18789",
"gateway.last.kind": "manual",
])
GatewaySettingsStore.saveLastGatewayConnectionDiscovered(stableID: "gw|abc", useTLS: true) GatewaySettingsStore.saveLastGatewayConnectionDiscovered(stableID: "gw|abc", useTLS: true)
let defaults = UserDefaults.standard
#expect(defaults.object(forKey: "gateway.last.host") == nil)
#expect(defaults.object(forKey: "gateway.last.port") == nil)
#expect(GatewaySettingsStore.loadLastGatewayConnection() == .discovered(stableID: "gw|abc", useTLS: true)) #expect(GatewaySettingsStore.loadLastGatewayConnection() == .discovered(stableID: "gw|abc", useTLS: true))
} }
} }
@Test func lastGateway_backCompat_manualLoadsWhenKindMissing() { @Test func lastGateway_migratesFromUserDefaults() {
withLastGatewayDefaultsSnapshot { withLastGatewaySnapshot {
// Clear Keychain entry and plant legacy UserDefaults values.
applyKeychain([lastGatewayKeychainEntry: nil])
applyDefaults([ applyDefaults([
"gateway.last.kind": nil, "gateway.last.kind": nil,
"gateway.last.host": "example.org", "gateway.last.host": "example.org",
@@ -179,6 +180,11 @@ private func withLastGatewayDefaultsSnapshot(_ body: () -> Void) {
let loaded = GatewaySettingsStore.loadLastGatewayConnection() let loaded = GatewaySettingsStore.loadLastGatewayConnection()
#expect(loaded == .manual(host: "example.org", port: 18789, useTLS: false, stableID: "manual|example.org|18789")) #expect(loaded == .manual(host: "example.org", port: 18789, useTLS: false, stableID: "manual|example.org|18789"))
// Legacy keys should be cleaned up after migration.
let defaults = UserDefaults.standard
#expect(defaults.object(forKey: "gateway.last.stableID") == nil)
#expect(defaults.object(forKey: "gateway.last.host") == nil)
} }
} }