Files
lerko 476abbed00 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.
2026-05-19 21:10:51 -04:00

95 lines
2.3 KiB
Go

package tui
import (
"testing"
"time"
"github.com/lerko/nib/internal/db"
)
func TestGroupByDate(t *testing.T) {
may19 := time.Date(2026, 5, 19, 10, 0, 0, 0, time.UTC)
may19b := time.Date(2026, 5, 19, 14, 0, 0, 0, time.UTC)
may18 := time.Date(2026, 5, 18, 9, 0, 0, 0, time.UTC)
entities := []*db.Entity{
{ID: "1", CreatedAt: may19, Body: "a"},
{ID: "2", CreatedAt: may19b, Body: "b"},
{ID: "3", CreatedAt: may18, Body: "c"},
}
groups := groupByDate(entities)
if len(groups) != 2 {
t.Fatalf("expected 2 groups, got %d", len(groups))
}
if len(groups[0].entities) != 2 {
t.Fatalf("first group should have 2 entities, got %d", len(groups[0].entities))
}
if len(groups[1].entities) != 1 {
t.Fatalf("second group should have 1 entity, got %d", len(groups[1].entities))
}
if groups[0].label != "may 19" {
t.Fatalf("first group label = %q, want %q", groups[0].label, "may 19")
}
}
func TestGroupByDate_Empty(t *testing.T) {
groups := groupByDate(nil)
if len(groups) != 0 {
t.Fatalf("expected 0 groups, got %d", len(groups))
}
}
func TestGroupByDate_SingleEntity(t *testing.T) {
e := []*db.Entity{{ID: "1", CreatedAt: time.Now(), Body: "solo"}}
groups := groupByDate(e)
if len(groups) != 1 || len(groups[0].entities) != 1 {
t.Fatal("single entity should produce 1 group with 1 entity")
}
}
func TestTruncate(t *testing.T) {
tests := []struct {
name string
input string
maxLen int
want string
}{
{"short enough", "hello", 10, "hello"},
{"exact length", "hello", 5, "hello"},
{"truncated", "hello world", 6, "hello…"},
{"very short max", "hello", 3, "…"},
{"unicode", "héllo wörld", 7, "héllo …"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := truncate(tt.input, tt.maxLen)
if got != tt.want {
t.Fatalf("truncate(%q, %d) = %q, want %q", tt.input, tt.maxLen, got, tt.want)
}
})
}
}
func TestStripAnsi(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"no ansi", "hello", "hello"},
{"with color", "\x1b[31mred\x1b[0m", "red"},
{"empty", "", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := stripAnsi(tt.input)
if got != tt.want {
t.Fatalf("stripAnsi(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}