test: isolate macos gateway connection control

This commit is contained in:
Peter Steinberger
2026-01-12 09:08:05 +00:00
parent f102d1bb9d
commit 83c206d68a

View File

@@ -1,17 +1,53 @@
import Foundation
import Testing import Testing
@testable import Clawdbot @testable import Clawdbot
@testable import ClawdbotIPC @testable import ClawdbotIPC
private final class FakeWebSocketTask: WebSocketTasking, @unchecked Sendable {
var state: URLSessionTask.State = .running
func resume() {}
func cancel(with _: URLSessionWebSocketTask.CloseCode, reason _: Data?) {
self.state = .canceling
}
func send(_: URLSessionWebSocketTask.Message) async throws {}
func receive() async throws -> URLSessionWebSocketTask.Message {
throw URLError(.cannotConnectToHost)
}
func receive(completionHandler: @escaping @Sendable (Result<URLSessionWebSocketTask.Message, Error>) -> Void) {
completionHandler(.failure(URLError(.cannotConnectToHost)))
}
}
private final class FakeWebSocketSession: WebSocketSessioning, @unchecked Sendable {
func makeWebSocketTask(url _: URL) -> WebSocketTaskBox {
WebSocketTaskBox(task: FakeWebSocketTask())
}
}
private func makeTestGatewayConnection() -> GatewayConnection {
GatewayConnection(
configProvider: {
(url: URL(string: "ws://127.0.0.1:1")!, token: nil, password: nil)
},
sessionBox: WebSocketSessionBox(session: FakeWebSocketSession()))
}
@Suite(.serialized) struct GatewayConnectionControlTests { @Suite(.serialized) struct GatewayConnectionControlTests {
@Test func statusFailsWhenProcessMissing() async { @Test func statusFailsWhenProcessMissing() async {
let result = await GatewayConnection.shared.status() let connection = makeTestGatewayConnection()
// We don't assert ok because the worker may not be available in CI. let result = await connection.status()
// Instead, ensure the call returns without throwing and provides a message. #expect(result.ok == false)
#expect(result.ok == true || result.error != nil) #expect(result.error != nil)
} }
@Test func rejectEmptyMessage() async { @Test func rejectEmptyMessage() async {
let result = await GatewayConnection.shared.sendAgent( let connection = makeTestGatewayConnection()
let result = await connection.sendAgent(
message: "", message: "",
thinking: nil, thinking: nil,
sessionKey: "main", sessionKey: "main",