1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2026 Chris Olson
// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
//
// 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.
//! Port of Go workflow/walk_test.go.
use std::sync::Arc;
use gadk_agent::{Agent, AgentConfig, agent};
use pretty_assertions::assert_eq;
use crate::workflow::node::testing::test_node;
use crate::workflow::walk::walk_agents;
use crate::workflow::{
AgentNode, Edge, Node, NodeConfig, ParallelWorker, START, WorkflowNode, chain,
};
fn new_agent(name: &str) -> Arc<dyn Agent> {
agent::new(AgentConfig {
name: name.into(),
..Default::default()
})
.expect("agent::new")
}
fn agent_node(a: &Arc<dyn Agent>) -> Arc<dyn Node> {
AgentNode::new(Arc::clone(a), NodeConfig::default()).expect("AgentNode::new")
}
fn workflow_node(name: &str, edges: Vec<Edge>) -> Arc<dyn Node> {
WorkflowNode::new(name, edges).expect("WorkflowNode::new")
}
fn parallel_worker(wrapped: Arc<dyn Node>) -> Arc<dyn Node> {
ParallelWorker::new("fan_out", wrapped, 0, NodeConfig::default()).expect("ParallelWorker::new")
}
/// Names of the walked agents, sorted (the walk promises no order; Go compares with cmpopts.SortSlices).
fn walked_names(edges: &[Edge]) -> Vec<String> {
let mut names: Vec<String> = walk_agents(edges)
.iter()
.map(|a| a.name().to_owned())
.collect();
names.sort();
names
}
// parity: adapted: the "nodes that run no agent" case uses a no-op test node instead of NewFunctionNode
#[test]
fn test_walk_agents() {
let s = Arc::clone(&START);
let cases: Vec<(&str, Vec<Edge>, Vec<&str>)> = vec![
(
"agent nodes on the edges",
chain(&[
Arc::clone(&s),
agent_node(&new_agent("first")),
agent_node(&new_agent("second")),
]),
vec!["first", "second"],
),
(
"a node on two edges is visited once",
{
let (middle, last) = (
agent_node(&new_agent("middle")),
agent_node(&new_agent("last")),
);
vec![
Edge {
from: Arc::clone(&s),
to: Arc::clone(&middle),
route: None,
},
Edge {
from: middle,
to: last,
route: None,
},
]
},
vec!["last", "middle"],
),
(
"a node that only starts an edge",
{
let (first, second) = (
agent_node(&new_agent("first")),
agent_node(&new_agent("second")),
);
vec![Edge {
from: first,
to: second,
route: None,
}]
},
vec!["first", "second"],
),
(
"agents in a nested sub-workflow",
{
let inner = workflow_node(
"inner",
chain(&[Arc::clone(&s), agent_node(&new_agent("buried"))]),
);
chain(&[Arc::clone(&s), agent_node(&new_agent("outer")), inner])
},
vec!["buried", "outer"],
),
(
"the agent a ParallelWorker runs",
chain(&[
Arc::clone(&s),
parallel_worker(agent_node(&new_agent("per_item"))),
]),
vec!["per_item"],
),
(
"agents in a sub-workflow a ParallelWorker runs",
{
let per_item = workflow_node(
"per_item_graph",
chain(&[Arc::clone(&s), agent_node(&new_agent("step"))]),
);
chain(&[Arc::clone(&s), parallel_worker(per_item)])
},
vec!["step"],
),
(
"an agent two nodes run is returned twice",
{
let shared = new_agent("shared");
let inner = workflow_node("inner", chain(&[Arc::clone(&s), agent_node(&shared)]));
chain(&[Arc::clone(&s), agent_node(&shared), inner])
},
vec!["shared", "shared"],
),
(
"nodes that run no agent",
chain(&[Arc::clone(&s), test_node("echo")]),
vec![],
),
];
for (name, edges, want) in cases {
assert_eq!(walked_names(&edges), want, "{name}");
}
}