Commit b8c44d0
committed
refactor(util): extract pure ScanStateReducer from VpnManager (#47)
Lifts the regex-based scan-line scraping out of the private stateful
`VpnManager.parseScanLine` into an `internal` pure
`(prev: ScanStateBundle, line: String) -> ScanStateBundle` function in
a new `ScanStateReducer.kt`. The singleton's public API is unchanged;
`parseScanLine` becomes a 10-line wrapper that threads its three
StateFlows (`_scanStatus`, `_activeResolvers`, `_connectionWarning`)
through the reducer and emits new values only when they change.
## Why
`parseScanLine` ran up to 10 regex matches per Go-core log line and
mutated 9 `MutableStateFlow` fields. It was private and stateful, so
the regex contract could not be unit-tested without driving the
singleton through `appendLog` and observing StateFlows via Turbine.
This made every change to the log-scraping logic risky and
uncharacterizable. Extracting a pure reducer creates a trivially
table-testable seam (the cheapest seam for the future god-object
decompose of `VpnManager`, finding 9) at zero behavior change.
## What changed
### `android/app/src/main/java/com/masterdns/vpn/util/ScanStateReducer.kt` (new, +155)
- `internal data class ScanStateBundle` -- immutable input/output bundle
holding `scanStatus: VpnManager.ScanStatus`, `activeResolvers: List<String>`,
`connectionWarning: String?`.
- `internal object ScanStateReducer` -- owns the 10 regex constants moved
verbatim from `VpnManager.kt` (patterns byte-identical, same
`RegexOption.IGNORE_CASE` / `DOT_MATCHES_ALL` where applicable):
`INDEXED_PROGRESS`, `TOTAL_CANDIDATES`, `SCAN_TOTALS`, `ACTIVE_RESOLVERS`,
`TOTAL_ACTIVE`, `REMAINING`, `SYNCED_MTU`, `RESOLVER_ADDED`,
`RESOLVER_REMOVED`, `SESSION_INIT_BACKOFF`.
- `internal fun reduce(prev, line)` -- pre-computes the 6 early-return
matches (`scanMatch`, `activeMatch`, `totalActiveMatch`, `remainingMatch`,
`syncedMtuMatch`, `testingMtu`) before a `when` cascade, then a single
`if (anyMatched) return` short-circuits the trailing `MTU Testing
Completed` / `Session Initialized Successfully` triggers and
`SESSION_INIT_BACKOFF` blocks. The 4 non-returning pre-blocks
(`RESOLVER_ADDED`, `RESOLVER_REMOVED`, `INDEXED_PROGRESS`,
`TOTAL_CANDIDATES`) run before the cascade, unchanged.
### `android/app/src/main/java/com/masterdns/vpn/util/VpnManager.kt` (+14/-131)
- `parseScanLine` body (was 93 lines, ended at line 451) replaced with
a 10-line wrapper: snapshot the 3 StateFlows into a `ScanStateBundle`,
call `ScanStateReducer.reduce(prev, line)`, emit each changed field
back via `if (next.X != prev.X) _X.value = next.X` guards.
- The 10 `private val *_REGEX` constants (previously at lines 101-140)
removed from `VpnManager` -- they now live in `ScanStateReducer`.
- `TIMESTAMP_CANDIDATES` and `TimestampCandidate` (timestamp
normalization, not scan-state) stay in `VpnManager`, untouched.
- The remaining `_scanStatus.value = _scanStatus.value.copy(...)` site
inside `updateState` (the `VpnState.CONNECTED` reset, line 147) is
unrelated to scan-line parsing and is intentionally preserved.
### `android/app/src/test/java/com/masterdns/vpn/util/ScanStateReducerTest.kt` (new, +120)
- 14 plain JUnit4 `@Test` methods (no new test dependencies -- reuses the
existing `testImplementation("junit:junit:4.13.2")` and
`kotlinx-coroutines-test:1.9.0` orchestrated by plan 005). Pattern
follows the existing `GlobalSettingsPortRangeTest.kt` skeleton
(same package `com.masterdns.vpn.util`, plain `assertEquals`/`assertTrue`).
- Cases cover: empty/non-matching lines, the `INDEXED_PROGRESS` happy
path and non-numeric-total skip, `SCAN_TOTALS` Accepted/Rejected,
`RESOLVER_ADDED` idempotence, `RESOLVER_REMOVED` removal,
`"Testing MTU sizes"` / `"MTU Testing Completed"` /
`"Session Initialized Successfully"` triggers, `SESSION_INIT_BACKOFF`,
the precedence invariant (case 13: a line matching both
`SCAN_TOTALS` and `ACTIVE_RESOLVERS` fires only the first), and
`SYNCED_MTU`.
## Behavior preservation
Public API of `VpnManager` (`state`, `scanStatus`, `activeResolvers`,
`connect`, `disconnect`, `appendLog`, `appendCoreLog`, etc.) is
unchanged -- `MasterDnsVpnService.kt`, `VpnTileService.kt`,
`ResolversScreen.kt`, and `HomeStatusCards.kt` continue to consume
the same StateFlows with no edits. The `if (next != prev)` emission
guards are an additive optimization: observers receive exactly the
same sequence of distinct StateFlow values as before; the no-match
case no longer fires a redundant emission.
## Scope
3 files in `android/app/src/main/java/com/masterdns/vpn/util/` and
`android/app/src/test/java/com/masterdns/vpn/util/`. No out-of-scope
edits; no `mobile/`, no `cmd/`/`internal/`, no `go.mod`/`go.sum`, no
other UI files. The `plans/` folder (locally gitignored via
`.git/info/exclude`) is not committed and stays on the local
checkout for reference.
## Verification
Gradle `compileDebugKotlin` + `testDebugUnitTest` were not run locally
per the user's no-local-build constraint; this PR is the gate --
`android-ci.yml` runs `assembleDebug` which compiles and runs the
unit-test suite on push. The 14 new tests are JVM-runnable (no
Robolectric, no device) and target the pure reducer directly.
Plan 005's `ResolverAnalyzerTest` and the existing
`GlobalSettingsPortRangeTest` prove the testDebugUnitTest task is
wired and runs in CI.
## Maintenance notes
- **Adding a new scan-state field**: extend `ScanStatus` and
`ScanStateBundle`, then add a new `?.let { match -> ... }` block in
`ScanStateReducer.reduce`. Do NOT re-mutate `_scanStatus` from
`parseScanLine` directly -- that is the anti-pattern this refactor
removed.
- **Performance**: the `if (next != prev)` guards are pure
optimizations; if a StateFlow-observer race appears under heavy
load, removing the guards is safe (emitting an identical value is
legal and cheap).
- **`ScanStateReducer` is the first Hilt-injectable seam for
`VpnManager`**: a follow-up can make it a `@Singleton` injected
interface so an `androidTest` can swap a fake reducer that asserts
on the lines it received.
Squash-merges planner commits 985dfb0 (extraction) and 8b02a0c
(precedence-cascade bugfix discovered in self-review).1 parent 6fd18bd commit b8c44d0
3 files changed
Lines changed: 284 additions & 131 deletions
File tree
- android/app/src
- main/java/com/masterdns/vpn/util
- test/java/com/masterdns/vpn/util
Lines changed: 155 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
Lines changed: 9 additions & 131 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | 101 | | |
142 | 102 | | |
143 | 103 | | |
| |||
357 | 317 | | |
358 | 318 | | |
359 | 319 | | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
451 | 329 | | |
452 | 330 | | |
453 | 331 | | |
| |||
0 commit comments