Skip to content

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

11.8 KB
Raw Blame
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, useLocation } from "react-router-dom";
import type {
  PipelinesOverviewResponse,
  Policy,
} from "@processor/api/pipelines";
import { parseSimplePolicy } from "@processor/api/policies";
import { Pipelines } from "@processor/views/Pipelines";

/** The builder route: shows the draft handed in navigation state, so the Customise hand-off can be
 * asserted without rendering the real builder. */
function DraftProbe() {
  const draft = (useLocation().state as { draft?: Policy } | null)?.draft;
  return (
    <div>
      pipeline page
      <span data-testid="draft-icon">{draft?.icon ?? ""}</span>
      <span data-testid="draft-name">{draft?.name ?? ""}</span>
      <span data-testid="draft-enabled">{String(draft?.enabled ?? "")}</span>
    </div>
  );
}

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

// Deterministic i18n: keys returned verbatim. initReactI18next/Trans are exported too because the
// unified page pulls in modules (the policy wizard/catalogue) that reference them at import time.
vi.mock("react-i18next", () => ({
  useTranslation: () => ({
    t: (key: string) => key,
    i18n: { changeLanguage: vi.fn() },
  }),
  initReactI18next: { type: "3rdParty", init: () => {} },
  Trans: (props: { children?: unknown }) => props.children,
}));

const fetchPipelines = vi.fn();
const fetchPipeline = vi.fn();
const fetchPolicyPermissions = vi.fn();
vi.mock("@processor/api/pipelines", () => ({
  fetchTriggers: () => Promise.resolve([]),
  fetchPipelines: () => fetchPipelines(),
  fetchPipeline: (id: string) => fetchPipeline(id),
  fetchPolicyPermissions: () => fetchPolicyPermissions(),
}));

// Spy the wizard's save without stubbing the rest of the module (parseSimplePolicy et al. stay real).
const savePolicy = vi.fn();
vi.mock("@processor/api/policies", async (importOriginal) => {
  const actual =
    await importOriginal<typeof import("@processor/api/policies")>();
  return { ...actual, savePolicy: (body: unknown) => savePolicy(body) };
});

const usePoliciesOverview = vi.fn();
vi.mock("@processor/queries/policies", () => ({
  usePoliciesOverview: () => usePoliciesOverview(),
}));

const RESPONSE: PipelinesOverviewResponse = {
  kpis: [
    { value: 2, description: "" },
    { value: 2, description: "" },
    { value: 0, description: "" },
  ],
  pipelines: [
    {
      id: "plc-redaction",
      name: "Redaction sweep",
      enabled: true,
      required: false,
      icon: "security",
      status: "active",
      trigger: "schedule",
      sources: [{ id: "src-claims", name: "Claims intake" }],
      steps: ["/api/v1/security/auto-redact"],
      output: "inline",
      owner: "[email protected]",
    },
  ],
};

function renderView(initial = "/processor/pipelines") {
  return render(
    <MemoryRouter initialEntries={[initial]}>
      <Routes>
        <Route path="/processor/pipelines" element={<Pipelines />} />
        <Route
          path="/processor/pipelines/new"
          element={<div>builder new</div>}
        />
        <Route path="/processor/pipelines/:id" element={<DraftProbe />} />
      </Routes>
    </MemoryRouter>,
  );
}

describe("Pipelines view", () => {
  beforeEach(() => {
    fetchPipelines.mockReset();
    fetchPipelines.mockResolvedValue(RESPONSE);
    fetchPipeline.mockReset();
    // A plain pipeline (no template origin) - parseSimplePolicy returns null, so the row opens the
    // full builder page.
    fetchPipeline.mockResolvedValue({
      id: "plc-redaction",
      name: "Redaction sweep",
      enabled: true,
      inputs: [],
      steps: [{ operation: "/api/v1/security/auto-redact", parameters: {} }],
      output: { type: "inline", options: {} },
      outputIds: [],
    });
    savePolicy.mockReset();
    savePolicy.mockResolvedValue(undefined);
    fetchPolicyPermissions.mockReset();
    fetchPolicyPermissions.mockResolvedValue({ canManagePolicies: true });
    usePoliciesOverview.mockReturnValue({
      data: null,
      loading: false,
      error: null,
    });
  });

  /** A template-representable, currently-paused policy. */
  const pausedPolicy: Policy = {
    id: "plc-redaction",
    name: "Redaction sweep",
    enabled: false,
    required: false,
    icon: "shield",
    inputs: [],
    steps: [{ operation: "/api/v1/security/auto-redact", parameters: {} }],
    output: { type: "inline", options: { categoryId: "security" } },
    outputIds: [],
    editor: { allowed: true, runOn: "upload" },
  };

  it("opens the builder when creating a pipeline", async () => {
    renderView();
    await screen.findByText("Redaction sweep");
    fireEvent.click(
      screen.getByText("portal.pipelines.actions.newCustomPipeline"),
    );
    expect(await screen.findByText("builder new")).toBeInTheDocument();
  });

  it("opens the full builder when a plain pipeline row is clicked", async () => {
    renderView();
    fireEvent.click(await screen.findByText("Redaction sweep"));
    expect(await screen.findByText("pipeline page")).toBeInTheDocument();
  });

  it("opens template settings directly without the usage details modal", async () => {
    fetchPipeline.mockResolvedValue(pausedPolicy);

    renderView();
    fireEvent.click(await screen.findByText("Redaction sweep"));

    expect(
      await screen.findByText("portal.policies.wizard.actions.saveChanges"),
    ).toBeInTheDocument();
    expect(
      screen.queryByText("portal.policies.detail.recentActivity"),
    ).not.toBeInTheDocument();
    expect(
      screen.queryByText("portal.policies.stats.docsEnforced"),
    ).not.toBeInTheDocument();
  });

  it("opens settings directly for an existing template setup link", async () => {
    usePoliciesOverview.mockReturnValue({
      data: { catalogue: [parseSimplePolicy(pausedPolicy, [])] },
      loading: false,
      error: null,
    });

    renderView("/processor/pipelines?setup=security");

    expect(
      await screen.findByText("portal.policies.wizard.actions.saveChanges"),
    ).toBeInTheDocument();
    expect(
      screen.queryByText("portal.policies.detail.recentActivity"),
    ).not.toBeInTheDocument();
    fireEvent.click(screen.getByText("portal.policies.wizard.actions.cancel"));
    await waitFor(() =>
      expect(screen.queryByRole("dialog")).not.toBeInTheDocument(),
    );
  });

  it("opens the full builder for a template with custom pipeline steps", async () => {
    fetchPipeline.mockResolvedValue({
      ...pausedPolicy,
      steps: [{ operation: "/api/v1/misc/compress-pdf", parameters: {} }],
    });

    renderView();
    fireEvent.click(await screen.findByText("Redaction sweep"));

    expect(await screen.findByText("pipeline page")).toBeInTheDocument();
  });

  it("keeps the custom icon and name when customising from the wizard", async () => {
    const policy = {
      id: "plc-redaction",
      name: "My custom redaction",
      enabled: true,
      required: false,
      icon: "shield",
      inputs: [],
      steps: [{ operation: "/api/v1/security/auto-redact", parameters: {} }],
      output: { type: "inline", options: { categoryId: "security" } },
      outputIds: [],
      editor: { allowed: true, runOn: "export" },
    };
    fetchPipeline.mockResolvedValue(policy);

    renderView();
    fireEvent.click(await screen.findByText("Redaction sweep"));
    fireEvent.click(
      await screen.findByText("portal.policies.wizard.actions.customise"),
    ); // hand off to the builder

    // The draft carried into the builder keeps the stored icon and name, not the category default.
    expect(await screen.findByTestId("draft-icon")).toHaveTextContent("shield");
    expect(screen.getByTestId("draft-name")).toHaveTextContent(
      "My custom redaction",
    );
  });

  it("keeps a paused policy paused when saved from the wizard", async () => {
    fetchPipeline.mockResolvedValue(pausedPolicy);

    renderView();
    fireEvent.click(await screen.findByText("Redaction sweep"));
    fireEvent.click(
      await screen.findByText("portal.policies.wizard.actions.saveChanges"),
    );

    await waitFor(() => expect(savePolicy).toHaveBeenCalled());
    // The wizard has no enabled control, so a save must not silently re-enable a paused policy.
    expect(savePolicy.mock.calls[0][0]).toMatchObject({ enabled: false });
  });

  it("keeps a paused policy paused when customising from the wizard", async () => {
    fetchPipeline.mockResolvedValue(pausedPolicy);

    renderView();
    fireEvent.click(await screen.findByText("Redaction sweep"));
    fireEvent.click(
      await screen.findByText("portal.policies.wizard.actions.customise"),
    ); // hand off to the builder

    expect(await screen.findByTestId("draft-enabled")).toHaveTextContent(
      "false",
    );
  });

  it("prevents a non-manager from saving template settings", async () => {
    fetchPolicyPermissions.mockResolvedValue({ canManagePolicies: false });
    fetchPipeline.mockResolvedValue({
      id: "plc-redaction",
      name: "Redaction sweep",
      enabled: true,
      required: true,
      icon: "shield",
      inputs: [],
      steps: [{ operation: "/api/v1/security/auto-redact", parameters: {} }],
      output: { type: "inline", options: { categoryId: "security" } },
      outputIds: [],
      editor: { allowed: true, runOn: "upload" },
    });

    renderView();
    fireEvent.click(await screen.findByText("Redaction sweep"));

    const save = await screen.findByRole("button", {
      name: "portal.policies.wizard.actions.saveChanges",
    });
    expect(save).toBeDisabled();
    fireEvent.click(save);
    expect(savePolicy).not.toHaveBeenCalled();
  });

  it("prevents saving while the permission check is still loading", async () => {
    // A check that never settles: canManage stays fail-closed false, isLoading stays true.
    fetchPolicyPermissions.mockReset();
    fetchPolicyPermissions.mockReturnValue(new Promise(() => {}));
    fetchPipeline.mockResolvedValue({
      id: "plc-redaction",
      name: "Redaction sweep",
      enabled: true,
      required: true,
      icon: "shield",
      inputs: [],
      steps: [{ operation: "/api/v1/security/auto-redact", parameters: {} }],
      output: { type: "inline", options: { categoryId: "security" } },
      outputIds: [],
      editor: { allowed: true, runOn: "upload" },
    });

    renderView();
    fireEvent.click(await screen.findByText("Redaction sweep"));

    expect(
      await screen.findByRole("button", {
        name: "portal.policies.wizard.actions.saveChanges",
      }),
    ).toBeDisabled();
  });

  it("warns with a retry when the permission check fails, instead of locking silently", async () => {
    fetchPolicyPermissions.mockReset();
    fetchPolicyPermissions.mockRejectedValueOnce(new Error("boom"));
    fetchPolicyPermissions.mockResolvedValue({ canManagePolicies: true });

    renderView();
    expect(
      await screen.findByText("portal.pipelines.permissionsUnavailable"),
    ).toBeInTheDocument();

    // Retry refetches; on success the warning clears.
    fireEvent.click(screen.getByText("portal.pipelines.permissionsRetry"));
    await waitFor(() =>
      expect(
        screen.queryByText("portal.pipelines.permissionsUnavailable"),
      ).not.toBeInTheDocument(),
    );
  });

  it("shows create + connect-source CTAs when empty", async () => {
    fetchPipelines.mockResolvedValue({
      kpis: [
        { value: 0, description: "" },
        { value: 0, description: "" },
        { value: 0, description: "" },
      ],
      pipelines: [],
    });
    renderView();
    expect(
      await screen.findByText("portal.pipelines.empty.title"),
    ).toBeInTheDocument();
    expect(
      screen.getByText("portal.pipelines.empty.connectSource"),
    ).toBeInTheDocument();
  });
});