test(tui): add tier 1 unit tests for pure logic functions
Cover search filtering, intent matching, card affordances, checklist parsing, template slot discovery/resolve, date grouping, and truncation.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseChecklist(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cardData *string
|
||||
wantLen int
|
||||
}{
|
||||
{"nil data", nil, 0},
|
||||
{"empty JSON", ptr("{}"), 0},
|
||||
{"malformed JSON", ptr("{bad"), 0},
|
||||
{"valid steps", ptr(`{"steps":[{"text":"step 1","done":false},{"text":"step 2","done":true}]}`), 2},
|
||||
{"empty steps array", ptr(`{"steps":[]}`), 0},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := parseChecklist(tt.cardData)
|
||||
if len(got) != tt.wantLen {
|
||||
t.Fatalf("parseChecklist() returned %d steps, want %d", len(got), tt.wantLen)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseChecklist_PreservesDoneState(t *testing.T) {
|
||||
data := `{"steps":[{"text":"first","done":false},{"text":"second","done":true}]}`
|
||||
steps := parseChecklist(&data)
|
||||
if steps[0].Done {
|
||||
t.Fatal("step 0 should not be done")
|
||||
}
|
||||
if !steps[1].Done {
|
||||
t.Fatal("step 1 should be done")
|
||||
}
|
||||
if steps[0].Text != "first" || steps[1].Text != "second" {
|
||||
t.Fatalf("texts wrong: %q, %q", steps[0].Text, steps[1].Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoneCount(t *testing.T) {
|
||||
r := newRunModel("id", ptr(`{"steps":[{"label":"a","done":true},{"label":"b","done":false},{"label":"c","done":true}]}`))
|
||||
if got := r.doneCount(); got != 2 {
|
||||
t.Fatalf("doneCount() = %d, want 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStepsJSON_Roundtrip(t *testing.T) {
|
||||
r := newRunModel("id", ptr(`{"steps":[{"text":"test","done":false}]}`))
|
||||
out := r.stepsJSON()
|
||||
|
||||
var parsed struct {
|
||||
Steps []runStep `json:"steps"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(out), &parsed); err != nil {
|
||||
t.Fatalf("stepsJSON() produced invalid JSON: %v", err)
|
||||
}
|
||||
if len(parsed.Steps) != 1 || parsed.Steps[0].Text != "test" {
|
||||
t.Fatalf("roundtrip failed: %+v", parsed.Steps)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user