Debian Codegraph @

pkg/util/message_test.go

raw symbols SCIP
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 func TestEllipsis(t *testing.T) {
95 t.Parallel()
96
97 tests := []struct {
98 name string
99 text string
100 maxLength int
101 want string
102 }{
103 {
104 name: "returns short text unchanged",
105 text: "Hello",
106 maxLength: 10,
107 want: "Hello",
108 },
109 {
110 name: "truncates long text with ellipsis",
111 text: "Hello World",
112 maxLength: 10,
113 want: "Hell [...]",
114 },
115 {
116 name: "handles exact length",
117 text: "HelloWorld",
118 maxLength: 10,
119 want: "HelloWorld",
120 },
121 {
122 name: "handles empty string",
123 text: "",
124 maxLength: 10,
125 want: "",
126 },
127 {
128 name: "handles unicode - counts bytes not runes",
129 text: "Hello世界",
130 maxLength: 8,
131 want: "He [...]",
132 },
133 {
134 name: "returns text unchanged when len equals maxLength",
135 text: "Hello",
136 maxLength: 5,
137 want: "Hello",
138 },
139 }
140
141 for _, tt := range tests {
142 t.Run(tt.name, func(t *testing.T) {
143 t.Parallel()
144
145 got := Ellipsis(tt.text, tt.maxLength)
146 assert.Equal(t, tt.want, got, "Ellipsis(%q, %d) result mismatch", tt.text, tt.maxLength)
147 })
148 }
149 }
150
151 func TestMessageItemsFromLines(t *testing.T) {
152 t.Parallel()
153
154 tests := []struct {
155 name string
156 plain string
157 limits types.MessageLimit
158 wantBatches int
159 wantFirstBatchLen int
160 }{
161 {
162 name: "returns empty for empty input",
163 plain: "",
164 limits: types.MessageLimit{
165 ChunkSize: 100,
166 TotalChunkSize: 300,
167 ChunkCount: 3,
168 },
169 wantBatches: 0,
170 wantFirstBatchLen: 0,
171 },
172 {
173 name: "creates single batch for single line",
174 plain: "Hello World",
175 limits: types.MessageLimit{
176 ChunkSize: 100,
177 TotalChunkSize: 300,
178 ChunkCount: 3,
179 },
180 wantBatches: 1,
181 wantFirstBatchLen: 1,
182 },
183 {
184 name: "splits multiple lines into items",
185 plain: "Line1\nLine2\nLine3",
186 limits: types.MessageLimit{
187 ChunkSize: 100,
188 TotalChunkSize: 300,
189 ChunkCount: 5,
190 },
191 wantBatches: 1,
192 wantFirstBatchLen: 3,
193 },
194 {
195 name: "respects chunk count limit",
196 plain: "Line1\nLine2\nLine3\nLine4",
197 limits: types.MessageLimit{
198 ChunkSize: 100,
199 TotalChunkSize: 300,
200 ChunkCount: 2,
201 },
202 wantBatches: 2,
203 wantFirstBatchLen: 2,
204 },
205 {
206 name: "truncates long lines",
207 plain: strings.Repeat("A", 200),
208 limits: types.MessageLimit{
209 ChunkSize: 50,
210 TotalChunkSize: 300,
211 ChunkCount: 5,
212 },
213 wantBatches: 1,
214 wantFirstBatchLen: 1,
215 },
216 {
217 name: "skips empty lines",
218 plain: "Line1\n\nLine2",
219 limits: types.MessageLimit{
220 ChunkSize: 100,
221 TotalChunkSize: 300,
222 ChunkCount: 5,
223 },
224 wantBatches: 1,
225 wantFirstBatchLen: 2,
226 },
227 {
228 name: "handles unicode lines",
229 plain: "Hello\n世界\nTest",
230 limits: types.MessageLimit{
231 ChunkSize: 100,
232 TotalChunkSize: 300,
233 ChunkCount: 5,
234 },
235 wantBatches: 1,
236 wantFirstBatchLen: 3,
237 },
238 }
239
240 for _, tt := range tests {
241 t.Run(tt.name, func(t *testing.T) {
242 t.Parallel()
243
244 got := MessageItemsFromLines(tt.plain, tt.limits)
245
246 assert.Len(t, got, tt.wantBatches, "Number of batches mismatch")
247
248 if tt.wantBatches > 0 && tt.wantFirstBatchLen > 0 {
249 assert.Len(t, got[0], tt.wantFirstBatchLen, "First batch length mismatch")
250 }
251 })
252 }
253 }
254
255 func TestMessageItemsFromLines_Truncation(t *testing.T) {
256 t.Parallel()
257
258 // Test that long lines are properly truncated with ellipsis
259 t.Run("truncates long lines with ellipsis", func(t *testing.T) {
260 t.Parallel()
261
262 limits := types.MessageLimit{
263 ChunkSize: 20,
264 TotalChunkSize: 100,
265 ChunkCount: 5,
266 }
267
268 longLine := strings.Repeat("A", 50)
269 batches := MessageItemsFromLines(longLine, limits)
270
271 assert.Len(t, batches, 1)
272 assert.Len(t, batches[0], 1)
273
274 // Should be truncated to chunk size with ellipsis
275 assert.Len(t, batches[0][0].Text, 20)
276 assert.True(t, strings.HasSuffix(batches[0][0].Text, " [...]"), "Truncated text should end with ellipsis")
277 })
278 }