clone

a deno framework for local-first web apps that open instantly, work offline and sync when users choose

fix/175-template-quality-pass
lofi / package / project_layout_doc_test.ts
project_layout_doc_test.ts 1.8K
1import { join, relative } from "node:path";2import { createProject } from "./create_core.ts";34function assert(condition: unknown, message: string): asserts condition {5 if (!condition) throw new Error(message);6}78async function generatedFiles(root: string): Promise<string[]> {9 const files: string[] = [];10 async function visit(directory: string) {11 for await (const entry of Deno.readDir(directory)) {12 const path = join(directory, entry.name);13 if (entry.isDirectory) await visit(path);14 else if (entry.isFile) files.push(relative(root, path).replaceAll("\\", "/"));15 }16 }17 await visit(root);18 return files.sort();19}2021function documentedFiles(markdown: string): string[] {22 const start = "<!-- generated-file-map:start -->";23 const end = "<!-- generated-file-map:end -->";24 const section = markdown.slice(markdown.indexOf(start) + start.length, markdown.indexOf(end));25 assert(markdown.includes(start) && markdown.includes(end), "project map markers are missing");26 return [...section.matchAll(/^\|\s+`([^`]+)`\s+\|/gm)].map((match) => match[1]).sort();27}2829Deno.test("documented project map matches a real generated project", async () => {30 const temporaryRoot = await Deno.makeTempDir({ dir: ".", prefix: ".lofi-layout-doc-test-" });31 try {32 const project = await createProject({ cwd: temporaryRoot, name: "mapped-app" });33 const actual = await generatedFiles(project.destination);34 const markdown = await Deno.readTextFile(35 new URL("../docs/reference/project-layout.md", import.meta.url),36 );37 const documented = documentedFiles(markdown);38 assert(39 JSON.stringify(actual) === JSON.stringify(documented),40 `generated project map drifted\nactual: ${actual.join(", ")}\ndocumented: ${41 documented.join(", ")42 }`,43 );44 } finally {45 await Deno.remove(temporaryRoot, { recursive: true });46 }47});