mirror of
				https://github.com/C4illin/ConvertX.git
				synced 2025-11-04 05:53:45 +00:00 
			
		
		
		
	test: add unit test for dvisvgm.ts
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
				
			|||||||
import { execFile } from "node:child_process";
 | 
					import { execFile as execFileOriginal } from "node:child_process";
 | 
				
			||||||
 | 
					import { ExecFileFn } from "./types.ts";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const properties = {
 | 
					export const properties = {
 | 
				
			||||||
  from: {
 | 
					  from: {
 | 
				
			||||||
@@ -14,8 +15,8 @@ export function convert(
 | 
				
			|||||||
  fileType: string,
 | 
					  fileType: string,
 | 
				
			||||||
  convertTo: string,
 | 
					  convertTo: string,
 | 
				
			||||||
  targetPath: string,
 | 
					  targetPath: string,
 | 
				
			||||||
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
					 | 
				
			||||||
  options?: unknown,
 | 
					  options?: unknown,
 | 
				
			||||||
 | 
					  execFile: ExecFileFn = execFileOriginal, // to make it mockable
 | 
				
			||||||
): Promise<string> {
 | 
					): Promise<string> {
 | 
				
			||||||
  const inputArgs: string[] = [];
 | 
					  const inputArgs: string[] = [];
 | 
				
			||||||
  if (fileType === "eps") {
 | 
					  if (fileType === "eps") {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,7 +4,7 @@ import {
 | 
				
			|||||||
  runConvertFailTest,
 | 
					  runConvertFailTest,
 | 
				
			||||||
  runConvertLogsStderror,
 | 
					  runConvertLogsStderror,
 | 
				
			||||||
  runConvertSuccessTest,
 | 
					  runConvertSuccessTest,
 | 
				
			||||||
} from "./convertersHelper.test.ts";
 | 
					} from "./helpers/converters.ts";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("convert resolves when execFile succeeds", async () => {
 | 
					test("convert resolves when execFile succeeds", async () => {
 | 
				
			||||||
  await runConvertSuccessTest(convert);
 | 
					  await runConvertSuccessTest(convert);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,7 +4,7 @@ import {
 | 
				
			|||||||
  runConvertFailTest,
 | 
					  runConvertFailTest,
 | 
				
			||||||
  runConvertLogsStderror,
 | 
					  runConvertLogsStderror,
 | 
				
			||||||
  runConvertSuccessTest,
 | 
					  runConvertSuccessTest,
 | 
				
			||||||
} from "./convertersHelper.test.ts";
 | 
					} from "./helpers/converters.ts";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("convert resolves when execFile succeeds", async () => {
 | 
					test("convert resolves when execFile succeeds", async () => {
 | 
				
			||||||
  await runConvertSuccessTest(convert);
 | 
					  await runConvertSuccessTest(convert);
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										93
									
								
								tests/converters/dvisvgm.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								tests/converters/dvisvgm.test.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,93 @@
 | 
				
			|||||||
 | 
					import type { ExecFileException } from "node:child_process";
 | 
				
			||||||
 | 
					import { expect, test } from "bun:test";
 | 
				
			||||||
 | 
					import { convert } from "../../src/converters/dvisvgm.ts";
 | 
				
			||||||
 | 
					import { ExecFileFn } from "../../src/converters/types.ts";
 | 
				
			||||||
 | 
					import {
 | 
				
			||||||
 | 
					  runConvertFailTest,
 | 
				
			||||||
 | 
					  runConvertLogsStderror,
 | 
				
			||||||
 | 
					  runConvertSuccessTest,
 | 
				
			||||||
 | 
					} from "./helpers/converters.ts";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convert resolves when execFile succeeds", async () => {
 | 
				
			||||||
 | 
					  await runConvertSuccessTest(convert);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convert rejects when execFile fails", async () => {
 | 
				
			||||||
 | 
					  await runConvertFailTest(convert);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convert logs stderr when present", async () => {
 | 
				
			||||||
 | 
					  await runConvertLogsStderror(convert);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convert respects eps filetype", 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", "eps", "stl", "output.stl", undefined, mockExecFile);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  console.log = originalConsoleLog;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  expect(result).toBe("Done");
 | 
				
			||||||
 | 
					  expect(loggedMessage).toBe("stdout: Fake stdout");
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convert respects pdf filetype", 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", "pdf", "stl", "output.stl", undefined, mockExecFile);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  console.log = originalConsoleLog;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  expect(result).toBe("Done");
 | 
				
			||||||
 | 
					  expect(loggedMessage).toBe("stdout: Fake stdout");
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convert respects svgz conversion target type", 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", "eps", "svgz", "output.stl", undefined, mockExecFile);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  console.log = originalConsoleLog;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  expect(result).toBe("Done");
 | 
				
			||||||
 | 
					  expect(loggedMessage).toBe("stdout: Fake stdout");
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
import type { ExecFileException } from "node:child_process";
 | 
					import type { ExecFileException } from "node:child_process";
 | 
				
			||||||
import { expect } from "bun:test";
 | 
					import { expect } from "bun:test";
 | 
				
			||||||
import { ConvertFnWithExecFile, ExecFileFn } from "../../src/converters/types.ts";
 | 
					import { ConvertFnWithExecFile, ExecFileFn } from "../../../src/converters/types.ts";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function runConvertSuccessTest(convertFn: ConvertFnWithExecFile) {
 | 
					export async function runConvertSuccessTest(convertFn: ConvertFnWithExecFile) {
 | 
				
			||||||
  const originalConsoleLog = console.log;
 | 
					  const originalConsoleLog = console.log;
 | 
				
			||||||
		Reference in New Issue
	
	Block a user