Debian Codegraph @

pkg/util/message_test.go

raw symbols SCIP
1 package utilscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/
2
3 import (
4 "stringsscip-go gomod github.com/golang/go/src . strings/"
5 "testingscip-go gomod github.com/golang/go/src . testing/"
6
7 "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/"
8
9 "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/"
10 )
11
12 func TestPartitionMessagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/TestPartitionMessage().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
13 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
14
15 tests := []struct {
16 name string
17 input string
18 limits typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/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: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
28 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 100,
29 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 300,
30 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#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: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
41 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 100,
42 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 300,
43 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#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: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
54 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 15,
55 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 100,
56 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#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: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
67 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 5,
68 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 100,
69 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#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.Runscip-go gomod github.com/golang/go/src . testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
80 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
81
82 got, gotOmitted := PartitionMessagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/PartitionMessage().(tt.input, tt.limits, tt.distance)
83
84 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Lenscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/Len().(t, got, tt.wantItems, "Number of items mismatch")
85 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.wantOmitted, gotOmitted, "Omitted count mismatch")
86
87 if tt.wantItems > 0 && tt.wantFirstText != "" {
88 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.wantFirstText, got[0].Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageItem#Text., "First item text mismatch")
89 }
90 })
91 }
92 }
93
94 func TestEllipsisscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/TestEllipsis().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
95 t.Parallelscip-go gomod github.com/golang/go/src . testing/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.Runscip-go gomod github.com/golang/go/src . testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
143 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
144
145 got := Ellipsisscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/Ellipsis().(tt.text, tt.maxLength)
146 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.want, got, "Ellipsis(%q, %d) result mismatch", tt.text, tt.maxLength)
147 })
148 }
149 }
150
151 func TestMessageItemsFromLinesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/TestMessageItemsFromLines().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
152 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
153
154 tests := []struct {
155 name string
156 plain string
157 limits typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#
158 wantBatches int
159 wantFirstBatchLen int
160 }{
161 {
162 name: "returns empty for empty input",
163 plain: "",
164 limits: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
165 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 100,
166 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 300,
167 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#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: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
176 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 100,
177 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 300,
178 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#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: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
187 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 100,
188 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 300,
189 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#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: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
198 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 100,
199 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 300,
200 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkCount.: 2,
201 },
202 wantBatches: 2,
203 wantFirstBatchLen: 2,
204 },
205 {
206 name: "truncates long lines",
207 plain: stringsscip-go gomod github.com/golang/go/src . strings/.Repeatscip-go gomod github.com/golang/go/src . strings/Repeat().("A", 200),
208 limits: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
209 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 50,
210 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 300,
211 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkCount.: 5,
212 },
213 wantBatches: 1,
214 wantFirstBatchLen: 1,
215 },
216 {
217 name: "skips empty lines",
218 plain: "Line1\n\nLine2",
219 limits: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
220 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 100,
221 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 300,
222 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkCount.: 5,
223 },
224 wantBatches: 1,
225 wantFirstBatchLen: 2,
226 },
227 {
228 name: "handles unicode lines",
229 plain: "Hello\n世界\nTest",
230 limits: typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
231 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 100,
232 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 300,
233 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkCount.: 5,
234 },
235 wantBatches: 1,
236 wantFirstBatchLen: 3,
237 },
238 }
239
240 for _, tt := range tests {
241 t.Runscip-go gomod github.com/golang/go/src . testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
242 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
243
244 got := MessageItemsFromLinesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/MessageItemsFromLines().(tt.plain, tt.limits)
245
246 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Lenscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/Len().(t, got, tt.wantBatches, "Number of batches mismatch")
247
248 if tt.wantBatches > 0 && tt.wantFirstBatchLen > 0 {
249 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Lenscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/Len().(t, got[0], tt.wantFirstBatchLen, "First batch length mismatch")
250 }
251 })
252 }
253 }
254
255 func TestMessageItemsFromLines_Truncationscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/TestMessageItemsFromLines_Truncation().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
256 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
257
258 // Test that long lines are properly truncated with ellipsis
259 t.Runscip-go gomod github.com/golang/go/src . testing/T#Run().("truncates long lines with ellipsis", func(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) {
260 t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().()
261
262 limits := typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.MessageLimitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#{
263 ChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkSize.: 20,
264 TotalChunkSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#TotalChunkSize.: 100,
265 ChunkCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageLimit#ChunkCount.: 5,
266 }
267
268 longLine := stringsscip-go gomod github.com/golang/go/src . strings/.Repeatscip-go gomod github.com/golang/go/src . strings/Repeat().("A", 50)
269 batches := MessageItemsFromLinesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/MessageItemsFromLines().(longLine, limits)
270
271 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Lenscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/Len().(t, batches, 1)
272 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Lenscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/Len().(t, batches[0], 1)
273
274 // Should be truncated to chunk size with ellipsis
275 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Lenscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/Len().(t, batches[0][0].Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageItem#Text., 20)
276 assertscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/.Truescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/assert`/True().(t, stringsscip-go gomod github.com/golang/go/src . strings/.HasSuffixscip-go gomod github.com/golang/go/src . strings/HasSuffix().(batches[0][0].Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/MessageItem#Text., " [...]"), "Truncated text should end with ellipsis")
277 })
278 }