Parse flags, load configuration, restore remembered topics, prime the stream cursor from history, and run the Bubble Tea program.
89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"lazyntfy/internal/config"
|
|
"lazyntfy/internal/history"
|
|
"lazyntfy/internal/store"
|
|
)
|
|
|
|
func TestParseArgsTopics(t *testing.T) {
|
|
var errBuf bytes.Buffer
|
|
opt, ver, err := parseArgs([]string{"--since", "1h", "--host", "https://ntfy.example", "alerts", "backups"}, &errBuf)
|
|
if err != nil || ver {
|
|
t.Fatal(err, ver)
|
|
}
|
|
if opt.Since != "1h" || opt.Host != "https://ntfy.example" || len(opt.Topics) != 2 || opt.Topics[0] != "alerts" {
|
|
t.Fatalf("%+v", opt)
|
|
}
|
|
}
|
|
|
|
func TestHelpExitsCleanly(t *testing.T) {
|
|
var errBuf bytes.Buffer
|
|
code := run([]string{"--help"}, &errBuf)
|
|
if code != 0 || !bytes.Contains(errBuf.Bytes(), []byte("lazyntfy")) {
|
|
t.Fatalf("code %d\n%s", code, errBuf.String())
|
|
}
|
|
}
|
|
|
|
func TestRememberedSubsUseCurrentHostAndAuth(t *testing.T) {
|
|
db, err := history.Open(filepath.Join(t.TempDir(), "history.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
if err := db.SaveTopic(config.Sub{Origin: "https://ntfy.example", Name: "alerts", Display: "Alerts"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.SaveTopic(config.Sub{Name: "backups"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
auth := config.Auth{Token: "tk_current"}
|
|
got := rememberedSubs(db, "https://default.example", auth)
|
|
if len(got) != 2 {
|
|
t.Fatalf("got %+v", got)
|
|
}
|
|
byName := map[string]config.Sub{}
|
|
for _, sub := range got {
|
|
byName[sub.Name] = sub
|
|
}
|
|
if sub := byName["alerts"]; sub.Origin != "https://ntfy.example" || sub.Auth.Token != "tk_current" {
|
|
t.Fatalf("alerts %+v", sub)
|
|
}
|
|
if sub := byName["backups"]; sub.Origin != "https://default.example" || sub.Auth.Token != "tk_current" {
|
|
t.Fatalf("backups %+v", sub)
|
|
}
|
|
if rememberedSubs(nil, "https://default.example", auth) != nil {
|
|
t.Fatal("nil history should yield no topics")
|
|
}
|
|
}
|
|
|
|
func TestNewestFromHistory(t *testing.T) {
|
|
db, err := history.Open(filepath.Join(t.TempDir(), "history.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
if err := db.SaveMessage("https://ntfy.example", store.Message{ID: "a", Topic: "alerts", Time: 100}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.SaveMessage("https://ntfy.example", store.Message{ID: "b", Topic: "alerts", Time: 250}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
subs := []config.Sub{{Origin: "https://ntfy.example", Name: "alerts"}}
|
|
got := newestFromHistory(db, subs)
|
|
if got["https://ntfy.example/alerts"] != 250 {
|
|
t.Fatalf("newest %+v", got)
|
|
}
|
|
if len(newestFromHistory(db, []config.Sub{{Origin: "https://ntfy.example", Name: "empty"}})) != 0 {
|
|
t.Fatal("topic without history should not be primed")
|
|
}
|
|
if newestFromHistory(nil, subs) != nil {
|
|
t.Fatal("nil history should yield no cursors")
|
|
}
|
|
}
|