Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

4.6 KB
Raw Blame
import { renderHook, act } from '@testing-library/react';

jest.mock('react-i18next', () => ({
  useTranslation: () => ({ t: (key: string) => key }),
}));

jest.mock('@/hooks/use-pipeline-operator', () => ({
  useActiveTab: () => ({ activeTab: '', setActiveTab: jest.fn() }),
  usePipelineOperatorNodes: () => ({ operatorNodes: [], loading: false }),
  useResetParserConfigOnPipelineChange: () => {},
}));

// The hook now fetches the canvas version list itself and derives the latest
// id reactively, so we mock the fetch and drive it via a mutable array.
let mockVersions: Array<{ id: string; version?: number; name?: string }> = [];
jest.mock('@/hooks/use-pipeline-version-request', () => ({
  useFetchPipelineVersionList: () => ({
    data: mockVersions,
    loading: false,
    isError: false,
  }),
  useFetchPipelineVersionCatalog: () => ({
    data: [],
    loading: false,
    isError: false,
  }),
}));
jest.mock('@/hooks/use-agent-request', () => ({
  useFetchBuiltinPipelines: () => ({ options: [], loading: false }),
}));

// Cut the chain parser-config-validation -> compilation-form -> locales/config
// (which calls i18n.use(initReactI18next) and would crash under the mock).
jest.mock(
  '@/components/pipeline-operator-tabs/parser-config-validation',
  () => ({
    addParserConfigIssues: jest.fn(),
  }),
);

import { useDocumentPipelineForm } from './use-document-pipeline-form';

function renderForm(props: any) {
  return renderHook((p: any) => useDocumentPipelineForm(p), {
    initialProps: props,
  });
}

describe('useDocumentPipelineForm pipeline_version_id default (design S8: never show Latest version)', () => {
  beforeEach(() => {
    mockVersions = [];
  });

  it('adopts the latest concrete version for an unpinned document (R1a)', () => {
    mockVersions = [{ id: 'v2', version: 2, name: 'v2' }];
    const { result } = renderForm({
      parserId: 'naive',
      pipelineId: 'canvas-1',
      pipelineVersionId: undefined,
    });
    expect(result.current.form.getValues('pipeline_version_id')).toBe('v2');
  });

  it('keeps an explicitly pinned version instead of overriding to latest (R1b)', () => {
    mockVersions = [{ id: 'v2', version: 2, name: 'v2' }];
    const { result } = renderForm({
      parserId: 'naive',
      pipelineId: 'canvas-1',
      pipelineVersionId: 'v1',
    });
    expect(result.current.form.getValues('pipeline_version_id')).toBe('v1');
  });

  it('stays empty when there is no version list at all (R1c)', () => {
    mockVersions = [];
    const { result } = renderForm({
      parserId: 'naive',
      pipelineId: undefined,
      pipelineVersionId: undefined,
    });
    expect(result.current.form.getValues('pipeline_version_id')).toBe('');
  });

  it('adopts the latest version once the list loads after mount (R1d, async)', () => {
    mockVersions = [];
    const { result, rerender } = renderForm({
      parserId: 'naive',
      pipelineId: 'canvas-1',
      pipelineVersionId: undefined,
    });
    expect(result.current.form.getValues('pipeline_version_id')).toBe('');
    mockVersions = [{ id: 'v3', version: 3, name: 'v3' }];
    act(() => {
      rerender({
        parserId: 'naive',
        pipelineId: 'canvas-1',
        pipelineVersionId: undefined,
      });
    });
    expect(result.current.form.getValues('pipeline_version_id')).toBe('v3');
  });
});

describe('useDocumentPipelineForm versionOutdated', () => {
  beforeEach(() => {
    mockVersions = [];
  });

  it('is true when the pinned version differs from the latest (Pipeline mode)', () => {
    mockVersions = [{ id: 'v2', version: 2, name: 'v2' }];
    const { result } = renderForm({
      parserId: 'naive',
      pipelineId: 'canvas-1',
      pipelineVersionId: 'v1',
    });
    expect(result.current.versionOutdated).toBe(true);
  });

  it('is false when the pinned version is the latest (Pipeline mode)', () => {
    mockVersions = [{ id: 'v2', version: 2, name: 'v2' }];
    const { result } = renderForm({
      parserId: 'naive',
      pipelineId: 'canvas-1',
      pipelineVersionId: 'v2',
    });
    expect(result.current.versionOutdated).toBe(false);
  });

  it('is false in BuiltIn mode even when a stale pin survives in the form', () => {
    // A pipeline→builtin switch clears pipeline_id but the backend keeps
    // pipeline_version_id, so a reopened dialog seeds the stale pin; the bang
    // must not show beside a builtin parser.
    mockVersions = [{ id: 'v2', version: 2, name: 'v2' }];
    const { result } = renderForm({
      parserId: 'naive',
      pipelineId: undefined,
      pipelineVersionId: 'v1',
    });
    expect(result.current.form.getValues('pipeline_version_id')).toBe('v1');
    expect(result.current.versionOutdated).toBe(false);
  });
});