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 { useState } from "react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import { VirtuosoMockContext } from "react-virtuoso";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { api } from "@multica/core/api";
import type { Agent, AgentActivityBucket, AgentTask } from "@multica/core/types";
import { WorkspaceSlugProvider } from "@multica/core/paths";
import { I18nProvider } from "@multica/core/i18n/react";
import enCommon from "../../../locales/en/common.json";
import enAgents from "../../../locales/en/agents.json";
import {
  NavigationProvider,
  type NavigationAdapter,
} from "../../../navigation";

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

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

vi.mock("@multica/core/api", () => ({ api: {
  getIssue: vi.fn((id: string) => Promise.resolve({ id, title: `Issue ${id}`, identifier: id })),
} }));

// Keep transcript internals out of history pagination tests while exposing
// the identity of each rendered task row.
vi.mock("../../../common/task-transcript", () => ({
  TranscriptButton: ({ task }: { task: AgentTask }) => <span data-testid={`task-${task.id}`} />,
}));

// Keep "Now" empty while varying activity outcomes and task-list loading.
const agentTasksRef = vi.hoisted(() => ({
  current: (_before?: string) => new Promise<unknown>(() => {}),
}));
const activityRef = vi.hoisted(() => ({ current: [] as AgentActivityBucket[] }));
vi.mock("@multica/core/agents", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@multica/core/agents")>();
  return {
    ...actual,
    agentTaskSnapshotOptions: () => ({
      queryKey: ["snapshot"],
      queryFn: () => Promise.resolve([]),
    }),
    agentTasksOptions: () => ({
      queryKey: ["agent-tasks"],
      initialPageParam: undefined,
      getNextPageParam: (page: { nextCursor: string | null }) => page.nextCursor ?? undefined,
      queryFn: ({ pageParam }: { pageParam?: string }) => agentTasksRef.current(pageParam),
    }),
    useWorkspaceActivityMap: () => ({
      byAgent: new Map([[
        "agent-1",
        actual.deriveAgentActivity(activityRef.current, "2026-01-01", Date.now()),
      ]]),
    }),
  };
});

import { AgentPerformanceSummary, AgentWorkSections } from "./activity-tab";

const baseAgent = {
  id: "agent-1",
  name: "Agent",
} as unknown as Agent;

const EMPTY_RECENT = "This agent hasn't completed anything yet.";

function WorkSections({ onViewAll }: { onViewAll: () => void }) {
  const [scrollParent, setScrollParent] = useState<HTMLDivElement | null>(null);
  return <div ref={setScrollParent} style={{ height: 1200, overflowY: "auto" }}>
    <AgentWorkSections agent={baseAgent} onViewAll={onViewAll} scrollParent={scrollParent} />
  </div>;
}

function renderTab(performance = false, onViewAll = vi.fn()) {
  const queryClient = new QueryClient({
    defaultOptions: { queries: { retry: false } },
  });
  const navigation: NavigationAdapter = {
    push: vi.fn(),
    replace: vi.fn(),
    back: vi.fn(),
    pathname: "/acme/agents/agent-1",
    searchParams: new URLSearchParams(),
    hash: "",
    getShareableUrl: (path) => path,
  };
  return render(
    <I18nProvider locale="en" resources={TEST_RESOURCES}>
      <NavigationProvider value={navigation}>
        <QueryClientProvider client={queryClient}>
          <WorkspaceSlugProvider slug="acme">
            {performance && <AgentPerformanceSummary agent={baseAgent} />}
            <VirtuosoMockContext.Provider value={{ viewportHeight: 1600, itemHeight: 40 }}>
              <WorkSections onViewAll={onViewAll} />
            </VirtuosoMockContext.Provider>
          </WorkspaceSlugProvider>
        </QueryClientProvider>
      </NavigationProvider>
    </I18nProvider>,
  );
}

beforeEach(() => {
  agentTasksRef.current = () => new Promise<unknown>(() => {});
  activityRef.current = [];
  vi.mocked(api.getIssue).mockClear();
});

describe("agent outcome presentation", () => {
  it("uses completed and failed outcomes for success and shows cancellations separately", () => {
    activityRef.current = [{
      agent_id: "agent-1",
      bucket_at: new Date().toISOString(),
      task_count: 10,
      failed_count: 1,
      completed_count: 1,
      cancelled_count: 8,
    }];
    renderTab(true);
    expect(screen.getByText("50%")).toBeInTheDocument();
    expect(screen.getByText("8 cancelled")).toBeInTheDocument();
    expect(screen.queryByText("90%")).not.toBeInTheDocument();
  });

  it("does not claim success for cancelled-only data", () => {
    activityRef.current = [{
      agent_id: "agent-1",
      bucket_at: new Date().toISOString(),
      task_count: 8,
      failed_count: 0,
      completed_count: 0,
      cancelled_count: 8,
    }];
    const { container } = renderTab(true);
    expect(screen.queryByText("100%")).not.toBeInTheDocument();
    expect(screen.queryByText("100% success")).not.toBeInTheDocument();
    expect(container.querySelectorAll('rect[fill="var(--color-brand)"]')).toHaveLength(0);
    expect(screen.getByText("success rate").parentElement).toHaveTextContent("—");
  });
});

describe("AgentWorkSections Recent work loading state", () => {
  it("shows a skeleton, not the empty state, while the task list is loading", () => {
    // Never-resolving queryFn keeps the per-agent task query pending, which is
    // exactly the first-paint window the skeleton is meant to cover.
    const { container } = renderTab();
    expect(
      container.querySelectorAll('[data-slot="skeleton"]').length,
    ).toBeGreaterThan(0);
    expect(screen.queryByText(EMPTY_RECENT)).not.toBeInTheDocument();
  });

  it("shows the empty state once the task list resolves to no runs", async () => {
    agentTasksRef.current = () => Promise.resolve({ tasks: [], nextCursor: null });
    renderTab();
    expect(await screen.findByText(EMPTY_RECENT)).toBeInTheDocument();
    expect(
      document.querySelectorAll('[data-slot="skeleton"]').length,
    ).toBe(0);
  });
});


describe("AgentWorkSections server pagination", () => {
  it("requests older history only on demand, disables duplicate fetches, and retries failures", async () => {
    let finish: (value: unknown) => void = () => {};
    const query = vi.fn()
      .mockResolvedValueOnce({ tasks: [], nextCursor: "older" })
      .mockImplementationOnce(() => new Promise((resolve) => { finish = resolve; }))
      .mockRejectedValueOnce(new Error("offline"))
      .mockResolvedValueOnce({ tasks: [], nextCursor: null });
    agentTasksRef.current = query;
    renderTab();
    const more = await screen.findByRole("button", { name: /Show more/ });
    expect(query).toHaveBeenCalledTimes(1);
    fireEvent.click(more);
    await waitFor(() => expect(more).toBeDisabled());
    fireEvent.click(more);
    expect(query).toHaveBeenCalledTimes(2);
    expect(query.mock.calls[1]?.[0]).toBe("older");
    finish({ tasks: [], nextCursor: "oldest" });
    await waitFor(() => expect(more).toBeEnabled());
    fireEvent.click(more);
    const retry = await screen.findByRole("button", { name: "Try again" });
    fireEvent.click(retry);
    await waitFor(() => expect(screen.queryByRole("button", { name: /Show more/ })).not.toBeInTheDocument());
    expect(query.mock.calls[3]?.[0]).toBe("oldest");
  });

  it("renders aggregate duration without fetching task history for the performance summary", () => {
    const query = vi.fn();
    agentTasksRef.current = query;
    activityRef.current = [{ agent_id: "agent-1", bucket_at: new Date().toISOString(), task_count: 201,
      completed_count: 201, failed_count: 0, cancelled_count: 0, duration_ms: 24120000, duration_count: 201 }];
    render(<I18nProvider locale="en" resources={TEST_RESOURCES}>
      <QueryClientProvider client={new QueryClient()}><AgentPerformanceSummary agent={baseAgent} /></QueryClientProvider>
    </I18nProvider>);
    expect(screen.getByText("2m 00s")).toBeInTheDocument();
    expect(query).not.toHaveBeenCalled();
  });
});


function historyTask(index: number): AgentTask {
  return {
    id: String(index), agent_id: "agent-1", runtime_id: "runtime-1", issue_id: "",
    status: "completed", priority: 0, dispatched_at: null, started_at: null,
    completed_at: new Date(Date.now() - index * 60000).toISOString(),
    created_at: new Date(Date.now() - index * 60000).toISOString(),
    result: null, error: null,
  };
}

describe("AgentWorkSections loaded history rows", () => {
  it("reveals cached rows before requesting older history and deduplicates overlapping pages", async () => {
    const tasks = Array.from({ length: 31 }, (_, i) => historyTask(i));
    const query = vi.fn()
      .mockResolvedValueOnce({ tasks, nextCursor: "older" })
      .mockResolvedValueOnce({ tasks: [tasks[30], historyTask(31)], nextCursor: null });
    agentTasksRef.current = query;
    renderTab();
    await screen.findByTestId("task-0");
    expect(screen.getAllByTestId(/^task-/)).toHaveLength(10);
    expect(screen.queryByTestId("task-10")).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole("button", { name: /Show more/ }));
    expect(screen.getAllByTestId(/^task-/)).toHaveLength(30);
    expect(query).toHaveBeenCalledTimes(1);
    fireEvent.click(screen.getByRole("button", { name: /Show more/ }));
    expect(screen.getAllByTestId(/^task-/)).toHaveLength(31);
    expect(query).toHaveBeenCalledTimes(1);
    fireEvent.click(screen.getByRole("button", { name: /Show more/ }));
    await screen.findByTestId("task-31");
    expect(query).toHaveBeenCalledTimes(2);
    expect(query.mock.calls[1]?.[0]).toBe("older");
    expect(screen.getAllByTestId(/^task-/)).toHaveLength(32);
    expect(screen.getAllByTestId("task-30")).toHaveLength(1);
    expect(screen.queryByRole("button", { name: /Show more/ })).not.toBeInTheDocument();
  });

  it("keeps loaded rows visible when an older page fails and retries the same cursor", async () => {
    const query = vi.fn()
      .mockResolvedValueOnce({ tasks: [historyTask(0)], nextCursor: "older" })
      .mockRejectedValueOnce(new Error("offline"))
      .mockResolvedValueOnce({ tasks: [historyTask(1)], nextCursor: null });
    agentTasksRef.current = query;
    renderTab();
    await screen.findByTestId("task-0");
    fireEvent.click(screen.getByRole("button", { name: /Show more/ }));
    const retry = await screen.findByRole("button", { name: "Try again" });
    expect(screen.getByTestId("task-0")).toBeInTheDocument();
    expect(screen.queryByText(EMPTY_RECENT)).not.toBeInTheDocument();
    fireEvent.click(retry);
    await screen.findByTestId("task-1");
    expect(screen.getAllByTestId(/^task-/)).toHaveLength(2);
    expect(query.mock.calls.slice(1).map(([before]) => before)).toEqual(["older", "older"]);
  });
});

// The Recent work count is the 7-day run total from the activity window, not
// the number of history rows loaded so far.
describe("AgentWorkSections section counts", () => {
  it("counts this week's runs from the activity window, whatever history has loaded", async () => {
    activityRef.current = [
      { agent_id: "agent-1", bucket_at: new Date().toISOString(), task_count: 38,
        failed_count: 2, completed_count: 36, cancelled_count: 0 },
      { agent_id: "agent-1", bucket_at: new Date(Date.now() - 20 * 86_400_000).toISOString(),
        task_count: 90, failed_count: 0, completed_count: 90, cancelled_count: 0 },
    ];
    agentTasksRef.current = () => Promise.resolve({
      tasks: Array.from({ length: 200 }, (_, i) => historyTask(i)),
      nextCursor: "older",
    });
    renderTab();
    await screen.findByTestId("task-0");
    expect(screen.getByText("38 this week")).toBeInTheDocument();
    expect(screen.getAllByTestId(/^task-/)).toHaveLength(10);
  });

  it("shows no Now count and a quiet line while nothing is running", async () => {
    agentTasksRef.current = () => Promise.resolve({ tasks: [], nextCursor: null });
    renderTab();
    expect(
      await screen.findByText("This agent isn't running anything right now."),
    ).toBeInTheDocument();
    expect(screen.queryByText(/^\d+ runs?$/)).not.toBeInTheDocument();
  });

  it("opens the full work list from View all", () => {
    const onViewAll = vi.fn();
    renderTab(false, onViewAll);
    fireEvent.click(screen.getByRole("button", { name: "View all" }));
    expect(onViewAll).toHaveBeenCalledTimes(1);
  });
});


describe("AgentWorkSections windowed history", () => {
  it("bounds mounted rows after repeatedly revealing cached history", async () => {
    agentTasksRef.current = () => Promise.resolve({
      tasks: Array.from({ length: 210 }, (_, i) => historyTask(i)),
      nextCursor: null,
    });
    renderTab();
    await screen.findByTestId("task-0");
    for (let i = 0; i < 10; i++) {
      fireEvent.click(screen.getByRole("button", { name: /Show more/ }));
    }
    expect(screen.queryByRole("button", { name: /Show more/ })).not.toBeInTheDocument();
    expect(screen.getAllByTestId(/^task-/).length).toBeLessThan(50);
    expect(document.querySelector("[data-virtuoso-scroller]")).toBeInTheDocument();
  });

  it("resolves issues only for the mounted window, sharing duplicate issue lookups", async () => {
    agentTasksRef.current = () => Promise.resolve({
      tasks: Array.from({ length: 210 }, (_, i) => ({ ...historyTask(i), issue_id: `issue-${Math.floor(i / 2)}` })),
      nextCursor: null,
    });
    renderTab();
    await screen.findAllByText("Issue issue-0");
    for (let i = 0; i < 10; i++) {
      fireEvent.click(screen.getByRole("button", { name: /Show more/ }));
    }
    await screen.findAllByText("Issue issue-10");
    expect(vi.mocked(api.getIssue).mock.calls.length).toBeLessThan(30);
    expect(vi.mocked(api.getIssue).mock.calls.filter(([id]) => id === "issue-0")).toHaveLength(1);
    expect(screen.queryByText("Issue issue-104")).not.toBeInTheDocument();
  });
});

describe("AgentWorkSections failure label", () => {
  it("lets a long failure reason truncate beside the title and shows it in full on hover", async () => {
    const failed: AgentTask = {
      ...historyTask(0),
      issue_id: "issue-1",
      status: "failed",
      failure_reason: "runtime_access_denied",
    } as AgentTask;
    agentTasksRef.current = () => Promise.resolve({ tasks: [failed], nextCursor: null });
    renderTab();
    const fullText = enAgents.task_failure.reasons.runtime_access_denied;
    const label = await screen.findByText(fullText);
    expect(label).toHaveAttribute("title", fullText);
    expect(label).toHaveClass("min-w-0", "truncate");
    expect(label).not.toHaveClass("shrink-0");
    expect(await screen.findByText("Issue issue-1")).toBeInTheDocument();
  });
});