Skip to content

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

8.2 KB
Raw Blame
package common

import (
	"context"
	"encoding/json"
	"errors"
	"strings"
	"testing"
	"time"

	"ragflow/internal/agent/runtime"
)

func TestRetryFailureReporterCoalescesRepeatedErrors(t *testing.T) {
	reporter := RetryFailureReporter{}
	err := errors.New("429 rate limit")

	if message, ok := reporter.FailureMessage(1, 6, time.Second, err, false); !ok || !strings.HasPrefix(message, "[WARN] LLM call failed (attempt 1/6)") {
		t.Fatalf("first failure = (%q, %v), want an emitted attempt message", message, ok)
	}
	if message, ok := reporter.FailureMessage(2, 6, 2*time.Second, err, false); ok || message != "" {
		t.Fatalf("second repeated failure = (%q, %v), want suppressed", message, ok)
	}
	if message, ok := reporter.FailureMessage(3, 6, 4*time.Second, err, false); ok || message != "" {
		t.Fatalf("third repeated failure = (%q, %v), want suppressed", message, ok)
	}
	if message, ok := reporter.FailureMessage(4, 6, 8*time.Second, err, false); !ok || !strings.HasPrefix(message, "[WARN] LLM call failed: 3 repeated failures") {
		t.Fatalf("fourth failure = (%q, %v), want a coalesced retry update", message, ok)
	}
	if message, ok := reporter.RecoveryMessage(5); !ok || !strings.Contains(message, "recovered after 4 failures") {
		t.Fatalf("recovery = (%q, %v), want recovery summary", message, ok)
	}
}

type captureChat struct{ req ChatRequest }

func (c *captureChat) Chat(_ context.Context, req ChatRequest) (*ChatResponse, error) {
	c.req = req
	return &ChatResponse{Content: `{"ok":true}`}, nil
}

func TestGenJSONDisablesInvokerRetry(t *testing.T) {
	chat := &captureChat{}
	if _, err := GenJSON(context.Background(), chat, ChatRequest{UserPrompt: "test"}, 0); err != nil {
		t.Fatalf("GenJSON: %v", err)
	}
	if !chat.req.DisableRetry {
		t.Fatal("GenJSON must disable retries in the underlying ChatInvoker")
	}
}

func TestRepairJSONTextRepairsMalformedResponse(t *testing.T) {
	got, err := RepairJSONText("Here is the result: {id: 'root', children: [{id: 'child',},]}")
	if err != nil {
		t.Fatalf("RepairJSONText: %v", err)
	}
	var parsed map[string]any
	if err := json.Unmarshal([]byte(got), &parsed); err != nil {
		t.Fatalf("repaired JSON is invalid: %v", err)
	}
	if parsed["id"] != "root" {
		t.Errorf("repaired id = %v, want root", parsed["id"])
	}
}

type failingChat struct{ err error }

func (c failingChat) Chat(context.Context, ChatRequest) (*ChatResponse, error) {
	return nil, c.err
}

func TestGenJSONReportsLLMFailure(t *testing.T) {
	var messages []string
	ctx := runtime.WithProgressMessageCallback(t.Context(), func(component, message string) {
		if component != "Compiler" {
			t.Fatalf("component = %q, want Compiler", component)
		}
		messages = append(messages, message)
	})
	err := errors.New("API request failed with status 401:\ninvalid key")
	if _, gotErr := GenJSON(ctx, failingChat{err: err}, ChatRequest{}, 0); !errors.Is(gotErr, err) {
		t.Fatalf("GenJSON error = %v, want %v", gotErr, err)
	}
	if len(messages) != 1 {
		t.Fatalf("messages = %v, want one error message", messages)
	}
	if !strings.Contains(messages[0], "[ERROR] LLM call failed (attempt 1/1)") || strings.Contains(messages[0], "\n") {
		t.Fatalf("unexpected progress message: %q", messages[0])
	}
}

// TestGenJSONRetryDelayOverride verifies the ctx-based retry-delay override
// shortens the backoff so failure-path tests need not wait out the real ~60s
// budget. Without the override GenJSON sleeps ~62s per failed call.
func TestGenJSONRetryDelayOverride(t *testing.T) {
	ctx := WithRetryDelay(context.Background(), time.Millisecond)
	var messages []string
	ctx = runtime.WithProgressMessageCallback(ctx, func(_, message string) {
		messages = append(messages, message)
	})
	start := time.Now()
	_, err := GenJSON(ctx, failingChat{err: errors.New("llm down")}, ChatRequest{})
	if err == nil {
		t.Fatal("expected the transient LLM error to surface")
	}
	if time.Since(start) > 5*time.Second {
		t.Fatalf("override did not shorten retry backoff: %s", time.Since(start))
	}
	if len(messages) != 3 || !strings.HasPrefix(messages[0], "[WARN]") || !strings.HasPrefix(messages[1], "[WARN]") || !strings.HasPrefix(messages[2], "[ERROR] LLM call failed after 6 attempts") {
		t.Fatalf("unexpected retry progress messages: %v", messages)
	}
}

func TestCompactErrorRedactsCredentials(t *testing.T) {
	got := CompactError(errors.New(`status=401 api_key="sk-secret-value" password=topsecret`))
	if strings.Contains(got, "sk-secret-value") || strings.Contains(got, "topsecret") {
		t.Fatalf("CompactError leaked credentials: %q", got)
	}
	if !strings.Contains(got, "api_key=[REDACTED]") || !strings.Contains(got, "password=[REDACTED]") {
		t.Fatalf("CompactError did not redact credential fields: %q", got)
	}
}

func vec(a, b, c float32) []float32 { return []float32{a, b, c} }

func TestMemStoreTopK(t *testing.T) {
	m := NewMemStore()
	m.Add(Product{ID: "a", Vector: vec(1, 0, 0)})
	m.Add(Product{ID: "b", Vector: vec(0, 1, 0)})
	m.Add(Product{ID: "c", Vector: vec(0, 0, 1)})

	// query aligned with "a" → exact match
	hits := m.TopK(vec(1, 0, 0), 3, 0.0)
	if len(hits) != 3 {
		t.Fatalf("expected 3 hits, got %d", len(hits))
	}
	if hits[0].ID != "a" || hits[0].Score < 0.999 {
		t.Fatalf("expected top hit a with score ~1, got %s %.3f", hits[0].ID, hits[0].Score)
	}

	// threshold filters out orthogonal vectors
	hits = m.TopK(vec(1, 0, 0), 3, 0.5)
	if len(hits) != 1 || hits[0].ID != "a" {
		t.Fatalf("threshold=0.5 should keep only a, got %v", hits)
	}

	// k caps results
	hits = m.TopK(vec(0.6, 0.6, 0.6), 2, 0.0)
	if len(hits) != 2 {
		t.Fatalf("k=2 should cap to 2, got %d", len(hits))
	}
}

func TestMemStoreUpsertDelete(t *testing.T) {
	m := NewMemStore()
	m.Add(Product{ID: "x", Vector: vec(1, 0, 0)})
	m.Add(Product{ID: "y", Vector: vec(0, 1, 0)})
	m.Upsert(Product{ID: "x", Vector: vec(0, 0, 1)})
	if got := m.TopK(vec(0, 0, 1), 1, 0.99); len(got) != 1 || got[0].ID != "x" {
		t.Fatalf("upsert did not replace x vector: %v", got)
	}
	m.Delete("x")
	if m.Len() != 1 {
		t.Fatalf("delete failed, len=%d", m.Len())
	}
	if got := m.Snapshot(); len(got) != 1 || got[0].ID != "y" {
		t.Fatalf("snapshot wrong after delete: %v", got)
	}
}

func TestStableRowID(t *testing.T) {
	a := StableRowID("t1", "d1", "structure", "hello")
	b := StableRowID("t1", "d1", "structure", "hello")
	c := StableRowID("t1", "d1", "structure", "world")
	if a != b {
		t.Fatalf("same parts must yield same id: %s vs %s", a, b)
	}
	if a == c {
		t.Fatalf("different content must yield different id")
	}
	// part order must not collide
	if StableRowID("ab", "c") == StableRowID("a", "bc") {
		t.Fatalf("part-ordering collided")
	}
}

func TestLocalize(t *testing.T) {
	if got := Localize("plain", "zh"); got != "plain" {
		t.Fatalf("string passthrough: %q", got)
	}
	// Lists render as numbered lines (mirrors Python's _struct_localize).
	if got := Localize([]string{"en-only"}, "zh"); got != "1. en-only" {
		t.Fatalf("slice numbered join: %q", got)
	}
	if got := Localize([]any{"a", "b"}, "en"); got != "1. a\n2. b" {
		t.Fatalf("any-slice numbered join: %q", got)
	}
	m := map[string]string{"en": "E", "zh": "中"}
	if got := Localize(m, "zh"); got != "中" {
		t.Fatalf("map lang pick: %q", got)
	}
	if got := Localize(m, "fr"); got != "E" {
		t.Fatalf("map fallback en: %q", got)
	}
	// lang == "en" with no "en" key yields "" (no arbitrary-language fallback,
	// matching Python).
	if got := Localize(map[string]string{"zh": "中"}, "en"); got != "" {
		t.Fatalf("no arbitrary fallback: %q", got)
	}
	// Nested map values localize recursively (list under lang key).
	if got := Localize(map[string]any{"en": []any{"x", "y"}}, "zh"); got != "1. x\n2. y" {
		t.Fatalf("nested en fallback: %q", got)
	}
}

func TestPackBatches(t *testing.T) {
	chunks := []Chunk{
		{ID: "1", Text: "aaaa"},
		{ID: "2", Text: "bbbb"},
		{ID: "3", Text: "cccccccccccc"}, // oversized
		{ID: "4", Text: "dddd"},
	}
	// EstimateTokens ≈ len/4 → 1,1,3,1. budget 2 → [1,2],[3],[4]
	batches := PackBatches(chunks, 2, nil)
	if len(batches) != 3 {
		t.Fatalf("expected 3 batches, got %d: %v", len(batches), batches)
	}
	if len(batches[1]) != 1 || batches[1][0].ID != "3" {
		t.Fatalf("oversized chunk must be alone: %v", batches[1])
	}

	// zero budget → single batch
	if got := PackBatches(chunks, 0, nil); len(got) != 1 || len(got[0]) != 4 {
		t.Fatalf("zero budget should be one batch of all: %v", got)
	}
}