-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathbuild_test.go
More file actions
165 lines (140 loc) · 4.83 KB
/
build_test.go
File metadata and controls
165 lines (140 loc) · 4.83 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
package main
import (
"fmt"
"os"
"slices"
"strings"
"testing"
"time"
"github.com/joeshaw/envdecode"
"github.com/stretchr/testify/require"
)
func init() {
if err := envdecode.Decode(&conf); err != nil {
fmt.Fprintf(os.Stderr, "Error decoding conf from env: %v", err)
os.Exit(1)
}
}
func TestExtCanonical(t *testing.T) {
require.Equal(t, ".jpg", extCanonical("https://example.com/image.jpg"))
require.Equal(t, ".jpg", extCanonical("https://example.com/image.JPG"))
require.Equal(t, ".jpg", extCanonical("https://example.com/image.jpg?query"))
}
func TestExtImageTarget(t *testing.T) {
require.Equal(t, ".jpg", extImageTarget(".jpg"))
require.Equal(t, ".webp", extImageTarget(".heic"))
}
func TestLexicographicBase32(t *testing.T) {
// Should only incorporate lower case characters.
require.Equal(t, lexicographicBase32, strings.ToLower(lexicographicBase32))
// All characters in the encoding set should be lexicographically ordered.
{
// This can be replaced with `strings.Clone` come Go 1.20.
b := make([]byte, len(lexicographicBase32))
copy(b, lexicographicBase32)
slices.Sort(b)
require.Equal(t, lexicographicBase32, string(b))
}
}
func TestPagePathKey(t *testing.T) {
require.Equal(t, "about", pagePathKey("./pages/about.ace"))
require.Equal(t, "about", pagePathKey("./pages-drafts/about.ace"))
require.Equal(t, "deep/about", pagePathKey("./pages/deep/about.ace"))
require.Equal(t, "deep/about", pagePathKey("./pages-drafts/deep/about.ace"))
require.Equal(t, "really/deep/about", pagePathKey("./pages/really/deep/about.ace"))
require.Equal(t, "really/deep/about", pagePathKey("./pages-drafts/really/deep/about.ace"))
}
func TestSimplifyMarkdownForSummary(t *testing.T) {
require.Equal(t, "check that links are removed", simplifyMarkdownForSummary("check that [links](/link) are removed"))
require.Equal(t, "double new lines are gone", simplifyMarkdownForSummary("double new\n\nlines are gone"))
require.Equal(t, "single new lines are gone", simplifyMarkdownForSummary("single new\nlines are gone"))
require.Equal(t, "space is trimmed", simplifyMarkdownForSummary(" space is trimmed "))
}
func TestBuildContributionGrid(t *testing.T) {
t.Parallel()
t.Run("Empty", func(t *testing.T) {
t.Parallel()
grid := buildContributionGrid(nil)
require.NotNil(t, grid)
require.Greater(t, len(grid.Weeks), 50)
require.NotEmpty(t, grid.MonthLabels)
// All cells should have count 0.
for _, week := range grid.Weeks {
for _, day := range week.Days {
require.Equal(t, 0, day.Count)
require.Equal(t, 0, day.Level)
}
}
})
t.Run("SingleAtom", func(t *testing.T) {
t.Parallel()
atoms := []*Atom{
{PublishedAt: time.Now().Add(-24 * time.Hour)},
}
grid := buildContributionGrid(atoms)
total := 0
for _, week := range grid.Weeks {
for _, day := range week.Days {
total += day.Count
}
}
require.Equal(t, 1, total)
})
t.Run("MultipleAtomsSameDay", func(t *testing.T) {
t.Parallel()
// Use yesterday noon UTC to avoid timezone boundary issues.
yesterday := time.Now().AddDate(0, 0, -1)
noon := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 12, 0, 0, 0, time.UTC)
atoms := []*Atom{
{PublishedAt: noon},
{PublishedAt: noon.Add(-1 * time.Hour)},
{PublishedAt: noon.Add(-2 * time.Hour)},
}
grid := buildContributionGrid(atoms)
dateStr := noon.Format("2006-01-02")
var found bool
for _, week := range grid.Weeks {
for _, day := range week.Days {
if day.Date == dateStr {
require.Equal(t, 3, day.Count)
require.Positive(t, day.Level)
found = true
}
}
}
require.True(t, found, "expected cell for %s not found in grid", dateStr)
})
t.Run("OldAtomsExcluded", func(t *testing.T) {
t.Parallel()
atoms := []*Atom{
{PublishedAt: time.Now().AddDate(-2, 0, 0)},
}
grid := buildContributionGrid(atoms)
total := 0
for _, week := range grid.Weeks {
for _, day := range week.Days {
total += day.Count
}
}
require.Equal(t, 0, total, "atoms older than 52 weeks should not appear")
})
t.Run("MonthLabelsPresent", func(t *testing.T) {
t.Parallel()
grid := buildContributionGrid(nil)
require.GreaterOrEqual(t, len(grid.MonthLabels), 12)
for _, m := range grid.MonthLabels {
require.NotEmpty(t, m.Name)
require.GreaterOrEqual(t, m.OffsetPct, 0.0)
require.Less(t, m.OffsetPct, 100.0)
}
})
}
func TestTruncateString(t *testing.T) {
require.Equal(t, "Short string unchanged.", truncateString("Short string unchanged.", 100))
exactly100Length := strings.Repeat("s", 100)
require.Equal(t, exactly100Length, truncateString(exactly100Length, 100))
require.Equal(t,
"This is a longer string that's going to need truncation and which will be truncated by ending it w …",
truncateString("This is a longer string that's going to need truncation and which will be truncated by ending it with a space and an ellipsis.", 100),
)
}