@mentra/bluetooth-sdk 3.5.0-dev.1271 → 3.5.0-dev.1276

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
  2. package/android/src/main/java/com/mentra/bluetoothsdk/MentraLocalNetworkModule.kt +57 -196
  3. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/Ipv4Prefix.kt +51 -0
  4. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/NetworkFacts.kt +81 -0
  5. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkError.kt +109 -0
  6. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadiness.kt +103 -0
  7. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpec.kt +53 -0
  8. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkState.kt +127 -0
  9. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedSoftApNetwork.kt +604 -0
  10. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/SoftApTrace.kt +143 -0
  11. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/Ipv4PrefixTest.kt +59 -0
  12. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/NetworkFactsTest.kt +59 -0
  13. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedLocalNetworkPermissionTest.kt +35 -0
  14. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkErrorTest.kt +96 -0
  15. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkLifecycleTest.kt +187 -0
  16. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadinessTest.kt +141 -0
  17. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpecTest.kt +69 -0
  18. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/SoftApTraceTest.kt +97 -0
  19. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/UnavailableRetryTest.kt +47 -0
  20. package/build/_private/MentraLocalNetworkModule.d.ts +1 -1
  21. package/build/_private/MentraLocalNetworkModule.d.ts.map +1 -1
  22. package/build/_private/MentraLocalNetworkModule.js.map +1 -1
  23. package/build/generated/releaseMetadata.js +5 -5
  24. package/build/generated/releaseMetadata.js.map +1 -1
  25. package/build/ota-transport/index.d.ts +3 -2
  26. package/build/ota-transport/index.d.ts.map +1 -1
  27. package/build/ota-transport/index.js +4 -3
  28. package/build/ota-transport/index.js.map +1 -1
  29. package/ios/Source/BluetoothSdkDefaults.swift +1 -1
  30. package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
  31. package/package.json +2 -2
  32. package/src/_private/MentraLocalNetworkModule.ts +5 -1
  33. package/src/generated/releaseMetadata.ts +5 -5
  34. package/src/ota-transport/index.ts +4 -3
  35. package/android/src/main/java/com/mentra/bluetoothsdk/ScopedNetworkReadiness.kt +0 -17
  36. package/android/src/test/java/com/mentra/bluetoothsdk/ScopedNetworkReadinessTest.kt +0 -39
@@ -0,0 +1,143 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ import android.os.SystemClock
4
+ import com.mentra.bluetoothsdk.utils.NativeLog as Log
5
+
6
+ /**
7
+ * Correlated stage logging for the SoftAP calling pipeline (phone side).
8
+ *
9
+ * TEMPORARY DIAGNOSTIC. Every line carries the literal `SOFTAP_TRACE` marker so a single
10
+ * `rg -n 'SOFTAP_TRACE'` finds all of them at cleanup time. Flip [ENABLED] to false to mute the
11
+ * layer for a release without deleting call sites.
12
+ *
13
+ * The logcat tag is deliberately distinct from the module's `ACS-SPIKE` tag so that
14
+ * `adb logcat -s SOFTAP-TRACE` isolates the whole SoftAP pipeline in one stream.
15
+ *
16
+ * The phone mints [traceId] when it starts the hotspot and passes it to the glasses in
17
+ * `start_stream`. Both devices stamp the same id on every line, which is the only way to correlate
18
+ * two logs whose clocks were never synchronised.
19
+ */
20
+ object SoftApTrace {
21
+
22
+ /** Master switch. Flip to false to mute the trace without removing call sites. */
23
+ const val ENABLED = true
24
+
25
+ /** Grep marker. Never build this by concatenation — a single grep must be exhaustive. */
26
+ const val MARKER = "SOFTAP_TRACE"
27
+
28
+ const val TAG = "SOFTAP-TRACE"
29
+
30
+ private const val REDACTED = "<redacted>"
31
+
32
+ /** Keys whose values must never reach logcat. Matched case-insensitively as substrings. */
33
+ private val SENSITIVE_KEYS = listOf(
34
+ "password", "passwd", "passphrase", "psk", "token", "secret",
35
+ "credential", "authorization", "bearer", "meetingurl",
36
+ )
37
+
38
+ @Volatile
39
+ private var traceId: String = ""
40
+
41
+ @Volatile
42
+ private var originMs: Long = 0L
43
+
44
+ @Volatile
45
+ private var lastStage: String = ""
46
+
47
+ /** Mint a new trace id and reset the elapsed-time origin. Called when the hotspot request starts. */
48
+ fun begin(id: String = newTraceId()): String {
49
+ traceId = id
50
+ originMs = SystemClock.elapsedRealtime()
51
+ lastStage = ""
52
+ return id
53
+ }
54
+
55
+ fun reset() {
56
+ traceId = ""
57
+ originMs = 0L
58
+ lastStage = ""
59
+ }
60
+
61
+ fun traceId(): String = traceId
62
+
63
+ /** Last stage successfully logged; use as failure context when reporting an error. */
64
+ fun lastStage(): String = lastStage
65
+
66
+ /** Short, collision-resistant enough for correlating one call's logs. */
67
+ fun newTraceId(): String = java.lang.Long.toHexString(System.nanoTime() and 0xFFFFFFFFL)
68
+
69
+ /**
70
+ * Log one pipeline stage transition.
71
+ *
72
+ * @param stage stable snake_case stage name, e.g. `scoped_network_available`
73
+ */
74
+ fun stage(stage: String, vararg fields: Pair<String, Any?>) {
75
+ if (!ENABLED) return
76
+ lastStage = stage
77
+ val elapsedMs = if (originMs == 0L) 0L else SystemClock.elapsedRealtime() - originMs
78
+ Log.i(TAG, format(traceId, stage, elapsedMs, *fields))
79
+ }
80
+
81
+ /** Error-level variant so genuine failures survive a logcat level filter. */
82
+ fun failure(stage: String, vararg fields: Pair<String, Any?>) {
83
+ if (!ENABLED) return
84
+ val elapsedMs = if (originMs == 0L) 0L else SystemClock.elapsedRealtime() - originMs
85
+ Log.e(TAG, format(traceId, stage, elapsedMs, *fields, "afterStage" to lastStage))
86
+ }
87
+
88
+ /**
89
+ * Pure formatter. Split out from [stage] so it can be unit tested without the Android logging
90
+ * framework, which is not available to plain JVM tests in this module.
91
+ */
92
+ fun format(
93
+ traceId: String,
94
+ stage: String,
95
+ elapsedMs: Long,
96
+ vararg fields: Pair<String, Any?>,
97
+ ): String = buildString {
98
+ append('[').append(MARKER).append(']')
99
+ if (traceId.isNotEmpty()) append(" traceId=").append(traceId)
100
+ append(" stage=").append(stage)
101
+ append(" elapsedMs=").append(elapsedMs)
102
+ for ((key, value) in fields) {
103
+ append(' ').append(key).append('=').append(sanitize(key, value))
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Redact secrets outright and strip query strings from URLs. The local WHIP URL is genuinely
109
+ * useful in a trace, but a URL carrying a watch token is not, so query and userinfo go.
110
+ */
111
+ fun sanitize(key: String, value: Any?): String {
112
+ if (isSensitive(key)) return REDACTED
113
+ if (value == null) return "null"
114
+
115
+ val text = value.toString()
116
+ if (text.isEmpty()) return "\"\""
117
+ val cleaned = stripUrlSecrets(text)
118
+ return if (cleaned.contains(' ')) "\"$cleaned\"" else cleaned
119
+ }
120
+
121
+ private fun isSensitive(key: String): Boolean {
122
+ val lower = key.lowercase()
123
+ return SENSITIVE_KEYS.any { lower.contains(it) }
124
+ }
125
+
126
+ /** Drop `?query`, `#fragment`, and `user:pass@` from anything URL-shaped. */
127
+ private fun stripUrlSecrets(text: String): String {
128
+ val schemeIndex = text.indexOf("://")
129
+ if (schemeIndex < 0) return text
130
+
131
+ var result = text
132
+ val query = result.indexOf('?')
133
+ if (query >= 0) result = result.substring(0, query) + "?" + REDACTED
134
+ val fragment = result.indexOf('#')
135
+ if (fragment >= 0) result = result.substring(0, fragment)
136
+
137
+ val at = result.indexOf('@', schemeIndex + 3)
138
+ if (at >= 0) {
139
+ result = result.substring(0, schemeIndex + 3) + REDACTED + "@" + result.substring(at + 1)
140
+ }
141
+ return result
142
+ }
143
+ }
@@ -0,0 +1,59 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertFalse
5
+ import org.junit.Assert.assertNull
6
+ import org.junit.Assert.assertTrue
7
+ import org.junit.Test
8
+
9
+ class Ipv4PrefixTest {
10
+
11
+ @Test
12
+ fun `contains the phone and the gateway on a 24`() {
13
+ val prefix = Ipv4Prefix("192.168.43.20", 24)
14
+ assertTrue(prefix.contains("192.168.43.1"))
15
+ assertTrue(prefix.contains("192.168.43.20"))
16
+ assertTrue(prefix.contains("192.168.43.255"))
17
+ }
18
+
19
+ /** The whole point: a cellular 10/8 address is RFC1918 and still the wrong interface. */
20
+ @Test
21
+ fun `rejects a private address on another interface`() {
22
+ val prefix = Ipv4Prefix("192.168.43.20", 24)
23
+ assertFalse(prefix.contains("10.171.36.4"))
24
+ assertFalse(prefix.contains("192.168.1.20"))
25
+ assertFalse(prefix.contains("172.16.0.1"))
26
+ }
27
+
28
+ /** An OEM hotspot on another subnet must still pass, which is why /24 is never assumed. */
29
+ @Test
30
+ fun `honours the reported prefix length`() {
31
+ assertTrue(Ipv4Prefix("192.168.49.1", 16).contains("192.168.43.7"))
32
+ assertFalse(Ipv4Prefix("192.168.49.1", 24).contains("192.168.43.7"))
33
+ assertTrue(Ipv4Prefix("10.5.4.3", 8).contains("10.99.99.99"))
34
+ }
35
+
36
+ @Test
37
+ fun `non-addresses are not addresses`() {
38
+ val prefix = Ipv4Prefix("192.168.43.20", 24)
39
+ assertFalse(prefix.contains(null))
40
+ assertFalse(prefix.contains("typ"))
41
+ assertFalse(prefix.contains("192.168.43"))
42
+ assertFalse(prefix.contains("192.168.43.256"))
43
+ assertFalse(prefix.contains("192.168.43.20.1"))
44
+ assertFalse(prefix.contains("a4f1c2d3.local"))
45
+ }
46
+
47
+ @Test
48
+ fun `packs and prints`() {
49
+ assertEquals(0x0A000001, Ipv4Prefix.packIpv4("10.0.0.1"))
50
+ assertNull(Ipv4Prefix.packIpv4("not.an.ip.addr"))
51
+ assertEquals("192.168.43.20/24", Ipv4Prefix("192.168.43.20", 24).toString())
52
+ }
53
+
54
+ /** An unparseable own-address must match nothing rather than everything. */
55
+ @Test
56
+ fun `a malformed prefix contains nothing`() {
57
+ assertFalse(Ipv4Prefix("garbage", 24).contains("192.168.43.1"))
58
+ }
59
+ }
@@ -0,0 +1,59 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ import com.mentra.bluetoothsdk.hotspot.NetworkFacts.Iface
4
+ import org.assertj.core.api.Assertions.assertThat
5
+ import org.junit.Test
6
+
7
+ /**
8
+ * The attribution helpers decide what the next SoftAP diagnosis says, so they are pinned rather
9
+ * than eyeballed in a log line.
10
+ */
11
+ class NetworkFactsTest {
12
+
13
+ private fun iface(
14
+ name: String,
15
+ ipv4: List<String>,
16
+ up: Boolean = true,
17
+ loopback: Boolean = false,
18
+ ) = Iface(name, 1, up, loopback, false, false, 1500, ipv4)
19
+
20
+ private val table =
21
+ listOf(
22
+ iface("lo", listOf("127.0.0.1/8"), loopback = true),
23
+ iface("wlan0", listOf("192.168.43.79/24")),
24
+ iface("rmnet_data0", listOf("10.48.51.202/30")),
25
+ iface("dummy0", emptyList()),
26
+ iface("epdg0", listOf("10.9.9.9/32"), up = false),
27
+ )
28
+
29
+ @Test
30
+ fun `the owner of an address is the interface the table says carries it`() {
31
+ assertThat(NetworkFacts.ownerOf("192.168.43.79", table)).isEqualTo("wlan0")
32
+ assertThat(NetworkFacts.ownerOf("10.48.51.202", table)).isEqualTo("rmnet_data0")
33
+ }
34
+
35
+ /** An address ICE gathered that is absent here means our two views of the network disagree. */
36
+ @Test
37
+ fun `an address the table has never seen has no owner`() {
38
+ assertThat(NetworkFacts.ownerOf("192.168.50.7", table)).isNull()
39
+ }
40
+
41
+ @Test
42
+ fun `a prefix is not mistaken for the address`() {
43
+ assertThat(NetworkFacts.ownerOf("192.168.43.79/24", table)).isNull()
44
+ }
45
+
46
+ @Test
47
+ fun `gatherable drops loopback, down, and addressless interfaces`() {
48
+ assertThat(NetworkFacts.gatherable(table).map { it.name })
49
+ .containsExactly("wlan0", "rmnet_data0")
50
+ }
51
+
52
+ @Test
53
+ fun `render keeps the name, flags and every address`() {
54
+ val line = NetworkFacts.render(listOf(iface("wlan0", listOf("192.168.43.79/24", "10.1.2.3/24"))))
55
+
56
+ assertThat(line).contains("wlan0#1")
57
+ assertThat(line).contains("192.168.43.79/24,10.1.2.3/24")
58
+ }
59
+ }
@@ -0,0 +1,35 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ import org.assertj.core.api.Assertions.assertThat
4
+ import org.junit.Test
5
+
6
+ class ScopedLocalNetworkPermissionTest {
7
+ @Test
8
+ fun `Android 17 preserves implicit access for the SDK 36 host`() {
9
+ assertThat(ScopedSoftApNetwork.hasLocalNetworkPermission(37, 36) {
10
+ error("Legacy targets must not query the unneeded runtime grant")
11
+ }).isTrue()
12
+ }
13
+
14
+ @Test
15
+ fun `older Android does not query a permission it cannot enforce`() {
16
+ assertThat(ScopedSoftApNetwork.hasLocalNetworkPermission(36, 37) {
17
+ error("Older Android must not query the new permission")
18
+ }).isTrue()
19
+ }
20
+
21
+ @Test
22
+ fun `Android 17 denies a target 37 host without the grant`() {
23
+ assertThat(ScopedSoftApNetwork.hasLocalNetworkPermission(37, 37) { false }).isFalse()
24
+ }
25
+
26
+ @Test
27
+ fun `Android 17 allows a target 37 host with the grant`() {
28
+ assertThat(ScopedSoftApNetwork.hasLocalNetworkPermission(37, 37) { true }).isTrue()
29
+ }
30
+
31
+ @Test
32
+ fun `future Android and target versions still enforce the grant`() {
33
+ assertThat(ScopedSoftApNetwork.hasLocalNetworkPermission(38, 38) { false }).isFalse()
34
+ }
35
+ }
@@ -0,0 +1,96 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ import com.mentra.bluetoothsdk.hotspot.ScopedNetworkState.Failure
4
+ import org.assertj.core.api.Assertions.assertThat
5
+ import org.junit.Test
6
+
7
+ class ScopedNetworkErrorTest {
8
+
9
+ @Test
10
+ fun `no failure maps to no error`() {
11
+ assertThat(ScopedNetworkError.from(Failure.NONE, "ssid", 30_000)).isNull()
12
+ }
13
+
14
+ @Test
15
+ fun `each failure maps to its typed error and stable code`() {
16
+ assertThat(ScopedNetworkError.from(Failure.TIMEOUT, "ap", 30_000))
17
+ .isInstanceOf(ScopedNetworkError.Timeout::class.java)
18
+ .extracting("code")
19
+ .isEqualTo(ScopedNetworkError.CODE_TIMEOUT)
20
+
21
+ assertThat(ScopedNetworkError.from(Failure.UNAVAILABLE, "ap", 30_000))
22
+ .isInstanceOf(ScopedNetworkError.Unavailable::class.java)
23
+
24
+ assertThat(ScopedNetworkError.from(Failure.LOST, "ap", 30_000))
25
+ .isInstanceOf(ScopedNetworkError.Lost::class.java)
26
+
27
+ assertThat(ScopedNetworkError.from(Failure.REQUEST_FAILED, "ap", 30_000))
28
+ .isInstanceOf(ScopedNetworkError.RequestFailed::class.java)
29
+ }
30
+
31
+ /**
32
+ * A denied permission must be its own error, not a timeout: otherwise it looks like a flaky
33
+ * hotspot and costs 30 seconds before failing.
34
+ */
35
+ @Test
36
+ fun `permission denial is distinct from a timeout`() {
37
+ val denied = ScopedNetworkError.from(Failure.PERMISSION_DENIED, "ap", 30_000)
38
+
39
+ assertThat(denied).isInstanceOf(ScopedNetworkError.PermissionDenied::class.java)
40
+ assertThat(denied?.code).isEqualTo(ScopedNetworkError.CODE_PERMISSION_DENIED)
41
+ assertThat(denied?.code).isNotEqualTo(ScopedNetworkError.CODE_TIMEOUT)
42
+ }
43
+
44
+ @Test
45
+ fun `error codes are stable and unique`() {
46
+ val codes =
47
+ listOf(
48
+ ScopedNetworkError.CODE_PERMISSION_DENIED,
49
+ ScopedNetworkError.CODE_TIMEOUT,
50
+ ScopedNetworkError.CODE_UNAVAILABLE,
51
+ ScopedNetworkError.CODE_LOST,
52
+ ScopedNetworkError.CODE_REQUEST_FAILED,
53
+ ScopedNetworkError.CODE_NO_LOCAL_ADDRESS,
54
+ ScopedNetworkError.CODE_NOT_READY,
55
+ ScopedNetworkError.CODE_WIFI_DISABLED,
56
+ ScopedNetworkError.CODE_VPN_ACTIVE,
57
+ )
58
+
59
+ assertThat(codes).doesNotHaveDuplicates()
60
+ assertThat(codes).allMatch { it.startsWith("SOFTAP_") }
61
+ }
62
+
63
+ @Test
64
+ fun `messages name the ssid so a log line is actionable`() {
65
+ assertThat(ScopedNetworkError.Timeout("MentraLive-1234", 30_000).message)
66
+ .contains("MentraLive-1234")
67
+ .contains("30000")
68
+ assertThat(ScopedNetworkError.Lost("MentraLive-1234").message).contains("MentraLive-1234")
69
+ }
70
+
71
+ @Test
72
+ fun `a capturing vpn is named as the cause and tells the user what to change`() {
73
+ val error = ScopedNetworkError.VpnCapturesApp()
74
+ assertThat(error.code).isEqualTo(ScopedNetworkError.CODE_VPN_ACTIVE)
75
+ assertThat(error.message).contains("VPN").contains("split tunneling")
76
+ }
77
+
78
+ /**
79
+ * "Joined but not usable" has to be its own code. Reported as a join success it becomes an ICE
80
+ * answer with no candidates, which is the symptom several unrelated faults share.
81
+ */
82
+ @Test
83
+ fun `a network that joined but never became usable names the missing source`() {
84
+ val error = ScopedNetworkError.NotReady("MentraLive-1234", "the kernel interface table")
85
+
86
+ assertThat(error.code).isEqualTo(ScopedNetworkError.CODE_NOT_READY)
87
+ assertThat(error.code).isNotEqualTo(ScopedNetworkError.CODE_NO_LOCAL_ADDRESS)
88
+ assertThat(error.message).contains("MentraLive-1234").contains("the kernel interface table")
89
+ }
90
+
91
+ @Test
92
+ fun `the local network permission name is the platform constant`() {
93
+ assertThat(ScopedNetworkError.LOCAL_NETWORK_PERMISSION)
94
+ .isEqualTo("android.permission.ACCESS_LOCAL_NETWORK")
95
+ }
96
+ }
@@ -0,0 +1,187 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ import com.mentra.bluetoothsdk.hotspot.ScopedNetworkState.Failure
4
+ import com.mentra.bluetoothsdk.hotspot.ScopedNetworkState.Phase
5
+ import org.assertj.core.api.Assertions.assertThat
6
+ import org.junit.Test
7
+
8
+ /**
9
+ * ConnectivityManager gives no ordering or at-most-once guarantee, so these cover the awkward
10
+ * orderings that would otherwise only show up on a device: duplicate callbacks, a callback from a
11
+ * request that was already replaced, and a timeout racing a late onAvailable.
12
+ */
13
+ class ScopedNetworkLifecycleTest {
14
+
15
+ @Test
16
+ fun `starts idle`() {
17
+ val state = ScopedNetworkState()
18
+ assertThat(state.phase).isEqualTo(Phase.IDLE)
19
+ assertThat(state.failure).isEqualTo(Failure.NONE)
20
+ assertThat(state.isActive).isFalse()
21
+ }
22
+
23
+ @Test
24
+ fun `happy path is idle to requesting to available`() {
25
+ val state = ScopedNetworkState()
26
+ val generation = state.startRequest()
27
+ assertThat(state.phase).isEqualTo(Phase.REQUESTING)
28
+
29
+ assertThat(state.onAvailable(generation)).isTrue()
30
+ assertThat(state.phase).isEqualTo(Phase.AVAILABLE)
31
+ assertThat(state.failure).isEqualTo(Failure.NONE)
32
+ }
33
+
34
+ @Test
35
+ fun `a duplicate onAvailable does not re-transition`() {
36
+ val state = ScopedNetworkState()
37
+ val generation = state.startRequest()
38
+
39
+ assertThat(state.onAvailable(generation)).isTrue()
40
+ assertThat(state.onAvailable(generation)).isFalse()
41
+ assertThat(state.phase).isEqualTo(Phase.AVAILABLE)
42
+ }
43
+
44
+ @Test
45
+ fun `losing an available network reports LOST`() {
46
+ val state = ScopedNetworkState()
47
+ val generation = state.startRequest()
48
+ state.onAvailable(generation)
49
+
50
+ assertThat(state.onLost(generation)).isTrue()
51
+ assertThat(state.phase).isEqualTo(Phase.LOST)
52
+ assertThat(state.failure).isEqualTo(Failure.LOST)
53
+ }
54
+
55
+ @Test
56
+ fun `a second onLost is a no-op`() {
57
+ val state = ScopedNetworkState()
58
+ val generation = state.startRequest()
59
+ state.onAvailable(generation)
60
+ state.onLost(generation)
61
+
62
+ assertThat(state.onLost(generation)).isFalse()
63
+ }
64
+
65
+ @Test
66
+ fun `timeout while still requesting fails as TIMEOUT`() {
67
+ val state = ScopedNetworkState()
68
+ val generation = state.startRequest()
69
+
70
+ assertThat(state.onTimeout(generation)).isTrue()
71
+ assertThat(state.phase).isEqualTo(Phase.LOST)
72
+ assertThat(state.failure).isEqualTo(Failure.TIMEOUT)
73
+ }
74
+
75
+ /** The join succeeded just before the watchdog fired; the network must stay usable. */
76
+ @Test
77
+ fun `a timeout arriving after onAvailable is ignored`() {
78
+ val state = ScopedNetworkState()
79
+ val generation = state.startRequest()
80
+ state.onAvailable(generation)
81
+
82
+ assertThat(state.onTimeout(generation)).isFalse()
83
+ assertThat(state.phase).isEqualTo(Phase.AVAILABLE)
84
+ assertThat(state.failure).isEqualTo(Failure.NONE)
85
+ }
86
+
87
+ @Test
88
+ fun `onUnavailable fails as UNAVAILABLE`() {
89
+ val state = ScopedNetworkState()
90
+ val generation = state.startRequest()
91
+
92
+ assertThat(state.onUnavailable(generation)).isTrue()
93
+ assertThat(state.failure).isEqualTo(Failure.UNAVAILABLE)
94
+ }
95
+
96
+ @Test
97
+ fun `a SecurityException maps to PERMISSION_DENIED`() {
98
+ val state = ScopedNetworkState()
99
+ val generation = state.startRequest()
100
+
101
+ assertThat(state.onRequestFailed(generation, permissionDenied = true)).isTrue()
102
+ assertThat(state.failure).isEqualTo(Failure.PERMISSION_DENIED)
103
+ }
104
+
105
+ @Test
106
+ fun `any other request throw maps to REQUEST_FAILED`() {
107
+ val state = ScopedNetworkState()
108
+ val generation = state.startRequest()
109
+
110
+ assertThat(state.onRequestFailed(generation, permissionDenied = false)).isTrue()
111
+ assertThat(state.failure).isEqualTo(Failure.REQUEST_FAILED)
112
+ }
113
+
114
+ // -----------------------------------------------------------------
115
+ // Stale generations
116
+ // -----------------------------------------------------------------
117
+
118
+ @Test
119
+ fun `callbacks from a superseded request are rejected`() {
120
+ val state = ScopedNetworkState()
121
+ val first = state.startRequest()
122
+ val second = state.startRequest()
123
+ assertThat(second).isNotEqualTo(first)
124
+
125
+ assertThat(state.onAvailable(first)).isFalse()
126
+ assertThat(state.onLost(first)).isFalse()
127
+ assertThat(state.onUnavailable(first)).isFalse()
128
+ assertThat(state.onTimeout(first)).isFalse()
129
+ assertThat(state.phase).isEqualTo(Phase.REQUESTING)
130
+
131
+ assertThat(state.onAvailable(second)).isTrue()
132
+ }
133
+
134
+ @Test
135
+ fun `a late onLost after release does not resurrect state`() {
136
+ val state = ScopedNetworkState()
137
+ val generation = state.startRequest()
138
+ state.onAvailable(generation)
139
+ state.release()
140
+
141
+ assertThat(state.onLost(generation)).isFalse()
142
+ assertThat(state.failure).isEqualTo(Failure.NONE)
143
+ }
144
+
145
+ @Test
146
+ fun `release is idempotent`() {
147
+ val state = ScopedNetworkState()
148
+ val generation = state.startRequest()
149
+ state.onAvailable(generation)
150
+
151
+ assertThat(state.release()).isTrue()
152
+ assertThat(state.release()).isFalse()
153
+ }
154
+
155
+ @Test
156
+ fun `release from idle is a no-op`() {
157
+ assertThat(ScopedNetworkState().release()).isFalse()
158
+ }
159
+
160
+ @Test
161
+ fun `reset returns the machine to idle for reuse`() {
162
+ val state = ScopedNetworkState()
163
+ val generation = state.startRequest()
164
+ state.onLost(generation)
165
+ state.reset()
166
+
167
+ assertThat(state.phase).isEqualTo(Phase.IDLE)
168
+ assertThat(state.failure).isEqualTo(Failure.NONE)
169
+
170
+ val next = state.startRequest()
171
+ assertThat(state.onAvailable(next)).isTrue()
172
+ }
173
+
174
+ /** Ten start/stop cycles must leave no residue, matching the rejoin soak requirement. */
175
+ @Test
176
+ fun `ten join and release cycles end clean`() {
177
+ val state = ScopedNetworkState()
178
+ repeat(10) {
179
+ val generation = state.startRequest()
180
+ assertThat(state.onAvailable(generation)).isTrue()
181
+ assertThat(state.release()).isTrue()
182
+ state.reset()
183
+ assertThat(state.phase).isEqualTo(Phase.IDLE)
184
+ }
185
+ assertThat(state.generation).isEqualTo(10)
186
+ }
187
+ }