1package notification_test
2
3import (
4 "fmt"
5 "os"
6 "testing"
7
8 "git.secluded.site/crush/internal/config"
9 "git.secluded.site/crush/internal/log"
10 "git.secluded.site/crush/internal/notification"
11 "github.com/stretchr/testify/require"
12)
13
14func TestSend_Disabled(t *testing.T) {
15 // Pre-initialize logger to os.DevNull to prevent file lock on Windows.
16 log.Setup(os.DevNull, false)
17
18 // Setup a temporary config with DisableNotifications = true
19 tempDir := t.TempDir()
20 cfg, err := config.Init(tempDir, tempDir, false)
21 require.NoError(t, err)
22
23 // Explicitly disable notifications
24 cfg.Options.DisableNotifications = true
25
26 // Call Send
27 // This should return nil immediately because notifications are disabled
28 err = notification.Send("Test Title", "Test Message")
29 require.NoError(t, err)
30}
31
32func TestSend_Focused(t *testing.T) {
33 // Pre-initialize logger to os.DevNull to prevent file lock on Windows.
34 log.Setup(os.DevNull, false)
35
36 // Reset globals after test
37 defer func() {
38 notification.SetFocusSupport(false)
39 notification.SetFocused(true)
40 }()
41
42 // Setup a temporary config with DisableNotifications = false
43 tempDir := t.TempDir()
44 cfg, err := config.Init(tempDir, tempDir, false)
45 require.NoError(t, err)
46
47 cfg.Options.DisableNotifications = false
48
49 // Set up focus state so notification should be skipped
50 notification.SetFocusSupport(true)
51 notification.SetFocused(true)
52
53 // Call Send
54 // This should return nil immediately because window is focused
55 err = notification.Send("Test Title", "Test Message")
56 require.NoError(t, err)
57}
58
59func TestSend_Success(t *testing.T) {
60 // Pre-initialize logger to os.DevNull to prevent file lock on Windows.
61 log.Setup(os.DevNull, false)
62
63 // Reset globals after test
64 defer func() {
65 notification.SetFocusSupport(false)
66 notification.SetFocused(true)
67 notification.ResetNotifyFunc()
68 }()
69
70 // Setup a temporary config with DisableNotifications = false
71 tempDir := t.TempDir()
72 cfg, err := config.Init(tempDir, tempDir, false)
73 require.NoError(t, err)
74
75 cfg.Options.DisableNotifications = false
76
77 // Set up focus state so notification should NOT be skipped
78 notification.SetFocusSupport(true)
79 notification.SetFocused(false)
80
81 // Mock the notify function
82 var capturedTitle, capturedMessage string
83 var capturedIcon any
84 mockNotify := func(title, message string, icon any) error {
85 capturedTitle = title
86 capturedMessage = message
87 capturedIcon = icon
88 return nil
89 }
90 notification.SetNotifyFunc(mockNotify)
91
92 // Call Send
93 err = notification.Send("Hello", "World")
94 require.NoError(t, err)
95
96 // Verify mock was called with correct arguments
97 require.Equal(t, "Hello", capturedTitle)
98 require.Equal(t, "World", capturedMessage)
99 require.NotNil(t, capturedIcon)
100}
101
102func TestSend_FocusNotSupported(t *testing.T) {
103 // Pre-initialize logger to os.DevNull to prevent file lock on Windows.
104 log.Setup(os.DevNull, false)
105
106 // Reset globals after test
107 defer func() {
108 notification.SetFocusSupport(false)
109 notification.SetFocused(true)
110 notification.ResetNotifyFunc()
111 }()
112
113 // Setup a temporary config with DisableNotifications = false
114 tempDir := t.TempDir()
115 cfg, err := config.Init(tempDir, tempDir, false)
116 require.NoError(t, err)
117
118 cfg.Options.DisableNotifications = false
119
120 // Focus support disabled, but "focused" is true (simulate default state where we assume focused but can't verify, or just focus tracking disabled)
121 // The logic says: "Do NOT send if focus reporting is not supported."
122 notification.SetFocusSupport(false)
123 notification.SetFocused(true)
124
125 // Mock the notify function
126 called := false
127 mockNotify := func(title, message string, icon any) error {
128 called = true
129 return nil
130 }
131 notification.SetNotifyFunc(mockNotify)
132
133 // Call Send
134 err = notification.Send("Title", "Message")
135 require.NoError(t, err)
136 require.False(t, called, "Should NOT send notification if focus support is disabled")
137}
138
139func TestSend_Error(t *testing.T) {
140 // Pre-initialize logger to os.DevNull to prevent file lock on Windows.
141 log.Setup(os.DevNull, false)
142
143 // Reset globals after test
144 defer func() {
145 notification.SetFocusSupport(false)
146 notification.SetFocused(true)
147 notification.ResetNotifyFunc()
148 }()
149
150 // Setup a temporary config with DisableNotifications = false
151 tempDir := t.TempDir()
152 cfg, err := config.Init(tempDir, tempDir, false)
153 require.NoError(t, err)
154
155 cfg.Options.DisableNotifications = false
156
157 // Ensure we try to send
158 notification.SetFocusSupport(true)
159 notification.SetFocused(false)
160
161 // Mock error
162 expectedErr := fmt.Errorf("mock error")
163 mockNotify := func(title, message string, icon any) error {
164 return expectedErr
165 }
166 notification.SetNotifyFunc(mockNotify)
167
168 // Call Send
169 err = notification.Send("Title", "Message")
170 require.Equal(t, expectedErr, err)
171}