gomodoro/main_test.go

196 lines
5.4 KiB
Go
Raw Normal View History

2024-10-18 21:23:09 +00:00
package main
import (
2024-10-23 21:00:45 +00:00
"fmt"
"reflect"
"runtime"
"strings"
2024-10-18 21:23:09 +00:00
"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-23 21:00:45 +00:00
m := newModelBasic(false, "#ffdd57", "#57ddff")
m.onFocusStart = []string{"echo Focus Start"}
2024-10-21 21:48:00 +00:00
m.Init()
2024-10-23 21:00:45 +00:00
m.startCommands(m.onFocusStart)
}
2024-10-18 21:23:09 +00:00
2024-10-23 21:00:45 +00:00
func keyMsgs(keys ...interface{}) []tea.Msg {
keyMessages := []tea.Msg{}
2024-10-18 21:23:09 +00:00
2024-10-23 21:00:45 +00:00
for _, key := range keys {
switch keyType := key.(type) {
case tea.KeyType:
keyMessages = append(keyMessages, tea.KeyMsg{Type: keyType})
case string:
for _, r := range key.(string) {
keyMessages = append(keyMessages, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
}
default:
panic(fmt.Sprintf("Unknown key type: %v", key))
}
}
return keyMessages
2024-10-18 21:23:09 +00:00
}
2024-10-23 21:00:45 +00:00
func sendKeys(m model, keys ...interface{}) (model, tea.Cmd) {
2024-10-21 21:48:00 +00:00
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-23 21:00:45 +00:00
for _, key := range keyMsgs(keys...) {
updatedModel, cmd = m.Update(key)
2024-10-21 21:48:00 +00:00
m = updatedModel.(model)
2024-10-23 21:00:45 +00:00
if cmd != nil {
updatedModel, cmd = m.Update(cmd())
m = updatedModel.(model)
}
2024-10-21 21:48:00 +00:00
}
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) {
2024-10-23 21:00:45 +00:00
m := newModel(false, "#ffdd57", "#57ddff", 0, 0, 0, []string{}, []string{}, []string{})
var updatedModel tea.Model
2024-10-18 21:23:09 +00:00
2024-10-23 21:00:45 +00:00
var resultCmd tea.Cmd
updatedModel, _ = m.Update(m.Init())
m = updatedModel.(model)
2024-10-18 21:23:09 +00:00
2024-10-23 21:00:45 +00:00
// Verify we're on the input screen
2024-10-21 21:48:00 +00:00
assertEqual(t, m.currentScreen, INPUT_SCREEN, "Expected currentScreen to be inputScreen")
2024-10-23 21:00:45 +00:00
assertFunc(t, strings.Contains, m.View(), "Break time", "Expected view to contain 'enter next'")
2024-10-18 21:23:09 +00:00
2024-10-23 21:00:45 +00:00
// Fill the form
m, resultCmd = sendKeys(m, "10m", tea.KeyEnter, "10m", tea.KeyEnter, "2", tea.KeyEnter)
2024-10-21 21:48:00 +00:00
assertNotEqual(t, resultCmd, nil, "Expected resultCmd to be not nil")
2024-10-23 21:00:45 +00:00
// Apply final next result
updatedModel, resultCmd = m.Update(resultCmd())
m = updatedModel.(model)
// Make sure the next command is to submit the form
assertNotEqual(t, resultCmd, nil, "Expected resultCmd to be not nil")
assertEqual(
t,
reflect.ValueOf(resultCmd).Pointer(),
reflect.ValueOf(formSubmit).Pointer(),
fmt.Sprintf("Expected resultCmd to be formSubmit, found %v", runtime.FuncForPC(reflect.ValueOf(resultCmd).Pointer()).Name()),
)
// Apply submit form command
updatedModel, resultCmd = m.Update(resultCmd())
m = updatedModel.(model)
2024-10-18 21:23:09 +00:00
assertEqual(t, m.err, nil, "Expected no error")
2024-10-23 21:00:45 +00:00
assertNotEqual(t, resultCmd, nil, "Expected resultCmd to be not nil")
assertEqual(
t,
reflect.ValueOf(resultCmd).Pointer(),
reflect.ValueOf(startTimer).Pointer(),
fmt.Sprintf("Expected resultCmd to be startTimer, found %v", runtime.FuncForPC(reflect.ValueOf(resultCmd).Pointer()).Name()),
)
2024-10-21 21:48:00 +00:00
}
func TestTimerView(t *testing.T) {
2024-10-23 21:00:45 +00:00
m := newModel(false, "#ffdd57", "#57ddff", 10*time.Minute, 10*time.Minute, 2, []string{}, []string{}, []string{})
2024-10-21 21:48:00 +00:00
2024-10-23 21:00:45 +00:00
var updatedModel tea.Model
// Init model with timer values
updatedModel, _ = m.Update(m.Init())
m = updatedModel.(model)
// Start timer (batch result from above doesn't apply here)
updatedModel, _ = m.Update(startTimer())
m = updatedModel.(model)
2024-10-18 21:23:09 +00:00
// Test timer view
2024-10-23 21:00:45 +00:00
assertEqual(t, m.currentScreen, TIMER_SCREEN, "Expected currentScreen to be timerScreen")
assertFunc(t, strings.Contains, m.View(), "Focus", "Expected view to contain 'Focus'")
assertEqual(t, m.state, "Focus", "Expected state to be 'Focus'")
2024-10-18 21:23:09 +00:00
oneSec := timeMsg(time.Now().Add(1 * time.Second))
2024-10-23 21:00:45 +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
}