mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: 31a27b0c5b
Co-authored-by: mbelinky <132747814+mbelinky@users.noreply.github.com>
Co-authored-by: mbelinky <132747814+mbelinky@users.noreply.github.com>
Reviewed-by: @mbelinky
332 lines
13 KiB
Swift
332 lines
13 KiB
Swift
import OpenClawKit
|
|
import Foundation
|
|
import Testing
|
|
import UIKit
|
|
@testable import OpenClaw
|
|
|
|
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()
|
|
}
|
|
|
|
@MainActor
|
|
private final class MockWatchMessagingService: WatchMessagingServicing, @unchecked Sendable {
|
|
var currentStatus = WatchMessagingStatus(
|
|
supported: true,
|
|
paired: true,
|
|
appInstalled: true,
|
|
reachable: true,
|
|
activationState: "activated")
|
|
var nextSendResult = WatchNotificationSendResult(
|
|
deliveredImmediately: true,
|
|
queuedForDelivery: false,
|
|
transport: "sendMessage")
|
|
var sendError: Error?
|
|
var lastSent: (id: String, title: String, body: String, priority: OpenClawNotificationPriority?)?
|
|
|
|
func status() async -> WatchMessagingStatus {
|
|
self.currentStatus
|
|
}
|
|
|
|
func sendNotification(
|
|
id: String,
|
|
title: String,
|
|
body: String,
|
|
priority: OpenClawNotificationPriority?) async throws -> WatchNotificationSendResult
|
|
{
|
|
self.lastSent = (id: id, title: title, body: body, priority: priority)
|
|
if let sendError = self.sendError {
|
|
throw sendError
|
|
}
|
|
return self.nextSendResult
|
|
}
|
|
}
|
|
|
|
@Suite(.serialized) struct NodeAppModelInvokeTests {
|
|
@Test @MainActor func decodeParamsFailsWithoutJSON() {
|
|
#expect(throws: Error.self) {
|
|
_ = try NodeAppModel._test_decodeParams(OpenClawCanvasNavigateParams.self, from: nil)
|
|
}
|
|
}
|
|
|
|
@Test @MainActor func encodePayloadEmitsJSON() throws {
|
|
struct Payload: Codable, Equatable {
|
|
var value: String
|
|
}
|
|
let json = try NodeAppModel._test_encodePayload(Payload(value: "ok"))
|
|
#expect(json.contains("\"value\""))
|
|
}
|
|
|
|
@Test @MainActor func chatSessionKeyDefaultsToIOSBase() {
|
|
let appModel = NodeAppModel()
|
|
#expect(appModel.chatSessionKey == "ios")
|
|
}
|
|
|
|
@Test @MainActor func chatSessionKeyUsesAgentScopedKeyForNonDefaultAgent() {
|
|
let appModel = NodeAppModel()
|
|
appModel.gatewayDefaultAgentId = "main"
|
|
appModel.setSelectedAgentId("agent-123")
|
|
#expect(appModel.chatSessionKey == SessionKey.makeAgentSessionKey(agentId: "agent-123", baseKey: "ios"))
|
|
#expect(appModel.mainSessionKey == "agent:agent-123:main")
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeRejectsBackgroundCommands() async {
|
|
let appModel = NodeAppModel()
|
|
appModel.setScenePhase(.background)
|
|
|
|
let req = BridgeInvokeRequest(id: "bg", command: OpenClawCanvasCommand.present.rawValue)
|
|
let res = await appModel._test_handleInvoke(req)
|
|
#expect(res.ok == false)
|
|
#expect(res.error?.code == .backgroundUnavailable)
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeRejectsCameraWhenDisabled() async {
|
|
let appModel = NodeAppModel()
|
|
let req = BridgeInvokeRequest(id: "cam", command: OpenClawCameraCommand.snap.rawValue)
|
|
|
|
let defaults = UserDefaults.standard
|
|
let key = "camera.enabled"
|
|
let previous = defaults.object(forKey: key)
|
|
defaults.set(false, forKey: key)
|
|
defer {
|
|
if let previous {
|
|
defaults.set(previous, forKey: key)
|
|
} else {
|
|
defaults.removeObject(forKey: key)
|
|
}
|
|
}
|
|
|
|
let res = await appModel._test_handleInvoke(req)
|
|
#expect(res.ok == false)
|
|
#expect(res.error?.code == .unavailable)
|
|
#expect(res.error?.message.contains("CAMERA_DISABLED") == true)
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeRejectsInvalidScreenFormat() async {
|
|
let appModel = NodeAppModel()
|
|
let params = OpenClawScreenRecordParams(format: "gif")
|
|
let data = try? JSONEncoder().encode(params)
|
|
let json = data.flatMap { String(data: $0, encoding: .utf8) }
|
|
|
|
let req = BridgeInvokeRequest(
|
|
id: "screen",
|
|
command: OpenClawScreenCommand.record.rawValue,
|
|
paramsJSON: json)
|
|
|
|
let res = await appModel._test_handleInvoke(req)
|
|
#expect(res.ok == false)
|
|
#expect(res.error?.message.contains("screen format must be mp4") == true)
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeCanvasCommandsUpdateScreen() async throws {
|
|
let appModel = NodeAppModel()
|
|
appModel.screen.navigate(to: "http://example.com")
|
|
|
|
let present = BridgeInvokeRequest(id: "present", command: OpenClawCanvasCommand.present.rawValue)
|
|
let presentRes = await appModel._test_handleInvoke(present)
|
|
#expect(presentRes.ok == true)
|
|
#expect(appModel.screen.urlString.isEmpty)
|
|
|
|
// Loopback URLs are rejected (they are not meaningful for a remote gateway).
|
|
let navigateParams = OpenClawCanvasNavigateParams(url: "http://example.com/")
|
|
let navData = try JSONEncoder().encode(navigateParams)
|
|
let navJSON = String(decoding: navData, as: UTF8.self)
|
|
let navigate = BridgeInvokeRequest(
|
|
id: "nav",
|
|
command: OpenClawCanvasCommand.navigate.rawValue,
|
|
paramsJSON: navJSON)
|
|
let navRes = await appModel._test_handleInvoke(navigate)
|
|
#expect(navRes.ok == true)
|
|
#expect(appModel.screen.urlString == "http://example.com/")
|
|
|
|
let evalParams = OpenClawCanvasEvalParams(javaScript: "1+1")
|
|
let evalData = try JSONEncoder().encode(evalParams)
|
|
let evalJSON = String(decoding: evalData, as: UTF8.self)
|
|
let eval = BridgeInvokeRequest(
|
|
id: "eval",
|
|
command: OpenClawCanvasCommand.evalJS.rawValue,
|
|
paramsJSON: evalJSON)
|
|
let evalRes = await appModel._test_handleInvoke(eval)
|
|
#expect(evalRes.ok == true)
|
|
let payloadData = try #require(evalRes.payloadJSON?.data(using: .utf8))
|
|
let payload = try JSONSerialization.jsonObject(with: payloadData) as? [String: Any]
|
|
#expect(payload?["result"] as? String == "2")
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeA2UICommandsFailWhenHostMissing() async throws {
|
|
let appModel = NodeAppModel()
|
|
|
|
let reset = BridgeInvokeRequest(id: "reset", command: OpenClawCanvasA2UICommand.reset.rawValue)
|
|
let resetRes = await appModel._test_handleInvoke(reset)
|
|
#expect(resetRes.ok == false)
|
|
#expect(resetRes.error?.message.contains("A2UI_HOST_NOT_CONFIGURED") == true)
|
|
|
|
let jsonl = "{\"beginRendering\":{}}"
|
|
let pushParams = OpenClawCanvasA2UIPushJSONLParams(jsonl: jsonl)
|
|
let pushData = try JSONEncoder().encode(pushParams)
|
|
let pushJSON = String(decoding: pushData, as: UTF8.self)
|
|
let push = BridgeInvokeRequest(
|
|
id: "push",
|
|
command: OpenClawCanvasA2UICommand.pushJSONL.rawValue,
|
|
paramsJSON: pushJSON)
|
|
let pushRes = await appModel._test_handleInvoke(push)
|
|
#expect(pushRes.ok == false)
|
|
#expect(pushRes.error?.message.contains("A2UI_HOST_NOT_CONFIGURED") == true)
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeUnknownCommandReturnsInvalidRequest() async {
|
|
let appModel = NodeAppModel()
|
|
let req = BridgeInvokeRequest(id: "unknown", command: "nope")
|
|
let res = await appModel._test_handleInvoke(req)
|
|
#expect(res.ok == false)
|
|
#expect(res.error?.code == .invalidRequest)
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeWatchStatusReturnsServiceSnapshot() async throws {
|
|
let watchService = MockWatchMessagingService()
|
|
watchService.currentStatus = WatchMessagingStatus(
|
|
supported: true,
|
|
paired: true,
|
|
appInstalled: true,
|
|
reachable: false,
|
|
activationState: "inactive")
|
|
let appModel = NodeAppModel(watchMessagingService: watchService)
|
|
let req = BridgeInvokeRequest(id: "watch-status", command: OpenClawWatchCommand.status.rawValue)
|
|
|
|
let res = await appModel._test_handleInvoke(req)
|
|
#expect(res.ok == true)
|
|
|
|
let payloadData = try #require(res.payloadJSON?.data(using: .utf8))
|
|
let payload = try JSONDecoder().decode(OpenClawWatchStatusPayload.self, from: payloadData)
|
|
#expect(payload.supported == true)
|
|
#expect(payload.reachable == false)
|
|
#expect(payload.activationState == "inactive")
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeWatchNotifyRoutesToWatchService() async throws {
|
|
let watchService = MockWatchMessagingService()
|
|
watchService.nextSendResult = WatchNotificationSendResult(
|
|
deliveredImmediately: false,
|
|
queuedForDelivery: true,
|
|
transport: "transferUserInfo")
|
|
let appModel = NodeAppModel(watchMessagingService: watchService)
|
|
let params = OpenClawWatchNotifyParams(
|
|
title: "OpenClaw",
|
|
body: "Meeting with Peter is at 4pm",
|
|
priority: .timeSensitive)
|
|
let paramsData = try JSONEncoder().encode(params)
|
|
let paramsJSON = String(decoding: paramsData, as: UTF8.self)
|
|
let req = BridgeInvokeRequest(
|
|
id: "watch-notify",
|
|
command: OpenClawWatchCommand.notify.rawValue,
|
|
paramsJSON: paramsJSON)
|
|
|
|
let res = await appModel._test_handleInvoke(req)
|
|
#expect(res.ok == true)
|
|
#expect(watchService.lastSent?.title == "OpenClaw")
|
|
#expect(watchService.lastSent?.body == "Meeting with Peter is at 4pm")
|
|
#expect(watchService.lastSent?.priority == .timeSensitive)
|
|
|
|
let payloadData = try #require(res.payloadJSON?.data(using: .utf8))
|
|
let payload = try JSONDecoder().decode(OpenClawWatchNotifyPayload.self, from: payloadData)
|
|
#expect(payload.deliveredImmediately == false)
|
|
#expect(payload.queuedForDelivery == true)
|
|
#expect(payload.transport == "transferUserInfo")
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeWatchNotifyRejectsEmptyMessage() async throws {
|
|
let watchService = MockWatchMessagingService()
|
|
let appModel = NodeAppModel(watchMessagingService: watchService)
|
|
let params = OpenClawWatchNotifyParams(title: " ", body: "\n")
|
|
let paramsData = try JSONEncoder().encode(params)
|
|
let paramsJSON = String(decoding: paramsData, as: UTF8.self)
|
|
let req = BridgeInvokeRequest(
|
|
id: "watch-notify-empty",
|
|
command: OpenClawWatchCommand.notify.rawValue,
|
|
paramsJSON: paramsJSON)
|
|
|
|
let res = await appModel._test_handleInvoke(req)
|
|
#expect(res.ok == false)
|
|
#expect(res.error?.code == .invalidRequest)
|
|
#expect(watchService.lastSent == nil)
|
|
}
|
|
|
|
@Test @MainActor func handleInvokeWatchNotifyReturnsUnavailableOnDeliveryFailure() async throws {
|
|
let watchService = MockWatchMessagingService()
|
|
watchService.sendError = NSError(
|
|
domain: "watch",
|
|
code: 1,
|
|
userInfo: [NSLocalizedDescriptionKey: "WATCH_UNAVAILABLE: no paired Apple Watch"])
|
|
let appModel = NodeAppModel(watchMessagingService: watchService)
|
|
let params = OpenClawWatchNotifyParams(title: "OpenClaw", body: "Delivery check")
|
|
let paramsData = try JSONEncoder().encode(params)
|
|
let paramsJSON = String(decoding: paramsData, as: UTF8.self)
|
|
let req = BridgeInvokeRequest(
|
|
id: "watch-notify-fail",
|
|
command: OpenClawWatchCommand.notify.rawValue,
|
|
paramsJSON: paramsJSON)
|
|
|
|
let res = await appModel._test_handleInvoke(req)
|
|
#expect(res.ok == false)
|
|
#expect(res.error?.code == .unavailable)
|
|
#expect(res.error?.message.contains("WATCH_UNAVAILABLE") == true)
|
|
}
|
|
|
|
@Test @MainActor func handleDeepLinkSetsErrorWhenNotConnected() async {
|
|
let appModel = NodeAppModel()
|
|
let url = URL(string: "openclaw://agent?message=hello")!
|
|
await appModel.handleDeepLink(url: url)
|
|
#expect(appModel.screen.errorText?.contains("Gateway not connected") == true)
|
|
}
|
|
|
|
@Test @MainActor func handleDeepLinkRejectsOversizedMessage() async {
|
|
let appModel = NodeAppModel()
|
|
let msg = String(repeating: "a", count: 20001)
|
|
let url = URL(string: "openclaw://agent?message=\(msg)")!
|
|
await appModel.handleDeepLink(url: url)
|
|
#expect(appModel.screen.errorText?.contains("Deep link too large") == true)
|
|
}
|
|
|
|
@Test @MainActor func sendVoiceTranscriptThrowsWhenGatewayOffline() async {
|
|
let appModel = NodeAppModel()
|
|
await #expect(throws: Error.self) {
|
|
try await appModel.sendVoiceTranscript(text: "hello", sessionKey: "main")
|
|
}
|
|
}
|
|
|
|
@Test @MainActor func canvasA2UIActionDispatchesStatus() async {
|
|
let appModel = NodeAppModel()
|
|
let body: [String: Any] = [
|
|
"userAction": [
|
|
"name": "tap",
|
|
"id": "action-1",
|
|
"surfaceId": "main",
|
|
"sourceComponentId": "button-1",
|
|
"context": ["value": "ok"],
|
|
],
|
|
]
|
|
await appModel._test_handleCanvasA2UIAction(body: body)
|
|
#expect(appModel.screen.urlString.isEmpty)
|
|
}
|
|
}
|