Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

17.5 KB
Raw Blame
import {
  AGENT_DOCUMENT_CATEGORY,
  AGENT_DOCUMENT_SKILL_CATEGORY,
  CUSTOM_DOCUMENT_FILE_TYPE,
  CUSTOM_FOLDER_FILE_TYPE,
} from '@lobechat/const';
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import type { DragEvent as ReactDragEvent, MouseEvent as ReactMouseEvent, ReactNode } from 'react';
import { MemoryRouter } from 'react-router';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import type { ExplorerTreeNode } from '@/features/ExplorerTree';
import type * as EventPathModule from '@/features/ExplorerTree/view/eventPath';

import DocumentExplorerTree from './DocumentExplorerTree';
import type { AgentDocumentItem } from './types';

const navigateMock = vi.hoisted(() => vi.fn());
const messageError = vi.hoisted(() => vi.fn());
const messageSuccess = vi.hoisted(() => vi.fn());
const messageWarning = vi.hoisted(() => vi.fn());
const modalConfirm = vi.hoisted(() => vi.fn());
const openDocumentMock = vi.hoisted(() => vi.fn());
const removeDocumentMock = vi.hoisted(() => vi.fn());

vi.mock('@lobehub/ui', async (importOriginal) => ({
  ...((await importOriginal()) as Record<string, unknown>),
  ActionIcon: ({
    icon,
    onClick,
    title,
  }: {
    icon?: { displayName?: string; name?: string };
    onClick?: () => void;
    title?: string;
  }) => (
    <button aria-label={title} data-icon={icon?.displayName ?? icon?.name} onClick={onClick}>
      {title}
    </button>
  ),
  confirmModal: modalConfirm,
  DropdownMenu: ({
    children,
    items,
  }: {
    children: ReactNode;
    items: { key?: string; label?: string; onClick?: () => void; type?: string }[];
  }) => (
    <div>
      {children}
      {items
        .filter((item) => item.type !== 'divider' && item.key)
        .map((item) => (
          <button data-testid={`create-menu-${item.key}`} key={item.key} onClick={item.onClick}>
            {item.label}
          </button>
        ))}
    </div>
  ),
}));

vi.mock('antd', async (importOriginal) => ({
  ...(await importOriginal<object>()),
  App: {
    useApp: () => ({
      message: { error: messageError, success: messageSuccess, warning: messageWarning },
      modal: { confirm: modalConfirm },
    }),
  },
}));

vi.mock('@/services/agentDocument', () => ({
  agentDocumentService: {
    removeDocument: removeDocumentMock,
  },
}));

vi.mock('@/features/Workspace/useWorkspaceAwareNavigate', () => ({
  useWorkspaceAwareNavigate: () => navigateMock,
}));

vi.mock('@/features/ExplorerTree', async () => {
  const { isItemToggleEventPath } = await vi.importActual<typeof EventPathModule>(
    '@/features/ExplorerTree/view/eventPath',
  );

  interface MockExplorerTreeProps {
    canDrag?: (node: ExplorerTreeNode<unknown>) => boolean;
    defaultExpandedIds?: string[];
    getContextMenuItems?: (node: ExplorerTreeNode<unknown>) => unknown[] | undefined;
    header?: ReactNode;
    nodes: ExplorerTreeNode<unknown>[];
    onNodeClick?: (node: ExplorerTreeNode<unknown>, event: ReactMouseEvent<HTMLElement>) => void;
    onNodeDragStart?: (node: ExplorerTreeNode<unknown>, event: ReactDragEvent<HTMLElement>) => void;
  }

  interface MockMenuItem {
    key?: number | string;
    onClick?: () => void;
    type?: string;
  }

  const ExplorerTree = ({
    canDrag,
    defaultExpandedIds,
    getContextMenuItems,
    header,
    nodes,
    onNodeClick,
    onNodeDragStart,
  }: MockExplorerTreeProps) => {
    const renderNodes = (parentId: string | null): ReactNode =>
      nodes
        .filter((node) => (node.parentId ?? null) === parentId)
        .map((node) => {
          const menuItems = (getContextMenuItems?.(node) ?? []) as MockMenuItem[];
          return (
            <div
              data-appearance={node.appearance ?? 'default'}
              data-can-drag={String(canDrag?.(node) ?? true)}
              data-folder={String(!!node.isFolder)}
              data-menu-count={String(menuItems.length)}
              data-testid={`tree-node-${node.id}`}
              key={node.id}
              onDragStart={(event) => {
                event.stopPropagation();
                onNodeDragStart?.(node, event);
              }}
            >
              <button
                data-testid={`tree-node-button-${node.id}`}
                onClick={(event) => onNodeClick?.(node, event)}
              >
                <span
                  data-item-section="icon"
                  data-testid={`tree-node-toggle-${node.id}`}
                  onClick={(event) => {
                    event.stopPropagation();
                    onNodeClick?.(node, event);
                  }}
                />
                {node.name}
              </button>
              {node.isFolder && (
                <div data-testid={`tree-node-children-${node.id}`}>{renderNodes(node.id)}</div>
              )}
              {menuItems
                .filter((item) => item.type !== 'divider' && item.key !== undefined)
                .map((item) => (
                  <button
                    data-testid={`tree-menu-${node.id}-${String(item.key)}`}
                    key={item.key}
                    onClick={() => item.onClick?.()}
                  >
                    {item.key}
                  </button>
                ))}
            </div>
          );
        });

    return (
      <div
        data-default-expanded-ids={JSON.stringify(defaultExpandedIds ?? [])}
        data-testid="explorer-tree"
      >
        {header}
        {renderNodes(null)}
      </div>
    );
  };

  return {
    DISABLE_ROW_TEXT_SELECTION_CSS: '',
    DOCUMENT_TREE_ROW_CSS: '',
    DOCUMENT_TREE_ICON_CSS: '',
    DOCUMENT_TREE_PARENT_DOCUMENT_GLYPH_CSS: '',
    isItemToggleEventPath,
    DOCUMENT_TREE_LAYOUT: {
      fontSize: 14,
      iconGap: 8,
      iconSize: 16,
      iconWidth: 16,
      itemHeight: 36,
      levelGap: 8,
    },
    ExplorerTree,
    FOLDER_ICON_CSS: '',
    HIDE_POINTER_FOCUS_RING_CSS: '',
    getExplorerTreeStyleVars: () => ({}),
  };
});

const createDocument = (overrides: Partial<AgentDocumentItem>): AgentDocumentItem =>
  ({
    accessPublic: 0,
    accessSelf: 0,
    accessShared: 0,
    agentId: 'agent-1',
    category: AGENT_DOCUMENT_CATEGORY,
    content: '',
    createdAt: new Date('2026-05-09T00:00:00Z'),
    deletedAt: null,
    deletedByAgentId: null,
    deletedByUserId: null,
    deleteReason: null,
    description: null,
    documentId: 'doc-1',
    editorData: null,
    filename: 'document.md',
    fileType: CUSTOM_DOCUMENT_FILE_TYPE,
    id: 'agent-doc-1',
    isFolder: false,
    isSkillBundle: false,
    isSkillIndex: false,
    loadRules: {},
    metadata: null,
    parentId: null,
    policy: null,
    policyLoad: 'disabled',
    policyLoadFormat: 'raw',
    policyLoadPosition: 'before-first-user',
    policyLoadRule: 'always',
    source: null,
    sourceType: 'file',
    templateId: null,
    title: 'Document',
    updatedAt: new Date('2026-05-09T00:00:00Z'),
    userId: 'user-1',
    ...overrides,
  }) as AgentDocumentItem;

const skillBundleInput = (): Parameters<typeof createDocument>[0] => ({
  category: AGENT_DOCUMENT_SKILL_CATEGORY,
  documentId: 'skill-bundle-doc',
  fileType: 'skills/bundle',
  filename: 'youtube-comment-retrieval-workflow',
  id: 'skill-bundle-row',
  isFolder: true,
  isSkillBundle: true,
  templateId: 'agent-skill',
  title: 'YouTube Comment Retrieval Workflow',
});

const notesFolderInput = (): Parameters<typeof createDocument>[0] => ({
  documentId: 'folder-doc',
  fileType: CUSTOM_FOLDER_FILE_TYPE,
  filename: 'Notes',
  id: 'folder-row',
  isFolder: true,
  title: 'Notes',
});

describe('DocumentExplorerTree', () => {
  beforeEach(() => {
    messageError.mockReset();
    messageSuccess.mockReset();
    messageWarning.mockReset();
    modalConfirm.mockReset();
    navigateMock.mockReset();
    openDocumentMock.mockReset();
    removeDocumentMock.mockReset();
    removeDocumentMock.mockResolvedValue({ deleted: true, id: 'skill-bundle-row' });
  });

  it('uses one plus menu for creating documents and folders', () => {
    render(
      <DocumentExplorerTree agentId="agent-1" data={[createDocument({})]} mutate={vi.fn()} />,
      { wrapper: MemoryRouter },
    );

    expect(
      screen.getByRole('button', { name: 'workingPanel.resources.tree.create' }),
    ).toHaveAttribute('data-icon', 'Plus');
    expect(screen.getByTestId('create-menu-new-document')).toHaveTextContent(
      'workingPanel.resources.tree.newDocument',
    );
    expect(screen.getByTestId('create-menu-new-folder')).toHaveTextContent(
      'workingPanel.resources.tree.newFolder',
    );
    expect(screen.getByTestId('create-menu-upload-file')).toHaveTextContent(
      'workingPanel.resources.tree.uploadFile',
    );
  });

  // A workflow document holding its run records must stay expandable in the
  // tree; before, nested children of a document were dropped from the panel.
  it('shows the documents nested under a document and still opens the parent', () => {
    const data = [
      createDocument({
        documentId: 'sop-doc',
        filename: 'Fix-round SOP',
        id: 'sop-row',
        title: 'Fix-round SOP',
      }),
      createDocument({
        documentId: 'run-doc',
        filename: 'Run 1',
        id: 'run-row',
        parentId: 'sop-doc',
        title: 'Run 1',
      }),
    ];
    const onOpenDocument = vi.fn();

    render(
      <DocumentExplorerTree
        agentId="agent-1"
        data={data}
        mutate={vi.fn()}
        onOpenDocument={onOpenDocument}
      />,
      { wrapper: MemoryRouter },
    );

    const parent = screen.getByTestId('tree-node-sop-row');
    expect(parent).toHaveAttribute('data-folder', 'true');
    expect(parent).toHaveAttribute('data-appearance', 'file');
    expect(
      within(screen.getByTestId('tree-node-children-sop-row')).getByTestId('tree-node-run-row'),
    ).toBeInTheDocument();
    expect(screen.getByTestId('tree-node-run-row')).toHaveAttribute('data-appearance', 'default');

    // The chevron only expands or collapses; the title opens the document.
    fireEvent.click(screen.getByTestId('tree-node-toggle-sop-row'));
    expect(onOpenDocument).not.toHaveBeenCalled();
    fireEvent.click(screen.getByTestId('tree-node-button-sop-row'));
    expect(onOpenDocument).toHaveBeenCalledWith('sop-doc', 'sop-row');

    // Still a document: it can be opened as a page and gain more sub-documents,
    // but does not grow the folder-only entries.
    expect(screen.getByTestId('tree-menu-sop-row-open-as-page')).toBeInTheDocument();
    expect(screen.getByTestId('tree-menu-sop-row-new-sub-document')).toBeInTheDocument();
    expect(screen.queryByTestId('tree-menu-sop-row-new-folder')).not.toBeInTheDocument();
  });

  it('offers a sub-document entry on a plain document', () => {
    render(
      <DocumentExplorerTree agentId="agent-1" data={[createDocument({})]} mutate={vi.fn()} />,
      { wrapper: MemoryRouter },
    );

    expect(screen.getByTestId('tree-node-agent-doc-1')).toHaveAttribute('data-folder', 'false');
    expect(screen.getByTestId('tree-menu-agent-doc-1-new-sub-document')).toBeInTheDocument();
    // A folder can be nested under a document too, e.g. to group a workflow's runs.
    expect(screen.getByTestId('tree-menu-agent-doc-1-new-sub-folder')).toBeInTheDocument();
  });

  it('offers upload on a folder context menu', () => {
    const data = [createDocument(notesFolderInput())];

    render(<DocumentExplorerTree agentId="agent-1" data={data} mutate={vi.fn()} />, {
      wrapper: MemoryRouter,
    });

    expect(screen.getByTestId('tree-menu-folder-row-upload-file')).toBeInTheDocument();
  });

  it('renders managed skill bundle as a folder with SKILL.md underneath', () => {
    const data = [
      createDocument(skillBundleInput()),
      createDocument({
        category: AGENT_DOCUMENT_SKILL_CATEGORY,
        documentId: 'skill-index-doc',
        fileType: 'skills/index',
        filename: 'SKILL.md',
        id: 'skill-index-row',
        isSkillIndex: true,
        parentId: 'skill-bundle-doc',
        templateId: 'agent-skill',
        title: 'Generated workflow title',
      }),
      createDocument(notesFolderInput()),
    ];

    render(<DocumentExplorerTree agentId="agent-1" data={data} mutate={vi.fn()} />, {
      wrapper: MemoryRouter,
    });

    const bundleNode = screen.getByTestId('tree-node-skill-bundle-row');
    expect(bundleNode).toHaveAttribute('data-folder', 'true');
    expect(bundleNode).toHaveAttribute('data-can-drag', 'false');
    expect(bundleNode).toHaveAttribute('data-menu-count', '0');
    expect(within(bundleNode).getByText('SKILL.md')).toBeInTheDocument();

    const skillIndexNode = screen.getByTestId('tree-node-skill-index-row');
    expect(skillIndexNode).toHaveAttribute('data-folder', 'false');
    expect(skillIndexNode).toHaveAttribute('data-can-drag', 'false');
    expect(skillIndexNode).toHaveAttribute('data-menu-count', '0');
  });

  it('opens SKILL.md in the document page but does not open the empty skill bundle', () => {
    const data = [
      createDocument(skillBundleInput()),
      createDocument({
        category: AGENT_DOCUMENT_SKILL_CATEGORY,
        documentId: 'skill-index-doc',
        fileType: 'skills/index',
        filename: 'SKILL.md',
        id: 'skill-index-row',
        isSkillIndex: true,
        parentId: 'skill-bundle-doc',
        templateId: 'agent-skill',
        title: 'SKILL.md',
      }),
    ];

    render(<DocumentExplorerTree agentId="agent-1" data={data} mutate={vi.fn()} />, {
      wrapper: MemoryRouter,
    });

    fireEvent.click(screen.getByTestId('tree-node-button-skill-bundle-row'));
    expect(navigateMock).not.toHaveBeenCalled();

    fireEvent.click(screen.getByTestId('tree-node-button-skill-index-row'));
    expect(navigateMock).toHaveBeenCalledWith('/agent/agent-1/docs/skill-index-doc');
  });

  it('delegates document opening to the caller when provided', () => {
    const data = [
      createDocument({
        documentId: 'doc-content-1',
        id: 'agent-doc-row-1',
        title: 'Brief',
      }),
    ];

    render(
      <DocumentExplorerTree
        agentId="agent-1"
        data={data}
        mutate={vi.fn()}
        onOpenDocument={openDocumentMock}
      />,
      {
        wrapper: MemoryRouter,
      },
    );

    fireEvent.click(screen.getByTestId('tree-node-button-agent-doc-row-1'));

    expect(openDocumentMock).toHaveBeenCalledWith('doc-content-1', 'agent-doc-row-1');
    expect(navigateMock).not.toHaveBeenCalled();
  });

  it('only drags plain documents out of the tree', () => {
    const onDocumentDragStart = vi.fn();
    const data = [
      createDocument({ documentId: 'doc-plain', id: 'plain-row', title: 'Plain' }),
      createDocument({ documentId: 'doc-folder', id: 'folder-row', isFolder: true }),
      createDocument({ documentId: 'doc-pending', id: 'pending:new-row', title: 'Creating' }),
      createDocument({
        category: AGENT_DOCUMENT_SKILL_CATEGORY,
        documentId: 'doc-skill',
        id: 'skill-row',
      }),
    ];

    render(
      <DocumentExplorerTree
        agentId="agent-1"
        data={data}
        mutate={vi.fn()}
        onDocumentDragStart={onDocumentDragStart}
      />,
      { wrapper: MemoryRouter },
    );

    fireEvent.dragStart(screen.getByTestId('tree-node-folder-row'));
    fireEvent.dragStart(screen.getByTestId('tree-node-skill-row'));
    fireEvent.dragStart(screen.getByTestId('tree-node-pending:new-row'));
    expect(onDocumentDragStart).not.toHaveBeenCalled();

    fireEvent.dragStart(screen.getByTestId('tree-node-plain-row'));
    expect(onDocumentDragStart).toHaveBeenCalledTimes(1);
    expect(onDocumentDragStart.mock.calls[0][0]).toMatchObject({ id: 'plain-row', title: 'Plain' });
  });

  it('does not default-expand document folders', () => {
    const data = [
      createDocument(notesFolderInput()),
      createDocument({
        documentId: 'nested-folder-doc',
        fileType: CUSTOM_FOLDER_FILE_TYPE,
        filename: 'Archive',
        id: 'nested-folder-row',
        isFolder: true,
        parentId: 'folder-doc',
        title: 'Archive',
      }),
    ];

    render(<DocumentExplorerTree agentId="agent-1" data={data} mutate={vi.fn()} />, {
      wrapper: MemoryRouter,
    });

    expect(screen.getByTestId('explorer-tree')).toHaveAttribute('data-default-expanded-ids', '[]');
  });

  it('shows delete recovery action for a managed skill bundle without SKILL.md', async () => {
    const mutate = vi.fn().mockResolvedValue(undefined);
    const data = [createDocument(skillBundleInput())];

    render(<DocumentExplorerTree agentId="agent-1" data={data} mutate={mutate} />, {
      wrapper: MemoryRouter,
    });

    const bundleNode = screen.getByTestId('tree-node-skill-bundle-row');
    expect(bundleNode).toHaveAttribute('data-folder', 'true');
    expect(bundleNode).toHaveAttribute('data-can-drag', 'false');
    expect(bundleNode).toHaveAttribute('data-menu-count', '1');
    expect(screen.queryByTestId('tree-menu-skill-bundle-row-rename')).not.toBeInTheDocument();
    expect(screen.getByTestId('tree-menu-skill-bundle-row-delete')).toBeInTheDocument();

    fireEvent.click(screen.getByTestId('tree-menu-skill-bundle-row-delete'));

    const [firstConfirmCall] = modalConfirm.mock.calls;
    const [{ onOk }] = firstConfirmCall;
    // onOk now returns synchronously so the modal closes immediately; the
    // server call runs in the background.
    onOk();

    // Optimistic removal happens before the server call.
    expect(mutate).toHaveBeenCalled();

    await waitFor(() => {
      expect(removeDocumentMock).toHaveBeenCalledWith({
        agentId: 'agent-1',
        documentId: 'skill-bundle-doc',
        id: 'skill-bundle-row',
        topicId: undefined,
        withChildren: true,
      });
    });
    expect(messageSuccess).not.toHaveBeenCalled();
  });
});