diff --git a/docs/superpowers/evidence/focus-owner-kotlin-test-split-2026-08-16.json b/docs/superpowers/evidence/focus-owner-kotlin-test-split-2026-08-16.json new file mode 100644 index 00000000..73cc53b2 --- /dev/null +++ b/docs/superpowers/evidence/focus-owner-kotlin-test-split-2026-08-16.json @@ -0,0 +1,38 @@ +{ + "intent": "Split the largest focus_owner Kotlin test file under the cap, and record why the Kotlin sources and the second test file cannot follow.", + "scope": [ + "android/app/src/test/.../EnforcementDecisionTest.kt (418 -> 168) + EnforcementDecisionCurfewTest.kt, EnforcementFixtures.kt", + "Not split: EnforcementRunner.kt (564), DevicePolicyBridge.kt (415), FocusPolicy.kt (277), EnforcementDecision.kt (274), EnforcementLogTest.kt (251)", + "Non-goal: no assertion changed" + ], + "changes": [ + "The shared policy builders, the home coordinates and the installed-package set moved to an internal EnforcementFixtures object, and both test classes delegate to it. Kotlin has no part/part of, so an object in the same package is the equivalent - a plain second class could not see the originals' private members.", + "EnforcementRunner.kt was examined and left alone: its location block reads the private constructor property `context` and the private prefs() six and five times respectively, so extension functions cannot reach them.", + "EnforcementLogTest.kt is one line over the cap and was reverted after a split attempt: it defines helpers (`candidate`, `window`) between its test methods rather than in a fixture block, so any cut leaves unresolved references. One line is not worth restructuring a test file for." + ], + "verification": [ + { + "command": "JAVA_HOME=/usr/lib/jvm/java-21-openjdk ./gradlew testDebugUnitTest --offline", + "result": "fail twice, then pass", + "evidence": "BUILD SUCCESSFUL. Two compile failures first, both real: 'Unresolved reference installed' when a fixture was missed, then the same for the log test's inline helpers." + }, + { + "command": "sum the tests= and failures= attributes across build/app/test-results/testDebugUnitTest/*.xml", + "result": "pass", + "evidence": "40 tests, 0 failures, now across 3 XML result files instead of 2 - matching the baseline of 40 measured before any edit. Gradle reports UP-TO-DATE rather than a count, so the XML is the only way to see it." + }, + { + "command": "bash ~/utils/scripts/check_file_length.sh --all | grep '\\.kt:'", + "result": "partial", + "evidence": "EnforcementDecisionTest.kt is gone from the list. Five Kotlin files remain, four of them sources with the shared-state problem and one a test that is one line over." + } + ], + "risks": [ + "JAVA_HOME must be pinned to JDK 21 for this build; the default JDK 26 breaks the AGP jlink transform. Any future session running these tests needs the same prefix.", + "Four Kotlin sources stay over the cap. Splitting EnforcementRunner means passing context and the prefs accessor explicitly, which changes its constructor contract." + ], + "rollback": [ + "git revert the commit; EnforcementDecisionTest.kt returns to 418 lines and the two new files disappear.", + "After rollback, re-run the gradle unit tests and expect 40 tests, 0 failures." + ] +} diff --git a/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementDecisionCurfewTest.kt b/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementDecisionCurfewTest.kt new file mode 100644 index 00000000..4b3069bc --- /dev/null +++ b/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementDecisionCurfewTest.kt @@ -0,0 +1,223 @@ +// Curfew and always-blocked decision cases. +// +// Split out of EnforcementDecisionTest.kt to keep each file under the cap. + +package com.kuhy.focus_owner +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class EnforcementDecisionCurfewTest { + + private val homeLat = EnforcementFixtures.homeLat + private val homeLon = EnforcementFixtures.homeLon + private val metresPerDegreeLat = EnforcementFixtures.metresPerDegreeLat + + private fun policy(withCoordinates: Boolean = true, curfew: String? = """{"start":"23:00","end":"05:00"}""") = + EnforcementFixtures.policy(withCoordinates, curfew) + + private fun policyWithAlwaysBlocked() = EnforcementFixtures.policyWithAlwaysBlocked() + + private val installed = EnforcementFixtures.installed + + private fun latOffset(metres: Double) = EnforcementFixtures.latOffset(metres) + + + @Test + fun `workout releases youtube and nothing else`() { + val decision = EnforcementDecision.evaluate( + policy(), + EnforcementInputs( + installed, + 23 * 60 + 30, + homeLat, + homeLon, + workoutActive = true, + ), + ) + assertEquals(EnforcementReason.WORKOUT, decision.reason) + assertTrue("com.google.android.youtube" in decision.packagesToShow) + assertTrue("com.discord" in decision.packagesToHide) + assertTrue("com.evil" in decision.packagesToHide) + } + + @Test + fun `hide and show partition the installed set`() { + // packagesToShow is the complete allowed set, never a delta, so a + // missed run self-repairs on the next one. + val decision = EnforcementDecision.evaluate( + policy(), + EnforcementInputs(installed, 12 * 60, homeLat, homeLon), + ) + assertEquals(installed, decision.packagesToHide + decision.packagesToShow) + assertTrue(decision.packagesToHide.intersect(decision.packagesToShow).isEmpty()) + } + + @Test + fun `an unknown schema version is rejected`() { + val thrown = try { + FocusPolicy.parse("""{"schema_version": 999, "home": {}}""") + false + } catch (_: PolicyFormatException) { + true + } + assertTrue("expected PolicyFormatException", thrown) + } + + @Test + fun `prefix protection matches whole labels only`() { + val p = policy() + assertTrue(p.isProtected("com.android.settings")) + assertTrue(p.isProtected("com.android.settings.sub")) + assertFalse(p.isProtected("com.android.settingsomething")) + } + + @Test + fun `distance and threshold are reported so the phone can explain itself`() { + val decision = EnforcementDecision.evaluate( + policy(), + EnforcementInputs(installed, 12 * 60, latOffset(5000.0), homeLon), + ) + // ~5 km away, and the value it was actually compared against. + assertEquals(5000.0, decision.distanceM!!, 50.0) + assertEquals(150.0, decision.thresholdM!!, 0.001) + assertEquals(false, decision.insideFence) + } + + @Test + fun `hysteresis widens the threshold only for a phone already inside`() { + // 160 m out: outside the bare 150 m radius, inside 150 + 30. This is + // what stops a phone parked on the boundary from flapping. + val outside = EnforcementDecision.evaluate( + policy(), + EnforcementInputs( + installed, + 12 * 60, + latOffset(160.0), + homeLon, + currentlyEnforcing = false, + ), + ) + assertEquals(150.0, outside.thresholdM!!, 0.001) + assertEquals(EnforcementReason.AWAY, outside.reason) + + val inside = EnforcementDecision.evaluate( + policy(), + EnforcementInputs( + installed, + 12 * 60, + latOffset(160.0), + homeLon, + currentlyEnforcing = true, + ), + ) + assertEquals(180.0, inside.thresholdM!!, 0.001) + assertEquals(EnforcementReason.AT_HOME, inside.reason) + } + + @Test + fun `an unanswerable fence reports null rather than outside`() { + // No fix at all. Recording "outside" here would drop hysteresis on the + // next pass at exactly the moment the input is least reliable. + val decision = EnforcementDecision.evaluate( + policy(), + EnforcementInputs(installed, 12 * 60, latitude = null, longitude = null), + ) + assertEquals(EnforcementReason.LOCATION_UNKNOWN, decision.reason) + assertEquals(null, decision.insideFence) + assertEquals(null, decision.distanceM) + } + + @Test + fun `no home coordinates reports an unknown distance, not zero`() { + // Fabricating 0 m would read on screen as "you are exactly at home". + val decision = EnforcementDecision.evaluate( + policy(withCoordinates = false), + EnforcementInputs(installed, 12 * 60, homeLat, homeLon), + ) + assertEquals(null, decision.distanceM) + assertEquals(true, decision.insideFence) + } + + @Test + fun `every hidden package carries a reason`() { + val decision = EnforcementDecision.evaluate( + policy(), + EnforcementInputs(installed, 12 * 60, homeLat, homeLon), + ) + // The UI lists these per package, so a gap would render as a blank + // explanation next to a hidden app. + assertEquals(decision.packagesToHide, decision.hideReasons.keys) + assertTrue( + decision.hideReasons.values.all { it == HideReason.NOT_IN_ALLOWLIST }, + ) + } + + @Test + fun `curfew hiding is attributed to the night list`() { + val decision = EnforcementDecision.evaluate( + policy(), + EnforcementInputs(installed, 23 * 60 + 30, homeLat, homeLon), + ) + assertEquals(EnforcementReason.CURFEW, decision.reason) + // com.discord is day-allowed but not night-allowed, so the distinction + // matters: "not in allowlist" would be misleading. + assertEquals( + HideReason.NOT_IN_NIGHT_ALLOWLIST, + decision.hideReasons["com.discord"], + ) + } + + @Test + fun `always-blocked is attributed as such even when away`() { + val decision = EnforcementDecision.evaluate( + policyWithAlwaysBlocked(), + EnforcementInputs(installed, 12 * 60, latOffset(5000.0), homeLon), + ) + assertEquals(EnforcementReason.AWAY, decision.reason) + assertEquals( + HideReason.ALWAYS_BLOCKED, + decision.hideReasons["com.google.android.youtube"], + ) + } + + @Test + fun `allowed prefixes cover a family of packages`() { + val p = FocusPolicy.parse( + """ + { + "schema_version": 1, + "home": { + "latitude": $homeLat, "longitude": $homeLon, + "radius_m": 150.0, "hysteresis_m": 30.0 + }, + "curfew": {"start":"23:00","end":"05:00"}, + "launcher_package": "com.launcher", + "allowed_packages": ["com.launcher"], + "night_allowed_packages": ["com.launcher"], + "never_disable_prefixes": [], + "workout_unblock_domains": [], + "allowed_prefixes": ["eu.kanade.tachiyomi"], + "night_allowed_prefixes": ["eu.kanade.tachiyomi"], + "browser_packages": [] + } + """.trimIndent(), + ) + assertTrue(p.isAllowed("eu.kanade.tachiyomi.sy", false)) + assertTrue(p.isAllowed("eu.kanade.tachiyomi.extension.all.mangadex", false)) + // Chosen 2026-08-14: manga stays available during the curfew. + assertTrue(p.isAllowed("eu.kanade.tachiyomi.sy", true)) + // Whole labels only. + assertFalse(p.isAllowed("eu.kanade.tachiyomisomething", false)) + } + + @Test + fun `an asset without prefix keys still parses`() { + // Back-compat: a policy rendered before prefixes existed must degrade + // to exact matching rather than failing to parse at all, since an + // unparsable policy skips the pass entirely. + assertTrue(policy().allowedPrefixes.isEmpty()) + assertTrue(policy().nightAllowedPrefixes.isEmpty()) + } +} diff --git a/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementDecisionTest.kt b/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementDecisionTest.kt index 45c48b9e..06b972bb 100644 --- a/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementDecisionTest.kt +++ b/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementDecisionTest.kt @@ -16,70 +16,18 @@ import org.junit.Test */ class EnforcementDecisionTest { - private val homeLat = 52.2297 - private val homeLon = 21.0122 - private val metresPerDegreeLat = 111_320.0 + private val homeLat = EnforcementFixtures.homeLat + private val homeLon = EnforcementFixtures.homeLon + private val metresPerDegreeLat = EnforcementFixtures.metresPerDegreeLat private fun policy(withCoordinates: Boolean = true, curfew: String? = """{"start":"23:00","end":"05:00"}""") = - FocusPolicy.parse( - """ - { - "schema_version": 1, - "home": { - "latitude": ${if (withCoordinates) homeLat else "null"}, - "longitude": ${if (withCoordinates) homeLon else "null"}, - "radius_m": 150.0, - "hysteresis_m": 30.0 - }, - "curfew": ${curfew ?: "null"}, - "launcher_package": "com.launcher", - "allowed_packages": ["com.launcher","pl.mbank","com.discord","com.google.android.youtube"], - "night_allowed_packages": ["com.launcher","pl.mbank"], - "never_disable_prefixes": ["com.android.settings"], - "workout_unblock_domains": ["youtube.com","googlevideo.com"], - "browser_packages": [] - } - """.trimIndent(), - ) + EnforcementFixtures.policy(withCoordinates, curfew) - /** - * A policy that names YouTube as always-blocked and drops it from the - * allowlist, since the exporter guarantees those two never overlap. - */ - private fun policyWithAlwaysBlocked() = - FocusPolicy.parse( - """ - { - "schema_version": 1, - "home": { - "latitude": $homeLat, - "longitude": $homeLon, - "radius_m": 150.0, - "hysteresis_m": 30.0 - }, - "curfew": {"start":"23:00","end":"05:00"}, - "launcher_package": "com.launcher", - "allowed_packages": ["com.launcher","pl.mbank","com.discord"], - "night_allowed_packages": ["com.launcher","pl.mbank"], - "never_disable_prefixes": ["com.android.settings"], - "workout_unblock_domains": ["youtube.com"], - "browser_packages": [], - "blockable_system_packages": ["com.google.android.youtube"], - "always_blocked_packages": ["com.google.android.youtube"] - } - """.trimIndent(), - ) + private fun policyWithAlwaysBlocked() = EnforcementFixtures.policyWithAlwaysBlocked() - private val installed = setOf( - "com.launcher", - "pl.mbank", - "com.discord", - "com.google.android.youtube", - "com.evil", - "com.android.settings", - ) + private val installed = EnforcementFixtures.installed - private fun latOffset(metres: Double) = homeLat + metres / metresPerDegreeLat + private fun latOffset(metres: Double) = EnforcementFixtures.latOffset(metres) @Test fun `away from home restores everything except the always-blocked set`() { @@ -218,201 +166,4 @@ class EnforcementDecisionTest { ) assertTrue("com.evil" in decision.packagesToHide) } - - @Test - fun `workout releases youtube and nothing else`() { - val decision = EnforcementDecision.evaluate( - policy(), - EnforcementInputs( - installed, - 23 * 60 + 30, - homeLat, - homeLon, - workoutActive = true, - ), - ) - assertEquals(EnforcementReason.WORKOUT, decision.reason) - assertTrue("com.google.android.youtube" in decision.packagesToShow) - assertTrue("com.discord" in decision.packagesToHide) - assertTrue("com.evil" in decision.packagesToHide) - } - - @Test - fun `hide and show partition the installed set`() { - // packagesToShow is the complete allowed set, never a delta, so a - // missed run self-repairs on the next one. - val decision = EnforcementDecision.evaluate( - policy(), - EnforcementInputs(installed, 12 * 60, homeLat, homeLon), - ) - assertEquals(installed, decision.packagesToHide + decision.packagesToShow) - assertTrue(decision.packagesToHide.intersect(decision.packagesToShow).isEmpty()) - } - - @Test - fun `an unknown schema version is rejected`() { - val thrown = try { - FocusPolicy.parse("""{"schema_version": 999, "home": {}}""") - false - } catch (_: PolicyFormatException) { - true - } - assertTrue("expected PolicyFormatException", thrown) - } - - @Test - fun `prefix protection matches whole labels only`() { - val p = policy() - assertTrue(p.isProtected("com.android.settings")) - assertTrue(p.isProtected("com.android.settings.sub")) - assertFalse(p.isProtected("com.android.settingsomething")) - } - - @Test - fun `distance and threshold are reported so the phone can explain itself`() { - val decision = EnforcementDecision.evaluate( - policy(), - EnforcementInputs(installed, 12 * 60, latOffset(5000.0), homeLon), - ) - // ~5 km away, and the value it was actually compared against. - assertEquals(5000.0, decision.distanceM!!, 50.0) - assertEquals(150.0, decision.thresholdM!!, 0.001) - assertEquals(false, decision.insideFence) - } - - @Test - fun `hysteresis widens the threshold only for a phone already inside`() { - // 160 m out: outside the bare 150 m radius, inside 150 + 30. This is - // what stops a phone parked on the boundary from flapping. - val outside = EnforcementDecision.evaluate( - policy(), - EnforcementInputs( - installed, - 12 * 60, - latOffset(160.0), - homeLon, - currentlyEnforcing = false, - ), - ) - assertEquals(150.0, outside.thresholdM!!, 0.001) - assertEquals(EnforcementReason.AWAY, outside.reason) - - val inside = EnforcementDecision.evaluate( - policy(), - EnforcementInputs( - installed, - 12 * 60, - latOffset(160.0), - homeLon, - currentlyEnforcing = true, - ), - ) - assertEquals(180.0, inside.thresholdM!!, 0.001) - assertEquals(EnforcementReason.AT_HOME, inside.reason) - } - - @Test - fun `an unanswerable fence reports null rather than outside`() { - // No fix at all. Recording "outside" here would drop hysteresis on the - // next pass at exactly the moment the input is least reliable. - val decision = EnforcementDecision.evaluate( - policy(), - EnforcementInputs(installed, 12 * 60, latitude = null, longitude = null), - ) - assertEquals(EnforcementReason.LOCATION_UNKNOWN, decision.reason) - assertEquals(null, decision.insideFence) - assertEquals(null, decision.distanceM) - } - - @Test - fun `no home coordinates reports an unknown distance, not zero`() { - // Fabricating 0 m would read on screen as "you are exactly at home". - val decision = EnforcementDecision.evaluate( - policy(withCoordinates = false), - EnforcementInputs(installed, 12 * 60, homeLat, homeLon), - ) - assertEquals(null, decision.distanceM) - assertEquals(true, decision.insideFence) - } - - @Test - fun `every hidden package carries a reason`() { - val decision = EnforcementDecision.evaluate( - policy(), - EnforcementInputs(installed, 12 * 60, homeLat, homeLon), - ) - // The UI lists these per package, so a gap would render as a blank - // explanation next to a hidden app. - assertEquals(decision.packagesToHide, decision.hideReasons.keys) - assertTrue( - decision.hideReasons.values.all { it == HideReason.NOT_IN_ALLOWLIST }, - ) - } - - @Test - fun `curfew hiding is attributed to the night list`() { - val decision = EnforcementDecision.evaluate( - policy(), - EnforcementInputs(installed, 23 * 60 + 30, homeLat, homeLon), - ) - assertEquals(EnforcementReason.CURFEW, decision.reason) - // com.discord is day-allowed but not night-allowed, so the distinction - // matters: "not in allowlist" would be misleading. - assertEquals( - HideReason.NOT_IN_NIGHT_ALLOWLIST, - decision.hideReasons["com.discord"], - ) - } - - @Test - fun `always-blocked is attributed as such even when away`() { - val decision = EnforcementDecision.evaluate( - policyWithAlwaysBlocked(), - EnforcementInputs(installed, 12 * 60, latOffset(5000.0), homeLon), - ) - assertEquals(EnforcementReason.AWAY, decision.reason) - assertEquals( - HideReason.ALWAYS_BLOCKED, - decision.hideReasons["com.google.android.youtube"], - ) - } - - @Test - fun `allowed prefixes cover a family of packages`() { - val p = FocusPolicy.parse( - """ - { - "schema_version": 1, - "home": { - "latitude": $homeLat, "longitude": $homeLon, - "radius_m": 150.0, "hysteresis_m": 30.0 - }, - "curfew": {"start":"23:00","end":"05:00"}, - "launcher_package": "com.launcher", - "allowed_packages": ["com.launcher"], - "night_allowed_packages": ["com.launcher"], - "never_disable_prefixes": [], - "workout_unblock_domains": [], - "allowed_prefixes": ["eu.kanade.tachiyomi"], - "night_allowed_prefixes": ["eu.kanade.tachiyomi"], - "browser_packages": [] - } - """.trimIndent(), - ) - assertTrue(p.isAllowed("eu.kanade.tachiyomi.sy", false)) - assertTrue(p.isAllowed("eu.kanade.tachiyomi.extension.all.mangadex", false)) - // Chosen 2026-08-14: manga stays available during the curfew. - assertTrue(p.isAllowed("eu.kanade.tachiyomi.sy", true)) - // Whole labels only. - assertFalse(p.isAllowed("eu.kanade.tachiyomisomething", false)) - } - - @Test - fun `an asset without prefix keys still parses`() { - // Back-compat: a policy rendered before prefixes existed must degrade - // to exact matching rather than failing to parse at all, since an - // unparsable policy skips the pass entirely. - assertTrue(policy().allowedPrefixes.isEmpty()) - assertTrue(policy().nightAllowedPrefixes.isEmpty()) - } } diff --git a/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementFixtures.kt b/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementFixtures.kt new file mode 100644 index 00000000..41b6502e --- /dev/null +++ b/focus_owner/android/app/src/test/kotlin/com/kuhy/focus_owner/EnforcementFixtures.kt @@ -0,0 +1,77 @@ +// Shared fixtures for the enforcement decision tests. +// +// Split out of EnforcementDecisionTest.kt to keep each file under the +// 250-line cap; both test classes build policies from these. + +package com.kuhy.focus_owner +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +internal object EnforcementFixtures { + val homeLat = 52.2297 + val homeLon = 21.0122 + val metresPerDegreeLat = 111_320.0 + + fun policy(withCoordinates: Boolean = true, curfew: String? = """{"start":"23:00","end":"05:00"}""") = + FocusPolicy.parse( + """ + { + "schema_version": 1, + "home": { + "latitude": ${if (withCoordinates) homeLat else "null"}, + "longitude": ${if (withCoordinates) homeLon else "null"}, + "radius_m": 150.0, + "hysteresis_m": 30.0 + }, + "curfew": ${curfew ?: "null"}, + "launcher_package": "com.launcher", + "allowed_packages": ["com.launcher","pl.mbank","com.discord","com.google.android.youtube"], + "night_allowed_packages": ["com.launcher","pl.mbank"], + "never_disable_prefixes": ["com.android.settings"], + "workout_unblock_domains": ["youtube.com","googlevideo.com"], + "browser_packages": [] + } + """.trimIndent(), + ) + + /** + * A policy that names YouTube as always-blocked and drops it from the + * allowlist, since the exporter guarantees those two never overlap. + */ + fun policyWithAlwaysBlocked() = + FocusPolicy.parse( + """ + { + "schema_version": 1, + "home": { + "latitude": $homeLat, + "longitude": $homeLon, + "radius_m": 150.0, + "hysteresis_m": 30.0 + }, + "curfew": {"start":"23:00","end":"05:00"}, + "launcher_package": "com.launcher", + "allowed_packages": ["com.launcher","pl.mbank","com.discord"], + "night_allowed_packages": ["com.launcher","pl.mbank"], + "never_disable_prefixes": ["com.android.settings"], + "workout_unblock_domains": ["youtube.com"], + "browser_packages": [], + "blockable_system_packages": ["com.google.android.youtube"], + "always_blocked_packages": ["com.google.android.youtube"] + } + """.trimIndent(), + ) + + val installed = setOf( + "com.launcher", + "pl.mbank", + "com.discord", + "com.google.android.youtube", + "com.evil", + "com.android.settings", + ) + + fun latOffset(metres: Double) = homeLat + metres / metresPerDegreeLat +}