#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
Fix automate unrunnable tools (#7311)
Anthony Stirling59ed4f5fd117
diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.javaindex 7f17738d98..1d369d282d 100644--- a/app/core/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java+++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java@@ -61,6 +61,7 @@ public class PdfOverlayController {int overlayPos = request.getOverlayPosition();MultipartFile[] overlayFiles = request.getOverlayFiles();+ validateOverlayFiles(overlayFiles);File[] overlayPdfFiles = new File[overlayFiles.length];List<File> tempFiles = new ArrayList<>(); // List to keep track of temporary files@@ -120,10 +121,29 @@ public class PdfOverlayController {}}+ // Both fields are declared required, but @ModelAttribute binding leaves them null when the+ // caller omits them, which would otherwise surface as a 500 instead of a 400.+ private void validateOverlayFiles(MultipartFile[] overlayFiles) {+ if (overlayFiles == null || overlayFiles.length == 0) {+ throw ExceptionUtils.createIllegalArgumentException(+ "error.overlayFilesRequired", "At least one overlay file is required");+ }+ for (MultipartFile overlayFile : overlayFiles) {+ if (overlayFile == null || overlayFile.isEmpty()) {+ throw ExceptionUtils.createIllegalArgumentException(+ "error.overlayFileEmpty", "Overlay files must not be empty");+ }+ }+ }+private Map<Integer, String> prepareOverlayGuide(int basePageCount, File[] overlayFiles, String mode, int[] counts, List<File> tempFiles)throws IOException {Map<Integer, String> overlayGuide = new HashMap<>();+ if (mode == null) {+ throw ExceptionUtils.createIllegalArgumentException(+ "error.invalidFormat", "Invalid {0} format: {1}", "overlay mode", "null");+ }switch (mode) {case "SequentialOverlay":sequentialOverlay(overlayGuide, overlayFiles, basePageCount, tempFiles);diff --git a/frontend/editor/src/core/components/tools/automate/ToolSelector.tsx b/frontend/editor/src/core/components/tools/automate/ToolSelector.tsxindex c5c3998bc0..ad7bb33a6e 100644--- a/frontend/editor/src/core/components/tools/automate/ToolSelector.tsx+++ b/frontend/editor/src/core/components/tools/automate/ToolSelector.tsx@@ -34,13 +34,17 @@ export default function ToolSelector({const [shouldAutoFocus, setShouldAutoFocus] = useState(false);const containerRef = useRef<HTMLDivElement>(null);- // Filter out excluded tools (like 'automate' itself) and tools that don't support automation+ // Filter out excluded tools (like 'automate' itself), tools that don't support+ // automation, and tools with no operationConfig - the executor resolves a step+ // through operationConfig, so offering one without it fails only at run time.const baseFilteredTools = useMemo(() => {return (Object.entries(toolRegistry) as [ToolId, ToolRegistryEntry][]).filter(([key, tool]) =>- !excludeTools.includes(key) && getToolSupportsAutomate(tool),+ !excludeTools.includes(key) &&+ getToolSupportsAutomate(tool) &&+ Boolean(tool.operationConfig),);}, [toolRegistry, excludeTools]);diff --git a/frontend/editor/src/core/data/automatableToolsHaveOperationConfig.test.tsx b/frontend/editor/src/core/data/automatableToolsHaveOperationConfig.test.tsxnew file mode 100644index 0000000000..461c07ebdf--- /dev/null+++ b/frontend/editor/src/core/data/automatableToolsHaveOperationConfig.test.tsx@@ -0,0 +1,33 @@+/**+ * Registry invariant: the Automate picker offers a tool whenever it doesn't opt out via+ * `supportsAutomate: false`, but automationExecutor resolves each step through the tool's+ * `operationConfig`. A tool that is offered without one is selectable in the builder and+ * only fails when the automation runs, with "Tool operation not supported: <toolId>".+ *+ * So a tool must either carry an operationConfig or declare supportsAutomate: false.+ */+import { describe, expect, test, vi } from "vitest";+import { renderHook } from "@testing-library/react";+import { useTranslatedToolCatalog } from "@app/data/useTranslatedToolRegistry";+import { getToolSupportsAutomate } from "@app/data/toolsTaxonomy";++vi.mock("react-i18next", () => ({+ useTranslation: () => ({+ t: (key: string, fallback?: string) => fallback ?? key,+ i18n: { changeLanguage: vi.fn(), language: "en-US" },+ }),+ Trans: ({ children }: { children?: unknown }) => children,+}));++describe("automatable tools", () => {+ test("every tool offered to Automate can be executed as a step", () => {+ const { result } = renderHook(() => useTranslatedToolCatalog());++ const offeredWithoutConfig = Object.entries(result.current.regularTools)+ .filter(([, entry]) => entry && getToolSupportsAutomate(entry))+ .filter(([, entry]) => !entry.operationConfig)+ .map(([id]) => id);++ expect(offeredWithoutConfig).toEqual([]);+ });+});diff --git a/frontend/editor/src/core/data/useTranslatedToolRegistry.tsx b/frontend/editor/src/core/data/useTranslatedToolRegistry.tsxindex 16b69d6983..0a39da2fb9 100644--- a/frontend/editor/src/core/data/useTranslatedToolRegistry.tsx+++ b/frontend/editor/src/core/data/useTranslatedToolRegistry.tsx@@ -50,6 +50,8 @@ import { changeMetadataOperationConfig } from "@app/hooks/tools/changeMetadata/uimport { signOperationConfig } from "@app/hooks/tools/sign/useSignOperation";import { cropOperationConfig } from "@app/hooks/tools/crop/useCropOperation";import { removeAnnotationsOperationConfig } from "@app/hooks/tools/removeAnnotations/useRemoveAnnotationsOperation";+import { removeImageOperationConfig } from "@app/hooks/tools/removeImage/useRemoveImageOperation";+import { pageLayoutOperationConfig } from "@app/hooks/tools/pageLayout/usePageLayoutOperation";import { extractImagesOperationConfig } from "@app/hooks/tools/extractImages/useExtractImagesOperation";import { replaceColorOperationConfig } from "@app/hooks/tools/replaceColor/useReplaceColorOperation";import { removePagesOperationConfig } from "@app/hooks/tools/removePages/useRemovePagesOperation";@@ -526,6 +528,9 @@ export function useTranslatedToolCatalog(): TranslatedToolCatalog {maxFiles: -1,endpoints: ["validate-signature"],synonyms: getSynonyms(t, "validateSignature"),+ // Reports on signatures rather than transforming the PDF, and its hook is+ // not on the operationConfig seam, so it cannot run as an automation step.+ supportsAutomate: false,automationSettings: null,},@@ -755,6 +760,7 @@ export function useTranslatedToolCatalog(): TranslatedToolCatalog {subcategoryId: SubcategoryId.PAGE_FORMATTING,maxFiles: -1,endpoints: ["multi-page-layout"],+ operationConfig: asRegistryConfig(pageLayoutOperationConfig),automationSettings: lazySettings(() => import("@app/components/tools/pageLayout/PageLayoutSettings"),),@@ -967,7 +973,7 @@ export function useTranslatedToolCatalog(): TranslatedToolCatalog {subcategoryId: SubcategoryId.REMOVAL,maxFiles: -1,endpoints: ["remove-image-pdf"],- operationConfig: undefined,+ operationConfig: asRegistryConfig(removeImageOperationConfig),synonyms: getSynonyms(t, "removeImage"),automationSettings: null,},@@ -1196,6 +1202,9 @@ export function useTranslatedToolCatalog(): TranslatedToolCatalog {subcategoryId: SubcategoryId.ADVANCED_FORMATTING,endpoints: ["scanner-effect"],synonyms: getSynonyms(t, "scannerEffect"),+ // No frontend implementation yet (component is null), so it has no+ // operationConfig to execute as an automation step.+ supportsAutomate: false,automationSettings: null,},diff --git a/frontend/editor/src/core/utils/toolIOCompat.test.ts b/frontend/editor/src/core/utils/toolIOCompat.test.tsindex c0e9d9efa3..b3fb117705 100644--- a/frontend/editor/src/core/utils/toolIOCompat.test.ts+++ b/frontend/editor/src/core/utils/toolIOCompat.test.ts@@ -2,6 +2,7 @@import { readFileSync } from "node:fs";import { dirname, resolve } from "node:path";+import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";import {validateToolChain,@@ -24,7 +25,9 @@ interface SharedCase {/** Shared with the backend and engine, so it lives at the repo root. */function casesFile(): string {- let current = dirname(new URL(import.meta.url).pathname);+ // fileURLToPath, not URL.pathname: on Windows the latter yields "/C:/..." and+ // resolving against it produces a "C:\C:\..." path that never matches.+ let current = dirname(fileURLToPath(import.meta.url));for (let i = 0; i < 12; i++) {const candidate = resolve(current, "testing/tool-io-cases.json");try {diff --git a/frontend/editor/src/core/utils/toolIOLabels.test.ts b/frontend/editor/src/core/utils/toolIOLabels.test.tsindex ac6b4c155b..98c529e677 100644--- a/frontend/editor/src/core/utils/toolIOLabels.test.ts+++ b/frontend/editor/src/core/utils/toolIOLabels.test.ts@@ -1,5 +1,6 @@import { readFileSync } from "node:fs";import { dirname, resolve } from "node:path";+import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";import { TOOL_FORMATS, type ToolFormat } from "@app/types/toolIO";import {@@ -9,7 +10,9 @@ import {/** The en-US `[toolFormat]` block, read straight from the locale file. */function toolFormatLabels(): Record<string, string> {- let current = dirname(new URL(import.meta.url).pathname);+ // fileURLToPath, not URL.pathname: on Windows the latter yields "/C:/..." and+ // resolving against it produces a "C:\C:\..." path that never matches.+ let current = dirname(fileURLToPath(import.meta.url));for (let i = 0; i < 12; i++) {try {const toml = readFileSync(