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

@ -8,37 +8,22 @@
// - idempotency (original .md untouched)
// Builds src/main/migration.ts with esbuild (no Electron needed).
// Run with: node tests/run-migration-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 out = path.join(os.tmpdir(), `folio-migration-${process.pid}.mjs`);
const entry = path.resolve(".folio-mig-test-entry.ts");
fs.writeFileSync(
entry,
`
const { mod, cleanupBundle } = await bundleForTest({
name: "folio-migration",
entrySource: `
export { migrateChapterToNative, migrateBookToNative, isNativeChapter } from "./src/main/migration.ts";
export { createBook, loadBook, saveMeta } from "./src/main/project.ts";
export { setChapterContent, getChapterContent } from "./src/main/chapters.ts";
export { markdownToNative } from "./src/main/markdown-to-native.ts";
export { parseChapter, serializeChapter, migrateDoc, getChapterDoc, nativeChapterFile, CURRENT_DOC_SCHEMA_VERSION, isValidDoc } from "./src/main/native-doc.ts";
export { getRecoveryRoot, _resetSessionBackupGuard } from "./src/main/backup.ts";
`
);
await esbuild.build({
entryPoints: [entry],
bundle: true,
platform: "node",
format: "esm",
outfile: out,
write: true,
logLevel: "silent",
`,
});
fs.rmSync(entry, { force: true });
const mod = await import(url.pathToFileURL(out).href);
const {
createBook,
setChapterContent,
@ -291,7 +276,7 @@ ok(
// ---------------------------------------------------------------------------
// Cleanup
// ---------------------------------------------------------------------------
fs.rmSync(out, { force: true });
cleanupBundle();
fs.rmSync(book, { recursive: true, force: true });
for (const d of backups) {
fs.rmSync(path.join(getRecoveryRoot(), d), { recursive: true, force: true });