mockforge-kafka 0.3.97

Kafka protocol support for MockForge
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
use chrono::Utc;
use serde::{Deserialize, Serialize};
use serde_yaml;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::time::{interval, Duration};

/// Kafka fixture for message generation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KafkaFixture {
    pub identifier: String,
    pub name: String,
    pub topic: String,
    pub partition: Option<i32>,      // None = all partitions
    pub key_pattern: Option<String>, // Template
    pub value_template: serde_json::Value,
    pub headers: HashMap<String, String>,
    pub auto_produce: Option<AutoProduceConfig>,
}

/// Configuration for auto-producing messages
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutoProduceConfig {
    pub enabled: bool,
    pub rate_per_second: u64,
    pub duration_seconds: Option<u64>,
    pub total_count: Option<usize>,
}

/// Auto-producer for fixtures
pub struct AutoProducer {
    fixtures: Arc<RwLock<HashMap<String, KafkaFixture>>>,
    template_engine: mockforge_core::templating::TemplateEngine,
    broker: Arc<super::broker::KafkaMockBroker>,
}

impl AutoProducer {
    /// Create a new auto-producer
    pub fn new(
        broker: Arc<super::broker::KafkaMockBroker>,
        template_engine: mockforge_core::templating::TemplateEngine,
    ) -> Self {
        Self {
            fixtures: Arc::new(RwLock::new(HashMap::new())),
            template_engine,
            broker,
        }
    }

    /// Add a fixture for auto-production
    pub async fn add_fixture(&self, fixture: KafkaFixture) {
        if fixture.auto_produce.as_ref().is_some_and(|ap| ap.enabled) {
            let fixture_id = fixture.identifier.clone();
            self.fixtures.write().await.insert(fixture_id, fixture);
        }
    }

    /// Start auto-producing messages
    pub async fn start(&self) -> anyhow::Result<()> {
        let fixtures = self.fixtures.clone();
        let _template_engine = self.template_engine.clone();
        let _broker = self.broker.clone();

        tokio::spawn(async move {
            let mut interval = interval(Duration::from_secs(1));

            loop {
                interval.tick().await;

                let fixtures_read = fixtures.read().await.clone();
                for fixture in fixtures_read.values() {
                    if let Some(auto_produce) = &fixture.auto_produce {
                        if auto_produce.enabled {
                            // Generate and produce messages
                            for _ in 0..auto_produce.rate_per_second {
                                if let Ok(message) = fixture.generate_message(&HashMap::new()) {
                                    // Produce the message to the broker
                                    let mut topics = _broker.topics.write().await;
                                    if let Some(topic) = topics.get_mut(&fixture.topic) {
                                        let partition = fixture.partition.unwrap_or_else(|| {
                                            topic.assign_partition(message.key.as_deref())
                                        });

                                        if let Err(e) = topic.produce(partition, message).await {
                                            tracing::error!(
                                                "Failed to produce message to topic {}: {}",
                                                fixture.topic,
                                                e
                                            );
                                        } else {
                                            tracing::debug!(
                                                "Auto-produced message to topic {} partition {}",
                                                fixture.topic,
                                                partition
                                            );
                                        }
                                    } else {
                                        tracing::warn!(
                                            "Topic {} not found for auto-production",
                                            fixture.topic
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        });

        Ok(())
    }

    /// Stop auto-producing for a specific fixture
    pub async fn stop_fixture(&self, fixture_id: &str) {
        if let Some(fixture) = self.fixtures.write().await.get_mut(fixture_id) {
            if let Some(auto_produce) = &mut fixture.auto_produce {
                auto_produce.enabled = false;
            }
        }
    }
}

impl KafkaFixture {
    /// Load fixtures from a directory
    pub fn load_from_dir(dir: &PathBuf) -> mockforge_core::Result<Vec<Self>> {
        let mut fixtures = Vec::new();
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) == Some("yaml")
                || path.extension().and_then(|s| s.to_str()) == Some("yml")
            {
                let file = fs::File::open(&path)?;
                let file_fixtures: Vec<Self> = serde_yaml::from_reader(file)?;
                fixtures.extend(file_fixtures);
            }
        }
        Ok(fixtures)
    }

    /// Generate a message using the fixture
    pub fn generate_message(
        &self,
        context: &HashMap<String, String>,
    ) -> mockforge_core::Result<crate::partitions::KafkaMessage> {
        // Render key if pattern provided
        let key = self.key_pattern.as_ref().map(|pattern| self.render_template(pattern, context));

        // Render value template
        let value_str = serde_json::to_string(&self.value_template)?;
        let value_rendered = self.render_template(&value_str, context);
        let value = value_rendered.into_bytes();

        // Render headers
        let headers = self
            .headers
            .iter()
            .map(|(k, v)| (k.clone(), self.render_template(v, context).into_bytes()))
            .collect();

        Ok(crate::partitions::KafkaMessage {
            offset: 0,
            timestamp: Utc::now().timestamp_millis(),
            key: key.map(|k| k.into_bytes()),
            value,
            headers,
        })
    }

    fn render_template(&self, template: &str, context: &HashMap<String, String>) -> String {
        let mut result = template.to_string();
        for (key, value) in context {
            result = result.replace(&format!("{{{{{}}}}}", key), value);
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use tempfile::TempDir;

    // ==================== KafkaFixture Tests ====================

    #[test]
    fn test_kafka_fixture_creation() {
        let fixture = KafkaFixture {
            identifier: "test-fixture".to_string(),
            name: "Test Fixture".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: Some("key-{{id}}".to_string()),
            value_template: serde_json::json!({"message": "test"}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        assert_eq!(fixture.identifier, "test-fixture");
        assert_eq!(fixture.topic, "test-topic");
        assert_eq!(fixture.partition, Some(0));
        assert!(fixture.auto_produce.is_none());
    }

    #[test]
    fn test_kafka_fixture_with_auto_produce() {
        let auto_produce = AutoProduceConfig {
            enabled: true,
            rate_per_second: 10,
            duration_seconds: Some(60),
            total_count: Some(100),
        };

        let fixture = KafkaFixture {
            identifier: "auto-fixture".to_string(),
            name: "Auto Fixture".to_string(),
            topic: "auto-topic".to_string(),
            partition: None,
            key_pattern: None,
            value_template: serde_json::json!({"auto": true}),
            headers: HashMap::new(),
            auto_produce: Some(auto_produce),
        };

        assert!(fixture.auto_produce.is_some());
        let ap = fixture.auto_produce.as_ref().unwrap();
        assert!(ap.enabled);
        assert_eq!(ap.rate_per_second, 10);
        assert_eq!(ap.duration_seconds, Some(60));
        assert_eq!(ap.total_count, Some(100));
    }

    #[test]
    fn test_kafka_fixture_clone() {
        let fixture = KafkaFixture {
            identifier: "clone-test".to_string(),
            name: "Clone Test".to_string(),
            topic: "clone-topic".to_string(),
            partition: Some(1),
            key_pattern: Some("key".to_string()),
            value_template: serde_json::json!({"data": "value"}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let cloned = fixture.clone();
        assert_eq!(fixture.identifier, cloned.identifier);
        assert_eq!(fixture.topic, cloned.topic);
        assert_eq!(fixture.partition, cloned.partition);
    }

    #[test]
    fn test_kafka_fixture_serialize_deserialize() {
        let fixture = KafkaFixture {
            identifier: "serde-test".to_string(),
            name: "Serde Test".to_string(),
            topic: "serde-topic".to_string(),
            partition: Some(0),
            key_pattern: Some("key-pattern".to_string()),
            value_template: serde_json::json!({"test": "data"}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let yaml = serde_yaml::to_string(&fixture).unwrap();
        let deserialized: KafkaFixture = serde_yaml::from_str(&yaml).unwrap();

        assert_eq!(fixture.identifier, deserialized.identifier);
        assert_eq!(fixture.topic, deserialized.topic);
    }

    // ==================== AutoProduceConfig Tests ====================

    #[test]
    fn test_auto_produce_config_enabled() {
        let config = AutoProduceConfig {
            enabled: true,
            rate_per_second: 5,
            duration_seconds: None,
            total_count: None,
        };

        assert!(config.enabled);
        assert_eq!(config.rate_per_second, 5);
        assert!(config.duration_seconds.is_none());
        assert!(config.total_count.is_none());
    }

    #[test]
    fn test_auto_produce_config_disabled() {
        let config = AutoProduceConfig {
            enabled: false,
            rate_per_second: 0,
            duration_seconds: None,
            total_count: None,
        };

        assert!(!config.enabled);
    }

    #[test]
    fn test_auto_produce_config_with_limits() {
        let config = AutoProduceConfig {
            enabled: true,
            rate_per_second: 100,
            duration_seconds: Some(300),
            total_count: Some(10000),
        };

        assert_eq!(config.rate_per_second, 100);
        assert_eq!(config.duration_seconds, Some(300));
        assert_eq!(config.total_count, Some(10000));
    }

    #[test]
    fn test_auto_produce_config_clone() {
        let config = AutoProduceConfig {
            enabled: true,
            rate_per_second: 10,
            duration_seconds: Some(60),
            total_count: Some(100),
        };

        let cloned = config.clone();
        assert_eq!(config.enabled, cloned.enabled);
        assert_eq!(config.rate_per_second, cloned.rate_per_second);
        assert_eq!(config.duration_seconds, cloned.duration_seconds);
        assert_eq!(config.total_count, cloned.total_count);
    }

    // ==================== KafkaFixture::generate_message Tests ====================

    #[test]
    fn test_generate_message_basic() {
        let fixture = KafkaFixture {
            identifier: "msg-test".to_string(),
            name: "Message Test".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: None,
            value_template: serde_json::json!({"message": "hello"}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let context = HashMap::new();
        let message = fixture.generate_message(&context).unwrap();

        assert!(message.key.is_none());
        assert!(!message.value.is_empty());
        assert_eq!(message.offset, 0);
        assert!(message.timestamp > 0);
    }

    #[test]
    fn test_generate_message_with_key() {
        let fixture = KafkaFixture {
            identifier: "key-test".to_string(),
            name: "Key Test".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: Some("order-12345".to_string()),
            value_template: serde_json::json!({"order": "data"}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let context = HashMap::new();
        let message = fixture.generate_message(&context).unwrap();

        assert!(message.key.is_some());
        assert_eq!(message.key.unwrap(), b"order-12345".to_vec());
    }

    #[test]
    fn test_generate_message_with_template_substitution() {
        let fixture = KafkaFixture {
            identifier: "template-test".to_string(),
            name: "Template Test".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: Some("user-{{userId}}".to_string()),
            value_template: serde_json::json!({"userId": "{{userId}}", "action": "login"}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let mut context = HashMap::new();
        context.insert("userId".to_string(), "123".to_string());

        let message = fixture.generate_message(&context).unwrap();

        assert!(message.key.is_some());
        assert_eq!(message.key.unwrap(), b"user-123".to_vec());

        let value_str = String::from_utf8(message.value).unwrap();
        assert!(value_str.contains("123"));
    }

    #[test]
    fn test_generate_message_with_headers() {
        let mut headers = HashMap::new();
        headers.insert("correlation-id".to_string(), "abc-123".to_string());
        headers.insert("source".to_string(), "test-service".to_string());

        let fixture = KafkaFixture {
            identifier: "header-test".to_string(),
            name: "Header Test".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: None,
            value_template: serde_json::json!({"data": "test"}),
            headers,
            auto_produce: None,
        };

        let context = HashMap::new();
        let message = fixture.generate_message(&context).unwrap();

        assert_eq!(message.headers.len(), 2);
        assert!(message.headers.iter().any(|(k, _)| k == "correlation-id"));
        assert!(message.headers.iter().any(|(k, _)| k == "source"));
    }

    #[test]
    fn test_generate_message_with_template_headers() {
        let mut headers = HashMap::new();
        headers.insert("trace-id".to_string(), "trace-{{traceId}}".to_string());

        let fixture = KafkaFixture {
            identifier: "header-template-test".to_string(),
            name: "Header Template Test".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: None,
            value_template: serde_json::json!({"data": "test"}),
            headers,
            auto_produce: None,
        };

        let mut context = HashMap::new();
        context.insert("traceId".to_string(), "xyz789".to_string());

        let message = fixture.generate_message(&context).unwrap();

        let trace_header = message.headers.iter().find(|(k, _)| k == "trace-id");
        assert!(trace_header.is_some());
        assert_eq!(trace_header.unwrap().1, b"trace-xyz789".to_vec());
    }

    #[test]
    fn test_generate_message_empty_context() {
        let fixture = KafkaFixture {
            identifier: "empty-context".to_string(),
            name: "Empty Context".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: Some("static-key".to_string()),
            value_template: serde_json::json!({"static": "value"}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let context = HashMap::new();
        let message = fixture.generate_message(&context).unwrap();

        assert!(message.key.is_some());
        assert_eq!(message.key.unwrap(), b"static-key".to_vec());
    }

    // ==================== KafkaFixture::render_template Tests ====================

    #[test]
    fn test_render_template_no_substitution() {
        let fixture = KafkaFixture {
            identifier: "render-test".to_string(),
            name: "Render Test".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: None,
            value_template: serde_json::json!({}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let context = HashMap::new();
        let result = fixture.render_template("static text", &context);
        assert_eq!(result, "static text");
    }

    #[test]
    fn test_render_template_single_substitution() {
        let fixture = KafkaFixture {
            identifier: "render-test".to_string(),
            name: "Render Test".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: None,
            value_template: serde_json::json!({}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let mut context = HashMap::new();
        context.insert("name".to_string(), "Alice".to_string());

        let result = fixture.render_template("Hello {{name}}", &context);
        assert_eq!(result, "Hello Alice");
    }

    #[test]
    fn test_render_template_multiple_substitutions() {
        let fixture = KafkaFixture {
            identifier: "render-test".to_string(),
            name: "Render Test".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: None,
            value_template: serde_json::json!({}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let mut context = HashMap::new();
        context.insert("first".to_string(), "John".to_string());
        context.insert("last".to_string(), "Doe".to_string());

        let result = fixture.render_template("{{first}} {{last}}", &context);
        assert_eq!(result, "John Doe");
    }

    #[test]
    fn test_render_template_missing_variable() {
        let fixture = KafkaFixture {
            identifier: "render-test".to_string(),
            name: "Render Test".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: None,
            value_template: serde_json::json!({}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        let context = HashMap::new();
        let result = fixture.render_template("Hello {{name}}", &context);
        // Missing variables are left as-is
        assert_eq!(result, "Hello {{name}}");
    }

    // ==================== KafkaFixture::load_from_dir Tests ====================

    #[test]
    fn test_load_from_dir_empty_directory() {
        let temp_dir = TempDir::new().unwrap();
        let result = KafkaFixture::load_from_dir(&temp_dir.path().to_path_buf()).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_load_from_dir_with_yaml_files() {
        let temp_dir = TempDir::new().unwrap();
        let fixture_path = temp_dir.path().join("fixtures.yaml");

        let fixtures = vec![KafkaFixture {
            identifier: "test-fixture".to_string(),
            name: "Test Fixture".to_string(),
            topic: "test-topic".to_string(),
            partition: Some(0),
            key_pattern: None,
            value_template: serde_json::json!({"test": "data"}),
            headers: HashMap::new(),
            auto_produce: None,
        }];

        let yaml_content = serde_yaml::to_string(&fixtures).unwrap();
        fs::write(&fixture_path, yaml_content).unwrap();

        let loaded = KafkaFixture::load_from_dir(&temp_dir.path().to_path_buf()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].identifier, "test-fixture");
    }

    #[test]
    fn test_load_from_dir_with_yml_extension() {
        let temp_dir = TempDir::new().unwrap();
        let fixture_path = temp_dir.path().join("fixtures.yml");

        let fixtures = vec![KafkaFixture {
            identifier: "yml-test".to_string(),
            name: "YML Test".to_string(),
            topic: "yml-topic".to_string(),
            partition: None,
            key_pattern: None,
            value_template: serde_json::json!({"yml": true}),
            headers: HashMap::new(),
            auto_produce: None,
        }];

        let yaml_content = serde_yaml::to_string(&fixtures).unwrap();
        fs::write(&fixture_path, yaml_content).unwrap();

        let loaded = KafkaFixture::load_from_dir(&temp_dir.path().to_path_buf()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].identifier, "yml-test");
    }

    #[test]
    fn test_load_from_dir_ignores_non_yaml_files() {
        let temp_dir = TempDir::new().unwrap();
        let txt_path = temp_dir.path().join("readme.txt");
        fs::write(&txt_path, "This is not a YAML file").unwrap();

        let loaded = KafkaFixture::load_from_dir(&temp_dir.path().to_path_buf()).unwrap();
        assert!(loaded.is_empty());
    }

    #[test]
    fn test_load_from_dir_multiple_files() {
        let temp_dir = TempDir::new().unwrap();

        let fixtures1 = vec![KafkaFixture {
            identifier: "fixture-1".to_string(),
            name: "Fixture 1".to_string(),
            topic: "topic-1".to_string(),
            partition: None,
            key_pattern: None,
            value_template: serde_json::json!({"id": 1}),
            headers: HashMap::new(),
            auto_produce: None,
        }];

        let fixtures2 = vec![KafkaFixture {
            identifier: "fixture-2".to_string(),
            name: "Fixture 2".to_string(),
            topic: "topic-2".to_string(),
            partition: None,
            key_pattern: None,
            value_template: serde_json::json!({"id": 2}),
            headers: HashMap::new(),
            auto_produce: None,
        }];

        fs::write(
            temp_dir.path().join("fixtures1.yaml"),
            serde_yaml::to_string(&fixtures1).unwrap(),
        )
        .unwrap();

        fs::write(
            temp_dir.path().join("fixtures2.yaml"),
            serde_yaml::to_string(&fixtures2).unwrap(),
        )
        .unwrap();

        let loaded = KafkaFixture::load_from_dir(&temp_dir.path().to_path_buf()).unwrap();
        assert_eq!(loaded.len(), 2);
    }

    #[test]
    fn test_load_from_dir_nonexistent() {
        let result = KafkaFixture::load_from_dir(&PathBuf::from("/nonexistent/path"));
        assert!(result.is_err());
    }

    // ==================== AutoProducer Tests ====================

    #[tokio::test]
    async fn test_auto_producer_creation() {
        let config = mockforge_core::config::KafkaConfig::default();
        let broker = Arc::new(crate::broker::KafkaMockBroker::new(config).await.unwrap());
        let template_engine = mockforge_core::templating::TemplateEngine::new();

        let producer = AutoProducer::new(broker, template_engine);
        let fixtures = producer.fixtures.read().await;
        assert!(fixtures.is_empty());
    }

    #[tokio::test]
    async fn test_auto_producer_add_fixture_enabled() {
        let config = mockforge_core::config::KafkaConfig::default();
        let broker = Arc::new(crate::broker::KafkaMockBroker::new(config).await.unwrap());
        let template_engine = mockforge_core::templating::TemplateEngine::new();

        let producer = AutoProducer::new(broker, template_engine);

        let fixture = KafkaFixture {
            identifier: "auto-enabled".to_string(),
            name: "Auto Enabled".to_string(),
            topic: "auto-topic".to_string(),
            partition: None,
            key_pattern: None,
            value_template: serde_json::json!({"auto": true}),
            headers: HashMap::new(),
            auto_produce: Some(AutoProduceConfig {
                enabled: true,
                rate_per_second: 1,
                duration_seconds: None,
                total_count: None,
            }),
        };

        producer.add_fixture(fixture).await;

        let fixtures = producer.fixtures.read().await;
        assert_eq!(fixtures.len(), 1);
        assert!(fixtures.contains_key("auto-enabled"));
    }

    #[tokio::test]
    async fn test_auto_producer_add_fixture_disabled() {
        let config = mockforge_core::config::KafkaConfig::default();
        let broker = Arc::new(crate::broker::KafkaMockBroker::new(config).await.unwrap());
        let template_engine = mockforge_core::templating::TemplateEngine::new();

        let producer = AutoProducer::new(broker, template_engine);

        let fixture = KafkaFixture {
            identifier: "auto-disabled".to_string(),
            name: "Auto Disabled".to_string(),
            topic: "disabled-topic".to_string(),
            partition: None,
            key_pattern: None,
            value_template: serde_json::json!({"auto": false}),
            headers: HashMap::new(),
            auto_produce: Some(AutoProduceConfig {
                enabled: false,
                rate_per_second: 1,
                duration_seconds: None,
                total_count: None,
            }),
        };

        producer.add_fixture(fixture).await;

        let fixtures = producer.fixtures.read().await;
        assert!(fixtures.is_empty());
    }

    #[tokio::test]
    async fn test_auto_producer_add_fixture_no_auto_produce() {
        let config = mockforge_core::config::KafkaConfig::default();
        let broker = Arc::new(crate::broker::KafkaMockBroker::new(config).await.unwrap());
        let template_engine = mockforge_core::templating::TemplateEngine::new();

        let producer = AutoProducer::new(broker, template_engine);

        let fixture = KafkaFixture {
            identifier: "no-auto".to_string(),
            name: "No Auto".to_string(),
            topic: "manual-topic".to_string(),
            partition: None,
            key_pattern: None,
            value_template: serde_json::json!({"manual": true}),
            headers: HashMap::new(),
            auto_produce: None,
        };

        producer.add_fixture(fixture).await;

        let fixtures = producer.fixtures.read().await;
        assert!(fixtures.is_empty());
    }

    #[tokio::test]
    async fn test_auto_producer_stop_fixture() {
        let config = mockforge_core::config::KafkaConfig::default();
        let broker = Arc::new(crate::broker::KafkaMockBroker::new(config).await.unwrap());
        let template_engine = mockforge_core::templating::TemplateEngine::new();

        let producer = AutoProducer::new(broker, template_engine);

        let fixture = KafkaFixture {
            identifier: "stop-test".to_string(),
            name: "Stop Test".to_string(),
            topic: "stop-topic".to_string(),
            partition: None,
            key_pattern: None,
            value_template: serde_json::json!({"test": true}),
            headers: HashMap::new(),
            auto_produce: Some(AutoProduceConfig {
                enabled: true,
                rate_per_second: 1,
                duration_seconds: None,
                total_count: None,
            }),
        };

        producer.add_fixture(fixture).await;
        producer.stop_fixture("stop-test").await;

        let fixtures = producer.fixtures.read().await;
        let fixture = fixtures.get("stop-test");
        assert!(fixture.is_some());
        assert!(!fixture.unwrap().auto_produce.as_ref().unwrap().enabled);
    }

    #[tokio::test]
    async fn test_auto_producer_stop_nonexistent_fixture() {
        let config = mockforge_core::config::KafkaConfig::default();
        let broker = Arc::new(crate::broker::KafkaMockBroker::new(config).await.unwrap());
        let template_engine = mockforge_core::templating::TemplateEngine::new();

        let producer = AutoProducer::new(broker, template_engine);
        producer.stop_fixture("nonexistent").await;

        // Should not panic
        let fixtures = producer.fixtures.read().await;
        assert!(fixtures.is_empty());
    }
}