-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite_test.go
More file actions
723 lines (620 loc) · 19.9 KB
/
Copy pathsite_test.go
File metadata and controls
723 lines (620 loc) · 19.9 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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
package pages
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewSite(t *testing.T) {
site := NewSite()
assert.False(t, site.Created.IsZero(), "Created time should be set")
assert.False(t, site.Updated.IsZero(), "Updated time should be set")
assert.Equal(t, "Localhost", site.Name, "Name should be 'Localhost'")
assert.Equal(t, "localhost", site.Host, "Host should be 'localhost'")
assert.Equal(t, "https", site.Scheme, "Scheme should be 'https'")
assert.Equal(t, "en", site.Locale, "Locale should be 'en'")
assert.Equal(t, "UTC", site.Timezone, "Timezone should be 'UTC'")
assert.Equal(t, " | ", site.Separator, "Separator should be ' | '")
assert.NotNil(t, site.Meta, "Meta map should be initialized")
assert.Equal(t, Draft, site.Status, "Status should be draft by default")
assert.False(t, site.IsDefault, "IsDefault should be false by default")
}
func TestSite_String(t *testing.T) {
tests := []struct {
name string
site *Site
want string
}{
{
name: "Site with name",
site: &Site{Name: "Test Site"},
want: "Test Site",
},
{
name: "Site without name",
site: &Site{},
want: "n/a",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.site.String()
assert.Equal(t, tt.want, got, "Site.String() should return the expected value")
})
}
}
func TestSite_IsLocalhost(t *testing.T) {
tests := []struct {
name string
site *Site
want bool
}{
{
name: "Empty host",
site: &Site{Host: ""},
want: true,
},
{
name: "localhost",
site: &Site{Host: "localhost"},
want: true,
},
{
name: "127.0.0.1",
site: &Site{Host: "127.0.0.1"},
want: true,
},
{
name: "example.com",
site: &Site{Host: "example.com"},
want: false,
},
{
name: "subdomain.example.com",
site: &Site{Host: "subdomain.example.com"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.site.IsLocalhost()
assert.Equal(t, tt.want, got, "Site.IsLocalhost() should return the expected value")
})
}
}
func TestSite_Origin(t *testing.T) {
tests := []struct {
name string
site *Site
want string
}{
{
name: "HTTPS site",
site: &Site{Scheme: "https", Host: "example.com"},
want: "https://example.com",
},
{
name: "HTTP site",
site: &Site{Scheme: "http", Host: "localhost"},
want: "http://localhost",
},
{
name: "Custom scheme",
site: &Site{Scheme: "ftp", Host: "files.example.com"},
want: "ftp://files.example.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.site.Origin()
assert.Equal(t, tt.want, got, "Site.Origin() should return the expected value")
})
}
}
func TestSite_URL(t *testing.T) {
tests := []struct {
name string
site *Site
want string
}{
{
name: "Site with empty relative path",
site: &Site{Scheme: "https", Host: "example.com", RelativePath: ""},
want: "https://example.com",
},
{
name: "Site with root relative path",
site: &Site{Scheme: "https", Host: "example.com", RelativePath: "/"},
want: "https://example.com",
},
{
name: "Site with relative path starting with /",
site: &Site{Scheme: "https", Host: "example.com", RelativePath: "/blog"},
want: "https://example.com/blog",
},
{
name: "Site with relative path without /",
site: &Site{Scheme: "https", Host: "example.com", RelativePath: "shop"},
want: "https://example.com/shop",
},
{
name: "Site with nested relative path without /",
site: &Site{Scheme: "https", Host: "example.com", RelativePath: "api/v1"},
want: "https://example.com/api/v1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.site.URL()
assert.Equal(t, tt.want, got, "Site.URL() should return the expected value")
})
}
}
func TestSite_Home(t *testing.T) {
tests := []struct {
name string
site *Site
isRoot bool
want string
}{
{
name: "Root site",
site: &Site{
Scheme: "https",
Host: "example.com",
IsRoot: true,
},
isRoot: true,
want: "https://example.com",
},
{
name: "Non-root site with empty relative path",
site: &Site{
Scheme: "https",
Host: "example.com",
RelativePath: "",
IsRoot: false,
},
isRoot: false,
want: "https://example.com",
},
{
name: "Non-root site with relative path",
site: &Site{
Scheme: "https",
Host: "example.com",
RelativePath: "/blog",
IsRoot: false,
},
isRoot: false,
want: "https://example.com/blog",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.site.IsRoot = tt.isRoot
got := tt.site.Home()
assert.Equal(t, tt.want, got, "Site.Home() should return the expected value")
})
}
}
func TestSite_Location(t *testing.T) {
tests := []struct {
name string
site *Site
wantErr bool
wantName string
}{
{
name: "Valid timezone UTC",
site: &Site{Timezone: "UTC"},
wantErr: false,
wantName: "UTC",
},
{
name: "Valid timezone America/New_York",
site: &Site{Timezone: "America/New_York"},
wantErr: false,
wantName: "America/New_York",
},
{
name: "Invalid timezone",
site: &Site{Timezone: "Invalid/Timezone"},
wantErr: true,
wantName: "UTC", // Should fallback to UTC
},
{
name: "Empty timezone",
site: &Site{Timezone: ""},
wantErr: true,
wantName: "UTC", // Should fallback to UTC
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.site.Location()
assert.Equal(t, tt.wantName, got.String(), "Site.Location() should return the expected timezone name")
})
}
}
func TestSite_Tag(t *testing.T) {
tests := []struct {
name string
site *Site
wantLang string
}{
{
name: "Valid locale en",
site: &Site{Locale: "en"},
wantLang: "en",
},
{
name: "Valid locale en-US",
site: &Site{Locale: "en-US"},
wantLang: "en-US",
},
{
name: "Locale with underscore",
site: &Site{Locale: "en_US"},
wantLang: "en-US", // Should convert underscore to hyphen
},
{
name: "Invalid locale",
site: &Site{Locale: "invalid-locale"},
wantLang: "en", // Should fallback to English
},
{
name: "Empty locale",
site: &Site{Locale: ""},
wantLang: "en", // Should fallback to English
},
{
name: "Complex locale zh-Hans-CN",
site: &Site{Locale: "zh-Hans-CN"},
wantLang: "zh-Hans-CN",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.site.Tag()
assert.Equal(t, tt.wantLang, got.String(), "Site.Tag() should return the expected language tag")
})
}
}
func TestSite_LocationCaching(t *testing.T) {
site := &Site{Timezone: "UTC"}
// First call should load the location
loc1 := site.Location()
// Second call should return cached location
loc2 := site.Location()
assert.Equal(t, loc1, loc2, "Location() should return cached location on subsequent calls")
// Verify location is cached
assert.NotNil(t, site.location, "Location() should cache the location in the site struct")
}
func TestSite_TagCaching(t *testing.T) {
site := &Site{Locale: "en"}
// First call should parse the tag
tag1 := site.Tag()
// Second call should return cached tag
tag2 := site.Tag()
assert.Equal(t, tag1, tag2, "Tag() should return cached tag on subsequent calls")
// Verify tag is cached
assert.NotNil(t, site.tag, "Tag() should cache the tag in the site struct")
}
func TestSite_FieldDefaults(t *testing.T) {
site := &Site{}
// Test that uninitialized fields don't panic
_ = site.String()
_ = site.IsLocalhost()
_ = site.Origin()
_ = site.URL()
_ = site.Home()
_ = site.Location()
_ = site.Tag()
// Should not panic even with empty fields
assert.True(t, site.IsLocalhost(), "Empty host should be considered localhost")
assert.Equal(t, "://", site.Origin(), "Empty scheme and host should produce '://'")
}
func TestSite_CompleteExample(t *testing.T) {
site := &Site{
ID: "test-site",
Created: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
Updated: time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC),
Name: "Example Website",
Title: "Example Site Title",
Separator: " - ",
Locale: "en-US",
Timezone: "America/New_York",
Countries: []string{"US", "CA"},
Scheme: "https",
Host: "example.com",
RelativePath: "/blog",
IsDefault: true,
Status: Published,
Meta: NewMeta(map[string]any{"theme": "dark"}),
}
// Test all methods
assert.Equal(t, "Example Website", site.String(), "String() should return 'Example Website'")
assert.False(t, site.IsLocalhost(), "Should not be localhost")
assert.Equal(t, "https://example.com", site.Origin(), "Origin() should return 'https://example.com'")
assert.Equal(t, "https://example.com/blog", site.URL(), "URL() should return 'https://example.com/blog'")
assert.Equal(t, "https://example.com/blog", site.Home(), "Home() should return 'https://example.com/blog'")
location := site.Location()
assert.Equal(t, "America/New_York", location.String(), "Location() should return 'America/New_York'")
tag := site.Tag()
assert.Equal(t, "en-US", tag.String(), "Tag() should return 'en-US'")
}
func TestSite_EdgeCases(t *testing.T) {
t.Run("Site with special characters in host", func(t *testing.T) {
site := &Site{Scheme: "https", Host: "sub-domain.example.co.uk"}
assert.Equal(t, "https://sub-domain.example.co.uk", site.Origin(), "Origin() with special characters should return the expected value")
})
t.Run("Site with port", func(t *testing.T) {
site := &Site{Scheme: "http", Host: "localhost:8080"}
assert.Equal(t, "http://localhost:8080", site.Origin(), "Origin() with port should return the expected value")
})
t.Run("Site with IPv6", func(t *testing.T) {
site := &Site{Scheme: "https", Host: "[::1]"}
assert.Equal(t, "https://[::1]", site.Origin(), "Origin() with IPv6 should return the expected value")
})
t.Run("Complex relative path", func(t *testing.T) {
site := &Site{Scheme: "https", Host: "example.com", RelativePath: "/api/v1/users"}
assert.Equal(t, "https://example.com/api/v1/users", site.URL(), "URL() with complex path should return the expected value")
})
t.Run("URL with trailing slash in relative path", func(t *testing.T) {
site := &Site{Scheme: "https", Host: "example.com", RelativePath: "/blog/"}
assert.Equal(t, "https://example.com/blog/", site.URL(), "URL() should preserve trailing slash in relative path")
})
t.Run("URL with query-like relative path", func(t *testing.T) {
site := &Site{Scheme: "https", Host: "example.com", RelativePath: "search?q=test"}
assert.Equal(t, "https://example.com/search?q=test", site.URL(), "URL() should handle query-like relative paths")
})
t.Run("URL with hash-like relative path", func(t *testing.T) {
site := &Site{Scheme: "https", Host: "example.com", RelativePath: "section#anchor"}
assert.Equal(t, "https://example.com/section#anchor", site.URL(), "URL() should handle hash-like relative paths")
})
}
func TestSite_Copy(t *testing.T) {
originalTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
t.Run("Copy basic site", func(t *testing.T) {
original := &Site{
ID: "test-id",
Created: originalTime,
Updated: originalTime,
Name: "Test Site",
Title: "Test Title",
Separator: " - ",
Locale: "en-US",
Timezone: "America/New_York",
Countries: []string{"US", "CA"},
Scheme: "https",
Host: "example.com",
RelativePath: "/blog",
IsDefault: true,
Status: Published,
Meta: NewMeta(map[string]any{"theme": "dark", "version": 1}),
IsRoot: false,
}
// Add some meta tags
original.DOM.Head.Add(
CharsetMetaNode("UTF-8"),
NameMetaNode("description", "Test description"),
PropertyMetaNode("og:title", "Test OG Title"),
)
copy := original.Copy()
// Verify all fields are copied but not the same reference
assert.NotSame(t, original, copy, "Copy should return a different instance")
assert.Equal(t, original.ID, copy.ID, "ID should be copied")
assert.Equal(t, original.Created, copy.Created, "Created should be copied")
assert.Equal(t, original.Updated, copy.Updated, "Updated should be copied")
assert.Equal(t, original.Name, copy.Name, "Name should be copied")
assert.Equal(t, original.Title, copy.Title, "Title should be copied")
assert.Equal(t, original.Separator, copy.Separator, "Separator should be copied")
assert.Equal(t, original.Locale, copy.Locale, "Locale should be copied")
assert.Equal(t, original.Timezone, copy.Timezone, "Timezone should be copied")
assert.Equal(t, original.Scheme, copy.Scheme, "Scheme should be copied")
assert.Equal(t, original.Host, copy.Host, "Host should be copied")
assert.Equal(t, original.RelativePath, copy.RelativePath, "RelativePath should be copied")
assert.Equal(t, original.IsDefault, copy.IsDefault, "IsDefault should be copied")
assert.Equal(t, original.Status, copy.Status, "Status should be copied")
// Verify slices are cloned
assert.Equal(t, original.Countries, copy.Countries, "Countries should be copied")
// Test independence by modifying copy
copy.Countries = append(copy.Countries, "FR")
assert.Len(t, original.Countries, 2, "Original countries should not be modified")
// Restore for later tests
copy.Countries = copy.Countries[:len(copy.Countries)-1]
// Verify maps are cloned
assert.Equal(t, original.Meta, copy.Meta, "Meta should be copied")
// Test independence by modifying copy
copy.Meta["test"] = "value"
assert.NotContains(t, original.Meta, "test", "Original metadata should be independent")
// Remove test key
delete(copy.Meta, "test")
// Verify MetaTags is copied correctly
copyHead := copy.DOM.Head.Nodes.String()
assert.Contains(t, copyHead, `charset="UTF-8"`, "Meta tag charset should be copied")
assert.Contains(t, copyHead, `name="description"`, "Meta tag description should be copied")
assert.Contains(t, copyHead, `property="og:title"`, "Meta tag og:title should be copied")
// Verify cached fields are reset
assert.Nil(t, copy.location, "Location cache should be reset")
assert.Nil(t, copy.tag, "Tag cache should be reset")
assert.False(t, copy.IsRoot, "IsRoot should be reset to false")
})
t.Run("Copy site with empty slices and maps", func(t *testing.T) {
original := &Site{
Name: "Test Site",
Countries: []string{},
Meta: NewMeta(nil),
}
copy := original.Copy()
assert.Empty(t, copy.Countries, "Countries slice should be empty")
// For empty slices, they're both empty but should be different instances
copy.Countries = append(copy.Countries, "test")
assert.Empty(t, original.Countries, "Original countries slice should remain empty")
assert.Empty(t, copy.Meta, "Meta map should be empty")
// For empty maps, they're both empty but should be different instances
copy.Meta["test"] = "value"
assert.Empty(t, original.Meta, "Original metadata map should remain empty")
})
t.Run("Modify copy doesn't affect original", func(t *testing.T) {
original := &Site{
Name: "Original Site",
Countries: []string{"US"},
Meta: NewMeta(map[string]any{"key": "original"}),
}
copy := original.Copy()
// Modify the copy
copy.Name = "Modified Site"
copy.Countries = append(copy.Countries, "CA")
copy.Meta["key"] = "modified"
copy.Meta["newKey"] = "new value"
// Verify original is unchanged
assert.Equal(t, "Original Site", original.Name, "Original name should be unchanged")
assert.Equal(t, []string{"US"}, original.Countries, "Original countries should be unchanged")
assert.Equal(t, "original", original.Meta["key"], "Original metadata should be unchanged")
assert.NotContains(t, original.Meta, "newKey", "Original should not have new metadata key")
})
}
func TestSite_InvalidTimezoneHandling(t *testing.T) {
t.Run("Multiple calls to Location with invalid timezone", func(t *testing.T) {
site := &Site{Timezone: "Invalid/Timezone"}
loc1 := site.Location()
loc2 := site.Location()
assert.Same(t, loc1, loc2, "Location() should return cached location even for invalid timezone")
assert.Equal(t, "UTC", loc1.String(), "Invalid timezone should fallback to UTC")
})
t.Run("Valid timezone after invalid one", func(t *testing.T) {
site := &Site{Timezone: "Invalid/Timezone"}
// First call with invalid timezone
loc1 := site.Location()
assert.Equal(t, "UTC", loc1.String(), "First call with invalid timezone should return UTC")
// Change to valid timezone
site.Timezone = "America/New_York"
site.location = nil // Reset cache
loc2 := site.Location()
assert.Equal(t, "America/New_York", loc2.String(), "Second call with valid timezone should return correct location")
})
}
func TestSite_InvalidLocaleHandling(t *testing.T) {
t.Run("Multiple calls to Tag with invalid locale", func(t *testing.T) {
site := &Site{Locale: "invalid-locale"}
tag1 := site.Tag()
tag2 := site.Tag()
assert.Equal(t, tag1, tag2, "Tag() should return cached tag on subsequent calls")
assert.Equal(t, "en", tag1.String(), "Invalid locale should fallback to English")
})
t.Run("Valid locale after invalid one", func(t *testing.T) {
site := &Site{Locale: "invalid-locale"}
// First call with invalid locale
tag1 := site.Tag()
assert.Equal(t, "en", tag1.String(), "First call with invalid locale should return English")
// Change to valid locale
site.Locale = "fr-FR"
site.tag = nil // Reset cache
tag2 := site.Tag()
assert.Equal(t, "fr-FR", tag2.String(), "Second call with valid locale should return correct tag")
})
}
func BenchmarkSite_String(b *testing.B) {
site := &Site{Name: "Test Site"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = site.String()
}
}
func BenchmarkSite_IsLocalhost(b *testing.B) {
sites := []*Site{
{Host: "localhost"},
{Host: "127.0.0.1"},
{Host: "example.com"},
{Host: ""},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
site := sites[i%len(sites)]
_ = site.IsLocalhost()
}
}
func BenchmarkSite_Origin(b *testing.B) {
site := &Site{Scheme: "https", Host: "example.com"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = site.Origin()
}
}
func BenchmarkSite_URL(b *testing.B) {
site := &Site{Scheme: "https", Host: "example.com", RelativePath: "/api/v1/users"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = site.URL()
}
}
func BenchmarkSite_Home(b *testing.B) {
site := &Site{
Scheme: "https",
Host: "example.com",
RelativePath: "/blog",
IsRoot: false,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = site.Home()
}
}
func BenchmarkSite_Location_Cached(b *testing.B) {
site := &Site{Timezone: "UTC"}
site.Location() // Pre-cache the location
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = site.Location()
}
}
func BenchmarkSite_Location_Uncached(b *testing.B) {
sites := []*Site{
{Timezone: "UTC"},
{Timezone: "America/New_York"},
{Timezone: "Europe/London"},
{Timezone: "Asia/Tokyo"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
site := sites[i%len(sites)]
site.location = nil // Reset cache each time
_ = site.Location()
}
}
func BenchmarkSite_Tag_Cached(b *testing.B) {
site := &Site{Locale: "en-US"}
site.Tag() // Pre-cache the tag
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = site.Tag()
}
}
func BenchmarkSite_Tag_Uncached(b *testing.B) {
locales := []string{"en-US", "fr-FR", "de-DE", "es-ES", "ja-JP"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
site := &Site{Locale: locales[i%len(locales)]}
_ = site.Tag()
}
}
func BenchmarkSite_Copy(b *testing.B) {
original := &Site{
ID: "test-id",
Created: time.Now(),
Updated: time.Now(),
Name: "Test Site",
Countries: []string{"US", "CA", "MX", "GB", "FR"},
Meta: NewMeta(map[string]any{"theme": "dark", "version": 1, "features": []string{"search", "api"}}),
}
original.DOM.Head.Add(
CharsetMetaNode("UTF-8"),
NameMetaNode("description", "Test description"),
PropertyMetaNode("og:title", "Test OG Title"),
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = original.Copy()
}
}