Skip to content

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

3.4 KB
Raw Blame
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react';
import React from 'react';
import { GenerateType, TraceType } from '@/constants/knowledge';

import { useTraceRunData } from '../use-dataset-generate';

jest.mock('react-router', () => ({
  useParams: jest.fn(() => ({ id: 'kb1' })),
}));

jest.mock('@/services/knowledge-service', () => ({
  getDatasetCompilationStatus: jest.fn(),
}));

// use-dataset-generate imports agent-service (and transitively register-server /
// next-request / locales config that touch import.meta.env). Mock it so the
// status path under test doesn't pull in that module graph.
jest.mock('@/services/agent-service', () => ({
  __esModule: true,
  default: { cancelDataflow: jest.fn(), deletePipelineTask: jest.fn() },
}));

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

import { getDatasetCompilationStatus } from '@/services/knowledge-service';

const mockStatus = jest.mocked(getDatasetCompilationStatus);

function makeWrapper() {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: { retry: false },
    },
  });
  // esbuild-jest config here loads .ts with the "tsx" loader but .tsx with the
  // plain "ts" loader, so JSX in this test file would not transform. Build the
  // provider element with createElement instead.
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const Wrapper = (props: { children: any }) =>
    React.createElement(
      QueryClientProvider,
      { client: queryClient },
      props.children,
    );
  return Wrapper;
}

describe('useTraceRunData (compile-status contract)', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('maps a successful status to the scheduler contract fields', async () => {
    mockStatus.mockResolvedValue({
      data: {
        code: 0,
        data: { state: 'running', inflight: 2, backlog: 1, error: '' },
      },
    } as never);

    const { result } = renderHook(
      () => useTraceRunData(GenerateType.Artifact),
      {
        wrapper: makeWrapper(),
      },
    );

    await waitFor(() => expect(result.current.isPending).toBe(false), {
      timeout: 5000,
    });

    const info = result.current.data;
    expect(info?.compilationState).toBe('running');
    expect(info?.inflight).toBe(2);
    expect(info?.backlog).toBe(1);
    expect(info?.compilationError).toBe('');
    // The trace type doubles as the backend `kind` alias, so each view's
    // status request is scoped to its own compile type.
    expect(mockStatus).toHaveBeenCalledWith('kb1', TraceType.Artifact);
  });

  it('rejects a non-zero business code instead of mapping to idle', async () => {
    mockStatus.mockResolvedValue({
      data: { code: 1, message: 'no authorization', data: undefined },
    } as never);

    const { result } = renderHook(
      () => useTraceRunData(GenerateType.Artifact),
      {
        wrapper: makeWrapper(),
      },
    );

    // The query configures retry: 3 with a 1s delay, so the terminal error state
    // arrives after the retries drain.
    await waitFor(() => expect(result.current.isError).toBe(true), {
      timeout: 10000,
    });

    expect(result.current.error).toBeTruthy();
    // The error must not be silently treated as an idle/empty status.
    expect(result.current.data).toBeUndefined();
  });
});