Skip to main content

mockforge_bench/
command.rs

1//! Bench command implementation
2
3use crate::crud_flow::{CrudFlowConfig, CrudFlowDetector};
4use crate::data_driven::{DataDistribution, DataDrivenConfig, DataDrivenGenerator, DataMapping};
5use crate::dynamic_params::{DynamicParamProcessor, DynamicPlaceholder};
6use crate::error::{BenchError, Result};
7use crate::executor::K6Executor;
8use crate::invalid_data::{InvalidDataConfig, InvalidDataGenerator};
9use crate::k6_gen::{K6Config, K6ScriptGenerator};
10use crate::mock_integration::{
11    MockIntegrationConfig, MockIntegrationGenerator, MockServerDetector,
12};
13use crate::owasp_api::{OwaspApiConfig, OwaspApiGenerator, OwaspCategory, ReportFormat};
14use crate::parallel_executor::{AggregatedResults, ParallelExecutor};
15use crate::parallel_requests::{ParallelConfig, ParallelRequestGenerator};
16use crate::param_overrides::ParameterOverrides;
17use crate::reporter::TerminalReporter;
18use crate::request_gen::RequestGenerator;
19use crate::scenarios::LoadScenario;
20use crate::security_payloads::{
21    SecurityCategory, SecurityPayload, SecurityPayloads, SecurityTestConfig, SecurityTestGenerator,
22};
23use crate::spec_dependencies::{
24    topological_sort, DependencyDetector, ExtractedValues, SpecDependencyConfig,
25};
26use crate::spec_parser::SpecParser;
27use crate::target_parser::parse_targets_file;
28use crate::wafbench::WafBenchLoader;
29use mockforge_openapi::multi_spec::{
30    load_specs_from_directory, load_specs_from_files, merge_specs, ConflictStrategy,
31};
32use mockforge_openapi::spec::OpenApiSpec;
33use std::collections::{HashMap, HashSet};
34use std::path::{Path, PathBuf};
35use std::str::FromStr;
36
37/// Parse a comma-separated header string into a `HashMap`.
38///
39/// Format: `Key:Value,Key2:Value2`
40///
41/// **Known limitation**: Header values containing commas will be incorrectly
42/// split. Cookie headers with semicolons work fine, but Cookie values with
43/// commas (e.g. `expires=Thu, 01 Jan 2099`) will break.
44pub fn parse_header_string(input: &str) -> Result<HashMap<String, String>> {
45    let mut headers = HashMap::new();
46
47    for pair in input.split(',') {
48        let parts: Vec<&str> = pair.splitn(2, ':').collect();
49        if parts.len() != 2 {
50            return Err(BenchError::Other(format!(
51                "Invalid header format: '{}'. Expected 'Key:Value'",
52                pair
53            )));
54        }
55        headers.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
56    }
57
58    Ok(headers)
59}
60
61/// Bench command configuration
62pub struct BenchCommand {
63    /// OpenAPI spec file(s) - can specify multiple
64    pub spec: Vec<PathBuf>,
65    /// Directory containing OpenAPI spec files (discovers .json, .yaml, .yml files)
66    pub spec_dir: Option<PathBuf>,
67    /// Conflict resolution strategy when merging multiple specs: "error" (default), "first", "last"
68    pub merge_conflicts: String,
69    /// Spec mode: "merge" (default) combines all specs, "sequential" runs them in order
70    pub spec_mode: String,
71    /// Dependency configuration file for cross-spec value passing (used with sequential mode)
72    pub dependency_config: Option<PathBuf>,
73    pub target: String,
74    /// API base path prefix (e.g., "/api" or "/v2/api")
75    /// If None, extracts from OpenAPI spec's servers URL
76    pub base_path: Option<String>,
77    pub duration: String,
78    pub vus: u32,
79    /// Target requests-per-second. When `Some(n)`, the generated k6 script
80    /// switches to `constant-arrival-rate` executor at `n` RPS with `vus`
81    /// pre-allocated. When `None`, uses the legacy `ramping-vus` executor
82    /// where RPS is implicit (VUs × 1 req/sec from the script's `sleep(1)`).
83    /// Issue #79 — Srikanth's round-3 reply.
84    pub target_rps: Option<u32>,
85    /// When true, every k6 request opens a new TCP/TLS connection
86    /// (`noConnectionReuse: true` and `--no-vu-connection-reuse`). Lets users
87    /// drive a high connections-per-second rate to exercise connection-limit
88    /// chaos and observe TCP-level fault injection. Issue #79.
89    pub no_keep_alive: bool,
90    pub scenario: String,
91    pub operations: Option<String>,
92    /// Exclude operations from testing (comma-separated)
93    ///
94    /// Supports "METHOD /path" or just "METHOD" to exclude all operations of that type.
95    pub exclude_operations: Option<String>,
96    pub auth: Option<String>,
97    pub headers: Option<String>,
98    pub output: PathBuf,
99    pub generate_only: bool,
100    pub script_output: Option<PathBuf>,
101    pub threshold_percentile: String,
102    pub threshold_ms: u64,
103    pub max_error_rate: f64,
104    pub verbose: bool,
105    pub skip_tls_verify: bool,
106    /// When true, set `Transfer-Encoding: chunked` on every k6 request body so
107    /// the server experiences chunked-encoded traffic. See
108    /// `K6ScriptTemplateData::chunked_request_bodies` for caveats — k6's Go
109    /// transport may still send Content-Length in some cases.
110    pub chunked_request_bodies: bool,
111    /// Optional file containing multiple targets
112    pub targets_file: Option<PathBuf>,
113    /// Maximum number of parallel executions (for multi-target mode)
114    pub max_concurrency: Option<u32>,
115    /// Results format: "per-target", "aggregated", or "both"
116    pub results_format: String,
117    /// Optional file containing parameter value overrides (JSON or YAML)
118    ///
119    /// Allows users to provide custom values for path parameters, query parameters,
120    /// headers, and request bodies instead of auto-generated placeholder values.
121    pub params_file: Option<PathBuf>,
122
123    // === CRUD Flow Options ===
124    /// Enable CRUD flow mode
125    pub crud_flow: bool,
126    /// Custom CRUD flow configuration file
127    pub flow_config: Option<PathBuf>,
128    /// Fields to extract from responses
129    pub extract_fields: Option<String>,
130
131    // === Parallel Execution Options ===
132    /// Number of resources to create in parallel
133    pub parallel_create: Option<u32>,
134
135    // === Data-Driven Testing Options ===
136    /// Test data file (CSV or JSON)
137    pub data_file: Option<PathBuf>,
138    /// Data distribution strategy
139    pub data_distribution: String,
140    /// Data column to field mappings
141    pub data_mappings: Option<String>,
142    /// Enable per-URI control mode (each row specifies method, uri, body, etc.)
143    pub per_uri_control: bool,
144
145    // === Invalid Data Testing Options ===
146    /// Percentage of requests with invalid data
147    pub error_rate: Option<f64>,
148    /// Types of invalid data to generate
149    pub error_types: Option<String>,
150
151    // === Security Testing Options ===
152    /// Enable security testing
153    pub security_test: bool,
154    /// Custom security payloads file
155    pub security_payloads: Option<PathBuf>,
156    /// Security test categories
157    pub security_categories: Option<String>,
158    /// Fields to target for security injection
159    pub security_target_fields: Option<String>,
160
161    // === WAFBench Integration ===
162    /// WAFBench test directory or glob pattern for loading CRS attack patterns
163    pub wafbench_dir: Option<String>,
164    /// Cycle through ALL WAFBench payloads instead of random sampling
165    pub wafbench_cycle_all: bool,
166
167    // === OpenAPI 3.0.0 Conformance Testing ===
168    /// Enable conformance testing mode
169    pub conformance: bool,
170    /// API key for conformance security tests
171    pub conformance_api_key: Option<String>,
172    /// Basic auth credentials for conformance security tests (user:pass)
173    pub conformance_basic_auth: Option<String>,
174    /// Conformance report output file
175    pub conformance_report: PathBuf,
176    /// Conformance categories to test (comma-separated, e.g. "parameters,security")
177    pub conformance_categories: Option<String>,
178    /// Conformance report format: "json" or "sarif"
179    pub conformance_report_format: String,
180    /// Custom headers to inject into every conformance request (for authentication).
181    /// Each entry is "Header-Name: value" format.
182    pub conformance_headers: Vec<String>,
183    /// When true, test ALL operations for method/response/body categories
184    /// instead of just one representative per feature check.
185    pub conformance_all_operations: bool,
186    /// Optional YAML file with custom conformance checks
187    pub conformance_custom: Option<PathBuf>,
188    /// Delay in milliseconds between consecutive conformance requests.
189    /// Useful when testing against rate-limited APIs.
190    pub conformance_delay_ms: u64,
191    /// Use k6 for conformance test execution instead of the native Rust executor
192    pub use_k6: bool,
193    /// Regex filter for custom conformance checks — only checks whose name or
194    /// path matches the pattern are included. Example: "wafcrs|ssl" to test
195    /// only checks with "wafcrs" or "ssl" in the name/path.
196    pub conformance_custom_filter: Option<String>,
197    /// When true, export all request/response pairs to
198    /// `conformance-requests.json` in the output directory.
199    pub export_requests: bool,
200    /// When true, validate each request against the OpenAPI spec and report
201    /// violations to `conformance-request-violations.json`.
202    pub validate_requests: bool,
203    /// Issue #79 round 13 (4) — when true, replace the standard
204    /// conformance run with a positive + per-category negative
205    /// self-test driver. Verifies that the server actually rejects
206    /// the negatives with 4xx (i.e. its validator is wired correctly).
207    /// Useful to confirm the round-13 (3) validator-bypass fix took
208    /// effect against the user's spec.
209    pub conformance_self_test: bool,
210
211    /// Round 18.5 — local source IPs to bind self-test requests to.
212    /// Each entry must be a valid `IpAddr` and already assigned to
213    /// an interface on the host. Operations round-robin through the
214    /// pool. Empty → one default client.
215    pub source_ips: Vec<String>,
216    /// Round 18.5 — fake source IPs to advertise via forwarded-IP
217    /// headers (rotated per operation). Used for GEODB testing
218    /// where the destination reads the IP from a header.
219    pub geo_source_ips: Vec<String>,
220    /// Round 18.5 — which forwarded-IP header(s) to populate when
221    /// `geo_source_ips` is non-empty. Empty → default 3-header set
222    /// (X-Forwarded-For, True-Client-IP, CF-Connecting-IP).
223    pub geo_source_headers: Vec<String>,
224
225    // === OWASP API Security Top 10 Testing ===
226    /// Enable OWASP API Security Top 10 testing mode
227    pub owasp_api_top10: bool,
228    /// OWASP API categories to test (comma-separated)
229    pub owasp_categories: Option<String>,
230    /// Authorization header name for OWASP auth tests
231    pub owasp_auth_header: String,
232    /// Valid authorization token for OWASP baseline requests
233    pub owasp_auth_token: Option<String>,
234    /// File containing admin/privileged paths to test
235    pub owasp_admin_paths: Option<PathBuf>,
236    /// Fields containing resource IDs for BOLA testing
237    pub owasp_id_fields: Option<String>,
238    /// OWASP report output file
239    pub owasp_report: Option<PathBuf>,
240    /// OWASP report format (json, sarif)
241    pub owasp_report_format: String,
242    /// Number of iterations per VU for OWASP tests (default: 1)
243    pub owasp_iterations: u32,
244}
245
246/// Round 18.5 — parse a list of CLI IP strings (each entry may be a
247/// single IP or a comma-separated list — the CLI flag is repeatable
248/// AND comma-friendly so `--source-ip 10.0.0.1,10.0.0.2` and
249/// `--source-ip 10.0.0.1 --source-ip 10.0.0.2` are equivalent).
250/// Malformed entries log a warning and are dropped; the bench
251/// continues with whatever resolved cleanly.
252fn parse_ip_list(raw: &[String], flag_name: &str) -> Vec<std::net::IpAddr> {
253    let mut out = Vec::new();
254    for entry in raw {
255        for piece in entry.split(',') {
256            let s = piece.trim();
257            if s.is_empty() {
258                continue;
259            }
260            match s.parse::<std::net::IpAddr>() {
261                Ok(ip) => out.push(ip),
262                Err(e) => {
263                    tracing::warn!(target: "mockforge::bench", "ignoring malformed --{flag_name} value '{s}': {e}");
264                }
265            }
266        }
267    }
268    out
269}
270
271impl BenchCommand {
272    /// Load and merge specs from --spec files and --spec-dir
273    pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
274        let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
275
276        // Load specs from --spec flags
277        if !self.spec.is_empty() {
278            let specs = load_specs_from_files(self.spec.clone())
279                .await
280                .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
281            all_specs.extend(specs);
282        }
283
284        // Load specs from --spec-dir if provided
285        if let Some(spec_dir) = &self.spec_dir {
286            let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
287                BenchError::Other(format!("Failed to load specs from directory: {}", e))
288            })?;
289            all_specs.extend(dir_specs);
290        }
291
292        if all_specs.is_empty() {
293            return Err(BenchError::Other(
294                "No spec files provided. Use --spec or --spec-dir.".to_string(),
295            ));
296        }
297
298        // If only one spec, return it directly (extract just the OpenApiSpec)
299        if all_specs.len() == 1 {
300            // Safe to unwrap because we just checked len() == 1
301            return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
302        }
303
304        // Merge multiple specs
305        let conflict_strategy = match self.merge_conflicts.as_str() {
306            "first" => ConflictStrategy::First,
307            "last" => ConflictStrategy::Last,
308            _ => ConflictStrategy::Error,
309        };
310
311        merge_specs(all_specs, conflict_strategy)
312            .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
313    }
314
315    /// Get a display name for the spec(s)
316    fn get_spec_display_name(&self) -> String {
317        if self.spec.len() == 1 {
318            self.spec[0].to_string_lossy().to_string()
319        } else if !self.spec.is_empty() {
320            format!("{} spec files", self.spec.len())
321        } else if let Some(dir) = &self.spec_dir {
322            format!("specs from {}", dir.display())
323        } else {
324            "no specs".to_string()
325        }
326    }
327
328    /// Execute the bench command
329    pub async fn execute(&self) -> Result<()> {
330        // Check if we're in multi-target mode
331        if let Some(targets_file) = &self.targets_file {
332            if self.conformance {
333                return self.execute_multi_target_conformance(targets_file).await;
334            }
335            return self.execute_multi_target(targets_file).await;
336        }
337
338        // Check if we're in sequential spec mode (for dependency handling)
339        if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
340            return self.execute_sequential_specs().await;
341        }
342
343        // Single target mode (existing behavior)
344        // Print header
345        TerminalReporter::print_header(
346            &self.get_spec_display_name(),
347            &self.target,
348            0, // Will be updated later
349            &self.scenario,
350            Self::parse_duration(&self.duration)?,
351        );
352
353        // Validate k6 installation
354        if !K6Executor::is_k6_installed() {
355            TerminalReporter::print_error("k6 is not installed");
356            TerminalReporter::print_warning(
357                "Install k6 from: https://k6.io/docs/get-started/installation/",
358            );
359            return Err(BenchError::K6NotFound);
360        }
361
362        // Check for conformance testing mode (before spec loading — conformance doesn't need a user spec)
363        if self.conformance {
364            return self.execute_conformance_test().await;
365        }
366
367        // Load and parse spec(s)
368        TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
369        let merged_spec = self.load_and_merge_specs().await?;
370        let parser = SpecParser::from_spec(merged_spec);
371        if self.spec.len() > 1 || self.spec_dir.is_some() {
372            TerminalReporter::print_success(&format!(
373                "Loaded and merged {} specification(s)",
374                self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
375            ));
376        } else {
377            TerminalReporter::print_success("Specification loaded");
378        }
379
380        // Check for mock server integration
381        let mock_config = self.build_mock_config().await;
382        if mock_config.is_mock_server {
383            TerminalReporter::print_progress("Mock server integration enabled");
384        }
385
386        // Check for CRUD flow mode
387        if self.crud_flow {
388            return self.execute_crud_flow(&parser).await;
389        }
390
391        // Check for OWASP API Top 10 testing mode
392        if self.owasp_api_top10 {
393            return self.execute_owasp_test(&parser).await;
394        }
395
396        // Get operations
397        TerminalReporter::print_progress("Extracting API operations...");
398        let mut operations = if let Some(filter) = &self.operations {
399            parser.filter_operations(filter)?
400        } else {
401            parser.get_operations()
402        };
403
404        // Apply exclusions if provided
405        if let Some(exclude) = &self.exclude_operations {
406            let before_count = operations.len();
407            operations = parser.exclude_operations(operations, exclude)?;
408            let excluded_count = before_count - operations.len();
409            if excluded_count > 0 {
410                TerminalReporter::print_progress(&format!(
411                    "Excluded {} operations matching '{}'",
412                    excluded_count, exclude
413                ));
414            }
415        }
416
417        if operations.is_empty() {
418            return Err(BenchError::Other("No operations found in spec".to_string()));
419        }
420
421        TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
422
423        // Load parameter overrides if provided
424        let param_overrides = if let Some(params_file) = &self.params_file {
425            TerminalReporter::print_progress("Loading parameter overrides...");
426            let overrides = ParameterOverrides::from_file(params_file)?;
427            TerminalReporter::print_success(&format!(
428                "Loaded parameter overrides ({} operation-specific, {} defaults)",
429                overrides.operations.len(),
430                if overrides.defaults.is_empty() { 0 } else { 1 }
431            ));
432            Some(overrides)
433        } else {
434            None
435        };
436
437        // Generate request templates
438        TerminalReporter::print_progress("Generating request templates...");
439        let templates: Vec<_> = operations
440            .iter()
441            .map(|op| {
442                let op_overrides = param_overrides.as_ref().map(|po| {
443                    po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
444                });
445                RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
446            })
447            .collect::<Result<Vec<_>>>()?;
448        TerminalReporter::print_success("Request templates generated");
449
450        // Parse headers
451        let custom_headers = self.parse_headers()?;
452
453        // Resolve base path (CLI option takes priority over spec's servers URL)
454        let base_path = self.resolve_base_path(&parser);
455        if let Some(ref bp) = base_path {
456            TerminalReporter::print_progress(&format!("Using base path: {}", bp));
457        }
458
459        // Generate k6 script
460        TerminalReporter::print_progress("Generating k6 load test script...");
461        let scenario =
462            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
463
464        let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
465
466        // Issue #79 round 6 follow-up — Srikanth reported k6 emitting
467        // "Insufficient VUs, reached 5 active VUs and cannot initialize more"
468        // when running `--rps 100 --vus 5`. With the `constant-arrival-rate`
469        // executor, k6 needs roughly `rps × avg_request_seconds` VUs to keep
470        // up; if `--vus` is too low it can't sustain the rate. Warn pre-flight
471        // so users know to bump `--vus` rather than chase the warning.
472        //
473        // Round 8 (#79): the static 100ms heuristic was wrong for fast targets
474        // (~2ms latency). Probe the actual target first to measure baseline
475        // latency, then derive a more accurate sizing recommendation. Fall
476        // back to the 100ms heuristic only when the probe can't reach the
477        // target (auth-gated endpoints, strict WAFs, etc).
478        //
479        // Round 9 (#79): factor in operation count. k6's constant-arrival-rate
480        // counts ITERATIONS, not requests — and every iteration runs all N
481        // operations sequentially, so required VUs scale with N. Srikanth's
482        // 12-op spec at --rps 100 with 15ms latency needs ~19 VUs, not 3.
483        let num_ops = operations.len() as u32;
484        if let Some(rps) = self.target_rps {
485            let probe =
486                crate::preflight::probe_target_latency(&self.target, 3, self.skip_tls_verify).await;
487
488            let (required_vus, basis) = match probe {
489                Some(p) => (
490                    p.required_vus(rps, num_ops),
491                    format!("avg {:.1}ms (measured)", p.avg_latency.as_secs_f64() * 1000.0),
492                ),
493                None => {
494                    // Static fallback: ~100ms heuristic × num_ops per iteration.
495                    let fallback = (rps as u64)
496                        .saturating_mul(num_ops.max(1) as u64)
497                        .div_ceil(10)
498                        .min(u32::MAX as u64) as u32;
499                    (fallback, "~100ms (default — probe failed)".to_string())
500                }
501            };
502
503            if self.vus < required_vus {
504                // Round 10 (#79): Srikanth's 11422-op spec at --rps 100 produced
505                // a recommendation of ~10,740 VUs, which is absurd in practice.
506                // When the recommendation goes super-linear, the real fix is to
507                // reduce the workload (use --operations filter), not bump VUs.
508                // Cap the suggestion at 1000 and steer the user toward filtering.
509                const VU_RECOMMENDATION_CAP: u32 = 1000;
510                let recommendation = required_vus.max(self.vus + 1);
511                if recommendation > VU_RECOMMENDATION_CAP {
512                    TerminalReporter::print_warning(&format!(
513                        "Workload is very large: --rps {} × {} ops/iteration × {} \
514                         baseline ⇒ ~{} VUs needed end-to-end, far beyond what's \
515                         practical to drive. Two ways to fix:\n  1. Reduce \
516                         operations per iteration with `--operations 'pattern,…'` \
517                         (or `--exclude-operations`) to focus the bench on a \
518                         representative subset.\n  2. Drop `--rps` and use \
519                         `--vus {}` alone — closed-model load runs as fast as \
520                         the VU pool allows, bounded by latency, with no per-\
521                         iteration deadline. Expect 1-iteration coverage of ~{} \
522                         operations in {}s.",
523                        rps,
524                        num_ops,
525                        basis,
526                        recommendation,
527                        self.vus.max(5),
528                        num_ops,
529                        Self::parse_duration(&self.duration).unwrap_or(0),
530                    ));
531                } else {
532                    TerminalReporter::print_warning(&format!(
533                        "--vus {} may be insufficient for --rps {} × {} ops/iteration \
534                         (baseline latency {}). k6's constant-arrival-rate counts ITERATIONS \
535                         and each runs every operation in the spec — required ≈ rps × ops × \
536                         latency_secs VUs. Bump --vus to ~{} if you see \"Insufficient VUs\" \
537                         warnings.",
538                        self.vus, rps, num_ops, basis, recommendation,
539                    ));
540                }
541            } else if probe.is_some() {
542                TerminalReporter::print_progress(&format!(
543                    "Pre-flight probe: target latency {}, {} ops/iteration — --vus {} \
544                     is sufficient for --rps {}",
545                    basis, num_ops, self.vus, rps,
546                ));
547            }
548        }
549
550        let k6_config = K6Config {
551            target_url: self.target.clone(),
552            base_path,
553            scenario,
554            duration_secs: Self::parse_duration(&self.duration)?,
555            max_vus: self.vus,
556            threshold_percentile: self.threshold_percentile.clone(),
557            threshold_ms: self.threshold_ms,
558            max_error_rate: self.max_error_rate,
559            auth_header: self.auth.clone(),
560            custom_headers,
561            skip_tls_verify: self.skip_tls_verify,
562            security_testing_enabled,
563            chunked_request_bodies: self.chunked_request_bodies,
564            target_rps: self.target_rps,
565            no_keep_alive: self.no_keep_alive,
566        };
567
568        let generator = K6ScriptGenerator::new(k6_config, templates);
569        let mut script = generator.generate()?;
570        TerminalReporter::print_success("k6 script generated");
571
572        // Check if any advanced features are enabled
573        let has_advanced_features = self.data_file.is_some()
574            || self.error_rate.is_some()
575            || self.security_test
576            || self.parallel_create.is_some()
577            || self.wafbench_dir.is_some();
578
579        // Enhance script with advanced features
580        if has_advanced_features {
581            script = self.generate_enhanced_script(&script)?;
582        }
583
584        // Add mock server integration code
585        if mock_config.is_mock_server {
586            let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
587            let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
588            let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
589
590            // Insert mock server code after imports
591            if let Some(import_end) = script.find("export const options") {
592                script.insert_str(
593                    import_end,
594                    &format!(
595                        "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
596                        helper_code, setup_code, teardown_code
597                    ),
598                );
599            }
600        }
601
602        // Validate the generated script
603        TerminalReporter::print_progress("Validating k6 script...");
604        let validation_errors = K6ScriptGenerator::validate_script(&script);
605        if !validation_errors.is_empty() {
606            TerminalReporter::print_error("Script validation failed");
607            for error in &validation_errors {
608                eprintln!("  {}", error);
609            }
610            return Err(BenchError::Other(format!(
611                "Generated k6 script has {} validation error(s). Please check the output above.",
612                validation_errors.len()
613            )));
614        }
615        TerminalReporter::print_success("Script validation passed");
616
617        // Write script to file
618        let script_path = if let Some(output) = &self.script_output {
619            output.clone()
620        } else {
621            self.output.join("k6-script.js")
622        };
623
624        if let Some(parent) = script_path.parent() {
625            std::fs::create_dir_all(parent)?;
626        }
627        std::fs::write(&script_path, &script)?;
628        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
629
630        // If generate-only mode, exit here
631        if self.generate_only {
632            println!("\nScript generated successfully. Run it with:");
633            println!("  k6 run {}", script_path.display());
634            return Ok(());
635        }
636
637        // Execute k6
638        TerminalReporter::print_progress("Executing load test...");
639        let executor = K6Executor::new()?;
640
641        std::fs::create_dir_all(&self.output)?;
642
643        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
644
645        // Print results
646        let duration_secs = Self::parse_duration(&self.duration)?;
647        TerminalReporter::print_summary_full(
648            &results,
649            duration_secs,
650            self.no_keep_alive,
651            Some(num_ops),
652        );
653
654        println!("\nResults saved to: {}", self.output.display());
655
656        Ok(())
657    }
658
659    /// Execute multi-target bench testing
660    async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
661        TerminalReporter::print_progress("Parsing targets file...");
662        let targets = parse_targets_file(targets_file)?;
663        let num_targets = targets.len();
664        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
665
666        if targets.is_empty() {
667            return Err(BenchError::Other("No targets found in file".to_string()));
668        }
669
670        // Determine max concurrency
671        let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
672        let max_concurrency = max_concurrency.min(num_targets); // Don't exceed number of targets
673
674        // Print header for multi-target mode
675        TerminalReporter::print_header(
676            &self.get_spec_display_name(),
677            &format!("{} targets", num_targets),
678            0,
679            &self.scenario,
680            Self::parse_duration(&self.duration)?,
681        );
682
683        // Create parallel executor
684        let executor = ParallelExecutor::new(
685            BenchCommand {
686                // Clone all fields except targets_file (we don't need it in the executor)
687                spec: self.spec.clone(),
688                spec_dir: self.spec_dir.clone(),
689                merge_conflicts: self.merge_conflicts.clone(),
690                spec_mode: self.spec_mode.clone(),
691                dependency_config: self.dependency_config.clone(),
692                target: self.target.clone(), // Not used in multi-target mode, but kept for compatibility
693                base_path: self.base_path.clone(),
694                duration: self.duration.clone(),
695                vus: self.vus,
696                target_rps: self.target_rps,
697                no_keep_alive: self.no_keep_alive,
698                scenario: self.scenario.clone(),
699                operations: self.operations.clone(),
700                exclude_operations: self.exclude_operations.clone(),
701                auth: self.auth.clone(),
702                headers: self.headers.clone(),
703                output: self.output.clone(),
704                generate_only: self.generate_only,
705                script_output: self.script_output.clone(),
706                threshold_percentile: self.threshold_percentile.clone(),
707                threshold_ms: self.threshold_ms,
708                max_error_rate: self.max_error_rate,
709                verbose: self.verbose,
710                skip_tls_verify: self.skip_tls_verify,
711                chunked_request_bodies: self.chunked_request_bodies,
712                targets_file: None,
713                max_concurrency: None,
714                results_format: self.results_format.clone(),
715                params_file: self.params_file.clone(),
716                crud_flow: self.crud_flow,
717                flow_config: self.flow_config.clone(),
718                extract_fields: self.extract_fields.clone(),
719                parallel_create: self.parallel_create,
720                data_file: self.data_file.clone(),
721                data_distribution: self.data_distribution.clone(),
722                data_mappings: self.data_mappings.clone(),
723                per_uri_control: self.per_uri_control,
724                error_rate: self.error_rate,
725                error_types: self.error_types.clone(),
726                security_test: self.security_test,
727                security_payloads: self.security_payloads.clone(),
728                security_categories: self.security_categories.clone(),
729                security_target_fields: self.security_target_fields.clone(),
730                wafbench_dir: self.wafbench_dir.clone(),
731                wafbench_cycle_all: self.wafbench_cycle_all,
732                owasp_api_top10: self.owasp_api_top10,
733                owasp_categories: self.owasp_categories.clone(),
734                owasp_auth_header: self.owasp_auth_header.clone(),
735                owasp_auth_token: self.owasp_auth_token.clone(),
736                owasp_admin_paths: self.owasp_admin_paths.clone(),
737                owasp_id_fields: self.owasp_id_fields.clone(),
738                owasp_report: self.owasp_report.clone(),
739                owasp_report_format: self.owasp_report_format.clone(),
740                owasp_iterations: self.owasp_iterations,
741                conformance: false,
742                conformance_api_key: None,
743                conformance_basic_auth: None,
744                conformance_report: PathBuf::from("conformance-report.json"),
745                conformance_categories: None,
746                conformance_report_format: "json".to_string(),
747                conformance_headers: vec![],
748                conformance_all_operations: false,
749                conformance_custom: None,
750                conformance_delay_ms: 0,
751                use_k6: false,
752                conformance_custom_filter: None,
753                export_requests: false,
754                validate_requests: false,
755                conformance_self_test: false,
756                source_ips: Vec::new(),
757                geo_source_ips: Vec::new(),
758                geo_source_headers: Vec::new(),
759            },
760            targets,
761            max_concurrency,
762        );
763
764        // Execute all targets
765        let start_time = std::time::Instant::now();
766        let aggregated_results = executor.execute_all().await?;
767        let elapsed = start_time.elapsed();
768
769        // Organize and report results
770        self.report_multi_target_results(&aggregated_results, elapsed)?;
771
772        Ok(())
773    }
774
775    /// Report results for multi-target execution
776    fn report_multi_target_results(
777        &self,
778        results: &AggregatedResults,
779        elapsed: std::time::Duration,
780    ) -> Result<()> {
781        // Print summary
782        TerminalReporter::print_multi_target_summary(results);
783
784        // Print elapsed time
785        let total_secs = elapsed.as_secs();
786        let hours = total_secs / 3600;
787        let minutes = (total_secs % 3600) / 60;
788        let seconds = total_secs % 60;
789        if hours > 0 {
790            println!("\n  Total Elapsed Time:   {}h {}m {}s", hours, minutes, seconds);
791        } else if minutes > 0 {
792            println!("\n  Total Elapsed Time:   {}m {}s", minutes, seconds);
793        } else {
794            println!("\n  Total Elapsed Time:   {}s", seconds);
795        }
796
797        // Save aggregated summary if requested
798        if self.results_format == "aggregated" || self.results_format == "both" {
799            let summary_path = self.output.join("aggregated_summary.json");
800            let summary_json = serde_json::json!({
801                "total_elapsed_seconds": elapsed.as_secs(),
802                "total_targets": results.total_targets,
803                "successful_targets": results.successful_targets,
804                "failed_targets": results.failed_targets,
805                "aggregated_metrics": {
806                    "total_requests": results.aggregated_metrics.total_requests,
807                    "total_failed_requests": results.aggregated_metrics.total_failed_requests,
808                    "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
809                    "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
810                    "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
811                    "error_rate": results.aggregated_metrics.error_rate,
812                    "total_rps": results.aggregated_metrics.total_rps,
813                    "avg_rps": results.aggregated_metrics.avg_rps,
814                    "total_vus_max": results.aggregated_metrics.total_vus_max,
815                },
816                "target_results": results.target_results.iter().map(|r| {
817                    serde_json::json!({
818                        "target_url": r.target_url,
819                        "target_index": r.target_index,
820                        "success": r.success,
821                        "error": r.error,
822                        "total_requests": r.results.total_requests,
823                        "failed_requests": r.results.failed_requests,
824                        "avg_duration_ms": r.results.avg_duration_ms,
825                        "min_duration_ms": r.results.min_duration_ms,
826                        "med_duration_ms": r.results.med_duration_ms,
827                        "p90_duration_ms": r.results.p90_duration_ms,
828                        "p95_duration_ms": r.results.p95_duration_ms,
829                        "p99_duration_ms": r.results.p99_duration_ms,
830                        "max_duration_ms": r.results.max_duration_ms,
831                        "rps": r.results.rps,
832                        "vus_max": r.results.vus_max,
833                        "output_dir": r.output_dir.to_string_lossy(),
834                    })
835                }).collect::<Vec<_>>(),
836            });
837
838            std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
839            TerminalReporter::print_success(&format!(
840                "Aggregated summary saved to: {}",
841                summary_path.display()
842            ));
843        }
844
845        // Write CSV with all per-target results for easy parsing
846        let csv_path = self.output.join("all_targets.csv");
847        let mut csv = String::from(
848            "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
849        );
850        for r in &results.target_results {
851            csv.push_str(&format!(
852                "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
853                r.target_url,
854                r.success,
855                r.results.total_requests,
856                r.results.failed_requests,
857                r.results.rps,
858                r.results.vus_max,
859                r.results.min_duration_ms,
860                r.results.avg_duration_ms,
861                r.results.med_duration_ms,
862                r.results.p90_duration_ms,
863                r.results.p95_duration_ms,
864                r.results.p99_duration_ms,
865                r.results.max_duration_ms,
866                r.error.as_deref().unwrap_or(""),
867            ));
868        }
869        let _ = std::fs::write(&csv_path, &csv);
870
871        println!("\nResults saved to: {}", self.output.display());
872        println!("  - Per-target results: {}", self.output.join("target_*").display());
873        println!("  - All targets CSV:    {}", csv_path.display());
874        if self.results_format == "aggregated" || self.results_format == "both" {
875            println!(
876                "  - Aggregated summary: {}",
877                self.output.join("aggregated_summary.json").display()
878            );
879        }
880
881        Ok(())
882    }
883
884    /// Parse duration string (e.g., "30s", "5m", "1h") to seconds
885    pub fn parse_duration(duration: &str) -> Result<u64> {
886        let duration = duration.trim();
887
888        if let Some(secs) = duration.strip_suffix('s') {
889            secs.parse::<u64>()
890                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
891        } else if let Some(mins) = duration.strip_suffix('m') {
892            mins.parse::<u64>()
893                .map(|m| m * 60)
894                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
895        } else if let Some(hours) = duration.strip_suffix('h') {
896            hours
897                .parse::<u64>()
898                .map(|h| h * 3600)
899                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
900        } else {
901            // Try parsing as seconds without suffix
902            duration
903                .parse::<u64>()
904                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
905        }
906    }
907
908    /// Parse headers from command line format (Key:Value,Key2:Value2)
909    pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
910        match &self.headers {
911            Some(s) => parse_header_string(s),
912            None => Ok(HashMap::new()),
913        }
914    }
915
916    fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
917        let extracted_path = output_dir.join("extracted_values.json");
918        if !extracted_path.exists() {
919            return Ok(ExtractedValues::new());
920        }
921
922        let content = std::fs::read_to_string(&extracted_path)
923            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
924        let parsed: serde_json::Value = serde_json::from_str(&content)
925            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
926
927        let mut extracted = ExtractedValues::new();
928        if let Some(values) = parsed.as_object() {
929            for (key, value) in values {
930                extracted.set(key.clone(), value.clone());
931            }
932        }
933
934        Ok(extracted)
935    }
936
937    /// Resolve the effective base path for API endpoints
938    ///
939    /// Priority:
940    /// 1. CLI --base-path option (if provided, even if empty string)
941    /// 2. Base path extracted from OpenAPI spec's servers URL
942    /// 3. None (no base path)
943    ///
944    /// An empty string from CLI explicitly disables base path.
945    fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
946        // CLI option takes priority (including empty string to disable)
947        if let Some(cli_base_path) = &self.base_path {
948            if cli_base_path.is_empty() {
949                // Empty string explicitly means "no base path"
950                return None;
951            }
952            return Some(cli_base_path.clone());
953        }
954
955        // Fall back to spec's base path
956        parser.get_base_path()
957    }
958
959    /// Build mock server integration configuration
960    async fn build_mock_config(&self) -> MockIntegrationConfig {
961        // Check if target looks like a mock server
962        if MockServerDetector::looks_like_mock_server(&self.target) {
963            // Try to detect if it's actually a MockForge server
964            if let Ok(info) = MockServerDetector::detect(&self.target).await {
965                if info.is_mockforge {
966                    TerminalReporter::print_success(&format!(
967                        "Detected MockForge server (version: {})",
968                        info.version.as_deref().unwrap_or("unknown")
969                    ));
970                    return MockIntegrationConfig::mock_server();
971                }
972            }
973        }
974        MockIntegrationConfig::real_api()
975    }
976
977    /// Build CRUD flow configuration
978    fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
979        if !self.crud_flow {
980            return None;
981        }
982
983        // If flow_config file is provided, load it
984        if let Some(config_path) = &self.flow_config {
985            match CrudFlowConfig::from_file(config_path) {
986                Ok(config) => return Some(config),
987                Err(e) => {
988                    TerminalReporter::print_warning(&format!(
989                        "Failed to load flow config: {}. Using auto-detection.",
990                        e
991                    ));
992                }
993            }
994        }
995
996        // Parse extract fields
997        let extract_fields = self
998            .extract_fields
999            .as_ref()
1000            .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
1001            .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
1002
1003        Some(CrudFlowConfig {
1004            flows: Vec::new(), // Will be auto-detected
1005            default_extract_fields: extract_fields,
1006        })
1007    }
1008
1009    /// Build data-driven testing configuration
1010    fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
1011        let data_file = self.data_file.as_ref()?;
1012
1013        let distribution = DataDistribution::from_str(&self.data_distribution)
1014            .unwrap_or(DataDistribution::UniquePerVu);
1015
1016        let mappings = self
1017            .data_mappings
1018            .as_ref()
1019            .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
1020            .unwrap_or_default();
1021
1022        Some(DataDrivenConfig {
1023            file_path: data_file.to_string_lossy().to_string(),
1024            distribution,
1025            mappings,
1026            csv_has_header: true,
1027            per_uri_control: self.per_uri_control,
1028            per_uri_columns: crate::data_driven::PerUriColumns::default(),
1029        })
1030    }
1031
1032    /// Build invalid data testing configuration
1033    fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
1034        let error_rate = self.error_rate?;
1035
1036        let error_types = self
1037            .error_types
1038            .as_ref()
1039            .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
1040            .unwrap_or_default();
1041
1042        Some(InvalidDataConfig {
1043            error_rate,
1044            error_types,
1045            target_fields: Vec::new(),
1046        })
1047    }
1048
1049    /// Build security testing configuration
1050    fn build_security_config(&self) -> Option<SecurityTestConfig> {
1051        if !self.security_test {
1052            return None;
1053        }
1054
1055        let categories = self
1056            .security_categories
1057            .as_ref()
1058            .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1059            .unwrap_or_else(|| {
1060                let mut default = HashSet::new();
1061                default.insert(SecurityCategory::SqlInjection);
1062                default.insert(SecurityCategory::Xss);
1063                default
1064            });
1065
1066        let target_fields = self
1067            .security_target_fields
1068            .as_ref()
1069            .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1070            .unwrap_or_default();
1071
1072        let custom_payloads_file =
1073            self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1074
1075        Some(SecurityTestConfig {
1076            enabled: true,
1077            categories,
1078            target_fields,
1079            custom_payloads_file,
1080            include_high_risk: false,
1081        })
1082    }
1083
1084    /// Build parallel execution configuration
1085    fn build_parallel_config(&self) -> Option<ParallelConfig> {
1086        let count = self.parallel_create?;
1087
1088        Some(ParallelConfig::new(count))
1089    }
1090
1091    /// Load WAFBench payloads from the specified directory or pattern
1092    fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
1093        let Some(ref wafbench_dir) = self.wafbench_dir else {
1094            return Vec::new();
1095        };
1096
1097        let mut loader = WafBenchLoader::new();
1098
1099        if let Err(e) = loader.load_from_pattern(wafbench_dir) {
1100            TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
1101            return Vec::new();
1102        }
1103
1104        let stats = loader.stats();
1105
1106        if stats.files_processed == 0 {
1107            TerminalReporter::print_warning(&format!(
1108                "No WAFBench YAML files found matching '{}'",
1109                wafbench_dir
1110            ));
1111            // Also report any parse errors that may explain why no files were processed
1112            if !stats.parse_errors.is_empty() {
1113                TerminalReporter::print_warning("Some files were found but failed to parse:");
1114                for error in &stats.parse_errors {
1115                    TerminalReporter::print_warning(&format!("  - {}", error));
1116                }
1117            }
1118            return Vec::new();
1119        }
1120
1121        TerminalReporter::print_progress(&format!(
1122            "Loaded {} WAFBench files, {} test cases, {} payloads",
1123            stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1124        ));
1125
1126        // Print category breakdown
1127        for (category, count) in &stats.by_category {
1128            TerminalReporter::print_progress(&format!("  - {}: {} tests", category, count));
1129        }
1130
1131        // Report any parse errors
1132        for error in &stats.parse_errors {
1133            TerminalReporter::print_warning(&format!("  Parse error: {}", error));
1134        }
1135
1136        loader.to_security_payloads()
1137    }
1138
1139    /// Generate enhanced k6 script with advanced features
1140    pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1141        let mut enhanced_script = base_script.to_string();
1142        let mut additional_code = String::new();
1143
1144        // Add data-driven testing code
1145        if let Some(config) = self.build_data_driven_config() {
1146            TerminalReporter::print_progress("Adding data-driven testing support...");
1147            additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1148            additional_code.push('\n');
1149            TerminalReporter::print_success("Data-driven testing enabled");
1150        }
1151
1152        // Add invalid data generation code
1153        if let Some(config) = self.build_invalid_data_config() {
1154            TerminalReporter::print_progress("Adding invalid data testing support...");
1155            additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1156            additional_code.push('\n');
1157            additional_code
1158                .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1159            additional_code.push('\n');
1160            additional_code
1161                .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1162            additional_code.push('\n');
1163            TerminalReporter::print_success(&format!(
1164                "Invalid data testing enabled ({}% error rate)",
1165                (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1166            ));
1167        }
1168
1169        // Add security testing code
1170        let security_config = self.build_security_config();
1171        let wafbench_payloads = self.load_wafbench_payloads();
1172        let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1173
1174        if security_config.is_some() || !wafbench_payloads.is_empty() {
1175            TerminalReporter::print_progress("Adding security testing support...");
1176
1177            // Combine built-in payloads with WAFBench payloads
1178            let mut payload_list: Vec<SecurityPayload> = Vec::new();
1179
1180            if let Some(ref config) = security_config {
1181                payload_list.extend(SecurityPayloads::get_payloads(config));
1182            }
1183
1184            // Add WAFBench payloads
1185            if !wafbench_payloads.is_empty() {
1186                TerminalReporter::print_progress(&format!(
1187                    "Loading {} WAFBench attack patterns...",
1188                    wafbench_payloads.len()
1189                ));
1190                payload_list.extend(wafbench_payloads);
1191            }
1192
1193            let target_fields =
1194                security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1195
1196            additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1197                &payload_list,
1198                self.wafbench_cycle_all,
1199            ));
1200            additional_code.push('\n');
1201            additional_code
1202                .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1203            additional_code.push('\n');
1204            additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1205            additional_code.push('\n');
1206
1207            let mode = if self.wafbench_cycle_all {
1208                "cycle-all"
1209            } else {
1210                "random"
1211            };
1212            TerminalReporter::print_success(&format!(
1213                "Security testing enabled ({} payloads, {} mode)",
1214                payload_list.len(),
1215                mode
1216            ));
1217        } else if security_requested {
1218            // User requested security testing (e.g., --wafbench-dir) but no payloads were loaded.
1219            // The template has security_testing_enabled=true so it renders calling code.
1220            // We must inject stub definitions to avoid undefined function references.
1221            TerminalReporter::print_warning(
1222                "Security testing was requested but no payloads were loaded. \
1223                 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1224            );
1225            additional_code
1226                .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1227            additional_code.push('\n');
1228            additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1229            additional_code.push('\n');
1230        }
1231
1232        // Add parallel execution code
1233        if let Some(config) = self.build_parallel_config() {
1234            TerminalReporter::print_progress("Adding parallel execution support...");
1235            additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1236            additional_code.push('\n');
1237            TerminalReporter::print_success(&format!(
1238                "Parallel execution enabled (count: {})",
1239                config.count
1240            ));
1241        }
1242
1243        // Insert additional code after the imports section
1244        if !additional_code.is_empty() {
1245            // Find the end of the import section
1246            if let Some(import_end) = enhanced_script.find("export const options") {
1247                enhanced_script.insert_str(
1248                    import_end,
1249                    &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1250                );
1251            }
1252        }
1253
1254        Ok(enhanced_script)
1255    }
1256
1257    /// Execute specs sequentially with dependency ordering and value passing
1258    async fn execute_sequential_specs(&self) -> Result<()> {
1259        TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1260
1261        // Load all specs (without merging)
1262        let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1263
1264        if !self.spec.is_empty() {
1265            let specs = load_specs_from_files(self.spec.clone())
1266                .await
1267                .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1268            all_specs.extend(specs);
1269        }
1270
1271        if let Some(spec_dir) = &self.spec_dir {
1272            let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1273                BenchError::Other(format!("Failed to load specs from directory: {}", e))
1274            })?;
1275            all_specs.extend(dir_specs);
1276        }
1277
1278        if all_specs.is_empty() {
1279            return Err(BenchError::Other(
1280                "No spec files found for sequential execution".to_string(),
1281            ));
1282        }
1283
1284        TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1285
1286        // Load dependency config or auto-detect
1287        let execution_order = if let Some(config_path) = &self.dependency_config {
1288            TerminalReporter::print_progress("Loading dependency configuration...");
1289            let config = SpecDependencyConfig::from_file(config_path)?;
1290
1291            if !config.disable_auto_detect && config.execution_order.is_empty() {
1292                // Auto-detect if config doesn't specify order
1293                self.detect_and_sort_specs(&all_specs)?
1294            } else {
1295                // Use configured order
1296                config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1297            }
1298        } else {
1299            // Auto-detect dependencies
1300            self.detect_and_sort_specs(&all_specs)?
1301        };
1302
1303        TerminalReporter::print_success(&format!(
1304            "Execution order: {}",
1305            execution_order
1306                .iter()
1307                .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1308                .collect::<Vec<_>>()
1309                .join(" → ")
1310        ));
1311
1312        // Execute each spec in order
1313        let mut extracted_values = ExtractedValues::new();
1314        let total_specs = execution_order.len();
1315
1316        for (index, spec_path) in execution_order.iter().enumerate() {
1317            let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1318
1319            TerminalReporter::print_progress(&format!(
1320                "[{}/{}] Executing spec: {}",
1321                index + 1,
1322                total_specs,
1323                spec_name
1324            ));
1325
1326            // Find the spec in our loaded specs (match by full path or filename)
1327            let spec = all_specs
1328                .iter()
1329                .find(|(p, _)| {
1330                    p == spec_path
1331                        || p.file_name() == spec_path.file_name()
1332                        || p.file_name() == Some(spec_path.as_os_str())
1333                })
1334                .map(|(_, s)| s.clone())
1335                .ok_or_else(|| {
1336                    BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1337                })?;
1338
1339            // Execute this spec with any extracted values from previous specs
1340            let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1341
1342            // Merge extracted values for the next spec
1343            extracted_values.merge(&new_values);
1344
1345            TerminalReporter::print_success(&format!(
1346                "[{}/{}] Completed: {} (extracted {} values)",
1347                index + 1,
1348                total_specs,
1349                spec_name,
1350                new_values.values.len()
1351            ));
1352        }
1353
1354        TerminalReporter::print_success(&format!(
1355            "Sequential execution complete: {} specs executed",
1356            total_specs
1357        ));
1358
1359        Ok(())
1360    }
1361
1362    /// Detect dependencies and return topologically sorted spec paths
1363    fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1364        TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1365
1366        let mut detector = DependencyDetector::new();
1367        let dependencies = detector.detect_dependencies(specs);
1368
1369        if dependencies.is_empty() {
1370            TerminalReporter::print_progress("No dependencies detected, using file order");
1371            return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1372        }
1373
1374        TerminalReporter::print_progress(&format!(
1375            "Detected {} cross-spec dependencies",
1376            dependencies.len()
1377        ));
1378
1379        for dep in &dependencies {
1380            TerminalReporter::print_progress(&format!(
1381                "  {} → {} (via field '{}')",
1382                dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1383                dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1384                dep.field_name
1385            ));
1386        }
1387
1388        topological_sort(specs, &dependencies)
1389    }
1390
1391    /// Execute a single spec and extract values for dependent specs
1392    async fn execute_single_spec(
1393        &self,
1394        spec: &OpenApiSpec,
1395        spec_name: &str,
1396        _external_values: &ExtractedValues,
1397    ) -> Result<ExtractedValues> {
1398        let parser = SpecParser::from_spec(spec.clone());
1399
1400        // For now, we execute in CRUD flow mode if enabled, otherwise standard mode
1401        if self.crud_flow {
1402            // Execute CRUD flow and extract values
1403            self.execute_crud_flow_with_extraction(&parser, spec_name).await
1404        } else {
1405            // Execute standard benchmark (no value extraction in non-CRUD mode)
1406            self.execute_standard_spec(&parser, spec_name).await?;
1407            Ok(ExtractedValues::new())
1408        }
1409    }
1410
1411    /// Execute CRUD flow with value extraction for sequential mode
1412    async fn execute_crud_flow_with_extraction(
1413        &self,
1414        parser: &SpecParser,
1415        spec_name: &str,
1416    ) -> Result<ExtractedValues> {
1417        let operations = parser.get_operations();
1418        let flows = CrudFlowDetector::detect_flows(&operations);
1419
1420        if flows.is_empty() {
1421            TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1422            return Ok(ExtractedValues::new());
1423        }
1424
1425        TerminalReporter::print_progress(&format!(
1426            "  {} CRUD flow(s) in {}",
1427            flows.len(),
1428            spec_name
1429        ));
1430
1431        // Generate and execute the CRUD flow script
1432        let mut handlebars = handlebars::Handlebars::new();
1433        // Register json helper for serializing arrays/objects in templates
1434        handlebars.register_helper(
1435            "json",
1436            Box::new(
1437                |h: &handlebars::Helper,
1438                 _: &handlebars::Handlebars,
1439                 _: &handlebars::Context,
1440                 _: &mut handlebars::RenderContext,
1441                 out: &mut dyn handlebars::Output|
1442                 -> handlebars::HelperResult {
1443                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1444                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1445                    Ok(())
1446                },
1447            ),
1448        );
1449        let template = include_str!("templates/k6_crud_flow.hbs");
1450        let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1451
1452        let custom_headers = self.parse_headers()?;
1453        let config = self.build_crud_flow_config().unwrap_or_default();
1454
1455        // Load parameter overrides if provided (for body configurations)
1456        let param_overrides = if let Some(params_file) = &self.params_file {
1457            let overrides = ParameterOverrides::from_file(params_file)?;
1458            Some(overrides)
1459        } else {
1460            None
1461        };
1462
1463        // Generate stages from scenario
1464        let duration_secs = Self::parse_duration(&self.duration)?;
1465        let scenario =
1466            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1467        let stages = scenario.generate_stages(duration_secs, self.vus);
1468
1469        // Resolve base path (CLI option takes priority over spec's servers URL)
1470        let api_base_path = self.resolve_base_path(parser);
1471
1472        // Build headers JSON string for the template
1473        let mut all_headers = custom_headers.clone();
1474        if let Some(auth) = &self.auth {
1475            all_headers.insert("Authorization".to_string(), auth.clone());
1476        }
1477        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1478
1479        // Track all dynamic placeholders across all operations
1480        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1481
1482        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1483            // Use the metric-name sanitizer (caps at 112 chars + hash suffix)
1484            // so deeply nested flow names don't blow past k6's 128-char limit
1485            // when concatenated with `_step{i}_latency`. See issue #79.
1486            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1487            serde_json::json!({
1488                "name": sanitized_name.clone(),
1489                "display_name": f.name,
1490                "base_path": f.base_path,
1491                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1492                    // Parse operation to get method and path
1493                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1494                    let method_raw = if !parts.is_empty() {
1495                        parts[0].to_uppercase()
1496                    } else {
1497                        "GET".to_string()
1498                    };
1499                    let method = if !parts.is_empty() {
1500                        let m = parts[0].to_lowercase();
1501                        // k6 uses 'del' for DELETE
1502                        if m == "delete" { "del".to_string() } else { m }
1503                    } else {
1504                        "get".to_string()
1505                    };
1506                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1507                    // Prepend API base path if configured
1508                    let path = if let Some(ref bp) = api_base_path {
1509                        format!("{}{}", bp, raw_path)
1510                    } else {
1511                        raw_path.to_string()
1512                    };
1513                    let is_get_or_head = method == "get" || method == "head";
1514                    // POST, PUT, PATCH typically have bodies
1515                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1516
1517                    // Look up body from params file if available
1518                    let body_value = if has_body {
1519                        param_overrides.as_ref()
1520                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1521                            .and_then(|oo| oo.body)
1522                            .unwrap_or_else(|| serde_json::json!({}))
1523                    } else {
1524                        serde_json::json!({})
1525                    };
1526
1527                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
1528                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1529
1530                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
1531                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1532                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1533
1534                    serde_json::json!({
1535                        "operation": s.operation,
1536                        "method": method,
1537                        "path": path,
1538                        "extract": s.extract,
1539                        "use_values": s.use_values,
1540                        "use_body": s.use_body,
1541                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1542                        "inject_attacks": s.inject_attacks,
1543                        "attack_types": s.attack_types,
1544                        "description": s.description,
1545                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1546                        "is_get_or_head": is_get_or_head,
1547                        "has_body": has_body,
1548                        "body": processed_body.value,
1549                        "body_is_dynamic": body_is_dynamic,
1550                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1551                    })
1552                }).collect::<Vec<_>>(),
1553            })
1554        }).collect();
1555
1556        // Collect all placeholders from all steps
1557        for flow_data in &flows_data {
1558            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1559                for step in steps {
1560                    if let Some(placeholders_arr) =
1561                        step.get("_placeholders").and_then(|p| p.as_array())
1562                    {
1563                        for p_str in placeholders_arr {
1564                            if let Some(p_name) = p_str.as_str() {
1565                                match p_name {
1566                                    "VU" => {
1567                                        all_placeholders.insert(DynamicPlaceholder::VU);
1568                                    }
1569                                    "Iteration" => {
1570                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
1571                                    }
1572                                    "Timestamp" => {
1573                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
1574                                    }
1575                                    "UUID" => {
1576                                        all_placeholders.insert(DynamicPlaceholder::UUID);
1577                                    }
1578                                    "Random" => {
1579                                        all_placeholders.insert(DynamicPlaceholder::Random);
1580                                    }
1581                                    "Counter" => {
1582                                        all_placeholders.insert(DynamicPlaceholder::Counter);
1583                                    }
1584                                    "Date" => {
1585                                        all_placeholders.insert(DynamicPlaceholder::Date);
1586                                    }
1587                                    "VuIter" => {
1588                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
1589                                    }
1590                                    _ => {}
1591                                }
1592                            }
1593                        }
1594                    }
1595                }
1596            }
1597        }
1598
1599        // Get required imports and globals based on placeholders used
1600        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1601        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1602
1603        // Check if security testing is enabled
1604        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1605
1606        let data = serde_json::json!({
1607            "base_url": self.target,
1608            "flows": flows_data,
1609            "extract_fields": config.default_extract_fields,
1610            "duration_secs": duration_secs,
1611            "max_vus": self.vus,
1612            "auth_header": self.auth,
1613            "custom_headers": custom_headers,
1614            "skip_tls_verify": self.skip_tls_verify,
1615            // Add missing template fields
1616            "stages": stages.iter().map(|s| serde_json::json!({
1617                "duration": s.duration,
1618                "target": s.target,
1619            })).collect::<Vec<_>>(),
1620            "threshold_percentile": self.threshold_percentile,
1621            "threshold_ms": self.threshold_ms,
1622            "max_error_rate": self.max_error_rate,
1623            "headers": headers_json,
1624            "dynamic_imports": required_imports,
1625            "dynamic_globals": required_globals,
1626            "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
1627            // Security testing settings
1628            "security_testing_enabled": security_testing_enabled,
1629            "has_custom_headers": !custom_headers.is_empty(),
1630        });
1631
1632        let mut script = handlebars
1633            .render_template(template, &data)
1634            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1635
1636        // Enhance script with security testing support if enabled
1637        if security_testing_enabled {
1638            script = self.generate_enhanced_script(&script)?;
1639        }
1640
1641        // Write and execute script
1642        let script_path =
1643            self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
1644
1645        std::fs::create_dir_all(self.output.clone())?;
1646        std::fs::write(&script_path, &script)?;
1647
1648        if !self.generate_only {
1649            let executor = K6Executor::new()?;
1650            std::fs::create_dir_all(&output_dir)?;
1651
1652            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1653
1654            let extracted = Self::parse_extracted_values(&output_dir)?;
1655            TerminalReporter::print_progress(&format!(
1656                "  Extracted {} value(s) from {}",
1657                extracted.values.len(),
1658                spec_name
1659            ));
1660            return Ok(extracted);
1661        }
1662
1663        Ok(ExtractedValues::new())
1664    }
1665
1666    /// Execute standard (non-CRUD) spec benchmark
1667    async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
1668        let mut operations = if let Some(filter) = &self.operations {
1669            parser.filter_operations(filter)?
1670        } else {
1671            parser.get_operations()
1672        };
1673
1674        if let Some(exclude) = &self.exclude_operations {
1675            operations = parser.exclude_operations(operations, exclude)?;
1676        }
1677
1678        if operations.is_empty() {
1679            TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
1680            return Ok(());
1681        }
1682
1683        TerminalReporter::print_progress(&format!(
1684            "  {} operations in {}",
1685            operations.len(),
1686            spec_name
1687        ));
1688
1689        // Generate request templates
1690        let templates: Vec<_> = operations
1691            .iter()
1692            .map(RequestGenerator::generate_template)
1693            .collect::<Result<Vec<_>>>()?;
1694
1695        // Parse headers
1696        let custom_headers = self.parse_headers()?;
1697
1698        // Resolve base path
1699        let base_path = self.resolve_base_path(parser);
1700
1701        // Generate k6 script
1702        let scenario =
1703            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1704
1705        let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
1706
1707        let k6_config = K6Config {
1708            target_url: self.target.clone(),
1709            base_path,
1710            scenario,
1711            duration_secs: Self::parse_duration(&self.duration)?,
1712            max_vus: self.vus,
1713            threshold_percentile: self.threshold_percentile.clone(),
1714            threshold_ms: self.threshold_ms,
1715            max_error_rate: self.max_error_rate,
1716            auth_header: self.auth.clone(),
1717            custom_headers,
1718            skip_tls_verify: self.skip_tls_verify,
1719            security_testing_enabled,
1720            chunked_request_bodies: self.chunked_request_bodies,
1721            target_rps: self.target_rps,
1722            no_keep_alive: self.no_keep_alive,
1723        };
1724
1725        let generator = K6ScriptGenerator::new(k6_config, templates);
1726        let mut script = generator.generate()?;
1727
1728        // Enhance script with advanced features (security testing, etc.)
1729        let has_advanced_features = self.data_file.is_some()
1730            || self.error_rate.is_some()
1731            || self.security_test
1732            || self.parallel_create.is_some()
1733            || self.wafbench_dir.is_some();
1734
1735        if has_advanced_features {
1736            script = self.generate_enhanced_script(&script)?;
1737        }
1738
1739        // Write and execute script
1740        let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
1741
1742        std::fs::create_dir_all(self.output.clone())?;
1743        std::fs::write(&script_path, &script)?;
1744
1745        if !self.generate_only {
1746            let executor = K6Executor::new()?;
1747            let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1748            std::fs::create_dir_all(&output_dir)?;
1749
1750            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1751        }
1752
1753        Ok(())
1754    }
1755
1756    /// Execute CRUD flow testing mode
1757    async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
1758        // Check if a custom flow config is provided
1759        let config = self.build_crud_flow_config().unwrap_or_default();
1760
1761        // Use flows from config if provided, otherwise auto-detect
1762        let flows = if !config.flows.is_empty() {
1763            TerminalReporter::print_progress("Using custom flow configuration...");
1764            config.flows.clone()
1765        } else {
1766            TerminalReporter::print_progress("Detecting CRUD operations...");
1767            let operations = parser.get_operations();
1768            CrudFlowDetector::detect_flows(&operations)
1769        };
1770
1771        if flows.is_empty() {
1772            return Err(BenchError::Other(
1773                "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
1774            ));
1775        }
1776
1777        if config.flows.is_empty() {
1778            TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
1779        } else {
1780            TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
1781        }
1782
1783        for flow in &flows {
1784            TerminalReporter::print_progress(&format!(
1785                "  - {}: {} steps",
1786                flow.name,
1787                flow.steps.len()
1788            ));
1789        }
1790
1791        // Generate CRUD flow script
1792        let mut handlebars = handlebars::Handlebars::new();
1793        // Register json helper for serializing arrays/objects in templates
1794        handlebars.register_helper(
1795            "json",
1796            Box::new(
1797                |h: &handlebars::Helper,
1798                 _: &handlebars::Handlebars,
1799                 _: &handlebars::Context,
1800                 _: &mut handlebars::RenderContext,
1801                 out: &mut dyn handlebars::Output|
1802                 -> handlebars::HelperResult {
1803                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1804                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1805                    Ok(())
1806                },
1807            ),
1808        );
1809        let template = include_str!("templates/k6_crud_flow.hbs");
1810
1811        let custom_headers = self.parse_headers()?;
1812
1813        // Load parameter overrides if provided (for body configurations)
1814        let param_overrides = if let Some(params_file) = &self.params_file {
1815            TerminalReporter::print_progress("Loading parameter overrides...");
1816            let overrides = ParameterOverrides::from_file(params_file)?;
1817            TerminalReporter::print_success(&format!(
1818                "Loaded parameter overrides ({} operation-specific, {} defaults)",
1819                overrides.operations.len(),
1820                if overrides.defaults.is_empty() { 0 } else { 1 }
1821            ));
1822            Some(overrides)
1823        } else {
1824            None
1825        };
1826
1827        // Generate stages from scenario
1828        let duration_secs = Self::parse_duration(&self.duration)?;
1829        let scenario =
1830            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1831        let stages = scenario.generate_stages(duration_secs, self.vus);
1832
1833        // Resolve base path (CLI option takes priority over spec's servers URL)
1834        let api_base_path = self.resolve_base_path(parser);
1835        if let Some(ref bp) = api_base_path {
1836            TerminalReporter::print_progress(&format!("Using base path: {}", bp));
1837        }
1838
1839        // Build headers JSON string for the template
1840        let mut all_headers = custom_headers.clone();
1841        if let Some(auth) = &self.auth {
1842            all_headers.insert("Authorization".to_string(), auth.clone());
1843        }
1844        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1845
1846        // Track all dynamic placeholders across all operations
1847        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1848
1849        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1850            // Sanitize flow name for use as JavaScript variable and k6 metric names.
1851            // Use the metric-name sanitizer (caps at 112 chars + hash suffix) so
1852            // deeply nested flow names don't blow past k6's 128-char limit when
1853            // concatenated with `_step{i}_latency`. See issue #79.
1854            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1855            serde_json::json!({
1856                "name": sanitized_name.clone(),  // Use sanitized name for variable names
1857                "display_name": f.name,          // Keep original for comments/display
1858                "base_path": f.base_path,
1859                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1860                    // Parse operation to get method and path
1861                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1862                    let method_raw = if !parts.is_empty() {
1863                        parts[0].to_uppercase()
1864                    } else {
1865                        "GET".to_string()
1866                    };
1867                    let method = if !parts.is_empty() {
1868                        let m = parts[0].to_lowercase();
1869                        // k6 uses 'del' for DELETE
1870                        if m == "delete" { "del".to_string() } else { m }
1871                    } else {
1872                        "get".to_string()
1873                    };
1874                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1875                    // Prepend API base path if configured
1876                    let path = if let Some(ref bp) = api_base_path {
1877                        format!("{}{}", bp, raw_path)
1878                    } else {
1879                        raw_path.to_string()
1880                    };
1881                    let is_get_or_head = method == "get" || method == "head";
1882                    // POST, PUT, PATCH typically have bodies
1883                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1884
1885                    // Look up body from params file if available (use raw_path for matching)
1886                    let body_value = if has_body {
1887                        param_overrides.as_ref()
1888                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1889                            .and_then(|oo| oo.body)
1890                            .unwrap_or_else(|| serde_json::json!({}))
1891                    } else {
1892                        serde_json::json!({})
1893                    };
1894
1895                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
1896                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1897                    // Note: all_placeholders is captured by the closure but we can't mutate it directly
1898                    // We'll collect placeholders separately below
1899
1900                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
1901                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1902                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1903
1904                    serde_json::json!({
1905                        "operation": s.operation,
1906                        "method": method,
1907                        "path": path,
1908                        "extract": s.extract,
1909                        "use_values": s.use_values,
1910                        "use_body": s.use_body,
1911                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1912                        "inject_attacks": s.inject_attacks,
1913                        "attack_types": s.attack_types,
1914                        "description": s.description,
1915                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1916                        "is_get_or_head": is_get_or_head,
1917                        "has_body": has_body,
1918                        "body": processed_body.value,
1919                        "body_is_dynamic": body_is_dynamic,
1920                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1921                    })
1922                }).collect::<Vec<_>>(),
1923            })
1924        }).collect();
1925
1926        // Collect all placeholders from all steps
1927        for flow_data in &flows_data {
1928            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1929                for step in steps {
1930                    if let Some(placeholders_arr) =
1931                        step.get("_placeholders").and_then(|p| p.as_array())
1932                    {
1933                        for p_str in placeholders_arr {
1934                            if let Some(p_name) = p_str.as_str() {
1935                                // Parse placeholder from debug string
1936                                match p_name {
1937                                    "VU" => {
1938                                        all_placeholders.insert(DynamicPlaceholder::VU);
1939                                    }
1940                                    "Iteration" => {
1941                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
1942                                    }
1943                                    "Timestamp" => {
1944                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
1945                                    }
1946                                    "UUID" => {
1947                                        all_placeholders.insert(DynamicPlaceholder::UUID);
1948                                    }
1949                                    "Random" => {
1950                                        all_placeholders.insert(DynamicPlaceholder::Random);
1951                                    }
1952                                    "Counter" => {
1953                                        all_placeholders.insert(DynamicPlaceholder::Counter);
1954                                    }
1955                                    "Date" => {
1956                                        all_placeholders.insert(DynamicPlaceholder::Date);
1957                                    }
1958                                    "VuIter" => {
1959                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
1960                                    }
1961                                    _ => {}
1962                                }
1963                            }
1964                        }
1965                    }
1966                }
1967            }
1968        }
1969
1970        // Get required imports and globals based on placeholders used
1971        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1972        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1973
1974        // Build invalid data config if error injection is enabled
1975        let invalid_data_config = self.build_invalid_data_config();
1976        let error_injection_enabled = invalid_data_config.is_some();
1977        let error_rate = self.error_rate.unwrap_or(0.0);
1978        let error_types: Vec<String> = invalid_data_config
1979            .as_ref()
1980            .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
1981            .unwrap_or_default();
1982
1983        if error_injection_enabled {
1984            TerminalReporter::print_progress(&format!(
1985                "Error injection enabled ({}% rate)",
1986                (error_rate * 100.0) as u32
1987            ));
1988        }
1989
1990        // Check if security testing is enabled
1991        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1992
1993        let data = serde_json::json!({
1994            "base_url": self.target,
1995            "flows": flows_data,
1996            "extract_fields": config.default_extract_fields,
1997            "duration_secs": duration_secs,
1998            "max_vus": self.vus,
1999            "auth_header": self.auth,
2000            "custom_headers": custom_headers,
2001            "skip_tls_verify": self.skip_tls_verify,
2002            // Add missing template fields
2003            "stages": stages.iter().map(|s| serde_json::json!({
2004                "duration": s.duration,
2005                "target": s.target,
2006            })).collect::<Vec<_>>(),
2007            "threshold_percentile": self.threshold_percentile,
2008            "threshold_ms": self.threshold_ms,
2009            "max_error_rate": self.max_error_rate,
2010            "headers": headers_json,
2011            "dynamic_imports": required_imports,
2012            "dynamic_globals": required_globals,
2013            "extracted_values_output_path": self
2014                .output
2015                .join("crud_flow_extracted_values.json")
2016                .to_string_lossy(),
2017            // Error injection settings
2018            "error_injection_enabled": error_injection_enabled,
2019            "error_rate": error_rate,
2020            "error_types": error_types,
2021            // Security testing settings
2022            "security_testing_enabled": security_testing_enabled,
2023            "has_custom_headers": !custom_headers.is_empty(),
2024        });
2025
2026        let mut script = handlebars
2027            .render_template(template, &data)
2028            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2029
2030        // Enhance script with security testing support if enabled
2031        if security_testing_enabled {
2032            script = self.generate_enhanced_script(&script)?;
2033        }
2034
2035        // Validate the generated CRUD flow script
2036        TerminalReporter::print_progress("Validating CRUD flow script...");
2037        let validation_errors = K6ScriptGenerator::validate_script(&script);
2038        if !validation_errors.is_empty() {
2039            TerminalReporter::print_error("CRUD flow script validation failed");
2040            for error in &validation_errors {
2041                eprintln!("  {}", error);
2042            }
2043            return Err(BenchError::Other(format!(
2044                "CRUD flow script validation failed with {} error(s)",
2045                validation_errors.len()
2046            )));
2047        }
2048
2049        TerminalReporter::print_success("CRUD flow script generated");
2050
2051        // Write and execute script
2052        let script_path = if let Some(output) = &self.script_output {
2053            output.clone()
2054        } else {
2055            self.output.join("k6-crud-flow-script.js")
2056        };
2057
2058        if let Some(parent) = script_path.parent() {
2059            std::fs::create_dir_all(parent)?;
2060        }
2061        std::fs::write(&script_path, &script)?;
2062        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2063
2064        if self.generate_only {
2065            println!("\nScript generated successfully. Run it with:");
2066            println!("  k6 run {}", script_path.display());
2067            return Ok(());
2068        }
2069
2070        // Execute k6
2071        TerminalReporter::print_progress("Executing CRUD flow test...");
2072        let executor = K6Executor::new()?;
2073        std::fs::create_dir_all(&self.output)?;
2074
2075        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2076
2077        let duration_secs = Self::parse_duration(&self.duration)?;
2078        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2079
2080        Ok(())
2081    }
2082
2083    /// Execute OpenAPI 3.0.0 conformance testing mode
2084    async fn execute_conformance_test(&self) -> Result<()> {
2085        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2086        use crate::conformance::report::ConformanceReport;
2087        use crate::conformance::spec::ConformanceFeature;
2088
2089        TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2090
2091        // Conformance testing is a functional correctness check (1 VU, 1 iteration).
2092        // --vus and -d flags are always ignored in this mode.
2093        TerminalReporter::print_progress(
2094            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2095        );
2096
2097        // Parse category filter
2098        let categories = self.conformance_categories.as_ref().map(|cats_str| {
2099            cats_str
2100                .split(',')
2101                .filter_map(|s| {
2102                    let trimmed = s.trim();
2103                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2104                        Some(canonical.to_string())
2105                    } else {
2106                        TerminalReporter::print_warning(&format!(
2107                            "Unknown conformance category: '{}'. Valid categories: {}",
2108                            trimmed,
2109                            ConformanceFeature::cli_category_names()
2110                                .iter()
2111                                .map(|(cli, _)| *cli)
2112                                .collect::<Vec<_>>()
2113                                .join(", ")
2114                        ));
2115                        None
2116                    }
2117                })
2118                .collect::<Vec<String>>()
2119        });
2120
2121        // Parse custom headers from "Key: Value" format
2122        let custom_headers: Vec<(String, String)> = self
2123            .conformance_headers
2124            .iter()
2125            .filter_map(|h| {
2126                let (name, value) = h.split_once(':')?;
2127                Some((name.trim().to_string(), value.trim().to_string()))
2128            })
2129            .collect();
2130
2131        if !custom_headers.is_empty() {
2132            TerminalReporter::print_progress(&format!(
2133                "Using {} custom header(s) for authentication",
2134                custom_headers.len()
2135            ));
2136        }
2137
2138        if self.conformance_delay_ms > 0 {
2139            TerminalReporter::print_progress(&format!(
2140                "Using {}ms delay between conformance requests",
2141                self.conformance_delay_ms
2142            ));
2143        }
2144
2145        // Ensure output dir exists so canonicalize works for the report path
2146        std::fs::create_dir_all(&self.output)?;
2147
2148        let config = ConformanceConfig {
2149            target_url: self.target.clone(),
2150            api_key: self.conformance_api_key.clone(),
2151            basic_auth: self.conformance_basic_auth.clone(),
2152            skip_tls_verify: self.skip_tls_verify,
2153            categories,
2154            base_path: self.base_path.clone(),
2155            custom_headers,
2156            output_dir: Some(self.output.clone()),
2157            all_operations: self.conformance_all_operations,
2158            custom_checks_file: self.conformance_custom.clone(),
2159            request_delay_ms: self.conformance_delay_ms,
2160            custom_filter: self.conformance_custom_filter.clone(),
2161            export_requests: self.export_requests,
2162            validate_requests: self.validate_requests,
2163        };
2164
2165        // Branch: spec-driven mode vs reference mode
2166        // Annotate operations if spec is provided (used by both native and k6 paths)
2167        // Round 18.1 — resolve the spec's base path so the self-test
2168        // path can prepend it to every URL. Pre-fix, self-test
2169        // ignored `--base-path /api` and hit the bare spec path,
2170        // returning 404 for every request on specs whose server is
2171        // proxied behind a base prefix.
2172        let mut resolved_base_path: Option<String> = None;
2173        let annotated_ops = if !self.spec.is_empty() {
2174            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2175            let parser = SpecParser::from_file(&self.spec[0]).await?;
2176            resolved_base_path = self.resolve_base_path(&parser);
2177
2178            // Issue #79 round 12 — Srikanth ran `--conformance --operations "GET,POST"`
2179            // and saw DELETE/PATCH exercised anyway. Conformance silently ignored
2180            // the filter. Apply it (and `--exclude-operations`) the same way the
2181            // regular bench path does so users can scope the run.
2182            let mut operations = if let Some(filter) = &self.operations {
2183                parser.filter_operations(filter)?
2184            } else {
2185                parser.get_operations()
2186            };
2187            if let Some(exclude) = &self.exclude_operations {
2188                let before_count = operations.len();
2189                operations = parser.exclude_operations(operations, exclude)?;
2190                let excluded_count = before_count - operations.len();
2191                if excluded_count > 0 {
2192                    TerminalReporter::print_progress(&format!(
2193                        "Excluded {} operations matching '{}'",
2194                        excluded_count, exclude
2195                    ));
2196                }
2197            }
2198
2199            let annotated =
2200                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2201                    &operations,
2202                    parser.spec(),
2203                );
2204            TerminalReporter::print_success(&format!(
2205                "Analyzed {} operations, found {} feature annotations",
2206                operations.len(),
2207                annotated.iter().map(|a| a.features.len()).sum::<usize>()
2208            ));
2209            Some(annotated)
2210        } else {
2211            None
2212        };
2213
2214        // Issue #79 round 13 (4) — `--conformance-self-test` replaces
2215        // the standard conformance run with a positive + per-category
2216        // negative driver that verifies the server actually rejects
2217        // bad requests with 4xx. Wires the spec-annotated operations
2218        // through `conformance::self_test::run_self_test` and prints
2219        // the resulting pass/fail matrix.
2220        if self.conformance_self_test {
2221            let Some(ops) = annotated_ops else {
2222                TerminalReporter::print_error(
2223                    "--conformance-self-test requires --spec; no operations to test",
2224                );
2225                return Ok(());
2226            };
2227            let cfg = crate::conformance::self_test::SelfTestConfig {
2228                target_url: self.target.clone(),
2229                skip_tls_verify: self.skip_tls_verify,
2230                timeout: std::time::Duration::from_secs(30),
2231                // `custom_headers` was already moved into the
2232                // `ConformanceConfig` above; re-derive from `self` so
2233                // we don't borrow it twice.
2234                extra_headers: self
2235                    .conformance_headers
2236                    .iter()
2237                    .filter_map(|h| {
2238                        let (n, v) = h.split_once(':')?;
2239                        Some((n.trim().to_string(), v.trim().to_string()))
2240                    })
2241                    .collect(),
2242                delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
2243                // Round 18.1 — honour `--base-path` (or the spec's
2244                // own first server prefix) so a deployment served
2245                // under a path-prefix doesn't 404 every positive.
2246                base_path: resolved_base_path.clone(),
2247                // Round 18.5 — GEODB multi-source-IP. Parse CLI IP
2248                // lists (malformed entries log a warning and are
2249                // dropped). Empty lists keep pre-18.5 behaviour.
2250                source_ips: parse_ip_list(&self.source_ips, "source-ip"),
2251                geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
2252                geo_source_headers: if self.geo_source_headers.is_empty() {
2253                    crate::conformance::self_test::default_geo_source_headers()
2254                } else {
2255                    self.geo_source_headers.clone()
2256                },
2257            };
2258            TerminalReporter::print_progress(&format!(
2259                "Self-test mode: driving {} operations with positive + per-category negative cases",
2260                ops.len()
2261            ));
2262            let report = crate::conformance::self_test::run_self_test(&ops, &cfg)
2263                .await
2264                .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2265            TerminalReporter::print_progress(&report.render_summary());
2266            // Persist the JSON report alongside the regular conformance
2267            // report so it's grep-able next to the buffer dump from the
2268            // admin endpoint.
2269            let json_path = self.output.join("conformance-self-test.json");
2270            if let Ok(json) = serde_json::to_string_pretty(&report) {
2271                let _ = std::fs::write(&json_path, json);
2272                TerminalReporter::print_progress(&format!(
2273                    "Self-test report written to {}",
2274                    json_path.display()
2275                ));
2276            }
2277            // Round 18.1 — surface the "every positive failed with
2278            // the same status" case loudly. Without this, a user
2279            // who forgot `--base-path /api` saw 404 for every
2280            // request, but the per-category negative rollup looked
2281            // all-green (because 404 is in the 4xx range the
2282            // negatives expect). Now the run is correctly called
2283            // out as misconfigured before showing the (meaningless)
2284            // negative results.
2285            if let Some(status) = report.detect_target_misconfiguration() {
2286                let hint = match status {
2287                    404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
2288                    401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
2289                    _ => "",
2290                };
2291                TerminalReporter::print_warning(&format!(
2292                    "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
2293                ));
2294            } else if !report.all_passed() {
2295                TerminalReporter::print_warning(
2296                    "Self-test detected gaps — server let through at least one request that should have been a 4xx",
2297                );
2298            } else {
2299                TerminalReporter::print_success(
2300                    "Self-test passed — all positive cases accepted and all negative cases rejected",
2301                );
2302            }
2303            // Round 17.6 — emit a self-contained HTML report alongside
2304            // the JSON. Groups by category and surfaces the missed-
2305            // negative list directly so a user doesn't need to grep
2306            // through the JSON to find which routes failed which
2307            // checks. Optionally folds in a round-17.4 spec audit
2308            // report if one exists in the same output directory.
2309            let html_path = self.output.join("conformance-report.html");
2310            let audit_path = self.output.join("conformance-spec-audit.json");
2311            let audit_value = std::fs::read_to_string(&audit_path)
2312                .ok()
2313                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
2314            let html = crate::conformance::report_html::render_html(&report, audit_value.as_ref());
2315            if std::fs::write(&html_path, html).is_ok() {
2316                TerminalReporter::print_progress(&format!(
2317                    "HTML report written to {}",
2318                    html_path.display()
2319                ));
2320            }
2321            return Ok(());
2322        }
2323
2324        // Request validation against OpenAPI spec (if --validate-requests is set)
2325        if self.validate_requests && !self.spec.is_empty() {
2326            TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2327            let violation_count = crate::conformance::request_validator::run_request_validation(
2328                &self.spec,
2329                self.conformance_custom.as_deref(),
2330                self.base_path.as_deref(),
2331                &self.output,
2332            )
2333            .await?;
2334            if violation_count > 0 {
2335                TerminalReporter::print_warning(&format!(
2336                    "{} request validation violation(s) found — see conformance-request-violations.json",
2337                    violation_count
2338                ));
2339            } else {
2340                TerminalReporter::print_success("All requests conform to the OpenAPI spec");
2341            }
2342        }
2343
2344        // If generate-only OR --use-k6, use the k6 script generation path
2345        if self.generate_only || self.use_k6 {
2346            let script = if let Some(annotated) = &annotated_ops {
2347                let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2348                    config,
2349                    annotated.clone(),
2350                );
2351                let op_count = gen.operation_count();
2352                let (script, check_count) = gen.generate()?;
2353                TerminalReporter::print_success(&format!(
2354                    "Conformance: {} operations analyzed, {} unique checks generated",
2355                    op_count, check_count
2356                ));
2357                script
2358            } else {
2359                let generator = ConformanceGenerator::new(config);
2360                generator.generate()?
2361            };
2362
2363            let script_path = self.output.join("k6-conformance.js");
2364            std::fs::write(&script_path, &script).map_err(|e| {
2365                BenchError::Other(format!("Failed to write conformance script: {}", e))
2366            })?;
2367            TerminalReporter::print_success(&format!(
2368                "Conformance script generated: {}",
2369                script_path.display()
2370            ));
2371
2372            if self.generate_only {
2373                println!("\nScript generated. Run with:");
2374                println!("  k6 run {}", script_path.display());
2375                return Ok(());
2376            }
2377
2378            // --use-k6: execute via k6
2379            if !K6Executor::is_k6_installed() {
2380                TerminalReporter::print_error("k6 is not installed");
2381                TerminalReporter::print_warning(
2382                    "Install k6 from: https://k6.io/docs/get-started/installation/",
2383                );
2384                return Err(BenchError::K6NotFound);
2385            }
2386
2387            TerminalReporter::print_progress("Running conformance tests via k6...");
2388            let executor = K6Executor::new()?;
2389            executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2390
2391            let report_path = self.output.join("conformance-report.json");
2392            if report_path.exists() {
2393                let report = ConformanceReport::from_file(&report_path)?;
2394                report.print_report_with_options(self.conformance_all_operations);
2395                self.save_conformance_report(&report, &report_path)?;
2396            } else {
2397                TerminalReporter::print_warning(
2398                    "Conformance report not generated (k6 handleSummary may not have run)",
2399                );
2400            }
2401
2402            return Ok(());
2403        }
2404
2405        // Default: Native Rust executor (no k6 dependency)
2406        TerminalReporter::print_progress("Running conformance tests (native executor)...");
2407
2408        let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2409
2410        executor = if let Some(annotated) = &annotated_ops {
2411            executor.with_spec_driven_checks(annotated)
2412        } else {
2413            executor.with_reference_checks()
2414        };
2415        executor = executor.with_custom_checks()?;
2416
2417        TerminalReporter::print_success(&format!(
2418            "Executing {} conformance checks...",
2419            executor.check_count()
2420        ));
2421
2422        let report = executor.execute().await?;
2423        report.print_report_with_options(self.conformance_all_operations);
2424
2425        // Save failure details to a separate file for easy debugging
2426        let failure_details = report.failure_details();
2427        if !failure_details.is_empty() {
2428            let details_path = self.output.join("conformance-failure-details.json");
2429            if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2430                let _ = std::fs::write(&details_path, json);
2431                TerminalReporter::print_success(&format!(
2432                    "Failure details saved to: {}",
2433                    details_path.display()
2434                ));
2435            }
2436        }
2437
2438        // Save report
2439        let report_path = self.output.join("conformance-report.json");
2440        let report_json = serde_json::to_string_pretty(&report.to_json())
2441            .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2442        std::fs::write(&report_path, &report_json)
2443            .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2444        TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
2445
2446        self.save_conformance_report(&report, &report_path)?;
2447
2448        Ok(())
2449    }
2450
2451    /// Save conformance report in the requested format (SARIF or JSON copy)
2452    fn save_conformance_report(
2453        &self,
2454        report: &crate::conformance::report::ConformanceReport,
2455        report_path: &Path,
2456    ) -> Result<()> {
2457        if self.conformance_report_format == "sarif" {
2458            use crate::conformance::sarif::ConformanceSarifReport;
2459            ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
2460            TerminalReporter::print_success(&format!(
2461                "SARIF report saved to: {}",
2462                self.conformance_report.display()
2463            ));
2464        } else if self.conformance_report != *report_path {
2465            std::fs::copy(report_path, &self.conformance_report)?;
2466            TerminalReporter::print_success(&format!(
2467                "Report saved to: {}",
2468                self.conformance_report.display()
2469            ));
2470        }
2471        Ok(())
2472    }
2473
2474    /// Execute conformance tests against multiple targets from a targets file.
2475    ///
2476    /// Uses the native `NativeConformanceExecutor` (no k6 dependency). Targets are
2477    /// tested sequentially to avoid overwhelming them, and per-target headers from
2478    /// the targets file are merged with the base `--conformance-header` headers.
2479    async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
2480        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2481        use crate::conformance::report::ConformanceReport;
2482        use crate::conformance::spec::ConformanceFeature;
2483
2484        TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
2485
2486        // Parse targets file
2487        TerminalReporter::print_progress("Parsing targets file...");
2488        let targets = parse_targets_file(targets_file)?;
2489        let num_targets = targets.len();
2490        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
2491
2492        if targets.is_empty() {
2493            return Err(BenchError::Other("No targets found in file".to_string()));
2494        }
2495
2496        TerminalReporter::print_progress(
2497            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2498        );
2499
2500        // Parse category filter (shared across all targets)
2501        let categories = self.conformance_categories.as_ref().map(|cats_str| {
2502            cats_str
2503                .split(',')
2504                .filter_map(|s| {
2505                    let trimmed = s.trim();
2506                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2507                        Some(canonical.to_string())
2508                    } else {
2509                        TerminalReporter::print_warning(&format!(
2510                            "Unknown conformance category: '{}'. Valid categories: {}",
2511                            trimmed,
2512                            ConformanceFeature::cli_category_names()
2513                                .iter()
2514                                .map(|(cli, _)| *cli)
2515                                .collect::<Vec<_>>()
2516                                .join(", ")
2517                        ));
2518                        None
2519                    }
2520                })
2521                .collect::<Vec<String>>()
2522        });
2523
2524        // Parse base custom headers from --conformance-header flags
2525        let base_custom_headers: Vec<(String, String)> = self
2526            .conformance_headers
2527            .iter()
2528            .filter_map(|h| {
2529                let (name, value) = h.split_once(':')?;
2530                Some((name.trim().to_string(), value.trim().to_string()))
2531            })
2532            .collect();
2533
2534        if !base_custom_headers.is_empty() {
2535            TerminalReporter::print_progress(&format!(
2536                "Using {} base custom header(s) for authentication",
2537                base_custom_headers.len()
2538            ));
2539        }
2540
2541        // Load spec once if provided (shared across all targets)
2542        let annotated_ops = if !self.spec.is_empty() {
2543            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2544            let parser = SpecParser::from_file(&self.spec[0]).await?;
2545            let operations = parser.get_operations();
2546            let annotated =
2547                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2548                    &operations,
2549                    parser.spec(),
2550                );
2551            TerminalReporter::print_success(&format!(
2552                "Analyzed {} operations, found {} feature annotations",
2553                operations.len(),
2554                annotated.iter().map(|a| a.features.len()).sum::<usize>()
2555            ));
2556            Some(annotated)
2557        } else {
2558            None
2559        };
2560
2561        // Ensure output dir exists
2562        std::fs::create_dir_all(&self.output)?;
2563
2564        // Collect per-target results for the summary
2565        struct TargetResult {
2566            url: String,
2567            passed: usize,
2568            failed: usize,
2569            elapsed: std::time::Duration,
2570            report_json: serde_json::Value,
2571            owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
2572        }
2573
2574        let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
2575        let total_start = std::time::Instant::now();
2576
2577        for (idx, target) in targets.iter().enumerate() {
2578            tracing::info!(
2579                "Running conformance tests against target {}/{}: {}",
2580                idx + 1,
2581                num_targets,
2582                target.url
2583            );
2584            TerminalReporter::print_progress(&format!(
2585                "\n--- Target {}/{}: {} ---",
2586                idx + 1,
2587                num_targets,
2588                target.url
2589            ));
2590
2591            // Merge base headers with per-target headers
2592            let mut merged_headers = base_custom_headers.clone();
2593            if let Some(ref target_headers) = target.headers {
2594                for (name, value) in target_headers {
2595                    // Per-target headers override base headers with the same name
2596                    if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
2597                        existing.1 = value.clone();
2598                    } else {
2599                        merged_headers.push((name.clone(), value.clone()));
2600                    }
2601                }
2602            }
2603            // Add auth header if present on target
2604            if let Some(ref auth) = target.auth {
2605                if let Some(existing) =
2606                    merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
2607                {
2608                    existing.1 = auth.clone();
2609                } else {
2610                    merged_headers.push(("Authorization".to_string(), auth.clone()));
2611                }
2612            }
2613
2614            // Per-target output dir (used by both native and k6 paths).
2615            // Created before the config so we can point the k6 script's
2616            // handleSummary at the per-target directory rather than the shared
2617            // parent output dir (otherwise every target would overwrite the
2618            // same conformance-report.json).
2619            let target_dir = self.output.join(format!("target_{}", idx));
2620            std::fs::create_dir_all(&target_dir)?;
2621
2622            let config = ConformanceConfig {
2623                target_url: target.url.clone(),
2624                api_key: self.conformance_api_key.clone(),
2625                basic_auth: self.conformance_basic_auth.clone(),
2626                skip_tls_verify: self.skip_tls_verify,
2627                categories: categories.clone(),
2628                base_path: self.base_path.clone(),
2629                custom_headers: merged_headers,
2630                output_dir: Some(target_dir.clone()),
2631                all_operations: self.conformance_all_operations,
2632                custom_checks_file: self.conformance_custom.clone(),
2633                request_delay_ms: self.conformance_delay_ms,
2634                custom_filter: self.conformance_custom_filter.clone(),
2635                export_requests: self.export_requests,
2636                validate_requests: self.validate_requests,
2637            };
2638
2639            let target_start = std::time::Instant::now();
2640            let report = if self.use_k6 {
2641                if !K6Executor::is_k6_installed() {
2642                    TerminalReporter::print_error("k6 is not installed");
2643                    TerminalReporter::print_warning(
2644                        "Install k6 from: https://k6.io/docs/get-started/installation/",
2645                    );
2646                    return Err(BenchError::K6NotFound);
2647                }
2648
2649                let script = if let Some(ref annotated) = annotated_ops {
2650                    let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2651                        config.clone(),
2652                        annotated.clone(),
2653                    );
2654                    let (script, _check_count) = gen.generate()?;
2655                    script
2656                } else {
2657                    let generator = ConformanceGenerator::new(config.clone());
2658                    generator.generate()?
2659                };
2660
2661                let script_path = target_dir.join("k6-conformance.js");
2662                std::fs::write(&script_path, &script).map_err(|e| {
2663                    BenchError::Other(format!("Failed to write conformance script: {}", e))
2664                })?;
2665                TerminalReporter::print_success(&format!(
2666                    "Conformance script generated: {}",
2667                    script_path.display()
2668                ));
2669
2670                TerminalReporter::print_progress(&format!(
2671                    "Running conformance tests via k6 against {}...",
2672                    target.url
2673                ));
2674                let k6 = K6Executor::new()?;
2675                // Unique k6 API port per target to avoid collisions.
2676                let api_port = 6565u16.saturating_add(idx as u16);
2677                k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
2678                    .await?;
2679
2680                let report_path = target_dir.join("conformance-report.json");
2681                if report_path.exists() {
2682                    ConformanceReport::from_file(&report_path)?
2683                } else {
2684                    TerminalReporter::print_warning(&format!(
2685                        "Conformance report not generated for target {} (k6 handleSummary may not have run)",
2686                        target.url
2687                    ));
2688                    continue;
2689                }
2690            } else {
2691                let mut executor =
2692                    crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2693
2694                executor = if let Some(ref annotated) = annotated_ops {
2695                    executor.with_spec_driven_checks(annotated)
2696                } else {
2697                    executor.with_reference_checks()
2698                };
2699                executor = executor.with_custom_checks()?;
2700
2701                TerminalReporter::print_success(&format!(
2702                    "Executing {} conformance checks against {}...",
2703                    executor.check_count(),
2704                    target.url
2705                ));
2706
2707                executor.execute().await?
2708            };
2709            let target_elapsed = target_start.elapsed();
2710
2711            let report_json = report.to_json();
2712
2713            // Extract pass/fail from the summary in the JSON
2714            let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
2715            let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
2716            let total_checks = passed + failed;
2717            let rate = if total_checks == 0 {
2718                0.0
2719            } else {
2720                (passed as f64 / total_checks as f64) * 100.0
2721            };
2722
2723            TerminalReporter::print_success(&format!(
2724                "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
2725                target.url,
2726                passed,
2727                total_checks,
2728                rate,
2729                target_elapsed.as_secs_f64()
2730            ));
2731
2732            // Save per-target report (target_dir created above)
2733            let target_report_path = target_dir.join("conformance-report.json");
2734            let report_str = serde_json::to_string_pretty(&report_json)
2735                .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2736            std::fs::write(&target_report_path, &report_str)
2737                .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2738
2739            // Save failure details if any
2740            let failure_details = report.failure_details();
2741            if !failure_details.is_empty() {
2742                let details_path = target_dir.join("conformance-failure-details.json");
2743                if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2744                    let _ = std::fs::write(&details_path, json);
2745                }
2746            }
2747
2748            // Compute OWASP coverage for this target
2749            let owasp_coverage = report.owasp_coverage_data();
2750
2751            target_results.push(TargetResult {
2752                url: target.url.clone(),
2753                passed,
2754                failed,
2755                elapsed: target_elapsed,
2756                report_json,
2757                owasp_coverage,
2758            });
2759        }
2760
2761        let total_elapsed = total_start.elapsed();
2762
2763        // Print summary table
2764        println!("\n{}", "=".repeat(80));
2765        println!("  Multi-Target Conformance Summary");
2766        println!("{}", "=".repeat(80));
2767        println!(
2768            "  {:<40} {:>8} {:>8} {:>8} {:>8}",
2769            "Target URL", "Passed", "Failed", "Rate", "Time"
2770        );
2771        println!("  {}", "-".repeat(76));
2772
2773        let mut total_passed = 0usize;
2774        let mut total_failed = 0usize;
2775
2776        for result in &target_results {
2777            let total_checks = result.passed + result.failed;
2778            let rate = if total_checks == 0 {
2779                0.0
2780            } else {
2781                (result.passed as f64 / total_checks as f64) * 100.0
2782            };
2783
2784            // Truncate long URLs for display
2785            let display_url = if result.url.len() > 38 {
2786                format!("{}...", &result.url[..35])
2787            } else {
2788                result.url.clone()
2789            };
2790
2791            println!(
2792                "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2793                display_url,
2794                result.passed,
2795                result.failed,
2796                rate,
2797                result.elapsed.as_secs_f64()
2798            );
2799
2800            total_passed += result.passed;
2801            total_failed += result.failed;
2802        }
2803
2804        let grand_total = total_passed + total_failed;
2805        let overall_rate = if grand_total == 0 {
2806            0.0
2807        } else {
2808            (total_passed as f64 / grand_total as f64) * 100.0
2809        };
2810
2811        println!("  {}", "-".repeat(76));
2812        println!(
2813            "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2814            format!("TOTAL ({} targets)", num_targets),
2815            total_passed,
2816            total_failed,
2817            overall_rate,
2818            total_elapsed.as_secs_f64()
2819        );
2820        println!("{}", "=".repeat(80));
2821
2822        // Print per-target OWASP coverage
2823        for result in &target_results {
2824            println!("\n  OWASP API Security Top 10 Coverage for {}:", result.url);
2825            for entry in &result.owasp_coverage {
2826                let status = if !entry.tested {
2827                    "-"
2828                } else if entry.all_passed {
2829                    "pass"
2830                } else {
2831                    "FAIL"
2832                };
2833                let via = if entry.via_categories.is_empty() {
2834                    String::new()
2835                } else {
2836                    format!(" (via {})", entry.via_categories.join(", "))
2837                };
2838                println!("    {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
2839            }
2840        }
2841
2842        // Save combined summary
2843        let per_target_summaries: Vec<serde_json::Value> = target_results
2844            .iter()
2845            .enumerate()
2846            .map(|(idx, r)| {
2847                let total_checks = r.passed + r.failed;
2848                let rate = if total_checks == 0 {
2849                    0.0
2850                } else {
2851                    (r.passed as f64 / total_checks as f64) * 100.0
2852                };
2853                let owasp_json: Vec<serde_json::Value> = r
2854                    .owasp_coverage
2855                    .iter()
2856                    .map(|e| {
2857                        serde_json::json!({
2858                            "id": e.id,
2859                            "name": e.name,
2860                            "tested": e.tested,
2861                            "all_passed": e.all_passed,
2862                            "via_categories": e.via_categories,
2863                        })
2864                    })
2865                    .collect();
2866                serde_json::json!({
2867                    "target_url": r.url,
2868                    "target_index": idx,
2869                    "checks_passed": r.passed,
2870                    "checks_failed": r.failed,
2871                    "total_checks": total_checks,
2872                    "pass_rate": rate,
2873                    "elapsed_seconds": r.elapsed.as_secs_f64(),
2874                    "report": r.report_json,
2875                    "owasp_coverage": owasp_json,
2876                })
2877            })
2878            .collect();
2879
2880        let combined_summary = serde_json::json!({
2881            "total_targets": num_targets,
2882            "total_checks_passed": total_passed,
2883            "total_checks_failed": total_failed,
2884            "overall_pass_rate": overall_rate,
2885            "total_elapsed_seconds": total_elapsed.as_secs_f64(),
2886            "targets": per_target_summaries,
2887        });
2888
2889        let summary_path = self.output.join("multi-target-conformance-summary.json");
2890        let summary_str = serde_json::to_string_pretty(&combined_summary)
2891            .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
2892        std::fs::write(&summary_path, &summary_str)
2893            .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
2894        TerminalReporter::print_success(&format!(
2895            "Combined summary saved to: {}",
2896            summary_path.display()
2897        ));
2898
2899        Ok(())
2900    }
2901
2902    /// Execute OWASP API Security Top 10 testing mode
2903    async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
2904        TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
2905
2906        // Parse custom headers from CLI
2907        let custom_headers = self.parse_headers()?;
2908
2909        // Build OWASP configuration from CLI options
2910        let mut config = OwaspApiConfig::new()
2911            .with_auth_header(&self.owasp_auth_header)
2912            .with_verbose(self.verbose)
2913            .with_insecure(self.skip_tls_verify)
2914            .with_concurrency(self.vus as usize)
2915            .with_iterations(self.owasp_iterations as usize)
2916            .with_base_path(self.base_path.clone())
2917            .with_custom_headers(custom_headers);
2918
2919        // Set valid auth token if provided
2920        if let Some(ref token) = self.owasp_auth_token {
2921            config = config.with_valid_auth_token(token);
2922        }
2923
2924        // Parse categories if provided
2925        if let Some(ref cats_str) = self.owasp_categories {
2926            let categories: Vec<OwaspCategory> = cats_str
2927                .split(',')
2928                .filter_map(|s| {
2929                    let trimmed = s.trim();
2930                    match trimmed.parse::<OwaspCategory>() {
2931                        Ok(cat) => Some(cat),
2932                        Err(e) => {
2933                            TerminalReporter::print_warning(&e);
2934                            None
2935                        }
2936                    }
2937                })
2938                .collect();
2939
2940            if !categories.is_empty() {
2941                config = config.with_categories(categories);
2942            }
2943        }
2944
2945        // Load admin paths from file if provided
2946        if let Some(ref admin_paths_file) = self.owasp_admin_paths {
2947            config.admin_paths_file = Some(admin_paths_file.clone());
2948            if let Err(e) = config.load_admin_paths() {
2949                TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
2950            }
2951        }
2952
2953        // Set ID fields if provided
2954        if let Some(ref id_fields_str) = self.owasp_id_fields {
2955            let id_fields: Vec<String> = id_fields_str
2956                .split(',')
2957                .map(|s| s.trim().to_string())
2958                .filter(|s| !s.is_empty())
2959                .collect();
2960            if !id_fields.is_empty() {
2961                config = config.with_id_fields(id_fields);
2962            }
2963        }
2964
2965        // Set report path and format
2966        if let Some(ref report_path) = self.owasp_report {
2967            config = config.with_report_path(report_path);
2968        }
2969        if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
2970            config = config.with_report_format(format);
2971        }
2972
2973        // Print configuration summary
2974        let categories = config.categories_to_test();
2975        TerminalReporter::print_success(&format!(
2976            "Testing {} OWASP categories: {}",
2977            categories.len(),
2978            categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
2979        ));
2980
2981        if config.valid_auth_token.is_some() {
2982            TerminalReporter::print_progress("Using provided auth token for baseline requests");
2983        }
2984
2985        // Create the OWASP generator
2986        TerminalReporter::print_progress("Generating OWASP security test script...");
2987        let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
2988
2989        // Generate the script
2990        let script = generator.generate()?;
2991        TerminalReporter::print_success("OWASP security test script generated");
2992
2993        // Write script to file
2994        let script_path = if let Some(output) = &self.script_output {
2995            output.clone()
2996        } else {
2997            self.output.join("k6-owasp-security-test.js")
2998        };
2999
3000        if let Some(parent) = script_path.parent() {
3001            std::fs::create_dir_all(parent)?;
3002        }
3003        std::fs::write(&script_path, &script)?;
3004        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
3005
3006        // If generate-only mode, exit here
3007        if self.generate_only {
3008            println!("\nOWASP security test script generated. Run it with:");
3009            println!("  k6 run {}", script_path.display());
3010            return Ok(());
3011        }
3012
3013        // Execute k6
3014        TerminalReporter::print_progress("Executing OWASP security tests...");
3015        let executor = K6Executor::new()?;
3016        std::fs::create_dir_all(&self.output)?;
3017
3018        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3019
3020        let duration_secs = Self::parse_duration(&self.duration)?;
3021        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
3022
3023        println!("\nOWASP security test results saved to: {}", self.output.display());
3024
3025        Ok(())
3026    }
3027}
3028
3029#[cfg(test)]
3030mod tests {
3031    use super::*;
3032    use tempfile::tempdir;
3033
3034    #[test]
3035    fn test_parse_duration() {
3036        assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
3037        assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
3038        assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
3039        assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
3040    }
3041
3042    #[test]
3043    fn test_parse_duration_invalid() {
3044        assert!(BenchCommand::parse_duration("invalid").is_err());
3045        assert!(BenchCommand::parse_duration("30x").is_err());
3046    }
3047
3048    #[test]
3049    fn test_parse_headers() {
3050        let cmd = BenchCommand {
3051            spec: vec![PathBuf::from("test.yaml")],
3052            spec_dir: None,
3053            merge_conflicts: "error".to_string(),
3054            spec_mode: "merge".to_string(),
3055            dependency_config: None,
3056            target: "http://localhost".to_string(),
3057            base_path: None,
3058            duration: "1m".to_string(),
3059            vus: 10,
3060            scenario: "ramp-up".to_string(),
3061            operations: None,
3062            exclude_operations: None,
3063            auth: None,
3064            headers: Some("X-API-Key:test123,X-Client-ID:client456".to_string()),
3065            output: PathBuf::from("output"),
3066            generate_only: false,
3067            script_output: None,
3068            threshold_percentile: "p(95)".to_string(),
3069            threshold_ms: 500,
3070            max_error_rate: 0.05,
3071            verbose: false,
3072            skip_tls_verify: false,
3073            chunked_request_bodies: false,
3074            target_rps: None,
3075            no_keep_alive: false,
3076            targets_file: None,
3077            max_concurrency: None,
3078            results_format: "both".to_string(),
3079            params_file: None,
3080            crud_flow: false,
3081            flow_config: None,
3082            extract_fields: None,
3083            parallel_create: None,
3084            data_file: None,
3085            data_distribution: "unique-per-vu".to_string(),
3086            data_mappings: None,
3087            per_uri_control: false,
3088            error_rate: None,
3089            error_types: None,
3090            security_test: false,
3091            security_payloads: None,
3092            security_categories: None,
3093            security_target_fields: None,
3094            wafbench_dir: None,
3095            wafbench_cycle_all: false,
3096            owasp_api_top10: false,
3097            owasp_categories: None,
3098            owasp_auth_header: "Authorization".to_string(),
3099            owasp_auth_token: None,
3100            owasp_admin_paths: None,
3101            owasp_id_fields: None,
3102            owasp_report: None,
3103            owasp_report_format: "json".to_string(),
3104            owasp_iterations: 1,
3105            conformance: false,
3106            conformance_api_key: None,
3107            conformance_basic_auth: None,
3108            conformance_report: PathBuf::from("conformance-report.json"),
3109            conformance_categories: None,
3110            conformance_report_format: "json".to_string(),
3111            conformance_headers: vec![],
3112            conformance_all_operations: false,
3113            conformance_custom: None,
3114            conformance_delay_ms: 0,
3115            use_k6: false,
3116            conformance_custom_filter: None,
3117            export_requests: false,
3118            validate_requests: false,
3119            conformance_self_test: false,
3120            source_ips: Vec::new(),
3121            geo_source_ips: Vec::new(),
3122            geo_source_headers: Vec::new(),
3123        };
3124
3125        let headers = cmd.parse_headers().unwrap();
3126        assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
3127        assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
3128    }
3129
3130    #[test]
3131    fn test_get_spec_display_name() {
3132        let cmd = BenchCommand {
3133            spec: vec![PathBuf::from("test.yaml")],
3134            spec_dir: None,
3135            merge_conflicts: "error".to_string(),
3136            spec_mode: "merge".to_string(),
3137            dependency_config: None,
3138            target: "http://localhost".to_string(),
3139            base_path: None,
3140            duration: "1m".to_string(),
3141            vus: 10,
3142            scenario: "ramp-up".to_string(),
3143            operations: None,
3144            exclude_operations: None,
3145            auth: None,
3146            headers: None,
3147            output: PathBuf::from("output"),
3148            generate_only: false,
3149            script_output: None,
3150            threshold_percentile: "p(95)".to_string(),
3151            threshold_ms: 500,
3152            max_error_rate: 0.05,
3153            verbose: false,
3154            skip_tls_verify: false,
3155            chunked_request_bodies: false,
3156            target_rps: None,
3157            no_keep_alive: false,
3158            targets_file: None,
3159            max_concurrency: None,
3160            results_format: "both".to_string(),
3161            params_file: None,
3162            crud_flow: false,
3163            flow_config: None,
3164            extract_fields: None,
3165            parallel_create: None,
3166            data_file: None,
3167            data_distribution: "unique-per-vu".to_string(),
3168            data_mappings: None,
3169            per_uri_control: false,
3170            error_rate: None,
3171            error_types: None,
3172            security_test: false,
3173            security_payloads: None,
3174            security_categories: None,
3175            security_target_fields: None,
3176            wafbench_dir: None,
3177            wafbench_cycle_all: false,
3178            owasp_api_top10: false,
3179            owasp_categories: None,
3180            owasp_auth_header: "Authorization".to_string(),
3181            owasp_auth_token: None,
3182            owasp_admin_paths: None,
3183            owasp_id_fields: None,
3184            owasp_report: None,
3185            owasp_report_format: "json".to_string(),
3186            owasp_iterations: 1,
3187            conformance: false,
3188            conformance_api_key: None,
3189            conformance_basic_auth: None,
3190            conformance_report: PathBuf::from("conformance-report.json"),
3191            conformance_categories: None,
3192            conformance_report_format: "json".to_string(),
3193            conformance_headers: vec![],
3194            conformance_all_operations: false,
3195            conformance_custom: None,
3196            conformance_delay_ms: 0,
3197            use_k6: false,
3198            conformance_custom_filter: None,
3199            export_requests: false,
3200            validate_requests: false,
3201            conformance_self_test: false,
3202            source_ips: Vec::new(),
3203            geo_source_ips: Vec::new(),
3204            geo_source_headers: Vec::new(),
3205        };
3206
3207        assert_eq!(cmd.get_spec_display_name(), "test.yaml");
3208
3209        // Test multiple specs
3210        let cmd_multi = BenchCommand {
3211            spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
3212            spec_dir: None,
3213            merge_conflicts: "error".to_string(),
3214            spec_mode: "merge".to_string(),
3215            dependency_config: None,
3216            target: "http://localhost".to_string(),
3217            base_path: None,
3218            duration: "1m".to_string(),
3219            vus: 10,
3220            scenario: "ramp-up".to_string(),
3221            operations: None,
3222            exclude_operations: None,
3223            auth: None,
3224            headers: None,
3225            output: PathBuf::from("output"),
3226            generate_only: false,
3227            script_output: None,
3228            threshold_percentile: "p(95)".to_string(),
3229            threshold_ms: 500,
3230            max_error_rate: 0.05,
3231            verbose: false,
3232            skip_tls_verify: false,
3233            chunked_request_bodies: false,
3234            target_rps: None,
3235            no_keep_alive: false,
3236            targets_file: None,
3237            max_concurrency: None,
3238            results_format: "both".to_string(),
3239            params_file: None,
3240            crud_flow: false,
3241            flow_config: None,
3242            extract_fields: None,
3243            parallel_create: None,
3244            data_file: None,
3245            data_distribution: "unique-per-vu".to_string(),
3246            data_mappings: None,
3247            per_uri_control: false,
3248            error_rate: None,
3249            error_types: None,
3250            security_test: false,
3251            security_payloads: None,
3252            security_categories: None,
3253            security_target_fields: None,
3254            wafbench_dir: None,
3255            wafbench_cycle_all: false,
3256            owasp_api_top10: false,
3257            owasp_categories: None,
3258            owasp_auth_header: "Authorization".to_string(),
3259            owasp_auth_token: None,
3260            owasp_admin_paths: None,
3261            owasp_id_fields: None,
3262            owasp_report: None,
3263            owasp_report_format: "json".to_string(),
3264            owasp_iterations: 1,
3265            conformance: false,
3266            conformance_api_key: None,
3267            conformance_basic_auth: None,
3268            conformance_report: PathBuf::from("conformance-report.json"),
3269            conformance_categories: None,
3270            conformance_report_format: "json".to_string(),
3271            conformance_headers: vec![],
3272            conformance_all_operations: false,
3273            conformance_custom: None,
3274            conformance_delay_ms: 0,
3275            use_k6: false,
3276            conformance_custom_filter: None,
3277            export_requests: false,
3278            validate_requests: false,
3279            conformance_self_test: false,
3280            source_ips: Vec::new(),
3281            geo_source_ips: Vec::new(),
3282            geo_source_headers: Vec::new(),
3283        };
3284
3285        assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
3286    }
3287
3288    #[test]
3289    fn test_parse_extracted_values_from_output_dir() {
3290        let dir = tempdir().unwrap();
3291        let path = dir.path().join("extracted_values.json");
3292        std::fs::write(
3293            &path,
3294            r#"{
3295  "pool_id": "abc123",
3296  "count": 0,
3297  "enabled": false,
3298  "metadata": { "owner": "team-a" }
3299}"#,
3300        )
3301        .unwrap();
3302
3303        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3304        assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
3305        assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
3306        assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
3307        assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
3308    }
3309
3310    #[test]
3311    fn test_parse_extracted_values_missing_file() {
3312        let dir = tempdir().unwrap();
3313        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3314        assert!(extracted.values.is_empty());
3315    }
3316}