Skip to content

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

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

import { cleanup, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { Agent, AgentRuntime, AgentTask, Issue } from "@multica/core/types";
import { renderWithI18n } from "../../test/i18n";

const activityQueryData = vi.hoisted(() => ({
  agents: [] as Agent[],
  runtimes: [] as AgentRuntime[],
}));

// The hover card renders one row per internal task record and counts product
// runs, so its header must describe runs — not agents. A single agent can have
// several runs at once (e.g. the workspace chip reads "2 working" for two
// unique agents while the card lists three run rows). An agent-worded header
// here would print "3 agents working" for those two agents, contradicting the
// chip. MUL-3872.

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

vi.mock("@multica/core/workspace/hooks", () => ({
  useActorName: () => ({
    getActorName: (_type: string, id: string) =>
      ({ "agent-1": "Niko", "agent-2": "J" })[id] ?? "Unknown Agent",
    getActorInitials: (_type: string, id: string) =>
      ({ "agent-1": "NI", "agent-2": "J" })[id] ?? "UA",
    getActorAvatarUrl: () => null,
  }),
}));

// The card only reads these query results for avatars / availability, never
// for the header count, so empty lists keep the row chrome inert while the
// header still derives from the task array.
vi.mock("@multica/core/runtimes/queries", () => ({
  runtimeListOptions: () => ({ queryKey: ["runtimes"] }),
}));

vi.mock("@multica/core/workspace/queries", () => ({
  agentListOptions: () => ({ queryKey: ["agents"] }),
}));

vi.mock("@multica/ui/components/common/actor-avatar", () => ({
  ActorAvatar: ({ name }: { name: string }) => (
    <span data-testid="actor-avatar">{name}</span>
  ),
}));

vi.mock("@tanstack/react-query", async () => {
  const actual =
    await vi.importActual<typeof import("@tanstack/react-query")>(
      "@tanstack/react-query",
    );
  return {
    ...actual,
    useQuery: (options: { queryKey?: readonly unknown[] }) => ({
      data:
        options.queryKey?.[0] === "agents"
          ? activityQueryData.agents
          : activityQueryData.runtimes,
    }),
  };
});

import {
  AgentActivityHoverContent,
  WorkspaceAgentActivityHoverContent,
} from "./agent-activity-hover-content";

function makeIssue(id: string, identifier: string, title: string): Issue {
  return {
    id,
    workspace_id: "ws-1",
    number: 1,
    identifier,
    title,
    description: null,
    status: "in_progress",
    priority: "none",
    assignee_type: null,
    assignee_id: null,
    creator_type: "member",
    creator_id: "user-1",
    parent_issue_id: null,
    project_id: null,
    position: 1,
    stage: null,
    start_date: null,
    due_date: null,
    metadata: {},
    properties: {},
    created_at: "2026-06-08T08:00:00Z",
    updated_at: "2026-06-08T08:00:00Z",
  };
}

function makeTask(overrides: Partial<AgentTask>): AgentTask {
  return {
    id: "task-1",
    agent_id: "agent-1",
    runtime_id: "runtime-1",
    issue_id: "issue-1",
    status: "running",
    priority: 0,
    dispatched_at: null,
    started_at: "2026-06-08T08:00:00Z",
    completed_at: null,
    result: null,
    error: null,
    created_at: "2026-06-08T08:00:00Z",
    ...overrides,
  };
}

afterEach(() => {
  cleanup();
  activityQueryData.agents = [];
  activityQueryData.runtimes = [];
});

describe("AgentActivityHoverContent", () => {
  // Two agents, three active runs (Niko has two at once). The header must
  // count the three run rows, not the two agents.
  const threeTasksTwoAgents = [
    makeTask({ id: "t1", agent_id: "agent-1" }),
    makeTask({ id: "t2", agent_id: "agent-1" }),
    makeTask({ id: "t3", agent_id: "agent-2" }),
  ];

  it("counts runs, not agents, in the header", () => {
    renderWithI18n(<AgentActivityHoverContent tasks={threeTasksTwoAgents} />);

    expect(screen.getByText("3 active runs")).toBeInTheDocument();
    // One row per run — three avatars for three runs.
    expect(screen.getAllByTestId("actor-avatar")).toHaveLength(3);
  });

  it("uses the singular run copy for a single run", () => {
    renderWithI18n(<AgentActivityHoverContent tasks={[makeTask({})]} />);

    expect(screen.getByText("1 active run")).toBeInTheDocument();
  });

  it("uses an online agent projection when its runtime row is hidden", () => {
    activityQueryData.agents = [
      {
        id: "agent-1",
        runtime_id: "private-runtime",
        runtime_availability: "online",
      } as Agent,
    ];

    const { container } = renderWithI18n(
      <AgentActivityHoverContent tasks={[makeTask({ status: "queued" })]} />,
    );

    expect(
      container.querySelector('[class*="bg-muted-foreground/40"]'),
    ).toBeInTheDocument();
    expect(
      container.querySelector('[class*="bg-warning"]'),
    ).not.toBeInTheDocument();
  });

  it("renders the Chinese run copy", () => {
    renderWithI18n(<AgentActivityHoverContent tasks={threeTasksTwoAgents} />, {
      locale: "zh-Hans",
    });

    expect(screen.getByText("3 次运行进行中")).toBeInTheDocument();
  });
});

// The workspace chip says WHO is working ("N agents working"). This card
// says WHERE: the two figures the chip does not carry, and the rows grouped
// by issue. It stays silent about work it excludes — see the component doc.
// MUL-4884.
describe("WorkspaceAgentActivityHoverContent", () => {
  it("carries the two figures the chip does not, and groups rows by issue", () => {
    renderWithI18n(
      <WorkspaceAgentActivityHoverContent
        issues={[
          makeIssue("i-1", "MUL-4879", "Counting logic looks wrong"),
          makeIssue("i-2", "MUL-4881", "daemon extra work dir"),
          makeIssue("i-3", "MUL-4883", "First PR review flow"),
        ]}
        tasksByIssueId={
          new Map([
            [
              "i-1",
              [
                makeTask({ id: "t1", agent_id: "agent-1", issue_id: "i-1" }),
                makeTask({ id: "t4", agent_id: "agent-2", issue_id: "i-1" }),
              ],
            ],
            ["i-2", [makeTask({ id: "t2", agent_id: "agent-1", issue_id: "i-2" })]],
            ["i-3", [makeTask({ id: "t3", agent_id: "agent-2", issue_id: "i-3" })]],
          ])
        }
        taskCount={4}
        />,
    );

    expect(screen.getByText("3 issues · 4 runs")).toBeInTheDocument();
    // Rows group under their issue, mirroring what clicking the chip does.
    expect(screen.getByText("MUL-4879")).toBeInTheDocument();
    expect(screen.getByText("Counting logic looks wrong")).toBeInTheDocument();
    expect(screen.getAllByTestId("actor-avatar")).toHaveLength(4);
  });

  it("says nothing about work it excludes", () => {
    // Chat/autopilot runs and out-of-scope tasks leave no trace on this page,
    // so a "not counted" footnote would explain an absence the user never
    // perceived. The card only ever describes what IS counted.
    renderWithI18n(
      <WorkspaceAgentActivityHoverContent
        issues={[makeIssue("i-1", "MUL-1", "One")]}
        tasksByIssueId={
          new Map([["i-1", [makeTask({ id: "t1", issue_id: "i-1" })]]])
        }
        taskCount={1}
      />,
    );

    expect(screen.queryByText(/not counted/)).not.toBeInTheDocument();
    expect(screen.getByText("1 issue · 1 run")).toBeInTheDocument();
  });

  it("falls back to the agent-worded empty copy when nothing is counted", () => {
    renderWithI18n(
      <WorkspaceAgentActivityHoverContent
        issues={[]}
        tasksByIssueId={new Map()}
        taskCount={0}
      />,
    );

    expect(screen.getByText("No agents working right now")).toBeInTheDocument();
    expect(screen.queryByText(/not counted/)).not.toBeInTheDocument();
  });

  it("renders the Chinese copy for the counted units", () => {
    renderWithI18n(
      <WorkspaceAgentActivityHoverContent
        issues={[makeIssue("i-1", "MUL-1", "One")]}
        tasksByIssueId={
          new Map([["i-1", [makeTask({ id: "t1", issue_id: "i-1" })]]])
        }
        taskCount={2}
      />,
      { locale: "zh-Hans" },
    );

    // issue is 任务 in Chinese; the agent execution is 运行, so the two
    // counted units remain distinct (conventions.zh.mdx).
    expect(screen.getByText("1 个任务 · 2 次运行")).toBeInTheDocument();
  });
});