test(tui-04): assert single-consumer contract for ws.messages and ws.connection

This commit is contained in:
2026-05-25 12:33:49 +04:00
parent de069dbf6a
commit 8b8f9c9379
2 changed files with 33 additions and 0 deletions
@@ -61,6 +61,10 @@ fun main(args: Array<String>) {
}
}
// IMPORTANT: ws.messages and ws.connection are single-consumer cold flows
// (Channel.UNLIMITED.receiveAsFlow). Do not add a second collector for either —
// events would be split across collectors and dropped from the dispatcher's view.
// If a second consumer is needed (e.g. metrics), share via a downstream broadcast.
effectScope.launch {
ws.messages.collect { msg ->
runner.runOnRenderThread { dispatch(Action.ServerEventReceived(msg)) }
@@ -0,0 +1,29 @@
package com.correx.apps.tui
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.io.File
class SingleConsumerContractTest {
private val sourceRoot = File("src/main/kotlin")
private fun countOccurrences(literal: String): Int =
sourceRoot.walkTopDown()
.filter { it.isFile && it.extension == "kt" }
.sumOf { file ->
file.readText().split(literal).size - 1
}
@Test
fun `ws messages has exactly one collector`() {
assertEquals(1, countOccurrences("ws.messages.collect"),
"Expected exactly one ws.messages.collect in src/main/kotlin")
}
@Test
fun `ws connection has exactly one collector`() {
assertEquals(1, countOccurrences("ws.connection.collect"),
"Expected exactly one ws.connection.collect in src/main/kotlin")
}
}