Files
ragflow/internal/agent/component/variable_aggregator_test.go

215 lines
6.0 KiB
Go
Raw Permalink Normal View History

feat(agent): Go port — canvas engine, 22 components, DSL v2, 13 endpoints (#15952) Ports the agent canvas subsystem from Python to Go. ## What's included ### Canvas Engine (Phase 0/1) - State engine, scheduler, variable resolver, Redis checkpoint store, cancel protocol - **209 tests** across canvas / component / io packages ### 22 Components (P0–P4) | Tier | Components | |---|---| | P0 T1+T2+T3 | LLM, Agent, ExitLoop, Switch, Categorize, Begin, Message, Invoke | | P1 T3 | VariableAggregator, VariableAssigner, StringTransform, ListOperations, DataOperations | | P2 T3 | Iteration, IterationItem, Loop, LoopItem | | P3 T3 | UserFillUp, Fillup | | P4 T5 | Browser, ExcelProcessor, DocsGenerator | ### DSL v2 Schema (Phase 2.5) - Typed v2 in-memory model with v1-to-v2 auto-detect converter - v1 legacy field stripping per plan §2.11.7 ### HTTP Endpoints & Bug Fixes (Plans PR1–PR3) - **DELETE SQL bug fix**: gorm v2 `Where("id = ?", id).Delete(...)` pattern - **CreateAgent validation**: title/DSL required, duplicate check, 103 envelope - **13 new endpoints**: templates, prompts, tags, sessions CRUD, chat/completions (SSE + non-stream stubs), rerun, test_db_connection, logs, webhook/logs - **756 Go unit tests** (745 → 756, +18) - **17 → 0 Python integration test failures** (test_agents.py + test_session_management/) ### Tools 21 eino tools: HTTPHelper, search tools, financial/data tools, mandatory stubs ### Infrastructure OTel observability, NATS message queue, DeepDoc gRPC client, SSRF guards, IDOR mitigation
2026-06-12 22:58:28 +08:00
//
// 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 component
import (
"context"
"testing"
"ragflow/internal/agent/canvas"
)
// TestVariableAggregator_FirstNonEmpty: 3 groups, 2 selectors each,
// first selector's ref is unset, second has a value → second wins.
func TestVariableAggregator_FirstNonEmpty(t *testing.T) {
state := canvas.NewCanvasState("run-1", "task-1")
state.Outputs["cpn_0"] = map[string]any{}
state.Outputs["cpn_1"] = map[string]any{"y": "second-a"}
state.Outputs["cpn_2"] = map[string]any{"y": "second-b"}
state.Outputs["cpn_3"] = map[string]any{"y": "second-c"}
ctx := canvas.WithState(context.Background(), state)
groups := []map[string]any{
{
"group_name": "group_a",
"variables": []any{
map[string]any{"value": "cpn_0@missing"},
map[string]any{"value": "cpn_1@y"},
},
},
{
"group_name": "group_b",
"variables": []any{
map[string]any{"value": "cpn_0@missing"},
map[string]any{"value": "cpn_2@y"},
},
},
{
"group_name": "group_c",
"variables": []any{
map[string]any{"value": "cpn_0@missing"},
map[string]any{"value": "cpn_3@y"},
},
},
}
c, err := NewVariableAggregatorComponent(map[string]any{"groups": groups})
if err != nil {
t.Fatalf("NewVariableAggregatorComponent: %v", err)
}
out, err := c.Invoke(ctx, nil)
if err != nil {
t.Fatalf("Invoke: %v", err)
}
if got, want := out["group_a"], "second-a"; got != want {
t.Errorf("group_a: got %v, want %v", got, want)
}
if got, want := out["group_b"], "second-b"; got != want {
t.Errorf("group_b: got %v, want %v", got, want)
}
if got, want := out["group_c"], "second-c"; got != want {
t.Errorf("group_c: got %v, want %v", got, want)
}
if len(out) != 3 {
t.Errorf("expected 3 output keys, got %d: %v", len(out), out)
}
}
// TestVariableAggregator_SkipsEmptyString: empty string is falsy in
// Python's bool() — the Go port must treat "" the same way.
func TestVariableAggregator_SkipsEmptyString(t *testing.T) {
state := canvas.NewCanvasState("run-2", "task-2")
state.Outputs["cpn_0"] = map[string]any{"x": ""}
state.Outputs["cpn_1"] = map[string]any{"y": "picked"}
ctx := canvas.WithState(context.Background(), state)
groups := []map[string]any{
{
"group_name": "out",
"variables": []any{
map[string]any{"value": "cpn_0@x"},
map[string]any{"value": "cpn_1@y"},
},
},
}
c, err := NewVariableAggregatorComponent(map[string]any{"groups": groups})
if err != nil {
t.Fatalf("NewVariableAggregatorComponent: %v", err)
}
out, err := c.Invoke(ctx, nil)
if err != nil {
t.Fatalf("Invoke: %v", err)
}
if got, want := out["out"], "picked"; got != want {
t.Errorf("out: got %v, want %v", got, want)
}
}
// TestVariableAggregator_MultipleGroups: 3 groups, each picks its own
// first-non-empty from independent state namespaces.
func TestVariableAggregator_MultipleGroups(t *testing.T) {
state := canvas.NewCanvasState("run-3", "task-3")
state.Sys["a"] = "alpha"
state.Sys["b"] = ""
state.Env["c"] = "gamma"
ctx := canvas.WithState(context.Background(), state)
groups := []map[string]any{
{
"group_name": "g1",
"variables": []any{
map[string]any{"value": "sys.a"},
},
},
{
"group_name": "g2",
"variables": []any{
map[string]any{"value": "sys.b"},
map[string]any{"value": "env.c"},
},
},
{
"group_name": "g3",
"variables": []any{
map[string]any{"value": "sys.d"}, // missing
map[string]any{"value": "env.c"},
},
},
}
c, err := NewVariableAggregatorComponent(map[string]any{"groups": groups})
if err != nil {
t.Fatalf("NewVariableAggregatorComponent: %v", err)
}
out, err := c.Invoke(ctx, nil)
if err != nil {
t.Fatalf("Invoke: %v", err)
}
if got, want := out["g1"], "alpha"; got != want {
t.Errorf("g1: got %v, want %v", got, want)
}
if got, want := out["g2"], "gamma"; got != want {
t.Errorf("g2: got %v, want %v", got, want)
}
if got, want := out["g3"], "gamma"; got != want {
t.Errorf("g3: got %v, want %v", got, want)
}
}
// TestVariableAggregator_AllEmpty: no group picks a value → no output
// keys. The component must not error or panic.
func TestVariableAggregator_AllEmpty(t *testing.T) {
state := canvas.NewCanvasState("run-4", "task-4")
state.Outputs["cpn_0"] = map[string]any{}
ctx := canvas.WithState(context.Background(), state)
groups := []map[string]any{
{
"group_name": "g1",
"variables": []any{
map[string]any{"value": "cpn_0@missing"},
},
},
}
c, err := NewVariableAggregatorComponent(map[string]any{"groups": groups})
if err != nil {
t.Fatalf("NewVariableAggregatorComponent: %v", err)
}
out, err := c.Invoke(ctx, nil)
if err != nil {
t.Fatalf("Invoke: %v", err)
}
if len(out) != 0 {
t.Errorf("expected empty outputs, got %v", out)
}
}
// TestVariableAggregator_ParamCheck: empty groups list must be rejected
// at construction time.
func TestVariableAggregator_ParamCheck(t *testing.T) {
_, err := NewVariableAggregatorComponent(map[string]any{"groups": []any{}})
if err == nil {
t.Fatal("expected error for empty groups, got nil")
}
}
// TestVariableAggregator_Registered: factory lookup via the registry.
func TestVariableAggregator_Registered(t *testing.T) {
c, err := New("VariableAggregator", map[string]any{
"groups": []map[string]any{
{
"group_name": "g",
"variables": []any{map[string]any{"value": "sys.x"}},
},
},
})
if err != nil {
t.Fatalf("registry lookup: %v", err)
}
if c.Name() != "VariableAggregator" {
t.Errorf("Name()=%q, want VariableAggregator", c.Name())
}
}