mockforge-sdk 0.3.115

Developer SDK for embedding MockForge in tests and applications
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
//! Response stub configuration

use mockforge_core::ResourceIdExtract as CoreResourceIdExtract;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Type alias for a dynamic response function
pub type DynamicResponseFn = Arc<dyn Fn(&RequestContext) -> Value + Send + Sync>;

/// Request context passed to dynamic response functions
#[derive(Debug, Clone)]
pub struct RequestContext {
    /// HTTP method
    pub method: String,
    /// Request path
    pub path: String,
    /// Path parameters extracted from the URL
    pub path_params: HashMap<String, String>,
    /// Query parameters
    pub query_params: HashMap<String, String>,
    /// Request headers
    pub headers: HashMap<String, String>,
    /// Request body
    pub body: Option<Value>,
}

/// State machine configuration for stateful stub responses
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateMachineConfig {
    /// Resource type identifier (e.g., "order", "user", "payment")
    pub resource_type: String,
    /// Resource ID extraction configuration
    #[serde(flatten)]
    pub resource_id_extract: ResourceIdExtractConfig,
    /// Initial state name
    pub initial_state: String,
    /// State-based response mappings (state name -> response override)
    /// If provided, responses will vary based on current state
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state_responses: Option<HashMap<String, StateResponseOverride>>,
}

/// Resource ID extraction configuration for state machines
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "extract_type", rename_all = "snake_case")]
pub enum ResourceIdExtractConfig {
    /// Extract from path parameter (e.g., "/`orders/{order_id`}" -> extract "`order_id`")
    PathParam {
        /// Path parameter name to extract
        param: String,
    },
    /// Extract from `JSONPath` in request body
    JsonPath {
        /// `JSONPath` expression to extract the resource ID
        path: String,
    },
    /// Extract from header value
    Header {
        /// Header name to extract the resource ID from
        name: String,
    },
    /// Extract from query parameter
    QueryParam {
        /// Query parameter name to extract
        param: String,
    },
}

/// State-based response override
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateResponseOverride {
    /// Optional status code override (if None, uses stub's default status)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<u16>,
    /// Optional body override (if None, uses stub's default body)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<Value>,
    /// Optional headers override (merged with stub's default headers)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<HashMap<String, String>>,
}

/// Fault injection configuration for per-stub error and latency simulation
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct StubFaultInjectionConfig {
    /// Enable fault injection for this stub
    #[serde(default)]
    pub enabled: bool,
    /// HTTP error codes to inject (randomly selected if multiple)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub http_errors: Option<Vec<u16>>,
    /// Probability of injecting HTTP error (0.0-1.0, default: 1.0 if `http_errors` set)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub http_error_probability: Option<f64>,
    /// Inject timeout error (returns 504 Gateway Timeout)
    #[serde(default)]
    pub timeout_error: bool,
    /// Timeout duration in milliseconds (only used if `timeout_error` is true)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout_ms: Option<u64>,
    /// Probability of timeout error (0.0-1.0, default: 1.0 if `timeout_error` is true)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout_probability: Option<f64>,
    /// Inject connection error (returns 503 Service Unavailable)
    #[serde(default)]
    pub connection_error: bool,
    /// Probability of connection error (0.0-1.0, default: 1.0 if `connection_error` is true)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub connection_error_probability: Option<f64>,
}

impl StubFaultInjectionConfig {
    /// Create a simple HTTP error injection config
    #[must_use]
    pub fn http_error(codes: Vec<u16>) -> Self {
        Self {
            enabled: true,
            http_errors: Some(codes),
            http_error_probability: Some(1.0),
            ..Default::default()
        }
    }

    /// Create a timeout error injection config
    #[must_use]
    pub fn timeout(ms: u64) -> Self {
        Self {
            enabled: true,
            timeout_error: true,
            timeout_ms: Some(ms),
            timeout_probability: Some(1.0),
            ..Default::default()
        }
    }

    /// Create a connection error injection config
    #[must_use]
    pub fn connection_error() -> Self {
        Self {
            enabled: true,
            connection_error: true,
            connection_error_probability: Some(1.0),
            ..Default::default()
        }
    }
}

/// A response stub for mocking API endpoints
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseStub {
    /// HTTP method (GET, POST, PUT, DELETE, etc.)
    pub method: String,
    /// Path pattern (supports {{`path_params`}})
    pub path: String,
    /// HTTP status code
    pub status: u16,
    /// Response headers
    pub headers: HashMap<String, String>,
    /// Response body (supports templates like {{uuid}}, {{faker.name}})
    pub body: Value,
    /// Optional latency in milliseconds
    pub latency_ms: Option<u64>,
    /// Optional state machine configuration for stateful behavior
    /// When set, responses will vary based on resource state
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state_machine: Option<StateMachineConfig>,
    /// Optional fault injection configuration for error simulation
    /// When set, can inject errors, timeouts, or connection failures
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fault_injection: Option<StubFaultInjectionConfig>,
}

impl ResponseStub {
    /// Create a new response stub
    pub fn new(method: impl Into<String>, path: impl Into<String>, body: Value) -> Self {
        Self {
            method: method.into(),
            path: path.into(),
            status: 200,
            headers: HashMap::new(),
            body,
            latency_ms: None,
            state_machine: None,
            fault_injection: None,
        }
    }

    /// Set the HTTP status code
    #[must_use]
    pub const fn status(mut self, status: u16) -> Self {
        self.status = status;
        self
    }

    /// Add a response header
    #[must_use]
    pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(key.into(), value.into());
        self
    }

    /// Set response latency in milliseconds
    #[must_use]
    pub const fn latency(mut self, ms: u64) -> Self {
        self.latency_ms = Some(ms);
        self
    }

    /// Set state machine configuration for stateful behavior
    #[must_use]
    pub fn with_state_machine(mut self, config: StateMachineConfig) -> Self {
        self.state_machine = Some(config);
        self
    }

    /// Check if this stub has state machine configuration
    #[must_use]
    pub const fn has_state_machine(&self) -> bool {
        self.state_machine.is_some()
    }

    /// Get state machine configuration
    #[must_use]
    pub const fn state_machine(&self) -> Option<&StateMachineConfig> {
        self.state_machine.as_ref()
    }

    /// Apply state-based response override if state machine is configured
    ///
    /// This method checks if the stub has state machine configuration and applies
    /// state-based response overrides based on the current state.
    ///
    /// Returns a modified stub with state-specific overrides applied, or the original
    /// stub if no state machine config or no override for current state.
    #[must_use]
    pub fn apply_state_override(&self, current_state: &str) -> Self {
        let mut stub = self.clone();

        if let Some(ref state_machine) = self.state_machine {
            if let Some(ref state_responses) = state_machine.state_responses {
                if let Some(override_config) = state_responses.get(current_state) {
                    // Apply status override
                    if let Some(status) = override_config.status {
                        stub.status = status;
                    }

                    // Apply body override
                    if let Some(ref body) = override_config.body {
                        stub.body = body.clone();
                    }

                    // Merge headers
                    if let Some(ref headers) = override_config.headers {
                        for (key, value) in headers {
                            stub.headers.insert(key.clone(), value.clone());
                        }
                    }
                }
            }
        }

        stub
    }

    /// Set fault injection configuration
    #[must_use]
    pub fn with_fault_injection(mut self, config: StubFaultInjectionConfig) -> Self {
        self.fault_injection = Some(config);
        self
    }

    /// Check if this stub has fault injection configured
    #[must_use]
    pub fn has_fault_injection(&self) -> bool {
        self.fault_injection.as_ref().is_some_and(|f| f.enabled)
    }

    /// Get fault injection configuration
    #[must_use]
    pub const fn fault_injection(&self) -> Option<&StubFaultInjectionConfig> {
        self.fault_injection.as_ref()
    }
}

impl ResourceIdExtractConfig {
    /// Convert to core's `ResourceIdExtract` enum
    #[must_use]
    pub fn to_core(&self) -> CoreResourceIdExtract {
        match self {
            Self::PathParam { param } => CoreResourceIdExtract::PathParam {
                param: param.clone(),
            },
            Self::JsonPath { path } => CoreResourceIdExtract::JsonPath { path: path.clone() },
            Self::Header { name } => CoreResourceIdExtract::Header { name: name.clone() },
            Self::QueryParam { param } => CoreResourceIdExtract::QueryParam {
                param: param.clone(),
            },
        }
    }
}

/// Dynamic stub with runtime response generation
pub struct DynamicStub {
    /// HTTP method
    pub method: String,
    /// Path pattern
    pub path: String,
    /// HTTP status code (can be dynamic)
    pub status: Arc<RwLock<u16>>,
    /// Response headers (can be dynamic)
    pub headers: Arc<RwLock<HashMap<String, String>>>,
    /// Dynamic response function
    pub response_fn: DynamicResponseFn,
    /// Optional latency in milliseconds
    pub latency_ms: Option<u64>,
}

impl DynamicStub {
    /// Create a new dynamic stub
    pub fn new<F>(method: impl Into<String>, path: impl Into<String>, response_fn: F) -> Self
    where
        F: Fn(&RequestContext) -> Value + Send + Sync + 'static,
    {
        Self {
            method: method.into(),
            path: path.into(),
            status: Arc::new(RwLock::new(200)),
            headers: Arc::new(RwLock::new(HashMap::new())),
            response_fn: Arc::new(response_fn),
            latency_ms: None,
        }
    }

    /// Set the HTTP status code
    pub async fn set_status(&self, status: u16) {
        *self.status.write().await = status;
    }

    /// Get the current status code
    pub async fn get_status(&self) -> u16 {
        *self.status.read().await
    }

    /// Add a response header
    pub async fn add_header(&self, key: String, value: String) {
        self.headers.write().await.insert(key, value);
    }

    /// Remove a response header
    pub async fn remove_header(&self, key: &str) {
        self.headers.write().await.remove(key);
    }

    /// Get all headers (returns a clone)
    ///
    /// For more efficient read-only access, consider using `with_headers()` instead.
    pub async fn get_headers(&self) -> HashMap<String, String> {
        self.headers.read().await.clone()
    }

    /// Access headers without cloning via a callback
    ///
    /// This is more efficient than `get_headers()` when you only need to
    /// read header values without modifying them.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use mockforge_sdk::DynamicStub;
    /// # use serde_json::json;
    /// # async fn example() {
    /// let stub = DynamicStub::new("GET", "/test", |_| json!({}));
    /// stub.add_header("X-Custom".to_string(), "value".to_string()).await;
    ///
    /// // Efficient read-only access
    /// let has_custom = stub.with_headers(|headers| {
    ///     headers.contains_key("X-Custom")
    /// }).await;
    /// # }
    /// ```
    pub async fn with_headers<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&HashMap<String, String>) -> R,
    {
        let headers = self.headers.read().await;
        f(&headers)
    }

    /// Generate a response for a given request context
    #[must_use]
    pub fn generate_response(&self, ctx: &RequestContext) -> Value {
        (self.response_fn)(ctx)
    }

    /// Set latency
    #[must_use]
    pub const fn with_latency(mut self, ms: u64) -> Self {
        self.latency_ms = Some(ms);
        self
    }
}

/// Builder for creating `ResponseStub` instances with a fluent API
///
/// Provides a convenient way to construct response stubs with method chaining.
///
/// # Examples
///
/// ```rust
/// use mockforge_sdk::StubBuilder;
/// use serde_json::json;
///
/// let stub = StubBuilder::new("GET", "/api/users")
///     .status(200)
///     .header("Content-Type", "application/json")
///     .body(json!({"users": []}))
///     .latency(100)
///     .build();
/// ```
pub struct StubBuilder {
    method: String,
    path: String,
    status: u16,
    headers: HashMap<String, String>,
    body: Option<Value>,
    latency_ms: Option<u64>,
    state_machine: Option<StateMachineConfig>,
    fault_injection: Option<StubFaultInjectionConfig>,
}

impl StubBuilder {
    /// Create a new stub builder
    ///
    /// # Arguments
    /// * `method` - HTTP method (GET, POST, PUT, DELETE, etc.)
    /// * `path` - URL path pattern
    pub fn new(method: impl Into<String>, path: impl Into<String>) -> Self {
        Self {
            method: method.into(),
            path: path.into(),
            status: 200,
            headers: HashMap::new(),
            body: None,
            latency_ms: None,
            state_machine: None,
            fault_injection: None,
        }
    }

    /// Set the HTTP status code
    #[must_use]
    pub const fn status(mut self, status: u16) -> Self {
        self.status = status;
        self
    }

    /// Add a response header
    #[must_use]
    pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(key.into(), value.into());
        self
    }

    /// Set the response body
    #[must_use]
    pub fn body(mut self, body: Value) -> Self {
        self.body = Some(body);
        self
    }

    /// Set response latency in milliseconds
    #[must_use]
    pub const fn latency(mut self, ms: u64) -> Self {
        self.latency_ms = Some(ms);
        self
    }

    /// Set state machine configuration
    #[must_use]
    pub fn state_machine(mut self, config: StateMachineConfig) -> Self {
        self.state_machine = Some(config);
        self
    }

    /// Set fault injection configuration
    #[must_use]
    pub fn fault_injection(mut self, config: StubFaultInjectionConfig) -> Self {
        self.fault_injection = Some(config);
        self
    }

    /// Build the response stub
    #[must_use]
    pub fn build(self) -> ResponseStub {
        ResponseStub {
            method: self.method,
            path: self.path,
            status: self.status,
            headers: self.headers,
            body: self.body.unwrap_or(Value::Null),
            latency_ms: self.latency_ms,
            state_machine: self.state_machine,
            fault_injection: self.fault_injection,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // ==================== RequestContext Tests ====================

    #[test]
    fn test_request_context_creation() {
        let ctx = RequestContext {
            method: "GET".to_string(),
            path: "/api/users".to_string(),
            path_params: HashMap::from([("id".to_string(), "123".to_string())]),
            query_params: HashMap::from([("page".to_string(), "1".to_string())]),
            headers: HashMap::from([("content-type".to_string(), "application/json".to_string())]),
            body: Some(json!({"name": "test"})),
        };

        assert_eq!(ctx.method, "GET");
        assert_eq!(ctx.path, "/api/users");
        assert_eq!(ctx.path_params.get("id"), Some(&"123".to_string()));
    }

    #[test]
    fn test_request_context_clone() {
        let ctx = RequestContext {
            method: "POST".to_string(),
            path: "/api/users".to_string(),
            path_params: HashMap::new(),
            query_params: HashMap::new(),
            headers: HashMap::new(),
            body: None,
        };

        let cloned = ctx.clone();
        assert_eq!(ctx.method, cloned.method);
        assert_eq!(ctx.path, cloned.path);
    }

    // ==================== StateMachineConfig Tests ====================

    #[test]
    fn test_state_machine_config_serialize() {
        let config = StateMachineConfig {
            resource_type: "order".to_string(),
            resource_id_extract: ResourceIdExtractConfig::PathParam {
                param: "order_id".to_string(),
            },
            initial_state: "pending".to_string(),
            state_responses: None,
        };

        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("order"));
        assert!(json.contains("pending"));
    }

    #[test]
    fn test_state_machine_config_with_responses() {
        let mut responses = HashMap::new();
        responses.insert(
            "confirmed".to_string(),
            StateResponseOverride {
                status: Some(200),
                body: Some(json!({"status": "confirmed"})),
                headers: None,
            },
        );

        let config = StateMachineConfig {
            resource_type: "order".to_string(),
            resource_id_extract: ResourceIdExtractConfig::PathParam {
                param: "order_id".to_string(),
            },
            initial_state: "pending".to_string(),
            state_responses: Some(responses),
        };

        assert!(config.state_responses.is_some());
    }

    // ==================== ResourceIdExtractConfig Tests ====================

    #[test]
    fn test_resource_id_extract_path_param() {
        let config = ResourceIdExtractConfig::PathParam {
            param: "user_id".to_string(),
        };
        let core = config.to_core();
        match core {
            CoreResourceIdExtract::PathParam { param } => assert_eq!(param, "user_id"),
            _ => panic!("Expected PathParam"),
        }
    }

    #[test]
    fn test_resource_id_extract_json_path() {
        let config = ResourceIdExtractConfig::JsonPath {
            path: "$.data.id".to_string(),
        };
        let core = config.to_core();
        match core {
            CoreResourceIdExtract::JsonPath { path } => assert_eq!(path, "$.data.id"),
            _ => panic!("Expected JsonPath"),
        }
    }

    #[test]
    fn test_resource_id_extract_header() {
        let config = ResourceIdExtractConfig::Header {
            name: "X-Resource-Id".to_string(),
        };
        let core = config.to_core();
        match core {
            CoreResourceIdExtract::Header { name } => assert_eq!(name, "X-Resource-Id"),
            _ => panic!("Expected Header"),
        }
    }

    #[test]
    fn test_resource_id_extract_query_param() {
        let config = ResourceIdExtractConfig::QueryParam {
            param: "id".to_string(),
        };
        let core = config.to_core();
        match core {
            CoreResourceIdExtract::QueryParam { param } => assert_eq!(param, "id"),
            _ => panic!("Expected QueryParam"),
        }
    }

    // ==================== StateResponseOverride Tests ====================

    #[test]
    fn test_state_response_override_status_only() {
        let override_config = StateResponseOverride {
            status: Some(404),
            body: None,
            headers: None,
        };
        assert_eq!(override_config.status, Some(404));
    }

    #[test]
    fn test_state_response_override_full() {
        let mut headers = HashMap::new();
        headers.insert("X-Custom".to_string(), "value".to_string());

        let override_config = StateResponseOverride {
            status: Some(200),
            body: Some(json!({"data": "test"})),
            headers: Some(headers),
        };

        assert_eq!(override_config.status, Some(200));
        assert!(override_config.body.is_some());
        assert!(override_config.headers.is_some());
    }

    // ==================== StubFaultInjectionConfig Tests ====================

    #[test]
    fn test_stub_fault_injection_default() {
        let config = StubFaultInjectionConfig::default();
        assert!(!config.enabled);
        assert!(config.http_errors.is_none());
        assert!(!config.timeout_error);
        assert!(!config.connection_error);
    }

    #[test]
    fn test_stub_fault_injection_http_error() {
        let config = StubFaultInjectionConfig::http_error(vec![500, 502, 503]);
        assert!(config.enabled);
        assert_eq!(config.http_errors, Some(vec![500, 502, 503]));
        assert_eq!(config.http_error_probability, Some(1.0));
    }

    #[test]
    fn test_stub_fault_injection_timeout() {
        let config = StubFaultInjectionConfig::timeout(5000);
        assert!(config.enabled);
        assert!(config.timeout_error);
        assert_eq!(config.timeout_ms, Some(5000));
        assert_eq!(config.timeout_probability, Some(1.0));
    }

    #[test]
    fn test_stub_fault_injection_connection_error() {
        let config = StubFaultInjectionConfig::connection_error();
        assert!(config.enabled);
        assert!(config.connection_error);
        assert_eq!(config.connection_error_probability, Some(1.0));
    }

    // ==================== ResponseStub Tests ====================

    #[test]
    fn test_response_stub_new() {
        let stub = ResponseStub::new("GET", "/api/users", json!({"users": []}));
        assert_eq!(stub.method, "GET");
        assert_eq!(stub.path, "/api/users");
        assert_eq!(stub.status, 200);
        assert!(stub.headers.is_empty());
        assert!(stub.latency_ms.is_none());
    }

    #[test]
    fn test_response_stub_status() {
        let stub = ResponseStub::new("GET", "/api/users", json!({})).status(404);
        assert_eq!(stub.status, 404);
    }

    #[test]
    fn test_response_stub_header() {
        let stub = ResponseStub::new("GET", "/api/users", json!({}))
            .header("Content-Type", "application/json")
            .header("X-Custom", "value");

        assert_eq!(stub.headers.get("Content-Type"), Some(&"application/json".to_string()));
        assert_eq!(stub.headers.get("X-Custom"), Some(&"value".to_string()));
    }

    #[test]
    fn test_response_stub_latency() {
        let stub = ResponseStub::new("GET", "/api/users", json!({})).latency(100);
        assert_eq!(stub.latency_ms, Some(100));
    }

    #[test]
    fn test_response_stub_with_state_machine() {
        let state_config = StateMachineConfig {
            resource_type: "user".to_string(),
            resource_id_extract: ResourceIdExtractConfig::PathParam {
                param: "user_id".to_string(),
            },
            initial_state: "active".to_string(),
            state_responses: None,
        };

        let stub = ResponseStub::new("GET", "/api/users/{user_id}", json!({}))
            .with_state_machine(state_config);

        assert!(stub.has_state_machine());
        assert!(stub.state_machine().is_some());
    }

    #[test]
    fn test_response_stub_no_state_machine() {
        let stub = ResponseStub::new("GET", "/api/users", json!({}));
        assert!(!stub.has_state_machine());
        assert!(stub.state_machine().is_none());
    }

    #[test]
    fn test_response_stub_apply_state_override() {
        let mut state_responses = HashMap::new();
        state_responses.insert(
            "inactive".to_string(),
            StateResponseOverride {
                status: Some(403),
                body: Some(json!({"error": "User is inactive"})),
                headers: Some(HashMap::from([("X-State".to_string(), "inactive".to_string())])),
            },
        );

        let state_config = StateMachineConfig {
            resource_type: "user".to_string(),
            resource_id_extract: ResourceIdExtractConfig::PathParam {
                param: "user_id".to_string(),
            },
            initial_state: "active".to_string(),
            state_responses: Some(state_responses),
        };

        let stub = ResponseStub::new("GET", "/api/users/{user_id}", json!({"status": "ok"}))
            .with_state_machine(state_config);

        // Apply inactive state override
        let overridden = stub.apply_state_override("inactive");
        assert_eq!(overridden.status, 403);
        assert_eq!(overridden.body, json!({"error": "User is inactive"}));
        assert_eq!(overridden.headers.get("X-State"), Some(&"inactive".to_string()));
    }

    #[test]
    fn test_response_stub_apply_state_override_no_match() {
        let state_config = StateMachineConfig {
            resource_type: "user".to_string(),
            resource_id_extract: ResourceIdExtractConfig::PathParam {
                param: "user_id".to_string(),
            },
            initial_state: "active".to_string(),
            state_responses: None,
        };

        let stub = ResponseStub::new("GET", "/api/users/{user_id}", json!({"original": true}))
            .status(200)
            .with_state_machine(state_config);

        // State override with no matching state should return original
        let overridden = stub.apply_state_override("unknown");
        assert_eq!(overridden.status, 200);
        assert_eq!(overridden.body, json!({"original": true}));
    }

    #[test]
    fn test_response_stub_with_fault_injection() {
        let fault_config = StubFaultInjectionConfig::http_error(vec![500]);
        let stub =
            ResponseStub::new("GET", "/api/users", json!({})).with_fault_injection(fault_config);

        assert!(stub.has_fault_injection());
        assert!(stub.fault_injection().is_some());
    }

    #[test]
    fn test_response_stub_no_fault_injection() {
        let stub = ResponseStub::new("GET", "/api/users", json!({}));
        assert!(!stub.has_fault_injection());
    }

    #[test]
    fn test_response_stub_serialize() {
        let stub = ResponseStub::new("POST", "/api/orders", json!({"id": 1}))
            .status(201)
            .header("Location", "/api/orders/1")
            .latency(50);

        let json = serde_json::to_string(&stub).unwrap();
        assert!(json.contains("POST"));
        assert!(json.contains("/api/orders"));
        assert!(json.contains("201"));
    }

    // ==================== DynamicStub Tests ====================

    #[test]
    fn test_dynamic_stub_new() {
        let stub = DynamicStub::new("GET", "/api/users", |ctx| json!({"path": ctx.path.clone()}));

        assert_eq!(stub.method, "GET");
        assert_eq!(stub.path, "/api/users");
    }

    #[tokio::test]
    async fn test_dynamic_stub_status() {
        let stub = DynamicStub::new("GET", "/test", |_| json!({}));
        assert_eq!(stub.get_status().await, 200);

        stub.set_status(404).await;
        assert_eq!(stub.get_status().await, 404);
    }

    #[tokio::test]
    async fn test_dynamic_stub_headers() {
        let stub = DynamicStub::new("GET", "/test", |_| json!({}));

        stub.add_header("X-Custom".to_string(), "value".to_string()).await;

        let headers = stub.get_headers().await;
        assert_eq!(headers.get("X-Custom"), Some(&"value".to_string()));

        stub.remove_header("X-Custom").await;
        let headers = stub.get_headers().await;
        assert!(!headers.contains_key("X-Custom"));
    }

    #[tokio::test]
    async fn test_dynamic_stub_with_headers() {
        let stub = DynamicStub::new("GET", "/test", |_| json!({}));
        stub.add_header("X-Test".to_string(), "test-value".to_string()).await;

        let has_header = stub.with_headers(|headers| headers.contains_key("X-Test")).await;
        assert!(has_header);
    }

    #[test]
    fn test_dynamic_stub_generate_response() {
        let stub = DynamicStub::new("GET", "/api/users/{id}", |ctx| {
            let id = ctx.path_params.get("id").cloned().unwrap_or_default();
            json!({"user_id": id})
        });

        let ctx = RequestContext {
            method: "GET".to_string(),
            path: "/api/users/123".to_string(),
            path_params: HashMap::from([("id".to_string(), "123".to_string())]),
            query_params: HashMap::new(),
            headers: HashMap::new(),
            body: None,
        };

        let response = stub.generate_response(&ctx);
        assert_eq!(response, json!({"user_id": "123"}));
    }

    #[test]
    fn test_dynamic_stub_with_latency() {
        let stub = DynamicStub::new("GET", "/test", |_| json!({})).with_latency(100);
        assert_eq!(stub.latency_ms, Some(100));
    }

    // ==================== StubBuilder Tests ====================

    #[test]
    fn test_stub_builder_basic() {
        let stub = StubBuilder::new("GET", "/api/users").body(json!({"users": []})).build();

        assert_eq!(stub.method, "GET");
        assert_eq!(stub.path, "/api/users");
        assert_eq!(stub.status, 200);
    }

    #[test]
    fn test_stub_builder_status() {
        let stub = StubBuilder::new("GET", "/api/users").status(404).build();

        assert_eq!(stub.status, 404);
    }

    #[test]
    fn test_stub_builder_headers() {
        let stub = StubBuilder::new("GET", "/api/users")
            .header("Content-Type", "application/json")
            .header("X-Custom", "value")
            .build();

        assert_eq!(stub.headers.len(), 2);
    }

    #[test]
    fn test_stub_builder_latency() {
        let stub = StubBuilder::new("GET", "/api/users").latency(500).build();

        assert_eq!(stub.latency_ms, Some(500));
    }

    #[test]
    fn test_stub_builder_state_machine() {
        let config = StateMachineConfig {
            resource_type: "order".to_string(),
            resource_id_extract: ResourceIdExtractConfig::PathParam {
                param: "order_id".to_string(),
            },
            initial_state: "pending".to_string(),
            state_responses: None,
        };

        let stub = StubBuilder::new("GET", "/api/orders/{order_id}").state_machine(config).build();

        assert!(stub.state_machine.is_some());
    }

    #[test]
    fn test_stub_builder_fault_injection() {
        let fault = StubFaultInjectionConfig::http_error(vec![500]);

        let stub = StubBuilder::new("GET", "/api/users").fault_injection(fault).build();

        assert!(stub.fault_injection.is_some());
    }

    #[test]
    fn test_stub_builder_full_chain() {
        let stub = StubBuilder::new("POST", "/api/orders")
            .status(201)
            .header("Location", "/api/orders/1")
            .body(json!({"id": 1, "status": "created"}))
            .latency(100)
            .build();

        assert_eq!(stub.method, "POST");
        assert_eq!(stub.path, "/api/orders");
        assert_eq!(stub.status, 201);
        assert_eq!(stub.headers.get("Location"), Some(&"/api/orders/1".to_string()));
        assert_eq!(stub.latency_ms, Some(100));
    }
}