|
| 1 | +package com.openminis.app.plugins |
| 2 | + |
| 3 | +import com.openminis.app.plugins.DevSuiteStore.Status |
| 4 | +import org.junit.Assert.assertEquals |
| 5 | +import org.junit.Assert.assertFalse |
| 6 | +import org.junit.Assert.assertNull |
| 7 | +import org.junit.Assert.assertTrue |
| 8 | +import org.junit.Test |
| 9 | + |
| 10 | +/** |
| 11 | + * [T-dev-suite-prefs-storm] Contract for the SharedPreferences write throttle. |
| 12 | + * |
| 13 | + * The installer feeds every apt/pip output line through |
| 14 | + * [DevSuiteStore.update] — hundreds of full-XML prefs rewrites per install |
| 15 | + * used to queue disk writes that QueuedWork.waitToFinish() could later force |
| 16 | + * the MAIN thread to drain (the install-crash amplifier). The contract: |
| 17 | + * only STATUS transitions persist; progress lines are in-memory only. |
| 18 | + * |
| 19 | + * Pure functions only (repo convention: framework entry points are |
| 20 | + * documented trade-offs, not unit-test targets). |
| 21 | + */ |
| 22 | +class DevSuiteStoreThrottleTest { |
| 23 | + |
| 24 | + // -- shouldPersistComponentState -- |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `progress-line refreshes with the same status never persist`() { |
| 28 | + // The apt/pip line stream: same status, new line, hundreds of times. |
| 29 | + assertFalse(DevSuiteStore.shouldPersistComponentState(Status.INSTALLING, Status.INSTALLING)) |
| 30 | + assertFalse(DevSuiteStore.shouldPersistComponentState(Status.INSTALLED, Status.INSTALLED)) |
| 31 | + assertFalse(DevSuiteStore.shouldPersistComponentState(Status.FAILED, Status.FAILED)) |
| 32 | + assertFalse(DevSuiteStore.shouldPersistComponentState(Status.NOT_INSTALLED, Status.NOT_INSTALLED)) |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + fun `status transitions always persist`() { |
| 37 | + // First write (nothing known yet), every real transition, and the |
| 38 | + // purge path back to NOT_INSTALLED. |
| 39 | + assertTrue(DevSuiteStore.shouldPersistComponentState(null, Status.INSTALLING)) |
| 40 | + assertTrue(DevSuiteStore.shouldPersistComponentState(Status.INSTALLING, Status.INSTALLED)) |
| 41 | + assertTrue(DevSuiteStore.shouldPersistComponentState(Status.INSTALLING, Status.FAILED)) |
| 42 | + assertTrue(DevSuiteStore.shouldPersistComponentState(Status.FAILED, Status.INSTALLING)) |
| 43 | + assertTrue(DevSuiteStore.shouldPersistComponentState(Status.INSTALLED, Status.NOT_INSTALLED)) |
| 44 | + } |
| 45 | + |
| 46 | + // -- encode / decode round trip -- |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `encode decode round trip preserves every field`() { |
| 50 | + val raw = DevSuiteStore.encodeComponentState(Status.INSTALLING, "apt 42% (Reading database…)", 1_728_950_000_000L) |
| 51 | + val state = DevSuiteStore.decodeComponentState("nodejs-suite", raw)!! |
| 52 | + |
| 53 | + assertEquals("nodejs-suite", state.componentId) |
| 54 | + assertEquals(Status.INSTALLING, state.status) |
| 55 | + assertEquals("apt 42% (Reading database…)", state.progressLine) |
| 56 | + assertEquals(1_728_950_000_000L, state.updatedAtMs) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `decode tolerates absent and corrupt rows without throwing`() { |
| 61 | + assertNull(DevSuiteStore.decodeComponentState("x", null)) |
| 62 | + |
| 63 | + // Corrupt/legacy row: unknown status token falls back to |
| 64 | + // NOT_INSTALLED, missing timestamp decodes as 0 — snapshot() must |
| 65 | + // never throw on prefs written by an older build. |
| 66 | + val state = DevSuiteStore.decodeComponentState("x", "GARBAGE")!! |
| 67 | + assertEquals(Status.NOT_INSTALLED, state.status) |
| 68 | + assertEquals("", state.progressLine) |
| 69 | + assertEquals(0L, state.updatedAtMs) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `decode degrades gracefully when a line contains the separator`() { |
| 74 | + // split(limit = 3) keeps the remainder in the LAST field, so a |
| 75 | + // progress line that itself contains \u0001 swallows the timestamp |
| 76 | + // (toLongOrNull → null → 0). The status stays intact and nothing |
| 77 | + // throws — that is the pinned contract: prefs corruption degrades, |
| 78 | + // never crashes snapshot(). |
| 79 | + val raw = "INSTALLING\u0001line\u0001with\u0001sep\u0001123" |
| 80 | + val state = DevSuiteStore.decodeComponentState("x", raw)!! |
| 81 | + assertEquals(Status.INSTALLING, state.status) |
| 82 | + assertEquals("line", state.progressLine) |
| 83 | + assertEquals(0L, state.updatedAtMs) |
| 84 | + } |
| 85 | +} |
0 commit comments