db8da89fdf31

test: LockScreen create-validation — extract pure createValidationError (length→strength→match order) + LockScreenTest 8 tests; v2.2.6 (312 tests)

time
2026-06-10 13:00 UTC · 5 months ago
machine
koh2
agent
claude-opus-4-8
session
test coverage lockscreen
parent
cf5291d978b7
2 files app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+14 −7app/src/test/java/com/peachpasswords/ui/lock/LockScreenTest.kt+52 −0
app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+14 −7
@@ -48,6 +48,18 @@4848 const val MIN_CREATE_STRENGTH = 24949 5050 /**51+ * Validate a create-vault attempt; returns the user-facing error, or null when the52+ * inputs are acceptable. Pure so the create rules (length floor, strength gate,53+ * confirm match — in that order) are unit-testable without the ViewModel.54+ */55+internal fun createValidationError(password: String, confirm: String, score: Int): String? = when {56+    password.length < MIN_PASSWORD_LENGTH -> "password must be at least $MIN_PASSWORD_LENGTH characters"57+    score < MIN_CREATE_STRENGTH -> "Password too weak — try a longer or more varied password"58+    password != confirm -> "passwords do not match"59+    else -> null60+}61+62+/**5163  * Where to land after a successful unlock (brief Fix 3): the last-viewed entry if5264  * it still exists, else the first active entry, else the vault list when the vault5365  * is empty. [resolve] re-validates the stored id against the live vault so a stale@@ -135,13 +147,8 @@135147 136148     fun create(password: String, confirm: String) {137149         if (_state.value.loading) return138-        when {139-            password.length < MIN_PASSWORD_LENGTH ->140-                return _state.update { it.copy(error = "password must be at least $MIN_PASSWORD_LENGTH characters") }141-            scorePassword(password) < MIN_CREATE_STRENGTH ->142-                return _state.update { it.copy(error = "Password too weak — try a longer or more varied password") }143-            password != confirm ->144-                return _state.update { it.copy(error = "passwords do not match") }150+        createValidationError(password, confirm, scorePassword(password))?.let { msg ->151+            return _state.update { it.copy(error = msg) }145152         }146153         _state.update { it.copy(loading = true, error = null) }147154         viewModelScope.launch {

view file

app/src/test/java/com/peachpasswords/ui/lock/LockScreenTest.kt+52 −0
@@ -0,0 +1,52 @@1+package com.peachpasswords.ui.lock2+3+import com.peachpasswords.ui.security.scorePassword4+import org.junit.Assert.assertEquals5+import org.junit.Assert.assertNull6+import org.junit.Assert.assertTrue7+import org.junit.Test8+9+/**10+ * LockScreen suite (brief Suite 6): the create-vault validation rules and their11+ * gating constants. The biometric/auto-lock paths need an instrumented device.12+ */13+class LockScreenTest {14+15+    @Test fun min_password_length_is_twelve() = assertEquals(12, MIN_PASSWORD_LENGTH)16+    @Test fun min_create_strength_is_two() = assertEquals(2, MIN_CREATE_STRENGTH)17+18+    @Test fun password_below_minimum_length_is_rejected() {19+        val err = createValidationError("short", "short", score = 4)20+        assertEquals("password must be at least 12 characters", err)21+    }22+23+    @Test fun weak_password_is_rejected_even_when_long() {24+        // 16 identical chars: long enough but trivially weak (score < 2).25+        val pw = "aaaaaaaaaaaaaaaa"26+        assertTrue("sanity: this password really is weak", scorePassword(pw) < MIN_CREATE_STRENGTH)27+        assertEquals("Password too weak — try a longer or more varied password",28+            createValidationError(pw, pw, scorePassword(pw)))29+    }30+31+    @Test fun mismatched_confirmation_is_rejected() {32+        // Passes length + strength, but the confirmation differs.33+        val err = createValidationError("Str0ng-Passphrase!23", "Str0ng-Passphrase!99", score = 4)34+        assertEquals("passwords do not match", err)35+    }36+37+    @Test fun valid_create_returns_no_error() {38+        assertNull(createValidationError("Str0ng-Passphrase!23", "Str0ng-Passphrase!23", score = 4))39+    }40+41+    @Test fun length_is_checked_before_strength() {42+        // A short password that is also weak surfaces the length error first.43+        assertEquals("password must be at least 12 characters",44+            createValidationError("ab1", "ab1", score = 0))45+    }46+47+    @Test fun strength_is_checked_before_match() {48+        // Long but weak AND mismatched → strength error wins over the match error.49+        assertEquals("Password too weak — try a longer or more varied password",50+            createValidationError("aaaaaaaaaaaaaaaa", "different-confirm", score = 1))51+    }52+}

view file