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
|
package index_test
import (
"errors"
"fmt"
"os"
"slices"
"testing"
"time"
"github.com/jpappel/atlas/pkg/index"
)
var indexCases map[string]func(t *testing.T) index.Index
func init() {
indexCases = make(map[string]func(t *testing.T) index.Index)
indexCases["single file"] = func(t *testing.T) index.Index {
root := t.TempDir()
index := index.Index{Root: root, Filters: []index.DocFilter{index.NewExtensionFilter(".md")}}
f, err := os.Create(root + "/a_file.md")
if err != nil {
t.Fatal(err)
}
f.WriteString("some file contents\n")
return index
}
indexCases["large file"] = func(t *testing.T) index.Index {
root := t.TempDir()
index := index.Index{Root: root}
return index
}
indexCases["worker saturation"] = func(t *testing.T) index.Index {
root := t.TempDir()
index := index.Index{Root: root}
permission := os.FileMode(0o777)
for _, dirName := range []string{"a", "b", "c", "d", "e", "f"} {
dir := root + "/" + dirName
if err := os.Mkdir(dir, permission); err != nil {
t.Fatal(err)
}
for i := range 8 {
fName := fmt.Sprint(dirName, i)
f, err := os.Create(dir + "/" + fName)
if err != nil {
t.Fatal(err)
}
f.WriteString(fName)
}
}
return index
}
}
func TestIndex_Traverse(t *testing.T) {
tests := []struct {
name string
indexCase func(t *testing.T) index.Index
numWorkers uint
want []string
}{
{name: "single file", indexCase: indexCases["single file"], numWorkers: 2, want: []string{"a_file.md"}},
{name: "saturation test", indexCase: indexCases["worker saturation"], numWorkers: 2, want: []string{
"a/a0", "a/a1", "a/a2", "a/a3", "a/a4", "a/a5", "a/a6", "a/a7",
"b/b0", "b/b1", "b/b2", "b/b3", "b/b4", "b/b5", "b/b6", "b/b7",
"c/c0", "c/c1", "c/c2", "c/c3", "c/c4", "c/c5", "c/c6", "c/c7",
"d/d0", "d/d1", "d/d2", "d/d3", "d/d4", "d/d5", "d/d6", "d/d7",
"e/e0", "e/e1", "e/e2", "e/e3", "e/e4", "e/e5", "e/e6", "e/e7",
"f/f0", "f/f1", "f/f2", "f/f3", "f/f4", "f/f5", "f/f6", "f/f7",
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
idx := tt.indexCase(t)
got := idx.Traverse(tt.numWorkers, true)
slices.Sort(got)
slices.Sort(tt.want)
n := min(len(got), len(tt.want))
if len(got) != len(tt.want) {
t.Errorf("Wanted %v got %v paths", len(tt.want), len(got))
t.Logf("Checking up to %d values", n)
}
for i := range n {
gotPath := got[i]
wantPath := idx.Root + "/" + tt.want[i]
if gotPath != wantPath {
t.Errorf("At %d wanted %v, got %v", i, wantPath, gotPath)
}
}
})
}
}
func TestIndex_Filter(t *testing.T) {
tests := []struct {
name string
paths []string
indexCase func(t *testing.T) index.Index
numWorkers uint
want []string
}{
{"single file", []string{"a_file.md"}, indexCases["single file"], 2, []string{"a_file.md"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
idx := tt.indexCase(t)
for i, path := range tt.paths {
tt.paths[i] = idx.Root + "/" + path
}
got := idx.Filter(tt.paths, tt.numWorkers)
slices.Sort(got)
slices.Sort(tt.want)
n := min(len(got), len(tt.want))
if len(got) != len(tt.want) {
t.Errorf("Wanted %v got %v paths", len(tt.want), len(got))
t.Logf("Checking up to %d values", n)
}
for i := range n {
gotPath := got[i]
wantPath := idx.Root + "/" + tt.want[i]
if gotPath != wantPath {
t.Errorf("At %d wanted %v, got %v", i, wantPath, gotPath)
}
}
})
}
}
func newTestFile(t *testing.T, name string) (*os.File, string) {
dir := t.TempDir()
path := dir + "/" + name
f, err := os.Create(path)
if err != nil {
panic(err)
}
return f, path
}
func TestIndex_ParseOne(t *testing.T) {
tests := []struct {
name string
pathMaker func(t *testing.T) string
parseOpts index.ParseOpts
want *index.Document
wantErr error
}{
{
"title only header",
func(t *testing.T) string {
f, path := newTestFile(t, "title")
defer f.Close()
f.WriteString("---\ntitle: A title\n---\n")
return path
},
index.ParseOpts{},
&index.Document{Title: "A title"},
nil,
},
{
"tags",
func(t *testing.T) string {
f, path := newTestFile(t, "tags")
defer f.Close()
f.WriteString("---\n")
f.WriteString("tags:\n")
f.WriteString("- a\n")
f.WriteString("- b\n")
f.WriteString("- c\n")
f.WriteString("---\n")
return path
},
index.ParseOpts{},
&index.Document{Tags: []string{"a", "b", "c"}},
nil,
},
{
"date",
func(t *testing.T) string {
f, path := newTestFile(t, "date")
defer f.Close()
f.WriteString("---\ndate: May 1, 2025\n---\n")
return path
},
index.ParseOpts{},
&index.Document{Date: time.Date(2025, time.May, 1, 0, 0, 0, 0, time.UTC)},
nil,
},
{
"single author",
func(t *testing.T) string {
f, path := newTestFile(t, "author")
defer f.Close()
f.WriteString("---\nauthor: Rob Pike\n---\n")
return path
},
index.ParseOpts{},
&index.Document{Authors: []string{"Rob Pike"}},
nil,
},
{
"multi author",
func(t *testing.T) string {
f, path := newTestFile(t, "author")
defer f.Close()
f.WriteString("---\nauthor:\n- Robert Griesemer\n- Rob Pike\n- Ken Thompson\n---\n")
return path
},
index.ParseOpts{},
&index.Document{Authors: []string{"Robert Griesemer", "Rob Pike", "Ken Thompson"}},
nil,
},
{
"meta",
func(t *testing.T) string {
f, path := newTestFile(t, "metadata")
defer f.Close()
f.WriteString("---\n")
f.WriteString("unknownKey: value\n")
f.WriteString("---\n")
return path
},
index.ParseOpts{ParseMeta: true},
&index.Document{OtherMeta: "unknownKey: value\n"},
nil,
},
{
"links",
func(t *testing.T) string {
f, path := newTestFile(t, "links")
defer f.Close()
f.WriteString("---\n")
f.WriteString("title: Link test\n")
f.WriteString("---\n")
f.WriteString(`
Here are some words in a *markdown* file.
In this sentence there is a valid [hyperlink](https://jpappel.xyz).
But in this sentence, the [link]() should not get parsed.
The same is true for the [link]( ) in this sentence.
There must be a nonwhitespace characters for a link to be a [valid link]( destination )
`)
return path
},
index.ParseOpts{ParseLinks: true},
&index.Document{
Title: "Link test",
Links: []string{"https://jpappel.xyz", "destination"},
},
nil,
},
{
"headings",
func(t *testing.T) string {
f, path := newTestFile(t, "headings")
defer f.Close()
f.WriteString("---\n")
f.WriteString("title: Heading test\n")
f.WriteString("---\n")
f.WriteString("# A Heading\n")
f.WriteString("##Another Heading\n")
f.WriteString("### [Linked Heading](but no link parse)\n")
return path
},
index.ParseOpts{ParseHeadings: true},
&index.Document{
Title: "Heading test",
Headings: "# A Heading\n##Another Heading\n### [Linked Heading]\n",
},
nil,
},
{
"linked_headings",
func(t *testing.T) string {
f, path := newTestFile(t, "linked_headings")
defer f.Close()
f.WriteString("---\n")
f.WriteString("title: Linked Heading Test\n")
f.WriteString("---\n")
f.WriteString("#[Top Level Heading](and its link)\n")
f.WriteString("## [Second Level heading]( sometext )\n")
return path
},
index.ParseOpts{ParseLinks: true, ParseHeadings: true},
&index.Document{
Title: "Linked Heading Test",
Headings: "#[Top Level Heading]\n## [Second Level heading]\n",
Links: []string{"and its link", "sometext"},
},
nil,
},
{
"bad tags",
func(t *testing.T) string {
f, path := newTestFile(t, "badtags")
defer f.Close()
f.WriteString("---\n")
f.WriteString("tags:\n- good tag\n-bad tag\n")
f.WriteString("---\n")
return path
},
index.ParseOpts{},
&index.Document{},
index.ErrHeaderParse,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := tt.pathMaker(t)
tt.want.Path = path
got, gotErr := index.ParseDoc(path, tt.parseOpts)
if !errors.Is(gotErr, tt.wantErr) {
t.Errorf("Recieved unexpected error: want %v got %v", tt.wantErr, gotErr)
return
} else if gotErr != nil {
return
}
if !got.Equal(*tt.want) {
t.Error("Recieved document is not equal")
t.Logf("Got = %+v", got)
t.Logf("Want = %+v", tt.want)
}
})
}
}
|