Emile Bangma
2025-07-03 74606a1bd372ff61373b5a41f00edc9c7cb0e9f6
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
import test, { describe, beforeEach } from "node:test"
import assert from "node:assert"
import { FileTrieNode } from "./fileTrie"
import { FullSlug } from "./path"
 
interface TestData {
  title: string
  slug: string
  filePath: string
}
 
describe("FileTrie", () => {
  let trie: FileTrieNode<TestData>
 
  beforeEach(() => {
    trie = new FileTrieNode<TestData>([])
  })
 
  describe("constructor", () => {
    test("should create an empty trie", () => {
      assert.deepStrictEqual(trie.children, [])
      assert.strictEqual(trie.slug, "")
      assert.strictEqual(trie.displayName, "")
      assert.strictEqual(trie.data, null)
    })
 
    test("should set displayName from data title", () => {
      const data = {
        title: "Test Title",
        slug: "test",
        filePath: "test.md",
      }
 
      trie.add(data)
      assert.strictEqual(trie.children[0].displayName, "Test Title")
    })
 
    test("should be able to set displayName", () => {
      const data = {
        title: "Test Title",
        slug: "test",
        filePath: "test.md",
      }
 
      trie.add(data)
      trie.children[0].displayName = "Modified"
      assert.strictEqual(trie.children[0].displayName, "Modified")
    })
  })
 
  describe("add", () => {
    test("should add a file at root level", () => {
      const data = {
        title: "Test",
        slug: "test",
        filePath: "test.md",
      }
 
      trie.add(data)
      assert.strictEqual(trie.children.length, 1)
      assert.strictEqual(trie.children[0].slug, "test")
      assert.strictEqual(trie.children[0].data, data)
    })
 
    test("should handle index files", () => {
      const data = {
        title: "Index",
        slug: "index",
        filePath: "index.md",
      }
 
      trie.add(data)
      assert.strictEqual(trie.data, data)
      assert.strictEqual(trie.children.length, 0)
    })
 
    test("should add nested files", () => {
      const data1 = {
        title: "Nested",
        slug: "folder/test",
        filePath: "folder/test.md",
      }
 
      const data2 = {
        title: "Really nested index",
        slug: "a/b/c/index",
        filePath: "a/b/c/index.md",
      }
 
      trie.add(data1)
      trie.add(data2)
      assert.strictEqual(trie.children.length, 2)
      assert.strictEqual(trie.children[0].slug, "folder/index")
      assert.strictEqual(trie.children[0].children.length, 1)
      assert.strictEqual(trie.children[0].children[0].slug, "folder/test")
      assert.strictEqual(trie.children[0].children[0].data, data1)
 
      assert.strictEqual(trie.children[1].slug, "a/index")
      assert.strictEqual(trie.children[1].children.length, 1)
      assert.strictEqual(trie.children[1].data, null)
 
      assert.strictEqual(trie.children[1].children[0].slug, "a/b/index")
      assert.strictEqual(trie.children[1].children[0].children.length, 1)
      assert.strictEqual(trie.children[1].children[0].data, null)
 
      assert.strictEqual(trie.children[1].children[0].children[0].slug, "a/b/c/index")
      assert.strictEqual(trie.children[1].children[0].children[0].data, data2)
      assert.strictEqual(trie.children[1].children[0].children[0].children.length, 0)
    })
  })
 
  describe("filter", () => {
    test("should filter nodes based on condition", () => {
      const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" }
      const data2 = { title: "Test2", slug: "test2", filePath: "test2.md" }
 
      trie.add(data1)
      trie.add(data2)
 
      trie.filter((node) => node.slug !== "test1")
      assert.strictEqual(trie.children.length, 1)
      assert.strictEqual(trie.children[0].slug, "test2")
    })
  })
 
  describe("map", () => {
    test("should apply function to all nodes", () => {
      const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" }
      const data2 = { title: "Test2", slug: "test2", filePath: "test2.md" }
 
      trie.add(data1)
      trie.add(data2)
 
      trie.map((node) => {
        if (node.data) {
          node.data.title = "Modified"
        }
      })
 
      assert.strictEqual(trie.children[0].displayName, "Modified")
      assert.strictEqual(trie.children[1].displayName, "Modified")
    })
 
    test("map over folders should work", () => {
      const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" }
      const data2 = {
        title: "Test2",
        slug: "a/b-with-space/test2",
        filePath: "a/b with space/test2.md",
      }
 
      trie.add(data1)
      trie.add(data2)
 
      trie.map((node) => {
        if (node.isFolder) {
          node.displayName = `Folder: ${node.displayName}`
        } else {
          node.displayName = `File: ${node.displayName}`
        }
      })
 
      assert.strictEqual(trie.children[0].displayName, "File: Test1")
      assert.strictEqual(trie.children[1].displayName, "Folder: a")
      assert.strictEqual(trie.children[1].children[0].displayName, "Folder: b with space")
      assert.strictEqual(trie.children[1].children[0].children[0].displayName, "File: Test2")
    })
  })
 
  describe("entries", () => {
    test("should return all entries", () => {
      const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" }
      const data2 = {
        title: "Test2",
        slug: "a/b-with-space/test2",
        filePath: "a/b with space/test2.md",
      }
 
      trie.add(data1)
      trie.add(data2)
 
      const entries = trie.entries()
      assert.deepStrictEqual(
        entries.map(([path, node]) => [path, node.data]),
        [
          ["index", trie.data],
          ["test1", data1],
          ["a/index", null],
          ["a/b-with-space/index", null],
          ["a/b-with-space/test2", data2],
        ],
      )
    })
  })
 
  describe("fromEntries", () => {
    test("nested", () => {
      const trie = FileTrieNode.fromEntries([
        ["index" as FullSlug, { title: "Root", slug: "index", filePath: "index.md" }],
        [
          "folder/file1" as FullSlug,
          { title: "File 1", slug: "folder/file1", filePath: "folder/file1.md" },
        ],
        [
          "folder/index" as FullSlug,
          { title: "Folder Index", slug: "folder/index", filePath: "folder/index.md" },
        ],
        [
          "folder/file2" as FullSlug,
          { title: "File 2", slug: "folder/file2", filePath: "folder/file2.md" },
        ],
        [
          "folder/folder2/index" as FullSlug,
          {
            title: "Subfolder Index",
            slug: "folder/folder2/index",
            filePath: "folder/folder2/index.md",
          },
        ],
      ])
 
      assert.strictEqual(trie.children.length, 1)
      assert.strictEqual(trie.children[0].slug, "folder/index")
      assert.strictEqual(trie.children[0].children.length, 3)
      assert.strictEqual(trie.children[0].children[0].slug, "folder/file1")
      assert.strictEqual(trie.children[0].children[1].slug, "folder/file2")
      assert.strictEqual(trie.children[0].children[2].slug, "folder/folder2/index")
      assert.strictEqual(trie.children[0].children[2].children.length, 0)
    })
  })
 
  describe("findNode", () => {
    test("should find root node with empty path", () => {
      const data = { title: "Root", slug: "index", filePath: "index.md" }
      trie.add(data)
      const found = trie.findNode([])
      assert.strictEqual(found, trie)
    })
 
    test("should find node at first level", () => {
      const data = { title: "Test", slug: "test", filePath: "test.md" }
      trie.add(data)
      const found = trie.findNode(["test"])
      assert.strictEqual(found?.data, data)
    })
 
    test("should find nested node", () => {
      const data = {
        title: "Nested",
        slug: "folder/subfolder/test",
        filePath: "folder/subfolder/test.md",
      }
      trie.add(data)
      const found = trie.findNode(["folder", "subfolder", "test"])
      assert.strictEqual(found?.data, data)
 
      // should find the folder and subfolder indexes too
      assert.strictEqual(
        trie.findNode(["folder", "subfolder", "index"]),
        trie.children[0].children[0],
      )
      assert.strictEqual(trie.findNode(["folder", "index"]), trie.children[0])
    })
 
    test("should return undefined for non-existent path", () => {
      const data = { title: "Test", slug: "test", filePath: "test.md" }
      trie.add(data)
      const found = trie.findNode(["nonexistent"])
      assert.strictEqual(found, undefined)
    })
 
    test("should return undefined for partial path", () => {
      const data = {
        title: "Nested",
        slug: "folder/subfolder/test",
        filePath: "folder/subfolder/test.md",
      }
      trie.add(data)
      const found = trie.findNode(["folder"])
      assert.strictEqual(found?.data, null)
    })
  })
 
  describe("getFolderPaths", () => {
    test("should return all folder paths", () => {
      const data1 = {
        title: "Root",
        slug: "index",
        filePath: "index.md",
      }
      const data2 = {
        title: "Test",
        slug: "folder/subfolder/test",
        filePath: "folder/subfolder/test.md",
      }
      const data3 = {
        title: "Folder Index",
        slug: "abc/index",
        filePath: "abc/index.md",
      }
 
      trie.add(data1)
      trie.add(data2)
      trie.add(data3)
      const paths = trie.getFolderPaths()
 
      assert.deepStrictEqual(paths, [
        "index",
        "folder/index",
        "folder/subfolder/index",
        "abc/index",
      ])
    })
  })
 
  describe("sort", () => {
    test("should sort nodes according to sort function", () => {
      const data1 = { title: "A", slug: "a", filePath: "a.md" }
      const data2 = { title: "B", slug: "b", filePath: "b.md" }
      const data3 = { title: "C", slug: "c", filePath: "c.md" }
 
      trie.add(data3)
      trie.add(data1)
      trie.add(data2)
 
      trie.sort((a, b) => a.slug.localeCompare(b.slug))
      assert.deepStrictEqual(
        trie.children.map((n) => n.slug),
        ["a", "b", "c"],
      )
    })
  })
 
  describe("pathToNode", () => {
    test("should return root node for empty path", () => {
      const data = { title: "Root", slug: "index", filePath: "index.md" }
      trie.add(data)
      const path = trie.ancestryChain([])
      assert.deepStrictEqual(path, [trie])
    })
 
    test("should return root node for index path", () => {
      const data = { title: "Root", slug: "index", filePath: "index.md" }
      trie.add(data)
      const path = trie.ancestryChain(["index"])
      assert.deepStrictEqual(path, [trie])
    })
 
    test("should return path to first level node", () => {
      const data = { title: "Test", slug: "test", filePath: "test.md" }
      trie.add(data)
      const path = trie.ancestryChain(["test"])
      assert.deepStrictEqual(path, [trie, trie.children[0]])
    })
 
    test("should return path to nested node", () => {
      const data = {
        title: "Nested",
        slug: "folder/subfolder/test",
        filePath: "folder/subfolder/test.md",
      }
      trie.add(data)
      const path = trie.ancestryChain(["folder", "subfolder", "test"])
      assert.deepStrictEqual(path, [
        trie,
        trie.children[0],
        trie.children[0].children[0],
        trie.children[0].children[0].children[0],
      ])
    })
 
    test("should return undefined for non-existent path", () => {
      const data = { title: "Test", slug: "test", filePath: "test.md" }
      trie.add(data)
      const path = trie.ancestryChain(["nonexistent"])
      assert.strictEqual(path, undefined)
    })
 
    test("should return file data for intermediate folders", () => {
      const data1 = {
        title: "Root",
        slug: "index",
        filePath: "index.md",
      }
      const data2 = {
        title: "Test",
        slug: "folder/subfolder/test",
        filePath: "folder/subfolder/test.md",
      }
      const data3 = {
        title: "Folder Index",
        slug: "folder/index",
        filePath: "folder/index.md",
      }
 
      trie.add(data1)
      trie.add(data2)
      trie.add(data3)
      const path = trie.ancestryChain(["folder", "subfolder"])
      assert.deepStrictEqual(path, [trie, trie.children[0], trie.children[0].children[0]])
      assert.strictEqual(path[1].data, data3)
    })
 
    test("should return path for partial path", () => {
      const data = {
        title: "Nested",
        slug: "folder/subfolder/test",
        filePath: "folder/subfolder/test.md",
      }
      trie.add(data)
      const path = trie.ancestryChain(["folder"])
      assert.deepStrictEqual(path, [trie, trie.children[0]])
    })
  })
})