Skip to content

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

2.8 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";

/**
 * Only the document reference is pinned, being the one field with a rule attached: a filename here
 * would reach a table that deliberately has nowhere to keep one.
 */

const post = vi.fn().mockResolvedValue({ data: { jobId: "run-1" } });

vi.mock("@editor/services/apiClient", () => ({
  default: { post: (...args: unknown[]) => post(...args) },
}));

const { runStoredPolicy } = await import("@editor/services/policyApi");

/** The multipart body the last call sent. */
function sentForm(): FormData {
  return post.mock.calls.at(-1)?.[1] as FormData;
}

/** jsdom's Blob has no text(). */
const textOf = (file: File) =>
  new Promise<string>((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => resolve(reader.result as string);
    reader.onerror = () => reject(reader.error);
    reader.readAsText(file);
  });

const document = () =>
  new File(["%PDF-1.7"], "quarterly-report.pdf", { type: "application/pdf" });

describe("runStoredPolicy", () => {
  beforeEach(() => {
    post.mockClear();
  });

  it("sends the workspace id of the document it is running on", async () => {
    await runStoredPolicy("policy-1", [document()], "editor-file-1");

    expect(post.mock.calls.at(-1)?.[0]).toBe("/api/v1/policies/policy-1/run");
    expect(sentForm().get("fileId")).toBe("editor-file-1");
  });

  it("sends no reference when the caller has none", async () => {
    // The export path can enforce on bytes with no workspace document behind them.
    await runStoredPolicy("policy-1", [document()]);

    expect(sentForm().has("fileId")).toBe(false);
  });

  it("never sends the document's name as the reference", async () => {
    await runStoredPolicy("policy-1", [document()], "editor-file-1");

    expect(sentForm().get("fileId")).not.toContain("quarterly-report");
  });

  it("uploads a fresh File over the caller's bytes, never the caller's File object", async () => {
    const source = document();
    await runStoredPolicy("policy-1", [source], "editor-file-1");

    const sent = sentForm().get("fileInput") as File;
    expect(sent).not.toBe(source);
    expect(sent.name).toBe(source.name);
    expect(await textOf(sent)).toBe(await textOf(source));
  });

  it("includes the pipeline and distinguishes automatic dispatches from manual retries", async () => {
    await runStoredPolicy("policy-1", [document()], "file-1", "background");
    expect(post.mock.calls.at(-1)?.[2]).toMatchObject({
      suppressErrorToast: true,
      accountLinkBlockContext: { pipelineId: "policy-1", trigger: "automatic" },
    });
    await runStoredPolicy("policy-1", [document()], "file-1");
    expect(post.mock.calls.at(-1)?.[2]).toMatchObject({
      suppressErrorToast: true,
      accountLinkBlockContext: { pipelineId: "policy-1", trigger: "manual" },
    });
  });
});