Modern Apps git

← Modern-Apps.git

games-voxels: restore FakeVoxelsRelay kept by MenuActivityOnlineTest

Commit
aa5a621eb14fe1bda0d1f672067733fff7b544e5
Author
Vayun Mathur <[email protected]>
When
2026-11-08 09:07 UTC (28 days ago)
Parents
79f74fc
MenuActivityOnlineTest was kept over cov/c33's deletion because it covers
the current join-link behavior, but its FakeVoxelsRelay helper came from the
same deletion. Restore the helper so the kept test compiles.

Diff

games/voxels/src/test/java/com/vayunmathur/games/voxels/network/FakeVoxelsRelay.kt
diff --git a/games/voxels/src/test/java/com/vayunmathur/games/voxels/network/FakeVoxelsRelay.kt b/games/voxels/src/test/java/com/vayunmathur/games/voxels/network/FakeVoxelsRelay.ktnew file mode 100644index 0000000000..17ab8a2140--- /dev/null+++ b/games/voxels/src/test/java/com/vayunmathur/games/voxels/network/FakeVoxelsRelay.kt@@ -0,0 +1,59 @@+package com.vayunmathur.games.voxels.network++import com.vayunmathur.library.network.SimpleResponse+import org.json.JSONArray+import org.json.JSONObject++/**+ * In-memory stand-in for the voxels relay (`/register|/getkey|/append|/pull`),+ * installed via [VoxelsSync.relayTransport] so tests never touch the network.+ * Mirrors the relay contract: append-only channel logs, seq = log size.+ */+object FakeVoxelsRelay {+    val keys = mutableMapOf<String, String>()+    val logs = mutableMapOf<String, MutableList<String>>()++    fun install() {+        VoxelsSync.relayTransport = { path, bodyJson -> handle(path, bodyJson) }+    }++    fun uninstall() {+        VoxelsSync.relayTransport = null+    }++    fun reset() {+        keys.clear(); logs.clear()+    }++    private fun resp(status: Int, body: String) = SimpleResponse(status, "", body, emptyMap(), "fake://relay")++    private fun handle(path: String, bodyJson: String): SimpleResponse {+        val body = JSONObject(bodyJson)+        return when (path) {+            "/register" -> {+                keys[body.getString("id")] = body.getString("key")+                resp(200, "{}")+            }+            "/getkey" -> {+                val key = keys[body.getString("id")]+                if (key != null) resp(200, key) else resp(404, "")+            }+            "/append" -> {+                val channel = body.getString("channel")+                val arr = body.getJSONArray("actions")+                val log = logs.getOrPut(channel) { mutableListOf() }+                for (i in 0 until arr.length()) log += arr.getString(i)+                resp(200, JSONObject().put("seq", log.size).toString())+            }+            "/pull" -> {+                val channel = body.getString("channel")+                val since = body.optInt("since", 0)+                val log = logs[channel].orEmpty()+                val actions = JSONArray()+                log.drop(since).forEach { actions.put(it) }+                resp(200, JSONObject().put("actions", actions).put("seq", log.size).toString())+            }+            else -> resp(404, "")+        }+    }+}