EntryEditScreenTest.kt

8.0 KB · raw

1package com.peachpasswords.ui.vault23import com.peachpasswords.vault.VaultEntry4import org.junit.Assert.assertEquals5import org.junit.Assert.assertFalse6import org.junit.Assert.assertTrue7import org.junit.Test89class EntryEditScreenTest {1011    // ── Top-bar label (cancel | <title> | save) ───────────────────────────────1213    @Test fun title_bar_label_is_type_specific_for_new_entries() {14        assertEquals("new entry", editTitleBarLabel(isNew = true, type = "login"))15        assertEquals("new note", editTitleBarLabel(isNew = true, type = "note"))16        assertEquals("new card", editTitleBarLabel(isNew = true, type = "card"))17    }1819    @Test fun title_bar_label_is_edit_for_existing_entries() {20        assertEquals("edit", editTitleBarLabel(isNew = false, type = "login"))21        assertEquals("edit", editTitleBarLabel(isNew = false, type = "note"))22    }2324    @Test fun primary_action_label_matches_browser_add_view() {25        assertEquals("create", entryEditPrimaryActionLabel(isNew = true, saving = false))26        assertEquals("save", entryEditPrimaryActionLabel(isNew = false, saving = false))27        assertEquals("saving...", entryEditPrimaryActionLabel(isNew = true, saving = true))28    }2930    @Test fun save_is_disabled_until_browser_minimum_fields_are_present() {31        assertFalse(canSaveEditForm(EditForm(type = "login", title = "")))32        assertTrue(canSaveEditForm(EditForm(type = "login", title = "GitHub")))33        assertFalse(canSaveEditForm(EditForm(type = "card", title = "Visa", cardholderName = "", cardNumber = "4242")))34        assertFalse(canSaveEditForm(EditForm(type = "card", title = "Visa", cardholderName = "June", cardNumber = "")))35        assertTrue(canSaveEditForm(EditForm(type = "card", title = "Visa", cardholderName = "June", cardNumber = "4242")))36    }3738    // ── New note routes to NotesMode, not the form ────────────────────────────3940    @Test fun new_note_routes_to_notes_mode() {41        assertTrue(shouldOpenNotesMode(isNew = true, type = "note"))42        // Existing note stays in the form; new login/card stay in the form.43        assertFalse(shouldOpenNotesMode(isNew = false, type = "note"))44        assertFalse(shouldOpenNotesMode(isNew = true, type = "login"))45        assertFalse(shouldOpenNotesMode(isNew = true, type = "card"))46    }4748    // ── Ghost watermark fades in as the title is typed ────────────────────────4950    @Test fun new_entry_ghost_watermark_appears_on_title_type() {51        assertEquals(0f, editGhostWatermarkTarget(""), 0f)52        assertEquals(0f, editGhostWatermarkTarget("   "), 0f)53        assertEquals(0.03f, editGhostWatermarkTarget("GitHub"), 0f)54    }5556    // ── Browser AddView optional login fields ────────────────────────────────5758    @Test fun new_login_hides_optional_totp_and_notes_until_requested() {59        assertFalse(shouldShowLoginTotp("login", revealed = false, totp = ""))60        assertFalse(shouldShowLoginNotes("login", revealed = false, notes = ""))61        assertTrue(shouldShowAddTotpLink("login", revealed = false, totp = ""))62        assertTrue(shouldShowAddNotesLink("login", revealed = false, notes = ""))63    }6465    @Test fun optional_login_fields_show_when_revealed_or_populated() {66        assertTrue(shouldShowLoginTotp("login", revealed = true, totp = ""))67        assertTrue(shouldShowLoginTotp("login", revealed = false, totp = "JBSWY3DPEHPK3PXP"))68        assertTrue(shouldShowLoginNotes("login", revealed = true, notes = ""))69        assertTrue(shouldShowLoginNotes("login", revealed = false, notes = "remember this"))70        assertFalse(shouldShowAddTotpLink("login", revealed = true, totp = ""))71        assertFalse(shouldShowAddNotesLink("login", revealed = false, notes = "remember this"))72    }7374    @Test fun identity_suggestions_split_username_and_email_like_browser() {75        val entries = listOf(76            identityEntry("a", username = "june", email = "[email protected]"),77            identityEntry("b", username = "june", email = null),78            identityEntry("c", username = "[email protected]", email = "[email protected]"),79            identityEntry("d", username = "[email protected]", email = null),80        )8182        assertEquals(listOf("june"), identitySuggestionsFor(entries, IdentityField.Username))83        assertEquals(84            listOf("[email protected]", "[email protected]", "[email protected]"),85            identitySuggestionsFor(entries, IdentityField.Email),86        )87    }8889    @Test fun generator_defaults_and_toggle_label_match_browser_add_view() {90        assertEquals(64, BROWSER_ADD_GENERATOR_LENGTH)91        assertEquals("generate", generatorToggleLabel(open = false))92        assertEquals("hide generator", generatorToggleLabel(open = true))93    }9495    @Test fun tag_suggestions_match_browser_add_view_limit_and_filtering() {96        val all = listOf("banking", "billing", "books", "business", "backup", "travel")97        assertEquals(98            listOf("banking", "billing", "books", "business", "backup"),99            tagSuggestionsFor(input = "b", allTags = all, tags = emptyList()),100        )101        assertEquals(102            listOf("billing", "books", "business", "backup"),103            tagSuggestionsFor(input = "b", allTags = all, tags = listOf("banking")),104        )105    }106107    @Test fun tag_normalization_allows_spaces_and_caps_length_like_browser() {108        assertEquals("shared login", normalizeTag(" Shared Login "))109        assertEquals("a".repeat(MAX_TAG_LEN), normalizeTag("A".repeat(MAX_TAG_LEN + 8)))110    }111112    // ── Card field formatting (CardFieldUtils, used live by setCardNumber etc.) ─113114    @Test fun new_card_number_auto_formats() {115        assertEquals("4242 4242 4242 4242", CardFieldUtils.formatPan("4242424242424242"))116        // Amex groups 4-6-5.117        assertEquals("3782 822463 10005", CardFieldUtils.formatPan("378282246310005"))118    }119120    @Test fun card_network_detection_matches_browser_add_view_badges() {121        assertEquals("visa", CardFieldUtils.detectNetwork("4242424242424242"))122        assertEquals("mastercard", CardFieldUtils.detectNetwork("5555555555554444"))123        assertEquals("mastercard", CardFieldUtils.detectNetwork("2221000000000009"))124        assertEquals("amex", CardFieldUtils.detectNetwork("378282246310005"))125        assertEquals("discover", CardFieldUtils.detectNetwork("6011111111111117"))126        assertEquals("discover", CardFieldUtils.detectNetwork("6500000000000002"))127        assertEquals("unknown", CardFieldUtils.detectNetwork("6000000000000000"))128    }129130    @Test fun new_card_expiry_auto_formats_browser_mm_year() {131        assertEquals("12", CardFieldUtils.formatExpiry("12"))132        assertEquals("12/28", CardFieldUtils.formatExpiry("1228"))133        assertEquals("12/2899", CardFieldUtils.formatExpiry("122899")) // caps year at 4 digits134        assertEquals("12", CardFieldUtils.expiryMonth("12/2028"))135        assertEquals("2028", CardFieldUtils.expiryYear("12/2028"))136        assertEquals("12/2028", CardFieldUtils.combineExpiry("12", "2028"))137    }138139    @Test fun new_card_expiry_rejects_invalid_month() {140        // currentYear2 = 26, currentMonth = 6 (2026-06).141        assertFalse(CardFieldUtils.isExpiryValid("13/28", currentYear2 = 26, currentMonth = 6))142        assertFalse(CardFieldUtils.isExpiryValid("00/28", currentYear2 = 26, currentMonth = 6))143        assertTrue(CardFieldUtils.isExpiryValid("12/28", currentYear2 = 26, currentMonth = 6))144        assertTrue(CardFieldUtils.isExpiryValid("12/2028", currentYear2 = 26, currentMonth = 6))145        // Past year rejected.146        assertFalse(CardFieldUtils.isExpiryValid("12/24", currentYear2 = 26, currentMonth = 6))147    }148149    private fun identityEntry(id: String, username: String?, email: String?) = VaultEntry(150        id = id,151        title = id,152        type = "login",153        username = username,154        email = email,155        createdAt = 0,156        modifiedAt = 0,157    )158}