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