// `umtool new /` and `umtool new --folder `: a // project made inside a folder of the reports tree, its id the path under the // root, routed as walkProjects routes it (2 Nov: `umtool new // emberparadox/kirsche` refused "will not route", and the way in was // REPORTS_DIR=). // // Run with: pnpm test:scripts import { test } from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { projectPlace, scaffoldReportVideo } from "./projects/scaffold.mjs"; import { walkProjects } from "./projects/walk.mjs"; const CLI = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "bin", "umtool.mjs"); test("folder/slug and --folder both make /, an id that routes and lists in its folder", async (t) => { const tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-new-folder-")); t.after(() => rm(tmp, { recursive: true, force: true })); const root = path.join(tmp, "reports"); // The parent: a project already in the folder. const r1 = await scaffoldReportVideo({ root, slug: "emberparadox/in-her-words", channelsDir: path.join(tmp, "channels") }); assert.equal(r1.id, "emberparadox/in-her-words"); assert.equal(r1.folder, "emberparadox"); assert.equal(r1.dir, path.join(root, "emberparadox", "in-her-words")); const r2 = await scaffoldReportVideo({ root, slug: "kirsche", folder: "emberparadox", channelsDir: path.join(tmp, "channels") }); assert.equal(r2.id, "emberparadox/kirsche"); assert.equal(r2.dir, path.join(root, "emberparadox", "kirsche")); const doc = JSON.parse(await readFile(path.join(r2.dir, "video.manifest.json"), "utf8")); assert.equal(doc.slug, "kirsche", "the manifest's slug is the project's own name"); const listed = await walkProjects(root); assert.deepEqual( listed.map((p) => [p.id, p.folder, p.routing]), [ ["emberparadox/in-her-words", "emberparadox", "ok"], ["emberparadox/kirsche", "emberparadox", "ok"], ], ); // A flat slug is as it was. const r3 = await scaffoldReportVideo({ root, slug: "flat-one", channelsDir: path.join(tmp, "channels") }); assert.equal(r3.id, "flat-one"); assert.equal(r3.folder, undefined); await assert.rejects(scaffoldReportVideo({ root, slug: "emberparadox/kirsche", channelsDir: path.join(tmp, "channels") }), /already exists/); }); test("what will not route is refused before anything is written", async (t) => { const tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-new-folder-")); t.after(() => rm(tmp, { recursive: true, force: true })); const root = path.join(tmp, "reports"); await assert.rejects(projectPlace(root, "Kirsche", "emberparadox"), /will not route/); await assert.rejects(projectPlace(root, "kirsche", "../out"), /will not route/); await assert.rejects(projectPlace(root, "kirsche", ".hidden"), /will not route/); await assert.rejects(projectPlace(root, "at/kirsche"), /reserved/); await assert.rejects(projectPlace(root, "a/b/c/d/e"), /levels deep/); // Inside a project: a project is a leaf, so the new one would never be listed. await mkdir(path.join(root, "parent"), { recursive: true }); await writeFile(path.join(root, "parent", "video.manifest.json"), "{}\n"); await assert.rejects(projectPlace(root, "parent/child"), /is a project, not a folder/); assert.deepEqual(await projectPlace(root, "/a/b/"), { id: "a/b", slug: "b", folder: "a" }); assert.deepEqual(await projectPlace(root, "c", "a/b"), { id: "a/b/c", slug: "c", folder: "a/b" }); assert.equal(await stat(path.join(root, "a")).then(() => true, () => false), false); }); test("the CLI: umtool new / and --folder print the project's id in their next steps", async (t) => { const tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-new-folder-")); t.after(() => rm(tmp, { recursive: true, force: true })); const env = { ...process.env, REPORTS_DIR: path.join(tmp, "reports"), CHANNELS_DIR: path.join(tmp, "channels"), UMTOOL_CACHE_DIR: path.join(tmp, "cache") }; const a = JSON.parse(execFileSync(process.execPath, [CLI, "new", "emberparadox/kirsche", "--json"], { env, encoding: "utf8" })); assert.equal(a.id, "emberparadox/kirsche"); const b = execFileSync(process.execPath, [CLI, "new", "kirsche-2", "--folder", "emberparadox"], { env, encoding: "utf8" }); assert.match(b, /umtool check emberparadox\/kirsche-2/); assert.ok((await stat(path.join(tmp, "reports", "emberparadox", "kirsche-2", "video.manifest.json"))).isFile()); });