mirror of
https://github.com/C4illin/ConvertX.git
synced 2026-06-28 06:55:48 +00:00
test: add unit test for assimp.ts
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { execFile as execFileOriginal } from "node:child_process";
|
||||
|
||||
type ExecFileFn = (
|
||||
cmd: string,
|
||||
args: string[],
|
||||
callback: (err: Error | null, stdout: string, stderr: string) => void,
|
||||
) => void;
|
||||
|
||||
export const properties = {
|
||||
from: {
|
||||
@@ -116,8 +122,8 @@ export async function convert(
|
||||
fileType: string,
|
||||
convertTo: string,
|
||||
targetPath: string,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
options?: unknown,
|
||||
execFile: ExecFileFn = execFileOriginal,
|
||||
): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
execFile("assimp", ["export", filePath, targetPath], (error, stdout, stderr) => {
|
||||
|
||||
70
tests/assimp.test.ts
Normal file
70
tests/assimp.test.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import type { ExecFileException } from "node:child_process";
|
||||
import { expect, test } from "bun:test";
|
||||
import { convert } from "../src/converters/assimp";
|
||||
|
||||
type ExecFileFn = (
|
||||
cmd: string,
|
||||
args: string[],
|
||||
callback: (err: Error | null, stdout: string, stderr: string) => void,
|
||||
) => void;
|
||||
|
||||
test("convert resolves when execFile succeeds", async () => {
|
||||
const originalConsoleLog = console.log;
|
||||
|
||||
let loggedMessage = "";
|
||||
console.log = (msg) => {
|
||||
loggedMessage = msg;
|
||||
};
|
||||
|
||||
const mockExecFile: ExecFileFn = (
|
||||
_cmd: string,
|
||||
_args: string[],
|
||||
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||
) => {
|
||||
callback(null, "Fake stdout", "");
|
||||
};
|
||||
|
||||
const result = await convert("input.obj", "obj", "stl", "output.stl", undefined, mockExecFile);
|
||||
|
||||
console.log = originalConsoleLog;
|
||||
|
||||
expect(result).toBe("Done");
|
||||
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||
});
|
||||
|
||||
test("convert rejects when execFile fails", async () => {
|
||||
const mockExecFile: ExecFileFn = (
|
||||
_cmd: string,
|
||||
_args: string[],
|
||||
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||
) => {
|
||||
callback(new Error("Test error"), "", "");
|
||||
};
|
||||
|
||||
expect(convert("input.obj", "obj", "stl", "output.stl", undefined, mockExecFile)).rejects.toMatch(
|
||||
/error: Error: Test error/,
|
||||
);
|
||||
});
|
||||
|
||||
test("convert logs stderr when present", async () => {
|
||||
const originalConsoleError = console.error;
|
||||
|
||||
let loggedMessage = "";
|
||||
console.error = (msg) => {
|
||||
loggedMessage = msg;
|
||||
};
|
||||
|
||||
const mockExecFile = (
|
||||
_cmd: string,
|
||||
_args: string[],
|
||||
cb: (err: Error | null, stdout: string, stderr: string) => void,
|
||||
) => {
|
||||
cb(null, "", "Fake stderr");
|
||||
};
|
||||
|
||||
await convert("file.obj", "obj", "stl", "out.stl", undefined, mockExecFile);
|
||||
|
||||
console.error = originalConsoleError;
|
||||
|
||||
expect(loggedMessage).toBe("stderr: Fake stderr");
|
||||
});
|
||||
Reference in New Issue
Block a user