Skip to content

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

Go
22.8 KB
Raw Blame
import { act, render } from "@testing-library/react";
import { createRef, type ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { I18nProvider } from "@multica/core/i18n/react";
import { workspaceKeys } from "@multica/core/workspace/queries";
import type { Agent, MemberWithUser } from "@multica/core/types";
import type { QueryClient } from "@tanstack/react-query";
import enEditor from "../../locales/en/editor.json";

const TEST_RESOURCES = {
  en: { editor: enEditor },
};

function I18nWrapper({ children }: { children: ReactNode }) {
  return (
    <I18nProvider locale="en" resources={TEST_RESOURCES}>
      {children}
    </I18nProvider>
  );
}

beforeAll(() => {
  Element.prototype.scrollIntoView = vi.fn();
});

vi.mock("@multica/core/platform", () => ({
  getCurrentWsId: () => "ws-1",
}));

const authState = { user: { id: "u1" } as { id: string } | null };
vi.mock("@multica/core/auth", () => ({
  useAuthStore: { getState: () => authState },
}));

const chatState = { selectedAgentId: "agent-1" as string | null };
vi.mock("@multica/core/chat", () => ({
  useChatStore: { getState: () => chatState },
}));

import {
  SlashCommandList,
  type SlashCommandListRef,
  createSlashCommandSuggestion,
  type SlashCommandItem,
  buildBuiltinCommandItems,
  BUILTIN_COMMANDS,
  createBuiltinCommandSuggestion,
  QUICK_ACTION_ITEM_PREFIX,
} from "./slash-command-suggestion";

function agent(overrides: Partial<Agent>): Agent {
  return {
    id: "agent-1",
    workspace_id: "ws-1",
    runtime_id: "runtime-1",
    name: "Agent",
    description: "",
    instructions: "",
    avatar_url: null,
    runtime_mode: "local",
    runtime_config: {},
    custom_args: [],
    visibility: "workspace",
    permission_mode: "public_to",
    invocation_targets: [{ target_type: "workspace", target_id: null }],
    status: "idle",
    max_concurrent_tasks: 1,
    model: "",
    owner_id: null,
    skills: [],
    created_at: "",
    updated_at: "",
    archived_at: null,
    archived_by: null,
    ...overrides,
  };
}

function fakeQc(data: {
  members?: Array<Pick<MemberWithUser, "user_id" | "name" | "role">>;
  agents?: Agent[];
}): QueryClient {
  const map = new Map<string, unknown>();
  map.set(JSON.stringify(workspaceKeys.members("ws-1")), data.members ?? []);
  map.set(JSON.stringify(workspaceKeys.agents("ws-1")), data.agents ?? []);
  return {
    getQueryData: (key: readonly unknown[]) => map.get(JSON.stringify(key)),
  } as unknown as QueryClient;
}

function items(qc: QueryClient, query = ""): SlashCommandItem[] {
  const config = createSlashCommandSuggestion(qc);
  return config.items!({
    query,
    editor: {} as never,
    signal: new AbortController().signal,
  }) as SlashCommandItem[];
}

describe("slash command suggestion items", () => {
  it("returns all active agent skills when query is empty", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            { id: "s1", name: "deploy", description: "Ship changes" },
            { id: "s2", name: "review", description: "Review code" },
          ],
        }),
      ],
    });

    expect(items(qc).map((i) => i.label)).toEqual(["deploy", "review"]);
  });

  it("filters paused skills from the active agent", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            { id: "paused", name: "paused", description: "", enabled: false },
            { id: "active", name: "active", description: "", enabled: true },
          ],
        }),
      ],
    });

    expect(items(qc).map((item) => item.id)).toEqual(["active"]);
  });

  it("filters skills by name case-insensitively", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            { id: "s1", name: "Deploy", description: "" },
            { id: "s2", name: "Review", description: "" },
          ],
        }),
      ],
    });

    expect(items(qc, "dep").map((i) => i.id)).toEqual(["s1"]);
  });

  it("filters skills by description", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            { id: "s1", name: "deploy", description: "Ship changes" },
            { id: "s2", name: "review", description: "Read a pull request" },
          ],
        }),
      ],
    });

    expect(items(qc, "pull").map((i) => i.id)).toEqual(["s2"]);
  });

  it("ranks name prefix matches above description-only matches", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            { id: "s1", name: "grilling", description: "Grill the user about a plan" },
            { id: "s2", name: "prototype", description: "Build a throwaway prototype" },
            { id: "s3", name: "wayfinder", description: "Plan a huge chunk of work" },
          ],
        }),
      ],
    });

    expect(items(qc, "wa").map((i) => i.id)).toEqual(["s3", "s2"]);
  });

  it("ranks an exact name match ahead of a longer prefix match", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            { id: "s1", name: "reviewer", description: "" },
            { id: "s2", name: "review", description: "" },
          ],
        }),
      ],
    });

    expect(items(qc, "review").map((i) => i.id)).toEqual(["s2", "s1"]);
  });

  it("ranks a name prefix above a mid-name match", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            { id: "s1", name: "pr-review", description: "" },
            { id: "s2", name: "review", description: "" },
          ],
        }),
      ],
    });

    expect(items(qc, "rev").map((i) => i.id)).toEqual(["s2", "s1"]);
  });

  it("keeps the configured skill order within a match tier", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            { id: "s1", name: "deploy-web", description: "" },
            { id: "s2", name: "deploy-api", description: "" },
          ],
        }),
      ],
    });

    expect(items(qc, "deploy").map((i) => i.id)).toEqual(["s1", "s2"]);
  });

  it("keeps a name match inside the 20-item cap when description hits fill it", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            ...Array.from({ length: 25 }, (_, i) => ({
              id: `d${i}`,
              name: `skill-${i}`,
              description: "Build a throwaway prototype",
            })),
            { id: "s-named", name: "wayfinder", description: "" },
          ],
        }),
      ],
    });

    const result = items(qc, "wa");
    expect(result).toHaveLength(20);
    expect(result[0]?.id).toBe("s-named");
  });

  it("tolerates skills with missing descriptions from cached API data", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [
            { id: "s1", name: "deploy" } as Agent["skills"][number],
          ],
        }),
      ],
    });

    expect(() => items(qc, "dep")).not.toThrow();
    expect(items(qc, "dep")).toEqual([
      { id: "s1", label: "deploy", description: "" },
    ]);
  });

  it("returns empty when the active agent has no skills", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [agent({ id: "agent-1", skills: [] })],
    });

    expect(items(qc)).toEqual([]);
  });

  it("caps results at 20", () => {
    chatState.selectedAgentId = "agent-1";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: Array.from({ length: 25 }, (_, i) => ({
            id: `s${i}`,
            name: `skill-${i}`,
            description: "",
          })),
        }),
      ],
    });

    expect(items(qc)).toHaveLength(20);
  });

  it("falls back to the first available agent when selectedAgentId is stale", () => {
    chatState.selectedAgentId = "missing";
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [
        agent({
          id: "agent-1",
          skills: [{ id: "s1", name: "deploy", description: "" }],
        }),
      ],
    });

    expect(items(qc).map((i) => i.id)).toEqual(["s1"]);
  });

  it("returns empty when no agents exist", () => {
    const qc = fakeQc({
      members: [{ user_id: "u1", name: "Alice", role: "member" }],
      agents: [],
    });

    expect(items(qc)).toEqual([]);
  });

  it("excludes skills from private agents the user cannot access", () => {
    chatState.selectedAgentId = "private-agent";
    const qc = fakeQc({
      members: [
        { user_id: "u1", name: "Alice", role: "member" },
        { user_id: "u2", name: "Bob", role: "member" },
      ],
      agents: [
        agent({
          id: "private-agent",
          visibility: "private",
          permission_mode: "private",
          invocation_targets: [],
          owner_id: "u2",
          skills: [{ id: "private-skill", name: "secret", description: "" }],
        }),
      ],
    });

    expect(items(qc)).toEqual([]);
  });
});

describe("SlashCommandList keyboard handling", () => {
  it("lets Enter and arrow keys fall through when there are no selectable items", () => {
    const ref = createRef<SlashCommandListRef>();

    render(
      <I18nWrapper>
        <SlashCommandList ref={ref} items={[]} query="" command={vi.fn()} />
      </I18nWrapper>,
    );

    expect(
      ref.current?.onKeyDown({
        event: new KeyboardEvent("keydown", { key: "Enter" }),
      }),
    ).toBe(false);
    expect(
      ref.current?.onKeyDown({
        event: new KeyboardEvent("keydown", { key: "Enter", metaKey: true }),
      }),
    ).toBe(false);
    expect(
      ref.current?.onKeyDown({
        event: new KeyboardEvent("keydown", { key: "ArrowUp" }),
      }),
    ).toBe(false);
    expect(
      ref.current?.onKeyDown({
        event: new KeyboardEvent("keydown", { key: "ArrowDown" }),
      }),
    ).toBe(false);
  });

  it("handles Enter and arrow keys when selectable items exist", () => {
    const ref = createRef<SlashCommandListRef>();
    const command = vi.fn();
    const selectableItems: SlashCommandItem[] = [
      { id: "s1", label: "deploy", description: "Ship changes" },
      { id: "s2", label: "review", description: "Review code" },
    ];

    render(
      <I18nWrapper>
        <SlashCommandList
          ref={ref}
          items={selectableItems}
          query=""
          command={command}
        />
      </I18nWrapper>,
    );

    expect(
      ref.current?.onKeyDown({
        event: new KeyboardEvent("keydown", { key: "ArrowUp" }),
      }),
    ).toBe(true);
    expect(
      ref.current?.onKeyDown({
        event: new KeyboardEvent("keydown", { key: "ArrowDown" }),
      }),
    ).toBe(true);
    expect(
      ref.current?.onKeyDown({
        event: new KeyboardEvent("keydown", { key: "Enter" }),
      }),
    ).toBe(true);
    expect(command).toHaveBeenCalledWith(selectableItems[0]);
  });

  // MUL-5495: same Ctrl aliases the command bar (cmdk) accepts, so the slash
  // picker navigates like every other list in the product.
  it("navigates with Ctrl+N/J and Ctrl+P/K, and leaves the bare letters alone", () => {
    const ref = createRef<SlashCommandListRef>();
    const command = vi.fn();
    const selectableItems: SlashCommandItem[] = [
      { id: "s1", label: "deploy", description: "Ship changes" },
      { id: "s2", label: "review", description: "Review code" },
      { id: "s3", label: "note", description: "Leave a note" },
    ];

    render(
      <I18nWrapper>
        <SlashCommandList
          ref={ref}
          items={selectableItems}
          query=""
          command={command}
        />
      </I18nWrapper>,
    );

    const highlightedLabel = () => {
      const buttons = Array.from(document.querySelectorAll<HTMLButtonElement>("button"));
      return buttons.find((b) => b.classList.contains("bg-accent"))?.textContent ?? "";
    };
    let handled: boolean | undefined;
    const press = (init: KeyboardEventInit) =>
      act(() => {
        handled = ref.current?.onKeyDown({ event: new KeyboardEvent("keydown", init) });
      });

    press({ key: "n", ctrlKey: true });
    expect(handled).toBe(true);
    expect(highlightedLabel()).toContain("/review");

    press({ key: "j", ctrlKey: true });
    expect(highlightedLabel()).toContain("/note");

    press({ key: "p", ctrlKey: true });
    expect(highlightedLabel()).toContain("/review");

    press({ key: "k", ctrlKey: true });
    expect(highlightedLabel()).toContain("/deploy");

    // Bare letters stay query characters — "/note" must remain typeable.
    press({ key: "n" });
    expect(handled).toBe(false);
    expect(highlightedLabel()).toContain("/deploy");

    press({ key: "Enter" });
    expect(command).toHaveBeenCalledWith(selectableItems[0]);
  });

  // MUL-3685: plain Tab accepts the highlighted item like Enter; Shift+Tab and
  // modifier+Tab fall through so reverse focus / OS switching are preserved.
  it("accepts the highlighted item on plain Tab, ignoring Shift/modifier+Tab", () => {
    const ref = createRef<SlashCommandListRef>();
    const command = vi.fn();
    const selectableItems: SlashCommandItem[] = [
      { id: "s1", label: "deploy", description: "Ship changes" },
      { id: "s2", label: "review", description: "Review code" },
    ];

    render(
      <I18nWrapper>
        <SlashCommandList
          ref={ref}
          items={selectableItems}
          query=""
          command={command}
        />
      </I18nWrapper>,
    );

    const press = (init: KeyboardEventInit) =>
      ref.current?.onKeyDown({ event: new KeyboardEvent("keydown", init) });

    expect(press({ key: "Tab", shiftKey: true })).toBe(false);
    expect(press({ key: "Tab", metaKey: true })).toBe(false);
    expect(command).not.toHaveBeenCalled();

    expect(press({ key: "Tab" })).toBe(true);
    expect(command).toHaveBeenCalledWith(selectableItems[0]);
  });

  it("lets Tab fall through when there are no selectable items, like Enter", () => {
    const ref = createRef<SlashCommandListRef>();

    render(
      <I18nWrapper>
        <SlashCommandList ref={ref} items={[]} query="" command={vi.fn()} />
      </I18nWrapper>,
    );

    expect(
      ref.current?.onKeyDown({
        event: new KeyboardEvent("keydown", { key: "Tab" }),
      }),
    ).toBe(false);
  });
});

describe("SlashCommandList empty states", () => {
  it("shows a configured-skills empty state before search text is entered", () => {
    const { getByText } = render(
      <I18nWrapper>
        <SlashCommandList items={[]} query="" command={vi.fn()} />
      </I18nWrapper>,
    );

    expect(getByText("No skills configured")).toBeInTheDocument();
  });

  it("shows a no-results empty state when search text has no matches", () => {
    const { getByText } = render(
      <I18nWrapper>
        <SlashCommandList items={[]} query="deploy" command={vi.fn()} />
      </I18nWrapper>,
    );

    expect(getByText("No matching skills")).toBeInTheDocument();
  });

  it("renders nothing on empty items when hideOnEmpty is set (command menu)", () => {
    const { container } = render(
      <I18nWrapper>
        <SlashCommandList items={[]} query="6" command={vi.fn()} hideOnEmpty />
      </I18nWrapper>,
    );

    // No popup box on a non-matching `/` (e.g. typing a date like 6/8).
    expect(container).toBeEmptyDOMElement();
  });
});

describe("buildBuiltinCommandItems", () => {
  it("returns only quick actions and /note for an empty query with many skills", () => {
    const skills = Array.from({ length: 18 }, (_, index) => ({
      id: `skill-${index}`,
      name: `skill-${index}`,
      description: "",
    }));
    const actions = [{ id: "qa-1", name: "Request changes", description: "Review this" }];

    expect(buildBuiltinCommandItems("", actions, skills)).toEqual([
      { id: "quick-action:qa-1", label: "Request changes", description: "Review this" },
      BUILTIN_COMMANDS[0],
    ]);
  });

  it("merges matching quick actions, skills, and /note for a nonempty query", () => {
    expect(buildBuiltinCommandItems(
      "no",
      [
        { id: "qa-1", name: "notify", description: "Notify the team" },
        { id: "qa-2", name: "review", description: "Review this" },
      ],
      [{ id: "skill-1", name: "note-deploy", description: "Ship changes" }],
    )).toEqual([
      { id: "quick-action:qa-1", label: "notify", description: "Notify the team" },
      { id: "skill:skill-1", label: "note-deploy", description: "Ship changes" },
      BUILTIN_COMMANDS[0],
    ]);
  });

  it("ranks Issue skills by name and description within the shared item cap", () => {
    const skills = [
      ...Array.from({ length: 25 }, (_, index) => ({
        id: `description-${index}`,
        name: `skill-${index}`,
        description: "Build a throwaway prototype",
      })),
      { id: "named", name: "wayfinder", description: "" },
    ];
    const result = buildBuiltinCommandItems("wa", [], skills);

    expect(result).toHaveLength(20);
    expect(result[0]?.id).toBe("skill:named");
    expect(result.every((item) => item.id.startsWith("skill:"))).toBe(true);
  });

  it.each(["n", "no"])("reserves /note for query %j with 25 matching skills", (query) => {
    const skills = Array.from({ length: 25 }, (_, index) => ({
      id: `skill-${index}`,
      name: `skill-${index}`,
      description: "note",
    }));

    const result = buildBuiltinCommandItems(query, [], skills);
    expect(result).toHaveLength(20);
    expect(result.at(-1)?.id).toBe("note");
  });

  it("includes /note while the query is a prefix of the label", () => {
    expect(buildBuiltinCommandItems("no").map((c) => c.id)).toEqual(["note"]);
    expect(buildBuiltinCommandItems("NOTE").map((c) => c.id)).toEqual(["note"]);
  });

  it("matches the label as a prefix only — not the description", () => {
    // "agent" appears in the description but is not a label prefix.
    expect(buildBuiltinCommandItems("agent")).toEqual([]);
    // A non-prefix substring of the label does not match either.
    expect(buildBuiltinCommandItems("ote")).toEqual([]);
  });

  it("returns nothing for a query that matches no command or skill", () => {
    const skills = [{ id: "skill-1", name: "code-review", description: "Review code" }];
    expect(buildBuiltinCommandItems("zzz", [], skills)).toEqual([]);
  });
});

describe("SlashCommandList built-in command rendering", () => {
  it("renders the localized description for a built-in command", () => {
    const { getByText } = render(
      <I18nWrapper>
        <SlashCommandList
          items={buildBuiltinCommandItems("")}
          query=""
          command={vi.fn()}
          hideOnEmpty
        />
      </I18nWrapper>,
    );

    expect(getByText("/note")).toBeInTheDocument();
    expect(
      getByText("Add a note — won't trigger any agents"),
    ).toBeInTheDocument();
  });
});


describe("builtin `/` menu — cached quick action content", () => {
  const range = { from: 0, to: 7 };
  const item = { id: `${QUICK_ACTION_ITEM_PREFIX}qa-1`, label: "review" };

  function fakeEditor() {
    const chain = {
      focus: vi.fn(() => chain),
      command: vi.fn(() => chain),
      insertContentAt: vi.fn(() => chain),
      run: vi.fn(() => true),
    };
    return {
      chain,
      editor: {
        chain: () => chain,
        view: { state: { selection: { $to: { nodeAfter: null } } } },
      },
    };
  }

  it("replaces the command range synchronously and parses mention markdown", () => {
    const selection = window.getSelection()!;
    const collapse = vi.spyOn(selection, "collapseToEnd").mockImplementation(() => {});
    const { editor, chain } = fakeEditor();
    const content = "[@Reviewer](mention://agent/agent-id)\n\nReview this";
    const getQuickActionContent = vi.fn(() => content);
    const suggestion = createBuiltinCommandSuggestion({ getQuickActionContent });

    suggestion.command!({ editor, range, props: item } as never);

    expect(getQuickActionContent).toHaveBeenCalledWith("qa-1");
    expect(chain.insertContentAt).toHaveBeenCalledWith(range, content, { contentType: "markdown" });
    expect(chain.run).toHaveBeenCalledOnce();
    collapse.mockRestore();
  });

  it("leaves the command untouched when content is unavailable", () => {
    const { editor, chain } = fakeEditor();
    const suggestion = createBuiltinCommandSuggestion({ getQuickActionContent: () => "" });

    suggestion.command!({ editor, range, props: item } as never);

    expect(chain.insertContentAt).not.toHaveBeenCalled();
  });

  it("inserts a skill through the shared slashCommand node shape", () => {
    const selection = window.getSelection()!;
    const collapse = vi.spyOn(selection, "collapseToEnd").mockImplementation(() => {});
    const { editor, chain } = fakeEditor();
    const suggestion = createBuiltinCommandSuggestion();

    suggestion.command!({
      editor,
      range,
      props: { id: "skill:skill-1", label: "deploy" },
    } as never);

    expect(chain.insertContentAt).toHaveBeenCalledWith(range, [
      {
        type: "slashCommand",
        attrs: { id: "skill-1", label: "deploy", mentionSuggestionChar: "/" },
      },
      { type: "text", text: " " },
    ]);
    expect(chain.run).toHaveBeenCalledOnce();
    collapse.mockRestore();
  });

  it("keeps /note as plain text", () => {
    const selection = window.getSelection()!;
    const collapse = vi.spyOn(selection, "collapseToEnd").mockImplementation(() => {});
    const { editor, chain } = fakeEditor();
    const suggestion = createBuiltinCommandSuggestion();

    suggestion.command!({ editor, range, props: { id: "note", label: "note" } } as never);

    expect(chain.insertContentAt).toHaveBeenCalledWith(range, [
      { type: "text", text: "/note " },
    ]);
    collapse.mockRestore();
  });
});