|
| 1 | +package ai.openclaw.app.chat |
| 2 | + |
| 3 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 4 | +import kotlinx.coroutines.test.TestScope |
| 5 | +import kotlinx.coroutines.test.runCurrent |
| 6 | +import kotlinx.coroutines.test.runTest |
| 7 | +import kotlinx.serialization.json.Json |
| 8 | +import org.junit.Assert.assertEquals |
| 9 | +import org.junit.Assert.assertNull |
| 10 | +import org.junit.Assert.assertTrue |
| 11 | +import org.junit.Test |
| 12 | + |
| 13 | +/** |
| 14 | + * Reconnect recovery scenarios: after a gateway disconnect, the next health event |
| 15 | + * refetches chat.history and re-adopts the run the gateway still reports in flight |
| 16 | + * (`inFlightRun`), matching the reconnect snapshot contract the TUI consumes. |
| 17 | + */ |
| 18 | +class ChatControllerReconnectRestoreTest { |
| 19 | + private val json = Json { ignoreUnknownKeys = true } |
| 20 | + |
| 21 | + private fun TestScope.newController(gateway: ScriptedGateway): ChatController = |
| 22 | + ChatController(scope = this, json = json, requestGateway = gateway::request) |
| 23 | + |
| 24 | + private val userTurn = ReplayHistoryMessage("user", "keep working", 1_000) |
| 25 | + |
| 26 | + @Test |
| 27 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 28 | + fun reconnectAdoptsInFlightRunAndConsumesLiveEvents() = |
| 29 | + runTest { |
| 30 | + val gateway = ScriptedGateway(json) |
| 31 | + gateway.respondWith("chat.history", historyResponse("session-1", listOf(userTurn))) |
| 32 | + val controller = newController(gateway) |
| 33 | + controller.load("main") |
| 34 | + runCurrent() |
| 35 | + assertEquals(0, controller.pendingRunCount.value) |
| 36 | + |
| 37 | + controller.onDisconnected("Reconnecting…") |
| 38 | + gateway.respondWith( |
| 39 | + "chat.history", |
| 40 | + historyResponse("session-1", listOf(userTurn), inFlightRun = "run-active" to "partial reply"), |
| 41 | + ) |
| 42 | + controller.handleGatewayEvent("health", null) |
| 43 | + runCurrent() |
| 44 | + |
| 45 | + assertEquals(1, controller.pendingRunCount.value) |
| 46 | + assertEquals("partial reply", controller.streamingAssistantText.value) |
| 47 | + assertEquals(1, controller.messages.value.size) |
| 48 | + |
| 49 | + // The adopted run keeps consuming live deltas and its terminal event. |
| 50 | + controller.handleGatewayEvent( |
| 51 | + "chat", |
| 52 | + chatDeltaPayload("main", "run-active", 5, " more", "partial reply more"), |
| 53 | + ) |
| 54 | + assertEquals("partial reply more", controller.streamingAssistantText.value) |
| 55 | + gateway.respondWith( |
| 56 | + "chat.history", |
| 57 | + historyResponse( |
| 58 | + "session-1", |
| 59 | + listOf(userTurn, ReplayHistoryMessage("assistant", "partial reply more", 2_000)), |
| 60 | + ), |
| 61 | + ) |
| 62 | + controller.handleGatewayEvent("chat", chatTerminalPayload("main", "run-active", seq = 6)) |
| 63 | + runCurrent() |
| 64 | + |
| 65 | + assertEquals(0, controller.pendingRunCount.value) |
| 66 | + assertNull(controller.streamingAssistantText.value) |
| 67 | + assertEquals(2, controller.messages.value.size) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 72 | + fun reconnectWithoutInFlightRunStaysClean() = |
| 73 | + runTest { |
| 74 | + val gateway = ScriptedGateway(json) |
| 75 | + gateway.respondWith("chat.history", historyResponse("session-1", listOf(userTurn))) |
| 76 | + val controller = newController(gateway) |
| 77 | + controller.load("main") |
| 78 | + runCurrent() |
| 79 | + val historyCallsAfterLoad = gateway.callCount("chat.history") |
| 80 | + |
| 81 | + controller.onDisconnected("Offline") |
| 82 | + controller.handleGatewayEvent("health", null) |
| 83 | + runCurrent() |
| 84 | + |
| 85 | + // Reconnect refetched history once and restored nothing. |
| 86 | + assertEquals(historyCallsAfterLoad + 1, gateway.callCount("chat.history")) |
| 87 | + assertEquals(0, controller.pendingRunCount.value) |
| 88 | + assertNull(controller.streamingAssistantText.value) |
| 89 | + assertNull(controller.errorText.value) |
| 90 | + assertTrue(controller.healthOk.value) |
| 91 | + assertEquals(1, controller.messages.value.size) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 96 | + fun repeatedReconnectsDoNotDuplicateRunOrRows() = |
| 97 | + runTest { |
| 98 | + val gateway = ScriptedGateway(json) |
| 99 | + gateway.respondWith( |
| 100 | + "chat.history", |
| 101 | + historyResponse("session-1", listOf(userTurn), inFlightRun = "run-active" to "partial"), |
| 102 | + ) |
| 103 | + val controller = newController(gateway) |
| 104 | + controller.load("main") |
| 105 | + runCurrent() |
| 106 | + assertEquals(1, controller.pendingRunCount.value) |
| 107 | + |
| 108 | + repeat(2) { |
| 109 | + controller.onDisconnected("Reconnecting…") |
| 110 | + assertEquals(0, controller.pendingRunCount.value) |
| 111 | + controller.handleGatewayEvent("health", null) |
| 112 | + runCurrent() |
| 113 | + } |
| 114 | + |
| 115 | + assertEquals(1, controller.pendingRunCount.value) |
| 116 | + assertEquals("partial", controller.streamingAssistantText.value) |
| 117 | + assertEquals(1, controller.messages.value.size) |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 122 | + fun staleSnapshotRunDoesNotReplaceLocallyOwnedSend() = |
| 123 | + runTest { |
| 124 | + val gateway = ScriptedGateway(json) |
| 125 | + gateway.respondWith("chat.history", historyResponse("session-1", emptyList())) |
| 126 | + gateway.respondChatSend(status = "started") |
| 127 | + val controller = newController(gateway) |
| 128 | + controller.load("main") |
| 129 | + runCurrent() |
| 130 | + |
| 131 | + // Reconnect arms a recovery refresh, then a send lands before it executes. |
| 132 | + controller.onDisconnected("Reconnecting…") |
| 133 | + gateway.respondWith( |
| 134 | + "chat.history", |
| 135 | + historyResponse("session-1", emptyList(), inFlightRun = "run-stale" to "old text"), |
| 136 | + ) |
| 137 | + controller.handleGatewayEvent("health", null) |
| 138 | + assertTrue(controller.sendMessageAwaitAcceptance("new work", "off", emptyList())) |
| 139 | + val localRunId = requireNotNull(gateway.lastRunId) |
| 140 | + runCurrent() |
| 141 | + |
| 142 | + assertEquals(1, controller.pendingRunCount.value) |
| 143 | + controller.handleGatewayEvent( |
| 144 | + "chat", |
| 145 | + chatDeltaPayload("main", localRunId, 1, "ours", "ours"), |
| 146 | + ) |
| 147 | + assertEquals("ours", controller.streamingAssistantText.value) |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 152 | + fun seqGapRefetchesHistoryAndRestoresInFlightRun() = |
| 153 | + runTest { |
| 154 | + val gateway = ScriptedGateway(json) |
| 155 | + gateway.respondWith( |
| 156 | + "chat.history", |
| 157 | + historyResponse("session-1", listOf(userTurn), inFlightRun = "run-active" to "still going"), |
| 158 | + ) |
| 159 | + val controller = newController(gateway) |
| 160 | + controller.load("main") |
| 161 | + runCurrent() |
| 162 | + assertEquals(1, controller.pendingRunCount.value) |
| 163 | + |
| 164 | + controller.handleGatewayEvent("seqGap", null) |
| 165 | + runCurrent() |
| 166 | + |
| 167 | + assertEquals(1, controller.pendingRunCount.value) |
| 168 | + assertEquals("still going", controller.streamingAssistantText.value) |
| 169 | + assertNull(controller.errorText.value) |
| 170 | + assertEquals(1, controller.messages.value.size) |
| 171 | + } |
| 172 | +} |
0 commit comments