master
  1package timer
  2
  3import (
  4	"testing"
  5	"time"
  6
  7	"charm.land/lipgloss/v2"
  8	tea "github.com/charmbracelet/bubbletea"
  9)
 10
 11func TestNewModel(t *testing.T) {
 12	m := NewModel()
 13
 14	if m.progress.FullColor != lipgloss.Color(titleColor) {
 15		t.Errorf("NewModel() progress color = %v, want %v", m.progress.FullColor, titleColor)
 16	}
 17
 18	if m.duration != 0 {
 19		t.Errorf("NewModel() duration = %v, want 0", m.duration)
 20	}
 21
 22	if !m.startTime.IsZero() {
 23		t.Errorf("NewModel() startTime should be zero")
 24	}
 25}
 26
 27func TestTimerValidation(t *testing.T) {
 28	tests := []struct {
 29		name    string
 30		args    []string
 31		wantErr bool
 32	}{
 33		{
 34			name:    "no args",
 35			args:    []string{},
 36			wantErr: true,
 37		},
 38		{
 39			name:    "invalid duration",
 40			args:    []string{"invalid"},
 41			wantErr: true,
 42		},
 43	}
 44
 45	for _, tt := range tests {
 46		t.Run(tt.name, func(t *testing.T) {
 47			err := Timer(tt.args)
 48			if (err != nil) != tt.wantErr {
 49				t.Errorf("Timer() error = %v, wantErr %v", err, tt.wantErr)
 50			}
 51		})
 52	}
 53}
 54
 55func TestModelInit(t *testing.T) {
 56	m := NewModel()
 57	cmd := m.Init()
 58
 59	if cmd == nil {
 60		t.Error("Init() should return a command")
 61	}
 62}
 63
 64func TestModelUpdate(t *testing.T) {
 65	m := NewModel()
 66	m.duration = 5 * time.Second
 67	m.startTime = time.Now()
 68
 69	t.Run("tick message", func(t *testing.T) {
 70		newModel, cmd := m.Update(tickMsg(time.Now()))
 71		if cmd == nil {
 72			t.Error("Update with tickMsg should return a command")
 73		}
 74		if newModel.(Model).quitting {
 75			t.Error("Model should not be quitting after tick")
 76		}
 77	})
 78
 79	t.Run("quit key", func(t *testing.T) {
 80		newModel, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'q'}})
 81		if !newModel.(Model).quitting {
 82			t.Error("Model should be quitting after 'q' key")
 83		}
 84	})
 85
 86	t.Run("ctrl+c", func(t *testing.T) {
 87		newModel, _ := m.Update(tea.KeyMsg{Type: tea.KeyCtrlC})
 88		if !newModel.(Model).quitting {
 89			t.Error("Model should be quitting after ctrl+c")
 90		}
 91	})
 92}
 93
 94func TestModelView(t *testing.T) {
 95	m := NewModel()
 96	m.duration = 5 * time.Second
 97	m.startTime = time.Now()
 98
 99	t.Run("normal view", func(t *testing.T) {
100		view := m.View()
101		if view == "" {
102			t.Error("View() should return non-empty string when not quitting")
103		}
104	})
105
106	t.Run("quitting view", func(t *testing.T) {
107		m.quitting = true
108		view := m.View()
109		if view != "" {
110			t.Error("View() should return empty string when quitting")
111		}
112	})
113}
114
115func TestTickCmd(t *testing.T) {
116	now := time.Now()
117	msg := tickCmd(now)
118
119	if _, ok := msg.(tickMsg); !ok {
120		t.Error("tickCmd() should return tickMsg type")
121	}
122}