1
+/**
2
+ * Sidebar nav index tests.
3
+ *
4
+ * Exercises the real docs page tree: `docs_sidebar_click` reports the group,
5
+ * folder and depth from this index, so a traversal bug silently drops pages
6
+ * from analytics rather than breaking the site.
7
+ */
8
+import { describe, expect, test } from 'bun:test';
9
+import { readFile } from 'node:fs/promises';
10
+import { join } from 'node:path';
11
+import { flattenTree } from 'fumadocs-core/page-tree';
12
+
13
+import { buildSidebarNavIndex } from '../../lib/sidebar-nav-index';
14
+import { source } from '../../lib/source';
15
+
16
+const index = buildSidebarNavIndex(source.pageTree);
17
+
18
+describe('buildSidebarNavIndex', () => {
19
+ test('folder children carry their group, folder and depth', () => {
20
+ expect(index['/docs/authentication/white-labeling-authentication']).toEqual({
21
+ group: 'Core concepts',
22
+ folder: 'Authentication',
23
+ depth: 2,
24
+ position: expect.any(Number),
25
+ });
26
+
27
+ expect(index['/docs/extending-sessions/shared-connections']).toEqual({
28
+ group: 'Guides',
29
+ folder: 'Extend sessions',
30
+ depth: 2,
31
+ position: expect.any(Number),
32
+ });
33
+ });
34
+
35
+ test('top-level pages have no folder', () => {
36
+ expect(index['/docs/quickstart']).toEqual({
37
+ group: 'Get Started',
38
+ folder: null,
39
+ depth: 1,
40
+ position: expect.any(Number),
41
+ });
42
+ });
43
+
44
+ test('folder index pages resolve to the folder row', () => {
45
+ expect(index['/docs/authentication']).toEqual({
46
+ group: 'Core concepts',
47
+ folder: 'Authentication',
48
+ depth: 1,
49
+ position: expect.any(Number),
50
+ });
51
+ });
52
+
53
+ test('position increases down a group and resets at the next separator', () => {
54
+ expect(index['/docs/quickstart'].position).toBeGreaterThan(index['/docs'].position);
55
+ expect(index['/docs/how-composio-works'].position).toBe(1);
56
+ });
57
+
58
+ test('every page in the sidebar tree resolves to a non-null group', () => {
59
+ const missing = flattenTree(source.pageTree.children)
60
+ .map(page => page.url)
61
+ .filter(url => index[url]?.group == null);
62
+
63
+ expect(missing).toEqual([]);
64
+ });
65
+
66
+ test('every root meta.json entry that is a page is indexed', async () => {
67
+ const meta = JSON.parse(
68
+ await readFile(join(import.meta.dir, '../../content/docs/meta.json'), 'utf-8')
69
+ ) as { pages: string[] };
70
+
71
+ const indexed = meta.pages
72
+ .filter(entry => !entry.startsWith('---') && entry !== '...')
73
+ .map(entry => (entry === 'index' ? '/docs' : `/docs/${entry}`))
74
+ .filter(url => index[url] != null);
75
+
76
+ // Entries that are folders without an index page have no url of their own.
77
+ expect(indexed).toContain('/docs/quickstart');
78
+ expect(indexed).toContain('/docs/authentication');
79
+ expect(indexed).toContain('/docs/providers');
80
+ });
81
+});