2024-10-18 21:23:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
2024-10-21 20:35:53 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-18 21:23:09 +00:00
|
|
|
// 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) {
|
2024-10-21 21:48:00 +00:00
|
|
|
m := initialModel(false, "#ffdd57", "#57ddff", nil, nil, nil)
|
|
|
|
m.Init()
|
2024-10-18 21:23:09 +00:00
|
|
|
|
2024-10-21 20:35:53 +00:00
|
|
|
m.startCommands([]string{"echo Hello, World!"})
|
2024-10-18 21:23:09 +00:00
|
|
|
|
2024-10-21 20:35:53 +00:00
|
|
|
m.shellrunner.Stop()
|
2024-10-18 21:23:09 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 21:48:00 +00:00
|
|
|
func sendKeys(m model, keys ...tea.KeyType) (model, tea.Cmd) {
|
|
|
|
var updatedModel tea.Model
|
2024-10-18 21:23:09 +00:00
|
|
|
|
2024-10-21 21:48:00 +00:00
|
|
|
var cmd tea.Cmd
|
2024-10-18 21:23:09 +00:00
|
|
|
|
2024-10-21 21:48:00 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
updatedModel, cmd = m.Update(tea.KeyMsg{Type: key})
|
|
|
|
m = updatedModel.(model)
|
|
|
|
m.form.UpdateFieldPositions()
|
|
|
|
}
|
2024-10-18 21:23:09 +00:00
|
|
|
|
2024-10-21 21:48:00 +00:00
|
|
|
return m, cmd
|
|
|
|
}
|
2024-10-18 21:23:09 +00:00
|
|
|
|
2024-10-21 21:48:00 +00:00
|
|
|
// TestInputView tests the Update method of the model for the input view
|
|
|
|
func TestInputView(t *testing.T) {
|
|
|
|
focusInput := "10m"
|
|
|
|
breakInput := "5m"
|
|
|
|
intervalInput := "1"
|
2024-10-18 21:23:09 +00:00
|
|
|
|
2024-10-21 21:48:00 +00:00
|
|
|
m := initialModel(false, "#ffdd57", "#57ddff", &focusInput, &breakInput, &intervalInput)
|
|
|
|
m.View()
|
2024-10-18 21:23:09 +00:00
|
|
|
|
2024-10-21 21:48:00 +00:00
|
|
|
assertEqual(t, m.currentScreen, INPUT_SCREEN, "Expected currentScreen to be inputScreen")
|
2024-10-18 21:23:09 +00:00
|
|
|
|
2024-10-21 21:48:00 +00:00
|
|
|
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")
|
|
|
|
*/
|
2024-10-18 21:23:09 +00:00
|
|
|
|
|
|
|
assertEqual(t, m.err, nil, "Expected no error")
|
2024-10-21 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2024-10-18 21:23:09 +00:00
|
|
|
|
|
|
|
// Test timer view
|
|
|
|
m.View()
|
|
|
|
|
|
|
|
oneSec := timeMsg(time.Now().Add(1 * time.Second))
|
2024-10-21 21:48:00 +00:00
|
|
|
updatedModel, _ := m.Update(oneSec)
|
2024-10-18 21:23:09 +00:00
|
|
|
m = updatedModel.(model)
|
|
|
|
|
|
|
|
assertEqual(t, m.state, "Focus", "Expected state to be 'Focus'")
|
|
|
|
|
|
|
|
// Test switch to break time
|
2024-10-21 20:35:53 +00:00
|
|
|
m.startTime = m.startTime.Add(-10 * time.Minute)
|
2024-10-18 21:23:09 +00:00
|
|
|
updatedModel, _ = m.Update(oneSec)
|
|
|
|
m = updatedModel.(model)
|
|
|
|
|
|
|
|
assertEqual(t, m.state, "Break", "Expected state to be 'Break'")
|
|
|
|
|
|
|
|
// Switch back to focus time
|
2024-10-21 20:35:53 +00:00
|
|
|
m.startTime = m.startTime.Add(-10 * time.Minute)
|
2024-10-18 21:23:09 +00:00
|
|
|
updatedModel, _ = m.Update(oneSec)
|
|
|
|
m = updatedModel.(model)
|
|
|
|
|
|
|
|
assertEqual(t, m.state, "Focus", "Expected state to be 'Focus'")
|
2024-10-21 21:48:00 +00:00
|
|
|
t.Logf("Incorrect state %+v", m)
|
2024-10-18 21:23:09 +00:00
|
|
|
}
|