▍ humdrum codex / ticktock v0.0.2
license AGPL-3.0
2.1 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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")
	}
}