package com.peachpasswords.autofill import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Test /** * AutofillNavRepository carries the one-shot "open the new-entry editor" request * from the autofill picker through MainActivity to PeachAppNavHost. The NavHost * consumes the request as part of its LaunchedEffect, so the repository must * surface the request exactly once before clearing — re-firing the LockScreen * post-unlock branch after the consume is what made the new-entry redirect * land on the vault list instead of the editor. */ class AutofillNavRepositoryTest { @Test fun request_makes_payload_visible_to_collectors() { val repo = AutofillNavRepository() assertNull(repo.newEntryRequest.value) repo.requestNewEntry(url = "https://github.com/login", username = "june", email = null) val req = repo.newEntryRequest.value assertEquals("https://github.com/login", req?.url) assertEquals("june", req?.username) assertNull(req?.email) } @Test fun blank_optional_fields_are_dropped_from_the_payload() { // Hosts that don't surface a typed username/email pass empty strings; // these must not pollute the new-entry editor's prefill. val repo = AutofillNavRepository() repo.requestNewEntry(url = "", username = "", email = "") val req = repo.newEntryRequest.value assertEquals(null, req?.url) assertEquals(null, req?.username) assertEquals(null, req?.email) } @Test fun consume_clears_the_request_so_lock_screen_does_not_re_fire_on_post_unlock() { // Regression: the v2.14.5 guard in LockScreen was keyed on `autofillPending`, // so when the NavHost's LaunchedEffect consumed the request the key // flipped and LockScreen re-fired `onUnlocked(...)`, dropping the user // on the vault list. The repository must surface exactly one non-null // value across the request/consume cycle, never two. val repo = AutofillNavRepository() repo.requestNewEntry(url = "https://example.com", username = null, email = null) assertEquals("https://example.com", repo.newEntryRequest.value?.url) repo.consumeNewEntryRequest() assertNull(repo.newEntryRequest.value) } }