package autotrack import "testing" func TestDomain(t *testing.T) { cases := []struct{ url, want string }{ {"https://www.github.com/foo", "github.com"}, {"https://mail.google.com/mail", "mail.google.com"}, {"HTTPS://WWW.Example.COM/x", "example.com"}, // lowercased, www stripped {"", ""}, {"://not-a-url", ""}, } for _, c := range cases { if got := Domain(c.url); got != c.want { t.Errorf("Domain(%q)=%q, want %q", c.url, got, c.want) } } } func TestClassify(t *testing.T) { active := &Ctx{App: "Trace", Title: "doc"} away := &Ctx{App: "loginwindow"} if got := Classify(0, active, 900, true); got == nil || !got.Idle { t.Errorf("locked ⇒ idle, got %+v", got) } if got := Classify(0, away, 900, false); got == nil || !got.Idle { t.Errorf("away app ⇒ idle, got %+v", got) } if got := Classify(900, active, 900, false); got == nil || !got.Idle { t.Errorf("idle >= grace ⇒ idle, got %+v", got) } if got := Classify(899, active, 900, false); got != active { t.Errorf("present-but-passive under grace passes through, got %+v", got) } if got := Classify(0, nil, 900, false); got != nil { t.Errorf("no foreground app, not locked ⇒ nil, got %+v", got) } if got := Classify(0, nil, 900, true); got == nil || !got.Idle { t.Errorf("locked with nil ctx still idle, got %+v", got) } } func TestCtxKey(t *testing.T) { if CtxKey(nil) != (Key{}) { t.Error("nil ctx should give zero key") } idle1, idle2 := CtxKey(&Ctx{Idle: true}), CtxKey(&Ctx{Idle: true, App: "x"}) if idle1 != idle2 { t.Error("idle is its own key regardless of app") } browser := CtxKey(&Ctx{App: "Safari", Title: "GitHub", Domain: "github.com"}) if browser != (Key{Valid: true, App: "Safari", Rest: "github.com"}) { t.Errorf("browser key should use domain, got %+v", browser) } editor := CtxKey(&Ctx{App: "Trace", Title: "notes.md"}) if editor != (Key{Valid: true, App: "Trace", Rest: "notes.md"}) { t.Errorf("non-browser key should use title, got %+v", editor) } if CtxKey(&Ctx{App: "Trace", Title: "a"}) == CtxKey(&Ctx{App: "Trace", Title: "b"}) { t.Error("different titles should be different activities") } }