use std::sync::Arc;
use std::time::Duration;
use gadk_genai::{Content, Part};
use pretty_assertions::assert_eq;
use rstest::rstest;
use serde_json::{Value, json};
use crate::error::WorkflowErrorKind as K;
use crate::tool::utils::CompiledSchema;
use crate::workflow::base_node::{default_validate_content_output, default_validate_output};
use crate::workflow::{
AgentNode, BaseNode, JoinNode, Node, NodeConfig, ParallelWorker, RetryConfig, WorkflowNode,
};
const _: fn() = || {
fn is_node<T: Node>() {}
is_node::<AgentNode>();
is_node::<JoinNode>();
is_node::<ParallelWorker>();
is_node::<WorkflowNode>();
};
fn test_schema_input() -> Value {
json!({"type": "object", "properties": {"value": {"type": "string"}}, "required": ["value"], "additionalProperties": false})
}
fn compiled(v: Value) -> Arc<CompiledSchema> {
Arc::new(CompiledSchema::new(v).expect("schema compiles"))
}
fn content(parts: Vec<Part>) -> Content {
Content {
parts,
..Default::default()
}
}
type CfgView = (
bool,
Option<bool>,
Option<bool>,
Option<i32>,
Option<Duration>,
bool,
);
fn cfg_view(c: &NodeConfig) -> CfgView {
(
c.parallel_worker,
c.rerun_on_resume,
c.wait_for_output,
c.retry_config.as_ref().map(|r| r.max_attempts),
c.timeout,
c.emits_own_span,
)
}
#[rstest]
#[case::zero_config("n", "desc", NodeConfig::default())]
#[case::wait_for_output_true("join", "fan-in", NodeConfig { wait_for_output: Some(true), ..Default::default() })]
#[case::parallel_worker_true("mapper", "data parallel", NodeConfig { parallel_worker: true, ..Default::default() })]
#[case::empty_name_and_description("", "", NodeConfig::default())]
#[case::fully_populated("full_node", "Node with all config fields set", NodeConfig {
parallel_worker: true, rerun_on_resume: Some(false), wait_for_output: Some(true),
retry_config: Some(RetryConfig { max_attempts: 3, ..Default::default() }), timeout: Some(Duration::from_secs(10)), emits_own_span: false })]
fn test_new_base_node_round_trip(#[case] name: &str, #[case] desc: &str, #[case] cfg: NodeConfig) {
let want = cfg_view(&cfg);
let b = BaseNode::new(name, desc, cfg);
assert_eq!(b.name(), name);
assert_eq!(b.description(), desc);
assert_eq!(cfg_view(b.config()), want);
}
#[test]
fn test_base_node_nil_schemas() {
let b = BaseNode::new(
"nil_schema_node",
"no validation node",
NodeConfig::default(),
);
assert!(b.input_schema().is_none());
assert!(b.output_schema().is_none());
let input = json!({"value": "hello"});
assert_eq!(b.validate_input(input.clone()).expect("passthrough"), input);
let output = json!({"value": "world"});
assert_eq!(
b.validate_output(output.clone()).expect("passthrough"),
output
);
}
#[test]
fn test_base_node_with_schemas() {
let b = BaseNode::with_schemas(
"schema_node",
"validation node",
Some(test_schema_input()),
Some(test_schema_input()),
NodeConfig::default(),
)
.expect("schemas compile");
assert_eq!(b.input_schema(), Some(&test_schema_input()));
assert_eq!(b.output_schema(), Some(&test_schema_input()));
let got = b
.validate_input(json!({"value": "hello"}))
.expect("valid input");
assert_eq!(got["value"], "hello");
let e = b
.validate_input(json!({"value": 123}))
.expect_err("invalid input");
assert_eq!(e.kind(), K::InputValidation);
let out = json!({"value": "world"});
assert_eq!(b.validate_output(out.clone()).expect("valid output"), out);
let e = b
.validate_output(json!({"value": 123}))
.expect_err("invalid output");
assert_eq!(e.kind(), K::OutputValidation);
}
#[test]
fn test_default_validate_output_content_fallback() {
let string_schema = compiled(json!({"type": "string"}));
let object_schema = compiled(test_schema_input());
let c = content(vec![Part::from_text("hello "), Part::from_text("world")]);
assert_eq!(
default_validate_content_output(&c, Some(&string_schema)).expect("string schema"),
json!("hello world")
);
let c = content(vec![Part::from_text(r#"{"value":"hello"}"#)]);
assert_eq!(
default_validate_content_output(&c, Some(&object_schema)).expect("object schema"),
json!({"value": "hello"})
);
let c = content(vec![Part::from_text("not valid json")]);
assert!(default_validate_content_output(&c, Some(&object_schema)).is_err());
let c = content(vec![Part::from_text(" ")]);
assert!(default_validate_content_output(&c, Some(&object_schema)).is_err());
let c = content(vec![
Part {
text: "internal reasoning, not for the user".into(),
thought: true,
..Default::default()
},
Part::from_text(r#"{"value":"hello"}"#),
]);
assert_eq!(
default_validate_content_output(&c, Some(&object_schema)).expect("thought part skipped"),
json!({"value": "hello"})
);
let c = content(vec![Part::from_text(" ")]);
assert_eq!(
default_validate_content_output(&c, Some(&string_schema)).expect("whitespace"),
json!(" ")
);
let min_len = compiled(json!({"type": "string", "minLength": 5}));
let c = content(vec![Part::from_text("hi")]);
assert!(default_validate_content_output(&c, Some(&min_len)).is_err());
let pattern = compiled(json!({"type": "string", "pattern": "^[0-9]+$"}));
let c = content(vec![Part::from_text("not-digits")]);
assert!(default_validate_content_output(&c, Some(&pattern)).is_err());
}
#[test]
fn test_default_validate_output_nil_passthrough() {
let got = default_validate_output(Value::Null, Some(&compiled(test_schema_input())))
.expect("nil passthrough");
assert_eq!(got, Value::Null);
}
#[test]
fn test_default_validate_output_string_fallback() {
let object_schema = compiled(test_schema_input());
let got = default_validate_output(json!(r#"{"value":"hello"}"#), Some(&object_schema))
.expect("parses json");
assert_eq!(got, json!({"value": "hello"}));
assert!(default_validate_output(json!("not json"), Some(&object_schema)).is_err());
let any_schema = compiled(json!({}));
assert_eq!(
default_validate_output(json!("plain text"), Some(&any_schema)).expect("any schema"),
json!("plain text")
);
let min_len = compiled(json!({"type": "string", "minLength": 5}));
assert!(default_validate_output(json!("hi"), Some(&min_len)).is_err());
}
#[test]
fn test_default_validate_output_multi_type_root() {
let schema = compiled(json!({"type": ["string", "null"]}));
let c = content(vec![Part::from_text("plain text")]);
assert_eq!(
default_validate_content_output(&c, Some(&schema)).expect("string or null"),
json!("plain text")
);
let schema = compiled(json!({"type": ["string", "integer"]}));
let c = content(vec![Part::from_text("42")]);
assert_eq!(
default_validate_content_output(&c, Some(&schema)).expect("string or integer"),
json!("42")
);
let schema = compiled(json!({"type": ["integer", "null"]}));
let c = content(vec![Part::from_text("42")]);
assert_eq!(
default_validate_content_output(&c, Some(&schema)).expect("integer or null"),
json!(42)
);
}