const fs = require("fs"); const path = require("path"); const stub = require("./nova-stub.js"); const EXT = path.join(__dirname, ".."); const ROOT = path.join(__dirname, ".tmp", "generate"); fs.rmSync(ROOT, { recursive: true, force: true }); fs.mkdirSync(path.join(ROOT, "docs"), { recursive: true }); fs.writeFileSync(path.join(ROOT, "docs", "one.md"), "# One\n\n```mermaid\ngraph TD\nA-->B\n```\n"); fs.writeFileSync(path.join(ROOT, "docs", "two.md"), "# Two\n"); const nova = stub.build({ root: ROOT, extensionPath: EXT }); global.nova = nova; global.Disposable = stub.Disposable; global.CompositeDisposable = stub.CompositeDisposable; global.Range = stub.Range; const preview = require(path.join(EXT, "Scripts", "preview.js")); const OUT = path.join(ROOT, ".nova", "MermaidPreview"); const results = []; function check(name, condition) { results.push([condition ? "PASS" : "FAIL", name]); } // -- generate ------------------------------------------------------------ const one = { path: path.join(ROOT, "docs", "one.md"), mode: "markdown", editor: new stub.FakeEditor(path.join(ROOT, "docs", "one.md"), fs.readFileSync(path.join(ROOT, "docs", "one.md"), "utf8")) }; const two = { path: path.join(ROOT, "docs", "two.md"), mode: "markdown", editor: new stub.FakeEditor(path.join(ROOT, "docs", "two.md"), fs.readFileSync(path.join(ROOT, "docs", "two.md"), "utf8")) }; const pathOne = preview.generate(one); check("generate writes a page", fs.existsSync(pathOne)); check("assets installed", fs.existsSync(path.join(OUT, "assets", "vendor", "mermaid.min.js"))); check("stylesheet installed", fs.existsSync(path.join(OUT, "assets", "preview.css"))); check("page references relative assets", fs.readFileSync(pathOne, "utf8").includes('src="assets/vendor/mermaid.min.js"')); check("page embeds the markdown", fs.readFileSync(pathOne, "utf8").includes("graph TD")); const mtime = fs.statSync(path.join(OUT, "assets", "preview.css")).mtimeMs; const pathTwo = preview.generate(two); check("second page has its own filename", pathOne !== pathTwo && fs.existsSync(pathTwo)); check("assets not reinstalled when current", fs.statSync(path.join(OUT, "assets", "preview.css")).mtimeMs === mtime); // -- discard ------------------------------------------------------------- preview.discard(pathOne); check("discard removes that page", !fs.existsSync(pathOne)); check("discard keeps the other page", fs.existsSync(pathTwo)); check("discard keeps assets while a page remains", fs.existsSync(path.join(OUT, "assets", "preview.css"))); preview.discard(pathTwo); check("last discard removes the directory", !fs.existsSync(OUT)); check("workspace otherwise untouched", fs.existsSync(path.join(ROOT, "docs", "one.md"))); // -- discard is safe to repeat ------------------------------------------- let threw = false; try { preview.discard(pathTwo); } catch (e) { threw = true; } check("discarding twice does not throw", !threw); for (const [status, name] of results) console.log(`${status} ${name}`); process.exitCode = results.some(([s]) => s === "FAIL") ? 1 : 0;