test: extend tui test coverage

This commit is contained in:
Slug-Boi
2025-04-10 12:17:08 +02:00
parent e8933bc764
commit e7656ae4e0
+186 -6
View File
@@ -2,6 +2,7 @@ package tui
import ( import (
"bytes" "bytes"
"fmt"
"os" "os"
"strings" "strings"
"testing" "testing"
@@ -77,12 +78,47 @@ func TestShowUser(t *testing.T) {
return bytes.Contains(bts, []byte("\"Authors\": {")) return bytes.Contains(bts, []byte("\"Authors\": {"))
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*2)) }, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*2))
keyPress(tm, "enter")
keyPress(tm, "q") keyPress(tm, "q")
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second)) tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
} }
func TestShowUserPanicFileNotFound(t *testing.T) {
setup()
defer teardown()
// Use defer with recover to catch panics
defer func() {
if r := recover(); r != nil {
t.Logf("Recovered from expected panic: %v", r)
// You can optionally verify the panic message here
if !strings.Contains(fmt.Sprint(r), "Could not open author file:") {
t.Errorf("Unexpected panic message: %v", r)
}
}
}()
m := intialModel_US("non_existent_file")
tm := teatest.NewTestModel(
t, m,
teatest.WithInitialTermSize(300, 300),
)
// Verify error message appears in output
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool {
return bytes.Contains(bts, []byte("could not open author file"))
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*2))
// Send quit command
keyPress(tm, "q")
// Wait for clean shutdown
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
// tui_show_users TESTS END // tui_show_users TESTS END
// tui_author TESTS BEGIN // tui_author TESTS BEGIN
@@ -132,6 +168,90 @@ func TestEntryTA(t *testing.T) {
} }
} }
func TestErrorGetMissingFields(t *testing.T) {
setup()
defer teardown()
// Test case 1: No inputs
m := createAuthorModel(nil)
errorGetMissingFields(m)
if len(m.errorModel.missing) != 4 {
t.Errorf("Expected 4 missing fields, got %d\n%v", len(m.errorModel.missing), m.errorModel.missing)
}
m = createAuthorModel(nil)
m.inputs[0].SetValue("")
m.inputs[1].SetValue("value")
m.inputs[2].SetValue("")
m.inputs[3].SetValue("value")
tempAuthorToggle = false
errorGetMissingFields(m)
expectedMissing := []string{"- Shortname", "- Username"}
if len(m.errorModel.missing) != len(expectedMissing) {
t.Errorf("Expected %d missing fields, got %d", len(expectedMissing), len(m.errorModel.missing))
}
for i, missing := range expectedMissing {
if m.errorModel.missing[i] != missing {
t.Errorf("Expected '%s', got '%s'", missing, m.errorModel.missing[i])
}
}
m = createAuthorModel(nil)
m.inputs[0].SetValue("value1")
m.inputs[1].SetValue("value2")
m.inputs[2].SetValue("value3")
m.inputs[3].SetValue("value4")
m.inputs[4].SetValue("value5")
tempAuthorToggle = true
errorGetMissingFields(m)
if len(m.errorModel.missing) != 0 {
t.Errorf("Expected no missing fields, got %v", m.errorModel.missing)
}
}
func Test_EntryCA_TriggerError(t *testing.T) {
setup()
defer teardown()
m := listModel()
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
keyPress(tm, "C")
keyPress(tm, "enter")
tm.Type("test")
keyPress(tm, "enter")
tm.Type("testing2")
keyPress(tm, "enter")
keyPress(tm, "enter")
keyPress(tm, "tab")
keyPress(tm, "enter")
keyPress(tm, "esc")
keyPress(tm, "esc")
keyPress(tm, "esc")
fm := tm.FinalModel(t)
mm, ok := fm.(model)
if !ok {
t.Errorf("Expected model_ca, got %T", fm)
}
if len(mm.list.Items()) != 2 {
t.Errorf("Expected 2 inputs, got %d\n%v", len(mm.list.Items()), mm.list.Items())
}
}
func Test_EntryCA(t *testing.T) { func Test_EntryCA(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
@@ -208,6 +328,8 @@ func Test_EntryCM(t *testing.T) {
t, m, teatest.WithInitialTermSize(300, 300), t, m, teatest.WithInitialTermSize(300, 300),
) )
tm.Type("test commit message") tm.Type("test commit message")
keyPress(tm, "shift+tab")
tm.Type("new line")
keyPress(tm, "enter") keyPress(tm, "enter")
@@ -217,11 +339,56 @@ func Test_EntryCM(t *testing.T) {
t.Errorf("Expected model_cm, got %T", fm) t.Errorf("Expected model_cm, got %T", fm)
} }
if m.textarea.Value() != "test commit message" { if m.textarea.Value() != "test commit message\nnew line" {
t.Errorf("Expected 'test commit message', got %s", m.textarea.Value()) t.Errorf("Expected 'test commit message', got %s", m.textarea.Value())
} }
} }
func Test_EntryCM_Quit(t *testing.T) {
setup()
defer teardown()
m := initialModel_cm()
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
keyPress(tm, "esc")
fm := tm.FinalModel(t)
m, ok := fm.(model_cm)
if !ok {
t.Errorf("Expected model_cm, got %T", fm)
}
if m.textarea.Value() != "" {
t.Errorf("Expected empty textarea, got %s", m.textarea.Value())
}
}
// cannot test sigkill as it does not play nicely with these types of tests :(
func Test_EntryCM_Unfocuse(t *testing.T) {
setup()
defer teardown()
m := initialModel_cm()
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
keyPress(tm, "down")
keyPress(tm, "esc")
fm := tm.FinalModel(t)
m, ok := fm.(model_cm)
if !ok {
t.Errorf("Expected model_cm, got %T", fm)
}
if m.textarea.Value() != "" {
t.Errorf("Expected empty textarea, got %s", m.textarea.Value())
}
}
// tui_commit_message TESTS END // tui_commit_message TESTS END
// tui_list TESTS BEGIN // tui_list TESTS BEGIN
@@ -363,7 +530,7 @@ func Test_GroupSelection(t *testing.T) {
keyPress(tm, "enter") keyPress(tm, "enter")
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
m, ok := fm.(model) _, ok := fm.(model)
if !ok { if !ok {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
@@ -377,7 +544,19 @@ func Test_pagination(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
m := mainModel{} // Add 20 authors to the test data
for i := 0; i < 20; i++ {
utils.Users[fmt.Sprintf("author%d", i)] = utils.User{
Shortname: fmt.Sprintf("a%d", i),
Longname: fmt.Sprintf("Author %d", i),
Username: fmt.Sprintf("AuthorUser%d", i),
Email: fmt.Sprintf("author%d@test.com", i),
Ex: false,
Groups: []string{},
}
}
m := listModel()
tm := teatest.NewTestModel( tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(25, 25), t, m, teatest.WithInitialTermSize(25, 25),
@@ -387,14 +566,15 @@ func Test_pagination(t *testing.T) {
tm.Quit() tm.Quit()
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
m, ok := fm.(mainModel) m, ok := fm.(model)
if !ok { if !ok {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
if m.paginator.Page != 1 { if m.list.Paginator.Page != 1 {
t.Errorf("Expected page 1, got %d", m.paginator.Page) t.Errorf("Expected page 1, got %d", m.list.Paginator.Page)
} }
} }
// tui_groups TESTS END // tui_groups TESTS END