refactor(tests): dedupe swift gateway and chat fixtures

This commit is contained in:
Peter Steinberger
2026-03-02 09:39:30 +00:00
parent 5f49a5da3c
commit fd7774a79e
14 changed files with 354 additions and 578 deletions

View File

@@ -24,6 +24,14 @@ private func waitUntil(
throw TimeoutError(label: label)
}
private func chatTextMessage(role: String, text: String, timestamp: Double) -> AnyCodable {
AnyCodable([
"role": role,
"content": [["type": "text", "text": text]],
"timestamp": timestamp,
])
}
private actor TestChatTransportState {
var historyCallCount: Int = 0
var sessionsCallCount: Int = 0
@@ -148,11 +156,10 @@ extension TestChatTransportState {
sessionKey: "main",
sessionId: sessionId,
messages: [
AnyCodable([
"role": "assistant",
"content": [["type": "text", "text": "final answer"]],
"timestamp": Date().timeIntervalSince1970 * 1000,
]),
chatTextMessage(
role: "assistant",
text: "final answer",
timestamp: Date().timeIntervalSince1970 * 1000),
],
thinkingLevel: "off")
@@ -225,11 +232,10 @@ extension TestChatTransportState {
sessionKey: "main",
sessionId: "sess-main",
messages: [
AnyCodable([
"role": "assistant",
"content": [["type": "text", "text": "from history"]],
"timestamp": Date().timeIntervalSince1970 * 1000,
]),
chatTextMessage(
role: "assistant",
text: "from history",
timestamp: Date().timeIntervalSince1970 * 1000),
],
thinkingLevel: "off")
@@ -267,27 +273,15 @@ extension TestChatTransportState {
sessionKey: "main",
sessionId: "sess-main",
messages: [
AnyCodable([
"role": "user",
"content": [["type": "text", "text": "first"]],
"timestamp": now,
]),
chatTextMessage(role: "user", text: "first", timestamp: now),
],
thinkingLevel: "off")
let history2 = OpenClawChatHistoryPayload(
sessionKey: "main",
sessionId: "sess-main",
messages: [
AnyCodable([
"role": "user",
"content": [["type": "text", "text": "first"]],
"timestamp": now,
]),
AnyCodable([
"role": "assistant",
"content": [["type": "text", "text": "from external run"]],
"timestamp": now + 1,
]),
chatTextMessage(role: "user", text: "first", timestamp: now),
chatTextMessage(role: "assistant", text: "from external run", timestamp: now + 1),
],
thinkingLevel: "off")
@@ -317,27 +311,15 @@ extension TestChatTransportState {
sessionKey: "main",
sessionId: "sess-main",
messages: [
AnyCodable([
"role": "user",
"content": [["type": "text", "text": "hello"]],
"timestamp": now,
]),
chatTextMessage(role: "user", text: "hello", timestamp: now),
],
thinkingLevel: "off")
let history2 = OpenClawChatHistoryPayload(
sessionKey: "main",
sessionId: "sess-main",
messages: [
AnyCodable([
"role": "user",
"content": [["type": "text", "text": "hello"]],
"timestamp": now,
]),
AnyCodable([
"role": "assistant",
"content": [["type": "text", "text": "world"]],
"timestamp": now + 1,
]),
chatTextMessage(role: "user", text: "hello", timestamp: now),
chatTextMessage(role: "assistant", text: "world", timestamp: now + 1),
],
thinkingLevel: "off")
@@ -427,11 +409,7 @@ extension TestChatTransportState {
sessionKey: "main",
sessionId: "sess-main",
messages: [
AnyCodable([
"role": "assistant",
"content": [["type": "text", "text": "resynced after gap"]],
"timestamp": now,
]),
chatTextMessage(role: "assistant", text: "resynced after gap", timestamp: now),
],
thinkingLevel: "off")

View File

@@ -114,38 +114,48 @@ private final class FakeGatewayWebSocketTask: WebSocketTasking, @unchecked Senda
}
private static func connectChallengeData(nonce: String) -> Data {
let json = """
{
"type": "event",
"event": "connect.challenge",
"payload": { "nonce": "\(nonce)" }
}
"""
return Data(json.utf8)
let frame: [String: Any] = [
"type": "event",
"event": "connect.challenge",
"payload": ["nonce": nonce],
]
return (try? JSONSerialization.data(withJSONObject: frame)) ?? Data()
}
private static func connectOkData(id: String) -> Data {
let json = """
{
"type": "res",
"id": "\(id)",
"ok": true,
"payload": {
let payload: [String: Any] = [
"type": "hello-ok",
"protocol": 2,
"server": { "version": "test", "connId": "test" },
"features": { "methods": [], "events": [] },
"snapshot": {
"presence": [ { "ts": 1 } ],
"health": {},
"stateVersion": { "presence": 0, "health": 0 },
"uptimeMs": 0
},
"policy": { "maxPayload": 1, "maxBufferedBytes": 1, "tickIntervalMs": 30000 }
}
}
"""
return Data(json.utf8)
"server": [
"version": "test",
"connId": "test",
],
"features": [
"methods": [],
"events": [],
],
"snapshot": [
"presence": [["ts": 1]],
"health": [:],
"stateVersion": [
"presence": 0,
"health": 0,
],
"uptimeMs": 0,
],
"policy": [
"maxPayload": 1,
"maxBufferedBytes": 1,
"tickIntervalMs": 30_000,
],
]
let frame: [String: Any] = [
"type": "res",
"id": id,
"ok": true,
"payload": payload,
]
return (try? JSONSerialization.data(withJSONObject: frame)) ?? Data()
}
}