diff --git a/src/converters/assimp.ts b/src/converters/assimp.ts index cac4f9f..9db38ce 100644 --- a/src/converters/assimp.ts +++ b/src/converters/assimp.ts @@ -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 { return new Promise((resolve, reject) => { execFile("assimp", ["export", filePath, targetPath], (error, stdout, stderr) => { diff --git a/tests/assimp.test.ts b/tests/assimp.test.ts new file mode 100644 index 0000000..062c115 --- /dev/null +++ b/tests/assimp.test.ts @@ -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"); +});