package com.peachpasswords.ui.vault
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class EntryEditScreenTest {
// ── Top-bar label (cancel |
| save) ───────────────────────────────
@Test fun title_bar_label_is_type_specific_for_new_entries() {
assertEquals("new entry", editTitleBarLabel(isNew = true, type = "login"))
assertEquals("new note", editTitleBarLabel(isNew = true, type = "note"))
assertEquals("new card", editTitleBarLabel(isNew = true, type = "card"))
}
@Test fun title_bar_label_is_edit_for_existing_entries() {
assertEquals("edit", editTitleBarLabel(isNew = false, type = "login"))
assertEquals("edit", editTitleBarLabel(isNew = false, type = "note"))
}
@Test fun primary_action_label_matches_browser_add_view() {
assertEquals("create", entryEditPrimaryActionLabel(isNew = true, saving = false))
assertEquals("save", entryEditPrimaryActionLabel(isNew = false, saving = false))
assertEquals("saving...", entryEditPrimaryActionLabel(isNew = true, saving = true))
}
@Test fun save_is_disabled_until_browser_minimum_fields_are_present() {
assertFalse(canSaveEditForm(EditForm(type = "login", title = "")))
assertTrue(canSaveEditForm(EditForm(type = "login", title = "GitHub")))
assertFalse(canSaveEditForm(EditForm(type = "card", title = "Visa", cardholderName = "", cardNumber = "4242")))
assertFalse(canSaveEditForm(EditForm(type = "card", title = "Visa", cardholderName = "June", cardNumber = "")))
assertTrue(canSaveEditForm(EditForm(type = "card", title = "Visa", cardholderName = "June", cardNumber = "4242")))
}
// ── New note routes to NotesMode, not the form ────────────────────────────
@Test fun new_note_routes_to_notes_mode() {
assertTrue(shouldOpenNotesMode(isNew = true, type = "note"))
// Existing note stays in the form; new login/card stay in the form.
assertFalse(shouldOpenNotesMode(isNew = false, type = "note"))
assertFalse(shouldOpenNotesMode(isNew = true, type = "login"))
assertFalse(shouldOpenNotesMode(isNew = true, type = "card"))
}
// ── Ghost watermark fades in as the title is typed ────────────────────────
@Test fun new_entry_ghost_watermark_appears_on_title_type() {
assertEquals(0f, editGhostWatermarkTarget(""), 0f)
assertEquals(0f, editGhostWatermarkTarget(" "), 0f)
assertEquals(0.03f, editGhostWatermarkTarget("GitHub"), 0f)
}
// ── Browser AddView optional login fields ────────────────────────────────
@Test fun new_login_hides_optional_totp_and_notes_until_requested() {
assertFalse(shouldShowLoginTotp("login", revealed = false, totp = ""))
assertFalse(shouldShowLoginNotes("login", revealed = false, notes = ""))
assertTrue(shouldShowAddTotpLink("login", revealed = false, totp = ""))
assertTrue(shouldShowAddNotesLink("login", revealed = false, notes = ""))
}
@Test fun optional_login_fields_show_when_revealed_or_populated() {
assertTrue(shouldShowLoginTotp("login", revealed = true, totp = ""))
assertTrue(shouldShowLoginTotp("login", revealed = false, totp = "JBSWY3DPEHPK3PXP"))
assertTrue(shouldShowLoginNotes("login", revealed = true, notes = ""))
assertTrue(shouldShowLoginNotes("login", revealed = false, notes = "remember this"))
assertFalse(shouldShowAddTotpLink("login", revealed = true, totp = ""))
assertFalse(shouldShowAddNotesLink("login", revealed = false, notes = "remember this"))
}
// ── Card field formatting (CardFieldUtils, used live by setCardNumber etc.) ─
@Test fun new_card_number_auto_formats() {
assertEquals("4242 4242 4242 4242", CardFieldUtils.formatPan("4242424242424242"))
// Amex groups 4-6-5.
assertEquals("3782 822463 10005", CardFieldUtils.formatPan("378282246310005"))
}
@Test fun new_card_expiry_auto_formats_mm_yy() {
assertEquals("12", CardFieldUtils.formatExpiry("12"))
assertEquals("12/28", CardFieldUtils.formatExpiry("1228"))
assertEquals("12/28", CardFieldUtils.formatExpiry("122899")) // caps at 4 digits
}
@Test fun new_card_expiry_rejects_invalid_month() {
// currentYear2 = 26, currentMonth = 6 (2026-06).
assertFalse(CardFieldUtils.isExpiryValid("13/28", currentYear2 = 26, currentMonth = 6))
assertFalse(CardFieldUtils.isExpiryValid("00/28", currentYear2 = 26, currentMonth = 6))
assertTrue(CardFieldUtils.isExpiryValid("12/28", currentYear2 = 26, currentMonth = 6))
// Past year rejected.
assertFalse(CardFieldUtils.isExpiryValid("12/24", currentYear2 = 26, currentMonth = 6))
}
}