aboutsummaryrefslogtreecommitdiffstats
path: root/internal/notify/policy_test.go
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-15 13:44:41 +0200
committerDanilo M. <danix@danix.xyz>2026-09-15 13:44:54 +0200
commitcb273a7561a390f829dbc677d3f3d6e2940dbc2d (patch)
treed0ae644d4f303a99d1652b514a5b24c6902859fb /internal/notify/policy_test.go
downloadnotifyd-cb273a7561a390f829dbc677d3f3d6e2940dbc2d.tar.gz
notifyd-cb273a7561a390f829dbc677d3f3d6e2940dbc2d.zip
feat: add the module and the notification policy
The policy functions are pure so they are tested without a bus: urgency from the hints, the timeout rule with its urgency defaults, the two stack tag spellings, and the action pair parse.
Diffstat (limited to 'internal/notify/policy_test.go')
-rw-r--r--internal/notify/policy_test.go89
1 files changed, 89 insertions, 0 deletions
diff --git a/internal/notify/policy_test.go b/internal/notify/policy_test.go
new file mode 100644
index 0000000..256c374
--- /dev/null
+++ b/internal/notify/policy_test.go
@@ -0,0 +1,89 @@
+package notify
+
+import (
+ "testing"
+
+ "github.com/godbus/dbus/v5"
+)
+
+func TestUrgencyFromHints(t *testing.T) {
+ cases := []struct {
+ name string
+ hints map[string]dbus.Variant
+ want Urgency
+ }{
+ {"missing is normal", map[string]dbus.Variant{}, Normal},
+ {"low byte", map[string]dbus.Variant{"urgency": dbus.MakeVariant(byte(0))}, Low},
+ {"normal byte", map[string]dbus.Variant{"urgency": dbus.MakeVariant(byte(1))}, Normal},
+ {"critical byte", map[string]dbus.Variant{"urgency": dbus.MakeVariant(byte(2))}, Critical},
+ {"critical int32", map[string]dbus.Variant{"urgency": dbus.MakeVariant(int32(2))}, Critical},
+ {"wrong type is normal", map[string]dbus.Variant{"urgency": dbus.MakeVariant("2")}, Normal},
+ }
+ for _, c := range cases {
+ t.Run(c.name, func(t *testing.T) {
+ if got := UrgencyFromHints(c.hints); got != c.want {
+ t.Errorf("UrgencyFromHints = %q, want %q", got, c.want)
+ }
+ })
+ }
+}
+
+func TestEffectiveTimeoutMS(t *testing.T) {
+ cases := []struct {
+ name string
+ expire int32
+ u Urgency
+ want int64
+ }{
+ {"zero uses low default", 0, Low, 10_000},
+ {"zero uses normal default", 0, Normal, 10_000},
+ {"zero critical never", 0, Critical, 0},
+ {"minus one never", -1, Normal, 0},
+ {"explicit wins", 3_000, Normal, 3_000},
+ {"sub second exact", 1, Normal, 1},
+ }
+ for _, c := range cases {
+ t.Run(c.name, func(t *testing.T) {
+ if got := EffectiveTimeoutMS(c.expire, c.u); got != c.want {
+ t.Errorf("EffectiveTimeoutMS(%d, %q) = %d, want %d", c.expire, c.u, got, c.want)
+ }
+ })
+ }
+}
+
+func TestStackTagFromHints(t *testing.T) {
+ dunst := map[string]dbus.Variant{"x-dunst-stack-tag": dbus.MakeVariant("mail-a")}
+ danix := map[string]dbus.Variant{"x-danix-stack-tag": dbus.MakeVariant("mail-b")}
+ both := map[string]dbus.Variant{
+ "x-dunst-stack-tag": dbus.MakeVariant("mail-a"),
+ "x-danix-stack-tag": dbus.MakeVariant("mail-b"),
+ }
+ if got := StackTagFromHints(dunst); got != "mail-a" {
+ t.Errorf("dunst tag = %q, want mail-a", got)
+ }
+ if got := StackTagFromHints(danix); got != "mail-b" {
+ t.Errorf("danix tag = %q, want mail-b", got)
+ }
+ if got := StackTagFromHints(both); got != "mail-a" {
+ t.Errorf("dunst wins when both present = %q, want mail-a", got)
+ }
+ if got := StackTagFromHints(map[string]dbus.Variant{}); got != "" {
+ t.Errorf("empty = %q, want empty", got)
+ }
+}
+
+func TestParseActions(t *testing.T) {
+ got := ParseActions([]string{"default", "open", "other", "do the thing"})
+ want := [][2]string{{"default", "open"}, {"other", "do the thing"}}
+ if len(got) != len(want) {
+ t.Fatalf("len = %d, want %d", len(got), len(want))
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ t.Errorf("action %d = %v, want %v", i, got[i], want[i])
+ }
+ }
+ if got := ParseActions(nil); len(got) != 0 {
+ t.Errorf("nil actions = %v, want empty", got)
+ }
+}