mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-01 08:15:44 +08:00
## Summary
Aligns the **Go agent runtime/canvas/components/tools** behavior with
the **Python `agent/` implementation** so the same stored canvas DSL
produces the same execution result on either side. Every component,
tool, and runtime primitive in `internal/agent/` is now driven by the
same semantics as its Python counterpart — variable resolution, template
substitution, control flow, error reporting, retry/cancel, and stream
event shapes.
The **retrieval component is the one explicit exception** in this PR. It
is being reworked in a separate change and is excluded from this
alignment pass; the wrapper slot (`universe_a_wrappers.go →
newRetrievalComponent`) is preserved.
## Scope of alignment
### Components (all aligned with `agent/component/`)
`Begin` · `Message` · `LLM` (incl. ChatTemplateKwargs,
MessageHistoryWindowSize, VisualFiles, Cite, OutputStructure,
JSONOutput, TopP, MaxRetries, DelayAfterError, credentials) · `Agent`
(react + tool artifact capture + `Reset()` interface-assert) · `Switch`
(12/12 operators, Python-equivalent semantics) · `Categorize` · `Invoke`
· `Iteration` · `Loop` (macro-expansion through `workflowx.AddLoopNode`)
· `UserFillUp` (Python-equivalent interrupt/resume via eino
`compose.Interrupt`/`ResumeWithData`) · `FillUp` · `DataOperations` ·
`ListOperations` · `StringTransform` · `VariableAggregator` ·
`VariableAssigner` · `Browser` (full stagehand runtime parity) ·
`DocsGenerator` · `ExcelProcessor`.
### Tools (all aligned with `agent/tools/`)
`Retrieval` (wrapper slot only — logic out of scope) · `MCPToolAdapter`
(streamable-HTTP) · `CodeExec` (sandbox bridge with
`code_exec_contract.go` matching Python contract) · `AkShare` · `ArXiv`
· `Crawler` · `DeepL` · `DuckDuckGo` · `Email` · `ExeSQL` · `GitHub` ·
`Google` · `GoogleScholar` · `Jin10` · `PubMed` · `QWeather` · `SearXNG`
· `Tavily` · `Tushare` · `Wencai` · `Wikipedia` · `YahooFinance` —
uniform `eino tool.InvokableTool` interface, SSRF protection, shared
HTTP client.
### Canvas execution engine (`internal/agent/canvas/`)
Aligned with Python's `agent/canvas.py`:
- **Scheduler** (`scheduler.go`): state pre/post handlers, node lambdas,
per-component timeout resolver (4-level: per-class env → per-class table
→ uniform env → 600s fallback), `legacyNoOpNames`.
- **Loop subgraph** (`loop_subgraph.go`): Python-equivalent
`AddLoopNode` macro expansion + condition translation.
- **Multibranch** (`multibranch.go`): `Switch` / `Categorize` routing
via `compose.NewGraphMultiBranch` — same branch selection semantics as
Python.
- **Parallel subgraph** (`parallel_subgraph.go`): matches Python's
parallel fan-out contract.
- **Interrupt/Resume** (`interrupt_resume.go`): `UserFillUpNodeBody` /
`IsInterruptError` / `ExtractInterruptContexts` — replaces the
deprecated Python sentinel chain with eino's native interrupt API,
preserving the same external behavior.
- **Checkpoint** (`checkpoint_store.go`): `RedisCheckPointStore`
Get/Set/Delete, with business metadata (status / canvas_id /
parent_run_id) on a parallel Redis Hash.
- **RunTracker** (`run_tracker.go`): Start / MarkSucceeded / MarkFailed
/ MarkCancelled / AttachCheckpoint — same lifecycle as the Python run
record.
- **Cancel** (`cancel.go`): Redis pub/sub watch.
- **Stream** (`stream.go`): SSE channel with `messages` / `waiting` /
`errors` / `done` events, same shape as Python's `agent.canvas.RunEvent`
payload.
### DSL bridge (`internal/agent/dsl/`)
- `normalize.go`: v1↔v2 collapsed into a single wire format — Python and
Go consume the same stored JSON.
- `reset.go`: per-run state reset matches Python's `Canvas.reset()`
semantics.
- Testdata mirrors Python's `agent_msg.json` / `all.json` / etc.
### Runtime (`internal/agent/runtime/`)
- `CanvasState` / `NewCanvasState` / `GetVar` / `SetVar` / `ReadVars`:
same `{{cpn_id@param}}` resolution model.
- `ResolveTemplate` (regex fast path + gonja fallback) — Python
Jinja-style semantics.
- `selector.go`, `metrics.go`, `component.go`: shared runtime contracts.
## Out of scope (intentionally)
- **`Retrieval` component logic** — wrapped only; full parity lands in a
follow-up PR.
- **Frontend** — only minor dsl-bridge / canvas UX fixes ride along.
- **CLI / admin / model registry** — orthogonal to agent behavior.
## How alignment is verified
`internal/service/agent_run_e2e_test.go` exercises the **full production
chain** against real Python-shaped DSL fixtures:
```
loadCanvasForUser → versionDAO.GetLatest → decodeCanvasFromDSL →
canvas.Compile → cc.Workflow.Invoke → answer extraction
```
using in-memory SQLite + miniredis (no Docker). Covers:
- `TestRunAgent_RealCanvas_BeginMessage` — happy path, `{{sys.query}}`
resolution
- `TestRunAgent_RealCanvas_WaitForUserResume` — two-run resume cycle
(Python-equivalent)
- `TestRunAgent_RealCanvas_CompileFails` — unknown component name →
sanitized error (Python-equivalent)
- `TestRunAgent_RealCanvas_InvokeFails` — unresolvable template ref
(Python-equivalent)
- `TestRunAgent_RunTracker_AttachCheckpoint_CallSequence` —
Start→AttachCheckpoint→MarkSucceeded lifecycle
`internal/handler/agent_test.go` — SSE streaming parity (`Content-Type:
text/event-stream`, `data: {…}\n\n`, trailing `data: [DONE]\n\n`,
OpenAI-compatible non-stream `choices`).
`internal/agent/canvas/fixture_compile_test.go` + per-component tests
pin the Python-equivalent outputs.
```
go test -count=1 -v -run 'TestRunAgent_RealCanvas|TestRunAgent_RunTracker' ./internal/service/
```
## Design reference
`docs/develop/agent-go-port-design.md` (1329 lines, last cross-checked
2026-06-17) — module layout, per-component / per-tool inventory,
corner-case catalogue, and the actionable backlog (Section 14, including
the retrieval alignment follow-up).
---------
Co-authored-by: Claude <noreply@anthropic.com>
521 lines
17 KiB
Go
521 lines
17 KiB
Go
//
|
|
// 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"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"ragflow/internal/agent/canvas"
|
|
)
|
|
|
|
// TestListOperations_Head: [1,2,3,4,5] op=head n=3 → [1,2,3], first=1, last=3.
|
|
func TestListOperations_Head(t *testing.T) {
|
|
c, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "head",
|
|
"n": 3,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewListOperationsComponent: %v", err)
|
|
}
|
|
state := canvas.NewCanvasState("run-1", "task-1")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{1, 2, 3, 4, 5}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
out, err := c.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke: %v", err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
want := []any{1, 2, 3}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("result: got %v, want %v", got, want)
|
|
}
|
|
if got, want := out["first"], 1; got != want {
|
|
t.Errorf("first: got %v, want %v", got, want)
|
|
}
|
|
if got, want := out["last"], 3; got != want {
|
|
t.Errorf("last: got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_TopNLegacyAlias pins the legacy DSL alias used by
|
|
// all.json: operations=topN should behave like head with n items so old
|
|
// imported workflows keep compiling and running unchanged.
|
|
func TestListOperations_TopNLegacyAlias(t *testing.T) {
|
|
c, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "topN",
|
|
"n": 2,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewListOperationsComponent: %v", err)
|
|
}
|
|
state := canvas.NewCanvasState("run-topn", "task-topn")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{1, 2, 3, 4}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
out, err := c.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke: %v", err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
want := []any{1, 2}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("result: got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_Filter: items ["foo", "bar", "foobar"], op=filter,
|
|
// operator="contains", value="bar" → ["bar", "foobar"].
|
|
func TestListOperations_Filter(t *testing.T) {
|
|
c, _ := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "filter",
|
|
"filter": map[string]any{"operator": "contains", "value": "bar"},
|
|
})
|
|
state := canvas.NewCanvasState("run-2", "task-2")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{"foo", "bar", "foobar"}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
out, err := c.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke: %v", err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
want := []any{"bar", "foobar"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("filter: got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_DropDuplicates: [{"k":1},{"k":1},{"k":2}] → [{"k":1},{"k":2}].
|
|
func TestListOperations_DropDuplicates(t *testing.T) {
|
|
c, _ := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "drop_duplicates",
|
|
})
|
|
state := canvas.NewCanvasState("run-3", "task-3")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{
|
|
map[string]any{"k": 1},
|
|
map[string]any{"k": 1},
|
|
map[string]any{"k": 2},
|
|
}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
out, err := c.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke: %v", err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
if len(got) != 2 {
|
|
t.Fatalf("expected 2 elements, got %d: %v", len(got), got)
|
|
}
|
|
if !reflect.DeepEqual(got[0], map[string]any{"k": 1}) {
|
|
t.Errorf("got[0]: %v, want {k:1}", got[0])
|
|
}
|
|
if !reflect.DeepEqual(got[1], map[string]any{"k": 2}) {
|
|
t.Errorf("got[1]: %v, want {k:2}", got[1])
|
|
}
|
|
}
|
|
|
|
// TestListOperations_Tail: [1,2,3,4,5] op=tail n=2 → [4,5].
|
|
func TestListOperations_Tail(t *testing.T) {
|
|
c, _ := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "tail",
|
|
"n": 2,
|
|
})
|
|
state := canvas.NewCanvasState("run-4", "task-4")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{1, 2, 3, 4, 5}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
out, err := c.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke: %v", err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
want := []any{4, 5}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("tail: got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_NthPositive: [a,b,c,d] n=3 → [c].
|
|
func TestListOperations_NthPositive(t *testing.T) {
|
|
c, _ := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "nth",
|
|
"n": 3,
|
|
})
|
|
state := canvas.NewCanvasState("run-5", "task-5")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{"a", "b", "c", "d"}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
out, err := c.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke: %v", err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
want := []any{"c"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("nth: got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_SortDesc: numeric sort with sort_method=desc.
|
|
func TestListOperations_SortDesc(t *testing.T) {
|
|
c, _ := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "sort",
|
|
"sort_method": "desc",
|
|
})
|
|
state := canvas.NewCanvasState("run-6", "task-6")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{3, 1, 4, 1, 5, 9, 2, 6}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
out, err := c.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke: %v", err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
sortedAsc := append([]any{}, got...)
|
|
sort.Slice(sortedAsc, func(i, j int) bool {
|
|
af, _ := sortedAsc[i].(int)
|
|
bf, _ := sortedAsc[j].(int)
|
|
return af < bf
|
|
})
|
|
// Reverse for desc
|
|
for i, j := 0, len(sortedAsc)-1; i < j; i, j = i+1, j-1 {
|
|
sortedAsc[i], sortedAsc[j] = sortedAsc[j], sortedAsc[i]
|
|
}
|
|
if !reflect.DeepEqual(got, sortedAsc) {
|
|
t.Errorf("sort desc: got %v, want %v", got, sortedAsc)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_SortByFieldList: with sort_by="score" the sort
|
|
// key is the value of the "score" map key, not the full hashable
|
|
// tuple. desc + score picks the row with the highest score first
|
|
// regardless of id ordering. Empty sort_by falls back to the legacy
|
|
// hashableKey (alphabetically first key) so existing DSLs keep
|
|
// working.
|
|
func TestListOperations_SortByFieldList(t *testing.T) {
|
|
state := canvas.NewCanvasState("run-sort-by", "task-sort-by")
|
|
state.Outputs["cpn_0"] = map[string]any{
|
|
"rows": []any{
|
|
map[string]any{"id": 1, "score": 0.91, "title": "Alpha"},
|
|
map[string]any{"id": 2, "score": 0.88, "title": "Beta"},
|
|
map[string]any{"id": 3, "score": 0.76, "title": "Gamma"},
|
|
},
|
|
}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
// sort_by="score", desc → Alpha(0.91), Beta(0.88), Gamma(0.76)
|
|
cSortDesc, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@rows",
|
|
"operations": "sort",
|
|
"sort_method": "desc",
|
|
"sort_by": "score",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewListOperationsComponent: %v", err)
|
|
}
|
|
out, err := cSortDesc.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke sort_by=score desc: %v", err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
wantOrder := []any{
|
|
map[string]any{"id": 1, "score": 0.91, "title": "Alpha"},
|
|
map[string]any{"id": 2, "score": 0.88, "title": "Beta"},
|
|
map[string]any{"id": 3, "score": 0.76, "title": "Gamma"},
|
|
}
|
|
if !reflect.DeepEqual(got, wantOrder) {
|
|
t.Errorf("sort_by=score desc: got %v, want %v", got, wantOrder)
|
|
}
|
|
|
|
// sort_by="" — falls back to hashableKey (alphabetically first
|
|
// field = id). desc → Gamma(id:3), Beta(id:2), Alpha(id:1).
|
|
cLegacy, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@rows",
|
|
"operations": "sort",
|
|
"sort_method": "desc",
|
|
"sort_by": "",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewListOperationsComponent (legacy): %v", err)
|
|
}
|
|
out, err = cLegacy.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke sort_by=\"\" desc: %v", err)
|
|
}
|
|
got, _ = out["result"].([]any)
|
|
wantLegacy := []any{
|
|
map[string]any{"id": 3, "score": 0.76, "title": "Gamma"},
|
|
map[string]any{"id": 2, "score": 0.88, "title": "Beta"},
|
|
map[string]any{"id": 1, "score": 0.91, "title": "Alpha"},
|
|
}
|
|
if !reflect.DeepEqual(got, wantLegacy) {
|
|
t.Errorf("sort_by=\"\" desc: got %v, want %v", got, wantLegacy)
|
|
}
|
|
|
|
// sort_by="score,title" — primary score, tiebreak title.
|
|
cTiebreak, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@rows",
|
|
"operations": "sort",
|
|
"sort_method": "asc",
|
|
"sort_by": "score,title",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewListOperationsComponent (tiebreak): %v", err)
|
|
}
|
|
out, err = cTiebreak.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke sort_by=score,title asc: %v", err)
|
|
}
|
|
got, _ = out["result"].([]any)
|
|
wantTiebreak := []any{
|
|
map[string]any{"id": 3, "score": 0.76, "title": "Gamma"},
|
|
map[string]any{"id": 2, "score": 0.88, "title": "Beta"},
|
|
map[string]any{"id": 1, "score": 0.91, "title": "Alpha"},
|
|
}
|
|
if !reflect.DeepEqual(got, wantTiebreak) {
|
|
t.Errorf("sort_by=score,title asc: got %v, want %v", got, wantTiebreak)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_NotAList: returns a clear error.
|
|
func TestListOperations_NotAList(t *testing.T) {
|
|
c, _ := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@x",
|
|
"operations": "head",
|
|
"n": 1,
|
|
})
|
|
state := canvas.NewCanvasState("run-7", "task-7")
|
|
state.Outputs["cpn_0"] = map[string]any{"x": "not-a-list"}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
_, err := c.Invoke(ctx, nil)
|
|
if err == nil {
|
|
t.Fatal("expected error for non-list input, got nil")
|
|
}
|
|
}
|
|
|
|
// TestListOperations_ParamCheck: empty query rejected.
|
|
func TestListOperations_ParamCheck(t *testing.T) {
|
|
_, err := NewListOperationsComponent(map[string]any{
|
|
"query": "",
|
|
"operations": "head",
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error for empty query, got nil")
|
|
}
|
|
}
|
|
|
|
// TestListOperations_Registered: factory lookup.
|
|
func TestListOperations_Registered(t *testing.T) {
|
|
c, err := New("ListOperations", map[string]any{
|
|
"query": "sys.x",
|
|
"operations": "head",
|
|
"n": 1,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("registry lookup: %v", err)
|
|
}
|
|
if c.Name() != "ListOperations" {
|
|
t.Errorf("Name()=%q, want ListOperations", c.Name())
|
|
}
|
|
}
|
|
|
|
// TestListOperations_StrictMode_ReturnsError pins Change #2 (panic →
|
|
// recoverable error). Strict mode with n=0 must surface a *listOpPanic
|
|
// error from Invoke() (not a goroutine-level panic). The Python
|
|
// reference raises ValueError, which the canvas framework catches.
|
|
func TestListOperations_StrictMode_ReturnsError(t *testing.T) {
|
|
c, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "nth",
|
|
"n": 0,
|
|
"strict": true,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewListOperationsComponent: %v", err)
|
|
}
|
|
state := canvas.NewCanvasState("run-strict", "task-strict")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{1, 2, 3}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
_, err = c.Invoke(ctx, nil)
|
|
if err == nil {
|
|
t.Fatal("expected error from strict-mode nth with n=0, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "strict mode") {
|
|
t.Errorf("error %q should mention 'strict mode'", err)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_StrictStringCoercion pins Change #3: passing
|
|
// "strict" as a string ("true"/"1"/"yes"/"on") must be coerced to a
|
|
// true bool, matching Python's _is_strict accept-list.
|
|
func TestListOperations_StrictStringCoercion(t *testing.T) {
|
|
for _, v := range []string{"true", "TRUE", "True", "1", "yes", "on"} {
|
|
c, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "nth",
|
|
"n": 0,
|
|
"strict": v,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("[strict=%q] NewListOperationsComponent: %v", v, err)
|
|
}
|
|
state := canvas.NewCanvasState("run-str-"+v, "task-str-"+v)
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{1, 2, 3}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
_, err = c.Invoke(ctx, nil)
|
|
if err == nil {
|
|
t.Errorf("[strict=%q] expected error from strict-mode nth with n=0, got nil", v)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestListOperations_StrictFalseStringsIgnored pins the negative case
|
|
// for Change #3: strings other than the accept-list must coerce to
|
|
// false (no strict-mode error).
|
|
func TestListOperations_StrictFalseStringsIgnored(t *testing.T) {
|
|
for _, v := range []string{"false", "FALSE", "0", "no", "off", "random"} {
|
|
c, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "nth",
|
|
"n": 99, // out-of-range, but non-strict → empty
|
|
"strict": v,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("[strict=%q] NewListOperationsComponent: %v", v, err)
|
|
}
|
|
state := canvas.NewCanvasState("run-strf-"+v, "task-strf-"+v)
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{1, 2, 3}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
out, err := c.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Errorf("[strict=%q] expected non-error (coerced to false), got %v", v, err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
if len(got) != 0 {
|
|
t.Errorf("[strict=%q] expected empty result, got %v", v, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestListOperations_CoerceNBool pins Change #4: toInt must follow
|
|
// Python's int() semantics for booleans (true→1, false→0).
|
|
func TestListOperations_CoerceNBool(t *testing.T) {
|
|
if got := toInt(true); got != 1 {
|
|
t.Errorf("toInt(true) = %d, want 1", got)
|
|
}
|
|
if got := toInt(false); got != 0 {
|
|
t.Errorf("toInt(false) = %d, want 0", got)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_FilterEqBool pins Change #5: normValue must render
|
|
// Go's bool as Python's str(bool) ("True"/"False") so filter `=` matches
|
|
// the Python DSL contract.
|
|
func TestListOperations_FilterEqBool(t *testing.T) {
|
|
c, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
"operations": "filter",
|
|
"filter": map[string]any{"operator": "=", "value": "True"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewListOperationsComponent: %v", err)
|
|
}
|
|
state := canvas.NewCanvasState("run-feq", "task-feq")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{true, false, true, "True"}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
out, err := c.Invoke(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("Invoke: %v", err)
|
|
}
|
|
got, _ := out["result"].([]any)
|
|
// Both true values plus the "True" string should match.
|
|
if len(got) != 3 {
|
|
t.Errorf("expected 3 matches (true, true, 'True'), got %d: %v", len(got), got)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_UnknownOp_ReturnsError pins Change #1: the
|
|
// defensive default: branch in Invoke() must surface an explicit
|
|
// error for any operation name that bypasses the allowlist. We
|
|
// construct the struct directly (same package) to skip Check().
|
|
func TestListOperations_UnknownOp_ReturnsError(t *testing.T) {
|
|
c := &ListOperationsComponent{
|
|
name: "ListOperations",
|
|
param: listOperationsParam{
|
|
Query: "cpn_0@xs",
|
|
Operations: "bogus",
|
|
N: 1,
|
|
},
|
|
}
|
|
state := canvas.NewCanvasState("run-bogus", "task-bogus")
|
|
state.Outputs["cpn_0"] = map[string]any{"xs": []any{1, 2, 3}}
|
|
ctx := canvas.WithState(context.Background(), state)
|
|
|
|
_, err := c.Invoke(ctx, nil)
|
|
if err == nil {
|
|
t.Fatal("expected error for unknown operation, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "unsupported operation") {
|
|
t.Errorf("error %q should mention 'unsupported operation'", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "bogus") {
|
|
t.Errorf("error %q should mention the bad op name 'bogus'", err)
|
|
}
|
|
}
|
|
|
|
// TestListOperations_InputsDocMatchesAllowlist pins Change #6: the
|
|
// Inputs() docstring must not claim support for operations that are
|
|
// not in the Check() allowlist (slice/shuffle/take/reverse/deduplicate).
|
|
// A bug here misleads the editor dropdown and the agent developer.
|
|
func TestListOperations_InputsDocMatchesAllowlist(t *testing.T) {
|
|
c, err := NewListOperationsComponent(map[string]any{
|
|
"query": "cpn_0@xs",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewListOperationsComponent: %v", err)
|
|
}
|
|
doc := c.Inputs()
|
|
for _, banned := range []string{"slice", "shuffle", "reverse"} {
|
|
if strings.Contains(doc["operations"], banned) {
|
|
t.Errorf("Inputs()[operations] doc must not mention %q (not in allowlist): %q", banned, doc["operations"])
|
|
}
|
|
}
|
|
// The doc must mention the six operations that are actually supported.
|
|
for _, op := range []string{"nth", "head", "tail", "filter", "sort", "drop_duplicates"} {
|
|
if !strings.Contains(doc["operations"], op) {
|
|
t.Errorf("Inputs()[operations] doc must mention %q: %q", op, doc["operations"])
|
|
}
|
|
}
|
|
}
|