| 1 |
package util |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
|
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 10 |
) |
| 11 |
|
| 12 |
func TestPartitionMessage(t *testing.T) { |
| 13 |
t.Parallel() |
| 14 |
|
| 15 |
tests := []struct { |
| 16 |
name string |
| 17 |
input string |
| 18 |
limits types.MessageLimit |
| 19 |
distance int |
| 20 |
wantItems int |
| 21 |
wantOmitted int |
| 22 |
wantFirstText string |
| 23 |
}{ |
| 24 |
{ |
| 25 |
name: "partitions empty string", |
| 26 |
input: "", |
| 27 |
limits: types.MessageLimit{ |
| 28 |
ChunkSize: 100, |
| 29 |
TotalChunkSize: 300, |
| 30 |
ChunkCount: 3, |
| 31 |
}, |
| 32 |
distance: 10, |
| 33 |
wantItems: 0, |
| 34 |
wantOmitted: 0, |
| 35 |
wantFirstText: "", |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "partitions short message without splitting", |
| 39 |
input: "Hello World", |
| 40 |
limits: types.MessageLimit{ |
| 41 |
ChunkSize: 100, |
| 42 |
TotalChunkSize: 300, |
| 43 |
ChunkCount: 3, |
| 44 |
}, |
| 45 |
distance: 10, |
| 46 |
wantItems: 1, |
| 47 |
wantOmitted: 0, |
| 48 |
wantFirstText: "Hello World", |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "partitions message at whitespace", |
| 52 |
input: "Hello World this is a test message", |
| 53 |
limits: types.MessageLimit{ |
| 54 |
ChunkSize: 15, |
| 55 |
TotalChunkSize: 100, |
| 56 |
ChunkCount: 5, |
| 57 |
}, |
| 58 |
distance: 5, |
| 59 |
wantItems: 3, |
| 60 |
wantOmitted: 0, |
| 61 |
wantFirstText: "Hello World", |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "handles message without whitespace", |
| 65 |
input: "ABCDEFGHIJ", |
| 66 |
limits: types.MessageLimit{ |
| 67 |
ChunkSize: 5, |
| 68 |
TotalChunkSize: 100, |
| 69 |
ChunkCount: 5, |
| 70 |
}, |
| 71 |
distance: 3, |
| 72 |
wantItems: 2, |
| 73 |
wantOmitted: 0, |
| 74 |
wantFirstText: "ABCDE", |
| 75 |
}, |
| 76 |
} |
| 77 |
|
| 78 |
for _, tt := range tests { |
| 79 |
t.Run(tt.name, func(t *testing.T) { |
| 80 |
t.Parallel() |
| 81 |
|
| 82 |
got, gotOmitted := PartitionMessage(tt.input, tt.limits, tt.distance) |
| 83 |
|
| 84 |
assert.Len(t, got, tt.wantItems, "Number of items mismatch") |
| 85 |
assert.Equal(t, tt.wantOmitted, gotOmitted, "Omitted count mismatch") |
| 86 |
|
| 87 |
if tt.wantItems > 0 && tt.wantFirstText != "" { |
| 88 |
assert.Equal(t, tt.wantFirstText, got[0].Text, "First item text mismatch") |
| 89 |
} |
| 90 |
}) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
//nolint:gosmopolitan // Unicode characters used intentionally for testing rune handling |
| 95 |
func TestEllipsis(t *testing.T) { |
| 96 |
t.Parallel() |
| 97 |
|
| 98 |
tests := []struct { |
| 99 |
name string |
| 100 |
text string |
| 101 |
maxLength int |
| 102 |
want string |
| 103 |
}{ |
| 104 |
{ |
| 105 |
name: "returns short text unchanged", |
| 106 |
text: "Hello", |
| 107 |
maxLength: 10, |
| 108 |
want: "Hello", |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "truncates long text with ellipsis", |
| 112 |
text: "Hello World", |
| 113 |
maxLength: 10, |
| 114 |
want: "Hell [...]", |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "handles exact length", |
| 118 |
text: "HelloWorld", |
| 119 |
maxLength: 10, |
| 120 |
want: "HelloWorld", |
| 121 |
}, |
| 122 |
{ |
| 123 |
name: "handles empty string", |
| 124 |
text: "", |
| 125 |
maxLength: 10, |
| 126 |
want: "", |
| 127 |
}, |
| 128 |
{ |
| 129 |
name: "handles unicode - counts bytes not runes", |
| 130 |
text: "Hello世界", |
| 131 |
maxLength: 8, |
| 132 |
want: "He [...]", |
| 133 |
}, |
| 134 |
{ |
| 135 |
name: "returns text unchanged when len equals maxLength", |
| 136 |
text: "Hello", |
| 137 |
maxLength: 5, |
| 138 |
want: "Hello", |
| 139 |
}, |
| 140 |
} |
| 141 |
|
| 142 |
for _, tt := range tests { |
| 143 |
t.Run(tt.name, func(t *testing.T) { |
| 144 |
t.Parallel() |
| 145 |
|
| 146 |
got := Ellipsis(tt.text, tt.maxLength) |
| 147 |
assert.Equal(t, tt.want, got, "Ellipsis(%q, %d) result mismatch", tt.text, tt.maxLength) |
| 148 |
}) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
//nolint:gosmopolitan // Unicode characters used intentionally for testing rune handling |
| 153 |
func TestMessageItemsFromLines(t *testing.T) { |
| 154 |
t.Parallel() |
| 155 |
|
| 156 |
tests := []struct { |
| 157 |
name string |
| 158 |
plain string |
| 159 |
limits types.MessageLimit |
| 160 |
wantBatches int |
| 161 |
wantFirstBatchLen int |
| 162 |
}{ |
| 163 |
{ |
| 164 |
name: "returns empty for empty input", |
| 165 |
plain: "", |
| 166 |
limits: types.MessageLimit{ |
| 167 |
ChunkSize: 100, |
| 168 |
TotalChunkSize: 300, |
| 169 |
ChunkCount: 3, |
| 170 |
}, |
| 171 |
wantBatches: 0, |
| 172 |
wantFirstBatchLen: 0, |
| 173 |
}, |
| 174 |
{ |
| 175 |
name: "creates single batch for single line", |
| 176 |
plain: "Hello World", |
| 177 |
limits: types.MessageLimit{ |
| 178 |
ChunkSize: 100, |
| 179 |
TotalChunkSize: 300, |
| 180 |
ChunkCount: 3, |
| 181 |
}, |
| 182 |
wantBatches: 1, |
| 183 |
wantFirstBatchLen: 1, |
| 184 |
}, |
| 185 |
{ |
| 186 |
name: "splits multiple lines into items", |
| 187 |
plain: "Line1\nLine2\nLine3", |
| 188 |
limits: types.MessageLimit{ |
| 189 |
ChunkSize: 100, |
| 190 |
TotalChunkSize: 300, |
| 191 |
ChunkCount: 5, |
| 192 |
}, |
| 193 |
wantBatches: 1, |
| 194 |
wantFirstBatchLen: 3, |
| 195 |
}, |
| 196 |
{ |
| 197 |
name: "respects chunk count limit", |
| 198 |
plain: "Line1\nLine2\nLine3\nLine4", |
| 199 |
limits: types.MessageLimit{ |
| 200 |
ChunkSize: 100, |
| 201 |
TotalChunkSize: 300, |
| 202 |
ChunkCount: 2, |
| 203 |
}, |
| 204 |
wantBatches: 2, |
| 205 |
wantFirstBatchLen: 2, |
| 206 |
}, |
| 207 |
{ |
| 208 |
name: "truncates long lines", |
| 209 |
plain: strings.Repeat("A", 200), |
| 210 |
limits: types.MessageLimit{ |
| 211 |
ChunkSize: 50, |
| 212 |
TotalChunkSize: 300, |
| 213 |
ChunkCount: 5, |
| 214 |
}, |
| 215 |
wantBatches: 1, |
| 216 |
wantFirstBatchLen: 1, |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "skips empty lines", |
| 220 |
plain: "Line1\n\nLine2", |
| 221 |
limits: types.MessageLimit{ |
| 222 |
ChunkSize: 100, |
| 223 |
TotalChunkSize: 300, |
| 224 |
ChunkCount: 5, |
| 225 |
}, |
| 226 |
wantBatches: 1, |
| 227 |
wantFirstBatchLen: 2, |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "handles unicode lines", |
| 231 |
plain: "Hello\n世界\nTest", |
| 232 |
limits: types.MessageLimit{ |
| 233 |
ChunkSize: 100, |
| 234 |
TotalChunkSize: 300, |
| 235 |
ChunkCount: 5, |
| 236 |
}, |
| 237 |
wantBatches: 1, |
| 238 |
wantFirstBatchLen: 3, |
| 239 |
}, |
| 240 |
} |
| 241 |
|
| 242 |
for _, tt := range tests { |
| 243 |
t.Run(tt.name, func(t *testing.T) { |
| 244 |
t.Parallel() |
| 245 |
|
| 246 |
got := MessageItemsFromLines(tt.plain, tt.limits) |
| 247 |
|
| 248 |
assert.Len(t, got, tt.wantBatches, "Number of batches mismatch") |
| 249 |
|
| 250 |
if tt.wantBatches > 0 && tt.wantFirstBatchLen > 0 { |
| 251 |
assert.Len(t, got[0], tt.wantFirstBatchLen, "First batch length mismatch") |
| 252 |
} |
| 253 |
}) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
func TestMessageItemsFromLines_Truncation(t *testing.T) { |
| 258 |
t.Parallel() |
| 259 |
|
| 260 |
// Test that long lines are properly truncated with ellipsis |
| 261 |
t.Run("truncates long lines with ellipsis", func(t *testing.T) { |
| 262 |
t.Parallel() |
| 263 |
|
| 264 |
limits := types.MessageLimit{ |
| 265 |
ChunkSize: 20, |
| 266 |
TotalChunkSize: 100, |
| 267 |
ChunkCount: 5, |
| 268 |
} |
| 269 |
|
| 270 |
longLine := strings.Repeat("A", 50) |
| 271 |
batches := MessageItemsFromLines(longLine, limits) |
| 272 |
|
| 273 |
assert.Len(t, batches, 1) |
| 274 |
assert.Len(t, batches[0], 1) |
| 275 |
|
| 276 |
// Should be truncated to chunk size with ellipsis |
| 277 |
assert.Len(t, batches[0][0].Text, 20) |
| 278 |
assert.True(t, strings.HasSuffix(batches[0][0].Text, " [...]"), "Truncated text should end with ellipsis") |
| 279 |
}) |
| 280 |
} |