agi.notification_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package agi
  2. import (
  3. "errors"
  4. "testing"
  5. notification "imuslab.com/arozos/mod/notification"
  6. )
  7. // newNotificationTestGateway builds a minimal Gateway whose NotificationSender
  8. // captures the last payload it received.
  9. func newNotificationTestGateway(sender func(*notification.NotificationPayload) error) *Gateway {
  10. return &Gateway{
  11. Option: &AgiSysInfo{
  12. NotificationSender: sender,
  13. },
  14. }
  15. }
  16. func TestBuildAndSendNotification_NoSender(t *testing.T) {
  17. g := newNotificationTestGateway(nil)
  18. err := g.buildAndSendNotification("mod", []string{"alice"}, "Title", "Body", "high")
  19. if err == nil {
  20. t.Error("expected error when no notification sender is configured")
  21. }
  22. }
  23. func TestBuildAndSendNotification_EmptyTitle(t *testing.T) {
  24. g := newNotificationTestGateway(func(p *notification.NotificationPayload) error { return nil })
  25. err := g.buildAndSendNotification("mod", []string{"alice"}, "", "Body", "high")
  26. if err == nil {
  27. t.Error("expected error for empty title")
  28. }
  29. }
  30. func TestBuildAndSendNotification_Success(t *testing.T) {
  31. var captured *notification.NotificationPayload
  32. g := newNotificationTestGateway(func(p *notification.NotificationPayload) error {
  33. captured = p
  34. return nil
  35. })
  36. err := g.buildAndSendNotification("Backup", []string{"alice", "bob"}, "Done", "Backup finished", "high")
  37. if err != nil {
  38. t.Fatalf("unexpected error: %v", err)
  39. }
  40. if captured == nil {
  41. t.Fatal("expected sender to be called")
  42. }
  43. if captured.Title != "Done" || captured.Message != "Backup finished" {
  44. t.Errorf("unexpected title/message: %+v", captured)
  45. }
  46. if captured.Sender != "Backup" {
  47. t.Errorf("unexpected sender: %s", captured.Sender)
  48. }
  49. if captured.Priority != notification.PriorityHigh {
  50. t.Errorf("expected high priority, got %d", captured.Priority)
  51. }
  52. if len(captured.Receiver) != 2 {
  53. t.Errorf("expected 2 receivers, got %d", len(captured.Receiver))
  54. }
  55. if captured.ID == "" {
  56. t.Error("expected a generated notification ID")
  57. }
  58. if captured.Timestamp == 0 {
  59. t.Error("expected a non-zero timestamp")
  60. }
  61. }
  62. func TestBuildAndSendNotification_DefaultPriority(t *testing.T) {
  63. var captured *notification.NotificationPayload
  64. g := newNotificationTestGateway(func(p *notification.NotificationPayload) error {
  65. captured = p
  66. return nil
  67. })
  68. //An unknown priority string should fall back to medium.
  69. if err := g.buildAndSendNotification("mod", []string{"x"}, "T", "M", "bogus"); err != nil {
  70. t.Fatalf("unexpected error: %v", err)
  71. }
  72. if captured.Priority != notification.PriorityMedium {
  73. t.Errorf("expected medium priority fallback, got %d", captured.Priority)
  74. }
  75. }
  76. func TestBuildAndSendNotification_SenderErrorPropagates(t *testing.T) {
  77. g := newNotificationTestGateway(func(p *notification.NotificationPayload) error {
  78. return errors.New("delivery failed")
  79. })
  80. err := g.buildAndSendNotification("mod", []string{"x"}, "T", "M", "low")
  81. if err == nil {
  82. t.Error("expected sender error to propagate")
  83. }
  84. }