Skip to content

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

Go
10.7 KB
Raw Blame
/**
 * @vitest-environment jsdom
 *
 * Board grouped by project. The server's group descriptors carry a project id
 * and nothing else, so both the column header text and the column's leading
 * glyph are resolved on the client β€” and `projectId` sits next to `assigneeId`
 * on BoardColumnGroup, where an unguarded fallthrough renders a project column
 * as the "no assignee" column.
 */
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { cleanup, screen } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ViewStoreProvider } from "@multica/core/issues/stores/view-store-context";
import { getIssueSurfaceViewStore } from "@multica/core/issues/stores/surface-view-store";
import type { SortField } from "@multica/core/issues/stores/view-store";
import type { Issue, Project } from "@multica/core/types";
import { renderWithI18n } from "../../test/i18n";
import { IssueContextMenuProvider } from "../actions/issue-actions-context-menu";
import type { IssueGroupBranches } from "../surface/use-issue-group-branches";
import { BoardView } from "./board-view";
import {
  ScrollRestorationProvider,
  type ScrollRestorationAdapter,
} from "../../platform";

// jsdom has no layout, so dnd-kit never reports a hovered column by itself.
const drag = vi.hoisted(() => ({ isOver: false }));
vi.mock("@dnd-kit/core", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@dnd-kit/core")>();
  return {
    ...actual,
    useDroppable: (args: Parameters<typeof actual.useDroppable>[0]) => ({
      ...actual.useDroppable(args),
      isOver: drag.isOver,
    }),
  };
});

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

vi.mock("@multica/core/properties", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@multica/core/properties")>()),
  propertyListOptions: () => ({
    queryKey: ["properties"],
    queryFn: async () => [],
  }),
  useSetIssueProperty: () => ({ mutate: () => {} }),
  useUnsetIssueProperty: () => ({ mutate: () => {} }),
}));

vi.mock("@multica/core/workspace/hooks", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@multica/core/workspace/hooks")>()),
  useActorName: () => ({ getActorName: () => "Someone" }),
}));

vi.mock("@multica/core/auth", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@multica/core/auth")>()),
  useAuthStore: (selector: (state: { user: { id: string } }) => unknown) =>
    selector({ user: { id: "viewer-1" } }),
}));

vi.mock("@multica/core/agents", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@multica/core/agents")>()),
  isAgentRuntimeBound: () => true,
  useAgentPresenceDetail: () => ({ availability: "offline", workload: null }),
}));

vi.mock("@multica/core/paths", async (importOriginal) => {
  const actual =
    await importOriginal<typeof import("@multica/core/paths")>();
  return {
    ...actual,
    useCurrentWorkspace: () => ({ id: "ws-1", slug: "acme" }),
    useWorkspacePaths: () => actual.paths.workspace("acme"),
  };
});

vi.mock("../../navigation", () => ({
  AppLink: ({ children, ...props }: React.ComponentProps<"a">) => (
    <a {...props}>{children}</a>
  ),
  useNavigation: () => ({
    push: () => {},
    openInNewTab: () => {},
    getShareableUrl: (path: string) => `https://app.example${path}`,
    pathname: "/",
  }),
  resolveClickIntent: () => "push",
  useIntentNavigate: () => () => {},
}));

const ACME_ID = "11111111-1111-4111-8111-111111111111";
const GHOST_ID = "22222222-2222-4222-8222-222222222222";

function makeProject(id: string, title: string, icon: string | null): Project {
  return {
    id,
    workspace_id: "ws-1",
    title,
    description: null,
    icon,
    status: "in_progress",
    priority: "none",
    lead_type: null,
    lead_id: null,
    start_date: null,
    due_date: null,
    created_at: "2026-01-01T00:00:00Z",
    updated_at: "2026-01-01T00:00:00Z",
    issue_count: 0,
    done_count: 0,
    resource_count: 0,
  };
}

function makeIssue(id: string, projectId: string | null): Issue {
  return {
    id,
    workspace_id: "ws-1",
    number: 1,
    identifier: `MUL-${id}`,
    title: `Task ${id}`,
    description: null,
    status: "todo",
    priority: "none",
    assignee_type: null,
    assignee_id: null,
    creator_type: "member",
    creator_id: "member-1",
    parent_issue_id: null,
    project_id: projectId,
    position: 1,
    stage: null,
    start_date: null,
    due_date: null,
    labels: [],
    metadata: {},
    properties: {},
    created_at: "2026-01-01T00:00:00Z",
    updated_at: "2026-01-01T00:00:00Z",
  };
}

const ISSUES = [
  makeIssue("acme", ACME_ID),
  makeIssue("ghost", GHOST_ID),
  makeIssue("loose", null),
];

const NO_PROJECT_DESCRIPTOR = {
  key: "project:none",
  value: { kind: "project" as const, project_id: null },
  count: 1,
};
const PROJECT_DESCRIPTORS = [
  {
    key: `project:${ACME_ID}`,
    value: { kind: "project" as const, project_id: ACME_ID },
    count: 4,
  },
  {
    key: `project:${GHOST_ID}`,
    value: { kind: "project" as const, project_id: GHOST_ID },
    count: 1,
  },
];

/** What `useIssueGroupBranches` hands the board for `{ kind: "project" }`. */
function makeGroupBranches(
  descriptors: IssueGroupBranches["descriptors"],
  issues: Issue[],
): IssueGroupBranches {
  return {
    enabled: true,
    descriptors,
    issues,
    pagination: {},
    total: issues.length,
    isLoading: false,
    isRefreshing: false,
    isError: false,
    hasMoreGroups: false,
    isLoadingMoreGroups: false,
    loadMoreGroups: () => {},
    retryGroups: () => {},
  };
}

class ObserverStub {
  observe() {}
  unobserve() {}
  disconnect() {}
  takeRecords() {
    return [];
  }
}

describe("Board grouped by project", () => {
  let queryClient: QueryClient;

  beforeEach(() => {
    vi.stubGlobal("IntersectionObserver", ObserverStub);
    vi.stubGlobal("ResizeObserver", ObserverStub);
    queryClient = new QueryClient({
      defaultOptions: { queries: { retry: false, staleTime: Infinity } },
    });
  });

  afterEach(() => {
    cleanup();
    vi.unstubAllGlobals();
    drag.isOver = false;
  });

  function render({
    descriptors = [NO_PROJECT_DESCRIPTOR, ...PROJECT_DESCRIPTORS],
    issues = ISSUES,
    scrollAdapter = { get: () => undefined },
    sortBy,
  }: {
    descriptors?: IssueGroupBranches["descriptors"];
    issues?: Issue[];
    scrollAdapter?: ScrollRestorationAdapter;
    sortBy?: SortField;
  } = {}) {
    const store = getIssueSurfaceViewStore(
      `board-project-${Math.floor(Math.random() * 1e9)}`,
    );
    store.getState().setGrouping("project");
    if (sortBy) store.getState().setSortBy(sortBy);
    // Cards carry their own project chip by default, which would put the same
    // title in two places and make "the column header says X" unprovable.
    if (store.getState().cardProperties.project) {
      store.getState().toggleCardProperty("project");
    }
    renderWithI18n(
      <QueryClientProvider client={queryClient}>
        <ViewStoreProvider store={store}>
          <ScrollRestorationProvider adapter={scrollAdapter}>
          <IssueContextMenuProvider>
          <BoardView
            issues={issues}
            visibleStatuses={["todo"]}
            onMoveIssue={() => {}}
            projectMap={
              new Map([[ACME_ID, makeProject(ACME_ID, "Acme Corp", "πŸš€")]])
            }
            groupBranches={makeGroupBranches(descriptors, issues)}
          />
          </IssueContextMenuProvider>
          </ScrollRestorationProvider>
        </ViewStoreProvider>
      </QueryClientProvider>,
    );
  }

  it("titles each column with its project, never with its id", () => {
    render();

    expect(screen.getByText("Acme Corp")).toBeTruthy();
    expect(screen.getByText("No project")).toBeTruthy();
    expect(screen.queryByText(ACME_ID)).toBeNull();
    expect(screen.queryByText(GHOST_ID)).toBeNull();
  });

  it("keeps a No project column when the server returns no such group", () => {
    // Clearing a card's project by dragging has to stay possible in a workspace
    // where every card currently has one, so this column is not derived from
    // the result set like the others.
    render({
      descriptors: PROJECT_DESCRIPTORS,
      issues: ISSUES.filter((issue) => issue.project_id !== null),
    });

    const header = screen.getByText("No project").parentElement!;
    expect(header.textContent).toContain("0");
  });

  it("keeps exactly one No project column when the server does return it", () => {
    render();

    expect(screen.getAllByText("No project")).toHaveLength(1);
  });

  it("reads an unresolvable project the way the Table does", () => {
    // The board and the table must not describe the same project differently.
    render();

    expect(screen.getByText("Unavailable value")).toBeTruthy();
  });

  it("shows the server's group count, not the loaded card count", () => {
    render();

    // Acme holds four issues; only one is on this page.
    const header = screen.getByText("Acme Corp").parentElement!;
    expect(header.textContent).toContain("4");
  });

  it("heads a project column with that project's own icon", () => {
    render();

    // BoardColumnGroup carries projectId beside assigneeId, so a project column
    // that is not matched explicitly falls through to the ASSIGNEE heading β€”
    // which renders the same title text next to a no-assignee actor glyph. The
    // icon is what tells the two apart.
    const header = screen.getByText("Acme Corp").parentElement!;
    expect(header.textContent).toContain("\u{1F680}");
  });

  it("puts each card in its own project column", () => {
    render();

    const acmeColumn = screen.getByText("Acme Corp").closest("div.flex-col")!;
    expect(acmeColumn.textContent).toContain("MUL-acme");
    expect(acmeColumn.textContent).not.toContain("MUL-loose");
  });

  it.each([
    ["the default sort", undefined, "Updated date"],
    ["created date", "created_at", "Created date"],
    ["priority", "priority", "Priority"],
  ] as const)("names %s in the hint a hovered column shows", (_, sortBy, field) => {
    drag.isOver = true;
    render({ sortBy });

    expect(screen.getAllByText(`Board ordered by ${field}`).length).toBeGreaterThan(0);
  });

  it("brings the column strip back to where it was scrolled sideways", () => {
    render({
      scrollAdapter: {
        get: (key) =>
          key === "board" ? { top: 0, height: 600, left: 480 } : undefined,
      },
    });

    const strip = document.querySelector<HTMLElement>("[data-board-scroller]")!;
    // Marked so the tab coordinator captures it on the way out …
    expect(strip.getAttribute("data-tab-scroll-root")).toBe("board");
    // … and given the saved offset back when it mounts again.
    expect(strip.scrollLeft).toBe(480);
  });
});