-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathomnibus.test.ts
More file actions
830 lines (737 loc) · 22.1 KB
/
omnibus.test.ts
File metadata and controls
830 lines (737 loc) · 22.1 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/* This file is a test suite for the documentation. It checks for broken links, missing glossary terms, and other issues.
Before adding to this file, please consider whether the test should be in a different file, closer with the behavior under test. */
import { expect, test } from "bun:test";
import dotenv from "dotenv";
import fs from "fs";
import {
type OpenAPIProperty,
urlForSchema,
} from "./components/ObjectDescription";
import NAVIGATION from "./content/navigation.json";
import matter from "gray-matter";
import OpenAPIEnums from "./lib/openapi/enums.json";
import OpenAPI from "./public/openapi.json";
import REDIRECTS from "./redirects.mjs";
dotenv.config();
const MARKDOC_DIRECTORY = "content/pages";
const VALID_APPLICATION_ROUTES = [
...JSON.parse(
// If this file is out of date, run `mise run //app:generate-files`!
fs.readFileSync("./autogen/author_facing_routes.json", "utf-8"),
),
"settings/integrations/umami",
"accounts/password/reset/",
// Marketing stuff.
"pricing",
"support",
"features/smtp-endpoint",
"features/analytics",
"features/teams",
"features/integrations",
"features/integrations/soundcloud",
"features/integrations/youtube",
"features/integrations/memberful",
"features/integrations/transistor",
"features/integrations/spotify",
"features/integrations/vimeo",
"features/concierge-migration",
"comparison-guides/esps",
"blog/lead-magnets",
"blog/netlify",
"legal/acceptable-use-policy",
"legal/adult-content-policy",
"legal/cookies",
"legal/gdpr-eu-compliance",
"legal/privacy",
"legal/support-policy",
"legal/terms",
"changelog/2025-02-06",
"register",
"login",
// Specific pages.
"el-classico/archive/who-was-telemachus-anyway/",
"cryptography-dispatches",
"occasional-puzzles",
"<yourusername>/",
// Weird edge cases.
"features/markdown",
];
const CHANGELOG_FILE = `api-changelog.mdoc`;
const extractInternalLinks = (content: string): string[] => {
// We look for links in two places:
// 1. Markdown internal links, e.g. [foo](/bar)
// 2. The `relatedPages` field, which is a list of slugs.
const internalLinks = content.match(/\[.*?\]\(.*?\)/g);
const qualifiedInternalLinks =
(internalLinks
?.map((link) => {
const path = link.match(/\[.*?\]\((.*?)\)/)?.[1].trim();
if (path?.startsWith("http")) {
return null;
}
if (path?.startsWith("mailto:")) {
return null;
}
if (path?.startsWith("#")) {
return null;
}
if (path?.startsWith("{")) {
return null;
}
// For now, we'll filter out any images or other non-page links under
// the assumption that they can be caught elsewhere (e.g. the build step.)
if (
path &&
path.match(/\.(png|jpg|jpeg|gif|svg|webp|ico|pdf|css|json)$/)
) {
return null;
}
return path;
})
.filter((link) => link !== null) as string[]) || [];
// We also look for related pages. The frontmatter looks like the following:
// > relatedPages:
// > - foo
// > - bar-baz
// > ---
// And so we should match on `foo` and `bar-baz`.
const relatedPages = content.match(/relatedPages:\s*(\n\s*- .*)+/g);
const qualifiedRelatedPages =
relatedPages?.flatMap((relatedPage) => {
return (
relatedPage
.replace("relatedPages:", "")
.split("\n")
.filter((line) => line.trim().length > 0)
// Remove the `- ` prefix.
.map((line) => line.replace(/- /, "").trim())
// Also remove wrapping quotes, if they exist.
.map((slug) => slug.replace(/'/g, ""))
// Ignore any empty lists (`[]`)
.filter((slug) => slug !== "[]")
.map((slug) => "/" + slug.trim())
);
}) || [];
return [...qualifiedInternalLinks, ...qualifiedRelatedPages];
};
const extractApplicationLinks = (content: string): string[] => {
const links = (
content.match(/\(https:\/\/buttondown.com\/(.*?)\)/g) || []
).map((link) => link.replace("(", "").replace(")", ""));
return links;
};
const extractLocalMarkdownImageLinks = (content: string): string[] => {
return [...content.matchAll(/!\[[^\]]*\]\(([^)]+)\)/g)]
.map((match) => match[1].trim().split(/\s+"/)[0])
.filter((url) => {
return (
url.startsWith("/") &&
!url.startsWith("//") &&
!url.startsWith("/http")
);
})
.map((url) => url.replace(/[?#].*$/, ""));
};
const FILENAME_TO_RAW_CONTENT = fs.readdirSync(MARKDOC_DIRECTORY).reduce(
(acc, filename) => {
const fullyQualifiedFilename = `${MARKDOC_DIRECTORY}/${filename}`;
acc[filename] = fs.readFileSync(fullyQualifiedFilename, "utf-8");
return acc;
},
{} as { [filename: string]: string },
);
const FILENAME_TO_INTERNAL_LINKS = Object.entries(
FILENAME_TO_RAW_CONTENT,
).reduce(
(acc, [filename, content]) => {
acc[filename] = extractInternalLinks(content);
return acc;
},
{} as { [filename: string]: string[] },
);
const FILENAME_TO_APPLICATION_LINKS = Object.entries(
FILENAME_TO_RAW_CONTENT,
).reduce(
(acc, [filename, content]) => {
acc[filename] = extractApplicationLinks(content);
return acc;
},
{} as { [filename: string]: string[] },
);
Object.entries(FILENAME_TO_APPLICATION_LINKS).forEach(([filename, routes]) => {
if (routes.length === 0) {
return;
}
test(filename + " only has valid application routes", () => {
routes.forEach((route) => {
const stripped = route.replace("https://buttondown.com/", "").split("?")[0];
expect(VALID_APPLICATION_ROUTES).toContain(stripped);
});
});
});
const extractDemoIframePaths = (content: string): string[] => {
return [...content.matchAll(/{% iframe src="https:\/\/demo\.buttondown\.com\/([^"?]*)/g)]
.map((m) => m[1].replace(/\/$/, ""));
};
const routeToRegex = (route: string): RegExp => {
const pattern = route
.replace(/<str:[^>]+>/g, "[^/]+")
.replace(/<uuid:[^>]+>/g, "[^/]+")
.replace(/<int:[^>]+>/g, "\\d+");
return new RegExp(`^${pattern}$`);
};
const APPLICATION_ROUTE_REGEXES = VALID_APPLICATION_ROUTES.map(routeToRegex);
const DEMO_NEWSLETTER_USERNAMES = JSON.parse(
fs.readFileSync("./autogen/demo_newsletter_usernames.json", "utf-8"),
) as string[];
const isValidDemoIframePath = (path: string): boolean => {
const firstSegment = path.split("/")[0];
if (DEMO_NEWSLETTER_USERNAMES.includes(firstSegment)) {
return true;
}
return APPLICATION_ROUTE_REGEXES.some((regex) => regex.test(path));
};
const FILENAME_TO_DEMO_IFRAME_PATHS = Object.entries(
FILENAME_TO_RAW_CONTENT,
).reduce(
(acc, [filename, content]) => {
acc[filename] = extractDemoIframePaths(content);
return acc;
},
{} as { [filename: string]: string[] },
);
Object.entries(FILENAME_TO_DEMO_IFRAME_PATHS).forEach(([filename, paths]) => {
if (paths.length === 0) {
return;
}
test(filename + " only has valid demo iframe paths", () => {
paths.forEach((path) => {
expect(
isValidDemoIframePath(path),
`Invalid demo iframe path "${path}" in ${filename}. If this is a subscriber-facing page, add the username to the demo bootstrap data. Otherwise, check that the route exists.`,
).toBeTruthy();
});
});
});
const VALID_INTERNAL_LINKS_THAT_ARE_NOT_BACKED_BY_MDOC = [
"/rss/api-changelog",
"$THREAD_1_LINK",
"$THREAD_2_LINK",
"$THREAD_3_LINK",
"$WORKSPACE_URL",
"url",
];
function slugify(text: string): string {
return (text || "")
.toString()
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^\w-]+/g, "")
.replace(/--+/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "");
}
function getAnchorsFromMdoc(filePath: string): string[] {
if (!fs.existsSync(filePath)) {
return [];
}
const content = fs.readFileSync(filePath, "utf-8");
const frontmatter = matter(content).data;
const slugs: string[] = [];
// Markdown headings.
const headingRegex = /^#{1,6}\s+(.+)$/gm;
let match;
while ((match = headingRegex.exec(content)) !== null) {
slugs.push(slugify(match[1]));
}
// Enum values (e.g. SubscriberType, AutomationActionType).
if (frontmatter.enum) {
const enumData =
OpenAPIEnums[frontmatter.enum as keyof typeof OpenAPIEnums];
if (enumData) {
slugs.push(...Object.keys(enumData));
}
}
// Schema property names (e.g. Email schema has "slug", "subject", etc.).
if (frontmatter.schema) {
const schema =
OpenAPI.components.schemas[
frontmatter.schema as keyof typeof OpenAPI.components.schemas
];
if (schema && "properties" in schema) {
slugs.push(...Object.keys(schema.properties));
}
}
// Endpoint pages get a dynamic "Error codes" section.
if (frontmatter.endpoint && frontmatter.method) {
slugs.push("error-codes");
}
return slugs;
}
function getHeadingsFromMdoc(content: string): Array<{
level: number;
text: string;
lineNumber: number;
}> {
const lines = content.split("\n");
const headings: Array<{ level: number; text: string; lineNumber: number }> = [];
let inCodeFence = false;
for (let index = 0; index < lines.length; index++) {
const line = lines[index];
const trimmedLine = line.trim();
if (trimmedLine.startsWith("```")) {
inCodeFence = !inCodeFence;
continue;
}
if (inCodeFence) {
continue;
}
const match = line.match(/^(#{1,6})\s+(.+)$/);
if (!match) {
continue;
}
headings.push({
level: match[1].length,
text: match[2].trim(),
lineNumber: index + 1,
});
}
return headings;
}
const isInternalURLValid = (url: string) => {
if (VALID_INTERNAL_LINKS_THAT_ARE_NOT_BACKED_BY_MDOC.includes(url)) {
return true;
}
const fragmentMatch = url.match(/#(.+)$/);
const fragment = fragmentMatch ? fragmentMatch[1] : null;
const urlWithFragmentRemoved = url.replace(/#.*$/, "");
const filePath = `${MARKDOC_DIRECTORY}/${urlWithFragmentRemoved}.mdoc`;
if (!fs.existsSync(filePath)) {
return false;
}
if (fragment && fragment !== "faqs") {
const validSlugs = getAnchorsFromMdoc(filePath);
return validSlugs.includes(fragment);
}
return true;
};
REDIRECTS.forEach(({ source, destination }) => {
// We only care about relative links.
if (destination.startsWith("http")) {
return;
}
test(`Redirect from "${source}" to "${destination}" is valid`, () => {
expect(
isInternalURLValid(destination),
`Redirect from "${source}" to "${destination}" does not exist.`,
).toBeTruthy();
});
});
Object.entries(FILENAME_TO_RAW_CONTENT).forEach(([filename, content]) => {
test(`${filename} has valid heading hierarchy`, () => {
const headings = getHeadingsFromMdoc(content);
if (headings.length === 0) {
return;
}
const firstHeading = headings[0];
expect(
firstHeading.level,
`${filename} should start headings at H2, but found H${firstHeading.level} ("${firstHeading.text}") on line ${firstHeading.lineNumber}`,
).toBe(2);
for (let i = 1; i < headings.length; i++) {
const previous = headings[i - 1];
const current = headings[i];
const isInvalidJump = current.level > previous.level + 1;
expect(
isInvalidJump,
`${filename} has an invalid heading jump from H${previous.level} ("${previous.text}", line ${previous.lineNumber}) to H${current.level} ("${current.text}", line ${current.lineNumber}).`,
).toBe(false);
}
});
});
// Test that all CSS files in subscriber_facing_styles are not empty
const SUBSCRIBER_FACING_STYLES_DIRECTORY = "public/subscriber_facing_styles";
// Make sure all mdoc files with internal links are valid.
Object.entries(FILENAME_TO_INTERNAL_LINKS).forEach(
([filename, internalLinks]) => {
test("Doc page has valid filename", () => {
expect(filename.endsWith(".mdoc")).toBeTruthy();
expect(filename.toLowerCase()).toBe(filename);
});
internalLinks.forEach((outboundPath) => {
test(`Internal link from "${filename}" to "${outboundPath}" is valid`, () => {
expect(
isInternalURLValid(outboundPath),
`Internal link from "${filename}" to "${outboundPath}" does not exist.`,
).toBeTruthy();
});
});
const CSS_FILES = fs
.readdirSync(SUBSCRIBER_FACING_STYLES_DIRECTORY)
.filter((filename) => filename.endsWith(".css"));
CSS_FILES.forEach((filename) => {
test(`${filename} in subscriber_facing_styles is not empty`, () => {
const content = fs.readFileSync(
`${SUBSCRIBER_FACING_STYLES_DIRECTORY}/${filename}`,
"utf-8",
);
expect(content.trim().length).toBeGreaterThan(0);
});
});
// Make sure that all changelog posts (which begin with `/api-changelog-`) are linked in the main changelog file
const isChangelogPage = filename.startsWith("api-changelog-");
if (isChangelogPage) {
test("Changelog contains link to " + filename, () => {
const changelogLinks = FILENAME_TO_INTERNAL_LINKS[CHANGELOG_FILE];
const references = Object.entries(changelogLinks).filter(([_, links]) =>
links.includes("/" + filename.replace(".mdoc", "")),
);
expect(references.length).toBeGreaterThanOrEqual(1);
});
}
},
);
const MAXIMUM_TITLE_LENGTH = 60;
// Terms that should be capitalized in a specific way
const CAPITALIZATION_EXCEPTIONS = [
"A/A",
"A/B",
"Alpine.js",
"AMP",
"API",
"AWeber",
"Benchmark",
"BIMI",
"Buttondown",
"CAC",
"COI",
"CAN-SPAM",
"Campaign Monitor",
"CAPTCHA",
"CFBL",
"CLI",
"ConvertKit",
"Constant Contact",
"CORS",
"CPM",
"CSS",
"CSV",
"CTA",
"daisyUI",
"DKIM",
"DMARC",
"DNS",
"DreamHost",
"EmailOctopus",
"ESP",
"EU",
"Fancy mode",
"FAQ",
"FeedBlitz",
"GDPR",
"Gmail",
"Google Workspace",
"H1",
"HTML",
"I",
"IP",
"JAMStack",
"JSON",
"JSON-LD",
"KYC",
"LaTeX",
"LinkedIn",
"MailerLite",
"Mantine",
"Modern template",
"Monthly Recurring Revenue",
"oEmbed",
"OpenAPI",
"POSSE",
"Precedence: Bulk",
"Promotions",
"REST",
"RSS",
"SEO",
"Shopify",
"SimpleAnalytics",
"SMTP",
"SpamAssassin",
"SPF",
"Stripe",
"UGC",
"URL",
"URLs",
"UTM",
"WordPress",
"WYSIWYG",
];
// Make sure that there are no broken video links.
Object.entries(FILENAME_TO_RAW_CONTENT).forEach(([filename, content]) => {
test(filename + " has only valid video links", () => {
const matches = content.matchAll(/{% video file="(.*?)" \/%}/g);
for (const match of matches) {
const path = match[1];
expect(fs.existsSync(`public/${path}`)).toBeTruthy();
}
});
test(filename + " has a recommended title length ", () => {
const title = content.match(/title: (.*)/)?.[1];
expect(title?.length).toBeLessThanOrEqual(MAXIMUM_TITLE_LENGTH);
});
test(filename + " does not contain 'Missing image' text", () => {
expect(content).not.toContain("Missing image");
});
test(filename + " does not link to docs.buttondown.com", () => {
expect(content).not.toContain("(https://docs.buttondown.com/");
});
test(filename + " has a sentence-case title", () => {
const titleMatch = content.match(/title: (.+?)\s*$/m);
if (!titleMatch) return;
const originalTitle = titleMatch[1].replace(/^["']|["']$/g, "");
// Skip date-only titles (e.g., "2025-01-02")
if (/^\d{4}-\d{2}-\d{2}$/.test(originalTitle)) return;
// Skip code/variable names (e.g., "email.subject", "subscribe_form")
if (
/^[a-z_]+(\.[a-z_]+)+$/.test(originalTitle) ||
/^[a-z_]+_[a-z_]+$/.test(originalTitle)
)
return;
let titleWithoutExceptions = originalTitle;
CAPITALIZATION_EXCEPTIONS.sort((a, b) => b.length - a.length).forEach(
(exception) => {
const escapedException = exception.replace(
/[.*+?^${}()|[\]\\]/g,
"\\$&",
);
titleWithoutExceptions = titleWithoutExceptions.replace(
new RegExp(`\\b${escapedException}\\b`, "g"),
"",
);
},
);
const removeCapitalFirstLetter = (str: string): string => {
return str.replace(/^[A-Z]/, "");
};
const isSentenceCased = (str: string): boolean => {
const sentences = str.split(".").map((sentence) => sentence.trim());
const sentencesWithoutCapitalFirstLetter = sentences.map(
removeCapitalFirstLetter,
);
return sentencesWithoutCapitalFirstLetter.every((sentence) => {
return sentence === sentence.toLowerCase();
});
};
expect(
isSentenceCased(titleWithoutExceptions),
`Title "${originalTitle}" should be sentence case, got "${titleWithoutExceptions}"`,
).toBe(true);
});
});
// Make sure all schemas with $ref in properties have URLs.
Object.entries(FILENAME_TO_RAW_CONTENT).forEach(([filename, content]) => {
// Check for the `schema` key in front matter.
const schemaNameLine = content.match(/schema: (.*)/);
if (!schemaNameLine) {
return;
}
const schemaName = schemaNameLine[1];
const schema =
OpenAPI.components.schemas[
schemaName as keyof typeof OpenAPI.components.schemas
];
// @ts-ignore
const refs = Object.values(schema.properties).filter(
(property) => "$ref" in (property as OpenAPIProperty),
);
refs.forEach((ref) => {
// @ts-ignore
test(`${ref.$ref} (referenced by ${filename}) has a URL in the schema`, () => {
expect(
// @ts-ignore
urlForSchema((ref as OpenAPIProperty).$ref as string),
// @ts-ignore
`Reference to ${
// @ts-ignore
(ref as OpenAPIProperty).$ref
} in ${filename} does not have a URL in the schema.`,
).toBeTruthy();
});
});
});
// Make sure all mdoc files are in the navigation data.
fs.readdirSync(MARKDOC_DIRECTORY).forEach((filename) => {
const slugs = Object.values(NAVIGATION).flatMap((section) =>
section.flatMap((subsection) => subsection.items.map((item) => item.value)),
);
test(filename + " is in the nav", () => {
expect(slugs.includes(filename.replace(".mdoc", ""))).toBeTruthy();
});
test(filename + " only appears once in the nav", () => {
expect(
slugs.filter((slug) => slug === filename.replace(".mdoc", "")).length,
).toBe(1);
});
});
// Make sure that the navigation doesn't reference any pages that don't exist.
Object.values(NAVIGATION).forEach((section) => {
section.forEach((subsection) => {
subsection.items.forEach((item) => {
if (item.discriminant === "page") {
test(`${item.value} exists`, () => {
expect(
fs.existsSync(`content/pages/${item.value}.mdoc`),
).toBeTruthy();
});
}
});
});
});
const IMAGE_SUFFIXES = [
".png",
".jpg",
".jpeg",
".gif",
".svg",
".webp",
];
const IMAGE_DIRECTORY = "public/images";
const ALL_IMAGES = fs
.readdirSync(IMAGE_DIRECTORY, {
recursive: true,
encoding: "utf-8",
})
.filter((filename) =>
IMAGE_SUFFIXES.some((suffix) => filename.endsWith(suffix)),
);
ALL_IMAGES.forEach((filename) => {
test(filename + " is under 1MB", () => {
const image = fs.readFileSync(`${IMAGE_DIRECTORY}/${filename}`);
expect(image.length).toBeLessThan(1024 * 1024);
});
test(filename + " is referenced by at least one page", () => {
const references = Object.entries(FILENAME_TO_RAW_CONTENT).filter(
([_, content]) => content.includes(filename),
);
expect(references.length).toBeGreaterThan(0);
});
});
Object.entries(FILENAME_TO_RAW_CONTENT).forEach(([filename, content]) => {
test(`${filename} has valid local markdown image paths`, () => {
const imageLinks = extractLocalMarkdownImageLinks(content);
imageLinks.forEach((imagePath) => {
const relativePath = imagePath.replace(/^\/images\//, "");
expect(
ALL_IMAGES,
`Missing image file: ${imagePath} (referenced by ${filename})`,
).toContain(relativePath);
});
});
});
// Make sure all API endpoints are referenced by at least one page.
// Ideally, we'd use the `operationID` to reference the page, but for boring
// tech debt reasons we don't actually use that within the codebase so we base it
// off of the path & the method.
const API_ENDPOINTS = Object.entries(OpenAPI.paths).flatMap(([path, method]) =>
Object.keys(method).map((operation) => {
return {
path, // e.g. "/exports"
operation, // e.g. "post"
};
}),
);
const FILENAME_TO_FRONTMATTER = Object.entries(FILENAME_TO_RAW_CONTENT).reduce(
(acc, [filename, content]) => {
acc[filename] = matter(content).data;
return acc;
},
{} as { [filename: string]: any },
);
// We should try not to add any new un-documented API endpoints,
// and in fact burn down this list over time.
const UNDOCUMENTED_API_ENDPOINTS = [
{
path: "/advertising_units",
operation: "get",
},
{
path: "/advertising_units",
operation: "post",
},
{
path: "/advertising_units/slots/{id}",
operation: "patch",
},
{
path: "/advertising_units/{id}",
operation: "patch",
},
{
path: "/advertising_units/{id}",
operation: "delete",
},
{
path: "/forms",
operation: "post",
},
{
path: "/forms",
operation: "get",
},
{
path: "/forms/{id}",
operation: "get",
},
{
path: "/forms/{id}",
operation: "patch",
},
{
path: "/forms/{id}",
operation: "delete",
},
{
path: "/books",
operation: "post",
},
{
path: "/books",
operation: "get",
},
{
path: "/books/{id}",
operation: "get",
},
{
path: "/books/{id}",
operation: "patch",
},
{
path: "/books/{id}",
operation: "delete",
},
{
path: "/emails/{id}/renders",
operation: "get",
},
];
API_ENDPOINTS.filter(
(endpoint) =>
!UNDOCUMENTED_API_ENDPOINTS.map((e) => JSON.stringify(e)).includes(
JSON.stringify(endpoint),
),
)
.filter((endpoint) => !endpoint.path.startsWith("/public"))
.forEach((endpoint) => {
test(
JSON.stringify(endpoint) + " is referenced by at least one page",
() => {
const relevantPage = Object.entries(FILENAME_TO_FRONTMATTER).find(
([filename, frontmatter]) =>
frontmatter.endpoint === endpoint.path &&
frontmatter.method === endpoint.operation,
);
expect(relevantPage).toBeDefined();
},
);
});