generated from nhcarrigan/template
feat: document all products, write tests (#12)
Node.js CI / Lint and Test (push) Successful in 1m43s
Node.js CI / Lint and Test (push) Successful in 1m43s
### Explanation _No response_ ### Issue _No response_ ### Attestations - [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [x] I have pinned the dependencies to a specific patch version. ### Style - [x] I have run the linter and resolved any errors. - [x] My pull request uses an appropriate title, matching the conventional commit standards. - [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [x] My contribution adds new code, and I have added tests to cover it. - [x] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [x] All new and existing tests pass locally with my changes. - [x] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Reviewed-on: #12 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #12.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { readdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { navigation } from "../src/components/navigation.ts";
|
||||
import matter from "gray-matter";
|
||||
|
||||
type Navigation = typeof navigation;
|
||||
type NavigationItem = Array<{
|
||||
label: string
|
||||
link: string
|
||||
items?: Array<{
|
||||
label: string
|
||||
link: string
|
||||
}>
|
||||
}>
|
||||
|
||||
const excludedFiles = ["intro.mdx", "projects/_template.md"];
|
||||
|
||||
// this should recursively walk the specified directory and return a list of all files, prefixing them with nested paths.
|
||||
// For example, calling walkDirectory() should return "/about/contact.md", "/about/mission.md", "/about/sustainability.md", etc.
|
||||
const walkDirectory = async (directory = join(__dirname, "..", "src", "content", "docs"), prefix = ""): Promise<Array<string>> => {
|
||||
const filesAndDirectories = await readdir(directory, { withFileTypes: true });
|
||||
const pages = [];
|
||||
for (const fileOrDirectory of filesAndDirectories) {
|
||||
if (fileOrDirectory.isDirectory()) {
|
||||
pages.push(...(await walkDirectory(join(directory, fileOrDirectory.name), join(prefix, fileOrDirectory.name))));
|
||||
} else {
|
||||
pages.push(join(prefix, fileOrDirectory.name));
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
const flattenNavigation = (navigation: Navigation | NavigationItem): Array<{ label: string; link: string }> => {
|
||||
const items: Array<{ label: string; link: string }> = [];
|
||||
for (const item of navigation) {
|
||||
if ("items" in item && item.items) {
|
||||
items.push(...flattenNavigation(item.items as NavigationItem));
|
||||
} else {
|
||||
items.push({ label: item.label, link: item.link });
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
describe("navigation", () => {
|
||||
it("should include all pages", async () => {
|
||||
expect.hasAssertions();
|
||||
let pages = await walkDirectory();
|
||||
pages = pages.filter((page) => !excludedFiles.includes(page));
|
||||
const flattenedNavigation = flattenNavigation(navigation);
|
||||
for (const page of pages) {
|
||||
const pageName = page.split(".")[0];
|
||||
const navItem = flattenedNavigation.find((item) => item.link === `/${pageName}`);
|
||||
expect(navItem, `Navigation item not found for page ${page}`).toBeDefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("should use page titles as navigation item labels", async () => {
|
||||
expect.hasAssertions();
|
||||
let pages = await walkDirectory();
|
||||
pages = pages.filter((page) => !excludedFiles.includes(page));
|
||||
const flattenedNavigation = flattenNavigation(navigation);
|
||||
for (const page of pages) {
|
||||
const pageName = page.split(".")[0];
|
||||
const navItem = flattenedNavigation.find((item) => item.link === `/${pageName}`);
|
||||
const pageContent = await readFile(join(__dirname, "..", "src", "content", "docs", page), "utf-8");
|
||||
const { data } = matter(pageContent);
|
||||
expect(navItem?.label, `Navigation item label for ${pageName} is not correct`).toBe(data.title);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { readdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import matter from "gray-matter";
|
||||
|
||||
interface Project {
|
||||
avatar?: string;
|
||||
category: string;
|
||||
description: string;
|
||||
name: string;
|
||||
premium: boolean;
|
||||
url?: string;
|
||||
wip: boolean;
|
||||
}
|
||||
const getProjects = async(): Promise<Array<Project>> => {
|
||||
const request = await fetch("https://data.nhcarrigan.com/projects.json");
|
||||
const data = await request.json();
|
||||
return data as Array<Project>;
|
||||
};
|
||||
|
||||
const projectNameMap: Record<string, string> = {
|
||||
"Naomi's Blog": "blog",
|
||||
"NHCarrigan Documentation": "docs",
|
||||
"Naomi's VSCode Themes": "vscode-themes",
|
||||
"Artists4Palestine Bot": "a4p-bot",
|
||||
"ESLint Config": "eslint-config",
|
||||
"Naomi's Adventure I: An Isekai Story": "naomis-adventure-i",
|
||||
};
|
||||
|
||||
const excludedProjects = new Set<string>([
|
||||
"Translation Service",
|
||||
"Gitea"
|
||||
]);
|
||||
|
||||
const convertTitleCaseToKebabCase = (string_: string): string => {
|
||||
return string_.toLowerCase().replace(/\s+/g, "-").replaceAll(/[:']/g, "");
|
||||
};
|
||||
|
||||
describe("projects documentation", () => {
|
||||
it("should include all public projects", async() => {
|
||||
expect.hasAssertions();
|
||||
const projects = await getProjects();
|
||||
const docs = await readdir(join(__dirname, "..", "src", "content", "docs", "projects"));
|
||||
for (const project of projects) {
|
||||
if (excludedProjects.has(project.name)) {
|
||||
continue;
|
||||
}
|
||||
const page = docs.find((d) => {
|
||||
return project.name in projectNameMap
|
||||
? d.split(".")[0] === projectNameMap[project.name]
|
||||
: d.split(".")[0] === convertTitleCaseToKebabCase(project.name);
|
||||
});
|
||||
expect(page, `Page not found for project ${project.name}`).toBeDefined();
|
||||
const content = await readFile(join(__dirname, "..", "src", "content", "docs", "projects", page!), "utf-8");
|
||||
const { data } = matter(content);
|
||||
expect(data.title, `Title not found for project ${project.name}`).toBeDefined();
|
||||
expect(data.title, `Title for project ${project.name} is not correct`).toBe(project.name);
|
||||
if (project.wip) {
|
||||
continue;
|
||||
}
|
||||
expect(content).not.toMatch(/:::note\nThis section is coming soon!\n:::/);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user