Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
import fs from "fs";
import path from "path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
// --- Mocks (hoisted above the module-under-test import) ---
class FakeHtml {
constructor(public data: any, public opts: any) {}
}
class FakeMarkdown {
constructor(public data: any) {}
}
class FakeShare {
constructor(public data: any) {}
}
vi.mock("@triliumnext/core/src/services/export/zip/html.js", () => ({ default: FakeHtml }));
vi.mock("@triliumnext/core/src/services/export/zip/markdown.js", () => ({ default: FakeMarkdown }));
vi.mock("./share_theme.js", () => ({ createShareThemeExportProvider: (data: any) => new FakeShare(data) }));
const data = { branch: {} } as any;
/**
* `isDev` in utils.ts is a module-load constant derived from TRILIUM_ENV, so to
* exercise both readContentCss branches we re-import the factory with modules
* reset under each env value (the html/markdown/share mocks are re-applied).
*/
async function importFactory(dev: boolean) {
vi.resetModules();
vi.doMock("@triliumnext/core/src/services/export/zip/html.js", () => ({ default: FakeHtml }));
vi.doMock("@triliumnext/core/src/services/export/zip/markdown.js", () => ({ default: FakeMarkdown }));
vi.doMock("./share_theme.js", () => ({ createShareThemeExportProvider: (data: any) => new FakeShare(data) }));
if (dev) {
vi.stubEnv("TRILIUM_ENV", "dev");
} else {
vi.stubEnv("TRILIUM_ENV", "");
}
return (await import("./factory.js")).serverZipExportProviderFactory;
}
let readFileSyncSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
readFileSyncSpy = vi.spyOn(fs, "readFileSync").mockImplementation(((file: string) =>
(String(file).endsWith("multicolumn.css") ? ".columns {}"
: /content[_-]colors\.css$/.test(String(file)) ? ".colors {}"
: "body { color: red; }")
) as any);
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
});
describe("serverZipExportProviderFactory", () => {
it("builds an HTML provider, reading content CSS from the resource dir in production", async () => {
const factory = await importFactory(false);
const provider = await factory("html", data);
expect(provider).toBeInstanceOf(FakeHtml);
expect((provider as any).opts.contentCss).toBe("body { color: red; }\n.colors {}\n.columns {}");
// production reads the bundled css next to the resource dir
const readPaths = readFileSyncSpy.mock.calls.map(([ file ]) => String(file));
expect(readPaths.map((file) => path.basename(file))).toEqual([
"ckeditor5-content.css", "ckeditor5-content-colors.css", "ckeditor5-multicolumn.css"
]);
expect(readPaths.some((file) => file.includes("node_modules"))).toBe(false);
});
it("builds an HTML provider, resolving content CSS from the ckeditor5 package in dev mode", async () => {
const factory = await importFactory(true);
const provider = await factory("html", data);
const readPaths = readFileSyncSpy.mock.calls.map(([ file ]) => String(file));
expect(readPaths).toHaveLength(3);
expect(readPaths[0]).toContain("ckeditor5-content.css");
// dev resolves the file from the ckeditor5 package (under node_modules),
// unlike the production path which reads from the resource dir.
expect(readPaths[0]).toContain("node_modules");
// The content colors and the multicolumn stylesheet come from the workspace's own editor package.
const theme = path.join("packages", "ckeditor5", "src", "theme");
expect(path.normalize(readPaths[1])).toContain(path.join(theme, "content_colors.css"));
expect(path.normalize(readPaths[2])).toContain(path.join(theme, "multicolumn.css"));
expect(fs.existsSync(readPaths[1])).toBe(true);
expect(fs.existsSync(readPaths[2])).toBe(true);
expect((provider as any).opts.contentCss).toBe("body { color: red; }\n.colors {}\n.columns {}");
});
it("builds a Markdown provider without reading any CSS", async () => {
const factory = await importFactory(false);
const provider = await factory("markdown", data);
expect(provider).toBeInstanceOf(FakeMarkdown);
expect(readFileSyncSpy).not.toHaveBeenCalled();
});
it("builds a Share theme provider", async () => {
const factory = await importFactory(false);
const provider = await factory("share", data);
expect(provider).toBeInstanceOf(FakeShare);
});
it("throws on an unsupported format", async () => {
const factory = await importFactory(false);
await expect(factory("xml" as any, data)).rejects.toThrow(/Unsupported export format/);
});
});