Skip to main content

tramli_plugins/testing/
mod.rs

1use tramli::{FlowDefinition, FlowState, TransitionType};
2
3/// A test scenario step.
4#[derive(Debug, Clone)]
5pub struct FlowScenario {
6    pub name: String,
7    pub steps: Vec<String>,
8}
9
10/// A test plan containing generated scenarios.
11#[derive(Debug, Clone)]
12pub struct FlowTestPlan {
13    pub scenarios: Vec<FlowScenario>,
14}
15
16/// Scenario test plugin — generates BDD-style scenarios from a flow definition.
17pub struct ScenarioTestPlugin;
18
19impl ScenarioTestPlugin {
20    pub fn generate<S: FlowState>(definition: &FlowDefinition<S>) -> FlowTestPlan {
21        let mut scenarios = Vec::new();
22        for t in &definition.transitions {
23            let mut steps = Vec::new();
24            steps.push(format!("given flow in {:?}", t.from));
25            match t.transition_type {
26                TransitionType::External => {
27                    if let Some(ref g) = t.guard {
28                        steps.push(format!("when external data satisfies guard {}", g.name()));
29                    }
30                }
31                TransitionType::Auto => {
32                    if let Some(ref p) = t.processor {
33                        steps.push(format!("when auto processor {} runs", p.name()));
34                    }
35                }
36                TransitionType::Branch => {
37                    if let Some(ref b) = t.branch {
38                        steps.push(format!("when branch {} selects a route", b.name()));
39                    }
40                }
41                _ => {}
42            }
43            steps.push(format!("then flow reaches {:?}", t.to));
44            scenarios.push(FlowScenario {
45                name: format!("{:?}_to_{:?}", t.from, t.to),
46                steps,
47            });
48        }
49        FlowTestPlan { scenarios }
50    }
51}