mirror of
https://github.com/hanzoai/bot.git
synced 2026-10-30 17:24:26 +00:00
Comprehensive rename of all openclaw references to hanzo-bot equivalents: - Package: openclaw → @hanzo/bot - Binary: openclaw → hanzo-bot - Types: OpenClaw* → Bot* - Env vars: OPENCLAW_* → BOT_* - Android: ai.openclaw.android → ai.hanzo.bot.android - iOS/macOS: ai.openclaw.* → ai.hanzo.* - Swift modules: OpenClaw* → Bot* - Plugins: openclaw.plugin.json → bot.plugin.json - Paths: .openclaw/ → .hanzo/bot/ - URLs: github.com/openclaw → github.com/hanzoai
123 lines
4.8 KiB
Swift
123 lines
4.8 KiB
Swift
import BotKit
|
|
import Foundation
|
|
import Testing
|
|
import UIKit
|
|
@testable import HanzoBot
|
|
|
|
private func withUserDefaults<T>(_ 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
|
|
let displayKey = "node.displayName"
|
|
|
|
withUserDefaults([displayKey: nil, "node.instanceId": "ios-test"]) {
|
|
let appModel = NodeAppModel()
|
|
let controller = GatewayConnectionController(appModel: appModel, startDiscovery: false)
|
|
|
|
let resolved = controller._test_resolvedDisplayName(defaults: defaults)
|
|
#expect(!resolved.isEmpty)
|
|
#expect(defaults.string(forKey: displayKey) == resolved)
|
|
}
|
|
}
|
|
|
|
@Test @MainActor func currentCapsReflectToggles() {
|
|
withUserDefaults([
|
|
"node.instanceId": "ios-test",
|
|
"node.displayName": "Test Node",
|
|
"camera.enabled": true,
|
|
"location.enabledMode": HanzoBotLocationMode.always.rawValue,
|
|
VoiceWakePreferences.enabledKey: true,
|
|
]) {
|
|
let appModel = NodeAppModel()
|
|
let controller = GatewayConnectionController(appModel: appModel, startDiscovery: false)
|
|
let caps = Set(controller._test_currentCaps())
|
|
|
|
#expect(caps.contains(HanzoBotCapability.canvas.rawValue))
|
|
#expect(caps.contains(HanzoBotCapability.screen.rawValue))
|
|
#expect(caps.contains(HanzoBotCapability.camera.rawValue))
|
|
#expect(caps.contains(HanzoBotCapability.location.rawValue))
|
|
#expect(caps.contains(HanzoBotCapability.voiceWake.rawValue))
|
|
}
|
|
}
|
|
|
|
@Test @MainActor func currentCommandsIncludeLocationWhenEnabled() {
|
|
withUserDefaults([
|
|
"node.instanceId": "ios-test",
|
|
"location.enabledMode": HanzoBotLocationMode.whileUsing.rawValue,
|
|
]) {
|
|
let appModel = NodeAppModel()
|
|
let controller = GatewayConnectionController(appModel: appModel, startDiscovery: false)
|
|
let commands = Set(controller._test_currentCommands())
|
|
|
|
#expect(commands.contains(HanzoBotLocationCommand.get.rawValue))
|
|
}
|
|
}
|
|
@Test @MainActor func currentCommandsExcludeDangerousSystemExecCommands() {
|
|
withUserDefaults([
|
|
"node.instanceId": "ios-test",
|
|
"camera.enabled": true,
|
|
"location.enabledMode": BotLocationMode.whileUsing.rawValue,
|
|
]) {
|
|
let appModel = NodeAppModel()
|
|
let controller = GatewayConnectionController(appModel: appModel, startDiscovery: false)
|
|
let commands = Set(controller._test_currentCommands())
|
|
|
|
// iOS should expose notify, but not host shell/exec-approval commands.
|
|
#expect(commands.contains(BotSystemCommand.notify.rawValue))
|
|
#expect(!commands.contains(BotSystemCommand.run.rawValue))
|
|
#expect(!commands.contains(BotSystemCommand.which.rawValue))
|
|
#expect(!commands.contains(BotSystemCommand.execApprovalsGet.rawValue))
|
|
#expect(!commands.contains(BotSystemCommand.execApprovalsSet.rawValue))
|
|
}
|
|
}
|
|
|
|
@Test @MainActor func loadLastConnectionReadsSavedValues() {
|
|
withUserDefaults([:]) {
|
|
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() {
|
|
withUserDefaults([
|
|
"gateway.last.kind": "manual",
|
|
"gateway.last.host": "",
|
|
"gateway.last.port": 0,
|
|
"gateway.last.tls": false,
|
|
"gateway.last.stableID": "manual|invalid|0",
|
|
]) {
|
|
let loaded = GatewaySettingsStore.loadLastGatewayConnection()
|
|
#expect(loaded == nil)
|
|
}
|
|
}
|
|
}
|