#![cfg(all(feature = "text-check", feature = "yaml"))]
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)]
use std::sync::Arc;
use futures::StreamExt;
use graphrt_core::Workflow;
use graphrt_core::engine::{Engine, RunInput};
use graphrt_core::event::Event;
use graphrt_core::graph::Graph;
use graphrt_core::node::Registry;
use serde_json::json;
const DRAFT_CHECK: &str = r#"
version: '1'
graph:
nodes:
- id: start
type: start
variables:
- {variable: draft, type: json}
- {variable: rules, type: json}
- id: check
type: text-check
text: [start, draft]
forbid:
- {name: semicolon, text: ";", message: "split the sentence with a full stop", replace: "."}
- {name: discontinued, text: "Spring Sale", fields: [highlights]}
require: [start, rules]
paragraphs: {highlights: 2}
ignore: ["N/A"]
- id: end
type: end
outputs:
- {variable: passed, value_selector: [check, passed]}
- {variable: total, value_selector: [check, issues_total]}
- {variable: notes, value_selector: [check, notes]}
- {variable: fixed, value_selector: [check, fixed]}
edges:
- {source: start, target: check}
- {source: check, target: end}
"#;
const PAYLOAD_GUARD: &str = r#"
version: '1'
graph:
nodes:
- id: start
type: start
variables:
- {variable: rows, type: json}
- id: guard
type: text-check
text: [start, rows]
forbid: [{detect: email}, {detect: phone}]
on_fail: error
- id: end
type: end
outputs:
- {variable: passed, value_selector: [guard, passed]}
edges:
- {source: start, target: guard}
- {source: guard, target: end}
"#;
async fn run(yaml: &str, input: RunInput) -> Event {
let workflow = Workflow::from_yaml(yaml).expect("valid document");
let graph = Arc::new(Graph::build(workflow, &Registry::with_builtins()).expect("valid graph"));
let events: Vec<Event> = Engine::new(graph).run(input).collect().await;
events.last().cloned().expect("a terminal event")
}
#[tokio::test]
async fn a_draft_is_checked_against_written_and_computed_rules() {
let draft = json!({
"summary": "Revenue rose 12%; orders held.",
"highlights": ["The Spring Sale led."],
"outlook": "N/A"
});
let rules = json!([
{"field": "summary", "text": "12%"},
{"field": "highlights", "text": "Garden", "message": "name the top category (Garden)"},
{"field": "outlook", "text": "next month"}
]);
let Event::RunSucceeded { outputs, .. } =
run(DRAFT_CHECK, RunInput::new().input("draft", draft).input("rules", rules)).await
else {
panic!("the run failed");
};
assert_eq!(outputs["passed"].as_bool(), Some(false));
assert_eq!(outputs["total"].as_i64(), Some(4));
assert_eq!(
outputs["notes"].to_text(),
"- summary: split the sentence with a full stop (\";\")\n\
- highlights[0]: remove \"Spring Sale\" (\"Spring Sale\")\n\
- highlights: name the top category (Garden)\n\
- highlights: write exactly 2 paragraphs, not 1"
);
assert_eq!(outputs["fixed"].to_json()["summary"], json!("Revenue rose 12%. orders held."));
}
#[tokio::test]
async fn a_payload_with_contact_details_stops_the_run_without_repeating_them() {
let clean = run(PAYLOAD_GUARD, RunInput::new().input("rows", json!([{"store": "North", "sales": 1200}]))).await;
assert!(matches!(clean, Event::RunSucceeded { .. }), "{clean:?}");
let leaking = run(PAYLOAD_GUARD, RunInput::new().input("rows", json!([{"owner": "ana@example.com"}]))).await;
let Event::RunFailed { error, error_type, .. } = leaking else { panic!("expected a failed run, got {leaking:?}") };
assert_eq!(error_type, "text_check");
assert!(error.contains("[0].owner: contains an email address"), "{error}");
assert!(!error.contains("ana@"), "{error}");
}