Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions app/src/main/java/com/pombo/android/core/RpcEndpoints.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,23 @@ object RpcEndpoints {
val rows = (0 until array.length()).mapNotNull { i ->
array.optJSONObject(i)?.let { Row(it.optString("key"), it.optBoolean("on")) }
}
return normalize(rows, obj.optString("customUrl"))
val customUrl = obj.optString("customUrl")
if (obj.optInt("v", 2) < 3 && rows.filter { it.on }.map { it.key }.toSet() == PREVIOUS_DEFAULT) {
return normalize(ALL.map { Row(it.key, false) } + Row(CUSTOM_KEY, false), customUrl)
}
return normalize(rows, customUrl)
}

/**
* A selection saved before the default changed whose enabled set is exactly
* this never chose anything: [fromJson] moves it to the current default,
* keeping a custom URL as an off row. Saving writes v3, so a later choice is
* never moved.
*/
private val PREVIOUS_DEFAULT = setOf("drpc", "publicnode", "tenderly")

fun toJson(selection: Selection): String = JSONObject()
.put("v", 2)
.put("v", 3)
.put("rows", JSONArray().apply {
selection.rows.forEach { put(JSONObject().put("key", it.key).put("on", it.on)) }
})
Expand Down
54 changes: 54 additions & 0 deletions app/src/test/java/com/pombo/android/core/RpcEndpointsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,58 @@ class RpcEndpointsTest {
assertEquals(start.rows, start.moved(start.rows.first().key, -1).rows)
assertEquals(start.rows, start.moved(start.rows.last().key, 1).rows)
}

private fun saved(version: Int, rows: List<Pair<String, Boolean>>, customUrl: String = "") =
org.json.JSONObject()
.put("v", version)
.put("rows", org.json.JSONArray().apply {
rows.forEach { (key, on) -> put(org.json.JSONObject().put("key", key).put("on", on)) }
})
.put("customUrl", customUrl)
.toString()

private val oldDefault = listOf("drpc", "publicnode", "tenderly")

@Test
fun previousDefault_movesToTheCurrentOne_whateverTheSavedOrder() {
val json = saved(2, listOf("tenderly" to true, "drpc" to true, "publicnode" to true, "1rpc" to false))
assertEquals(
RpcEndpoints.DEFAULT_ENABLED.map { RpcEndpoints.byKey(it)!!.url },
RpcEndpoints.fromJson(json).urls
)
}

@Test
fun previousDefault_keepsACustomUrlAsAnOffRow() {
val json = saved(
2, oldDefault.map { it to true } + (RpcEndpoints.CUSTOM_KEY to false), "https://my-own-node.example"
)
val moved = RpcEndpoints.fromJson(json)
assertEquals(RpcEndpoints.DEFAULT_ENABLED, keysOn(moved))
assertEquals("https://my-own-node.example", moved.customUrl)
assertEquals(RpcEndpoints.Row(RpcEndpoints.CUSTOM_KEY, false), moved.rows.first { it.key == RpcEndpoints.CUSTOM_KEY })
}

@Test
fun anyOtherSavedChoice_staysAsItWas() {
val choices = listOf(
saved(2, listOf("drpc" to true, "tenderly" to true)) to listOf("drpc", "tenderly"),
saved(2, (oldDefault + "1rpc").map { it to true }) to oldDefault + "1rpc",
saved(
2, oldDefault.map { it to true } + (RpcEndpoints.CUSTOM_KEY to true), "https://my-own-node.example"
) to oldDefault + RpcEndpoints.CUSTOM_KEY
)
choices.forEach { (json, enabled) -> assertEquals(enabled, keysOn(RpcEndpoints.fromJson(json))) }
}

@Test
fun aChoiceSavedAfterTheChange_isNeverMoved_evenTheOldDefault() {
assertEquals(oldDefault, keysOn(RpcEndpoints.fromJson(saved(3, oldDefault.map { it to true }))))
}

@Test
fun movedSelection_staysPutOnceSaved() {
val moved = RpcEndpoints.fromJson(saved(2, oldDefault.map { it to true }))
assertEquals(moved, RpcEndpoints.fromJson(RpcEndpoints.toJson(moved)))
}
}
Loading