Avoid blocking UI during vault open and unlock

This commit is contained in:
Joe Julian
2026-04-01 16:54:16 -07:00
parent 4f2bb3d158
commit 80737869ea
4 changed files with 513 additions and 49 deletions
+203 -3
View File
@@ -44,6 +44,17 @@ func TestMain(m *testing.M) {
os.Exit(code)
}
func waitForBackgroundResult(t *testing.T, u *ui) backgroundActionResult {
t.Helper()
select {
case result := <-u.backgroundResults:
return result
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for background action result")
return backgroundActionResult{}
}
}
func TestUIFiltersUsingVaultModelPathsAndSearch(t *testing.T) {
t.Parallel()
@@ -150,6 +161,42 @@ func TestUIClearingSearchResetsToCurrentSectionListing(t *testing.T) {
}
}
func TestUIRunBackgroundActionIgnoresDuplicateWhileLoading(t *testing.T) {
t.Parallel()
u := newUIWithSession("desktop", &session.Manager{})
started := make(chan struct{})
release := make(chan struct{})
runs := 0
u.runBackgroundAction("open vault", func() (func() error, error) {
runs++
close(started)
<-release
return func() error { return nil }, nil
})
<-started
u.runBackgroundAction("open vault", func() (func() error, error) {
runs++
return func() error { return nil }, nil
})
if runs != 1 {
t.Fatalf("background runs = %d, want 1", runs)
}
if got := u.loadingMessage; got != "Open vault..." {
t.Fatalf("loadingMessage = %q, want %q", got, "Open vault...")
}
close(release)
result := waitForBackgroundResult(t, u)
u.applyBackgroundResult(result)
if got := u.loadingMessage; got != "" {
t.Fatalf("loadingMessage after apply = %q, want empty", got)
}
}
func TestUIChildGroupsComeFromVaultModel(t *testing.T) {
t.Parallel()
@@ -867,6 +914,66 @@ func TestUIOpenRemoteAndSaveThroughConfiguredWebDAVTarget(t *testing.T) {
}
}
func TestUIStartOpenRemoteActionAppliesResultOnMainThread(t *testing.T) {
t.Parallel()
key := vault.MasterKey{Password: "correct horse battery staple"}
model := vault.Model{
Entries: []vault.Entry{{
ID: "vault-console",
Title: "Vault Console",
Username: "dannyocean",
Password: "token-1",
URL: "https://vault.crew.example.invalid",
Path: []string{"Root", "Internet"},
}},
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Fatalf("unexpected method %s", r.Method)
}
var encoded bytes.Buffer
if err := vault.SaveKDBXWithKey(&encoded, model, key); err != nil {
t.Fatalf("SaveKDBXWithKey() error = %v", err)
}
w.Header().Set("ETag", "\"v1\"")
_, _ = w.Write(encoded.Bytes())
}))
defer server.Close()
manager := &session.Manager{}
u := newUIWithSession("desktop", manager)
u.masterPassword.SetText(key.Password)
u.remoteBaseURL.SetText(server.URL)
u.remotePath.SetText("vaults/main.kdbx")
u.startOpenRemoteAction()
if got := u.loadingMessage; got != "Open remote vault..." {
t.Fatalf("loadingMessage after start = %q, want %q", got, "Open remote vault...")
}
if manager.HasVault() {
t.Fatal("manager.HasVault() = true before remote result applied, want false")
}
result := waitForBackgroundResult(t, u)
u.applyBackgroundResult(result)
if got := u.loadingMessage; got != "" {
t.Fatalf("loadingMessage after apply = %q, want empty", got)
}
if got := u.state.ErrorMessage; got != "" {
t.Fatalf("ErrorMessage after apply = %q, want empty", got)
}
if !manager.HasVault() {
t.Fatal("manager.HasVault() = false after remote result applied, want true")
}
if got := u.filteredTitles(); !slices.Equal(got, []string{"Vault Console"}) {
t.Fatalf("filteredTitles() = %v, want [Vault Console]", got)
}
}
func TestUIOpenRemoteReportsTransportFailure(t *testing.T) {
t.Parallel()
@@ -1016,6 +1123,88 @@ func TestUIAdvancedSynchronizeFromLocalMergesIntoCurrentVault(t *testing.T) {
}
}
func TestUIStartOpenVaultActionAppliesResultOnMainThread(t *testing.T) {
t.Parallel()
key := vault.MasterKey{Password: "correct horse battery staple"}
path := filepath.Join(t.TempDir(), "vault.kdbx")
writeKDBXMainTestFile(t, path, vault.Model{
Entries: []vault.Entry{{
ID: "entry-1",
Title: "Vault Console",
Username: "dannyocean",
Password: "token-current",
URL: "https://vault.crew.example.invalid",
Path: []string{"Root", "Internet"},
}},
}, key)
manager := &session.Manager{}
u := newUIWithSession("desktop", manager)
u.masterPassword.SetText(key.Password)
u.vaultPath.SetText(path)
u.startOpenVaultAction()
if got := u.loadingMessage; got != "Open vault..." {
t.Fatalf("loadingMessage after start = %q, want %q", got, "Open vault...")
}
if manager.HasVault() {
t.Fatal("manager.HasVault() = true before background result applied, want false")
}
result := waitForBackgroundResult(t, u)
u.applyBackgroundResult(result)
if got := u.loadingMessage; got != "" {
t.Fatalf("loadingMessage after apply = %q, want empty", got)
}
if got := u.state.ErrorMessage; got != "" {
t.Fatalf("ErrorMessage after apply = %q, want empty", got)
}
if !manager.HasVault() {
t.Fatal("manager.HasVault() = false after background result applied, want true")
}
if got := u.filteredTitles(); !slices.Equal(got, []string{"Vault Console"}) {
t.Fatalf("filteredTitles() = %v, want [Vault Console]", got)
}
}
func TestUIStartUnlockActionAppliesResultOnMainThread(t *testing.T) {
t.Parallel()
key := vault.MasterKey{Password: "correct horse battery staple"}
manager := &session.Manager{}
u := newUIWithSession("desktop", manager)
u.masterPassword.SetText(key.Password)
if err := u.createVaultAction(); err != nil {
t.Fatalf("createVaultAction() error = %v", err)
}
if err := u.lockAction(); err != nil {
t.Fatalf("lockAction() error = %v", err)
}
u.masterPassword.SetText(key.Password)
u.startUnlockAction()
if got := u.loadingMessage; got != "Unlock vault..." {
t.Fatalf("loadingMessage after start = %q, want %q", got, "Unlock vault...")
}
if !manager.IsLocked() {
t.Fatal("manager.IsLocked() = false before background result applied, want true")
}
result := waitForBackgroundResult(t, u)
u.applyBackgroundResult(result)
if got := u.loadingMessage; got != "" {
t.Fatalf("loadingMessage after apply = %q, want empty", got)
}
if manager.IsLocked() {
t.Fatal("manager.IsLocked() = true after background result applied, want false")
}
}
func TestUIAdvancedSynchronizeToRemoteWritesMergedVaultToTarget(t *testing.T) {
t.Parallel()
@@ -3022,12 +3211,23 @@ func TestEnterOnLockedScreenDefaultsToUnlockVault(t *testing.T) {
if !handled {
t.Fatal("handleKeyPress(Return) = false, want true while locked")
}
if u.isVaultLocked() {
t.Fatal("isVaultLocked() = true, want false after unlock")
}
if got := u.masterPassword.Text(); got != "" {
t.Fatalf("masterPassword after unlock = %q, want empty", got)
}
if !u.isVaultLocked() {
t.Fatal("isVaultLocked() = false before background apply, want still locked")
}
result := waitForBackgroundResult(t, u)
if err := result.err; err != nil {
t.Fatalf("background unlock prepare error = %v", err)
}
u.applyBackgroundResult(result)
if got := u.state.ErrorMessage; got != "" {
t.Fatalf("state.ErrorMessage after unlock apply = %q, want empty", got)
}
if u.isVaultLocked() {
t.Fatal("isVaultLocked() = true, want false after unlock apply")
}
}
func TestUILockedVaultUsesSingleUnlockPaneAndOmitsSearchFocus(t *testing.T) {