refactor(tests): centralize test harness boilerplate into bundle.mjs helper

This commit is contained in:
avi 2026-08-24 21:15:06 -05:00
commit 43a03272bf
9 changed files with 107 additions and 150 deletions

View file

@ -1,34 +1,18 @@
// Standalone test for chapter CRUD logic.
// Run with: node tests/run-chapters-test.mjs
import esbuild from "esbuild";
import fs from "fs";
import os from "os";
import path from "path";
import url from "url";
import { bundleForTest } from "./helpers/bundle.mjs";
const projOut = path.join(os.tmpdir(), `folio-project-${process.pid}.mjs`);
const chapOut = path.join(os.tmpdir(), `folio-chapters-${process.pid}.mjs`);
await esbuild.build({
entryPoints: [path.resolve("src/main/project.ts")],
bundle: true,
platform: "node",
format: "esm",
outfile: projOut,
write: true,
logLevel: "silent",
const { mod: project, cleanupBundle: cleanupProj } = await bundleForTest({
name: "folio-project",
entryPoints: ["src/main/project.ts"],
});
await esbuild.build({
entryPoints: [path.resolve("src/main/chapters.ts")],
bundle: true,
platform: "node",
format: "esm",
outfile: chapOut,
write: true,
logLevel: "silent",
const { mod: chapters, cleanupBundle: cleanupChap } = await bundleForTest({
name: "folio-chapters",
entryPoints: ["src/main/chapters.ts"],
});
const project = await import(url.pathToFileURL(projOut).href);
const chapters = await import(url.pathToFileURL(chapOut).href);
const { createBook, loadBook, saveMeta, hasBook } = project;
const {
listChaptersInOrder,
@ -145,18 +129,12 @@ ok(JSON.stringify(moveInOrder(["a", "b", "c", "d"], 3, 0)) === JSON.stringify(["
ok(JSON.stringify(moveInOrder(["a", "b", "c", "d"], 1, 1)) === JSON.stringify(["a", "b", "c", "d"]), "no-op on same index");
// ---- export (combineMarkdown + buildZip) ----
const exportOut = path.join(os.tmpdir(), `folio-export-${process.pid}.cjs`);
await esbuild.build({
entryPoints: [path.resolve("src/main/export.ts")],
bundle: true,
platform: "node",
const { mod: exportMod, cleanupBundle: cleanupExport } = await bundleForTest({
name: "folio-export",
entryPoints: ["src/main/export.ts"],
format: "cjs",
outfile: exportOut,
write: true,
logLevel: "silent",
});
const exportMod = (await import(url.pathToFileURL(exportOut).href)).default;
const { combineMarkdown, buildZip } = exportMod;
const { combineMarkdown, buildZip } = exportMod.default;
const b1 = createChapter(dir, "Intro");
setChapterContent(dir, b1.id, "# Intro\n\nHello world\n\n- a\n- b");
@ -177,10 +155,10 @@ ok(zip.includes("title_page.md"), "zip contains title_page.md");
ok(zip.includes("manifest.json"), "zip contains manifest.json");
ok(zip.includes("chapters"), "zip contains a chapters/ folder");
ok(zip.slice(0, 2).toString("ascii") === "PK", "zip starts with PK signature");
fs.rmSync(exportOut, { force: true });
cleanupExport();
fs.rmSync(projOut, { force: true });
fs.rmSync(chapOut, { force: true });
cleanupProj();
cleanupChap();
fs.rmSync(dir, { recursive: true, force: true });
console.log("\nChapter tests: " + (failures === 0 ? "ALL PASSED" : failures + " FAILED"));