Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

6.1 KB
Raw Blame
import { beforeEach, expect, test, vi } from "vitest";
import type { ToolRegistry } from "@editor/data/toolsTaxonomy";
const mocks = vi.hoisted(() => ({
  submit: vi.fn(),
  wait: vi.fn(),
  download: vi.fn(),
  local: vi.fn(),
}));
vi.mock("@editor-core/utils/automationExecutor", () => ({
  executeToolOperation: mocks.local,
  executeToolOperationWithPrefix: mocks.local,
  processMultiFileResponse: vi.fn(),
}));
vi.mock("@editor/services/serverAutomationSession", () => ({
  getServerAutomationSession: async () => ({
    key: "owner",
    baseUrl: "https://server.test",
  }),
}));
vi.mock("@editor/services/serverPipeline", () => ({
  submitServerPipeline: mocks.submit,
  waitForServerPipeline: mocks.wait,
  downloadServerPipelineOutput: mocks.download,
}));
import { executeAutomationSequence } from "@editor/utils/automationExecutor";
import {
  clearPdfAccess,
  rememberUnencryptedPdf,
  setSignedPdfEditPolicy,
} from "@editor/services/pdfAccessService";
import { ToolType } from "@editor/hooks/tools/shared/useToolOperation";

beforeEach(() => {
  vi.resetAllMocks();
  clearPdfAccess();
  setSignedPdfEditPolicy(false);
});

test("refuses a signed input before submitting a pipeline that rewrites it", async () => {
  setSignedPdfEditPolicy(true);
  const signed = new File(["signed"], "signed.pdf", {
    type: "application/pdf",
  });
  rememberUnencryptedPdf(signed, 1, true);
  const onStepError = vi.fn();
  const registry = {
    rotate: {
      operationConfig: {
        toolType: ToolType.singleFile,
        endpoint: "/api/v1/general/rotate-pdf",
        buildFormData: () => new FormData(),
        defaultParameters: {},
      },
    },
  } as unknown as ToolRegistry;
  await expect(
    executeAutomationSequence(
      {
        id: "auto",
        name: "Pipeline",
        operations: [{ operation: "rotate", parameters: {} }],
        createdAt: "",
        updatedAt: "",
      },
      [signed],
      registry,
      undefined,
      undefined,
      onStepError,
    ),
  ).rejects.toThrow("digitally signed");
  expect(mocks.submit).not.toHaveBeenCalled();
  expect(onStepError).toHaveBeenCalledOnce();
});

test("lets a signed input through a pipeline whose first step converts it away from PDF", async () => {
  setSignedPdfEditPolicy(true);
  const signed = new File(["signed"], "signed.pdf", {
    type: "application/pdf",
  });
  rememberUnencryptedPdf(signed, 1, true);
  const registry = {
    toImage: {
      operationConfig: {
        toolType: ToolType.singleFile,
        endpoint: "/api/v1/convert/pdf/img",
        buildFormData: () => new FormData(),
        defaultParameters: {},
      },
    },
    rotate: {
      operationConfig: {
        toolType: ToolType.singleFile,
        endpoint: "/api/v1/general/rotate-pdf",
        buildFormData: () => new FormData(),
        defaultParameters: {},
      },
    },
  } as unknown as ToolRegistry;
  mocks.submit.mockResolvedValue("run");
  mocks.wait.mockResolvedValue({ outputs: [] });
  await executeAutomationSequence(
    {
      id: "auto",
      name: "Pipeline",
      operations: [
        { operation: "toImage", parameters: {} },
        { operation: "rotate", parameters: {} },
      ],
      createdAt: "",
      updatedAt: "",
    },
    [signed],
    registry,
  );
  expect(mocks.submit).toHaveBeenCalledOnce();
});
test("sends locally supported tool steps together and never calls the local executor", async () => {
  const buildFormData = () => {
    const form = new FormData();
    form.append("quality", "50");
    form.append("pageNumbers", "1");
    form.append("pageNumbers", "3");
    return form;
  };
  const registry = {
    compress: {
      operationConfig: {
        toolType: ToolType.singleFile,
        endpoint: "/api/v1/misc/compress-pdf",
        buildFormData,
        defaultParameters: {},
      },
    },
    rotate: {
      operationConfig: {
        toolType: ToolType.singleFile,
        endpoint: "/api/v1/general/rotate-pdf",
        buildFormData,
        defaultParameters: {},
      },
    },
  } as unknown as ToolRegistry;
  mocks.submit.mockResolvedValue("run");
  mocks.wait.mockResolvedValue({
    outputs: [{ fileId: "output", fileName: "result.pdf" }],
  });
  const output = new File(["output"], "result.pdf");
  mocks.download.mockResolvedValue(output);
  const result = await executeAutomationSequence(
    {
      id: "auto",
      name: "Pipeline",
      operations: [
        { operation: "compress", parameters: {} },
        { operation: "rotate", parameters: {} },
      ],
      createdAt: "",
      updatedAt: "",
    },
    [new File(["input"], "input.pdf")],
    registry,
  );
  expect(result).toEqual([output]);
  expect(mocks.submit).toHaveBeenCalledTimes(1);
  expect(mocks.submit.mock.calls[0][2]).toEqual([
    {
      operation: "/api/v1/misc/compress-pdf",
      parameters: { quality: "50", pageNumbers: ["1", "3"] },
      fileParameters: {},
    },
    {
      operation: "/api/v1/general/rotate-pdf",
      parameters: { quality: "50", pageNumbers: ["1", "3"] },
      fileParameters: {},
    },
  ]);
  expect(mocks.local).not.toHaveBeenCalled();
});

test("custom tools use their server parameter mapping without invoking their processor", async () => {
  const registry = {
    convert: {
      operationConfig: {
        toolType: ToolType.custom,
        endpoint: () => "/api/v1/convert/pdf/img",
        defaultParameters: { toExtension: "png" },
        toApiParams: (params: { toExtension: string }) => ({
          imageFormat: params.toExtension,
          colorType: "color",
        }),
        customProcessor: mocks.local,
      },
    },
  } as unknown as ToolRegistry;
  mocks.submit.mockResolvedValue("run");
  mocks.wait.mockResolvedValue({ outputs: [] });
  await executeAutomationSequence(
    {
      id: "convert",
      name: "Convert pipeline",
      operations: [{ operation: "convert", parameters: {} }],
      createdAt: "",
      updatedAt: "",
    },
    [new File(["input"], "input.pdf")],
    registry,
  );
  expect(mocks.submit.mock.calls[0][2]).toEqual([
    {
      operation: "/api/v1/convert/pdf/img",
      parameters: { imageFormat: "png", colorType: "color" },
      fileParameters: {},
    },
  ]);
  expect(mocks.local).not.toHaveBeenCalled();
});