Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

2.2 KB
Raw Blame
import { fireEvent, render, screen } from "@testing-library/react";

const automationRow = {
  id: "auto-1",
  name: "Ping webhook",
  trigger: { id: "trig-1" },
  action: { type: "WEBHOOK" },
};

vi.mock("next-auth/react", () => ({
  useSession: () => ({ data: null, status: "unauthenticated" }),
}));

vi.mock("@/src/utils/api", () => ({
  api: {
    automations: {
      getAutomations: {
        useQuery: () => ({ data: [automationRow], isSuccess: true }),
      },
    },
    useUtils: () => ({
      automations: { getAutomations: { fetch: vi.fn(async () => []) } },
    }),
  },
}));

import { MonitorAutomationsPanel } from "./MonitorAutomationsPanel";

describe("MonitorAutomationsPanel access gating", () => {
  it("read-only access: clicking a row does not toggle the trigger", () => {
    const onTriggerIdsChange = vi.fn();
    render(
      <MonitorAutomationsPanel
        projectId="p1"
        hasAccess={false}
        triggerIds={[]}
        onTriggerIdsChange={onTriggerIdsChange}
      />,
    );

    fireEvent.click(screen.getByRole("button", { name: /ping webhook/i }));
    expect(onTriggerIdsChange).not.toHaveBeenCalled();
  });

  it("write access: clicking a row toggles the trigger", () => {
    const onTriggerIdsChange = vi.fn();
    render(
      <MonitorAutomationsPanel
        projectId="p1"
        hasAccess={true}
        triggerIds={[]}
        onTriggerIdsChange={onTriggerIdsChange}
      />,
    );

    fireEvent.click(screen.getByRole("button", { name: /ping webhook/i }));
    expect(onTriggerIdsChange).toHaveBeenCalledTimes(1);
  });

  it("read-only access: the add-automation trigger is disabled", () => {
    render(
      <MonitorAutomationsPanel
        projectId="p1"
        hasAccess={false}
        triggerIds={[]}
        onTriggerIdsChange={vi.fn()}
      />,
    );
    expect(screen.getByRole("button", { name: /automation/i })).toBeDisabled();
  });

  it("write access: the add-automation trigger is enabled", () => {
    render(
      <MonitorAutomationsPanel
        projectId="p1"
        hasAccess={true}
        triggerIds={[]}
        onTriggerIdsChange={vi.fn()}
      />,
    );
    expect(
      screen.getByRole("button", { name: /automation/i }),
    ).not.toBeDisabled();
  });
});