fix: land mac node bridge ping loop (#572) (thanks @ngutman)

This commit is contained in:
Peter Steinberger
2026-01-09 14:01:20 +01:00
parent 975aa5bf82
commit cb86d0d6d4
12 changed files with 80 additions and 56 deletions

View File

@@ -252,12 +252,17 @@ actor MacNodeBridgeSession {
}
private func send(_ obj: some Encodable) async throws {
guard let connection = self.connection else {
throw NSError(domain: "Bridge", code: 15, userInfo: [
NSLocalizedDescriptionKey: "not connected",
])
}
let data = try self.encoder.encode(obj)
var line = Data()
line.append(data)
line.append(0x0A)
try await withCheckedThrowingContinuation(isolation: self) { (cont: CheckedContinuation<Void, Error>) in
self.connection?.send(content: line, completion: .contentProcessed { err in
connection.send(content: line, completion: .contentProcessed { err in
if let err { cont.resume(throwing: err) } else { cont.resume(returning: ()) }
})
}
@@ -334,9 +339,8 @@ actor MacNodeBridgeSession {
}
private func notePong(_ pong: BridgePong) {
if pong.id == self.lastPingId || self.lastPingId == nil {
self.lastPongAt = Date()
}
_ = pong
self.lastPongAt = Date()
}
private static func makeStateStream(

View File

@@ -0,0 +1,19 @@
import Testing
@testable import Clawdbot
@Suite
struct MacNodeBridgeSessionTests {
@Test func sendEventThrowsWhenNotConnected() async {
let session = MacNodeBridgeSession()
do {
try await session.sendEvent(event: "test", payloadJSON: "{}")
Issue.record("Expected sendEvent to throw when disconnected")
} catch {
let ns = error as NSError
#expect(ns.domain == "Bridge")
#expect(ns.code == 15)
}
}
}