-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathllms-coverage.test.ts
More file actions
65 lines (53 loc) · 1.8 KB
/
Copy pathllms-coverage.test.ts
File metadata and controls
65 lines (53 loc) · 1.8 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
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
const DOCS_ORIGIN = 'https://docs.xquik.com';
const MARKDOWN_LINK_PATTERN =
/\[[^\]]+\]\(https:\/\/docs\.xquik\.com\/([^\s)]+)\)/gu;
interface NavigationGroup {
readonly anchors?: readonly NavigationItem[];
readonly groups?: readonly NavigationItem[];
readonly pages?: readonly NavigationItem[];
readonly tabs?: readonly NavigationItem[];
}
type NavigationItem = string | NavigationGroup;
interface DocsConfig {
readonly navigation: NavigationGroup;
}
function flattenNavigationPages(item: NavigationItem): readonly string[] {
if (typeof item === 'string') {
return [normalizePagePath(item)];
}
return [
...(item.anchors ?? []),
...(item.groups ?? []),
...(item.pages ?? []),
...(item.tabs ?? []),
].flatMap(flattenNavigationPages);
}
function normalizePagePath(page: string): string {
return page
.replace(/^\/+/u, '')
.replace(/\.mdx?$/u, '')
.replace(/#.*$/u, '');
}
function documentedLlmsPages(source: string): ReadonlySet<string> {
return new Set(
[...source.matchAll(MARKDOWN_LINK_PATTERN)].map((match): string =>
normalizePagePath(match[1] ?? ''),
),
);
}
describe('llms.txt coverage', (): void => {
it('lists every docs.json navigation page as a markdown link', (): void => {
expect.assertions(1);
const docsConfig = JSON.parse(
readFileSync('docs.json', 'utf8'),
) as DocsConfig;
const expectedPages = flattenNavigationPages(docsConfig.navigation);
const actualPages = documentedLlmsPages(readFileSync('llms.txt', 'utf8'));
const missingPages = expectedPages
.filter((page): boolean => !actualPages.has(page))
.map((page): string => `${DOCS_ORIGIN}/${page}`);
expect(missingPages).toStrictEqual([]);
});
});