package main import ( "testing" "time" tea "github.com/charmbracelet/bubbletea" ) type compareFunc[T comparable] func(T, T) bool func assertFunc[T comparable](t *testing.T, compare compareFunc[T], actual, expected T, msg string) { t.Helper() if !compare(actual, expected) { t.Errorf("%s: expected %v, got %v", msg, expected, actual) } } // assertEqual checks if two values are equal and reports an error if they are not. func assertEqual(t *testing.T, actual, expected interface{}, msg string) { t.Helper() if actual != expected { t.Errorf("%s: expected %v, got %v", msg, expected, actual) } } // assertNotEqual checks if two values are not equal and reports an error if they are. func assertNotEqual(t *testing.T, actual, expected interface{}, msg string) { t.Helper() if actual == expected { t.Errorf("%s: expected %v to be different from %v", msg, actual, expected) } } // TestParseDuration tests the parseDuration function func TestParseDuration(t *testing.T) { tests := []struct { input string expected time.Duration hasError bool }{ {"10", 10 * time.Minute, false}, {"1h", 1 * time.Hour, false}, {"invalid", 0, true}, } for _, test := range tests { result, err := parseDuration(test.input) if test.hasError { assertNotEqual(t, err, nil, "Expected an error for input "+test.input) } else { assertEqual(t, err, nil, "Did not expect an error for input "+test.input) assertEqual(t, result, test.expected, "Expected duration for input "+test.input) } } } // TestRunCommands tests the runCommands function func TestRunCommands(t *testing.T) { m := initialModel(false, "#ffdd57", "#57ddff", nil, nil, nil) m.Init() m.startCommands([]string{"echo Hello, World!"}) m.shellrunner.Stop() } func sendKeys(m model, keys ...tea.KeyType) (model, tea.Cmd) { var updatedModel tea.Model var cmd tea.Cmd for _, key := range keys { updatedModel, cmd = m.Update(tea.KeyMsg{Type: key}) m = updatedModel.(model) m.form.UpdateFieldPositions() } return m, cmd } // TestInputView tests the Update method of the model for the input view func TestInputView(t *testing.T) { focusInput := "10m" breakInput := "5m" intervalInput := "1" m := initialModel(false, "#ffdd57", "#57ddff", &focusInput, &breakInput, &intervalInput) m.View() assertEqual(t, m.currentScreen, INPUT_SCREEN, "Expected currentScreen to be inputScreen") var resultCmd tea.Cmd m, resultCmd = sendKeys(m, tea.KeyTab, tea.KeyTab, tea.KeyTab, tea.KeyEnter) assertNotEqual(t, resultCmd, nil, "Expected resultCmd to be not nil") /* * assertEqual(t, m.form.State, huh.StateCompleted, fmt.Sprintf("Expected form state to be completed: %s", m.form.View())) * assertEqual(t, m.currentScreen, TIMER_SCREEN, "Expected currentScreen to be timerScreen") * assertEqual(t, m.remaining.Round(time.Second), 10*time.Minute, "Expected remaining to be 10 minutes") */ assertEqual(t, m.err, nil, "Expected no error") } func TestTimerView(t *testing.T) { m := initialModel(false, "#ffdd57", "#57ddff", nil, nil, nil) m.View() m.focusTime = 10 * time.Minute m.breakTime = 10 * time.Minute m.intervals = 2 m.state = "Focus" m.currentScreen = TIMER_SCREEN m.startTime = time.Now() // Test timer view m.View() oneSec := timeMsg(time.Now().Add(1 * time.Second)) updatedModel, _ := m.Update(oneSec) m = updatedModel.(model) assertEqual(t, m.state, "Focus", "Expected state to be 'Focus'") // Test switch to break time m.startTime = m.startTime.Add(-10 * time.Minute) updatedModel, _ = m.Update(oneSec) m = updatedModel.(model) assertEqual(t, m.state, "Break", "Expected state to be 'Break'") // Switch back to focus time m.startTime = m.startTime.Add(-10 * time.Minute) updatedModel, _ = m.Update(oneSec) m = updatedModel.(model) assertEqual(t, m.state, "Focus", "Expected state to be 'Focus'") t.Logf("Incorrect state %+v", m) }