2025-12-14 05:05:13 +00:00
|
|
|
import Foundation
|
2026-03-02 04:15:43 +00:00
|
|
|
import OpenClawProtocol
|
2025-12-14 05:05:13 +00:00
|
|
|
import Testing
|
2026-01-30 03:15:10 +01:00
|
|
|
@testable import OpenClaw
|
2025-12-14 05:05:13 +00:00
|
|
|
|
|
|
|
|
@Suite(.serialized) struct VoiceWakeGlobalSettingsSyncTests {
|
2026-03-02 09:55:34 +00:00
|
|
|
private func voiceWakeChangedEvent(payload: OpenClawProtocol.AnyCodable) -> EventFrame {
|
|
|
|
|
EventFrame(
|
2025-12-14 05:05:13 +00:00
|
|
|
type: "event",
|
|
|
|
|
event: "voicewake.changed",
|
|
|
|
|
payload: payload,
|
|
|
|
|
seq: nil,
|
|
|
|
|
stateversion: nil)
|
2026-03-02 09:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func applyTriggersAndCapturePrevious(_ triggers: [String]) async -> [String] {
|
|
|
|
|
let previous = await MainActor.run { AppStateStore.shared.swabbleTriggerWords }
|
|
|
|
|
await MainActor.run {
|
|
|
|
|
AppStateStore.shared.applyGlobalVoiceWakeTriggers(triggers)
|
|
|
|
|
}
|
|
|
|
|
return previous
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 13:22:46 +00:00
|
|
|
@Test func `applies voice wake changed event to app state`() async {
|
2026-03-02 09:55:34 +00:00
|
|
|
let previous = await applyTriggersAndCapturePrevious(["before"])
|
2026-03-08 13:22:46 +00:00
|
|
|
let evt = self.voiceWakeChangedEvent(payload: OpenClawProtocol.AnyCodable(["triggers": [
|
|
|
|
|
"openclaw",
|
|
|
|
|
"computer",
|
|
|
|
|
]]))
|
2025-12-14 05:05:13 +00:00
|
|
|
|
|
|
|
|
await VoiceWakeGlobalSettingsSync.shared.handle(push: .event(evt))
|
|
|
|
|
|
|
|
|
|
let updated = await MainActor.run { AppStateStore.shared.swabbleTriggerWords }
|
2026-01-30 03:15:10 +01:00
|
|
|
#expect(updated == ["openclaw", "computer"])
|
2025-12-14 05:05:13 +00:00
|
|
|
|
|
|
|
|
await MainActor.run {
|
|
|
|
|
AppStateStore.shared.applyGlobalVoiceWakeTriggers(previous)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 13:22:46 +00:00
|
|
|
@Test func `ignores voice wake changed event with invalid payload`() async {
|
2026-03-02 09:55:34 +00:00
|
|
|
let previous = await applyTriggersAndCapturePrevious(["before"])
|
2026-03-08 13:22:46 +00:00
|
|
|
let evt = self.voiceWakeChangedEvent(payload: OpenClawProtocol.AnyCodable(["unexpected": 123]))
|
2025-12-14 05:05:13 +00:00
|
|
|
|
|
|
|
|
await VoiceWakeGlobalSettingsSync.shared.handle(push: .event(evt))
|
|
|
|
|
|
|
|
|
|
let updated = await MainActor.run { AppStateStore.shared.swabbleTriggerWords }
|
|
|
|
|
#expect(updated == ["before"])
|
|
|
|
|
|
|
|
|
|
await MainActor.run {
|
|
|
|
|
AppStateStore.shared.applyGlobalVoiceWakeTriggers(previous)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|