oclaw/openclaw/apps/macos/Tests/OpenClawIPCTests/GatewayFrameDecodeTests.swift
oliver dbbe3add6a 重构主控编排与运行时预热链路,统一工作区提示词/专家调度协议并补齐 wiki 记忆注入与写回闭环。
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。

Made-with: Cursor
2026-04-26 08:34:33 +08:00

98 lines
2.8 KiB
Swift

import Foundation
import OpenClawProtocol
import Testing
struct GatewayFrameDecodeTests {
@Test func `decodes event frame with any codable payload`() throws {
let json = """
{
"type": "event",
"event": "presence",
"payload": { "foo": "bar", "count": 1 },
"seq": 7
}
"""
let frame = try JSONDecoder().decode(GatewayFrame.self, from: Data(json.utf8))
#expect({
if case .event = frame { true } else { false }
}(), "expected .event frame")
guard case let .event(evt) = frame else {
return
}
let payload = evt.payload?.value as? [String: AnyCodable]
#expect(payload?["foo"]?.value as? String == "bar")
#expect(payload?["count"]?.value as? Int == 1)
#expect(evt.seq == 7)
}
@Test func `decodes request frame with nested params`() throws {
let json = """
{
"type": "req",
"id": "1",
"method": "agent.send",
"params": {
"text": "hi",
"items": [1, null, {"ok": true}],
"meta": { "count": 2 }
}
}
"""
let frame = try JSONDecoder().decode(GatewayFrame.self, from: Data(json.utf8))
#expect({
if case .req = frame { true } else { false }
}(), "expected .req frame")
guard case let .req(req) = frame else {
return
}
let params = req.params?.value as? [String: AnyCodable]
#expect(params?["text"]?.value as? String == "hi")
let items = params?["items"]?.value as? [AnyCodable]
#expect(items?.count == 3)
#expect(items?[0].value as? Int == 1)
#expect(items?[1].value is NSNull)
let item2 = items?[2].value as? [String: AnyCodable]
#expect(item2?["ok"]?.value as? Bool == true)
let meta = params?["meta"]?.value as? [String: AnyCodable]
#expect(meta?["count"]?.value as? Int == 2)
}
@Test func `decodes unknown frame and preserves raw`() throws {
let json = """
{
"type": "made-up",
"foo": "bar",
"count": 1,
"nested": { "ok": true }
}
"""
let frame = try JSONDecoder().decode(GatewayFrame.self, from: Data(json.utf8))
#expect({
if case .unknown = frame { true } else { false }
}(), "expected .unknown frame")
guard case let .unknown(type, raw) = frame else {
return
}
#expect(type == "made-up")
#expect(raw["type"]?.value as? String == "made-up")
#expect(raw["foo"]?.value as? String == "bar")
#expect(raw["count"]?.value as? Int == 1)
let nested = raw["nested"]?.value as? [String: AnyCodable]
#expect(nested?["ok"]?.value as? Bool == true)
}
}