|
| 1 | +package notification |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestContextNotifier(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + terminalTitle string |
| 11 | + notification Notification |
| 12 | + expectedTitle string |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "adds cwd and terminal title", |
| 16 | + terminalTitle: "My Project", |
| 17 | + notification: Notification{ |
| 18 | + Title: "Test Notification", |
| 19 | + Message: "Test message", |
| 20 | + }, |
| 21 | + expectedTitle: "Claude Code: claude-code-ntfy - My Project", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "removes claude icon from title", |
| 25 | + terminalTitle: "✅ claude", |
| 26 | + notification: Notification{ |
| 27 | + Title: "Test Notification", |
| 28 | + Message: "Test message", |
| 29 | + }, |
| 30 | + expectedTitle: "Claude Code: claude-code-ntfy", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "handles empty terminal title", |
| 34 | + terminalTitle: "", |
| 35 | + notification: Notification{ |
| 36 | + Title: "Test Notification", |
| 37 | + Message: "Test message", |
| 38 | + }, |
| 39 | + expectedTitle: "Claude Code: claude-code-ntfy", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "ignores plain claude title", |
| 43 | + terminalTitle: "claude", |
| 44 | + notification: Notification{ |
| 45 | + Title: "Test Notification", |
| 46 | + Message: "Test message", |
| 47 | + }, |
| 48 | + expectedTitle: "Claude Code: claude-code-ntfy", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "removes various claude icons", |
| 52 | + terminalTitle: "🤖 My Terminal", |
| 53 | + notification: Notification{ |
| 54 | + Title: "Test Notification", |
| 55 | + Message: "Test message", |
| 56 | + }, |
| 57 | + expectedTitle: "Claude Code: claude-code-ntfy - My Terminal", |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + // Create a mock notifier to capture the sent notification |
| 64 | + var sentNotification Notification |
| 65 | + mockNotifier := &mockNotifier{ |
| 66 | + sendFunc: func(n Notification) error { |
| 67 | + sentNotification = n |
| 68 | + return nil |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + // Create context notifier |
| 73 | + cn := NewContextNotifier(mockNotifier, func() string { |
| 74 | + return tt.terminalTitle |
| 75 | + }) |
| 76 | + |
| 77 | + // The CWD basename will be "claude-code-ntfy" in tests |
| 78 | + // (based on the current directory structure) |
| 79 | + |
| 80 | + // Send notification |
| 81 | + err := cn.Send(tt.notification) |
| 82 | + if err != nil { |
| 83 | + t.Errorf("unexpected error: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + // Check if title was modified correctly |
| 87 | + // The title should start with "Claude Code:" |
| 88 | + if len(sentNotification.Title) < 12 || sentNotification.Title[:12] != "Claude Code:" { |
| 89 | + t.Errorf("expected title to start with 'Claude Code:', got %q", sentNotification.Title) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestCleanTerminalTitle(t *testing.T) { |
| 96 | + cn := &ContextNotifier{} |
| 97 | + |
| 98 | + tests := []struct { |
| 99 | + name string |
| 100 | + input string |
| 101 | + expected string |
| 102 | + }{ |
| 103 | + { |
| 104 | + name: "removes checkmark icon", |
| 105 | + input: "✅ claude", |
| 106 | + expected: "claude", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "removes robot emoji", |
| 110 | + input: "🤖 My Project", |
| 111 | + expected: "My Project", |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "removes lightning bolt", |
| 115 | + input: "⚡ Terminal", |
| 116 | + expected: "Terminal", |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "handles no icon", |
| 120 | + input: "Plain Title", |
| 121 | + expected: "Plain Title", |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "removes generic unicode at start", |
| 125 | + input: "🎯 Something", |
| 126 | + expected: "Something", |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "preserves unicode elsewhere", |
| 130 | + input: "Title with 🎯 emoji", |
| 131 | + expected: "Title with 🎯 emoji", |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "removes garbage characters", |
| 135 | + input: "✳ Test Coverage 1", |
| 136 | + expected: "Test Coverage 1", |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "handles mixed garbage and valid", |
| 140 | + input: "∂ß婃∆ My Project", |
| 141 | + expected: "My Project", |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "preserves brackets and numbers", |
| 145 | + input: "[1] Development Server", |
| 146 | + expected: "[1] Development Server", |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + for _, tt := range tests { |
| 151 | + t.Run(tt.name, func(t *testing.T) { |
| 152 | + result := cn.cleanTerminalTitle(tt.input) |
| 153 | + if result != tt.expected { |
| 154 | + t.Errorf("cleanTerminalTitle(%q) = %q, want %q", tt.input, result, tt.expected) |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// mockNotifier for testing |
| 161 | +type mockNotifier struct { |
| 162 | + sendFunc func(Notification) error |
| 163 | +} |
| 164 | + |
| 165 | +func (m *mockNotifier) Send(n Notification) error { |
| 166 | + if m.sendFunc != nil { |
| 167 | + return m.sendFunc(n) |
| 168 | + } |
| 169 | + return nil |
| 170 | +} |
| 171 | + |
0 commit comments