use commandagi::design::skills::*;
use commandagi::design::{declare, Declared};
use commandagi::design::El;
fn document(root: El) -> (String, Json) {
match declare(root).unwrap() {
Declared::Document { format, document, .. } => (format, document),
Declared::Graph(_) => panic!("a skill is a document"),
}
}
#[test]
fn the_example_is_its_document() {
let (format, doc) = document(
skill([
step().expr("mul(x, 3)").as_("out"),
step().tool("read_file").args(json(r#"{"path": "{file}"}"#)).as_("read").always(true),
reply().text("{out}"),
])
.about("Three times a number.")
.origin("frozen:cua"),
);
assert_eq!(format, "skill");
assert_eq!(
doc,
Json::parse(
r#"{"about": "Three times a number.", "origin": "frozen:cua",
"steps": [{"expr": "mul(x, 3)", "as": "out"}, {"tool": "read_file", "args": {"path": "{file}"}, "as": "read", "always": true}],
"reply": "{out}"}"#
)
.unwrap()
);
assert_eq!(document(result().value(2)), ("result".to_string(), Json::parse(r#"{"value": 2}"#).unwrap()));
}
#[test]
fn a_skill_that_does_not_read_is_refused_with_why() {
for (s, why) in [
(skill([step().expr("1").tool("read_file")]), "is exactly one of expr, tool, script, skill, see (it has expr and tool)"),
(skill([step().as_("x")]), "is exactly one of"),
(skill([step().expr("1").always("yes")]), "always is true or false"),
(skill([step().tool("read_file").args(["a"])]), "args is an object"),
(skill([]).attr("gives", json(r#"["at(arm, home)"]"#)), "gives"),
] {
let said = declare(s).unwrap_err();
assert!(said.contains(why), "{why}: {said}");
}
}
#[test]
fn a_phrase_an_example_a_rule_or_a_fact_is_refused_by_name() {
for tag in ["phrase", "example", "rule", "fact"] {
let said = declare(skill([El::new(tag).attr("is", "a")])).unwrap_err();
assert!(said.contains(tag), "{tag}: {said}");
}
}