mockforge-core 0.3.116

Shared logic for MockForge - routing, validation, latency, proxy
Documentation
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
//! Chain execution engine for request chaining
//!
//! This module provides the execution engine that manages chain execution with
//! dependency resolution, parallel execution when possible, and proper error handling.

use crate::request_chaining::{
    ChainConfig, ChainDefinition, ChainExecutionContext, ChainLink, ChainResponse,
    ChainTemplatingContext, RequestChainRegistry,
};
#[cfg(feature = "scripting")]
use crate::request_scripting::{ScriptContext, ScriptEngine};
use crate::templating::{expand_str_with_context, TemplatingContext};
use crate::{Error, Result};
use chrono::Utc;
use futures::future::join_all;
use reqwest::{
    header::{HeaderMap, HeaderName, HeaderValue},
    Client, Method,
};
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::{timeout, Duration};

/// Record of a chain execution with timestamp
#[derive(Debug, Clone)]
pub struct ExecutionRecord {
    /// ISO 8601 timestamp when the chain was executed
    pub executed_at: String,
    /// Result of the chain execution
    pub result: ChainExecutionResult,
}

/// Engine for executing request chains
#[derive(Debug)]
pub struct ChainExecutionEngine {
    /// HTTP client for making requests
    http_client: Client,
    /// Chain registry
    registry: Arc<RequestChainRegistry>,
    /// Global configuration
    config: ChainConfig,
    /// Execution history storage (chain_id -> Vec<ExecutionRecord>)
    execution_history: Arc<Mutex<HashMap<String, Vec<ExecutionRecord>>>>,
    /// JavaScript scripting engine for pre/post request scripts
    #[cfg(feature = "scripting")]
    script_engine: ScriptEngine,
}

impl ChainExecutionEngine {
    /// Create a new chain execution engine
    ///
    /// # Panics
    ///
    /// This method will panic if the HTTP client cannot be created, which typically
    /// indicates a system configuration issue. For better error handling, use `try_new()`.
    pub fn new(registry: Arc<RequestChainRegistry>, config: ChainConfig) -> Self {
        Self::try_new(registry, config)
            .unwrap_or_else(|e| {
                panic!(
                    "Failed to create HTTP client for chain execution engine: {}. \
                    This typically indicates a system configuration issue (e.g., invalid timeout value).",
                    e
                )
            })
    }

    /// Try to create a new chain execution engine
    ///
    /// Returns an error if the HTTP client cannot be created.
    pub fn try_new(registry: Arc<RequestChainRegistry>, config: ChainConfig) -> Result<Self> {
        let http_client = Client::builder()
            .timeout(Duration::from_secs(config.global_timeout_secs))
            .build()
            .map_err(|e| {
                Error::internal(format!(
                    "Failed to create HTTP client: {}. \
                Check that the timeout value ({}) is valid.",
                    e, config.global_timeout_secs
                ))
            })?;

        Ok(Self {
            http_client,
            registry,
            config,
            execution_history: Arc::new(Mutex::new(HashMap::new())),
            #[cfg(feature = "scripting")]
            script_engine: ScriptEngine::new(),
        })
    }

    /// Execute a chain by ID
    pub async fn execute_chain(
        &self,
        chain_id: &str,
        variables: Option<Value>,
    ) -> Result<ChainExecutionResult> {
        let chain = self
            .registry
            .get_chain(chain_id)
            .await
            .ok_or_else(|| Error::internal(format!("Chain '{}' not found", chain_id)))?;

        let result = self.execute_chain_definition(&chain, variables).await?;

        // Store execution in history
        let record = ExecutionRecord {
            executed_at: Utc::now().to_rfc3339(),
            result: result.clone(),
        };

        let mut history = self.execution_history.lock().await;
        history.entry(chain_id.to_string()).or_insert_with(Vec::new).push(record);

        Ok(result)
    }

    /// Get execution history for a chain
    pub async fn get_chain_history(&self, chain_id: &str) -> Vec<ExecutionRecord> {
        let history = self.execution_history.lock().await;
        history.get(chain_id).cloned().unwrap_or_default()
    }

    /// Execute a chain definition
    pub async fn execute_chain_definition(
        &self,
        chain_def: &ChainDefinition,
        variables: Option<Value>,
    ) -> Result<ChainExecutionResult> {
        // First validate the chain
        self.registry.validate_chain(chain_def).await?;

        let start_time = std::time::Instant::now();
        let mut execution_context = ChainExecutionContext::new(chain_def.clone());

        // Initialize context with chain variables
        for (key, value) in &chain_def.variables {
            execution_context
                .templating
                .chain_context
                .set_variable(key.clone(), value.clone());
        }

        // Merge custom variables from request
        if let Some(Value::Object(map)) = variables {
            for (key, value) in map {
                execution_context.templating.chain_context.set_variable(key, value);
            }
        }

        if self.config.enable_parallel_execution {
            self.execute_with_parallelism(&mut execution_context).await
        } else {
            self.execute_sequential(&mut execution_context).await
        }
        .map(|_| ChainExecutionResult {
            chain_id: chain_def.id.clone(),
            status: ChainExecutionStatus::Successful,
            total_duration_ms: start_time.elapsed().as_millis() as u64,
            request_results: execution_context.templating.chain_context.responses.clone(),
            error_message: None,
        })
    }

    /// Execute chain using topological sorting for parallelism
    async fn execute_with_parallelism(
        &self,
        execution_context: &mut ChainExecutionContext,
    ) -> Result<()> {
        let dep_graph = self.build_dependency_graph(&execution_context.definition.links);
        let topo_order = self.topological_sort(&dep_graph)?;

        // Group requests by dependency level
        let mut level_groups = vec![];
        let mut processed = HashSet::new();

        for request_id in topo_order {
            if !processed.contains(&request_id) {
                let mut level = vec![];
                self.collect_dependency_level(request_id, &dep_graph, &mut level, &mut processed);
                level_groups.push(level);
            }
        }

        // Execute levels in parallel
        for level in level_groups {
            if level.len() == 1 {
                // Single request, execute directly
                let request_id = &level[0];
                let link = execution_context
                    .definition
                    .links
                    .iter()
                    .find(|l| l.request.id == *request_id)
                    .ok_or_else(|| {
                        Error::internal(format!(
                            "Chain link not found for request_id '{}' during parallel execution",
                            request_id
                        ))
                    })?;

                let link_clone = link.clone();
                self.execute_request(&link_clone, execution_context).await?;
            } else {
                // Execute level in parallel
                let tasks = level
                    .into_iter()
                    .filter_map(|request_id| {
                        let link = execution_context
                            .definition
                            .links
                            .iter()
                            .find(|l| l.request.id == request_id);
                        let link = match link {
                            Some(l) => l.clone(),
                            None => {
                                tracing::error!(
                                    "Chain link not found for request_id '{}' during parallel execution",
                                    request_id
                                );
                                return None;
                            }
                        };
                        // Create a new context for parallel execution
                        let parallel_context = ChainExecutionContext {
                            definition: execution_context.definition.clone(),
                            templating: execution_context.templating.clone(),
                            start_time: std::time::Instant::now(),
                            config: execution_context.config.clone(),
                        };

                        let context = Arc::new(Mutex::new(parallel_context));
                        let engine =
                            ChainExecutionEngine::new(self.registry.clone(), self.config.clone());

                        Some(tokio::spawn(async move {
                            let mut ctx = context.lock().await;
                            engine.execute_request(&link, &mut ctx).await
                        }))
                    })
                    .collect::<Vec<_>>();

                let results = join_all(tasks).await;
                for result in results {
                    result
                        .map_err(|e| Error::internal(format!("Task join error: {}", e)))?
                        .map_err(|e| Error::internal(format!("Request execution error: {}", e)))?;
                }
            }
        }

        Ok(())
    }

    /// Execute requests sequentially
    async fn execute_sequential(
        &self,
        execution_context: &mut ChainExecutionContext,
    ) -> Result<()> {
        let links = execution_context.definition.links.clone();
        for link in &links {
            self.execute_request(link, execution_context).await?;
        }
        Ok(())
    }

    /// Execute a single request in the chain
    async fn execute_request(
        &self,
        link: &ChainLink,
        execution_context: &mut ChainExecutionContext,
    ) -> Result<()> {
        let request_start = std::time::Instant::now();

        // Prepare the request with templating
        execution_context.templating.set_current_request(link.request.clone());

        let method = Method::from_bytes(link.request.method.as_bytes()).map_err(|e| {
            Error::internal(format!("Invalid HTTP method '{}': {}", link.request.method, e))
        })?;

        let url = self.expand_template(&link.request.url, &execution_context.templating);

        // Prepare headers
        let mut headers = HeaderMap::new();
        for (key, value) in &link.request.headers {
            let expanded_value = self.expand_template(value, &execution_context.templating);
            let header_name = HeaderName::from_str(key)
                .map_err(|e| Error::internal(format!("Invalid header name '{}': {}", key, e)))?;
            let header_value = HeaderValue::from_str(&expanded_value).map_err(|e| {
                Error::internal(format!("Invalid header value for '{}': {}", key, e))
            })?;
            headers.insert(header_name, header_value);
        }

        // Prepare request builder
        let mut request_builder = self.http_client.request(method, &url).headers(headers.clone());

        // Add body if present
        if let Some(body) = &link.request.body {
            match body {
                crate::request_chaining::RequestBody::Json(json_value) => {
                    let expanded_body =
                        self.expand_template_in_json(json_value, &execution_context.templating);
                    request_builder = request_builder.json(&expanded_body);
                }
                crate::request_chaining::RequestBody::BinaryFile { path, content_type } => {
                    // Create templating context for path expansion
                    let templating_context =
                        TemplatingContext::with_chain(execution_context.templating.clone());

                    // Expand templates in the file path
                    let expanded_path = expand_str_with_context(path, &templating_context);

                    // Create a new body with expanded path
                    let binary_body = crate::request_chaining::RequestBody::binary_file(
                        expanded_path,
                        content_type.clone(),
                    );

                    // Read the binary file
                    match binary_body.to_bytes().await {
                        Ok(file_bytes) => {
                            request_builder = request_builder.body(file_bytes);

                            // Set content type if specified
                            if let Some(ct) = content_type {
                                let mut headers = headers.clone();
                                headers.insert(
                                    "content-type",
                                    ct.parse().unwrap_or_else(|_| {
                                        HeaderValue::from_static("application/octet-stream")
                                    }),
                                );
                                request_builder = request_builder.headers(headers);
                            }
                        }
                        Err(e) => {
                            return Err(e);
                        }
                    }
                }
            }
        }

        // Set timeout if specified
        if let Some(timeout_secs) = link.request.timeout_secs {
            request_builder = request_builder.timeout(Duration::from_secs(timeout_secs));
        }

        // Execute pre-request script if configured
        #[cfg(feature = "scripting")]
        if let Some(scripting) = &link.request.scripting {
            if let Some(pre_script) = &scripting.pre_script {
                let script_context = ScriptContext {
                    request: Some(link.request.clone()),
                    response: None,
                    chain_context: execution_context.templating.chain_context.variables.clone(),
                    variables: HashMap::new(),
                    env_vars: std::env::vars().collect(),
                };

                match self
                    .script_engine
                    .execute_script(pre_script, &script_context, scripting.timeout_ms)
                    .await
                {
                    Ok(script_result) => {
                        // Merge script-modified variables into chain context
                        for (key, value) in script_result.modified_variables {
                            execution_context.templating.chain_context.set_variable(key, value);
                        }
                    }
                    Err(e) => {
                        tracing::warn!(
                            "Pre-script execution failed for request '{}': {}",
                            link.request.id,
                            e
                        );
                        // Continue execution even if script fails
                    }
                }
            }
        }

        // Execute the request
        let response_result =
            timeout(Duration::from_secs(self.config.global_timeout_secs), request_builder.send())
                .await;

        let response = match response_result {
            Ok(Ok(resp)) => resp,
            Ok(Err(e)) => {
                return Err(Error::internal(format!(
                    "Request '{}' failed: {}",
                    link.request.id, e
                )));
            }
            Err(_) => {
                return Err(Error::internal(format!("Request '{}' timed out", link.request.id)));
            }
        };

        let status = response.status();
        let headers: HashMap<String, String> = response
            .headers()
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string()))
            .collect();

        let body_text = response.text().await.unwrap_or_default();
        let body_json: Option<Value> = serde_json::from_str(&body_text).ok();

        let duration_ms = request_start.elapsed().as_millis() as u64;
        let executed_at = Utc::now().to_rfc3339();

        let chain_response = ChainResponse {
            status: status.as_u16(),
            headers,
            body: body_json,
            duration_ms,
            executed_at,
            error: None,
        };

        // Validate expected status if specified
        if let Some(expected) = &link.request.expected_status {
            if !expected.contains(&status.as_u16()) {
                let error_msg = format!(
                    "Request '{}' returned status {} but expected one of {:?}",
                    link.request.id,
                    status.as_u16(),
                    expected
                );
                return Err(Error::internal(error_msg));
            }
        }

        // Store the response
        if let Some(store_name) = &link.store_as {
            execution_context
                .templating
                .chain_context
                .store_response(store_name.clone(), chain_response.clone());
        }

        // Extract variables from response
        for (var_name, extraction_path) in &link.extract {
            if let Some(value) = self.extract_from_response(&chain_response, extraction_path) {
                execution_context.templating.chain_context.set_variable(var_name.clone(), value);
            }
        }

        // Execute post-request script if configured
        #[cfg(feature = "scripting")]
        if let Some(scripting) = &link.request.scripting {
            if let Some(post_script) = &scripting.post_script {
                let script_context = ScriptContext {
                    request: Some(link.request.clone()),
                    response: Some(chain_response.clone()),
                    chain_context: execution_context.templating.chain_context.variables.clone(),
                    variables: HashMap::new(),
                    env_vars: std::env::vars().collect(),
                };

                match self
                    .script_engine
                    .execute_script(post_script, &script_context, scripting.timeout_ms)
                    .await
                {
                    Ok(script_result) => {
                        // Merge script-modified variables into chain context
                        for (key, value) in script_result.modified_variables {
                            execution_context.templating.chain_context.set_variable(key, value);
                        }
                    }
                    Err(e) => {
                        tracing::warn!(
                            "Post-script execution failed for request '{}': {}",
                            link.request.id,
                            e
                        );
                        // Continue execution even if script fails
                    }
                }
            }
        }

        // Also store by request ID as fallback
        execution_context
            .templating
            .chain_context
            .store_response(link.request.id.clone(), chain_response);

        Ok(())
    }

    /// Build dependency graph from chain links
    fn build_dependency_graph(&self, links: &[ChainLink]) -> HashMap<String, Vec<String>> {
        let mut graph = HashMap::new();

        for link in links {
            graph
                .entry(link.request.id.clone())
                .or_insert_with(Vec::new)
                .extend(link.request.depends_on.iter().cloned());
        }

        graph
    }

    /// Perform topological sort on dependency graph
    fn topological_sort(&self, graph: &HashMap<String, Vec<String>>) -> Result<Vec<String>> {
        let mut visited = HashSet::new();
        let mut rec_stack = HashSet::new();
        let mut result = Vec::new();

        for node in graph.keys() {
            if !visited.contains(node) {
                self.topo_sort_util(node, graph, &mut visited, &mut rec_stack, &mut result)?;
            }
        }

        result.reverse();
        Ok(result)
    }

    /// Utility function for topological sort
    #[allow(clippy::only_used_in_recursion)]
    fn topo_sort_util(
        &self,
        node: &str,
        graph: &HashMap<String, Vec<String>>,
        visited: &mut HashSet<String>,
        rec_stack: &mut HashSet<String>,
        result: &mut Vec<String>,
    ) -> Result<()> {
        visited.insert(node.to_string());
        rec_stack.insert(node.to_string());

        if let Some(dependencies) = graph.get(node) {
            for dep in dependencies {
                if !visited.contains(dep) {
                    self.topo_sort_util(dep, graph, visited, rec_stack, result)?;
                } else if rec_stack.contains(dep) {
                    return Err(Error::internal(format!(
                        "Circular dependency detected involving '{}'",
                        node
                    )));
                }
            }
        }

        rec_stack.remove(node);
        result.push(node.to_string());
        Ok(())
    }

    /// Collect requests that can be executed in parallel (same dependency level)
    fn collect_dependency_level(
        &self,
        request_id: String,
        _graph: &HashMap<String, Vec<String>>,
        level: &mut Vec<String>,
        processed: &mut HashSet<String>,
    ) {
        level.push(request_id.clone());
        processed.insert(request_id);
    }

    /// Expand template string with chain context
    fn expand_template(&self, template: &str, context: &ChainTemplatingContext) -> String {
        let templating_context = TemplatingContext {
            chain_context: Some(context.clone()),
            env_context: None,
            virtual_clock: None,
        };
        expand_str_with_context(template, &templating_context)
    }

    /// Expand template variables in JSON value
    fn expand_template_in_json(&self, value: &Value, context: &ChainTemplatingContext) -> Value {
        match value {
            Value::String(s) => Value::String(self.expand_template(s, context)),
            Value::Array(arr) => {
                Value::Array(arr.iter().map(|v| self.expand_template_in_json(v, context)).collect())
            }
            Value::Object(map) => {
                let mut new_map = serde_json::Map::new();
                for (k, v) in map {
                    new_map.insert(
                        self.expand_template(k, context),
                        self.expand_template_in_json(v, context),
                    );
                }
                Value::Object(new_map)
            }
            _ => value.clone(),
        }
    }

    /// Extract value from response using JSON path-like syntax
    fn extract_from_response(&self, response: &ChainResponse, path: &str) -> Option<Value> {
        let parts: Vec<&str> = path.split('.').collect();

        if parts.is_empty() || parts[0] != "body" {
            return None;
        }

        let mut current = response.body.as_ref()?;

        for part in &parts[1..] {
            match current {
                Value::Object(map) => {
                    current = map.get(*part)?;
                }
                Value::Array(arr) => {
                    if part.starts_with('[') && part.ends_with(']') {
                        let index_str = &part[1..part.len() - 1];
                        if let Ok(index) = index_str.parse::<usize>() {
                            current = arr.get(index)?;
                        } else {
                            return None;
                        }
                    } else {
                        return None;
                    }
                }
                _ => return None,
            }
        }

        Some(current.clone())
    }
}

/// Result of executing a request chain
#[derive(Debug, Clone)]
pub struct ChainExecutionResult {
    /// Unique identifier for the executed chain
    pub chain_id: String,
    /// Overall execution status
    pub status: ChainExecutionStatus,
    /// Total duration of chain execution in milliseconds
    pub total_duration_ms: u64,
    /// Results of individual requests in the chain, keyed by request ID
    pub request_results: HashMap<String, ChainResponse>,
    /// Error message if execution failed
    pub error_message: Option<String>,
}

/// Status of chain execution
#[derive(Debug, Clone, PartialEq)]
pub enum ChainExecutionStatus {
    /// All requests in the chain succeeded
    Successful,
    /// Some requests succeeded but others failed
    PartialSuccess,
    /// Chain execution failed
    Failed,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::request_chaining::{ChainRequest, ChainResponse};
    use serde_json::json;
    use std::sync::Arc;

    fn create_test_engine() -> ChainExecutionEngine {
        let registry = Arc::new(RequestChainRegistry::new(ChainConfig::default()));
        ChainExecutionEngine::new(registry, ChainConfig::default())
    }

    fn create_test_chain_response() -> ChainResponse {
        ChainResponse {
            status: 200,
            headers: {
                let mut h = HashMap::new();
                h.insert("content-type".to_string(), "application/json".to_string());
                h
            },
            body: Some(json!({
                "user": {
                    "id": 123,
                    "name": "test",
                    "roles": ["admin", "user"]
                },
                "items": [
                    {"id": 1, "value": "a"},
                    {"id": 2, "value": "b"}
                ]
            })),
            duration_ms: 50,
            executed_at: "2024-01-15T10:00:00Z".to_string(),
            error: None,
        }
    }

    // ExecutionRecord tests
    #[test]
    fn test_execution_record_debug() {
        let record = ExecutionRecord {
            executed_at: "2024-01-15T10:00:00Z".to_string(),
            result: ChainExecutionResult {
                chain_id: "test-chain".to_string(),
                status: ChainExecutionStatus::Successful,
                total_duration_ms: 100,
                request_results: HashMap::new(),
                error_message: None,
            },
        };

        let debug = format!("{:?}", record);
        assert!(debug.contains("ExecutionRecord"));
        assert!(debug.contains("executed_at"));
    }

    #[test]
    fn test_execution_record_clone() {
        let record = ExecutionRecord {
            executed_at: "2024-01-15T10:00:00Z".to_string(),
            result: ChainExecutionResult {
                chain_id: "test-chain".to_string(),
                status: ChainExecutionStatus::Successful,
                total_duration_ms: 100,
                request_results: HashMap::new(),
                error_message: None,
            },
        };

        let cloned = record.clone();
        assert_eq!(cloned.executed_at, record.executed_at);
        assert_eq!(cloned.result.chain_id, record.result.chain_id);
    }

    // ChainExecutionResult tests
    #[test]
    fn test_chain_execution_result_debug() {
        let result = ChainExecutionResult {
            chain_id: "test-chain".to_string(),
            status: ChainExecutionStatus::Successful,
            total_duration_ms: 100,
            request_results: HashMap::new(),
            error_message: None,
        };

        let debug = format!("{:?}", result);
        assert!(debug.contains("ChainExecutionResult"));
        assert!(debug.contains("chain_id"));
    }

    #[test]
    fn test_chain_execution_result_clone() {
        let mut request_results = HashMap::new();
        request_results.insert("req1".to_string(), create_test_chain_response());

        let result = ChainExecutionResult {
            chain_id: "test-chain".to_string(),
            status: ChainExecutionStatus::Successful,
            total_duration_ms: 100,
            request_results,
            error_message: Some("test error".to_string()),
        };

        let cloned = result.clone();
        assert_eq!(cloned.chain_id, result.chain_id);
        assert_eq!(cloned.total_duration_ms, result.total_duration_ms);
        assert_eq!(cloned.error_message, result.error_message);
    }

    // ChainExecutionStatus tests
    #[test]
    fn test_chain_execution_status_debug() {
        let status = ChainExecutionStatus::Successful;
        let debug = format!("{:?}", status);
        assert!(debug.contains("Successful"));

        let status = ChainExecutionStatus::PartialSuccess;
        let debug = format!("{:?}", status);
        assert!(debug.contains("PartialSuccess"));

        let status = ChainExecutionStatus::Failed;
        let debug = format!("{:?}", status);
        assert!(debug.contains("Failed"));
    }

    #[test]
    fn test_chain_execution_status_clone() {
        let status = ChainExecutionStatus::Successful;
        let cloned = status.clone();
        assert_eq!(cloned, ChainExecutionStatus::Successful);
    }

    #[test]
    fn test_chain_execution_status_eq() {
        assert_eq!(ChainExecutionStatus::Successful, ChainExecutionStatus::Successful);
        assert_eq!(ChainExecutionStatus::PartialSuccess, ChainExecutionStatus::PartialSuccess);
        assert_eq!(ChainExecutionStatus::Failed, ChainExecutionStatus::Failed);

        assert_ne!(ChainExecutionStatus::Successful, ChainExecutionStatus::Failed);
        assert_ne!(ChainExecutionStatus::PartialSuccess, ChainExecutionStatus::Successful);
    }

    // ChainExecutionEngine tests
    #[tokio::test]
    async fn test_engine_creation() {
        let registry = Arc::new(RequestChainRegistry::new(ChainConfig::default()));
        let _engine = ChainExecutionEngine::new(registry, ChainConfig::default());

        // Engine should be created successfully
    }

    #[tokio::test]
    async fn test_engine_try_new() {
        let registry = Arc::new(RequestChainRegistry::new(ChainConfig::default()));
        let result = ChainExecutionEngine::try_new(registry, ChainConfig::default());
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_engine_debug() {
        let engine = create_test_engine();
        let debug = format!("{:?}", engine);
        assert!(debug.contains("ChainExecutionEngine"));
    }

    #[tokio::test]
    async fn test_topological_sort() {
        let registry = Arc::new(RequestChainRegistry::new(ChainConfig::default()));
        let engine = ChainExecutionEngine::new(registry, ChainConfig::default());

        let mut graph = HashMap::new();
        graph.insert("A".to_string(), vec![]);
        graph.insert("B".to_string(), vec!["A".to_string()]);
        graph.insert("C".to_string(), vec!["A".to_string()]);
        graph.insert("D".to_string(), vec!["B".to_string(), "C".to_string()]);

        let topo_order = engine.topological_sort(&graph).unwrap();

        // Verify this is a valid topological ordering
        // D should come before B and C (its dependencies)
        // B should come before A (its dependency)
        // C should come before A (its dependency)
        let d_pos = topo_order.iter().position(|x| x == "D").unwrap();
        let b_pos = topo_order.iter().position(|x| x == "B").unwrap();
        let c_pos = topo_order.iter().position(|x| x == "C").unwrap();
        let a_pos = topo_order.iter().position(|x| x == "A").unwrap();

        assert!(d_pos < b_pos, "D should come before B");
        assert!(d_pos < c_pos, "D should come before C");
        assert!(b_pos < a_pos, "B should come before A");
        assert!(c_pos < a_pos, "C should come before A");
        assert_eq!(topo_order.len(), 4, "Should have all 4 nodes");
    }

    #[tokio::test]
    async fn test_topological_sort_single_node() {
        let engine = create_test_engine();

        let mut graph = HashMap::new();
        graph.insert("A".to_string(), vec![]);

        let topo_order = engine.topological_sort(&graph).unwrap();
        assert_eq!(topo_order, vec!["A".to_string()]);
    }

    #[tokio::test]
    async fn test_topological_sort_linear_chain() {
        let engine = create_test_engine();

        let mut graph = HashMap::new();
        graph.insert("A".to_string(), vec![]);
        graph.insert("B".to_string(), vec!["A".to_string()]);
        graph.insert("C".to_string(), vec!["B".to_string()]);

        let topo_order = engine.topological_sort(&graph).unwrap();

        let c_pos = topo_order.iter().position(|x| x == "C").unwrap();
        let b_pos = topo_order.iter().position(|x| x == "B").unwrap();
        let a_pos = topo_order.iter().position(|x| x == "A").unwrap();

        assert!(c_pos < b_pos);
        assert!(b_pos < a_pos);
    }

    #[tokio::test]
    async fn test_topological_sort_empty_graph() {
        let engine = create_test_engine();
        let graph = HashMap::new();

        let topo_order = engine.topological_sort(&graph).unwrap();
        assert!(topo_order.is_empty());
    }

    #[tokio::test]
    async fn test_circular_dependency_detection() {
        let registry = Arc::new(RequestChainRegistry::new(ChainConfig::default()));
        let engine = ChainExecutionEngine::new(registry, ChainConfig::default());

        let mut graph = HashMap::new();
        graph.insert("A".to_string(), vec!["B".to_string()]);
        graph.insert("B".to_string(), vec!["A".to_string()]); // Circular dependency

        let result = engine.topological_sort(&graph);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_circular_dependency_self_reference() {
        let engine = create_test_engine();

        let mut graph = HashMap::new();
        graph.insert("A".to_string(), vec!["A".to_string()]); // Self-reference

        let result = engine.topological_sort(&graph);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_circular_dependency_chain() {
        let engine = create_test_engine();

        let mut graph = HashMap::new();
        graph.insert("A".to_string(), vec!["C".to_string()]);
        graph.insert("B".to_string(), vec!["A".to_string()]);
        graph.insert("C".to_string(), vec!["B".to_string()]); // A -> C -> B -> A

        let result = engine.topological_sort(&graph);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_build_dependency_graph() {
        let engine = create_test_engine();

        let links = vec![
            ChainLink {
                request: ChainRequest {
                    id: "req1".to_string(),
                    method: "GET".to_string(),
                    url: "http://example.com/1".to_string(),
                    headers: HashMap::new(),
                    body: None,
                    depends_on: vec![],
                    timeout_secs: None,
                    expected_status: None,
                    scripting: None,
                },
                store_as: None,
                extract: HashMap::new(),
            },
            ChainLink {
                request: ChainRequest {
                    id: "req2".to_string(),
                    method: "GET".to_string(),
                    url: "http://example.com/2".to_string(),
                    headers: HashMap::new(),
                    body: None,
                    depends_on: vec!["req1".to_string()],
                    timeout_secs: None,
                    expected_status: None,
                    scripting: None,
                },
                store_as: None,
                extract: HashMap::new(),
            },
            ChainLink {
                request: ChainRequest {
                    id: "req3".to_string(),
                    method: "GET".to_string(),
                    url: "http://example.com/3".to_string(),
                    headers: HashMap::new(),
                    body: None,
                    depends_on: vec!["req1".to_string(), "req2".to_string()],
                    timeout_secs: None,
                    expected_status: None,
                    scripting: None,
                },
                store_as: None,
                extract: HashMap::new(),
            },
        ];

        let graph = engine.build_dependency_graph(&links);

        assert!(graph.contains_key("req1"));
        assert!(graph.contains_key("req2"));
        assert!(graph.contains_key("req3"));
        assert_eq!(graph.get("req1").unwrap().len(), 0);
        assert_eq!(graph.get("req2").unwrap(), &vec!["req1".to_string()]);
        assert_eq!(graph.get("req3").unwrap(), &vec!["req1".to_string(), "req2".to_string()]);
    }

    // extract_from_response tests
    #[tokio::test]
    async fn test_extract_from_response_simple_field() {
        let engine = create_test_engine();
        let response = create_test_chain_response();

        let value = engine.extract_from_response(&response, "body.user.id");
        assert!(value.is_some());
        assert_eq!(value.unwrap(), json!(123));
    }

    #[tokio::test]
    async fn test_extract_from_response_nested_field() {
        let engine = create_test_engine();
        let response = create_test_chain_response();

        let value = engine.extract_from_response(&response, "body.user.name");
        assert!(value.is_some());
        assert_eq!(value.unwrap(), json!("test"));
    }

    #[tokio::test]
    async fn test_extract_from_response_array_element() {
        let engine = create_test_engine();
        let response = create_test_chain_response();

        let value = engine.extract_from_response(&response, "body.items.[0].value");
        assert!(value.is_some());
        assert_eq!(value.unwrap(), json!("a"));
    }

    #[tokio::test]
    async fn test_extract_from_response_array_element_second() {
        let engine = create_test_engine();
        let response = create_test_chain_response();

        let value = engine.extract_from_response(&response, "body.items.[1].id");
        assert!(value.is_some());
        assert_eq!(value.unwrap(), json!(2));
    }

    #[tokio::test]
    async fn test_extract_from_response_invalid_path() {
        let engine = create_test_engine();
        let response = create_test_chain_response();

        let value = engine.extract_from_response(&response, "body.nonexistent");
        assert!(value.is_none());
    }

    #[tokio::test]
    async fn test_extract_from_response_non_body_path() {
        let engine = create_test_engine();
        let response = create_test_chain_response();

        let value = engine.extract_from_response(&response, "headers.content-type");
        assert!(value.is_none()); // Must start with "body"
    }

    #[tokio::test]
    async fn test_extract_from_response_empty_path() {
        let engine = create_test_engine();
        let response = create_test_chain_response();

        let value = engine.extract_from_response(&response, "");
        assert!(value.is_none());
    }

    #[tokio::test]
    async fn test_extract_from_response_invalid_array_index() {
        let engine = create_test_engine();
        let response = create_test_chain_response();

        let value = engine.extract_from_response(&response, "body.items.[invalid].value");
        assert!(value.is_none());
    }

    #[tokio::test]
    async fn test_extract_from_response_array_out_of_bounds() {
        let engine = create_test_engine();
        let response = create_test_chain_response();

        let value = engine.extract_from_response(&response, "body.items.[100].value");
        assert!(value.is_none());
    }

    #[tokio::test]
    async fn test_extract_from_response_no_body() {
        let engine = create_test_engine();
        let response = ChainResponse {
            status: 200,
            headers: HashMap::new(),
            body: None,
            duration_ms: 50,
            executed_at: "2024-01-15T10:00:00Z".to_string(),
            error: None,
        };

        let value = engine.extract_from_response(&response, "body.user.id");
        assert!(value.is_none());
    }

    // expand_template tests
    #[tokio::test]
    async fn test_expand_template_simple() {
        use crate::request_chaining::ChainContext;
        let engine = create_test_engine();
        let context = ChainTemplatingContext::new(ChainContext::new());

        let result = engine.expand_template("hello world", &context);
        assert_eq!(result, "hello world");
    }

    #[tokio::test]
    async fn test_expand_template_with_variable() {
        use crate::request_chaining::ChainContext;
        let engine = create_test_engine();
        let mut context = ChainTemplatingContext::new(ChainContext::new());
        context.chain_context.set_variable("name".to_string(), json!("test"));

        let result = engine.expand_template("hello {{chain.name}}", &context);
        // Template expansion should work
        assert!(result.contains("hello"));
    }

    // expand_template_in_json tests
    #[tokio::test]
    async fn test_expand_template_in_json_string() {
        use crate::request_chaining::ChainContext;
        let engine = create_test_engine();
        let context = ChainTemplatingContext::new(ChainContext::new());

        let input = json!("hello world");
        let result = engine.expand_template_in_json(&input, &context);
        assert_eq!(result, json!("hello world"));
    }

    #[tokio::test]
    async fn test_expand_template_in_json_number() {
        use crate::request_chaining::ChainContext;
        let engine = create_test_engine();
        let context = ChainTemplatingContext::new(ChainContext::new());

        let input = json!(42);
        let result = engine.expand_template_in_json(&input, &context);
        assert_eq!(result, json!(42));
    }

    #[tokio::test]
    async fn test_expand_template_in_json_boolean() {
        use crate::request_chaining::ChainContext;
        let engine = create_test_engine();
        let context = ChainTemplatingContext::new(ChainContext::new());

        let input = json!(true);
        let result = engine.expand_template_in_json(&input, &context);
        assert_eq!(result, json!(true));
    }

    #[tokio::test]
    async fn test_expand_template_in_json_null() {
        use crate::request_chaining::ChainContext;
        let engine = create_test_engine();
        let context = ChainTemplatingContext::new(ChainContext::new());

        let input = json!(null);
        let result = engine.expand_template_in_json(&input, &context);
        assert_eq!(result, json!(null));
    }

    #[tokio::test]
    async fn test_expand_template_in_json_array() {
        use crate::request_chaining::ChainContext;
        let engine = create_test_engine();
        let context = ChainTemplatingContext::new(ChainContext::new());

        let input = json!(["a", "b", "c"]);
        let result = engine.expand_template_in_json(&input, &context);
        assert_eq!(result, json!(["a", "b", "c"]));
    }

    #[tokio::test]
    async fn test_expand_template_in_json_object() {
        use crate::request_chaining::ChainContext;
        let engine = create_test_engine();
        let context = ChainTemplatingContext::new(ChainContext::new());

        let input = json!({"key": "value", "nested": {"inner": "data"}});
        let result = engine.expand_template_in_json(&input, &context);
        assert_eq!(result, json!({"key": "value", "nested": {"inner": "data"}}));
    }

    // get_chain_history tests
    #[tokio::test]
    async fn test_get_chain_history_empty() {
        let engine = create_test_engine();

        let history = engine.get_chain_history("nonexistent").await;
        assert!(history.is_empty());
    }

    // collect_dependency_level tests
    #[tokio::test]
    async fn test_collect_dependency_level() {
        let engine = create_test_engine();
        let graph = HashMap::new();
        let mut level = vec![];
        let mut processed = HashSet::new();

        engine.collect_dependency_level("req1".to_string(), &graph, &mut level, &mut processed);

        assert_eq!(level, vec!["req1".to_string()]);
        assert!(processed.contains("req1"));
    }

    // Chain execution with non-existent chain
    #[tokio::test]
    async fn test_execute_chain_not_found() {
        let engine = create_test_engine();

        let result = engine.execute_chain("nonexistent", None).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("not found"));
    }

    // Test engine with custom config
    #[tokio::test]
    async fn test_engine_with_custom_config() {
        let registry = Arc::new(RequestChainRegistry::new(ChainConfig::default()));
        let config = ChainConfig {
            enabled: true,
            max_chain_length: 50,
            global_timeout_secs: 60,
            enable_parallel_execution: false,
        };

        let result = ChainExecutionEngine::try_new(registry, config);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_engine_with_default_config() {
        let registry = Arc::new(RequestChainRegistry::new(ChainConfig::default()));
        let config = ChainConfig::default();

        let result = ChainExecutionEngine::try_new(registry, config);
        assert!(result.is_ok());
    }
}