choreo 0.13.0

DSL for BDD type testing.
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
use crate::parser::ast::{
    Action, Condition, GivenStep, Scenario, Statement, TestCase, TestSuite, TestSuiteSettings,
    ThenStep, Value, WhenStep,
};
use std::collections::{HashMap, HashSet};

// The E, W and I codes are inspired by ESLint's conventions.
pub struct DiagnosticRule {
    pub code: &'static str,
    pub message: &'static str,
}

pub struct DiagnosticCodes;

impl DiagnosticCodes {
    // Error codes (E) - Critical issues in the chor file
    pub const TIMEOUT_ZERO: DiagnosticRule = DiagnosticRule {
        code: "E001",
        message: "Timeout cannot be zero",
    };
    pub const INVALID_HTTP_STATUS: DiagnosticRule = DiagnosticRule {
        code: "E002",
        message: "Invalid HTTP status code",
    };
    pub const INVALID_HEADER_NAME: DiagnosticRule = DiagnosticRule {
        code: "E003",
        message:
            "Invalid HTTP header name. Header names cannot contain spaces or special characters.",
    };
    pub const INVALID_JSON_BODY: DiagnosticRule = DiagnosticRule {
        code: "E004",
        message: "Request body is not valid JSON, but Content-Type is 'application/json'.",
    };

    // Warning codes (W) - Potential issues
    pub const SCENARIO_NO_TESTS: DiagnosticRule = DiagnosticRule {
        code: "W001",
        message: "Scenario has no test cases and will be skipped.",
    };
    pub const TEST_NO_GIVEN_STEPS: DiagnosticRule = DiagnosticRule {
        code: "W002",
        message: "Test has no `given` steps; its execution may depend on implicit state.",
    };
    pub const HTTP_URL_NO_PROTOCOL: DiagnosticRule = DiagnosticRule {
        code: "W003",
        message: "HTTP URL should start with http:// or https://",
    };
    pub const WAIT_TIME_EXCESSIVE: DiagnosticRule = DiagnosticRule {
        code: "W004",
        message: "Wait time exceeds 5 minutes",
    };
    pub const TIMEOUT_EXCESSIVE: DiagnosticRule = DiagnosticRule {
        code: "W005",
        message: "timeout exceeds 5 minutes",
    };
    pub const EXPECTED_FAILURES_HIGH: DiagnosticRule = DiagnosticRule {
        code: "W006",
        message: "Expected failures count seems unusually high",
    };

    pub const DUPLICATE_SCENARIO_NAME: DiagnosticRule = DiagnosticRule {
        code: "W008",
        message: "Scenario name is duplicated. All scenario names in a feature should be unique.",
    };
    pub const MISSING_CLEANUP: DiagnosticRule = DiagnosticRule {
        code: "W009",
        message: "Scenario creates files or directories but has no `after` block for cleanup.",
    };
    pub const UNUSED_VARIABLE: DiagnosticRule = DiagnosticRule {
        code: "W010",
        message: "Variable is defined but never used.",
    };
    pub const UNUSED_ACTOR: DiagnosticRule = DiagnosticRule {
        code: "W019",
        message: "Actor is declared but never used.",
    };
    pub const HTTP_URL_IS_LOCALHOST: DiagnosticRule = DiagnosticRule {
        code: "W011",
        message: "URL points to localhost. This may not be accessible in all environments.",
    };
    pub const HTTP_URL_IS_PLACEHOLDER: DiagnosticRule = DiagnosticRule {
        code: "W012",
        message: "URL uses a placeholder domain (e.g. example.com). Is this intentional?",
    };
    pub const SUSPICIOUS_HEADER_TYPO: DiagnosticRule = DiagnosticRule {
        code: "W013",
        message: "HTTP header contains a common typo.",
    };
    pub const CONFLICTING_HEADER: DiagnosticRule = DiagnosticRule { code: "W014", message: "Conflicting HTTP header. A header like 'Content-Type' should only be set once per request." };
    pub const LARGE_REQUEST_BODY: DiagnosticRule = DiagnosticRule {
        code: "W015",
        message: "HTTP request body is very large and may cause performance issues or timeouts.",
    };
    pub const HARDCODED_CREDENTIALS: DiagnosticRule = DiagnosticRule {
        code: "W016",
        message: "Potential hardcoded credentials found in URL or header. Use variables instead.",
    };
    pub const INSECURE_HTTP_URL: DiagnosticRule = DiagnosticRule {
        code: "W017",
        message: "URL uses insecure HTTP protocol instead of HTTPS.",
    };
    pub const MISSING_USER_AGENT: DiagnosticRule = DiagnosticRule {
        code: "W018",
        message: "No User-Agent header was set for the HTTP request.",
    };

    // Info codes (I) - Informational
    pub const BEST_PRACTICE_SUGGESTION: DiagnosticRule = DiagnosticRule {
        code: "I001",
        message: "Consider using best practices",
    };
    pub const STOP_ON_FAILURE_ENABLED: DiagnosticRule = DiagnosticRule {
        code: "I002",
        message: "Stop on failure is enabled - tests will halt on first failure",
    };
}

pub struct Diagnostic {
    pub rule_id: String,
    pub message: String,
    pub line: usize,
    pub severity: Severity,
}

#[derive(Debug, PartialEq)]
pub enum Severity {
    Warning,
    Error,
    Info,
}

struct Linter {
    diagnostics: Vec<Diagnostic>,
    defined_vars: HashSet<String>,
    used_vars: HashSet<String>,
    defined_actors: HashSet<String>,
    used_actors: HashSet<String>,
    seen_scenario_names: HashSet<String>,
    current_headers: HashMap<String, String>,
}

impl Linter {
    pub fn new() -> Self {
        Self {
            diagnostics: Vec::new(),
            defined_vars: HashSet::new(),
            used_vars: HashSet::new(),
            defined_actors: HashSet::new(),
            used_actors: HashSet::new(),
            seen_scenario_names: HashSet::new(),
            current_headers: HashMap::new(),
        }
    }

    pub fn lint(&mut self, suite: &TestSuite) -> &Vec<Diagnostic> {
        self.diagnostics.clear();
        self.visit_test_suite(suite);
        &self.diagnostics
    }

    fn lint_url(&mut self, url: &str) {
        // Skip linting if the URL contains variable substitution like ${URL}
        if url.contains("${") {
            return;
        }

        if url.contains("localhost") || url.contains("127.0.0.1") {
            self.add_diagnostic(
                &DiagnosticCodes::HTTP_URL_IS_LOCALHOST,
                &format!(
                    "{} {}",
                    DiagnosticCodes::HTTP_URL_IS_LOCALHOST.code,
                    DiagnosticCodes::HTTP_URL_IS_LOCALHOST.message
                ),
                0,
                Severity::Warning,
            );
        }
        if url.contains("example.com") || url.contains("example.org") {
            self.add_diagnostic(
                &DiagnosticCodes::HTTP_URL_IS_PLACEHOLDER,
                &format!(
                    "{} {}",
                    DiagnosticCodes::HTTP_URL_IS_PLACEHOLDER.code,
                    DiagnosticCodes::HTTP_URL_IS_PLACEHOLDER.message
                ),
                0,
                Severity::Warning,
            );
        }
        if url.starts_with("http://") {
            self.add_diagnostic(
                &DiagnosticCodes::INSECURE_HTTP_URL,
                &format!(
                    "{} {}",
                    DiagnosticCodes::INSECURE_HTTP_URL.code,
                    DiagnosticCodes::INSECURE_HTTP_URL.message
                ),
                0,
                Severity::Warning,
            );
        }

        if !url.starts_with("http://") && !url.starts_with("https://") {
            self.add_diagnostic(
                &DiagnosticCodes::HTTP_URL_NO_PROTOCOL,
                &format!(
                    "{}: {}",
                    DiagnosticCodes::HTTP_URL_NO_PROTOCOL.code,
                    DiagnosticCodes::HTTP_URL_NO_PROTOCOL.message,
                ),
                0,
                Severity::Warning,
            );
        }

        if url.contains('@') && !url.contains("${") {
            self.add_diagnostic(
                &DiagnosticCodes::HARDCODED_CREDENTIALS,
                &format!(
                    "{} {}",
                    DiagnosticCodes::HARDCODED_CREDENTIALS.code,
                    DiagnosticCodes::HARDCODED_CREDENTIALS.message
                ),
                0,
                Severity::Warning,
            );
        }
    }

    fn lint_header(&mut self, key: &str, value: &str) {
        let lower_key = key.to_lowercase();
        // This regex ensures HTTP header names contain only valid "token" characters as defined by RFC 7230.
        // Valid: Content-Type, User-Agent, X-Custom-Header
        // Invalid: Content Type (space), User@Agent (@ symbol), Header:Value (colon)
        let re = regex::Regex::new(r"^[!#$%&'*+\-.^_`|~0-9a-zA-Z]+$").unwrap();
        //println!("key: {}", key);
        if !re.is_match(key) {
            //println!("key matches regexp {}", key);
            self.add_diagnostic(
                &DiagnosticCodes::INVALID_HEADER_NAME,
                &format!(
                    "{} {}",
                    DiagnosticCodes::INVALID_HEADER_NAME.code,
                    DiagnosticCodes::INVALID_HEADER_NAME.message
                ),
                0,
                Severity::Warning,
            );
        }

        if lower_key == "contet-type" || lower_key == "acept" {
            self.add_diagnostic(
                &DiagnosticCodes::SUSPICIOUS_HEADER_TYPO,
                &format!(
                    "{} {}",
                    DiagnosticCodes::SUSPICIOUS_HEADER_TYPO.code,
                    DiagnosticCodes::SUSPICIOUS_HEADER_TYPO.message
                ),
                0,
                Severity::Warning,
            );
        }

        if lower_key == "authorization" && !value.contains("${") {
            self.add_diagnostic(
                &DiagnosticCodes::HARDCODED_CREDENTIALS,
                &format!(
                    "{} {}",
                    DiagnosticCodes::HARDCODED_CREDENTIALS.code,
                    DiagnosticCodes::HARDCODED_CREDENTIALS.message
                ),
                0,
                Severity::Warning,
            );
        }

        if self.current_headers.contains_key(&lower_key) && lower_key == "content-type" {
            self.add_diagnostic(
                &DiagnosticCodes::CONFLICTING_HEADER,
                &format!(
                    "{} {}",
                    DiagnosticCodes::CONFLICTING_HEADER.code,
                    DiagnosticCodes::CONFLICTING_HEADER.message,
                ),
                0,
                Severity::Warning,
            );
        }

        self.current_headers
            .insert(lower_key.to_owned(), value.to_owned());
    }

    fn lint_http_body(&mut self, body: &str) {
        // Skip linting if the body contains variable substitution
        if body.contains("${") {
            return;
        }

        //println!("body: {}", body);

        if body.len() > 10 * 1024 {
            // 10 KB
            self.add_diagnostic(
                &DiagnosticCodes::LARGE_REQUEST_BODY,
                &format!(
                    "{} {}",
                    DiagnosticCodes::LARGE_REQUEST_BODY.code,
                    DiagnosticCodes::LARGE_REQUEST_BODY.message,
                ),
                0,
                Severity::Warning,
            );
        }
        if let Some(content_type) = self.current_headers.get("content-type") {
            if content_type.contains("application/json")
                && serde_json::from_str::<serde_json::Value>(body).is_err()
            {
                self.add_diagnostic(
                    &DiagnosticCodes::INVALID_JSON_BODY,
                    &format!(
                        "{} {}",
                        DiagnosticCodes::INVALID_JSON_BODY.code,
                        DiagnosticCodes::INVALID_JSON_BODY.message
                    ),
                    0,
                    Severity::Warning,
                );
            }
        }
    }

    // Use a custom formatted message
    fn add_diagnostic(
        &mut self,
        rule: &DiagnosticRule,
        message: &str,
        line: usize,
        severity: Severity,
    ) {
        self.diagnostics.push(Diagnostic {
            rule_id: rule.code.to_string(),
            message: message.to_string(),
            line,
            severity,
        });
    }
}

// Add this convenience function at the module level
pub fn lint(suite: &TestSuite) -> Vec<String> {
    let mut linter = Linter::new();
    let diagnostics = linter.lint(suite);

    diagnostics
        .iter()
        .map(|d| format!("[{}] {}", d.rule_id, d.message))
        .collect()
}

pub trait Visitor {
    fn visit_test_suite(&mut self, suite: &TestSuite);
    fn visit_statement(&mut self, stmt: &Statement);
    fn visit_settings(&mut self, settings: &TestSuiteSettings);
    fn visit_scenario(&mut self, scenario: &Scenario);
    fn visit_test_case(&mut self, test: &TestCase);
    fn visit_given_step(&mut self, step: &GivenStep);
    fn visit_action(&mut self, action: &Action);
    fn visit_condition(&mut self, condition: &Condition);
    fn visit_background(&mut self, steps: &Vec<GivenStep>);
    fn visit_env_def(&mut self, vars: &Vec<String>);
    fn visit_var_def(&mut self, name: &String, value: &Value);
    fn visit_actor_def(&mut self, actors: &Vec<String>);
    //fn visit_feature_def(&mut self, name: &String);
}

impl Visitor for Linter {
    fn visit_test_suite(&mut self, suite: &TestSuite) {
        // First pass: collect all variable definitions
        for statement in &suite.statements {
            match statement {
                Statement::EnvDef(vars) => {
                    for var in vars {
                        //println!("Env: {}", var);
                        self.defined_vars.insert(var.clone());
                    }
                }
                Statement::VarDef(name, _value) => {
                    //println!("Var: {}", name);
                    self.defined_vars.insert(name.clone());
                }
                _ => {}
            }
        }

        // Second pass: visit all nodes to find variable usages and check rules
        self.seen_scenario_names.clear();
        for statement in &suite.statements {
            self.visit_statement(statement);
        }

        // Third pass: check for unused variables
        let unused_vars: Vec<String> = self
            .defined_vars
            .difference(&self.used_vars)
            .cloned()
            .collect();
        for var in unused_vars {
            self.add_diagnostic(
                &DiagnosticCodes::UNUSED_VARIABLE,
                &format!(
                    "{}: {} ({})",
                    DiagnosticCodes::UNUSED_VARIABLE.code,
                    DiagnosticCodes::UNUSED_VARIABLE.message,
                    var
                ),
                0, // line number - need to get span info
                Severity::Warning,
            );
        }

        // Fourth pass: check for unused actors
        let unused_actors: Vec<String> = self
            .defined_actors
            .difference(&self.used_actors)
            .cloned()
            .collect();
        for actor in unused_actors {
            self.add_diagnostic(
                &DiagnosticCodes::UNUSED_ACTOR,
                &format!(
                    "{}: {} ({})",
                    DiagnosticCodes::UNUSED_ACTOR.code,
                    DiagnosticCodes::UNUSED_ACTOR.message,
                    actor
                ),
                0,
                Severity::Warning,
            );
        }
    }

    fn visit_statement(&mut self, stmt: &Statement) {
        match stmt {
            Statement::Scenario(scenario) => self.visit_scenario(scenario),
            Statement::TestCase(test) => self.visit_test_case(test),
            Statement::SettingsDef(settings) => self.visit_settings(settings),
            Statement::BackgroundDef(steps) => self.visit_background(steps),
            // Nothing to lint really
            // Statement::FeatureDef(name) => self.visit_feature_def(name),
            Statement::ActorDef(actors) => self.visit_actor_def(actors),
            Statement::EnvDef(vars) => self.visit_env_def(vars),
            Statement::VarDef(name, value) => self.visit_var_def(name, value),
            _ => {}
        }
    }

    fn visit_settings(&mut self, settings: &TestSuiteSettings) {
        let default_span = settings
            .span
            .as_ref()
            .map(|s| (s.line, s.column))
            .unwrap_or((0, 0));

        if settings.timeout_seconds == 0 {
            let (line, _column) = settings
                .setting_spans
                .as_ref()
                .and_then(|spans| spans.timeout_seconds_span.as_ref())
                .map(|span| (span.line, span.column))
                .unwrap_or(default_span);

            self.add_diagnostic(
                &DiagnosticCodes::TIMEOUT_ZERO,
                &format!(
                    "{}: {} (line: {})",
                    DiagnosticCodes::TIMEOUT_ZERO.code,
                    DiagnosticCodes::TIMEOUT_ZERO.message,
                    line
                ),
                line,
                Severity::Error,
            );
        }

        if settings.timeout_seconds > 300 {
            let (line, _column) = settings
                .setting_spans
                .as_ref()
                .and_then(|spans| spans.timeout_seconds_span.as_ref())
                .map(|span| (span.line, span.column))
                .unwrap_or(default_span);

            self.add_diagnostic(
                &DiagnosticCodes::TIMEOUT_EXCESSIVE,
                &format!(
                    "{}: {} (line: {})",
                    DiagnosticCodes::TIMEOUT_EXCESSIVE.code,
                    DiagnosticCodes::TIMEOUT_EXCESSIVE.message,
                    line
                ),
                line,
                Severity::Error,
            );
        }

        // Warn if stop_on_failure is enabled
        if settings.stop_on_failure {
            let (line, _column) = settings
                .setting_spans
                .as_ref()
                .and_then(|spans| spans.stop_on_failure_span.as_ref())
                .map(|span| (span.line, span.column))
                .unwrap_or(default_span);
            self.add_diagnostic(
                &DiagnosticCodes::STOP_ON_FAILURE_ENABLED,
                &format!(
                    "{}: {} (line: {})",
                    DiagnosticCodes::STOP_ON_FAILURE_ENABLED.code,
                    DiagnosticCodes::STOP_ON_FAILURE_ENABLED.message,
                    line
                ),
                line,
                Severity::Info,
            );
        }

        // Validate expected_failures
        if settings.expected_failures > 100 {
            let (line, _column) = settings
                .setting_spans
                .as_ref()
                .and_then(|spans| spans.expected_failures_span.as_ref())
                .map(|span| (span.line, span.column))
                .unwrap_or(default_span);
            self.add_diagnostic(
                &DiagnosticCodes::EXPECTED_FAILURES_HIGH,
                &format!(
                    "{}: {} (line: {})",
                    DiagnosticCodes::EXPECTED_FAILURES_HIGH.code,
                    DiagnosticCodes::EXPECTED_FAILURES_HIGH.message,
                    line
                ),
                line,
                Severity::Warning,
            );
        }
    }

    fn visit_scenario(&mut self, scenario: &Scenario) {
        let (line, _column) = scenario
            .span
            .as_ref()
            .map_or((0, 0), |s| (s.line, s.column));
        //println!("Scenario: {}", scenario.name);

        // Check for empty scenarios.
        if scenario.tests.is_empty() {
            self.add_diagnostic(
                &DiagnosticCodes::SCENARIO_NO_TESTS,
                &format!(
                    "{}: {} (line: {})",
                    DiagnosticCodes::SCENARIO_NO_TESTS.code,
                    DiagnosticCodes::SCENARIO_NO_TESTS.message,
                    line
                ),
                line,
                Severity::Warning,
            );
        }

        // Check for duplicate scenario names.
        if !self.seen_scenario_names.insert(scenario.name.clone()) {
            self.add_diagnostic(
                &DiagnosticCodes::DUPLICATE_SCENARIO_NAME,
                &format!(
                    "{}: {} (line: {})",
                    DiagnosticCodes::DUPLICATE_SCENARIO_NAME.code,
                    DiagnosticCodes::DUPLICATE_SCENARIO_NAME.message,
                    line
                ),
                line,
                Severity::Warning,
            );
        }

        // Check if setup actions exist without a corresponding cleanup.
        if scenario_has_setup_actions(scenario) && scenario.after.is_empty() {
            self.add_diagnostic(
                &DiagnosticCodes::MISSING_CLEANUP,
                &format!(
                    "{}: {} (line: {})",
                    DiagnosticCodes::MISSING_CLEANUP.code,
                    DiagnosticCodes::MISSING_CLEANUP.message,
                    line
                ),
                line,
                Severity::Warning,
            );
        }

        for test in &scenario.tests {
            self.visit_test_case(test);
        }
    }

    fn visit_test_case(&mut self, test: &TestCase) {
        self.current_headers.clear();
        let (line, _column) = test.span.as_ref().map_or((0, 0), |s| (s.line, s.column));
        //println!("Test: {}", test.name);

        for step in &test.given {
            match step {
                GivenStep::Action(a) => self.visit_action(a),
                GivenStep::Condition(c) => self.visit_condition(c),
                GivenStep::TaskCall(_) => {} // Task calls will be expanded before execution
            }
        }
        for step in &test.when {
            match step {
                WhenStep::Action(action) => self.visit_action(action),
                WhenStep::TaskCall(_) => {} // Task calls will be expanded before execution
            }
        }
        for step in &test.then {
            match step {
                ThenStep::Condition(condition) => self.visit_condition(condition),
                ThenStep::TaskCall(_) => {} // Task calls will be expanded before execution
            }
        }

        // Check for missing User-Agent
        if self.current_headers.values().any(|v| v.starts_with("http"))
            && !self.current_headers.contains_key("user-agent")
        {
            self.add_diagnostic(
                &DiagnosticCodes::MISSING_USER_AGENT,
                &format!(
                    "{} {}",
                    DiagnosticCodes::MISSING_USER_AGENT.code,
                    DiagnosticCodes::MISSING_USER_AGENT.message
                ),
                line,
                Severity::Warning,
            );
        }
    }

    fn visit_given_step(&mut self, step: &GivenStep) {
        match step {
            GivenStep::Action(a) => self.visit_action(&a),
            GivenStep::Condition(c) => self.visit_condition(&c),
            GivenStep::TaskCall(_) => {} // Task calls will be expanded before execution
        }
    }

    fn visit_action(&mut self, action: &Action) {
        // A helper closure to find variables in a string.
        let find_vars = |s: &str, used: &mut HashSet<String>| {
            let re = regex::Regex::new(r"\$\{(\w+)}").unwrap();
            for cap in re.captures_iter(s) {
                used.insert(cap[1].to_string());
            }
        };

        match action {
            Action::Run { command, .. } => {
                self.used_actors.insert("Terminal".to_string());
                find_vars(command, &mut self.used_vars);
            }
            Action::SetCwd { path } => {
                self.used_actors.insert("Terminal".to_string());
                find_vars(path, &mut self.used_vars);
            }
            Action::CreateFile { path, content } => {
                self.used_actors.insert("FileSystem".to_string());
                find_vars(path, &mut self.used_vars);
                find_vars(content, &mut self.used_vars);
            }
            Action::DeleteFile { path }
            | Action::CreateDir { path }
            | Action::DeleteDir { path } => {
                self.used_actors.insert("FileSystem".to_string());
                find_vars(path, &mut self.used_vars);
            }
            Action::ReadFile { path, .. } => {
                self.used_actors.insert("FileSystem".to_string());
                find_vars(path, &mut self.used_vars);
            }
            Action::HttpSetHeader { key, value } | Action::HttpSetCookie { key, value } => {
                self.used_actors.insert("Web".to_string());
                find_vars(key, &mut self.used_vars);
                find_vars(value, &mut self.used_vars);
                self.lint_header(key, value);
            }
            Action::HttpGet { url } | Action::HttpDelete { url } => {
                self.used_actors.insert("Web".to_string());
                find_vars(url, &mut self.used_vars);
                self.lint_url(url);
            }
            Action::HttpPost { url, body }
            | Action::HttpPut { url, body }
            | Action::HttpPatch { url, body } => {
                self.used_actors.insert("Web".to_string());
                find_vars(url, &mut self.used_vars);
                find_vars(body, &mut self.used_vars);
                self.lint_url(url);
                self.lint_http_body(body);
            }
            Action::Pause { .. }
            | Action::Log { .. }
            | Action::Timestamp { .. }
            | Action::Uuid { .. } => {
                self.used_actors.insert("System".to_string());
            }
            // Other actions...
            _ => {}
        }
    }

    fn visit_condition(&mut self, condition: &Condition) {
        // A helper closure to find variables in a string.
        let mut find_cond_vars = |s: &str| {
            let re = regex::Regex::new(r"\$\{(\w+)}").unwrap();
            for cap in re.captures_iter(s) {
                self.used_vars.insert(cap[1].to_string());
            }
        };

        const VALID_HTTP_STATUS_RANGE: std::ops::RangeInclusive<u16> = 100..=599;

        match condition {
            Condition::Wait { wait, .. } => {
                // Excessive wait time
                if *wait > 300.0 {
                    // 5 minutes
                    self.add_diagnostic(
                        &DiagnosticCodes::WAIT_TIME_EXCESSIVE,
                        &format!(
                            "{}: {} ({:.1}s)",
                            DiagnosticCodes::WAIT_TIME_EXCESSIVE.code,
                            DiagnosticCodes::WAIT_TIME_EXCESSIVE.message,
                            wait
                        ),
                        0,
                        Severity::Warning,
                    );
                }
            }

            Condition::OutputContains { text, .. }
            | Condition::OutputNotContains { text, .. }
            | Condition::StderrContains(text)
            | Condition::OutputStartsWith(text)
            | Condition::OutputEndsWith(text)
            | Condition::OutputEquals(text) => {
                self.used_actors.insert("Terminal".to_string());
                find_cond_vars(text);
            }

            Condition::OutputMatches { regex, .. } => {
                self.used_actors.insert("Terminal".to_string());
                find_cond_vars(regex);
            }

            Condition::JsonPathEquals {
                path,
                expected_value: value,
            } => {
                find_cond_vars(path);
                if let Value::String(s) = value {
                    find_cond_vars(s);
                }
            }

            Condition::JsonValueIsString { path }
            | Condition::JsonValueIsNumber { path }
            | Condition::JsonValueIsArray { path }
            | Condition::JsonValueIsObject { path }
            | Condition::JsonBodyHasPath { path } => {
                find_cond_vars(path);
            }
            Condition::JsonValueHasSize { path, .. }
            | Condition::JsonOutputAtEquals { path, .. }
            | Condition::JsonOutputAtIncludes { path, .. }
            | Condition::JsonOutputAtHasItemCount { path, .. } => {
                find_cond_vars(path);
            }

            Condition::FileExists { path }
            | Condition::FileDoesNotExist { path }
            | Condition::DirExists { path }
            | Condition::DirDoesNotExist { path }
            | Condition::FileIsEmpty { path }
            | Condition::FileIsNotEmpty { path } => {
                self.used_actors.insert("FileSystem".to_string());
                find_cond_vars(path);
            }

            Condition::ResponseStatusIs(status) => {
                self.used_actors.insert("Web".to_string());
                // Invalid HTTP status code
                if !VALID_HTTP_STATUS_RANGE.contains(status) {
                    self.add_diagnostic(
                        &DiagnosticCodes::INVALID_HTTP_STATUS,
                        &format!(
                            "{}: {} ({})",
                            DiagnosticCodes::INVALID_HTTP_STATUS.code,
                            DiagnosticCodes::INVALID_HTTP_STATUS.message,
                            status
                        ),
                        0,
                        Severity::Error,
                    );
                }
            }
            Condition::ResponseStatusIsIn(statuses) => {
                self.used_actors.insert("Web".to_string());
                for status in statuses {
                    if !VALID_HTTP_STATUS_RANGE.contains(status) {
                        self.add_diagnostic(
                            &DiagnosticCodes::INVALID_HTTP_STATUS,
                            &format!(
                                "{}: {} ({})",
                                DiagnosticCodes::INVALID_HTTP_STATUS.code,
                                DiagnosticCodes::INVALID_HTTP_STATUS.message,
                                status
                            ),
                            0,
                            Severity::Error,
                        );
                    }
                }
            }
            Condition::ResponseBodyContains { value } => {
                find_cond_vars(value);
            }
            Condition::ResponseBodyMatches { regex, .. } => {
                find_cond_vars(regex);
            }

            // Other conditions...
            _ => {}
        }
    }

    fn visit_background(&mut self, steps: &Vec<GivenStep>) {
        // A background block is essentially a given bloch that is parsed before everything else in a scenario
        for step in steps {
            self.visit_given_step(step);
        }
    }

    fn visit_env_def(&mut self, vars: &Vec<String>) {
        for var in vars {
            // Check for standard naming convention
            if !var
                .chars()
                .all(|c| c.is_uppercase() || c.is_numeric() || c == '_')
            {
                self.add_diagnostic(
                    &DiagnosticCodes::BEST_PRACTICE_SUGGESTION,
                    &format!(
                        "Environment variable '{}' should use SCREAMING_SNAKE_CASE",
                        var
                    ),
                    0,
                    Severity::Info,
                );
            }

            // Warn about potentially missing common environment variables
            if var == "PATH" || var == "HOME" {
                self.add_diagnostic(
                    &DiagnosticCodes::BEST_PRACTICE_SUGGESTION,
                    &format!(
                        "Environment variable '{}' is system-critical. Ensure it's available.",
                        var
                    ),
                    0,
                    Severity::Info,
                );
            }
        }
    }

    fn visit_var_def(&mut self, name: &String, _value: &Value) {
        // Check for naming convention (SCREAMING_SNAKE_CASE for variables)
        if !name
            .chars()
            .all(|c| c.is_uppercase() || c.is_numeric() || c == '_')
        {
            self.add_diagnostic(
                &DiagnosticCodes::BEST_PRACTICE_SUGGESTION,
                &format!(
                    "Variable '{}' should use SCREAMING_SNAKE_CASE naming convention",
                    name
                ),
                0,
                Severity::Info,
            );
        }

        // Check for potentially sensitive variable names
        let sensitive_keywords = ["PASSWORD", "SECRET", "TOKEN", "KEY", "API_KEY"];
        if sensitive_keywords
            .iter()
            .any(|&keyword| name.to_uppercase().contains(keyword))
        {
            self.add_diagnostic(
                &DiagnosticCodes::HARDCODED_CREDENTIALS,
                &format!("Variable '{}' appears to contain sensitive data", name),
                0,
                Severity::Warning,
            );
        }
    }

    fn visit_actor_def(&mut self, actors: &Vec<String>) {
        const VALID_ACTORS: &[&str] = &["Web", "Terminal", "System", "FileSystem"];

        let mut seen_actors = HashSet::new();

        for actor in actors {
            // Track declared actors for unused-actor detection
            self.defined_actors.insert(actor.clone());

            //println!("actor: {}", actor);
            // Check for duplicate actors
            if !seen_actors.insert(actor.clone()) {
                self.add_diagnostic(
                    &DiagnosticCodes::DUPLICATE_SCENARIO_NAME, // reuse
                    &format!("Duplicate actor '{}' found", actor),
                    0,
                    Severity::Warning,
                );
            }

            // Check if actor is valid
            if !VALID_ACTORS.contains(&actor.as_str()) {
                self.add_diagnostic(
                    &DiagnosticCodes::BEST_PRACTICE_SUGGESTION,
                    &format!(
                        "Unknown actor '{}'. Valid actors are: {}",
                        actor,
                        VALID_ACTORS.join(", ")
                    ),
                    0,
                    Severity::Error,
                );
            }

            // Check naming convention (PascalCase)
            if !actor.chars().next().unwrap_or(' ').is_uppercase() {
                self.add_diagnostic(
                    &DiagnosticCodes::BEST_PRACTICE_SUGGESTION,
                    &format!(
                        "Actor '{}' should follow PascalCase naming convention",
                        actor
                    ),
                    0,
                    Severity::Info,
                );
            }
        }
    }
}

///Helper function to check if a scenario contains file system creation actions.
fn scenario_has_setup_actions(scenario: &Scenario) -> bool {
    for test in &scenario.tests {
        // Collect actions from given steps
        let given_actions: Vec<_> = test
            .given
            .iter()
            .filter_map(|s| match s {
                GivenStep::Action(a) => Some(a),
                _ => None,
            })
            .collect();

        // Collect actions from when steps
        let when_actions: Vec<_> = test
            .when
            .iter()
            .filter_map(|s| match s {
                WhenStep::Action(a) => Some(a),
                _ => None,
            })
            .collect();

        // Check all actions for filesystem creation
        for action in given_actions.into_iter().chain(when_actions.into_iter()) {
            if action.is_filesystem_creation() {
                return true;
            }
        }
    }
    false
}