Skip to content

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

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

import { cleanup, fireEvent, screen, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { afterEach, describe, expect, it, vi } from "vitest";
import { buildIssueStatusCatalog } from "@multica/core/issue-statuses";
import { agentListOptions, memberListOptions } from "@multica/core/workspace/queries";
import { projectListOptions } from "@multica/core/projects/queries";
import { renderWithI18n } from "../test/i18n";
import type { IssueWorkflow } from "@multica/core/types";
import { WorkflowEditorPage } from "./workflow-editor-page";

const { createWorkflow, statusEntries } = vi.hoisted(() => ({ createWorkflow: vi.fn(), statusEntries: { current: [] as unknown[] } }));

vi.mock("@multica/core/api", () => ({ api: {}, ApiError: class extends Error {}, clientErrorMessage: () => null }));
vi.mock("@multica/core/hooks", () => ({ useWorkspaceId: () => "ws-1" }));
vi.mock("@multica/core/paths", () => ({ useCurrentWorkspace: () => ({ id: "ws-1", name: "Acme" }) }));
vi.mock("@multica/core/issue-statuses/hooks", () => ({
  useIssueStatuses: () => buildIssueStatusCatalog(statusEntries.current as never),
}));
vi.mock("@multica/core/workspace/hooks", () => ({ useActorName: () => ({ getActorName: () => "Builder" }) }));
vi.mock("@multica/core/issue-workflows", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@multica/core/issue-workflows")>()),
  useIssueWorkflows: () => ({ workflows: [], isLoaded: true }),
  useCreateIssueWorkflow: () => ({ mutateAsync: createWorkflow, isPending: false }),
  useUpdateIssueWorkflow: () => ({ mutateAsync: vi.fn(), isPending: false }),
  useDeleteIssueWorkflow: () => ({ mutateAsync: vi.fn(), isPending: false }),
}));
vi.mock("../common/actor-avatar", () => ({
  ActorAvatar: ({ actorId }: { actorId: string }) => <span data-testid={`avatar-${actorId}`} />,
}));

function renderEditor(props: { template?: "content"; canCreateStatus?: boolean; seed?: IssueWorkflow } = {}) {
  const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  qc.setQueryData(projectListOptions("ws-1").queryKey, [] as never);
  qc.setQueryData(agentListOptions("ws-1").queryKey, [{ id: "agent-1", name: "Builder", archived_at: null }] as never);
  qc.setQueryData(memberListOptions("ws-1").queryKey, [] as never);
  renderWithI18n(
    <QueryClientProvider client={qc}>
      <WorkflowEditorPage workflowId={null} seed={props.seed ?? null} canEdit onBack={vi.fn()} onOpen={vi.fn()} template={props.template} canCreateStatus={props.canCreateStatus} />
    </QueryClientProvider>,
  );
}

afterEach(() => {
  cleanup();
  createWorkflow.mockReset();
  statusEntries.current = [];
});

describe("WorkflowEditorPage", () => {
  it("edits a new status step's handoff before the save (MUL-8118)", async () => {
    createWorkflow.mockResolvedValue({ id: "wf-1" });
    renderEditor();

    fireEvent.click(screen.getByRole("button", { name: /Add step/ }));
    fireEvent.click(await screen.findByRole("menuitem", { name: /New status/ }));
    const input = screen.getByPlaceholderText("Status name");
    fireEvent.change(input, { target: { value: "Deploy" } });
    fireEvent.keyDown(input, { key: "Enter" });

    // The new step is selected, and its handoff can be edited right away.
    expect(screen.getByRole("heading", { name: "Deploy" })).toBeTruthy();
    const agent = screen.getByRole("radio", { name: /Hand to an agent/ });
    expect((agent as HTMLButtonElement).disabled).toBe(false);
    fireEvent.click(agent);
    fireEvent.click(screen.getByRole("button", { name: "Choose an agent" }));
    fireEvent.click(await screen.findByText("Builder"));

    fireEvent.change(screen.getByRole("textbox", { name: "Name" }), { target: { value: "Release" } });
    fireEvent.click(screen.getByRole("button", { name: "Create" }));

    await waitFor(() => expect(createWorkflow).toHaveBeenCalled());
    const steps = createWorkflow.mock.calls[0]![0].steps as unknown[];
    expect(steps).toContainEqual({
      new_status: { name: "Deploy" },
      handler: { type: "agent", id: "agent-1" },
      instructions: "",
    });
  });

  it("resolves a default next over the unsaved steps, as the save will", async () => {
    renderEditor();
    await addNewStatus("QA");
    await addNewStatus("Deploy");

    // QA is followed by Deploy on the board, so that is where its agent goes
    // next — not Done, the next status that exists before the save.
    fireEvent.click(stepCard("QA"));
    fireEvent.click(screen.getByRole("radio", { name: /Hand to an agent/ }));
    fireEvent.click(screen.getByRole("button", { name: "Choose an agent" }));
    fireEvent.click(await screen.findByText("Builder"));

    expect(screen.getByRole("combobox", { name: "When done, move to" }).textContent).toContain("Next column (Deploy)");
    expect(stepCard("QA").textContent).toContain("then → Deploy");
    // The explicit choices still list only statuses that exist before the save.
    fireEvent.click(screen.getByRole("combobox", { name: "When done, move to" }));
    expect(screen.queryByRole("option", { name: "Deploy" })).toBeNull();
  });

  // The agent's brief never routes finished work into a closed column, so
  // the editor's default must not either (MUL-9913).
  it("skips a custom closed column when showing the default next", () => {
    statusEntries.current = [{ key: "wont_fix", name: "Won't Fix", category: "closed", description: "", color: "", icon: "", position: 0 }];
    const step = (status_key: string) => ({ status_key, handler: { type: "none" as const }, instructions: "" });
    // Seeded as a duplicate: a new draft pre-filled with these columns.
    renderEditor({
      seed: {
        id: "wf-1", workspace_id: "ws-1", name: "Delivery", description: "", is_default: false, owner_id: null,
        project_ids: [], created_at: "", updated_at: "",
        steps: [
          step("todo"),
          { status_key: "in_review", handler: { type: "agent", id: "agent-1" }, instructions: "" },
          step("wont_fix"),
          step("done"),
          step("cancelled"),
        ],
      },
    });
    fireEvent.click(stepCard("In Review"));
    expect(screen.getByRole("combobox", { name: "When done, move to" }).textContent).toContain("Next column (Done)");
  });
});

// Statuses are shared by every board, so only admins add them (MUL-8120).
describe("WorkflowEditorPage status creation", () => {
  it("blocks saving a template that needs new statuses for a member", () => {
    renderEditor({ template: "content", canCreateStatus: false });
    expect(screen.getByRole("alert").textContent).toMatch(/Only workspace admins can add statuses/);
    expect(screen.getByRole("button", { name: "Create" }).hasAttribute("disabled")).toBe(true);
  });

  it("lets an admin save the same template", () => {
    renderEditor({ template: "content", canCreateStatus: true });
    expect(screen.queryByRole("alert")).toBeNull();
    expect(screen.getByRole("button", { name: "Create" }).hasAttribute("disabled")).toBe(false);
  });
});

// The catalog refetches whenever anyone adds or archives a status.
describe("WorkflowEditorPage when the status catalog changes", () => {
  const status = (key: string, name: string) => ({ key, name, category: "started", description: "", color: "", icon: "", position: 0 });
  const changeDescription = (value: string) =>
    fireEvent.change(screen.getByRole("textbox", { name: "Description" }), { target: { value } });

  it("keeps an unsaved new workflow", async () => {
    renderEditor();
    fireEvent.change(screen.getByRole("textbox", { name: "Name" }), { target: { value: "Release" } });
    await addNewStatus("Deploy");

    statusEntries.current = [status("triage", "Triage")];
    changeDescription("Ships weekly");

    expect((screen.getByRole("textbox", { name: "Name" }) as HTMLInputElement).value).toBe("Release");
    expect((screen.getByRole("textbox", { name: "Description" }) as HTMLTextAreaElement).value).toBe("Ships weekly");
    expect(stepCard("Deploy")).toBeTruthy();
  });

  it("uses a status an admin adds for a template without dropping the member's edits", async () => {
    createWorkflow.mockResolvedValue({ id: "wf-1" });
    renderEditor({ template: "content", canCreateStatus: false });
    fireEvent.change(screen.getByRole("textbox", { name: "Name" }), { target: { value: "Blog" } });
    expect(screen.getByRole("alert")).toBeTruthy();

    statusEntries.current = [status("drafting", "Drafting"), status("editing", "Editing"), status("scheduled", "Scheduled")];
    changeDescription("Posts");

    await waitFor(() => expect(screen.queryByRole("alert")).toBeNull());
    expect((screen.getByRole("textbox", { name: "Name" }) as HTMLInputElement).value).toBe("Blog");
    fireEvent.click(screen.getByRole("button", { name: "Create" }));
    await waitFor(() => expect(createWorkflow).toHaveBeenCalled());
    const steps = createWorkflow.mock.calls[0]![0].steps as { status_key?: string }[];
    expect(steps.map((s) => s.status_key)).toEqual(expect.arrayContaining(["drafting", "editing", "scheduled"]));
  });
});

/** A step in the list (the detail panel's status picker shares its name). */
function stepCard(name: string) {
  const card = screen
    .getAllByRole("button", { name: new RegExp(`^${name}`) })
    .find((b) => b.hasAttribute("aria-pressed"));
  if (!card) throw new Error(`no step card ${name}`);
  return card;
}

async function addNewStatus(name: string) {
  fireEvent.click(screen.getByRole("button", { name: /Add step/ }));
  fireEvent.click(await screen.findByRole("menuitem", { name: /New status/ }));
  const input = screen.getByPlaceholderText("Status name");
  fireEvent.change(input, { target: { value: name } });
  fireEvent.keyDown(input, { key: "Enter" });
}