notification_test.go

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