mirror of
https://github.com/C4illin/ConvertX.git
synced 2026-03-03 03:47:02 +00:00
test: add tests for dasel, pandoc and vtracer
This commit is contained in:
72
tests/converters/dasel.test.ts
Normal file
72
tests/converters/dasel.test.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import fs from "fs";
|
||||
import { beforeEach, afterEach, expect, test, describe } from "bun:test";
|
||||
import { convert } from "../../src/converters/dasel";
|
||||
import type { ExecFileFn } from "../../src/converters/types";
|
||||
|
||||
const originalWriteFile = fs.writeFile;
|
||||
|
||||
describe("convert", () => {
|
||||
let mockExecFile: ExecFileFn;
|
||||
|
||||
beforeEach(() => {
|
||||
// mock fs.writeFile
|
||||
// @ts-expect-error: property __promisify__ is missing
|
||||
fs.writeFile = (path, data, cb) => cb(null);
|
||||
// mock execFile
|
||||
mockExecFile = (cmd, args, callback) => callback(null, "output-data", "");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// reset fs.writeFile
|
||||
fs.writeFile = originalWriteFile;
|
||||
});
|
||||
|
||||
test("should call dasel with correct arguments and write output", async () => {
|
||||
let calledArgs: Parameters<ExecFileFn> = ["", [], () => {}];
|
||||
mockExecFile = (cmd, args, callback) => {
|
||||
calledArgs = [cmd, args, callback];
|
||||
callback(null, "output-data", "");
|
||||
};
|
||||
|
||||
let writeFileCalled = false;
|
||||
// @ts-expect-error: property __promisify__ is missing
|
||||
fs.writeFile = (path, data, cb) => {
|
||||
writeFileCalled = true;
|
||||
expect(path).toBe("output.json");
|
||||
expect(data).toBe("output-data");
|
||||
// @ts-expect-error: could not be callable with null
|
||||
cb(null);
|
||||
};
|
||||
|
||||
const result = await convert(
|
||||
"input.yaml",
|
||||
"yaml",
|
||||
"json",
|
||||
"output.json",
|
||||
undefined,
|
||||
mockExecFile,
|
||||
);
|
||||
|
||||
expect(calledArgs[0]).toBe("dasel");
|
||||
expect(calledArgs[1]).toEqual(["--file", "input.yaml", "--read", "yaml", "--write", "json"]);
|
||||
expect(writeFileCalled).toBe(true);
|
||||
expect(result).toBe("Done");
|
||||
});
|
||||
|
||||
test("should reject if execFile returns an error", async () => {
|
||||
mockExecFile = (cmd, args, callback) => callback(new Error("fail"), "", "");
|
||||
expect(
|
||||
convert("input.yaml", "yaml", "json", "output.json", undefined, mockExecFile),
|
||||
).rejects.toMatch(/error: Error: fail/);
|
||||
});
|
||||
|
||||
test("should reject if writeFile fails", async () => {
|
||||
// @ts-expect-error: property __promisify__ is missing
|
||||
fs.writeFile = (path, data, cb) => cb(new Error("write fail"));
|
||||
expect(
|
||||
convert("input.yaml", "yaml", "json", "output.json", undefined, (cmd, args, cb) =>
|
||||
cb(null, "output-data", ""),
|
||||
),
|
||||
).rejects.toMatch(/Failed to write output/);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { afterEach, beforeEach, expect, test } from "bun:test";
|
||||
import { convert } from "../../src/converters/libreoffice"; // ← adjust
|
||||
import type { ExecFileFn } from "../../src/converters/types"; // ← adjust
|
||||
import { convert } from "../../src/converters/libreoffice";
|
||||
import type { ExecFileFn } from "../../src/converters/types";
|
||||
|
||||
function requireDefined<T>(value: T, msg: string): NonNullable<T> {
|
||||
if (value === undefined || value === null) throw new Error(msg);
|
||||
|
||||
66
tests/converters/pandoc.test.ts
Normal file
66
tests/converters/pandoc.test.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { beforeEach, expect, test, describe } from "bun:test";
|
||||
import { convert } from "../../src/converters/pandoc";
|
||||
import type { ExecFileFn } from "../../src/converters/types";
|
||||
|
||||
describe("convert", () => {
|
||||
let mockExecFile: ExecFileFn;
|
||||
|
||||
beforeEach(() => {
|
||||
mockExecFile = (cmd, args, callback) => callback(null, "output-data", "");
|
||||
});
|
||||
|
||||
test("should call pandoc with correct arguments (normal)", async () => {
|
||||
let calledArgs: Parameters<ExecFileFn> = ["", [], () => {}];
|
||||
mockExecFile = (cmd, args, callback) => {
|
||||
calledArgs = [cmd, args, callback];
|
||||
callback(null, "output-data", "");
|
||||
};
|
||||
|
||||
const result = await convert(
|
||||
"input.md",
|
||||
"markdown",
|
||||
"html",
|
||||
"output.html",
|
||||
undefined,
|
||||
mockExecFile,
|
||||
);
|
||||
|
||||
expect(calledArgs[0]).toBe("pandoc");
|
||||
expect(calledArgs[1]).toEqual([
|
||||
"input.md",
|
||||
"-f",
|
||||
"markdown",
|
||||
"-t",
|
||||
"html",
|
||||
"-o",
|
||||
"output.html",
|
||||
]);
|
||||
expect(result).toBe("Done");
|
||||
});
|
||||
|
||||
test("should add xelatex argument for pdf/latex", async () => {
|
||||
let calledArgs: Parameters<ExecFileFn> = ["", [], () => {}];
|
||||
mockExecFile = (cmd, args, callback) => {
|
||||
calledArgs = [cmd, args, callback];
|
||||
callback(null, "output-data", "");
|
||||
};
|
||||
|
||||
await convert("input.md", "markdown", "pdf", "output.pdf", undefined, mockExecFile);
|
||||
|
||||
expect(calledArgs[1][0]).toBe("--pdf-engine=xelatex");
|
||||
expect(calledArgs[1]).toContain("input.md");
|
||||
expect(calledArgs[1]).toContain("-f");
|
||||
expect(calledArgs[1]).toContain("markdown");
|
||||
expect(calledArgs[1]).toContain("-t");
|
||||
expect(calledArgs[1]).toContain("pdf");
|
||||
expect(calledArgs[1]).toContain("-o");
|
||||
expect(calledArgs[1]).toContain("output.pdf");
|
||||
});
|
||||
|
||||
test("should reject if execFile returns an error", async () => {
|
||||
mockExecFile = (cmd, args, callback) => callback(new Error("fail"), "", "");
|
||||
expect(
|
||||
convert("input.md", "markdown", "html", "output.html", undefined, mockExecFile),
|
||||
).rejects.toMatch(/error: Error: fail/);
|
||||
});
|
||||
});
|
||||
58
tests/converters/vtracer.test.ts
Normal file
58
tests/converters/vtracer.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { beforeEach, expect, test, describe } from "bun:test";
|
||||
import { convert } from "../../src/converters/vtracer";
|
||||
import type { ExecFileFn } from "../../src/converters/types";
|
||||
|
||||
describe("convert", () => {
|
||||
let mockExecFile: ExecFileFn;
|
||||
|
||||
beforeEach(() => {
|
||||
mockExecFile = (cmd, args, callback) => callback(null, "output-data", "");
|
||||
});
|
||||
|
||||
test("should call vtracer with correct arguments (minimal)", async () => {
|
||||
let calledArgs: Parameters<ExecFileFn> = ["", [], () => {}];
|
||||
mockExecFile = (cmd, args, callback) => {
|
||||
calledArgs = [cmd, args, callback];
|
||||
callback(null, "output-data", "");
|
||||
};
|
||||
|
||||
const result = await convert("input.png", "png", "svg", "output.svg", undefined, mockExecFile);
|
||||
|
||||
expect(calledArgs[0]).toBe("vtracer");
|
||||
expect(calledArgs[1]).toEqual(["--input", "input.png", "--output", "output.svg"]);
|
||||
expect(result).toBe("Done");
|
||||
});
|
||||
|
||||
test("should add options as arguments", async () => {
|
||||
let calledArgs: Parameters<ExecFileFn> = ["", [], () => {}];
|
||||
mockExecFile = (cmd, args, callback) => {
|
||||
calledArgs = [cmd, args, callback];
|
||||
callback(null, "output-data", "");
|
||||
};
|
||||
|
||||
const options = {
|
||||
colormode: "color",
|
||||
hierarchical: "true",
|
||||
filter_speckle: 5,
|
||||
path_precision: 0.8,
|
||||
};
|
||||
|
||||
await convert("input.png", "png", "svg", "output.svg", options, mockExecFile);
|
||||
|
||||
expect(calledArgs[1]).toContain("--colormode");
|
||||
expect(calledArgs[1]).toContain("color");
|
||||
expect(calledArgs[1]).toContain("--hierarchical");
|
||||
expect(calledArgs[1]).toContain("true");
|
||||
expect(calledArgs[1]).toContain("--filter_speckle");
|
||||
expect(calledArgs[1]).toContain("5");
|
||||
expect(calledArgs[1]).toContain("--path_precision");
|
||||
expect(calledArgs[1]).toContain("0.8");
|
||||
});
|
||||
|
||||
test("should reject if execFile returns an error", async () => {
|
||||
mockExecFile = (cmd, args, callback) => callback(new Error("fail"), "", "stderr output");
|
||||
expect(
|
||||
convert("input.png", "png", "svg", "output.svg", undefined, mockExecFile),
|
||||
).rejects.toMatch(/error: Error: fail\nstderr: stderr output/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user