Files
ragflow/internal/service/chunk/chunk_test.go
Jack 87b8062df4 feat: implement POST /api/v1/searchbots/ask — streaming RAG with citations and think-tag processing (#15825)
Implements POST /api/v1/searchbots/ask in Go with streaming SSE,
citations, and think-tag processing. 23 files, 90+ unit tests.

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-09 22:48:50 +08:00

63 lines
2.0 KiB
Go

package chunk
import (
"context"
"ragflow/internal/common"
"reflect"
"testing"
)
func TestIsZeroVector(t *testing.T) {
if !common.IsZeroVector([]float64{0, 0, 0}) {
t.Error("all zeros should be true")
}
if common.IsZeroVector([]float64{0, 1, 0}) {
t.Error("non-zero should be false")
}
if !common.IsZeroVector([]float64{}) {
t.Error("empty should be true (treated as zero)")
}
if !common.IsZeroVector(nil) {
t.Error("nil should be true")
}
}
func TestHydrateChunkVectors_AllNonZero(t *testing.T) {
chunks := []map[string]interface{}{
{"id": "c1", "vector": []float64{1, 2, 3}},
{"id": "c2", "vector": []float64{4, 5, 6}},
}
// No zero vectors → nothing to hydrate.
hydrateChunkVectors(context.Background(), nil, chunks, nil, nil)
if !reflect.DeepEqual(chunks[0]["vector"], []float64{1, 2, 3}) {
t.Error("non-zero vector should not be changed")
}
if !reflect.DeepEqual(chunks[1]["vector"], []float64{4, 5, 6}) {
t.Error("non-zero vector should not be changed")
}
}
func TestHydrateChunkVectors_EmptyChunks(t *testing.T) {
// Should not panic on empty or nil.
hydrateChunkVectors(context.Background(), nil, nil, nil, nil)
hydrateChunkVectors(context.Background(), nil, []map[string]interface{}{}, nil, nil)
}
func TestHydrateChunkVectors_MissingIDs(t *testing.T) {
chunks := []map[string]interface{}{
{"vector": []float64{1.0}}, // no id — skipped
}
hydrateChunkVectors(context.Background(), nil, chunks, nil, nil)
// Should not change anything when engine is nil (FetchChunkVectors returns zero vectors).
// The function doesn't panic — it just can't hydrate because dim is 0.
// With nil engine, FetchChunkVectors returns zero vectors, so the zero stays zero.
}
func TestHydrateChunkVectors_NoDim(t *testing.T) {
chunks := []map[string]interface{}{
{"id": "c1", "vector": []float64{}},
}
hydrateChunkVectors(context.Background(), nil, chunks, []string{"kb1"}, []string{"t1"})
// Empty vectors have dim=0 → early return. No crash.
}