fix(#1281): send connector owner instead of top-level session_id (#1294)

This commit is contained in:
m57 authored and GitHub committed 2026-09-26 19:06:58 +03:00
1 parent 2084f9083d
commit 89858fc35d
7 files changed
+165 -18

No files matched your search

@@ -1,6 +1,7 @@
package com.m57.hermescontrol.data.ws
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
@@ -63,6 +64,39 @@ class ConnectionOperationEventParserTest {
assertEquals("", (event as WsEvent.Unknown).raw)
}
@Test
fun connectionUpdate_bindsToSessionOwner() {
val event =
EventParser.parseParams(
mapOf(
"type" to "connection.update",
"payload" to
operationPayload(seq = 2L) +
mapOf("owner" to mapOf("type" to "session", "session_id" to "owner-session")),
),
)
assertTrue(event is WsEvent.ConnectionUpdate)
assertEquals("owner-session", (event as WsEvent.ConnectionUpdate).snapshot.sessionId)
}
@Test
fun connectionUpdate_accountOwnerNeverBindsToSession() {
val event =
EventParser.parseParams(
mapOf(
"type" to "connection.update",
"session_id" to "",
"payload" to
operationPayload(seq = 2L) +
mapOf("owner" to mapOf("type" to "account"), "session_id" to "stale-session"),
),
)
assertTrue(event is WsEvent.ConnectionUpdate)
assertNull((event as WsEvent.ConnectionUpdate).snapshot.sessionId)
}
private fun operationPayload(seq: Long): Map<String, Any?> =
mapOf(
"op_id" to "op-1",
@@ -44,7 +44,10 @@ class ConnectorRepositoryTest {
val result = repo.listConnectors("sess-abc-123")
assertTrue(result is ConnectorListResult.Success)
assertEquals(WsMethods.CONNECTORS_LIST, recordedMethod)
assertEquals(mapOf("session_id" to "sess-abc-123"), recordedParams)
assertEquals(
mapOf("owner" to mapOf("type" to "session", "session_id" to "sess-abc-123")),
recordedParams,
)
// Verify no profile or singular slug mutations
assertFalse(recordedParams?.containsKey("profile") == true)
assertFalse(recordedParams?.containsKey("connector") == true)
@@ -80,9 +83,14 @@ class ConnectorRepositoryTest {
assertTrue(result is ConnectorConnectResult.Success)
assertEquals(WsMethods.CONNECTORS_CONNECT, recordedMethod)
assertEquals("sess-456", recordedParams?.get("session_id"))
assertEquals(listOf("linear", "github_app-1"), recordedParams?.get("connectors"))
assertEquals(true, recordedParams?.get("reconnect"))
assertEquals(
mapOf(
"owner" to mapOf("type" to "session", "session_id" to "sess-456"),
"connectors" to listOf("linear", "github_app-1"),
"reconnect" to true,
),
recordedParams,
)
// Verify no singular slug key mutation
assertFalse(recordedParams?.containsKey("connector") == true)
@@ -13,6 +13,8 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
@@ -109,7 +111,8 @@ class ChatConnectionOperationDelegateTest {
assertEquals(1, requester.calls.size)
val call = requester.calls.single()
assertEquals(WsMethods.CONNECTION_RESPOND, call.first)
assertEquals("session-a", call.second["session_id"])
assertEquals(setOf("owner", "op_id", "result"), call.second.keys)
assertEquals(mapOf("type" to "session", "session_id" to "session-a"), call.second["owner"])
assertEquals("op-a", call.second["op_id"])
@Suppress("UNCHECKED_CAST")
val result = call.second["result"] as Map<String, Any>
@@ -177,11 +180,63 @@ class ChatConnectionOperationDelegateTest {
continueDelegate.continueOperation()
assertEquals(
mapOf("settled_by" to "continue"),
continueRequester.calls.single().second["result"],
mapOf(
"owner" to mapOf("type" to "session", "session_id" to "session-a"),
"op_id" to "op-a",
"result" to mapOf("settled_by" to "continue"),
),
continueRequester.calls.single().second,
)
}
@Test
fun wakePayload_sendsSessionOwnerNotTopLevelSessionId() =
runTest {
val requester = RecordingRequester()
val delegate = delegate(requester)
delegate.reset("session-a")
delegate.acceptRequest(snapshot())
delegate.wake("op-a")
val call = requester.calls.single()
assertEquals(WsMethods.CONNECTORS_OPERATION_WAKE, call.first)
assertEquals(
mapOf(
"owner" to mapOf("type" to "session", "session_id" to "session-a"),
"op_id" to "op-a",
),
call.second,
)
}
@Test
fun ownershipAndRuntimeFailures_mapToTypedNonRetryableErrors() =
runTest {
val cases =
listOf(
HermesWsClient.HermesRpcException("x", code = 4001) to
ChatConnectionOperationDelegate.NOT_OWNER,
HermesWsClient.HermesRpcException(
"x",
code = 5033,
data = JsonObject(mapOf("reason" to JsonPrimitive("UNSUPPORTED_RUNTIME"))),
) to ChatConnectionOperationDelegate.UNSUPPORTED_RUNTIME,
)
for ((failure, expected) in cases) {
val delegate = delegate(RecordingRequester(failure = failure))
delegate.reset("session-a")
delegate.acceptRequest(snapshot())
delegate.continueOperation()
val error = delegate.state.value.error
assertEquals(expected, error?.message)
assertFalse(error?.retryable ?: true)
assertNull(delegate.state.value.pendingAction)
}
}
@Test
fun rpcFailureUnlocksWithSanitizedError() =
runTest {