Debian Codegraph @

core/custom_test.go

raw
1 package core
2
3 import (
4 "fmt"
5 "os"
6 "strings"
7 "testing"
8
9 "github.com/cqroot/prompt/choose"
10
11 "github.com/miniscruff/changie/then"
12 )
13
14 type ValidationSpecs struct {
15 Name string
16 Custom Custom
17 Specs []ValidationSpec
18 }
19
20 func (s *ValidationSpecs) Run(t *testing.T) {
21 for _, spec := range s.Specs {
22 t.Run(s.Name+spec.Name, func(t *testing.T) {
23 spec.Run(t, s.Custom)
24 })
25 }
26 }
27
28 type ValidationSpec struct {
29 Name string
30 Input string
31 Error error
32 }
33
34 func (s *ValidationSpec) Run(t *testing.T, custom Custom) {
35 err := custom.Validate(s.Input)
36 if s.Error == nil {
37 then.Nil(t, err)
38 } else {
39 then.Err(t, s.Error, err)
40 }
41 }
42
43 func PtrInt64(value int64) *int64 {
44 return &value
45 }
46
47 func TestDisplayLabelDefaultsToKey(t *testing.T) {
48 custom := Custom{Key: "key"}
49 then.Equals(t, "key", custom.DisplayLabel())
50 }
51
52 func TestDisplayLabelUsesLabelIfDefined(t *testing.T) {
53 custom := Custom{Key: "key", Label: "Lab"}
54 then.Equals(t, "Lab", custom.DisplayLabel())
55 }
56
57 func TestErrorInvalidPromptType(t *testing.T) {
58 custom := Custom{Type: "invalid-type"}
59 _, err := custom.AskPrompt(os.Stdin)
60 then.Err(t, errInvalidPromptType, err)
61 }
62
63 func TestErrorInvalidPromptTypeWhenValidating(t *testing.T) {
64 custom := Custom{Type: "invalid-type"}
65 err := custom.Validate("ignored")
66 then.Err(t, errInvalidPromptType, err)
67 }
68
69 func TestCreateCustomIntPrompt(t *testing.T) {
70 reader, writer := then.WithReadWritePipe(t)
71 then.DelayWrite(
72 t, writer,
73 []byte("15"),
74 []byte{13}, // 13=enter
75 )
76
77 custom := Custom{Type: CustomInt, Key: "name"}
78 value, err := custom.AskPrompt(reader)
79
80 then.Nil(t, err)
81 then.Equals(t, value, "15")
82 }
83
84 func TestCanRunBlockPrompt(t *testing.T) {
85 reader, writer := then.WithReadWritePipe(t)
86 then.DelayWrite(
87 t, writer,
88 []byte("I can write multiple lines"),
89 []byte{13},
90 []byte("In a block prompt"),
91 []byte{4}, // 4=EOT or ctrl+d
92 )
93
94 custom := Custom{Type: CustomBlock, Key: "block"}
95 value, err := custom.AskPrompt(reader)
96
97 then.Nil(t, err)
98 then.Equals(t, value, "I can write multiple lines\nIn a block prompt")
99 }
100
101 func TestCanRunEnumPrompt(t *testing.T) {
102 reader, writer := then.WithReadWritePipe(t)
103 then.DelayWrite(
104 t, writer,
105 []byte{106, 13}, // 106 = down, 13 = enter
106 )
107
108 opts := []string{"a", "b", "c"}
109 custom := Custom{Type: CustomEnum, EnumOptions: opts}
110
111 value, err := custom.AskPrompt(reader)
112 then.Nil(t, err)
113 then.Equals(t, "b", value)
114 }
115
116 func TestCanRunEnumsPrompt(t *testing.T) {
117 reader, writer := then.WithReadWritePipe(t)
118 then.DelayWrite(
119 t, writer,
120 []byte{32, 106, 32, 13}, // 32=space, 106=down, 32=space, 13=enter
121 )
122
123 opts := []string{"a", "b", "c"}
124 custom := Custom{Type: CustomEnums, EnumOptions: opts}
125
126 value, err := custom.AskPrompt(reader)
127 then.Nil(t, err)
128 then.Equals(t, "a, b", value)
129 }
130
131 var validationSpecs = []ValidationSpecs{
132 {
133 Name: "String",
134 Custom: Custom{
135 Type: CustomString,
136 Key: "string",
137 MinLength: PtrInt64(3),
138 MaxLength: PtrInt64(15),
139 },
140 Specs: []ValidationSpec{
141 {
142 Name: "Empty",
143 Input: "",
144 Error: errInputTooShort,
145 },
146 {
147 Name: "TooShort",
148 Input: "0",
149 Error: errInputTooShort,
150 },
151 {
152 Name: "TooLong",
153 Input: "01234567890123456789",
154 Error: errInputTooLong,
155 },
156 {
157 Name: "Valid",
158 Input: "normal",
159 },
160 },
161 },
162 {
163 Name: "OptionalString",
164 Custom: Custom{
165 Type: CustomString,
166 Label: "OptString",
167 Optional: true,
168 MinLength: PtrInt64(3),
169 MaxLength: PtrInt64(15),
170 },
171 Specs: []ValidationSpec{
172 {
173 Name: "Empty",
174 },
175 {
176 Name: "TooShort",
177 Input: "0",
178 Error: errInputTooShort,
179 },
180 {
181 Name: "TooLong",
182 Input: "01234567890123456789",
183 Error: errInputTooLong,
184 },
185 {
186 Name: "Valid",
187 Input: "normal",
188 },
189 },
190 },
191 {
192 Name: "Block",
193 Custom: Custom{
194 Type: CustomBlock,
195 Key: "string",
196 MinLength: PtrInt64(3),
197 MaxLength: PtrInt64(15),
198 },
199 Specs: []ValidationSpec{
200 {
201 Name: "Empty",
202 Input: "",
203 Error: errInputTooShort,
204 },
205 {
206 Name: "TooShort",
207 Input: "0",
208 Error: errInputTooShort,
209 },
210 {
211 Name: "TooLong",
212 Input: "01234567890123456789",
213 Error: errInputTooLong,
214 },
215 {
216 Name: "Valid",
217 Input: "normal",
218 },
219 },
220 },
221 {
222 Name: "OptionalBlock",
223 Custom: Custom{
224 Type: CustomBlock,
225 Label: "OptString",
226 Optional: true,
227 MinLength: PtrInt64(3),
228 MaxLength: PtrInt64(15),
229 },
230 Specs: []ValidationSpec{
231 {
232 Name: "Empty",
233 },
234 {
235 Name: "TooShort",
236 Input: "0",
237 Error: errInputTooShort,
238 },
239 {
240 Name: "TooLong",
241 Input: "01234567890123456789",
242 Error: errInputTooLong,
243 },
244 {
245 Name: "Valid",
246 Input: "normal",
247 },
248 },
249 },
250 {
251 Name: "Int",
252 Custom: Custom{
253 Type: CustomInt,
254 Label: "Int",
255 MinInt: PtrInt64(10),
256 MaxInt: PtrInt64(100),
257 },
258 Specs: []ValidationSpec{
259 {
260 Name: "Empty",
261 Input: "",
262 Error: errInvalidIntInput,
263 },
264 {
265 Name: "Text",
266 Input: "random text",
267 Error: errInvalidIntInput,
268 },
269 {
270 Name: "Float Value",
271 Input: "13.66",
272 Error: errInvalidIntInput,
273 },
274 {
275 Name: "TooLow",
276 Input: "5",
277 Error: errIntTooLow,
278 },
279 {
280 Name: "TooHigh",
281 Input: "500",
282 Error: errIntTooHigh,
283 },
284 {
285 Name: "Valid",
286 Input: "52",
287 },
288 },
289 },
290 {
291 Name: "OptionalInt",
292 Custom: Custom{
293 Type: CustomInt,
294 Label: "Int",
295 Optional: true,
296 MinInt: PtrInt64(10),
297 MaxInt: PtrInt64(100),
298 },
299 Specs: []ValidationSpec{
300 {
301 Name: "Empty",
302 Input: "",
303 },
304 {
305 Name: "Text",
306 Input: "random text",
307 Error: errInvalidIntInput,
308 },
309 {
310 Name: "Float Value",
311 Input: "13.66",
312 Error: errInvalidIntInput,
313 },
314 {
315 Name: "TooLow",
316 Input: "5",
317 Error: errIntTooLow,
318 },
319 {
320 Name: "TooHigh",
321 Input: "500",
322 Error: errIntTooHigh,
323 },
324 {
325 Name: "Valid",
326 Input: "52",
327 },
328 },
329 },
330 }
331
332 func TestValidators(t *testing.T) {
333 for _, specs := range validationSpecs {
334 specs.Run(t)
335 }
336 }
337
338 func TestValidEnum(t *testing.T) {
339 custom := Custom{
340 Type: CustomEnum,
341 EnumOptions: []string{"north", "south", "east", "west"},
342 }
343 err := custom.Validate("south")
344 then.Nil(t, err)
345 }
346
347 func TestInvalidEnum(t *testing.T) {
348 custom := Custom{
349 Type: CustomEnum,
350 EnumOptions: []string{"north", "south", "east", "west"},
351 }
352 err := custom.Validate("north-south")
353 then.Err(t, errInvalidEnum, err)
354 }
355
356 func TestValidEnums(t *testing.T) {
357 custom := Custom{
358 Type: CustomEnums,
359 EnumOptions: []string{"north", "south", "east", "west"},
360 }
361 err := custom.Validate("south, west")
362 then.Nil(t, err)
363 }
364
365 func TestInvalidEnums(t *testing.T) {
366 custom := Custom{
367 Type: CustomEnums,
368 EnumOptions: []string{"north", "south", "east", "west"},
369 }
370 err := custom.Validate("south, north-south")
371 then.Err(t, errInvalidEnum, err)
372 }
373
374 func TestOptionalEmptyEnums(t *testing.T) {
375 custom := Custom{
376 Type: CustomEnums,
377 Optional: true,
378 EnumOptions: []string{"north", "south", "east", "west"},
379 }
380 err := custom.Validate("")
381 then.Nil(t, err)
382 }
383
384 func TestCustomMapFromStrings(t *testing.T) {
385 expected := map[string]string{
386 "Issue": "15",
387 "Tag": "alpha",
388 "Owner": "team-name",
389 }
390
391 inputs := []string{"Issue=15", "Tag=alpha", "Owner=team-name"}
392 customValues, err := CustomMapFromStrings(inputs)
393 then.Nil(t, err)
394 then.MapEquals(t, expected, customValues)
395 }
396
397 func TestErrorBadMapFormat(t *testing.T) {
398 inputs := []string{"Issue=15", "Tag=alpha", "Ownerteam-name"}
399 _, err := CustomMapFromStrings(inputs)
400 then.Err(t, errInvalidCustomFormat, err)
401 }
402
403 func TestThemeScrollWithFewerThan10Choices(t *testing.T) {
404 choices := []choose.Choice{
405 {Text: "Option 1"},
406 {Text: "Option 2"},
407 {Text: "Option 3"},
408 }
409
410 result := ThemeScroll(choices, 0) // cursor at index 0
411
412 // Should show all 3 options with the second one selected
413 then.True(t, strings.Contains(result, "• Option 1"))
414 then.True(t, strings.Contains(result, " Option 2"))
415 then.True(t, strings.Contains(result, " Option 3"))
416 then.False(t, strings.Contains(result, "Option 4"))
417 }
418
419 func TestThemeScrollWithExactly10Choices(t *testing.T) {
420 choices := make([]choose.Choice, 10)
421 for i := range 10 {
422 choices[i] = choose.Choice{Text: fmt.Sprintf("Option %d", i+1)}
423 }
424
425 result := ThemeScroll(choices, 5) // cursor at middle
426
427 // Should show all 10 options with the 6th one selected
428 then.True(t, strings.Contains(result, " Option 1"))
429 then.True(t, strings.Contains(result, "• Option 6"))
430 then.True(t, strings.Contains(result, " Option 10"))
431 }
432
433 func TestThemeScrollWithMoreThan10ChoicesCursorAtStart(t *testing.T) {
434 choices := make([]choose.Choice, 15)
435 for i := range 15 {
436 choices[i] = choose.Choice{Text: fmt.Sprintf("Option %d", i+1)}
437 }
438
439 result := ThemeScroll(choices, 2) // cursor at index 2 (less than limit/2)
440
441 // Should show first 10 options with the 3rd one selected
442 then.True(t, strings.Contains(result, " Option 1"))
443 then.True(t, strings.Contains(result, "• Option 3"))
444 then.True(t, strings.Contains(result, " Option 10"))
445 then.False(t, strings.Contains(result, "Option 11"))
446 }
447
448 func TestThemeScrollWithMoreThan10ChoicesCursorInMiddle(t *testing.T) {
449 choices := make([]choose.Choice, 15)
450 for i := range 15 {
451 choices[i] = choose.Choice{Text: fmt.Sprintf("Option %d", i+1)}
452 }
453
454 result := ThemeScroll(choices, 7) // cursor at index 7 (in middle range)
455
456 // Should show options 3-12 with the 8th one selected (index 7 becomes index 5 in display)
457 then.True(t, strings.Contains(result, " Option 3"))
458 then.True(t, strings.Contains(result, "• Option 8"))
459 then.True(t, strings.Contains(result, " Option 12"))
460 // Verify the range is correct by checking that we have exactly 10 options
461 lines := strings.Split(strings.TrimSpace(result), "\n")
462 then.Equals(t, 10, len(lines))
463 }
464
465 func TestThemeScrollWithMoreThan10ChoicesCursorAtEnd(t *testing.T) {
466 choices := make([]choose.Choice, 15)
467 for i := range 15 {
468 choices[i] = choose.Choice{Text: fmt.Sprintf("Option %d", i+1)}
469 }
470
471 result := ThemeScroll(choices, 13) // cursor at index 13 (near end)
472
473 // Should show last 10 options with the 14th one selected
474 then.True(t, strings.Contains(result, " Option 6"))
475 then.True(t, strings.Contains(result, "• Option 14"))
476 then.True(t, strings.Contains(result, " Option 15"))
477 // Verify the range is correct by checking that we have exactly 10 options
478 lines := strings.Split(strings.TrimSpace(result), "\n")
479 then.SliceLen(t, 10, lines)
480 }
481
482 func TestThemeScrollWithMoreThan10ChoicesCursorAtVeryEnd(t *testing.T) {
483 choices := make([]choose.Choice, 15)
484 for i := range 15 {
485 choices[i] = choose.Choice{Text: fmt.Sprintf("Option %d", i+1)}
486 }
487
488 result := ThemeScroll(choices, 14) // cursor at last index
489
490 // Should show last 10 options with the 15th one selected
491 then.True(t, strings.Contains(result, " Option 6"))
492 then.True(t, strings.Contains(result, " Option 14"))
493 then.True(t, strings.Contains(result, "• Option 15"))
494 then.False(t, strings.Contains(result, "Option 2"))
495 then.False(t, strings.Contains(result, "Option 5"))
496 }
497
498 func TestThemeScrollWithEmptyChoices(t *testing.T) {
499 choices := []choose.Choice{}
500
501 result := ThemeScroll(choices, 0)
502
503 // Should return just a newline
504 then.Equals(t, "\n", result)
505 }
506
507 func TestThemeScrollWithSingleChoice(t *testing.T) {
508 choices := []choose.Choice{
509 {Text: "Only Option"},
510 }
511
512 result := ThemeScroll(choices, 0)
513
514 // Should show the single option as selected
515 then.True(t, strings.EqualFold(strings.TrimSpace(result), "• Only Option"))
516 }
517
518 func TestThemeScrollCursorOutOfBounds(t *testing.T) {
519 choices := []choose.Choice{
520 {Text: "Option 1"},
521 {Text: "Option 2"},
522 {Text: "Option 3"},
523 }
524
525 // Test with cursor beyond array bounds
526 result := ThemeScroll(choices, 5)
527
528 // Should handle gracefully and show all options
529 then.True(t, strings.Contains(result, " Option 1"))
530 then.True(t, strings.Contains(result, " Option 2"))
531 then.True(t, strings.Contains(result, " Option 3"))
532 }
533
534 func TestThemeScrollLimitCalculation(t *testing.T) {
535 // Test that limit is correctly calculated as min(10, numChoices)
536 // Test with 5 choices
537 choices5 := make([]choose.Choice, 5)
538 for i := range 5 {
539 choices5[i] = choose.Choice{Text: fmt.Sprintf("Option %d", i+1)}
540 }
541
542 result5 := ThemeScroll(choices5, 0)
543 lines5 := strings.Split(strings.TrimSpace(result5), "\n")
544 // Should have 5 lines (one for each choice)
545 then.SliceLen(t, 5, lines5)
546
547 // Test with 15 choices
548 choices15 := make([]choose.Choice, 15)
549 for i := range 15 {
550 choices15[i] = choose.Choice{Text: fmt.Sprintf("Option %d", i+1)}
551 }
552
553 result15 := ThemeScroll(choices15, 0)
554 lines15 := strings.Split(strings.TrimSpace(result15), "\n")
555 // Should have 10 lines (limited by min(10, 15))
556 then.SliceLen(t, 10, lines15)
557 }