package services import ( "fmt" "testing" "github.com/Wikid82/CaddyProxyManagerPlus/backend/internal/models" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gorm.io/driver/sqlite" "gorm.io/gorm" ) func setupNotificationTestDB(t *testing.T) *gorm.DB { db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{}) require.NoError(t, err) db.AutoMigrate(&models.Notification{}) return db } func TestNotificationService_Create(t *testing.T) { db := setupNotificationTestDB(t) svc := NewNotificationService(db) notif, err := svc.Create(models.NotificationTypeInfo, "Test", "Message") require.NoError(t, err) assert.Equal(t, "Test", notif.Title) assert.Equal(t, "Message", notif.Message) assert.False(t, notif.Read) } func TestNotificationService_List(t *testing.T) { db := setupNotificationTestDB(t) svc := NewNotificationService(db) svc.Create(models.NotificationTypeInfo, "N1", "M1") svc.Create(models.NotificationTypeInfo, "N2", "M2") list, err := svc.List(false) require.NoError(t, err) assert.Len(t, list, 2) // Mark one as read db.Model(&models.Notification{}).Where("title = ?", "N1").Update("read", true) listUnread, err := svc.List(true) require.NoError(t, err) assert.Len(t, listUnread, 1) assert.Equal(t, "N2", listUnread[0].Title) } func TestNotificationService_MarkAsRead(t *testing.T) { db := setupNotificationTestDB(t) svc := NewNotificationService(db) notif, _ := svc.Create(models.NotificationTypeInfo, "N1", "M1") err := svc.MarkAsRead(fmt.Sprintf("%s", notif.ID)) require.NoError(t, err) var updated models.Notification db.First(&updated, "id = ?", notif.ID) assert.True(t, updated.Read) } func TestNotificationService_MarkAllAsRead(t *testing.T) { db := setupNotificationTestDB(t) svc := NewNotificationService(db) svc.Create(models.NotificationTypeInfo, "N1", "M1") svc.Create(models.NotificationTypeInfo, "N2", "M2") err := svc.MarkAllAsRead() require.NoError(t, err) var count int64 db.Model(&models.Notification{}).Where("read = ?", false).Count(&count) assert.Equal(t, int64(0), count) }