composer_test.go

 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 (Subject), 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 (Body), got %d", composer.focusIndex)
33		}
34
35		// Simulate pressing Tab again to move to the 'Attachment' field.
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 (Attachment), got %d", composer.focusIndex)
40		}
41
42		// Simulate pressing Tab again to move to the 'Send' button.
43		model, _ = composer.Update(tea.KeyMsg{Type: tea.KeyTab})
44		composer = model.(*Composer)
45		if composer.focusIndex != 4 {
46			t.Errorf("After four Tabs, focusIndex should be 4 (Send), got %d", composer.focusIndex)
47		}
48
49		// Simulate one more Tab to wrap around to the 'To' field.
50		model, _ = composer.Update(tea.KeyMsg{Type: tea.KeyTab})
51		composer = model.(*Composer)
52		if composer.focusIndex != 0 {
53			t.Errorf("After five Tabs, focusIndex should wrap to 0, got %d", composer.focusIndex)
54		}
55	})
56
57	t.Run("Send email message", func(t *testing.T) {
58		// Set values for the email fields.
59		composer.toInput.SetValue("recipient@example.com")
60		composer.subjectInput.SetValue("Test Subject")
61		composer.bodyInput.SetValue("This is the body.")
62		// Set focus to the Send button.
63		composer.focusIndex = 4
64
65		// Simulate pressing Enter to send the email.
66		_, cmd := composer.Update(tea.KeyMsg{Type: tea.KeyEnter})
67		if cmd == nil {
68			t.Fatal("Expected a command to be returned, but got nil.")
69		}
70
71		// Execute the command and check the resulting message.
72		msg := cmd()
73		sendMsg, ok := msg.(SendEmailMsg)
74		if !ok {
75			t.Fatalf("Expected a SendEmailMsg, but got %T", msg)
76		}
77
78		// Verify the content of the message.
79		expectedMsg := SendEmailMsg{
80			To:             "recipient@example.com",
81			Subject:        "Test Subject",
82			Body:           "This is the body.",
83			AttachmentPath: "", // Expect empty attachment path in this test
84		}
85		if !reflect.DeepEqual(sendMsg, expectedMsg) {
86			t.Errorf("Mismatched SendEmailMsg.\nGot:  %+v\nWant: %+v", sendMsg, expectedMsg)
87		}
88	})
89}