forked from floatpane/matcha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposer_test.go
More file actions
323 lines (273 loc) · 12.1 KB
/
Copy pathcomposer_test.go
File metadata and controls
323 lines (273 loc) · 12.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package tui
import (
"testing"
tea "charm.land/bubbletea/v2"
"github.com/floatpane/matcha/config"
)
// TestComposerUpdate verifies the state transitions in the email composer.
func TestComposerUpdate(t *testing.T) {
// Initialize a new composer with accounts.
accounts := []config.Account{
{ID: "account-1", Email: "test@example.com", Name: "Test User"},
}
composer := NewComposerWithAccounts(accounts, "account-1", "", "", "", false)
t.Run("Focus cycling", func(t *testing.T) {
// Initial focus is on the 'To' input (index 1, since From is 0).
// But NewComposer starts focus at focusTo which is 1.
if composer.focusIndex != focusTo {
t.Errorf("Initial focusIndex should be %d (focusTo), got %d", focusTo, composer.focusIndex)
}
// Simulate pressing Tab to move to the 'Cc' field.
model, _ := composer.Update(tea.KeyPressMsg{Code: tea.KeyTab})
composer = model.(*Composer)
if composer.focusIndex != focusCc {
t.Errorf("After one Tab, focusIndex should be %d (focusCc), got %d", focusCc, composer.focusIndex)
}
// Simulate pressing Tab to move to the 'Bcc' field.
model, _ = composer.Update(tea.KeyPressMsg{Code: tea.KeyTab})
composer = model.(*Composer)
if composer.focusIndex != focusBcc {
t.Errorf("After two Tabs, focusIndex should be %d (focusBcc), got %d", focusBcc, composer.focusIndex)
}
// Simulate pressing Tab to move to the 'Subject' field.
model, _ = composer.Update(tea.KeyPressMsg{Code: tea.KeyTab})
composer = model.(*Composer)
if composer.focusIndex != focusSubject {
t.Errorf("After three Tabs, focusIndex should be %d (focusSubject), got %d", focusSubject, composer.focusIndex)
}
// Simulate pressing Tab again to move to the 'Body' field.
model, _ = composer.Update(tea.KeyPressMsg{Code: tea.KeyTab})
composer = model.(*Composer)
if composer.focusIndex != focusBody {
t.Errorf("After four Tabs, focusIndex should be %d (focusBody), got %d", focusBody, composer.focusIndex)
}
// Simulate pressing Tab again to move to the 'Signature' field.
model, _ = composer.Update(tea.KeyPressMsg{Code: tea.KeyTab})
composer = model.(*Composer)
if composer.focusIndex != focusSignature {
t.Errorf("After five Tabs, focusIndex should be %d (focusSignature), got %d", focusSignature, composer.focusIndex)
}
// Simulate pressing Tab again to move to the 'Attachment' field.
model, _ = composer.Update(tea.KeyPressMsg{Code: tea.KeyTab})
composer = model.(*Composer)
if composer.focusIndex != focusAttachment {
t.Errorf("After six Tabs, focusIndex should be %d (focusAttachment), got %d", focusAttachment, composer.focusIndex)
}
// Simulate pressing Tab again to move to the 'EncryptSMIME' toggle.
model, _ = composer.Update(tea.KeyPressMsg{Code: tea.KeyTab})
composer = model.(*Composer)
if composer.focusIndex != focusEncryptSMIME {
t.Errorf("After seven Tabs, focusIndex should be %d (focusEncryptSMIME), got %d", focusEncryptSMIME, composer.focusIndex)
}
// Simulate pressing Tab again to move to the 'Send' button.
model, _ = composer.Update(tea.KeyPressMsg{Code: tea.KeyTab})
composer = model.(*Composer)
if composer.focusIndex != focusSend {
t.Errorf("After eight Tabs, focusIndex should be %d (focusSend), got %d", focusSend, composer.focusIndex)
}
// Simulate one more Tab to wrap around.
// With single account, From field is skipped, so it wraps to focusTo.
model, _ = composer.Update(tea.KeyPressMsg{Code: tea.KeyTab})
composer = model.(*Composer)
if composer.focusIndex != focusTo {
t.Errorf("After nine Tabs, focusIndex should wrap to %d (focusTo) since single account skips From, got %d", focusTo, composer.focusIndex)
}
})
t.Run("Send email message", func(t *testing.T) {
// Re-initialize composer for this test
composer = NewComposerWithAccounts(accounts, "account-1", "", "", "", false)
// Set values for the email fields.
composer.toInput.SetValue("recipient@example.com")
composer.subjectInput.SetValue("Test Subject")
composer.bodyInput.SetValue("This is the body.")
// Set focus to the Send button.
composer.focusIndex = focusSend
// Simulate pressing Enter to send the email.
_, cmd := composer.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
if cmd == nil {
t.Fatal("Expected a command to be returned, but got nil.")
}
// Execute the command and check the resulting message.
msg := cmd()
sendMsg, ok := msg.(SendEmailMsg)
if !ok {
t.Fatalf("Expected a SendEmailMsg, but got %T", msg)
}
// Verify the content of the message.
if sendMsg.To != "recipient@example.com" {
t.Errorf("Expected To 'recipient@example.com', got %q", sendMsg.To)
}
if sendMsg.Subject != "Test Subject" {
t.Errorf("Expected Subject 'Test Subject', got %q", sendMsg.Subject)
}
if sendMsg.Body != "This is the body." {
t.Errorf("Expected Body 'This is the body.', got %q", sendMsg.Body)
}
if sendMsg.AccountID != "account-1" {
t.Errorf("Expected AccountID 'account-1', got %q", sendMsg.AccountID)
}
})
t.Run("Account picker with multiple accounts", func(t *testing.T) {
multiAccounts := []config.Account{
{ID: "account-1", Email: "test1@example.com", Name: "User 1"},
{ID: "account-2", Email: "test2@example.com", Name: "User 2"},
}
multiComposer := NewComposerWithAccounts(multiAccounts, "account-1", "", "", "", false)
// Move focus to From field
multiComposer.focusIndex = focusFrom
// Press Enter to open account picker
model, _ := multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
multiComposer = model.(*Composer)
if !multiComposer.showAccountPicker {
t.Error("Expected account picker to be shown")
}
// Navigate down to select second account
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyDown})
multiComposer = model.(*Composer)
if multiComposer.selectedAccountIdx != 1 {
t.Errorf("Expected selectedAccountIdx to be 1, got %d", multiComposer.selectedAccountIdx)
}
// Press Enter to confirm selection
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
multiComposer = model.(*Composer)
if multiComposer.showAccountPicker {
t.Error("Expected account picker to be closed")
}
// Verify the selected account
if multiComposer.GetSelectedAccountID() != "account-2" {
t.Errorf("Expected selected account ID 'account-2', got %q", multiComposer.GetSelectedAccountID())
}
})
t.Run("Single account no picker", func(t *testing.T) {
singleAccounts := []config.Account{
{ID: "account-1", Email: "test@example.com"},
}
singleComposer := NewComposerWithAccounts(singleAccounts, "account-1", "", "", "", false)
// Move focus to From field
singleComposer.focusIndex = focusFrom
// Press Enter - should not open picker with single account
model, _ := singleComposer.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
singleComposer = model.(*Composer)
if singleComposer.showAccountPicker {
t.Error("Account picker should not open with single account")
}
})
t.Run("Multi-account focus cycling includes From", func(t *testing.T) {
multiAccounts := []config.Account{
{ID: "account-1", Email: "test1@example.com"},
{ID: "account-2", Email: "test2@example.com"},
}
multiComposer := NewComposerWithAccounts(multiAccounts, "account-1", "", "", "", false)
// Initial focus is on 'To' field
if multiComposer.focusIndex != focusTo {
t.Errorf("Initial focusIndex should be %d (focusTo), got %d", focusTo, multiComposer.focusIndex)
}
// Tab through all fields: To -> Cc -> Bcc -> Subject -> Body -> Signature -> Attachment -> EncryptSMIME -> Send -> From (wrap)
model, _ := multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // To -> Cc
multiComposer = model.(*Composer)
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // Cc -> Bcc
multiComposer = model.(*Composer)
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // Bcc -> Subject
multiComposer = model.(*Composer)
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // Subject -> Body
multiComposer = model.(*Composer)
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // Body -> Signature
multiComposer = model.(*Composer)
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // Signature -> Attachment
multiComposer = model.(*Composer)
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // Attachment -> EncryptSMIME
multiComposer = model.(*Composer)
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // EncryptSMIME -> Send
multiComposer = model.(*Composer)
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // Send -> From (wrap)
multiComposer = model.(*Composer)
model, _ = multiComposer.Update(tea.KeyPressMsg{Code: tea.KeyTab}) // From -> To (wrap)
multiComposer = model.(*Composer)
// With multiple accounts, From field should be included in tab order
if multiComposer.focusIndex != focusTo {
t.Errorf("After nine Tabs with multi-account, focusIndex should wrap to %d (focusTo), got %d", focusTo, multiComposer.focusIndex)
}
})
}
// TestComposerGetFromAddress verifies the from address formatting.
func TestComposerGetFromAddress(t *testing.T) {
t.Run("With name", func(t *testing.T) {
accounts := []config.Account{
{ID: "account-1", FetchEmail: "test@example.com", Name: "Test User"},
}
composer := NewComposerWithAccounts(accounts, "account-1", "", "", "", false)
fromAddr := composer.getFromAddress()
expected := "Test User <test@example.com>"
if fromAddr != expected {
t.Errorf("Expected from address %q, got %q", expected, fromAddr)
}
})
t.Run("Without name", func(t *testing.T) {
accounts := []config.Account{
{ID: "account-1", FetchEmail: "test@example.com"},
}
composer := NewComposerWithAccounts(accounts, "account-1", "", "", "", false)
fromAddr := composer.getFromAddress()
expected := "test@example.com"
if fromAddr != expected {
t.Errorf("Expected from address %q, got %q", expected, fromAddr)
}
})
t.Run("No accounts", func(t *testing.T) {
composer := NewComposer("", "", "", "", false)
fromAddr := composer.getFromAddress()
if fromAddr != "" {
t.Errorf("Expected empty from address, got %q", fromAddr)
}
})
}
// TestComposerSetSelectedAccount verifies account selection.
func TestComposerSetSelectedAccount(t *testing.T) {
accounts := []config.Account{
{ID: "account-1", FetchEmail: "test1@example.com"},
{ID: "account-2", FetchEmail: "test2@example.com"},
{ID: "account-3", FetchEmail: "test3@example.com"},
}
composer := NewComposerWithAccounts(accounts, "account-1", "", "", "", false)
composer.SetSelectedAccount("account-3")
if composer.selectedAccountIdx != 2 {
t.Errorf("Expected selectedAccountIdx 2, got %d", composer.selectedAccountIdx)
}
if composer.GetSelectedAccountID() != "account-3" {
t.Errorf("Expected selected account ID 'account-3', got %q", composer.GetSelectedAccountID())
}
// Test non-existent account (should not change)
composer.SetSelectedAccount("non-existent")
if composer.selectedAccountIdx != 2 {
t.Errorf("Expected selectedAccountIdx to remain 2, got %d", composer.selectedAccountIdx)
}
}
// TestComposerDynamicHeight verifies that window resize updates textarea heights.
func TestComposerDynamicHeight(t *testing.T) {
composer := NewComposer("", "", "", "", false)
model, _ := composer.Update(tea.WindowSizeMsg{Width: 120, Height: 40})
composer = model.(*Composer)
if composer.height != 40 {
t.Errorf("Expected height 40, got %d", composer.height)
}
bodyH := composer.bodyInput.Height()
sigH := composer.signatureInput.Height()
if bodyH <= 3 {
t.Errorf("Expected bodyInput height > 3, got %d", bodyH)
}
if sigH <= 1 {
t.Errorf("Expected signatureInput height > 1, got %d", sigH)
}
if bodyH <= sigH {
t.Errorf("Expected bodyInput height (%d) > signatureInput height (%d)", bodyH, sigH)
}
// Small window: heights should not go below minimums
model, _ = composer.Update(tea.WindowSizeMsg{Width: 80, Height: 10})
composer = model.(*Composer)
if composer.bodyInput.Height() < 3 {
t.Errorf("bodyInput height should be at least 3, got %d", composer.bodyInput.Height())
}
if composer.signatureInput.Height() < 2 {
t.Errorf("signatureInput height should be at least 2, got %d", composer.signatureInput.Height())
}
}