Skip to content

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

48.6 KB
Raw Blame
import { classificationCondition } from "@editor/data/classificationConditions";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
  fireEvent,
  render as baseRender,
  screen,
  waitFor,
} from "@testing-library/react";
import { PortalTestProviders } from "@processor/test/TestQueryProvider";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { useQueryClient } from "@tanstack/react-query";
import { qk } from "@processor/queries/keys";
import type { Policy, TriggerOutcome } from "@processor/api/pipelines";
import type { Source, SourceView } from "@processor/api/sources";
import type { ToolRegistryCatalog } from "@editor/contexts/ToolRegistryContext";
import type { ToolRegistryEntry } from "@editor/data/toolsTaxonomy";
import { PipelineBuilder } from "@processor/views/PipelineBuilder";

const render = (
  ui: Parameters<typeof baseRender>[0],
  options?: Parameters<typeof baseRender>[1],
) => baseRender(ui, { wrapper: PortalTestProviders, ...options });

// Deterministic i18n: keys returned verbatim.
vi.mock("react-i18next", () => ({
  useTranslation: () => ({
    t: (key: string) => key,
    i18n: { changeLanguage: vi.fn() },
  }),
}));

vi.mock("@processor/hooks/useAiEngineEnabled", () => ({
  useAiEngineEnabled: () => ({
    enabled: true,
    classificationEnabled: true,
    loading: false,
  }),
}));

const fetchPipeline = vi.fn();
const fetchTriggers = vi.fn();
const savePipeline = vi.fn();
const deletePipeline = vi.fn();
const triggerPipeline = vi.fn();
const fetchRun = vi.fn();
const runPipelineTest = vi.fn();
const fetchRunOutput = vi.fn();
vi.mock("@processor/api/pipelines", () => ({
  fetchPipeline: (id: string) => fetchPipeline(id),
  fetchTriggers: () => fetchTriggers(),
  savePipeline: (policy: unknown) => savePipeline(policy),
  deletePipeline: (id: string) => deletePipeline(id),
  triggerPipeline: (id: string) => triggerPipeline(id),
  fetchRun: (runId: string) => fetchRun(runId),
  fetchPolicyPermissions: () => Promise.resolve({ canManagePolicies: true }),
  runPipelineTest: (...args: unknown[]) => runPipelineTest(...args),
  fetchRunOutput: (...args: unknown[]) => fetchRunOutput(...args),
}));

const uploadPipelineAsset = vi.fn();
const listPipelineAssets = vi.fn();
vi.mock("@processor/api/pipelineAssets", () => ({
  uploadPipelineAsset: (file: File) => uploadPipelineAsset(file),
  listPipelineAssets: () => listPipelineAssets(),
}));

const fetchSources = vi.fn();
vi.mock("@processor/api/sources", () => ({
  fetchSources: () => fetchSources(),
}));

const clearProcessedHistory = vi.fn();
vi.mock("@processor/api/policies", () => ({
  clearProcessedHistory: (id: string) => clearProcessedHistory(id),
}));

const fetchS3Connections = vi.fn();
const createIntegration = vi.fn();
vi.mock("@processor/api/integrations", () => ({
  fetchIntegrations: () => fetchS3Connections(),
  // Custom-API authoring is a server decision; these tests assert the default view.
  fetchIntegrationCapabilities: () => Promise.resolve({ customApi: false }),
  fetchS3Connections: () => fetchS3Connections(),
  createIntegration: (...args: unknown[]) => createIntegration(...args),
}));

// The destination picker just selects saved sources; stub its three affordances
// (pick, create, edit) to buttons, keeping this suite focused on the builder.
vi.mock("@processor/components/pipelines/DestinationPicker", () => ({
  DestinationPicker: ({
    value,
    onChange,
    onCreateNew,
    onEdit,
  }: {
    value: string[];
    onChange: (ids: string[]) => void;
    onCreateNew: () => void;
    onEdit: (sourceId: string) => void;
  }) => (
    <>
      <button type="button" onClick={() => onChange(["src-in"])}>
        {value.length > 0 ? `output:${value.join(",")}` : "pick output"}
      </button>
      <button type="button" onClick={onCreateNew}>
        new destination
      </button>
      <button type="button" onClick={() => onEdit(value[0] ?? "")}>
        edit destination
      </button>
      <button type="button" onClick={() => onChange([])}>
        clear destination
      </button>
    </>
  ),
}));

let createdSource: Source;

// The source modal has its own suite; stub it to the two things the builder
// depends on - the record it was opened on, and the sources-cache invalidation
// that follows a save (which is how a new source reaches the pickers).
vi.mock("@processor/components/sources/SourceModal", () => ({
  SourceModal: ({
    open,
    sourceId,
    onCreated,
  }: {
    open: boolean;
    sourceId?: string | null;
    onCreated?: (source: Source) => void;
  }) => {
    const queryClient = useQueryClient();
    if (!open) return null;
    return (
      <div>
        <span>source-modal:{sourceId || "new"}</span>
        <button
          type="button"
          onClick={async () => {
            await queryClient.invalidateQueries({ queryKey: qk.sources() });
            onCreated?.(createdSource);
          }}
        >
          source saved
        </button>
      </div>
    );
  },
}));

// One editable tool, Compress, so the picker and step settings have something to render.
vi.mock("@editor/contexts/ToolRegistryContext", () => {
  const compress = {
    name: "Compress",
    icon: null,
    component: null,
    description: "",
    categoryId: "recommendedTools",
    subcategoryId: "general",
    automationSettings: (props: {
      onParameterChange: (key: string, value: unknown) => void;
    }) => (
      <button
        type="button"
        onClick={() =>
          props.onParameterChange(
            "watermarkImage",
            new File(["x"], "logo.png", { type: "image/png" }),
          )
        }
      >
        upload logo
      </button>
    ),
    operationConfig: {
      operationType: "compress",
      toolType: 0,
      endpoint: "/api/v1/misc/compress-pdf",
      defaultParameters: {},
      // Sends the supporting file under a named field, like a real file tool, so the upload path
      // has a field to bind. The scalar mapper drops the File (files never ride in parameters).
      buildFormData: (params: Record<string, unknown>, file: File | File[]) => {
        const fd = new FormData();
        fd.append("fileInput", Array.isArray(file) ? file[0] : file);
        if (params.watermarkImage instanceof File) {
          fd.append("watermarkImage", params.watermarkImage);
        }
        return fd;
      },
      toApiParams: (params: Record<string, unknown>) => {
        const scalars = { ...params };
        delete scalars.watermarkImage;
        return scalars;
      },
      fromApiParams: (params: Record<string, unknown>) => ({ ...params }),
    },
  } as unknown as ToolRegistryEntry;
  // Produces images, so nothing downstream that wants a PDF can follow it.
  const extractImages = {
    name: "Extract images",
    icon: null,
    component: null,
    description: "",
    categoryId: "recommendedTools",
    subcategoryId: "general",
    operationConfig: {
      operationType: "extractImages",
      toolType: 0,
      endpoint: "/api/v1/misc/extract-images",
      defaultParameters: {},
      buildFormData: () => new FormData(),
      toApiParams: (params: Record<string, unknown>) => ({ ...params }),
      fromApiParams: (params: Record<string, unknown>) => ({ ...params }),
    },
  } as unknown as ToolRegistryEntry;
  // A tool that will not run on its defaults: its validateParams is the same predicate its own Run
  // button uses, so a step for it is "unconfigured" until a language is chosen.
  const ocr = {
    name: "OCR",
    icon: null,
    component: null,
    description: "",
    categoryId: "recommendedTools",
    subcategoryId: "general",
    automationSettings: (props: {
      onParameterChange: (key: string, value: unknown) => void;
    }) => (
      <button
        type="button"
        onClick={() => props.onParameterChange("languages", ["eng"])}
      >
        pick language
      </button>
    ),
    operationConfig: {
      operationType: "ocr",
      toolType: 0,
      endpoint: "/api/v1/misc/ocr-pdf",
      defaultParameters: { languages: [] },
      validateParams: (params: { languages?: string[] }) =>
        (params.languages ?? []).length > 0,
      buildFormData: () => new FormData(),
      toApiParams: (params: Record<string, unknown>) => ({ ...params }),
      fromApiParams: (params: Record<string, unknown>) => ({ ...params }),
    },
  } as unknown as ToolRegistryEntry;
  // A tool whose buildFormData throws, so it can't be probed: exercises the "activeFileFields is
  // null" path where a reopened step's stored binding must be kept, not dropped.
  const sign = {
    name: "Sign",
    icon: null,
    component: null,
    description: "",
    categoryId: "recommendedTools",
    subcategoryId: "general",
    operationConfig: {
      operationType: "certSign",
      toolType: 0,
      endpoint: "/api/v1/security/cert-sign",
      defaultParameters: {},
      buildFormData: () => {
        throw new Error("cannot build");
      },
      toApiParams: (params: Record<string, unknown>) => ({ ...params }),
      fromApiParams: (params: Record<string, unknown>) => ({ ...params }),
    },
  } as unknown as ToolRegistryEntry;
  const allTools = {
    compress,
    extractImages,
    ocr,
    sign,
  } as unknown as ToolRegistryCatalog["allTools"];
  const catalog: ToolRegistryCatalog = {
    regularTools: allTools,
    superTools: allTools,
    linkTools: allTools,
    allTools,
    getToolById: () => null,
  };
  return { useToolRegistry: () => catalog };
});

const routingRule = (values: string[]) => ({
  condition: classificationCondition(values),
  outputId: "src-1",
});

const CLASSIFY_STEP = {
  operation: "/api/v1/ai/tools/classify-and-label",
  parameters: {},
};

const POLICY: Policy = {
  id: "plc-1",
  name: "Existing pipeline",
  enabled: true,
  inputs: [],
  steps: [],
  output: { type: "inline", options: {} },
  outputIds: [],
};

/** The built-in editor source, offered as an input so a pipeline can run in the browser. */
const EDITOR_SOURCE: SourceView = {
  id: "src-editor",
  name: "Editor",
  type: "editor",
  status: "active",
  referenceCount: 0,
  referencingPolicies: [],
  config: [],
  docsTotal: 0,
  docs24h: 0,
  docs30d: 0,
};

const SOURCE: SourceView = {
  id: "src-in",
  name: "Claims intake",
  type: "folder",
  status: "active",
  referenceCount: 0,
  referencingPolicies: [],
  config: [],
  docsTotal: 0,
  docs24h: 0,
  docs30d: 0,
};

const DESTINATION_SOURCE: SourceView = {
  ...SOURCE,
  id: "src-1",
  name: "Archive",
};

function outcome(overrides: Partial<TriggerOutcome>): TriggerOutcome {
  return {
    runIds: [],
    filesListed: 0,
    alreadyProcessed: 0,
    parked: 0,
    inFlight: 0,
    ...overrides,
  };
}

function renderBuilder(initial: string) {
  return render(
    <MemoryRouter initialEntries={[initial]}>
      <Routes>
        <Route path="/processor/pipelines/new" element={<PipelineBuilder />} />
        <Route path="/processor/pipelines/:id" element={<PipelineBuilder />} />
        <Route
          path="/processor/pipelines"
          element={<div>pipelines list</div>}
        />
      </Routes>
    </MemoryRouter>,
  );
}

describe("PipelineBuilder", () => {
  beforeEach(() => {
    createdSource = {
      id: "src-new",
      name: "Scanner drop",
      type: "folder",
      options: {},
      enabled: true,
    };
    fetchPipeline.mockReset();
    fetchTriggers.mockReset();
    savePipeline.mockReset();
    deletePipeline.mockReset();
    triggerPipeline.mockReset();
    fetchRun.mockReset();
    runPipelineTest.mockReset();
    runPipelineTest.mockResolvedValue({ runId: "run-test-1" });
    fetchSources.mockReset();
    fetchPipeline.mockResolvedValue(POLICY);
    fetchTriggers.mockResolvedValue([]);
    fetchSources.mockResolvedValue({ kpis: [], sources: [SOURCE] });
    savePipeline.mockResolvedValue({});
    deletePipeline.mockResolvedValue(undefined);
    triggerPipeline.mockResolvedValue(outcome({ runIds: ["run-1"] }));
    fetchRun.mockResolvedValue({ status: "COMPLETED" });
    clearProcessedHistory.mockReset();
    clearProcessedHistory.mockResolvedValue(undefined);
    fetchS3Connections.mockReset();
    fetchS3Connections.mockResolvedValue([]);
    createIntegration.mockReset();
    uploadPipelineAsset.mockReset();
    uploadPipelineAsset.mockResolvedValue({
      id: "ast-1",
      fileName: "logo.png",
      contentType: "image/png",
      size: 1,
      createdAt: 0,
    });
    listPipelineAssets.mockReset();
    listPipelineAssets.mockResolvedValue([]);
  });

  // The settings of a node are reached by selecting it in the graph, so every helper below opens
  // its node first. Nodes are found by position rather than by label, because a node's title is
  // its current value - it changes as the pipeline is filled in.

  /** The graph's selectable nodes, in chain order: input, each step, output. */
  function graphNodes(): HTMLElement[] {
    return screen
      .getAllByRole("button")
      .filter((b) => b.hasAttribute("aria-pressed"));
  }

  /**
   * The control that opens an end of the chain, once the graph has rendered. A new pipeline has not
   * placed its ends yet, so that control is the "add" placeholder and clicking it both puts the node
   * on the chain and selects it; a loaded pipeline already has the node, so it is a plain select.
   */
  function endOpener(end: "input" | "output"): Promise<HTMLElement> {
    return waitFor(() => {
      const placeholder = screen.queryByText(
        `portal.pipelines.graph.add.${end}`,
      );
      if (placeholder) return placeholder;
      const nodes = graphNodes();
      if (nodes.length === 0) throw new Error("the graph has not rendered yet");
      return end === "input" ? nodes[0] : nodes[nodes.length - 1];
    });
  }

  async function openInput() {
    fireEvent.click(await endOpener("input"));
  }

  async function openOutput() {
    fireEvent.click(await endOpener("output"));
  }

  async function pickInputSource(sourceName: string) {
    await openInput();
    fireEvent.click(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.builder.inputSource",
      }),
    );
    fireEvent.click(await screen.findByText(sourceName));
  }

  /**
   * Add a tool. An empty chain offers the placeholder; once it has steps, the wires carry the
   * inserts instead.
   */
  async function addTool(toolName: string) {
    const placeholder = screen.queryByText(
      "portal.pipelines.graph.addFirstTool",
    );
    if (placeholder) {
      fireEvent.click(placeholder);
    } else {
      // The LAST wire, so repeated calls append. Taking the first would insert each new tool ahead
      // of the ones already there, silently reversing the order a caller asked for.
      const inserts = screen.getAllByLabelText(
        "portal.pipelines.graph.insertHere",
      );
      fireEvent.click(inserts[inserts.length - 1]);
    }
    fireEvent.click(await screen.findByText(toolName));
  }

  async function pickDestination() {
    await openOutput();
    fireEvent.click(await screen.findByText("pick output"));
  }

  /** Open the header's overflow tray. */
  async function openTray() {
    fireEvent.click(
      await screen.findByLabelText("portal.pipelines.builder.moreActions"),
    );
  }

  it("greets a new pipeline with places to fill, not problems to fix", async () => {
    renderBuilder("/processor/pipelines/new");

    // Both ends offer to be added rather than complaining about being empty: the user has not been
    // asked for a source or a destination yet, so there is nothing yet to warn them about.
    expect(
      await screen.findByText("portal.pipelines.graph.add.input"),
    ).toBeInTheDocument();
    expect(
      screen.getByText("portal.pipelines.graph.add.output"),
    ).toBeInTheDocument();
    expect(
      screen.queryByText("portal.pipelines.builder.needsSource"),
    ).not.toBeInTheDocument();
    expect(
      screen.queryByText("portal.pipelines.builder.needsDestination"),
    ).not.toBeInTheDocument();
    // Nothing is on the chain, so there is nothing to remove either.
    expect(
      screen.queryByLabelText(/portal.pipelines.graph.removeNode/),
    ).not.toBeInTheDocument();
  });

  it("warns on a step whose tool cannot run on its defaults", async () => {
    renderBuilder("/processor/pipelines/new");
    await addTool("OCR");

    // The tool declares its own mandatory parameters, so the node says so without the builder
    // knowing anything about OCR.
    expect(
      await screen.findByText("portal.pipelines.builder.needsConfiguring"),
    ).toBeInTheDocument();
    expect(
      screen.getByText("portal.pipelines.composer.create").closest("button"),
    ).toBeDisabled();
  });

  it("stays quiet on the wires around a step that still needs setting up", async () => {
    renderBuilder("/processor/pipelines/new");
    await addTool("OCR");

    // The node itself flags that it needs setting up...
    await screen.findByText("portal.pipelines.builder.needsConfiguring");
    // ...but the wires around it don't nag: an unconfigured step's operation is unknown, so neither
    // the wire into it nor the one out of it shows a compatibility warning until it is configured.
    expect(
      screen.queryByText(
        "portal.pipelines.builder.diagnostic.undeclared-operation",
      ),
    ).not.toBeInTheDocument();
  });

  it("clears the warning once the step is configured", async () => {
    renderBuilder("/processor/pipelines/new");
    await addTool("OCR");
    await screen.findByText("portal.pipelines.builder.needsConfiguring");

    // Adding a step selects it, so its settings are already open.
    fireEvent.click(screen.getByText("pick language"));

    await waitFor(() =>
      expect(
        screen.queryByText("portal.pipelines.builder.needsConfiguring"),
      ).not.toBeInTheDocument(),
    );
  });

  it("leaves a tool that runs happily on its defaults unwarned", async () => {
    renderBuilder("/processor/pipelines/new");
    await addTool("Compress");

    expect(
      await screen.findByText("portal.pipelines.graph.add.input"),
    ).toBeInTheDocument();
    expect(
      screen.queryByText("portal.pipelines.builder.needsConfiguring"),
    ).not.toBeInTheDocument();
  });

  it("only asks for a source once the user has asked for the node", async () => {
    renderBuilder("/processor/pipelines/new");
    await openInput();

    // Placing the node is what turns it into an outstanding choice.
    expect(
      await screen.findByText("portal.pipelines.builder.needsSource"),
    ).toBeInTheDocument();
    // Still nothing chosen, so the pipeline cannot be created.
    expect(
      screen.getByText("portal.pipelines.composer.create").closest("button"),
    ).toBeDisabled();
  });

  it("puts an end back to a placeholder when it is removed", async () => {
    renderBuilder("/processor/pipelines/new");
    await openInput();
    await screen.findByText("portal.pipelines.builder.needsSource");

    fireEvent.click(screen.getByLabelText("portal.pipelines.graph.removeNode"));

    expect(
      await screen.findByText("portal.pipelines.graph.add.input"),
    ).toBeInTheDocument();
    expect(
      screen.queryByText("portal.pipelines.builder.needsSource"),
    ).not.toBeInTheDocument();
  });

  it("edits a node's settings only once it is selected", async () => {
    renderBuilder("/processor/pipelines/new");
    await screen.findByText("portal.pipelines.graph.add.input");

    // Nothing selected: the inspector says so rather than showing a form.
    expect(
      screen.getByText("portal.pipelines.inspector.noSelectionTitle"),
    ).toBeInTheDocument();

    await openInput();
    expect(
      screen.getByRole("textbox", {
        name: "portal.pipelines.builder.inputSource",
      }),
    ).toBeInTheDocument();
  });

  it("builds a new pipeline: name it, add a tool, an input, a destination, and save", async () => {
    renderBuilder("/processor/pipelines/new");

    fireEvent.change(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.composer.name",
      }),
      {
        target: { value: "Nightly compress" },
      },
    );

    await addTool("Compress");

    // A pipeline must have at least one input source and one output destination.
    await pickInputSource("Claims intake");
    await pickDestination();

    fireEvent.click(screen.getByText("portal.pipelines.composer.create"));

    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    expect(savePipeline).toHaveBeenCalledWith(
      expect.objectContaining({
        name: "Nightly compress",
        // The input pairs the chosen source with its trigger (manual by default).
        inputs: [{ sourceId: "src-in", trigger: null }],
        outputIds: ["src-in"],
        steps: [
          expect.objectContaining({ operation: "/api/v1/misc/compress-pdf" }),
        ],
      }),
    );
    expect(await screen.findByText("pipelines list")).toBeInTheDocument();
  });

  it("blocks saving a chain whose steps can't run on each other", async () => {
    renderBuilder("/processor/pipelines/new");

    fireEvent.change(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.composer.name",
      }),
      { target: { value: "Broken chain" } },
    );
    await pickInputSource("Claims intake");
    await pickDestination();

    // Extract images emits images; compress only takes a PDF, so it can never run.
    await addTool("Extract images");
    await addTool("Compress");

    expect(
      await screen.findByText("portal.pipelines.builder.stepsIncompatible"),
    ).toBeInTheDocument();
    expect(
      screen.getByText("portal.pipelines.composer.create").closest("button"),
    ).toBeDisabled();
  });

  it("says why on the wire arriving at the step that cannot run", async () => {
    renderBuilder("/processor/pipelines/new");
    await pickInputSource("Claims intake");
    await pickDestination();

    await addTool("Extract images");
    await addTool("Compress");

    // The banner names which steps are at fault; the wire explains what is wrong where it happens.
    const note = await screen.findByText(
      /portal\.pipelines\.builder\.diagnostic\./,
    );
    expect(note.closest(".portal-graph-edge")).toHaveClass("is-blocking");
  });

  it("allows a chain whose steps line up", async () => {
    renderBuilder("/processor/pipelines/new");

    fireEvent.change(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.composer.name",
      }),
      { target: { value: "Fine chain" } },
    );
    await pickInputSource("Claims intake");
    await pickDestination();

    await addTool("Compress");

    expect(
      screen.queryByText("portal.pipelines.builder.stepsIncompatible"),
    ).not.toBeInTheDocument();
    expect(
      screen.getByText("portal.pipelines.composer.create").closest("button"),
    ).not.toBeDisabled();
  });

  it("requires at least one source and one destination before saving", async () => {
    renderBuilder("/processor/pipelines/new");

    fireEvent.change(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.composer.name",
      }),
      {
        target: { value: "Needs both" },
      },
    );
    const createButton = () =>
      screen.getByText("portal.pipelines.composer.create").closest("button");

    // Name only: blocked (no source, no destination).
    expect(createButton()).toBeDisabled();

    // An input with a source but still no destination: blocked.
    await pickInputSource("Claims intake");
    expect(createButton()).toBeDisabled();

    // Both chosen: allowed, and both are sent.
    await pickDestination();
    fireEvent.click(screen.getByText("portal.pipelines.composer.create"));
    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    expect(savePipeline).toHaveBeenCalledWith(
      expect.objectContaining({
        inputs: [{ sourceId: "src-in", trigger: null }],
        outputIds: ["src-in"],
      }),
    );
  });

  it("creates and edits sources in place through the modal", async () => {
    renderBuilder("/processor/pipelines/new");
    await pickInputSource("Claims intake");

    // Connect source opens the modal in create mode, without leaving the
    // builder (and its unsaved edits) for the Sources page.
    fireEvent.click(screen.getByText("portal.sources.actions.connectSource"));
    expect(screen.getByText("source-modal:new")).toBeInTheDocument();
    expect(screen.queryByText("pipelines list")).not.toBeInTheDocument();

    // The pencil beside the input opens the same modal on the chosen source.
    fireEvent.click(
      screen.getByLabelText("portal.pipelines.composer.editSource"),
    );
    expect(screen.getByText("source-modal:src-in")).toBeInTheDocument();
  });

  it("cannot edit an input source before one is chosen", async () => {
    renderBuilder("/processor/pipelines/new");
    await openInput();

    expect(
      screen.getByLabelText("portal.pipelines.composer.editSource"),
    ).toBeDisabled();
    await pickInputSource("Claims intake");
    expect(
      screen.getByLabelText("portal.pipelines.composer.editSource"),
    ).not.toBeDisabled();
  });

  it("makes a source created from the input row the pipeline's input", async () => {
    fetchSources
      .mockResolvedValueOnce({ kpis: [], sources: [SOURCE] })
      .mockResolvedValue({
        kpis: [],
        sources: [SOURCE, { ...SOURCE, id: "src-new", name: "Scanner drop" }],
      });
    renderBuilder("/processor/pipelines/new");
    await openInput();

    fireEvent.click(screen.getByText("portal.sources.actions.connectSource"));
    fireEvent.click(screen.getByText("source saved"));

    // The new source is the one the pipeline was missing, so it becomes the input.
    await waitFor(() =>
      expect(
        screen.getByRole("textbox", {
          name: "portal.pipelines.builder.inputSource",
        }),
      ).toHaveValue("Scanner drop"),
    );
  });

  it("selects the created destination even when another source arrives concurrently", async () => {
    createdSource = {
      ...createdSource,
      name: "Vector database destination",
      type: "vectordb",
    };
    fetchSources
      .mockResolvedValueOnce({ kpis: [], sources: [SOURCE] })
      .mockResolvedValue({
        kpis: [],
        sources: [
          SOURCE,
          {
            ...SOURCE,
            id: "src-unrelated",
            name: "Concurrent folder",
            type: "folder",
          },
          {
            ...SOURCE,
            id: "src-new",
            name: "Vector database destination",
            type: "vectordb",
          },
        ],
      });
    renderBuilder("/processor/pipelines/new");
    await openInput();
    openOutput();
    await screen.findByText("pick output");

    fireEvent.click(screen.getByText("new destination"));
    fireEvent.click(screen.getByText("source saved"));

    // Created from the destination picker, so it lands in the output rather
    // than the input.
    await waitFor(() =>
      expect(screen.getByText("output:src-new")).toBeInTheDocument(),
    );
    // The input was left alone: its node still shows the prompt.
    expect(
      screen.getByText("portal.pipelines.builder.chooseSource"),
    ).toBeInTheDocument();
  });

  it("leaves a new source that cannot be written to out of the destination", async () => {
    createdSource = { ...createdSource, id: "src-hook", type: "webhook" };
    // A webhook can be read from but not written to, so it must not be picked
    // as a destination the dropdown has no option for.
    fetchSources
      .mockResolvedValueOnce({ kpis: [], sources: [SOURCE] })
      .mockResolvedValue({
        kpis: [],
        sources: [
          SOURCE,
          { ...SOURCE, id: "src-hook", name: "Partner hook", type: "webhook" },
        ],
      });
    renderBuilder("/processor/pipelines/new");
    await openInput();
    openOutput();
    await screen.findByText("pick output");

    fireEvent.click(screen.getByText("new destination"));
    fireEvent.click(screen.getByText("source saved"));

    // It was not made the destination...
    await waitFor(() => expect(fetchSources).toHaveBeenCalledTimes(2));
    expect(screen.getByText("pick output")).toBeInTheDocument();

    // ...but it did arrive, and is offered as an input, where a webhook makes sense.
    await openInput();
    fireEvent.click(
      screen.getByRole("textbox", {
        name: "portal.pipelines.builder.inputSource",
      }),
    );
    expect(await screen.findByText("Partner hook")).toBeInTheDocument();
  });

  it("edits the chosen destination through the same modal", async () => {
    renderBuilder("/processor/pipelines/new");
    await pickDestination();

    fireEvent.click(screen.getByText("edit destination"));
    expect(screen.getByText("source-modal:src-in")).toBeInTheDocument();
  });

  it("saves an editor pipeline as its own flag, not as a wire input", async () => {
    fetchSources.mockResolvedValue({
      kpis: [],
      sources: [SOURCE, EDITOR_SOURCE],
    });
    renderBuilder("/processor/pipelines/new");
    fireEvent.change(
      await screen.findByLabelText("portal.pipelines.composer.name"),
      { target: { value: "Label on upload" } },
    );
    await addTool("Compress");
    await pickInputSource("Editor");

    fireEvent.click(screen.getByText("portal.pipelines.composer.create"));

    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    const body = savePipeline.mock.calls[0][0];
    // The editor is virtual: nothing sweeps it server-side, so it is recorded as the policy's own
    // editor flag rather than as an input the backend would try to pull from.
    expect(body.inputs).toEqual([]);
    expect(body.editor).toEqual({ allowed: true, runOn: "upload" });
    // And it needs no destination - results land back in the workspace the file came from.
    expect(body.outputIds).toEqual([]);
  });

  it("keeps external delivery selected when a saved destination is cleared", async () => {
    fetchSources.mockResolvedValue({
      kpis: [],
      sources: [SOURCE, EDITOR_SOURCE],
    });
    fetchPipeline.mockResolvedValue({
      ...POLICY,
      inputs: [],
      editor: { allowed: true, runOn: "upload" },
      outputIds: ["src-in"],
      routingRules: [],
    });
    renderBuilder("/processor/pipelines/plc-1");
    await screen.findByLabelText("portal.pipelines.builder.rename");
    await openOutput();
    fireEvent.click(await screen.findByText("clear destination"));
    expect(
      screen.getByRole("textbox", {
        name: "portal.policies.wizard.locations.delivery",
      }),
    ).toHaveValue("portal.policies.wizard.locations.keepOriginal");
    expect(screen.getByText("pick output")).toBeInTheDocument();
    expect(
      screen.getByRole("button", { name: "portal.pipelines.composer.save" }),
    ).toBeDisabled();
  });

  it("clears hidden routing blockers when returning results to the editor", async () => {
    HTMLElement.prototype.scrollIntoView = vi.fn();
    fetchSources.mockResolvedValue({
      kpis: [],
      sources: [SOURCE, EDITOR_SOURCE],
    });
    fetchPipeline.mockResolvedValue({
      ...POLICY,
      inputs: [],
      editor: { allowed: true, runOn: "upload" },
      outputIds: ["src-in"],
      routingRules: [routingRule([])],
    });
    renderBuilder("/processor/pipelines/plc-1");
    await screen.findByLabelText("portal.pipelines.builder.rename");
    await openOutput();
    fireEvent.click(
      screen.getByRole("textbox", {
        name: "portal.policies.wizard.locations.delivery",
      }),
    );
    fireEvent.click(
      await screen.findByText(
        "portal.policies.wizard.locations.returnToEditor",
      ),
    );
    fireEvent.click(screen.getByText("portal.pipelines.composer.save"));
    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    expect(savePipeline.mock.calls[0][0]).toMatchObject({
      outputIds: [],
      routingRules: [],
    });
  });

  it("saves a separate destination for an editor copy", async () => {
    HTMLElement.prototype.scrollIntoView = vi.fn();
    fetchSources.mockResolvedValue({
      kpis: [],
      sources: [SOURCE, EDITOR_SOURCE],
    });
    renderBuilder("/processor/pipelines/new");
    fireEvent.change(
      await screen.findByLabelText("portal.pipelines.composer.name"),
      { target: { value: "Copy to archive" } },
    );
    await addTool("Compress");
    await pickInputSource("Editor");
    await openOutput();
    fireEvent.click(
      await screen.findByRole("textbox", {
        name: "portal.policies.wizard.locations.delivery",
      }),
    );
    fireEvent.click(
      await screen.findByText("portal.policies.wizard.locations.keepOriginal"),
    );
    expect(
      screen.getByRole("button", {
        name: "portal.pipelines.composer.create",
      }),
    ).toBeDisabled();
    fireEvent.click(await screen.findByText("pick output"));
    fireEvent.click(screen.getByText("portal.pipelines.composer.create"));
    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    expect(savePipeline.mock.calls[0][0]).toMatchObject({
      editor: { allowed: true, runOn: "upload" },
      required: false,
      inputs: [],
      outputIds: ["src-in"],
    });
  });

  it("blocks a vector route with a file fallback until chunks are prepared", async () => {
    fetchSources.mockResolvedValue({
      kpis: [],
      sources: [SOURCE, { ...DESTINATION_SOURCE, type: "vectordb" }],
    });
    fetchPipeline.mockResolvedValue({
      ...POLICY,
      inputs: [{ sourceId: "src-in", trigger: null }],
      steps: [CLASSIFY_STEP],
      outputIds: ["src-in"],
      routingRules: [routingRule(["invoice"])],
    });
    renderBuilder("/processor/pipelines/plc-1");
    fireEvent.click(
      await screen.findByLabelText("portal.pipelines.builder.rename"),
    );
    fireEvent.change(
      await screen.findByLabelText("portal.pipelines.composer.name"),
      {
        target: { value: "Renamed" },
      },
    );
    await openOutput();
    expect(
      screen.getAllByText(
        "portal.pipelines.builder.ingest.destinationNeedsChunks",
      ).length,
    ).toBeGreaterThan(0);
    fireEvent.click(screen.getByText("portal.pipelines.composer.save"));
    expect(savePipeline).not.toHaveBeenCalled();
  });

  it("carries a saved pipeline's routes through an unrelated edit", async () => {
    fetchSources.mockResolvedValue({
      kpis: [],
      sources: [SOURCE, DESTINATION_SOURCE],
    });
    const routed = routingRule(["invoice"]);
    fetchPipeline.mockResolvedValue({
      ...POLICY,
      inputs: [{ sourceId: "src-in", trigger: null }],
      // Routing reads the verdict this step writes, so a routing pipeline carries one.
      steps: [CLASSIFY_STEP],
      outputIds: ["src-1"],
      routingRules: [routed],
    });
    renderBuilder("/processor/pipelines/plc-1");

    // The name is a heading until the rename affordance is used.
    fireEvent.click(
      await screen.findByLabelText("portal.pipelines.builder.rename"),
    );
    fireEvent.change(
      await screen.findByLabelText("portal.pipelines.composer.name"),
      { target: { value: "Renamed" } },
    );
    fireEvent.click(screen.getByText("portal.pipelines.composer.save"));

    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    // Routing is edited on the destination node; renaming the pipeline must not drop it.
    expect(savePipeline.mock.calls[0][0].routingRules).toEqual([routed]);
  });

  it("refuses to save a route with no document types on it", async () => {
    fetchSources.mockResolvedValue({
      kpis: [],
      sources: [SOURCE, DESTINATION_SOURCE],
    });
    fetchPipeline.mockResolvedValue({
      ...POLICY,
      inputs: [{ sourceId: "src-in", trigger: null }],
      steps: [CLASSIFY_STEP],
      outputIds: ["src-1"],
      // What the toggle seeds: a rule the user has not filled in yet.
      routingRules: [routingRule([])],
    });
    renderBuilder("/processor/pipelines/plc-1");

    fireEvent.click(
      await screen.findByLabelText("portal.pipelines.builder.rename"),
    );
    fireEvent.change(
      await screen.findByLabelText("portal.pipelines.composer.name"),
      { target: { value: "Renamed" } },
    );
    fireEvent.click(screen.getByText("portal.pipelines.composer.save"));

    // The backend's PolicyValidator would reject this; the builder says so first.
    await waitFor(() => expect(savePipeline).not.toHaveBeenCalled());
  });

  it("refuses to save routes whose classify step has been removed", async () => {
    fetchSources.mockResolvedValue({
      kpis: [],
      sources: [SOURCE, DESTINATION_SOURCE],
    });
    fetchPipeline.mockResolvedValue({
      ...POLICY,
      inputs: [{ sourceId: "src-in", trigger: null }],
      // Rules outliving the step that feeds them: every document would fall to the fallback.
      steps: [],
      outputIds: ["src-1"],
      routingRules: [routingRule(["invoice"])],
    });
    renderBuilder("/processor/pipelines/plc-1");

    fireEvent.click(
      await screen.findByLabelText("portal.pipelines.builder.rename"),
    );
    fireEvent.change(
      await screen.findByLabelText("portal.pipelines.composer.name"),
      { target: { value: "Renamed" } },
    );
    fireEvent.click(screen.getByText("portal.pipelines.composer.save"));

    await waitFor(() => expect(savePipeline).not.toHaveBeenCalled());
  });

  it("runs an existing pipeline and reports success", async () => {
    renderBuilder("/processor/pipelines/plc-1");

    fireEvent.click(await screen.findByText("portal.pipelines.detail.run"));

    await waitFor(() => expect(triggerPipeline).toHaveBeenCalledWith("plc-1"));
    expect(
      await screen.findByText("portal.pipelines.run.completed"),
    ).toBeInTheDocument();
  });

  it("explains an empty trigger when files are parked by a failed run", async () => {
    triggerPipeline.mockResolvedValue(outcome({ filesListed: 2, parked: 2 }));
    renderBuilder("/processor/pipelines/plc-1");

    fireEvent.click(await screen.findByText("portal.pipelines.detail.run"));

    expect(
      await screen.findByText("portal.pipelines.run.parked"),
    ).toBeInTheDocument();
  });

  it("explains an empty trigger when everything is already processed", async () => {
    triggerPipeline.mockResolvedValue(
      outcome({ filesListed: 3, alreadyProcessed: 3 }),
    );
    renderBuilder("/processor/pipelines/plc-1");

    fireEvent.click(await screen.findByText("portal.pipelines.detail.run"));

    expect(
      await screen.findByText("portal.pipelines.run.allProcessed"),
    ).toBeInTheDocument();
  });

  it("reprocesses the source: clears the processed record, runs, and reports", async () => {
    renderBuilder("/processor/pipelines/plc-1");

    await openTray();
    fireEvent.click(screen.getByText("portal.pipelines.detail.clearHistory"));

    // It forgets what was processed, then triggers a run so those files go through now.
    await waitFor(() =>
      expect(clearProcessedHistory).toHaveBeenCalledWith("plc-1"),
    );
    await waitFor(() => expect(triggerPipeline).toHaveBeenCalledWith("plc-1"));
    expect(
      await screen.findByText("portal.pipelines.run.completed"),
    ).toBeInTheDocument();
  });

  function twoStepPolicy(): Policy {
    return {
      ...POLICY,
      steps: [
        { operation: "/api/v1/misc/compress-pdf", parameters: {} },
        { operation: "/api/v1/misc/extract-images", parameters: {} },
      ],
    };
  }

  async function runTestWith(view: Record<string, unknown>) {
    fetchRun.mockResolvedValue({
      runId: "run-test-1",
      stepCount: 2,
      outputs: [],
      error: null,
      ...view,
    });
    renderBuilder("/processor/pipelines/plc-1");
    await screen.findByText("portal.pipelines.builder.testRun");
    const picker = document.querySelector<HTMLInputElement>(
      'input[type="file"][accept="application/pdf"]',
    );
    if (!picker) throw new Error("test-run file input not rendered");
    fireEvent.change(picker, {
      target: {
        files: [new File(["x"], "in.pdf", { type: "application/pdf" })],
      },
    });
    await waitFor(() => expect(runPipelineTest).toHaveBeenCalledTimes(1));
  }

  it("blames the step that actually failed, not the one after it", async () => {
    fetchPipeline.mockResolvedValue(twoStepPolicy());

    await runTestWith({ status: "FAILED", currentStep: 1, error: "boom" });

    await screen.findByLabelText("portal.pipelines.graph.showError");
    expect(screen.queryByText("portal.pipelines.graph.run.done")).toBeNull();
  });

  it("marks only the steps that finished before the one still running", async () => {
    fetchPipeline.mockResolvedValue(twoStepPolicy());

    await runTestWith({ status: "RUNNING", currentStep: 2 });

    await waitFor(() =>
      expect(
        screen.getAllByText("portal.pipelines.graph.run.done"),
      ).toHaveLength(1),
    );
  });

  it("retires a test result once the chain it ran against changes", async () => {
    fetchPipeline.mockResolvedValue(twoStepPolicy());

    await runTestWith({ status: "COMPLETED", currentStep: 2 });
    await waitFor(() =>
      expect(
        screen.getAllByText("portal.pipelines.graph.run.done"),
      ).toHaveLength(2),
    );

    await addTool("OCR");

    await waitFor(() =>
      expect(screen.queryByText("portal.pipelines.graph.run.done")).toBeNull(),
    );
  });

  it("uploads a step's supporting file and saves it as an asset binding", async () => {
    renderBuilder("/processor/pipelines/new");

    fireEvent.change(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.composer.name",
      }),
      {
        target: { value: "Watermarked" },
      },
    );
    await addTool("Compress");
    // The tool's settings attach a supporting file.
    fireEvent.click(await screen.findByText("upload logo"));

    await pickInputSource("Claims intake");
    await pickDestination();

    fireEvent.click(screen.getByText("portal.pipelines.composer.create"));

    // The file is uploaded to the asset store first, then the policy is saved binding that asset.
    await waitFor(() => expect(uploadPipelineAsset).toHaveBeenCalledTimes(1));
    expect(uploadPipelineAsset.mock.calls[0][0]).toBeInstanceOf(File);
    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    expect(savePipeline).toHaveBeenCalledWith(
      expect.objectContaining({
        steps: [
          expect.objectContaining({
            operation: "/api/v1/misc/compress-pdf",
            fileParameters: { watermarkImage: "asset:ast-1" },
          }),
        ],
      }),
    );
  });

  it("keeps a step's stored file binding on save when the tool can't be probed", async () => {
    // buildFormData throws for `sign`, so activeFileFields is null. The stored binding must survive
    // the save unchanged - dropping it would let the server GC the user's uploaded file - and no
    // re-upload should happen.
    fetchPipeline.mockResolvedValue({
      id: "plc-sign",
      name: "Signed",
      enabled: true,
      inputs: [{ sourceId: "src-in", trigger: null }],
      steps: [
        {
          operation: "/api/v1/security/cert-sign",
          parameters: {},
          fileParameters: { certFile: "asset:x" },
        },
      ],
      output: { type: "inline", options: {} },
      outputIds: ["src-in"],
    });
    renderBuilder("/processor/pipelines/plc-sign");

    fireEvent.click(await screen.findByText("portal.pipelines.composer.save"));

    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    expect(savePipeline).toHaveBeenCalledWith(
      expect.objectContaining({
        steps: [
          expect.objectContaining({
            operation: "/api/v1/security/cert-sign",
            fileParameters: { certFile: "asset:x" },
          }),
        ],
      }),
    );
    expect(uploadPipelineAsset).not.toHaveBeenCalled();
  });

  it("blocks saving an integration step with no account chosen", async () => {
    // A Discord step added but left without an account would fail at run time with a raw backend
    // rejection; the builder must refuse to save it and say why, where the fix is one click away.
    renderBuilder("/processor/pipelines/new");

    fireEvent.change(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.composer.name",
      }),
      {
        target: { value: "Notify only" },
      },
    );
    await addTool("portal.policies.operations.discordNotify.label");

    // Operation chosen, account not: still not saveable.
    expect(
      await screen.findByText("portal.pipelines.builder.stepsNeedSetup"),
    ).toBeInTheDocument();
    expect(
      screen.getByText("portal.pipelines.composer.create").closest("button"),
    ).toBeDisabled();
  });

  it("deletes an existing pipeline after confirmation", async () => {
    renderBuilder("/processor/pipelines/plc-1");

    // Delete is a rare, destructive action, so it lives behind the overflow tray.
    await openTray();
    fireEvent.click(await screen.findByText("portal.pipelines.detail.delete"));
    fireEvent.click(await screen.findByText("portal.pipelines.delete.confirm"));

    await waitFor(() => expect(deletePipeline).toHaveBeenCalledWith("plc-1"));
    expect(await screen.findByText("pipelines list")).toBeInTheDocument();
  });

  it("pauses a live pipeline at once, in place, and re-saves the persisted policy", async () => {
    renderBuilder("/processor/pipelines/plc-1");

    // POLICY.enabled is true, so the toggle offers to pause it.
    fireEvent.click(await screen.findByText("portal.pipelines.builder.pause"));

    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    expect(savePipeline).toHaveBeenCalledWith(
      expect.objectContaining({ id: "plc-1", enabled: false }),
    );
    // It acts in place: the builder stays open and the control now offers to activate again.
    expect(
      await screen.findByText("portal.pipelines.builder.activate"),
    ).toBeInTheDocument();
    expect(screen.queryByText("pipelines list")).not.toBeInTheDocument();
  });

  it("creates a paused pipeline when Create paused is chosen", async () => {
    renderBuilder("/processor/pipelines/new");

    fireEvent.change(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.composer.name",
      }),
      { target: { value: "Paused draft" } },
    );
    await addTool("Compress");
    await pickInputSource("Claims intake");
    await pickDestination();

    fireEvent.click(screen.getByText("portal.pipelines.composer.createPaused"));

    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    expect(savePipeline).toHaveBeenCalledWith(
      expect.objectContaining({ name: "Paused draft", enabled: false }),
    );
    expect(await screen.findByText("pipelines list")).toBeInTheDocument();
  });

  it("prompts to save or discard when leaving with unsaved edits", async () => {
    renderBuilder("/processor/pipelines/new");

    fireEvent.change(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.composer.name",
      }),
      {
        target: { value: "Draft" },
      },
    );
    fireEvent.click(screen.getByLabelText("portal.pipelines.builder.back"));

    expect(
      await screen.findByText("portal.pipelines.builder.unsavedTitle"),
    ).toBeInTheDocument();
    fireEvent.click(screen.getByText("portal.pipelines.builder.discard"));
    expect(await screen.findByText("pipelines list")).toBeInTheDocument();
  });

  it("keeps the account chosen for an integration step", async () => {
    // The regression: integration steps are deliberately toolId-less, and the builder's param
    // update used to skip exactly those, so picking an account looked like it did nothing.
    fetchS3Connections.mockResolvedValue([
      {
        id: 9,
        name: "Ops alerts",
        integrationType: "API",
        config: { presetId: "discord" },
      },
    ]);
    renderBuilder("/processor/pipelines/new");

    fireEvent.change(
      await screen.findByRole("textbox", {
        name: "portal.pipelines.composer.name",
      }),
      {
        target: { value: "Notify on processed" },
      },
    );

    await addTool("portal.policies.operations.discordNotify.label");

    fireEvent.click(
      await screen.findByPlaceholderText(
        "portal.connections.picker.placeholder",
      ),
    );
    fireEvent.click(await screen.findByText("Ops alerts"));

    // Saving needs the input's source and a destination.
    await pickInputSource("Claims intake");
    await pickDestination();

    fireEvent.click(screen.getByText("portal.pipelines.composer.create"));

    await waitFor(() => expect(savePipeline).toHaveBeenCalledTimes(1));
    const saved = savePipeline.mock.calls[0][0] as Policy;
    const step = saved.steps[0] as unknown as {
      operation: string;
      parameters: Record<string, string>;
    };
    expect(step.operation).toBe("/api/v1/integration/external-api-call");
    // The selection survived all the way to the wire, not just to the dropdown.
    expect(step.parameters.connectionId).toBe("9");
    expect(step.parameters.operationId).toBe("discordNotify");
  });

  it("leaves immediately when there are no unsaved edits", async () => {
    renderBuilder("/processor/pipelines/new");

    await screen.findByRole("textbox", {
      name: "portal.pipelines.composer.name",
    });
    fireEvent.click(screen.getByLabelText("portal.pipelines.builder.back"));

    expect(await screen.findByText("pipelines list")).toBeInTheDocument();
  });
});