fix: resolve lint issues (XSS K601, knip unused exports, eslint errors)

- Fix XSS warnings in user.tsx by adding safe attribute to elements
- Remove unused exports: ChunkUploadRequest, getArchiveInfo,
  getFileForDirectDownload, createDirectDownloadHeaders, createConverterArchive
- Fix eslint no-unused-vars errors across multiple files
- Fix pdfmathtranslate async Promise executor issue
- Update tests to use toThrow instead of toMatch for Error objects
- Apply prettier formatting
This commit is contained in:
Your Name 2026-01-21 16:00:26 +08:00
parent db5f47586e
commit 79fc6f7067
21 changed files with 439 additions and 439 deletions

View file

@ -31,7 +31,7 @@ describe("MinerU converter properties", () => {
describe("MinerU converter md-t mode", () => {
const testDir = "./test-output-mineru-t";
beforeEach(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
@ -46,12 +46,11 @@ describe("MinerU converter md-t mode", () => {
test("should call mineru with markdown table mode for md-t", async () => {
let mineruArgs: string[] = [];
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "mineru") {
mineruArgs = args;
@ -64,7 +63,10 @@ describe("MinerU converter md-t mode", () => {
if (!existsSync(autoDir)) {
mkdirSync(autoDir, { recursive: true });
}
writeFileSync(join(autoDir, "output.md"), "# Test\n\n| Col1 | Col2 |\n|---|---|\n| A | B |");
writeFileSync(
join(autoDir, "output.md"),
"# Test\n\n| Col1 | Col2 |\n|---|---|\n| A | B |",
);
callback(null, "MinerU conversion complete", "");
} else if (cmd === "tar") {
// Simulate .tar creation (no compression)
@ -83,7 +85,7 @@ describe("MinerU converter md-t mode", () => {
describe("MinerU converter md-i mode", () => {
const testDir = "./test-output-mineru-i";
beforeEach(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
@ -98,12 +100,11 @@ describe("MinerU converter md-i mode", () => {
test("should call mineru with image table mode for md-i", async () => {
let capturedArgs: string[] = [];
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "mineru") {
capturedArgs = args;
@ -135,7 +136,7 @@ describe("MinerU converter md-i mode", () => {
describe("MinerU converter .tar output (no compression)", () => {
const testDir = "./test-output-mineru-tar";
beforeEach(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
@ -151,12 +152,11 @@ describe("MinerU converter .tar output (no compression)", () => {
test("should create .tar archive from output (without gzip)", async () => {
let tarCalled = false;
let tarArgs: string[] = [];
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "mineru") {
// Simulate MinerU creating output
@ -194,9 +194,8 @@ describe("MinerU converter .tar output (no compression)", () => {
test("should reject on mineru error", async () => {
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "mineru") {
callback(new Error("MinerU failed") as ExecFileException, "", "Error processing file");
@ -204,18 +203,18 @@ describe("MinerU converter .tar output (no compression)", () => {
};
const targetPath = join(testDir, "output.tar");
expect(convert("test.pdf", "pdf", "md-t", targetPath, undefined, mockExecFile))
.rejects.toMatch(/mineru error/);
expect(convert("test.pdf", "pdf", "md-t", targetPath, undefined, mockExecFile)).rejects.toMatch(
/mineru error/,
);
});
test("should use correct tar arguments without compression", async () => {
let tarArgs: string[] = [];
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "mineru") {
const outputDir = args[3];
@ -248,12 +247,11 @@ describe("MinerU converter .tar output (no compression)", () => {
test("should convert .tar.gz target path to .tar", async () => {
let tarOutputPath = "";
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "mineru") {
const outputDir = args[3];

View file

@ -2,7 +2,7 @@ import { test, expect, describe, beforeEach, afterEach } from "bun:test";
import { convert, properties } from "../../src/converters/pdfmathtranslate";
import type { ExecFileException } from "node:child_process";
import { ExecFileFn } from "../../src/converters/types";
import { mkdirSync, existsSync, writeFileSync, rmSync, readFileSync } from "node:fs";
import { mkdirSync, existsSync, writeFileSync, rmSync, readdirSync } from "node:fs";
import { join } from "node:path";
// Skip common tests as PDFMathTranslate has different behavior (archive output)
@ -31,7 +31,7 @@ describe("PDFMathTranslate converter properties", () => {
describe("PDFMathTranslate converter - Chinese translation", () => {
const testDir = "./test-output-pdfmathtranslate-zh";
const testInputFile = join(testDir, "input.pdf");
beforeEach(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
@ -49,17 +49,16 @@ describe("PDFMathTranslate converter - Chinese translation", () => {
test("should call pdf2zh with correct language argument for zh", async () => {
let pdf2zhArgs: string[] = [];
let pdf2zhCalled = false;
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "pdf2zh") {
pdf2zhCalled = true;
pdf2zhArgs = args;
// Simulate pdf2zh creating output files
const outputDirIndex = args.indexOf("-o");
if (outputDirIndex !== -1 && args[outputDirIndex + 1]) {
@ -95,7 +94,7 @@ describe("PDFMathTranslate converter - Chinese translation", () => {
describe("PDFMathTranslate converter - English translation", () => {
const testDir = "./test-output-pdfmathtranslate-en";
const testInputFile = join(testDir, "input.pdf");
beforeEach(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
@ -111,12 +110,11 @@ describe("PDFMathTranslate converter - English translation", () => {
test("should call pdf2zh with correct language argument for en", async () => {
let pdf2zhArgs: string[] = [];
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "pdf2zh") {
pdf2zhArgs = args;
@ -145,7 +143,7 @@ describe("PDFMathTranslate converter - English translation", () => {
describe("PDFMathTranslate converter - Japanese translation", () => {
const testDir = "./test-output-pdfmathtranslate-ja";
const testInputFile = join(testDir, "input.pdf");
beforeEach(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
@ -161,12 +159,11 @@ describe("PDFMathTranslate converter - Japanese translation", () => {
test("should call pdf2zh with correct language argument for ja", async () => {
let pdf2zhArgs: string[] = [];
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "pdf2zh") {
pdf2zhArgs = args;
@ -195,7 +192,7 @@ describe("PDFMathTranslate converter - Japanese translation", () => {
describe("PDFMathTranslate converter - Traditional Chinese", () => {
const testDir = "./test-output-pdfmathtranslate-zhtw";
const testInputFile = join(testDir, "input.pdf");
beforeEach(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
@ -211,12 +208,11 @@ describe("PDFMathTranslate converter - Traditional Chinese", () => {
test("should call pdf2zh with correct language argument for zh-TW", async () => {
let pdf2zhArgs: string[] = [];
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "pdf2zh") {
pdf2zhArgs = args;
@ -245,7 +241,7 @@ describe("PDFMathTranslate converter - Traditional Chinese", () => {
describe("PDFMathTranslate converter - Output structure", () => {
const testDir = "./test-output-pdfmathtranslate-structure";
const testInputFile = join(testDir, "input.pdf");
beforeEach(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
@ -262,12 +258,11 @@ describe("PDFMathTranslate converter - Output structure", () => {
test("should create archive with original.pdf and translated-<lang>.pdf", async () => {
let tarSourceDir = "";
let archiveContents: string[] = [];
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "pdf2zh") {
const outputDirIndex = args.indexOf("-o");
@ -286,7 +281,6 @@ describe("PDFMathTranslate converter - Output structure", () => {
tarSourceDir = args[cIndex + 1];
// Read the files in the archive source directory
if (existsSync(tarSourceDir)) {
const { readdirSync } = require("node:fs");
archiveContents = readdirSync(tarSourceDir);
}
}
@ -304,12 +298,11 @@ describe("PDFMathTranslate converter - Output structure", () => {
test("should only use .tar format, not .tar.gz", async () => {
let tarArgs: string[] = [];
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "pdf2zh") {
const outputDirIndex = args.indexOf("-o");
@ -333,7 +326,7 @@ describe("PDFMathTranslate converter - Output structure", () => {
// Verify tar is called with -cf (not -czf for gzip compression)
expect(tarArgs[0]).toBe("-cf");
expect(tarArgs[0]).not.toBe("-czf");
// Verify output path ends with .tar not .tar.gz
const outputTar = tarArgs[1];
expect(outputTar).toMatch(/\.tar$/);
@ -345,7 +338,7 @@ describe("PDFMathTranslate converter - Output structure", () => {
describe("PDFMathTranslate converter - Error handling", () => {
const testDir = "./test-output-pdfmathtranslate-error";
const testInputFile = join(testDir, "input.pdf");
beforeEach(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
@ -362,9 +355,8 @@ describe("PDFMathTranslate converter - Error handling", () => {
test("should reject with error when pdf2zh fails", async () => {
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "pdf2zh") {
const error = new Error("Translation failed") as ExecFileException;
@ -373,18 +365,17 @@ describe("PDFMathTranslate converter - Error handling", () => {
};
const targetPath = join(testDir, "output.tar");
await expect(
convert(testInputFile, "pdf", "pdf-zh", targetPath, undefined, mockExecFile)
).rejects.toMatch(/pdf2zh error/);
convert(testInputFile, "pdf", "pdf-zh", targetPath, undefined, mockExecFile),
).rejects.toThrow(/pdf2zh error|PDFMathTranslate error/);
});
test("should reject when no output PDF is generated", async () => {
const mockExecFile: ExecFileFn = (
cmd: string,
args: string[],
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
options?: any,
) => {
if (cmd === "pdf2zh") {
// Don't create any output files
@ -395,9 +386,9 @@ describe("PDFMathTranslate converter - Error handling", () => {
};
const targetPath = join(testDir, "output.tar");
await expect(
convert(testInputFile, "pdf", "pdf-zh", targetPath, undefined, mockExecFile)
).rejects.toMatch(/No.*PDF.*found/);
convert(testInputFile, "pdf", "pdf-zh", targetPath, undefined, mockExecFile),
).rejects.toThrow(/No.*PDF.*found|No translated PDF/);
});
});