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.

5.3 KB
Raw Blame
import { render } from '@testing-library/react';
import type { ReactNode } from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import type { ViewMode } from '@/features/ResourceManager/store/initialState';
import { FilesTabs, ResourceSourceFilter, SortType } from '@/types/files';

import ResourceExplorer from './index';

const mocks = vi.hoisted(() => ({
  resourceManagerState: {
    category: 'all' as FilesTabs,
    libraryId: undefined as string | undefined,
    listVisibility: 'private' as const,
    searchQuery: null as string | null,
    sorter: 'createdAt' as const,
    sortType: 'desc' as SortType,
    sourceFilter: undefined as ResourceSourceFilter | undefined,
    viewMode: 'list' as ViewMode,
  },
  useFetchResources: vi.fn(),
}));

vi.mock('@/features/ResourceManager/hooks/useFolderPath', () => ({
  useFolderPath: () => ({ currentFolderId: null }),
}));

vi.mock('@/features/ResourceManager/hooks/useResourceManagerUrlSync', () => ({
  useResourceManagerUrlSync: vi.fn(),
}));

vi.mock('@/features/ResourceManager/store', () => ({
  useResourceManagerStore: (selector: (state: typeof mocks.resourceManagerState) => unknown) =>
    selector(mocks.resourceManagerState),
}));

// Real selectors: the query params under test (visibility narrowing, source
// defaults) are exactly what those selectors decide, so stubbing them would
// assert the stub.
vi.mock(import('@/features/ResourceManager/store/selectors'), async (importOriginal) => ({
  ...(await importOriginal()),
  sortFileList: (items: any) => items,
}));

vi.mock('@/store/file/slices/resource/hooks', () => ({
  useFetchResources: mocks.useFetchResources,
  useResourceStore: () => ({ resourceList: [] }),
}));

vi.mock('../KnowledgeBaseListProvider', () => ({
  KnowledgeBaseListProvider: ({ children }: { children: ReactNode }) => children,
}));

vi.mock('./EmptyPlaceholder', () => ({
  default: () => <div data-testid="empty" />,
}));

vi.mock('./Header', () => ({
  default: () => <div data-testid="header" />,
}));

vi.mock('./hooks/useResetSelectionOnQueryChange', () => ({
  useResetSelectionOnQueryChange: vi.fn(),
}));

vi.mock('./ListView', () => ({
  default: () => <div data-testid="list" />,
}));

vi.mock('./MasonryView', () => ({
  default: () => <div data-testid="masonry" />,
}));

vi.mock('./SearchResultsOverlay', () => ({
  default: () => null,
}));

vi.mock('./useCheckTaskStatus', () => ({
  useCheckTaskStatus: vi.fn(),
}));

describe('ResourceExplorer', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mocks.resourceManagerState.category = FilesTabs.All;
    mocks.resourceManagerState.libraryId = undefined;
    mocks.resourceManagerState.listVisibility = 'private';
    mocks.resourceManagerState.searchQuery = null;
    mocks.resourceManagerState.sorter = 'createdAt';
    mocks.resourceManagerState.sortType = SortType.Desc;
    mocks.resourceManagerState.sourceFilter = undefined;
    mocks.resourceManagerState.viewMode = 'list';
    mocks.useFetchResources.mockReturnValue({ isLoading: false, isValidating: false });
  });

  it('lists every uploaded file, library ones included, and no pages in the drive', () => {
    render(<ResourceExplorer />);

    expect(mocks.useFetchResources).toHaveBeenCalledWith(
      expect.objectContaining({
        category: FilesTabs.All,
        filesOnly: true,
        includeContentPreview: false,
        libraryId: undefined,
        parentId: null,
        showFilesInKnowledgeBase: true,
      }),
    );
  });

  it('requests a server-generated content preview only for masonry cards', () => {
    mocks.resourceManagerState.viewMode = 'masonry';

    render(<ResourceExplorer />);

    expect(mocks.useFetchResources).toHaveBeenCalledWith(
      expect.objectContaining({ includeContentPreview: true }),
    );
  });

  it('opens the images category on generated output without an explicit pick', () => {
    mocks.resourceManagerState.category = FilesTabs.Images;

    render(<ResourceExplorer />);

    expect(mocks.useFetchResources).toHaveBeenCalledWith(
      expect.objectContaining({
        category: FilesTabs.Images,
        sourceFilter: ResourceSourceFilter.Generated,
      }),
    );
  });

  it('sends the explicit source pick over the category default', () => {
    mocks.resourceManagerState.category = FilesTabs.Images;
    mocks.resourceManagerState.sourceFilter = ResourceSourceFilter.Uploaded;

    render(<ResourceExplorer />);

    expect(mocks.useFetchResources).toHaveBeenCalledWith(
      expect.objectContaining({ sourceFilter: ResourceSourceFilter.Uploaded }),
    );
  });

  it('does not reuse the resource-home visibility filter inside a library', () => {
    mocks.resourceManagerState.libraryId = 'kb-shared';
    mocks.resourceManagerState.listVisibility = 'private';

    render(<ResourceExplorer />);

    expect(mocks.useFetchResources).toHaveBeenCalledWith(
      expect.objectContaining({
        libraryId: 'kb-shared',
        visibility: undefined,
      }),
    );
  });

  it('lists the library own rows, pages and folders included, inside a library', () => {
    mocks.resourceManagerState.libraryId = 'kb-shared';

    render(<ResourceExplorer />);

    expect(mocks.useFetchResources).toHaveBeenCalledWith(
      expect.objectContaining({
        filesOnly: false,
        libraryId: 'kb-shared',
        showFilesInKnowledgeBase: false,
      }),
    );
  });
});