notification_test.go

 1package notification_test
 2
 3import (
 4	"testing"
 5
 6	"git.secluded.site/crush/internal/ui/notification"
 7	"github.com/stretchr/testify/require"
 8)
 9
10func TestNoopBackend_Send(t *testing.T) {
11	t.Parallel()
12
13	backend := notification.NoopBackend{}
14	err := backend.Send(notification.Notification{
15		Title:   "Test Title",
16		Message: "Test Message",
17	})
18	require.NoError(t, err)
19}
20
21func TestNativeBackend_Send(t *testing.T) {
22	t.Parallel()
23
24	backend := notification.NewNativeBackend(nil)
25
26	var capturedTitle, capturedMessage string
27	var capturedIcon any
28	backend.SetNotifyFunc(func(title, message string, icon any) error {
29		capturedTitle = title
30		capturedMessage = message
31		capturedIcon = icon
32		return nil
33	})
34
35	err := backend.Send(notification.Notification{
36		Title:   "Hello",
37		Message: "World",
38	})
39	require.NoError(t, err)
40	require.Equal(t, "Hello", capturedTitle)
41	require.Equal(t, "World", capturedMessage)
42	require.Nil(t, capturedIcon)
43}
44
45func TestChannelSink(t *testing.T) {
46	t.Parallel()
47
48	ch := make(chan notification.Notification, 1)
49	sink := notification.NewChannelSink(ch)
50
51	sink(notification.Notification{
52		Title:   "Test",
53		Message: "Notification",
54	})
55
56	select {
57	case n := <-ch:
58		require.Equal(t, "Test", n.Title)
59		require.Equal(t, "Notification", n.Message)
60	default:
61		t.Fatal("expected notification in channel")
62	}
63}
64
65func TestChannelSink_FullChannel(t *testing.T) {
66	t.Parallel()
67
68	// Create a full channel (buffer of 1, already has 1 item).
69	ch := make(chan notification.Notification, 1)
70	ch <- notification.Notification{Title: "First", Message: "First"}
71
72	sink := notification.NewChannelSink(ch)
73
74	// This should not block; it drains the old notification and sends the new.
75	sink(notification.Notification{
76		Title:   "Second",
77		Message: "Second",
78	})
79
80	// The second notification should replace the first (drain-before-send).
81	n := <-ch
82	require.Equal(t, "Second", n.Title)
83
84	select {
85	case <-ch:
86		t.Fatal("expected channel to be empty")
87	default:
88		// Expected.
89	}
90}