EntryEditScreenTest.kt

7.9 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 Entry creates logins/cards; Notes mode creates notes ─────────────3940    @Test fun entry_type_picker_excludes_notes_for_new_and_existing_entries() {41        assertEquals(listOf("login", "card"), entryEditTypeSegments().map { it.key })42    }4344    // ── Ghost watermark fades in as the title is typed ────────────────────────4546    @Test fun new_entry_ghost_watermark_appears_on_title_type() {47        assertEquals(0f, editGhostWatermarkTarget(""), 0f)48        assertEquals(0f, editGhostWatermarkTarget("   "), 0f)49        assertEquals(0.03f, editGhostWatermarkTarget("GitHub"), 0f)50    }5152    // ── Browser AddView optional login fields ────────────────────────────────5354    @Test fun new_login_hides_optional_totp_and_notes_until_requested() {55        assertFalse(shouldShowLoginTotp("login", revealed = false, totp = ""))56        assertFalse(shouldShowLoginNotes("login", revealed = false, notes = ""))57        assertTrue(shouldShowAddTotpLink("login", revealed = false, totp = ""))58        assertTrue(shouldShowAddNotesLink("login", revealed = false, notes = ""))59        assertFalse(shouldShowLoginNotesLink(isNew = true, type = "login", revealed = false, notes = ""))60        assertTrue(shouldShowLoginNotesLink(isNew = false, type = "login", revealed = false, notes = ""))61    }6263    @Test fun optional_login_fields_show_when_revealed_or_populated() {64        assertTrue(shouldShowLoginTotp("login", revealed = true, totp = ""))65        assertTrue(shouldShowLoginTotp("login", revealed = false, totp = "JBSWY3DPEHPK3PXP"))66        assertTrue(shouldShowLoginNotes("login", revealed = true, notes = ""))67        assertTrue(shouldShowLoginNotes("login", revealed = false, notes = "remember this"))68        assertFalse(shouldShowAddTotpLink("login", revealed = true, totp = ""))69        assertFalse(shouldShowAddNotesLink("login", revealed = false, notes = "remember this"))70    }7172    @Test fun identity_suggestions_split_username_and_email_like_browser() {73        val entries = listOf(74            identityEntry("a", username = "june", email = "[email protected]"),75            identityEntry("b", username = "june", email = null),76            identityEntry("c", username = "[email protected]", email = "[email protected]"),77            identityEntry("d", username = "[email protected]", email = null),78        )7980        assertEquals(listOf("june"), identitySuggestionsFor(entries, IdentityField.Username))81        assertEquals(82            listOf("[email protected]", "[email protected]", "[email protected]"),83            identitySuggestionsFor(entries, IdentityField.Email),84        )85    }8687    @Test fun generator_defaults_and_toggle_label_match_browser_add_view() {88        assertEquals(64, BROWSER_ADD_GENERATOR_LENGTH)89        assertEquals("generate", generatorToggleLabel(open = false))90        assertEquals("hide generator", generatorToggleLabel(open = true))91    }9293    @Test fun tag_suggestions_match_browser_add_view_limit_and_filtering() {94        val all = listOf("banking", "billing", "books", "business", "backup", "travel")95        assertEquals(96            listOf("banking", "billing", "books", "business", "backup"),97            tagSuggestionsFor(input = "b", allTags = all, tags = emptyList()),98        )99        assertEquals(100            listOf("billing", "books", "business", "backup"),101            tagSuggestionsFor(input = "b", allTags = all, tags = listOf("banking")),102        )103    }104105    @Test fun tag_normalization_allows_spaces_and_caps_length_like_browser() {106        assertEquals("shared login", normalizeTag(" Shared Login "))107        assertEquals("a".repeat(MAX_TAG_LEN), normalizeTag("A".repeat(MAX_TAG_LEN + 8)))108    }109110    // ── Card field formatting (CardFieldUtils, used live by setCardNumber etc.) ─111112    @Test fun new_card_number_auto_formats() {113        assertEquals("4242 4242 4242 4242", CardFieldUtils.formatPan("4242424242424242"))114        // Amex groups 4-6-5.115        assertEquals("3782 822463 10005", CardFieldUtils.formatPan("378282246310005"))116    }117118    @Test fun card_network_detection_matches_browser_add_view_badges() {119        assertEquals("visa", CardFieldUtils.detectNetwork("4242424242424242"))120        assertEquals("mastercard", CardFieldUtils.detectNetwork("5555555555554444"))121        assertEquals("mastercard", CardFieldUtils.detectNetwork("2221000000000009"))122        assertEquals("amex", CardFieldUtils.detectNetwork("378282246310005"))123        assertEquals("discover", CardFieldUtils.detectNetwork("6011111111111117"))124        assertEquals("discover", CardFieldUtils.detectNetwork("6500000000000002"))125        assertEquals("unknown", CardFieldUtils.detectNetwork("6000000000000000"))126    }127128    @Test fun new_card_expiry_auto_formats_browser_mm_year() {129        assertEquals("12", CardFieldUtils.formatExpiry("12"))130        assertEquals("12/28", CardFieldUtils.formatExpiry("1228"))131        assertEquals("12/2899", CardFieldUtils.formatExpiry("122899")) // caps year at 4 digits132        assertEquals("12", CardFieldUtils.expiryMonth("12/2028"))133        assertEquals("2028", CardFieldUtils.expiryYear("12/2028"))134        assertEquals("12/2028", CardFieldUtils.combineExpiry("12", "2028"))135    }136137    @Test fun new_card_expiry_rejects_invalid_month() {138        // currentYear2 = 26, currentMonth = 6 (2026-06).139        assertFalse(CardFieldUtils.isExpiryValid("13/28", currentYear2 = 26, currentMonth = 6))140        assertFalse(CardFieldUtils.isExpiryValid("00/28", currentYear2 = 26, currentMonth = 6))141        assertTrue(CardFieldUtils.isExpiryValid("12/28", currentYear2 = 26, currentMonth = 6))142        assertTrue(CardFieldUtils.isExpiryValid("12/2028", currentYear2 = 26, currentMonth = 6))143        // Past year rejected.144        assertFalse(CardFieldUtils.isExpiryValid("12/24", currentYear2 = 26, currentMonth = 6))145    }146147    private fun identityEntry(id: String, username: String?, email: String?) = VaultEntry(148        id = id,149        title = id,150        type = "login",151        username = username,152        email = email,153        createdAt = 0,154        modifiedAt = 0,155    )156}