Skip to content

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

5.0 KB
Raw Blame
//
//  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

package globals

import (
	"context"
	"testing"

	"ragflow/internal/agent/runtime"
)

// withChunkCap attaches a CanvasState carrying debug_chunk_cap to ctx.
func withChunkCap(t *testing.T, cap int) context.Context {
	t.Helper()
	st := &runtime.CanvasState{Globals: map[string]any{DebugChunkCapKey: cap}}
	return runtime.WithState(context.Background(), st)
}

// TestDebugChunkCap_DefaultZero asserts the cap reads 0 when no state is
// attached (headless unit tests) or when the key is absent — 0 means "no cap".
func TestDebugChunkCap_DefaultZero(t *testing.T) {
	if got := DebugChunkCap(context.Background()); got != 0 {
		t.Errorf("DebugChunkCap(nil ctx) = %d, want 0", got)
	}
	st := &runtime.CanvasState{Globals: map[string]any{}}
	if got := DebugChunkCap(runtime.WithState(context.Background(), st)); got != 0 {
		t.Errorf("DebugChunkCap(empty globals) = %d, want 0", got)
	}
}

// TestDebugChunkCap_ReadsFromGlobals asserts the cap is read from
// CanvasState.Globals, in all numeric shapes a run input may carry (int from a
// Go-side default, float64 from a JSON-decoded override).
func TestDebugChunkCap_ReadsFromGlobals(t *testing.T) {
	cases := []struct {
		name string
		cap  any
		want int
	}{
		{"int default", 3, 3},
		{"int64", int64(5), 5},
		{"float64 from json", float64(7), 7},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			ctx := runtime.WithState(context.Background(), &runtime.CanvasState{
				Globals: map[string]any{DebugChunkCapKey: c.cap},
			})
			if got := DebugChunkCap(ctx); got != c.want {
				t.Errorf("DebugChunkCap = %d, want %d", got, c.want)
			}
		})
	}
}

// TestTaskID_EmptyWithoutState asserts TaskID degrades to "" when no
// CanvasState is attached (headless unit tests) or the key was never seeded.
// Callers use "" as "no task scope" and must skip per-task bookkeeping.
func TestTaskID_EmptyWithoutState(t *testing.T) {
	if got := TaskID(context.Background()); got != "" {
		t.Errorf("TaskID(bare ctx) = %q, want \"\"", got)
	}
	ctx := runtime.WithState(context.Background(), &runtime.CanvasState{Globals: map[string]any{}})
	if got := TaskID(ctx); got != "" {
		t.Errorf("TaskID(empty globals) = %q, want \"\"", got)
	}
}

// TestSetTaskID_RoundTrips asserts the pipeline-side seeder and the
// component-side reader agree on the storage slot.
func TestSetTaskID_RoundTrips(t *testing.T) {
	st := &runtime.CanvasState{Globals: map[string]any{}}
	ctx := runtime.WithState(context.Background(), st)
	SetTaskID(ctx, "task-42")
	if got := TaskID(ctx); got != "task-42" {
		t.Errorf("TaskID = %q, want \"task-42\"", got)
	}
}

// TestSetTaskID_NoStateIsNoop asserts seeding without a CanvasState does not
// panic — headless component tests invoke stages with a bare context.
func TestSetTaskID_NoStateIsNoop(t *testing.T) {
	SetTaskID(context.Background(), "task-42")
}

// TestTaskIDKey_NotAWhitelistedMetadataKey asserts the task id is seeded
// explicitly by the pipeline and is NOT part of GlobalMetadataKeys. Being on
// that whitelist would let any component output (or run input) carrying a
// "task_id" field overwrite the run's real task id, which would silently
// redirect per-task cache bookkeeping to the wrong task.
func TestTaskIDKey_NotAWhitelistedMetadataKey(t *testing.T) {
	for _, k := range GlobalMetadataKeys {
		if k == taskIDKey {
			t.Fatalf("GlobalMetadataKeys must not contain %q", taskIDKey)
		}
	}
	st := &runtime.CanvasState{Globals: map[string]any{}}
	ctx := runtime.WithState(context.Background(), st)
	SetTaskID(ctx, "task-real")
	SeedIngestionGlobals(ctx, map[string]any{taskIDKey: "task-spoofed"})
	if got := TaskID(ctx); got != "task-real" {
		t.Errorf("TaskID after hostile seed = %q, want \"task-real\"", got)
	}
}

// TestSeedIngestionGlobals_PicksUpDebugChunkCap asserts that adding
// DebugChunkCapKey to GlobalMetadataKeys makes SeedIngestionGlobals copy the
// run-input value into CanvasState.Globals — the link the executor relies on
// to deliver the cap to the chunker decorator. Production inputs never set
// this key, so persist runs are unaffected.
func TestSeedIngestionGlobals_PicksUpDebugChunkCap(t *testing.T) {
	st := &runtime.CanvasState{Globals: map[string]any{}}
	ctx := runtime.WithState(context.Background(), st)
	SeedIngestionGlobals(ctx, map[string]any{
		"name":           "doc.pdf",
		DebugChunkCapKey: 3,
	})
	if got, ok := st.GetGlobal(DebugChunkCapKey); !ok || got != 3 {
		t.Errorf("after SeedIngestionGlobals, globals[%q] = %v (ok=%v), want 3", DebugChunkCapKey, got, ok)
	}
}