package tui import ( "strings" "testing" "time" tea "charm.land/bubbletea/v2" "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/cache" "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" ) func TestFavoritesHistoryAndActions(t *testing.T) { c := cache.New("", 3600) for _, name := range []string{"a", "b", "pinned"} { c.Upsert(cache.Entry{Name: name, IP: "10.0.0.1", Kind: "device"}) c.MarkUsed(name) } m := New(nil, c, "") m.SetHistoryLimit(1) m.SetPreferences(map[string]bool{"pinned": true, "offline/missing": true}, map[string]bool{"pinned": true}, nil) msg := m.loadRecent()().(searchResultMsg) if len(msg.entries) < 2 || msg.entries[0].Target() != "pinned" { t.Fatalf("%+v", msg.entries) } m.Update(msg) m.Update(tea.WindowSizeMsg{Width: 160, Height: 30}) if !strings.Contains(m.View().Content, "◆") || !strings.Contains(m.View().Content, "cache") { t.Fatal(m.View().Content) } m.SetCommands(func(name, kind string, tags []string) []string { return []string{"mtr"} }) m.Update(tea.KeyPressMsg{Code: 'r', Mod: tea.ModCtrl}) m.Update(tea.KeyPressMsg{Code: tea.KeyDown}) m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) if m.Selected() == nil || m.Selected().Command != "mtr" || m.Selected().Name != "pinned" { t.Fatalf("%+v", m.Selected()) } } func TestHistoryLimitAndPreferencePersistence(t *testing.T) { c := cache.New("", 3600) for _, n := range []string{"a", "b", "c"} { c.Upsert(cache.Entry{Name: n}) c.MarkUsed(n) } m := New(nil, c, "") m.SetHistoryLimit(2) if got := len(m.loadRecent()().(searchResultMsg).entries); got != 2 { t.Fatal(got) } m.SetHistoryLimit(0) if got := len(m.loadRecent()().(searchResultMsg).entries); got != 3 { t.Fatal(got) } saved := false m.SetPreferences(nil, nil, func(f, p map[string]bool) error { saved = f["prod/web"] && p["prod/web"]; return nil }) entries := []netbox.HostEntry{{Name: "web", Source: "prod", Cached: true, CachedAt: time.Now().Add(-2 * time.Hour)}} m.lastResults = entries m.updateListItems(entries) m.Update(tea.KeyPressMsg{Code: 'p', Mod: tea.ModCtrl}) if !saved { t.Fatal("pin not persisted") } item := m.list.Items()[0].(hostItem) if !strings.Contains(item.provenance, "stale") { t.Fatal(item.provenance) } }