1package tui
2
3import (
4 "reflect"
5 "testing"
6
7 tea "github.com/charmbracelet/bubbletea"
8)
9
10// TestComposerUpdate verifies the state transitions in the email composer.
11func TestComposerUpdate(t *testing.T) {
12 // Initialize a new composer.
13 composer := NewComposer("test@example.com")
14
15 t.Run("Focus cycling", func(t *testing.T) {
16 // Initial focus is on the 'To' input (index 0).
17 if composer.focusIndex != 0 {
18 t.Errorf("Initial focusIndex should be 0, got %d", composer.focusIndex)
19 }
20
21 // Simulate pressing Tab to move to the 'Subject' field.
22 model, _ := composer.Update(tea.KeyMsg{Type: tea.KeyTab})
23 composer = model.(Composer)
24 if composer.focusIndex != 1 {
25 t.Errorf("After one Tab, focusIndex should be 1, got %d", composer.focusIndex)
26 }
27
28 // Simulate pressing Tab again to move to the 'Body' field.
29 model, _ = composer.Update(tea.KeyMsg{Type: tea.KeyTab})
30 composer = model.(Composer)
31 if composer.focusIndex != 2 {
32 t.Errorf("After two Tabs, focusIndex should be 2, got %d", composer.focusIndex)
33 }
34
35 // Simulate pressing Tab again to move to the 'Send' button.
36 model, _ = composer.Update(tea.KeyMsg{Type: tea.KeyTab})
37 composer = model.(Composer)
38 if composer.focusIndex != 3 {
39 t.Errorf("After three Tabs, focusIndex should be 3 (Send), got %d", composer.focusIndex)
40 }
41
42 // Simulate one more Tab to wrap around to the 'To' field.
43 model, _ = composer.Update(tea.KeyMsg{Type: tea.KeyTab})
44 composer = model.(Composer)
45 if composer.focusIndex != 0 {
46 t.Errorf("After four Tabs, focusIndex should wrap to 0, got %d", composer.focusIndex)
47 }
48 })
49
50 t.Run("Send email message", func(t *testing.T) {
51 // Set values for the email fields.
52 composer.toInput.SetValue("recipient@example.com")
53 composer.subjectInput.SetValue("Test Subject")
54 composer.bodyInput.SetValue("This is the body.")
55 // Set focus to the Send button.
56 composer.focusIndex = 3
57
58 // Simulate pressing Enter to send the email.
59 _, cmd := composer.Update(tea.KeyMsg{Type: tea.KeyEnter})
60 if cmd == nil {
61 t.Fatal("Expected a command to be returned, but got nil.")
62 }
63
64 // Execute the command and check the resulting message.
65 msg := cmd()
66 sendMsg, ok := msg.(SendEmailMsg)
67 if !ok {
68 t.Fatalf("Expected a SendEmailMsg, but got %T", msg)
69 }
70
71 // Verify the content of the message.
72 expectedMsg := SendEmailMsg{
73 To: "recipient@example.com",
74 Subject: "Test Subject",
75 Body: "This is the body.",
76 }
77 if !reflect.DeepEqual(sendMsg, expectedMsg) {
78 t.Errorf("Mismatched SendEmailMsg.\nGot: %+v\nWant: %+v", sendMsg, expectedMsg)
79 }
80 })
81}