-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_test.go
More file actions
174 lines (142 loc) · 4.4 KB
/
Copy pathupdate_test.go
File metadata and controls
174 lines (142 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"testing"
tea "github.com/charmbracelet/bubbletea"
)
func TestPendingActionAdd(t *testing.T) {
m := initialModel("")
m.bwUnlocked = false
m.bwChecking = false
m.bwInstalled = true
m.bwStatus = "locked"
// Simulate pressing "a" while locked
result, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'a'}})
m = result.(model)
if m.pendingAction != "add" {
t.Errorf("expected pendingAction 'add', got %q", m.pendingAction)
}
if m.mode != bwPasswordView {
t.Errorf("expected bwPasswordView, got %v", m.mode)
}
}
func TestPendingActionResumedAfterItemsFetched(t *testing.T) {
m := initialModel("")
m.bwUnlocked = true
m.pendingAction = "add"
// Simulate bwItemsFetchedMsg arriving after unlock
result, _ := m.Update(bwItemsFetchedMsg{
items: []BWItem{{ID: "item-1", Name: "Test Item"}},
})
m = result.(model)
if m.mode != bwSelectView {
t.Errorf("expected bwSelectView after add action, got %v", m.mode)
}
if m.pendingAction != "" {
t.Errorf("expected pendingAction cleared, got %q", m.pendingAction)
}
if m.editingIndex != -1 {
t.Errorf("expected editingIndex -1, got %d", m.editingIndex)
}
}
func TestPendingActionClearedWhenNone(t *testing.T) {
m := initialModel("")
m.bwUnlocked = true
m.mode = bwPasswordView
m.pendingAction = ""
result, _ := m.Update(bwItemsFetchedMsg{
items: []BWItem{{ID: "item-1", Name: "Test Item"}},
})
m = result.(model)
if m.mode != listView {
t.Errorf("expected listView when no pending action, got %v", m.mode)
}
}
func TestPendingActionTokenFlow(t *testing.T) {
m := initialModel("")
m.bwUnlocked = false
m.bwChecking = false
m.bwInstalled = true
m.bwStatus = "locked"
// Add a client to the list first
m.clients = []Client{{Name: "test", BitwardenItemID: "bw-1", Issuer: "https://auth.example.com"}}
m.updateList()
// Simulate pressing Enter while locked
result, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'\r'}})
m = result.(model)
// Enter key on list selects and tries to get token
// But we need the list to have a selected item, which depends on bubbles internals
// Just verify the basic flow works without error
}
func TestPendingActionEditFlow(t *testing.T) {
m := initialModel("")
m.bwUnlocked = false
m.bwChecking = false
m.bwInstalled = true
m.bwStatus = "locked"
// Simulate pressing "e" while locked
result, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'e'}})
m = result.(model)
if m.pendingAction != "edit" {
t.Errorf("expected pendingAction 'edit', got %q", m.pendingAction)
}
}
func TestStartupForcesLoginWhenUnauthenticated(t *testing.T) {
m := initialModel("")
m.bwChecking = true
m.mode = listView
// Simulate bwStatusMsg arriving with "unauthenticated"
result, _ := m.Update(bwStatusMsg{installed: true, status: "unauthenticated"})
m = result.(model)
if m.mode != bwLoginView {
t.Errorf("expected bwLoginView on unauthenticated, got %v", m.mode)
}
}
func TestStartupForcesLoginWhenBWNotInstalled(t *testing.T) {
m := initialModel("")
m.bwChecking = true
m.mode = listView
// Simulate bwStatusMsg arriving with installed=false
result, _ := m.Update(bwStatusMsg{installed: false, status: "unauthenticated"})
m = result.(model)
if m.mode != bwLoginView {
t.Errorf("expected bwLoginView when bw not installed, got %v", m.mode)
}
}
func TestUnlockKeepsLoadingUntilItemsFetched(t *testing.T) {
m := initialModel("")
m.bwUnlocking = true
m.mode = bwPasswordView
// Simulate successful unlock
result, _ := m.Update(bwUnlockResultMsg{session: "test-session"})
m = result.(model)
// Should still show loading state while items are being fetched
if !m.bwUnlocking {
t.Error("expected bwUnlocking to remain true while fetching items")
}
if m.mode != bwPasswordView {
t.Errorf("expected bwPasswordView while loading, got %v", m.mode)
}
// Now items arrive
result, _ = m.Update(bwItemsFetchedMsg{
items: []BWItem{{ID: "item-1", Name: "Test"}},
})
m = result.(model)
if m.bwUnlocking {
t.Error("expected bwUnlocking to be false after items fetched")
}
}
func TestBWErrorResetsUnlockState(t *testing.T) {
m := initialModel("")
m.bwUnlocked = true
m.mode = tokenView
m.tokenLoading = true
// Simulate a bitwarden error in token response
result, _ := m.Update(tokenResponseMsg{
err: fmt.Errorf("bitwarden: vault is locked"),
})
m = result.(model)
if m.bwUnlocked {
t.Error("expected bwUnlocked to be reset after bitwarden error")
}
}