Repository navigation
Expand file tree
/
Copy pathsearch_service_test.go
More file actions
688 lines (621 loc) · 19.4 KB
/
Copy pathsearch_service_test.go
File metadata and controls
688 lines (621 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package main
import (
"testing"
)
// ============================================================================
// detectQueryType Tests
// ============================================================================
// TestDetectQueryTypeBlock tests block height detection
func TestDetectQueryTypeBlock(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"0", "block"},
{"1", "block"},
{"123456", "block"},
{"999999999", "block"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != tt.expected {
t.Errorf("detectQueryType(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// TestDetectQueryTypeDURL tests dURL detection
func TestDetectQueryTypeDURL(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"dero://myapp", "durl"},
{"dero://test-app", "durl"},
{"DERO://MyApp", "durl"},
{"dero://", "durl"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != tt.expected {
t.Errorf("detectQueryType(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// TestDetectQueryTypeAddress tests DERO address detection
func TestDetectQueryTypeAddress(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"full mainnet address", "dero1qyw4fl3dupcg5qlrcsvcedze507q9u67lxfpu8kgnzp04aq73yheqqg2ctjn4", "address"},
{"uppercase address", "DERO1qyw4fl3dupcg5qlrcsvcedze507q9u67lxfpu8kgnzp04aq73yheqqg2ctjn4", "address"},
{"short address", "dero1abc", "address"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != tt.expected {
t.Errorf("detectQueryType(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// TestDetectQueryTypeHash tests 64-char hex hash detection
func TestDetectQueryTypeHash(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Note: All-numeric 64-char strings are detected as "block" first (numeric check)
// Only hex strings with a-f characters are detected as "hash"
{"hash with a-f letters", "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", "hash"},
{"tx hash with mixed", "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2", "hash"},
{"scid with letters", "deadbeef1234567890deadbeef1234567890deadbeef1234567890deadbeef12", "hash"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != tt.expected {
t.Errorf("detectQueryType(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// TestDetectQueryTypeUnknown tests unknown query types
func TestDetectQueryTypeUnknown(t *testing.T) {
tests := []struct {
name string
input string
}{
{"random text", "hello world"},
{"partial hash", "abcd1234"},
{"invalid prefix", "btc1address"},
{"special chars", "!@#$%"},
{"empty spaces", " "},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != "unknown" {
t.Errorf("detectQueryType(%q) = %q, want 'unknown'", tt.input, result)
}
})
}
}
// ============================================================================
// truncateQuery Tests
// ============================================================================
// TestTruncateQueryShort tests that short queries are not truncated
func TestTruncateQueryShort(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"hello", "hello"},
{"12345678901234567890", "12345678901234567890"}, // Exactly 20 chars
{"abc", "abc"},
{"", ""},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := truncateQuery(tt.input)
if result != tt.expected {
t.Errorf("truncateQuery(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// TestTruncateQueryLong tests that long queries are truncated
func TestTruncateQueryLong(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
// 21 chars: first 10 + "..." + last 10
"21 chars",
"123456789012345678901",
"1234567890...2345678901",
},
{
// 64 chars: first 10 + "..." + last 10
"64-char hash",
"abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
"abcdef1234...1234567890",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := truncateQuery(tt.input)
if result != tt.expected {
t.Errorf("truncateQuery(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// ============================================================================
// SearchResult Struct Tests
// ============================================================================
// TestSearchResultFields tests SearchResult struct field access
func TestSearchResultFields(t *testing.T) {
result := SearchResult{
Success: true,
Type: "block",
Query: "123456",
Data: map[string]interface{}{
"height": 123456,
},
}
if !result.Success {
t.Error("Success should be true")
}
if result.Type != "block" {
t.Errorf("Type = %q, want 'block'", result.Type)
}
if result.Query != "123456" {
t.Errorf("Query = %q, want '123456'", result.Query)
}
if result.Data["height"] != 123456 {
t.Errorf("Data[height] = %v, want 123456", result.Data["height"])
}
}
// TestSearchResultError tests error result creation
func TestSearchResultError(t *testing.T) {
result := SearchResult{
Success: false,
Type: "unknown",
Query: "invalid",
Error: "Test error message",
}
if result.Success {
t.Error("Success should be false")
}
if result.Error != "Test error message" {
t.Errorf("Error = %q, want 'Test error message'", result.Error)
}
}
// ============================================================================
// Address Validation Tests (searchAddress helper logic)
// ============================================================================
// TestAddressValidation tests address format validation logic
func TestAddressValidation(t *testing.T) {
tests := []struct {
name string
address string
valid bool
}{
{"valid mainnet address", "dero1qyw4fl3dupcg5qlrcsvcedze507q9u67lxfpu8kgnzp04aq73yheqqg2ctjn4", true},
{"valid uppercase", "DERO1qyw4fl3dupcg5qlrcsvcedze507q9u67lxfpu8kgnzp04aq73yheqqg2ctjn4", true},
{"valid short", "dero1abc", true},
{"invalid prefix", "btc1address", false},
{"empty", "", false},
{"just dero1", "dero1", true}, // Technically passes prefix check
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// This tests the validation logic used in searchAddress
isValid := len(tt.address) > 0 && (len(tt.address) >= 5 &&
(tt.address[:5] == "dero1" || tt.address[:5] == "DERO1"))
// Simplified check matching searchAddress logic
hasPrefix := len(tt.address) >= 5 &&
(tt.address[:5] == "dero1" || tt.address[:5] == "DERO1")
if hasPrefix != tt.valid {
t.Errorf("address %q hasPrefix = %v, want %v", tt.address, hasPrefix, tt.valid)
}
_ = isValid // silence unused warning
})
}
}
// ============================================================================
// getMapKeys Helper Tests
// ============================================================================
// TestGetMapKeys tests the map key extraction helper
func TestGetMapKeys(t *testing.T) {
tests := []struct {
name string
input map[string]interface{}
expected int
}{
{
"empty map",
map[string]interface{}{},
0,
},
{
"single key",
map[string]interface{}{"code": "test"},
1,
},
{
"multiple keys",
map[string]interface{}{
"code": "Function test() {}",
"balance": uint64(1000),
"stringkeys": map[string]interface{}{},
"uint64keys": map[string]interface{}{},
},
4,
},
{
"nil values in map",
map[string]interface{}{
"key1": nil,
"key2": "value",
"key3": nil,
},
3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
keys := getMapKeys(tt.input)
if len(keys) != tt.expected {
t.Errorf("getMapKeys() returned %d keys, want %d", len(keys), tt.expected)
}
})
}
}
// TestGetMapKeysContents verifies actual key names are returned
func TestGetMapKeysContents(t *testing.T) {
input := map[string]interface{}{
"alpha": 1,
"beta": 2,
"gamma": 3,
}
keys := getMapKeys(input)
keySet := make(map[string]bool)
for _, k := range keys {
keySet[k] = true
}
expectedKeys := []string{"alpha", "beta", "gamma"}
for _, expected := range expectedKeys {
if !keySet[expected] {
t.Errorf("getMapKeys() missing expected key %q", expected)
}
}
}
// ============================================================================
// Edge Case Tests for detectQueryType
// ============================================================================
// TestDetectQueryType64DigitNumeric tests the edge case where a 64-digit
// all-numeric string is detected as "hash" (potential SCID) rather than "block"
// Session 86 fixed this: 64-char hex check comes BEFORE block height check
// to properly handle all-digit SCIDs like Genesis SC (0000...0001)
func TestDetectQueryType64DigitNumeric(t *testing.T) {
// A 64-digit all-numeric string (valid hex, could be SCID)
input := "1234567890123456789012345678901234567890123456789012345678901234"
result := detectQueryType(input)
// Current behavior: 64-char hex check comes first, so this is "hash"
// This allows proper handling of Genesis SC SCID (0000...0001)
if result != "hash" {
t.Errorf("detectQueryType(%q) = %q, expected 'hash' (64-char hex priority)", input, result)
}
}
// TestDetectQueryTypeWhitespace tests whitespace handling
func TestDetectQueryTypeWhitespace(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"leading spaces block", " 123456", "block"},
{"trailing spaces block", "123456 ", "block"},
{"both spaces block", " 123456 ", "block"},
{"leading spaces address", " dero1abc", "address"},
{"trailing spaces address", "dero1abc ", "address"},
{"leading spaces durl", " dero://app", "durl"},
{"trailing spaces durl", "dero://app ", "durl"},
{"leading spaces hash", " abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", "hash"},
{"tabs and spaces", "\t 123456 \t", "block"},
{"newline wrapped", "\n123456\n", "block"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != tt.expected {
t.Errorf("detectQueryType(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// TestDetectQueryTypeCaseSensitivity tests case variations
func TestDetectQueryTypeCaseSensitivity(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"lowercase durl", "dero://app", "durl"},
{"uppercase durl", "DERO://APP", "durl"},
{"mixed case durl", "DeRo://ApP", "durl"},
{"lowercase address", "dero1qyw4fl3dupcg5qlrcsvcedze507q9u67lxfpu8kgnzp04aq73yheqqg2ctjn4", "address"},
{"uppercase address", "DERO1QYW4FL3DUPCG5QLRCSVCEDZE507Q9U67LXFPU8KGNZP04AQ73YHEQQG2CTJN4", "address"},
{"mixed case address", "DeRo1qyw4fl3dupcg5qlrcsvcedze507q9u67lxfpu8kgnzp04aq73yheqqg2ctjn4", "address"},
{"uppercase hex hash", "ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890", "hash"},
{"mixed case hex hash", "AbCdEf1234567890AbCdEf1234567890AbCdEf1234567890AbCdEf1234567890", "hash"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != tt.expected {
t.Errorf("detectQueryType(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// TestDetectQueryTypeHashBoundary tests hash length boundaries
func TestDetectQueryTypeHashBoundary(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"63 char hex (too short)", "abcdef1234567890abcdef1234567890abcdef1234567890abcdef123456789", "unknown"},
{"64 char hex (exact)", "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", "hash"},
{"65 char hex (too long)", "abcdef1234567890abcdef1234567890abcdef1234567890abcdef12345678901", "unknown"},
{"32 char hex (half)", "abcdef1234567890abcdef1234567890", "unknown"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != tt.expected {
t.Errorf("detectQueryType(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// TestDetectQueryTypeSpecialInputs tests special/malformed inputs
func TestDetectQueryTypeSpecialInputs(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"empty string", "", "unknown"},
{"just whitespace", " ", "unknown"},
{"dero:// without name", "dero://", "durl"},
{"dero1 only", "dero1", "address"},
{"negative number", "-123456", "unknown"},
{"decimal number", "123.456", "unknown"},
{"hex with 0x prefix", "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", "unknown"},
{"hash with spaces", "abcdef12 34567890 abcdef12 34567890 abcdef12 34567890 abcdef12 34567890", "unknown"},
{"durl with query params", "dero://app?param=value", "durl"},
{"durl with path", "dero://app/path/to/resource", "durl"},
{"unicode in query", "hello世界", "unknown"},
{"emoji", "[START]123456", "unknown"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != tt.expected {
t.Errorf("detectQueryType(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// ============================================================================
// truncateQuery Edge Cases
// ============================================================================
// TestTruncateQueryBoundary tests boundary conditions
func TestTruncateQueryBoundary(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"exactly 19 chars", "1234567890123456789", "1234567890123456789"},
{"exactly 20 chars", "12345678901234567890", "12345678901234567890"},
{"exactly 21 chars", "123456789012345678901", "1234567890...2345678901"},
{"exactly 22 chars", "1234567890123456789012", "1234567890...3456789012"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := truncateQuery(tt.input)
if result != tt.expected {
t.Errorf("truncateQuery(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
// TestTruncateQueryUnicode tests unicode character handling
func TestTruncateQueryUnicode(t *testing.T) {
// Note: truncateQuery uses len() which counts bytes, not runes
// This documents the current behavior
tests := []struct {
name string
input string
}{
{"short unicode", "hello世界"},
{"unicode address-like", "dero1世界test"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Should not panic
result := truncateQuery(tt.input)
if result == "" && tt.input != "" {
t.Errorf("truncateQuery(%q) returned empty string unexpectedly", tt.input)
}
})
}
}
// ============================================================================
// SearchResult Edge Cases
// ============================================================================
// TestSearchResultNilData tests SearchResult with nil Data map
func TestSearchResultNilData(t *testing.T) {
result := SearchResult{
Success: false,
Type: "block",
Query: "99999999999",
Error: "Block not found",
Data: nil,
}
if result.Data != nil {
t.Error("Data should be nil")
}
if result.Error != "Block not found" {
t.Errorf("Error = %q, want 'Block not found'", result.Error)
}
}
// TestSearchResultEmptyData tests SearchResult with empty Data map
func TestSearchResultEmptyData(t *testing.T) {
result := SearchResult{
Success: true,
Type: "address",
Query: "dero1test",
Data: map[string]interface{}{},
}
if result.Data == nil {
t.Error("Data should not be nil")
}
if len(result.Data) != 0 {
t.Errorf("Data should be empty, got %d keys", len(result.Data))
}
}
// TestSearchResultComplexData tests SearchResult with nested data
func TestSearchResultComplexData(t *testing.T) {
result := SearchResult{
Success: true,
Type: "address",
Query: "dero1test",
Data: map[string]interface{}{
"address": "dero1test",
"valid": true,
"hasGnomon": false,
"owned_apps": []map[string]interface{}{},
"nested": map[string]interface{}{
"key": "value",
},
},
}
if !result.Success {
t.Error("Success should be true")
}
if result.Data["valid"] != true {
t.Error("Data[valid] should be true")
}
if apps, ok := result.Data["owned_apps"].([]map[string]interface{}); !ok {
t.Error("Data[owned_apps] should be []map[string]interface{}")
} else if len(apps) != 0 {
t.Errorf("Data[owned_apps] should be empty, got %d", len(apps))
}
}
// ============================================================================
// Type Detection Priority Tests
// ============================================================================
// TestDetectQueryTypePriority verifies the priority order of type detection
func TestDetectQueryTypePriority(t *testing.T) {
// This test documents the detection priority:
// 1. Numeric (block height) - checked first
// 2. dURL (dero:// prefix)
// 3. Address (dero1 prefix)
// 4. 64-char hex (hash)
// 5. Unknown (fallback)
// A string that could match multiple patterns should follow priority
tests := []struct {
name string
input string
expected string
explanation string
}{
{
"numeric wins over all",
"123",
"block",
"Pure numeric is always block height",
},
{
"dero:// wins for durl",
"dero://123",
"durl",
"dero:// prefix triggers durl even with numeric path",
},
{
"dero1 wins for address",
"dero1abcdef",
"address",
"dero1 prefix triggers address detection",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := detectQueryType(tt.input)
if result != tt.expected {
t.Errorf("detectQueryType(%q) = %q, want %q (%s)",
tt.input, result, tt.expected, tt.explanation)
}
})
}
}
// ============================================================================
// Benchmarks
// ============================================================================
// BenchmarkGetMapKeys benchmarks the map key extraction
func BenchmarkGetMapKeys(b *testing.B) {
m := map[string]interface{}{
"code": "Function test() { return 1; }",
"balance": uint64(1000000),
"stringkeys": map[string]interface{}{"owner": "dero1..."},
"uint64keys": map[string]interface{}{"count": uint64(42)},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
getMapKeys(m)
}
}
// BenchmarkDetectQueryType benchmarks query type detection
func BenchmarkDetectQueryType(b *testing.B) {
queries := []string{
"123456",
"dero://myapp",
"dero1qyw4fl3dupcg5qlrcsvcedze507q9u67lxfpu8kgnzp04aq73yheqqg2ctjn4",
"abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
"unknown query",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, q := range queries {
detectQueryType(q)
}
}
}
// BenchmarkTruncateQuery benchmarks query truncation
func BenchmarkTruncateQuery(b *testing.B) {
queries := []string{
"short",
"abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, q := range queries {
truncateQuery(q)
}
}
}