Skip to content

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

6.3 KB
Raw Blame
package service

import (
	"context"
	"strings"
	"testing"

	"ragflow/internal/common"
	"ragflow/internal/dao"
	"ragflow/internal/entity"
	taskpkg "ragflow/internal/ingestion/task"
	"ragflow/internal/ingestion/testutil"
)

func TestDefaultRunDocumentTask_BothPipelineAndParserMissing(t *testing.T) {
	db := testutil.SetupTestDB(t)
	cleanup := testutil.ReplaceDBForTest(t, db)
	defer cleanup()

	// Seed a document with empty parser_id so that neither pipeline_id
	// nor parser_id is configured — the only case that should still fail.
	_, kbID, docID, taskID := testutil.SeedTestData(t, db,
		testutil.WithTenantID("tenant-1"),
		testutil.WithKBID("kb-1"),
		testutil.WithDocID("doc-1"),
		testutil.WithTaskID("task-1"),
	)

	// Clear parser_id on the document so both identifiers are missing.
	if err := db.Model(&entity.Document{}).Where("id = ?", docID).Update("parser_id", "").Error; err != nil {
		t.Fatalf("clear parser_id: %v", err)
	}

	ingestor := newUnitIngestor("test", 1, []string{"pdf"})
	err := ingestor.defaultRunDocumentTask(context.Background(), &entity.IngestionTask{
		ID:         taskID,
		DocumentID: docID,
		DatasetID:  kbID,
		Status:     common.RUNNING,
	})
	if err == nil {
		t.Fatal("expected error when neither pipeline_id nor parser_id is configured")
	}
	msg := err.Error()
	if !strings.Contains(msg, "pipeline_id") && !strings.Contains(msg, "parser_id") {
		t.Fatalf("error should mention pipeline_id/parser_id: %v", err)
	}
}

// TestDefaultRunDocumentTask_ParserIDWithoutPipelineID proceeds via the
// builtin DSL path when only parser_id is configured. The execution will
// fail downstream (no storage engine in test), but the error must NOT be
// about missing pipeline_id.
func TestDefaultRunDocumentTask_ParserIDWithoutPipelineID(t *testing.T) {
	db := testutil.SetupTestDB(t)
	cleanup := testutil.ReplaceDBForTest(t, db)
	defer cleanup()

	// Seed with default ParserID="naive" and no PipelineID.
	_, kbID, docID, taskID := testutil.SeedTestData(t, db,
		testutil.WithTenantID("tenant-1"),
		testutil.WithKBID("kb-1"),
		testutil.WithDocID("doc-1"),
		testutil.WithTaskID("task-1"),
	)

	ingestor := newUnitIngestor("test", 1, []string{"pdf"})
	err := ingestor.defaultRunDocumentTask(context.Background(), &entity.IngestionTask{
		ID:         taskID,
		DocumentID: docID,
		DatasetID:  kbID,
		Status:     common.RUNNING,
	})
	// The builtin path resolves naive->general from the embedded registry
	// and proceeds to execute. It will fail because there is no storage
	// engine available in this test — but it must NOT fail with a
	// "no pipeline_id" error.
	if err == nil {
		t.Fatal("expected downstream error (no storage engine)")
	}
	msg := err.Error()
	if strings.Contains(msg, "no pipeline_id") {
		t.Fatalf("builtin path must not fail with missing pipeline_id: %v", err)
	}
}

func TestExecuteTask_RunsDocumentTask(t *testing.T) {
	db := testutil.SetupTestDB(t)
	cleanup := testutil.ReplaceDBForTest(t, db)
	defer cleanup()

	_, _, docID, taskID := testutil.SeedTestData(t, db,
		testutil.WithPipelineID("flow-1"),
		testutil.WithTenantID("tenant-1"),
	)
	ingestor := newUnitIngestor("test", 1, []string{"pdf"})
	var runDocumentTaskCalled bool
	var gotTaskID string
	var gotProgress []float64
	var gotMsgs []string
	ingestor.runDocumentTask = func(ctx context.Context, ingestionTask *entity.IngestionTask) error {
		runDocumentTaskCalled = true
		gotTaskID = ingestionTask.ID
		wrapped := func(prog float64, msg string) {
			gotProgress = append(gotProgress, prog*100)
			gotMsgs = append(gotMsgs, msg)
		}
		wrapped(0.82, "mock pipeline start")
		wrapped(1.0, "mock pipeline done")
		return nil
	}

	taskCtx := taskpkg.NewTaskContextForScheduling(
		context.Background(),
		&entity.IngestionTask{ID: taskID, DocumentID: docID, DatasetID: "kb-1", Status: common.RUNNING},
	)

	ctx := t.Context()
	ingestor.executeTask(ctx, taskCtx)

	if !runDocumentTaskCalled {
		t.Fatal("expected executeTask to run runDocumentTask")
	}
	if gotTaskID != taskID {
		t.Fatalf("runDocumentTask got task ID %q, want %q", gotTaskID, taskID)
	}
	finalTask, err := dao.NewIngestionTaskDAO().GetByID(ctx, db, taskID)
	if err != nil {
		t.Fatalf("load final ingestion task: %v", err)
	}
	if finalTask.Status != common.COMPLETED {
		t.Fatalf("final status = %s, want %s", finalTask.Status, common.COMPLETED)
	}
	if len(gotProgress) != 2 || gotProgress[0] != 82 || gotProgress[1] != 100 {
		t.Fatalf("gotProgress = %v, want [82 100]", gotProgress)
	}
	if len(gotMsgs) != 2 || gotMsgs[1] != "mock pipeline done" {
		t.Fatalf("gotMsgs = %v, want final message %q", gotMsgs, "mock pipeline done")
	}
}

// TestExecuteTask_CancelBeforePipeline verifies that when cancelCheck returns
// true at task start, the task is cancelled before the pipeline runs and the
// legacy document progress message remains untouched.
func TestExecuteTask_CancelBeforePipeline(t *testing.T) {
	db := testutil.SetupTestDB(t)
	cleanup := testutil.ReplaceDBForTest(t, db)
	defer cleanup()
	_, _, docID, taskID := testutil.SeedTestData(t, db,
		testutil.WithPipelineID("flow-1"),
		testutil.WithTenantID("tenant-1"),
	)
	const progressMessage = "Queued before cancellation"
	if err := db.Model(&entity.Document{}).Where("id = ?", docID).Update("progress_msg", progressMessage).Error; err != nil {
		t.Fatalf("seed document progress message: %v", err)
	}

	ingestor := newUnitIngestor("test", 1, []string{"pdf"})
	ingestor.cancelCheck = func(ctx context.Context, taskID string) bool { return true }

	var runDocumentTaskCalled bool
	ingestor.runDocumentTask = func(ctx context.Context, ingestionTask *entity.IngestionTask) error {
		runDocumentTaskCalled = true
		return nil
	}

	taskCtx := taskpkg.NewTaskContextForScheduling(
		context.Background(),
		&entity.IngestionTask{ID: taskID, DocumentID: docID, DatasetID: "kb-1", Status: common.RUNNING},
	)
	ctx := t.Context()
	ingestor.executeTask(ctx, taskCtx)

	if runDocumentTaskCalled {
		t.Fatal("expected runDocumentTask to NOT be called when cancel is detected before pipeline")
	}

	doc, err := dao.NewDocumentDAO().GetByID(ctx, db, docID)
	if err != nil {
		t.Fatalf("load document: %v", err)
	}
	if doc.Progress != -1 {
		t.Fatalf("document.progress = %v, want -1 (cancelled)", doc.Progress)
	}
	if doc.ProgressMsg == nil || *doc.ProgressMsg != progressMessage {
		t.Fatalf("document.progress_msg = %v, want preserved %q", doc.ProgressMsg, progressMessage)
	}
}