Files
nib-v1/internal/tui/fill_test.go
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

82 lines
1.8 KiB
Go

package tui
import (
"testing"
)
func TestDiscoverSlots(t *testing.T) {
tests := []struct {
name string
body string
wantNames []string
}{
{"no slots", "plain text", nil},
{"single slot", "Hello ${name}", []string{"name"}},
{"multiple slots", "${greeting} ${name}, welcome to ${place}", []string{"greeting", "name", "place"}},
{"duplicate slot deduped", "${x} and ${x} again", []string{"x"}},
{"adjacent slots", "${a}${b}", []string{"a", "b"}},
{"nested braces ignored", "${{bad}}", nil},
{"empty body", "", nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := discoverSlots(tt.body)
if len(tt.wantNames) == 0 && len(got) == 0 {
return
}
if len(got) != len(tt.wantNames) {
t.Fatalf("got %d slots, want %d", len(got), len(tt.wantNames))
}
for i, s := range got {
if s.Name != tt.wantNames[i] {
t.Fatalf("slot[%d].Name = %q, want %q", i, s.Name, tt.wantNames[i])
}
}
})
}
}
func TestResolve(t *testing.T) {
tests := []struct {
name string
body string
slots []fillSlot
want string
}{
{
"all filled",
"Hello ${name}, welcome to ${place}",
[]fillSlot{{Name: "name", Value: "Alice"}, {Name: "place", Value: "Nib"}},
"Hello Alice, welcome to Nib",
},
{
"unfilled stays as placeholder",
"${greeting} ${name}",
[]fillSlot{{Name: "greeting", Value: "Hi"}, {Name: "name"}},
"Hi ${name}",
},
{
"no slots",
"plain text",
nil,
"plain text",
},
{
"repeated slot filled everywhere",
"${x} and ${x}",
[]fillSlot{{Name: "x", Value: "Y"}},
"Y and Y",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := fillModel{body: tt.body, slots: tt.slots}
if got := f.resolve(); got != tt.want {
t.Fatalf("resolve() = %q, want %q", got, tt.want)
}
})
}
}