Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
14.5 KB
Raw Blame
// @vitest-environment jsdom

import { describe, it, expect, vi, beforeEach } from "vitest";
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { I18nProvider } from "@multica/core/i18n/react";
import enCommon from "../../locales/en/common.json";
import enAgents from "../../locales/en/agents.json";
import enBilling from "../../locales/en/billing.json";
import { ApiError } from "@multica/core/api";
import type { MemberWithUser, WorkspaceFeatures } from "@multica/core/types";
import { toast } from "sonner";

const TEST_RESOURCES = { en: { common: enCommon, agents: enAgents, billing: enBilling } };

const updateAgentSpy = vi.hoisted(() =>
  vi.fn(async (_id: string, _patch: Record<string, unknown>) => ({})),
);
const archiveSpy = vi.hoisted(() => vi.fn(async () => ({})));
const restoreSpy = vi.hoisted(() => vi.fn(async () => ({})));
const featuresSpy = vi.hoisted(() =>
  vi.fn(async (): Promise<WorkspaceFeatures> => ({ features: {} })),
);

vi.mock("@multica/core/hooks", () => ({
  useWorkspaceId: () => "ws-1",
}));
vi.mock("@multica/core/api", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@multica/core/api")>()),
  api: {
    updateAgent: updateAgentSpy,
    archiveAgent: archiveSpy,
    restoreAgent: restoreSpy,
    getWorkspaceFeatures: featuresSpy,
  },
}));
vi.mock("@multica/core/paths", async (importOriginal) => ({
  ...(await importOriginal<object>()),
  useWorkspacePaths: () => ({ settings: () => "/acme/settings" }),
}));
vi.mock("../../navigation", async (importOriginal) => ({
  ...(await importOriginal<object>()),
  AppLink: ({ href, children }: { href: string; children: React.ReactNode }) => <a href={href}>{children}</a>,
}));
vi.mock("../../common/actor-avatar", () => ({
  ActorAvatar: () => <div>avatar</div>,
}));
vi.mock("sonner", () => ({
  toast: { success: vi.fn(), error: vi.fn(), info: vi.fn() },
}));

import { AgentBatchToolbar } from "./agent-batch-toolbar";
import type { AgentListRow } from "./agents-page";

function makeAgent(
  id: string,
  ownerId: string | null,
  overrides: Record<string, unknown> = {},
) {
  return {
    id,
    workspace_id: "ws-1",
    runtime_id: "rt-1",
    name: `Agent ${id}`,
    description: "",
    instructions: "",
    avatar_url: null,
    runtime_mode: "local" as const,
    runtime_config: {},
    max_concurrent_tasks: 1,
    owner_id: ownerId,
    archived_at: null,
    custom_args: [] as string[],
    visibility: "private" as const,
    permission_mode: "private" as const,
    invocation_targets: [] as unknown[],
    model: "claude",
    created_at: "2026-01-01T00:00:00Z",
    updated_at: "2026-01-01T00:00:00Z",
    status: "idle" as const,
    skills: [] as unknown[],
    archived_by: null,
    ...overrides,
  };
}

function makeRow(
  id: string,
  ownerId: string | null,
  overrides: Record<string, unknown> = {},
): AgentListRow {
  const agent = makeAgent(id, ownerId, overrides);
  return {
    agent: agent as AgentListRow["agent"],
    runtime: null,
    presence: null,
    activity: null,
    runCount: 0,
    lastActiveDays: null,
    owner: ownerId
      ? ({ user_id: ownerId, name: `Owner ${ownerId}`, email: "" } as AgentListRow["owner"])
      : null,
    isOwnedByMe: ownerId === "user-1",
    canManage: ownerId === "user-1",
  };
}

function renderToolbar(rows: AgentListRow[], members: MemberWithUser[] = []) {
  const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  const ui = (nextRows: AgentListRow[]) => (
    <QueryClientProvider client={qc}>
      <I18nProvider locale="en" resources={TEST_RESOURCES}>
        <AgentBatchToolbar
          rows={nextRows}
          members={members}
          currentUserId="user-1"
          onClear={() => {}}
        />
      </I18nProvider>
    </QueryClientProvider>
  );
  const view = render(ui(rows));
  return {
    ...view,
    rerenderRows: (nextRows: AgentListRow[]) => view.rerender(ui(nextRows)),
  };
}

const MEMBERS = [
  { user_id: "u-a", name: "Alice", role: "member" },
  { user_id: "u-b", name: "Bob", role: "member" },
  { user_id: "u-c", name: "Carol", role: "member" },
] as unknown as MemberWithUser[];

function specificPeople(...ids: string[]) {
  return {
    permission_mode: "public_to" as const,
    invocation_targets: ids.map((id) => ({ target_type: "member", target_id: id })),
  };
}

beforeEach(() => {
  updateAgentSpy.mockClear();
  updateAgentSpy.mockResolvedValue({});
  featuresSpy.mockReset();
  featuresSpy.mockResolvedValue({ features: {} });
  vi.mocked(toast.error).mockClear();
});

describe("AgentBatchToolbar — action order", () => {
  it("renders Archive last, after the other batch actions", () => {
    // One archived + one active owned row surfaces all three actions at once.
    renderToolbar([
      makeRow("a", "user-1", { archived_at: "2026-01-01T00:00:00Z" }),
      makeRow("b", "user-1"),
    ]);

    // Labelled buttons in DOM order; the clear-selection button is icon-only.
    const actions = screen
      .getAllByRole("button")
      .map((b) => b.textContent?.trim())
      .filter((text): text is string => !!text);

    expect(actions).toEqual(["Restore", "Set access scope", "Archive"]);
  });
});

describe("AgentBatchToolbar — presence", () => {
  it("closes dialogs and removes the toolbar when selection becomes empty", async () => {
    const view = renderToolbar([makeRow("a", "user-1")]);

    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    await screen.findByText(/Applies to 1 agent/);

    view.rerenderRows([]);

    await waitFor(() => {
      expect(screen.queryByText(/Applies to 1 agent/)).not.toBeInTheDocument();
      expect(
        screen.queryByRole("button", { name: "Set access scope" }),
      ).not.toBeInTheDocument();
    });
  });
});

describe("AgentBatchToolbar — bulk Set access scope", () => {

  it("renders Applies to N with skip count in the dialog", async () => {
    renderToolbar([
      makeRow("a", "user-1"),
      makeRow("b", "user-1"),
      makeRow("c", "user-2"), // not owned → skipped
    ]);

    fireEvent.click(
      screen.getByRole("button", { name: "Set access scope" }),
    );

    await screen.findByText(/Applies to 2 agents/);
    expect(
      screen.getByText(/1 skipped/),
    ).toBeInTheDocument();
  });

  it("keeps Apply disabled until a scope is picked", async () => {
    renderToolbar([makeRow("a", "user-1")]);

    fireEvent.click(
      screen.getByRole("button", { name: "Set access scope" }),
    );
    await screen.findByText(/Applies to 1 agent/);

    // No radio clicked yet — Apply must be disabled
    expect(screen.getByRole("button", { name: "Apply" })).toBeDisabled();
  });

  // Regression: AgentBatchToolbar <-> AccessPicker used to notify each other on
  // every render (unstable onReadyChange + a change object rebuilt per render,
  // plus a cleanup that cleared the parent on each dependency change). Picking a
  // scope never settled, so this whole flow could not complete.
  it("settles after picking a scope: Apply enables and stays enabled", async () => {
    renderToolbar([makeRow("a", "user-1")]);

    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    await screen.findByText(/Applies to 1 agent/);

    fireEvent.click(screen.getByRole("radio", { name: /Entire workspace/ }));

    const apply = screen.getByRole("button", { name: "Apply" });
    await waitFor(() => expect(apply).toBeEnabled());

    // Reaching a fixed point is the point: further flushes must not flip the
    // button back to disabled or re-enter the notify effect.
    for (let i = 0; i < 5; i++) {
      await act(async () => {});
      expect(apply).toBeEnabled();
      expect(
        screen.getByRole("radio", { name: /Entire workspace/ }),
      ).toBeChecked();
    }
  });

  it("applies Entire workspace to each owned agent exactly once", async () => {
    renderToolbar([
      makeRow("a", "user-1"),
      makeRow("b", "user-1"),
      makeRow("c", "user-2"), // not owned → must be skipped
    ]);

    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    await screen.findByText(/Applies to 2 agents/);

    fireEvent.click(screen.getByRole("radio", { name: /Entire workspace/ }));

    const apply = screen.getByRole("button", { name: "Apply" });
    await waitFor(() => expect(apply).toBeEnabled());

    fireEvent.click(apply);

    await waitFor(() => expect(updateAgentSpy).toHaveBeenCalledTimes(2));
    expect(updateAgentSpy.mock.calls.map((call) => call[0]).sort()).toEqual([
      "a",
      "b",
    ]);
    for (const call of updateAgentSpy.mock.calls) {
      expect(call[1]).toEqual({
        permission_mode: "public_to",
        invocation_targets: [{ target_type: "workspace" }],
      });
    }

    // One click, one write per owned agent — the dialog is gone and nothing
    // re-fires afterwards.
    await act(async () => {});
    expect(updateAgentSpy).toHaveBeenCalledTimes(2);
  });

  it("does not retain the previous selection after close and reopen", async () => {
    renderToolbar([makeRow("a", "user-1")]);

    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    await screen.findByText(/Applies to 1 agent/);
    fireEvent.click(screen.getByRole("radio", { name: /Entire workspace/ }));
    await waitFor(() =>
      expect(screen.getByRole("button", { name: "Apply" })).toBeEnabled(),
    );

    fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
    await waitFor(() =>
      expect(
        screen.queryByRole("button", { name: "Apply" }),
      ).not.toBeInTheDocument(),
    );

    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    await screen.findByText(/Applies to 1 agent/);

    expect(screen.getByRole("button", { name: "Apply" })).toBeDisabled();
    expect(
      screen.getByRole("radio", { name: /Entire workspace/ }),
    ).not.toBeChecked();
    expect(updateAgentSpy).not.toHaveBeenCalled();
  });
});

describe("AgentBatchToolbar — plan without member access (MUL-9491)", () => {
  it("offers Specific people when the plan includes it", async () => {
    featuresSpy.mockResolvedValue({ features: { agent_invocation_members: "available" } });
    renderToolbar([makeRow("a", "user-1")]);
    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    await waitFor(() => expect(screen.getByRole("radio", { name: /^Specific people/ })).toBeEnabled());
    expect(screen.queryByText("View plans")).not.toBeInTheDocument();
  });

  it("disables Specific people in bulk when a selected agent has no member list", async () => {
    featuresSpy.mockResolvedValue({ features: { agent_invocation_members: "locked" } });
    renderToolbar(
      [
        makeRow("a", "user-1", specificPeople("u-a", "u-b")),
        makeRow("b", "user-1", { permission_mode: "public_to", invocation_targets: [{ target_type: "workspace", target_id: null }] }),
      ],
      MEMBERS,
    );
    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    expect(await screen.findByText("Not included in this workspace's plan.")).toBeInTheDocument();
    expect(screen.getByRole("radio", { name: /^Specific people/ })).toBeDisabled();
    expect(screen.getByRole("radio", { name: /^Entire workspace/ })).toBeEnabled();
    expect(screen.getByRole("button", { name: enBilling.paid_feature.badge_label })).toBeInTheDocument();
  });

  it("lets a locked plan bulk-reduce agents that share the same people", async () => {
    featuresSpy.mockResolvedValue({ features: { agent_invocation_members: "locked" } });
    renderToolbar(
      [makeRow("a", "user-1", specificPeople("u-a", "u-b")), makeRow("b", "user-1", specificPeople("u-a", "u-b"))],
      MEMBERS,
    );
    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    await screen.findByText("Read-only. Upgrade to edit.");
    const members = screen.getByRole("radio", { name: /^Specific people/ });
    expect(members).toBeEnabled();
    fireEvent.click(members);
    expect(screen.getByRole("checkbox", { name: /Bob/ })).toBeInTheDocument();
    expect(screen.queryByRole("checkbox", { name: /Carol/ })).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole("checkbox", { name: /Alice/ }));
    const apply = screen.getByRole("button", { name: "Apply" });
    await waitFor(() => expect(apply).toBeEnabled());
    fireEvent.click(apply);

    await waitFor(() => expect(updateAgentSpy).toHaveBeenCalledTimes(2));
    for (const id of ["a", "b"]) {
      expect(updateAgentSpy).toHaveBeenCalledWith(id, {
        permission_mode: "public_to",
        invocation_targets: [{ target_type: "member", target_id: "u-a" }],
      });
    }
  });

  it("offers only the people every selected agent already has", async () => {
    featuresSpy.mockResolvedValue({ features: { agent_invocation_members: "locked" } });
    renderToolbar(
      [makeRow("a", "user-1", specificPeople("u-a", "u-b")), makeRow("b", "user-1", specificPeople("u-a"))],
      MEMBERS,
    );
    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    await screen.findByText("Read-only. Upgrade to edit.");
    const members = screen.getByRole("radio", { name: /^Specific people/ });
    expect(members).toBeEnabled();
    fireEvent.click(members);
    expect(screen.getByRole("checkbox", { name: /Alice/ })).toBeInTheDocument();
    expect(screen.queryByRole("checkbox", { name: /Bob/ })).not.toBeInTheDocument();
    expect(screen.queryByRole("checkbox", { name: /Carol/ })).not.toBeInTheDocument();
  });

  it("explains a feature_locked refusal and refetches the plan", async () => {
    featuresSpy.mockResolvedValue({ features: { agent_invocation_members: "available" } });
    updateAgentSpy.mockImplementation(async (id: string) => {
      if (id === "b") {
        throw new ApiError("locked", 403, "Forbidden", { code: "feature_locked", feature: "agent_invocation_members" });
      }
      return {};
    });
    renderToolbar([makeRow("a", "user-1"), makeRow("b", "user-1")]);
    fireEvent.click(screen.getByRole("button", { name: "Set access scope" }));
    await waitFor(() => expect(featuresSpy).toHaveBeenCalledTimes(1));
    fireEvent.click(screen.getByRole("radio", { name: /Entire workspace/ }));
    const apply = screen.getByRole("button", { name: "Apply" });
    await waitFor(() => expect(apply).toBeEnabled());
    fireEvent.click(apply);

    await waitFor(() => expect(updateAgentSpy).toHaveBeenCalledTimes(2));
    await waitFor(() =>
      expect(toast.error).toHaveBeenCalledWith(enBilling.paid_feature.locked_error),
    );
    await waitFor(() => expect(featuresSpy).toHaveBeenCalledTimes(2));
  });
});