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