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 list of `Key:Value` header strings into a `HashMap`.
38///
39/// Each element is one header in `Key:Value` form, supplied via a repeated
40/// `--headers` flag (`--headers "A:1" --headers "B:2"`). One header per flag
41/// means header VALUES may freely contain commas (#761) -- e.g. a Cookie value
42/// like `expires=Thu, 01 Jan 2099` is preserved intact, because we no longer
43/// split on commas. Empty elements are skipped so a stray flag is harmless.
44pub fn parse_header_string(inputs: &[String]) -> Result<HashMap<String, String>> {
45    let mut headers = HashMap::new();
46
47    for pair in inputs {
48        let pair = pair.trim();
49        if pair.is_empty() {
50            continue;
51        }
52        let parts: Vec<&str> = pair.splitn(2, ':').collect();
53        if parts.len() != 2 {
54            return Err(BenchError::Other(format!(
55                "Invalid header format: '{}'. Expected 'Key:Value'",
56                pair
57            )));
58        }
59        headers.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
60    }
61
62    Ok(headers)
63}
64
65/// Bench command configuration
66pub struct BenchCommand {
67    /// OpenAPI spec file(s) - can specify multiple
68    pub spec: Vec<PathBuf>,
69    /// Directory containing OpenAPI spec files (discovers .json, .yaml, .yml files)
70    pub spec_dir: Option<PathBuf>,
71    /// Conflict resolution strategy when merging multiple specs: "error" (default), "first", "last"
72    pub merge_conflicts: String,
73    /// Spec mode: "merge" (default) combines all specs, "sequential" runs them in order
74    pub spec_mode: String,
75    /// Dependency configuration file for cross-spec value passing (used with sequential mode)
76    pub dependency_config: Option<PathBuf>,
77    pub target: String,
78    /// API base path prefix (e.g., "/api" or "/v2/api")
79    /// If None, extracts from OpenAPI spec's servers URL
80    pub base_path: Option<String>,
81    pub duration: String,
82    pub vus: u32,
83    /// Target requests-per-second. When `Some(n)`, the generated k6 script
84    /// switches to `constant-arrival-rate` executor at `n` RPS with `vus`
85    /// pre-allocated. When `None`, uses the legacy `ramping-vus` executor
86    /// where RPS is implicit (VUs × 1 req/sec from the script's `sleep(1)`).
87    /// Issue #79 — Srikanth's round-3 reply.
88    pub target_rps: Option<u32>,
89    /// When true, every k6 request opens a new TCP/TLS connection
90    /// (`noConnectionReuse: true` and `--no-vu-connection-reuse`). Lets users
91    /// drive a high connections-per-second rate to exercise connection-limit
92    /// chaos and observe TCP-level fault injection. Issue #79.
93    pub no_keep_alive: bool,
94    pub scenario: String,
95    pub operations: Option<String>,
96    /// Exclude operations from testing (comma-separated)
97    ///
98    /// Supports "METHOD /path" or just "METHOD" to exclude all operations of that type.
99    pub exclude_operations: Option<String>,
100    pub auth: Option<String>,
101    /// Additional headers, one `Key:Value` per entry (repeated `--headers` flag).
102    /// One header per entry so values may contain commas (#761).
103    pub headers: Vec<String>,
104    pub output: PathBuf,
105    pub generate_only: bool,
106    pub script_output: Option<PathBuf>,
107    pub threshold_percentile: String,
108    pub threshold_ms: u64,
109    pub max_error_rate: f64,
110    pub verbose: bool,
111    pub skip_tls_verify: bool,
112    /// When true, set `Transfer-Encoding: chunked` on every k6 request body so
113    /// the server experiences chunked-encoded traffic. See
114    /// `K6ScriptTemplateData::chunked_request_bodies` for caveats — k6's Go
115    /// transport may still send Content-Length in some cases.
116    pub chunked_request_bodies: bool,
117    /// Optional file containing multiple targets
118    pub targets_file: Option<PathBuf>,
119    /// Maximum number of parallel executions (for multi-target mode)
120    pub max_concurrency: Option<u32>,
121    /// Results format: "per-target", "aggregated", or "both"
122    pub results_format: String,
123    /// Optional file containing parameter value overrides (JSON or YAML)
124    ///
125    /// Allows users to provide custom values for path parameters, query parameters,
126    /// headers, and request bodies instead of auto-generated placeholder values.
127    pub params_file: Option<PathBuf>,
128
129    // === CRUD Flow Options ===
130    /// Enable CRUD flow mode
131    pub crud_flow: bool,
132    /// Custom CRUD flow configuration file
133    pub flow_config: Option<PathBuf>,
134    /// Fields to extract from responses
135    pub extract_fields: Option<String>,
136
137    // === Parallel Execution Options ===
138    /// Number of resources to create in parallel
139    pub parallel_create: Option<u32>,
140
141    // === Data-Driven Testing Options ===
142    /// Test data file (CSV or JSON)
143    pub data_file: Option<PathBuf>,
144    /// Data distribution strategy
145    pub data_distribution: String,
146    /// Data column to field mappings
147    pub data_mappings: Option<String>,
148    /// Enable per-URI control mode (each row specifies method, uri, body, etc.)
149    pub per_uri_control: bool,
150
151    // === Invalid Data Testing Options ===
152    /// Percentage of requests with invalid data
153    pub error_rate: Option<f64>,
154    /// Types of invalid data to generate
155    pub error_types: Option<String>,
156
157    // === Security Testing Options ===
158    /// Enable security testing
159    pub security_test: bool,
160    /// Custom security payloads file
161    pub security_payloads: Option<PathBuf>,
162    /// Security test categories
163    pub security_categories: Option<String>,
164    /// Fields to target for security injection
165    pub security_target_fields: Option<String>,
166
167    // === WAFBench Integration ===
168    /// WAFBench test directory or glob pattern for loading CRS attack patterns
169    pub wafbench_dir: Option<String>,
170    /// Cycle through ALL WAFBench payloads instead of random sampling
171    pub wafbench_cycle_all: bool,
172
173    // === OpenAPI 3.0.0 Conformance Testing ===
174    /// Enable conformance testing mode
175    pub conformance: bool,
176    /// API key for conformance security tests
177    pub conformance_api_key: Option<String>,
178    /// Basic auth credentials for conformance security tests (user:pass)
179    pub conformance_basic_auth: Option<String>,
180    /// Conformance report output file
181    pub conformance_report: PathBuf,
182    /// Conformance categories to test (comma-separated, e.g. "parameters,security")
183    pub conformance_categories: Option<String>,
184    /// Conformance report format: "json" or "sarif"
185    pub conformance_report_format: String,
186    /// Custom headers to inject into every conformance request (for authentication).
187    /// Each entry is "Header-Name: value" format.
188    pub conformance_headers: Vec<String>,
189    /// When true, test ALL operations for method/response/body categories
190    /// instead of just one representative per feature check.
191    pub conformance_all_operations: bool,
192    /// Optional YAML file with custom conformance checks
193    pub conformance_custom: Option<PathBuf>,
194    /// Delay in milliseconds between consecutive conformance requests.
195    /// Useful when testing against rate-limited APIs.
196    pub conformance_delay_ms: u64,
197    /// Use k6 for conformance test execution instead of the native Rust executor
198    pub use_k6: bool,
199    /// Regex filter for custom conformance checks — only checks whose name or
200    /// path matches the pattern are included. Example: "wafcrs|ssl" to test
201    /// only checks with "wafcrs" or "ssl" in the name/path.
202    pub conformance_custom_filter: Option<String>,
203    /// When true, export all request/response pairs to
204    /// `conformance-requests.json` in the output directory.
205    pub export_requests: bool,
206    /// When true, validate each request against the OpenAPI spec and report
207    /// violations to `conformance-request-violations.json`.
208    pub validate_requests: bool,
209    /// Issue #79 round 13 (4) — when true, replace the standard
210    /// conformance run with a positive + per-category negative
211    /// self-test driver. Verifies that the server actually rejects
212    /// the negatives with 4xx (i.e. its validator is wired correctly).
213    /// Useful to confirm the round-13 (3) validator-bypass fix took
214    /// effect against the user's spec.
215    pub conformance_self_test: bool,
216    /// Round 23 (c-iii) — when true, capture every self-test probe's
217    /// full request/response to `conformance-self-test-requests.jsonl`.
218    /// No effect outside `--conformance-self-test`.
219    pub conformance_self_test_capture: bool,
220    /// Round 25 (21.3 / a2 / a3) — when true, validate every probe's
221    /// response body against the spec's response schema for the actual
222    /// status returned. Requires `--conformance-self-test-capture`
223    /// because validation reads the captured body. No effect outside
224    /// `--conformance-self-test`.
225    pub validate_response_schemas: bool,
226    /// Round 47 (#79) — repeat the full self-test probe matrix this
227    /// many times in sequence. Defaults to 1. Combines with
228    /// `--conformance-delay` to keep a steady probe rate for network-
229    /// failure simulation.
230    pub conformance_self_test_iterations: u32,
231    /// Round 47 (#79) — alternative to iterations: keep firing the
232    /// probe matrix until this duration elapses. Overrides
233    /// `conformance_self_test_iterations` when present (iterations
234    /// becomes the floor — at least one matrix is always fired).
235    pub conformance_self_test_duration: Option<String>,
236
237    /// Round 18.5 — local source IPs to bind self-test requests to.
238    /// Each entry must be a valid `IpAddr` and already assigned to
239    /// an interface on the host. Operations round-robin through the
240    /// pool. Empty → one default client.
241    pub source_ips: Vec<String>,
242    /// Round 18.5 — fake source IPs to advertise via forwarded-IP
243    /// headers (rotated per operation). Used for GEODB testing
244    /// where the destination reads the IP from a header.
245    pub geo_source_ips: Vec<String>,
246    /// Round 18.5 — which forwarded-IP header(s) to populate when
247    /// `geo_source_ips` is non-empty. Empty → default 3-header set
248    /// (X-Forwarded-For, True-Client-IP, CF-Connecting-IP).
249    pub geo_source_headers: Vec<String>,
250
251    /// Round 21.1 — cap the HTML conformance report's missed-negative
252    /// drill-down at N rows. `Some(0)` means no cap; `None` keeps the
253    /// default of 200. The JSON report always carries the full set
254    /// regardless of this knob — it only controls what the HTML drill
255    /// view shows so a 50 000-violation run doesn't produce a 5 MB
256    /// browser-choking HTML file by default.
257    pub report_missed_cap: Option<u32>,
258
259    // === OWASP API Security Top 10 Testing ===
260    /// Enable OWASP API Security Top 10 testing mode
261    pub owasp_api_top10: bool,
262    /// OWASP API categories to test (comma-separated)
263    pub owasp_categories: Option<String>,
264    /// Authorization header name for OWASP auth tests
265    pub owasp_auth_header: String,
266    /// Valid authorization token for OWASP baseline requests
267    pub owasp_auth_token: Option<String>,
268    /// File containing admin/privileged paths to test
269    pub owasp_admin_paths: Option<PathBuf>,
270    /// Fields containing resource IDs for BOLA testing
271    pub owasp_id_fields: Option<String>,
272    /// OWASP report output file
273    pub owasp_report: Option<PathBuf>,
274    /// OWASP report format (json, sarif)
275    pub owasp_report_format: String,
276    /// Number of iterations per VU for OWASP tests (default: 1)
277    pub owasp_iterations: u32,
278}
279
280/// Round 18.5 / 19 — parse a list of CLI IP strings. Each entry may be:
281/// - a single IPv4/IPv6 (`10.0.0.5` / `2001:db8::1`)
282/// - a comma-separated list (`10.0.0.5,10.0.0.6,2001:db8::1`)
283/// - a CIDR range (`10.0.0.0/29` expands to 8 hosts;
284///   `2001:db8::/126` expands to 4 IPv6 hosts)
285///
286/// CIDR ranges are capped at `MAX_CIDR_EXPANSION` (256) host
287/// addresses to avoid OOM'ing on `/8` typos. The cap is generous
288/// for GEODB testing (you want 20–100 IPs, not 10M) and the warning
289/// names the cap so it's debuggable.
290///
291/// Malformed entries log a warning and are dropped; the bench
292/// continues with whatever resolved cleanly.
293fn parse_ip_list(raw: &[String], flag_name: &str) -> Vec<std::net::IpAddr> {
294    use std::net::IpAddr;
295    const MAX_CIDR_EXPANSION: usize = 256;
296    let mut out = Vec::new();
297    for entry in raw {
298        for piece in entry.split(',') {
299            let s = piece.trim();
300            if s.is_empty() {
301                continue;
302            }
303            // CIDR form: `ip/prefix`
304            if let Some((addr_part, prefix_part)) = s.split_once('/') {
305                let prefix: u32 = match prefix_part.parse() {
306                    Ok(p) => p,
307                    Err(e) => {
308                        tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR prefix: {e}");
309                        continue;
310                    }
311                };
312                let net_addr: IpAddr = match addr_part.parse() {
313                    Ok(a) => a,
314                    Err(e) => {
315                        tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR address: {e}");
316                        continue;
317                    }
318                };
319                expand_cidr(net_addr, prefix, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
320                continue;
321            }
322            // Round 22.4 — range form: `start-end` (Srikanth (h)).
323            // Lets users specify non-power-of-2 ranges without
324            // finding a clean prefix. IPv4 only for now (the most
325            // common case); IPv6 ranges with `:` collide with the
326            // address literal so they'd need a different separator.
327            if let Some((start_str, end_str)) = s.split_once('-') {
328                let start_s = start_str.trim();
329                let end_s = end_str.trim();
330                // Reject ambiguous IPv6 ranges (contain `:`) so we
331                // don't accidentally parse `2001:db8::1-2001:db8::5`
332                // as a half address.
333                if start_s.contains(':') || end_s.contains(':') {
334                    tracing::warn!(target: "mockforge::bench", "--{flag_name} '{s}': IPv6 range syntax not supported (use CIDR like 2001:db8::/126 instead)");
335                    continue;
336                }
337                let start: IpAddr = match start_s.parse() {
338                    Ok(a) => a,
339                    Err(e) => {
340                        tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range start: {e}");
341                        continue;
342                    }
343                };
344                let end: IpAddr = match end_s.parse() {
345                    Ok(a) => a,
346                    Err(e) => {
347                        tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range end: {e}");
348                        continue;
349                    }
350                };
351                expand_range(start, end, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
352                continue;
353            }
354            // Plain IP form
355            match s.parse::<IpAddr>() {
356                Ok(ip) => out.push(ip),
357                Err(e) => {
358                    tracing::warn!(target: "mockforge::bench", "ignoring malformed --{flag_name} value '{s}': {e}");
359                }
360            }
361        }
362    }
363    out
364}
365
366/// Round 22.4 — expand an inclusive `start-end` IPv4 range to host
367/// addresses, capped at `cap`. Returns silently on a backwards or
368/// mixed-family range with a warning.
369fn expand_range(
370    start: std::net::IpAddr,
371    end: std::net::IpAddr,
372    cap: usize,
373    flag_name: &str,
374    raw: &str,
375    out: &mut Vec<std::net::IpAddr>,
376) {
377    use std::net::{IpAddr, Ipv4Addr};
378    let (start_v4, end_v4) = match (start, end) {
379        (IpAddr::V4(a), IpAddr::V4(b)) => (a, b),
380        _ => {
381            tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range start/end must both be IPv4");
382            return;
383        }
384    };
385    let start_u32 = u32::from(start_v4);
386    let end_u32 = u32::from(end_v4);
387    if end_u32 < start_u32 {
388        tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range end {end_v4} is before start {start_v4}");
389        return;
390    }
391    let total = (end_u32 - start_u32).saturating_add(1) as usize;
392    let take = total.min(cap);
393    if total > cap {
394        tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range has {total} addresses, capping at {cap}");
395    }
396    for i in 0..take as u32 {
397        out.push(IpAddr::V4(Ipv4Addr::from(start_u32 + i)));
398    }
399}
400
401/// Expand a CIDR (IPv4 or IPv6) into individual host IPs, appending
402/// to `out`. Capped at `cap` to prevent runaway expansion on a `/8`
403/// typo. When the cap kicks in we log a warning and skip the rest.
404fn expand_cidr(
405    net: std::net::IpAddr,
406    prefix: u32,
407    cap: usize,
408    flag_name: &str,
409    raw: &str,
410    out: &mut Vec<std::net::IpAddr>,
411) {
412    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
413    match net {
414        IpAddr::V4(ipv4) => {
415            if prefix > 32 {
416                tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv4 prefix must be <= 32");
417                return;
418            }
419            let total: u64 = 1u64 << (32 - prefix);
420            let take = total.min(cap as u64) as u32;
421            if total > cap as u64 {
422                tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': CIDR has {total} addresses, capping at {cap}");
423            }
424            let mask: u32 = if prefix == 0 {
425                0
426            } else {
427                !0u32 << (32 - prefix)
428            };
429            let net_u32 = u32::from(ipv4) & mask;
430            for i in 0..take {
431                out.push(IpAddr::V4(Ipv4Addr::from(net_u32.wrapping_add(i))));
432            }
433        }
434        IpAddr::V6(ipv6) => {
435            if prefix > 128 {
436                tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv6 prefix must be <= 128");
437                return;
438            }
439            // Total addresses = 2^(128-prefix). Cap at u128::MAX
440            // conceptually but since `cap` is small (256) we just
441            // iterate up to cap.
442            let mask: u128 = if prefix == 0 {
443                0
444            } else {
445                !0u128 << (128 - prefix)
446            };
447            let net_u128 = u128::from(ipv6) & mask;
448            let remaining_bits = 128 - prefix;
449            // Compute total carefully — for prefix=0 this is 2^128
450            // which overflows; we just clamp via take.
451            let total_capped = if remaining_bits >= 64 {
452                cap as u128
453            } else {
454                (1u128 << remaining_bits).min(cap as u128)
455            };
456            if remaining_bits < 128 && (1u128 << remaining_bits) > cap as u128 {
457                tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': IPv6 CIDR exceeds {cap} addresses, capping");
458            }
459            for i in 0..total_capped {
460                out.push(IpAddr::V6(Ipv6Addr::from(net_u128.wrapping_add(i))));
461            }
462        }
463    }
464}
465
466impl BenchCommand {
467    /// Load and merge specs from --spec files and --spec-dir
468    pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
469        let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
470
471        // Load specs from --spec flags
472        if !self.spec.is_empty() {
473            let specs = load_specs_from_files(self.spec.clone())
474                .await
475                .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
476            all_specs.extend(specs);
477        }
478
479        // Load specs from --spec-dir if provided
480        if let Some(spec_dir) = &self.spec_dir {
481            let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
482                BenchError::Other(format!("Failed to load specs from directory: {}", e))
483            })?;
484            all_specs.extend(dir_specs);
485        }
486
487        if all_specs.is_empty() {
488            return Err(BenchError::Other(
489                "No spec files provided. Use --spec or --spec-dir.".to_string(),
490            ));
491        }
492
493        // If only one spec, return it directly (extract just the OpenApiSpec)
494        if all_specs.len() == 1 {
495            // Safe to unwrap because we just checked len() == 1
496            return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
497        }
498
499        // Merge multiple specs
500        let conflict_strategy = match self.merge_conflicts.as_str() {
501            "first" => ConflictStrategy::First,
502            "last" => ConflictStrategy::Last,
503            _ => ConflictStrategy::Error,
504        };
505
506        merge_specs(all_specs, conflict_strategy)
507            .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
508    }
509
510    /// Get a display name for the spec(s)
511    fn get_spec_display_name(&self) -> String {
512        if self.spec.len() == 1 {
513            self.spec[0].to_string_lossy().to_string()
514        } else if !self.spec.is_empty() {
515            format!("{} spec files", self.spec.len())
516        } else if let Some(dir) = &self.spec_dir {
517            format!("specs from {}", dir.display())
518        } else {
519            "no specs".to_string()
520        }
521    }
522
523    /// Round 29 — capacity advisory. Counts the targets-file entries
524    /// (or 1 for single-target), multiplies by VUs and configured RPS,
525    /// and prints a warning when the product exceeds rule-of-thumb
526    /// limits a single client can handle. The detailed sizing table
527    /// lives in `book/src/reference/bench-capacity-sizing.md`; this
528    /// just nudges the user toward it before they hang their VM.
529    fn advise_capacity(&self) {
530        let target_count = self
531            .targets_file
532            .as_ref()
533            .and_then(|p| std::fs::read_to_string(p).ok())
534            .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
535            .and_then(|v| v.as_array().map(|a| a.len()))
536            .unwrap_or(1);
537        let vus = self.vus.max(1);
538        let rps_total = self.target_rps.unwrap_or(0) as usize * target_count.max(1);
539        // 50 targets × 5 VUs = 250 is roughly where Srikanth's 15GB
540        // client started hanging on a 10 MB spec. Surface a warning
541        // well before that.
542        let load_product = target_count * vus as usize;
543        if load_product >= 150 {
544            let est_ram_gb =
545                (vus as usize * 50) / 1024 + (target_count * 10 * 2) / 1024 + target_count / 2;
546            let est_cores = ((vus as usize) / 50).max(2);
547            TerminalReporter::print_warning(&format!(
548                "Capacity advisory: targets={target_count}, VUs={vus}, RPS-total≈{rps_total}. \
549                 Single-client estimate: ~{est_cores} CPU cores, ~{est_ram_gb} GB RAM. \
550                 If your machine is below that, expect OOM hangs partway through the run. \
551                 See https://docs.mockforge.dev/reference/bench-capacity-sizing.html \
552                 for the sizing table and sharding guide."
553            ));
554        }
555    }
556
557    /// Execute the bench command
558    pub async fn execute(&self) -> Result<()> {
559        // Round 23 — Srikanth flagged that k6 _does_ support per-VU source
560        // IPs via `--local-ips` (the round-22 warning that said otherwise
561        // was wrong). The k6 path now forwards `--source-ip` straight to
562        // `k6 run --local-ips`, so the only case worth flagging is the
563        // self-test+k6 combo: self-test returns before k6 ever launches,
564        // so `--use-k6` on that command is a no-op.
565        if self.conformance_self_test && self.use_k6 {
566            TerminalReporter::print_warning(
567                "--use-k6 has no effect with --conformance-self-test: the self-test driver runs and returns before k6 is invoked. Drop one or the other depending on whether you want the spec-driven self-test or a k6 bench run.",
568            );
569        }
570
571        // Round 29 — Srikanth's "5 VUs against 50 targets hung the VM"
572        // report. Print an up-front capacity advisory so the user knows
573        // BEFORE the run starts that their config exceeds what one
574        // client can comfortably handle. Pure heuristic; rules of
575        // thumb live in book/src/reference/bench-capacity-sizing.md.
576        self.advise_capacity();
577
578        // Check if we're in multi-target mode
579        if let Some(targets_file) = &self.targets_file {
580            // Round 48 (#79) — Srikanth on 0.3.192: --conformance-self-test
581            // with --targets-file silently ignored both the self-test
582            // driver AND the round-47 iterations/duration knobs because
583            // the dispatch above returned into `execute_multi_target_conformance`
584            // which runs the regular k6 conformance flow. Now route to
585            // a dedicated multi-target self-test path that runs the
586            // self-test driver against EACH target with the same
587            // iteration/duration loop the single-target path got in r47.
588            if self.conformance && self.conformance_self_test {
589                return self.execute_multi_target_self_test(targets_file).await;
590            }
591            if self.conformance {
592                return self.execute_multi_target_conformance(targets_file).await;
593            }
594            return self.execute_multi_target(targets_file).await;
595        }
596
597        // Check if we're in sequential spec mode (for dependency handling)
598        if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
599            return self.execute_sequential_specs().await;
600        }
601
602        // Single target mode (existing behavior)
603        // Print header
604        TerminalReporter::print_header(
605            &self.get_spec_display_name(),
606            &self.target,
607            0, // Will be updated later
608            &self.scenario,
609            Self::parse_duration(&self.duration)?,
610        );
611
612        // Validate k6 installation
613        if !K6Executor::is_k6_installed() {
614            TerminalReporter::print_error("k6 is not installed");
615            TerminalReporter::print_warning(
616                "Install k6 from: https://k6.io/docs/get-started/installation/",
617            );
618            return Err(BenchError::K6NotFound);
619        }
620
621        // Check for conformance testing mode (before spec loading — conformance doesn't need a user spec)
622        if self.conformance {
623            return self.execute_conformance_test().await;
624        }
625
626        // Load and parse spec(s)
627        TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
628        let merged_spec = self.load_and_merge_specs().await?;
629        let parser = SpecParser::from_spec(merged_spec);
630        if self.spec.len() > 1 || self.spec_dir.is_some() {
631            TerminalReporter::print_success(&format!(
632                "Loaded and merged {} specification(s)",
633                self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
634            ));
635        } else {
636            TerminalReporter::print_success("Specification loaded");
637        }
638
639        // Check for mock server integration
640        let mock_config = self.build_mock_config().await;
641        if mock_config.is_mock_server {
642            TerminalReporter::print_progress("Mock server integration enabled");
643        }
644
645        // Check for CRUD flow mode
646        if self.crud_flow {
647            return self.execute_crud_flow(&parser).await;
648        }
649
650        // Check for OWASP API Top 10 testing mode
651        if self.owasp_api_top10 {
652            return self.execute_owasp_test(&parser).await;
653        }
654
655        // Get operations
656        TerminalReporter::print_progress("Extracting API operations...");
657        let mut operations = if let Some(filter) = &self.operations {
658            parser.filter_operations(filter)?
659        } else {
660            parser.get_operations()
661        };
662
663        // Apply exclusions if provided
664        if let Some(exclude) = &self.exclude_operations {
665            let before_count = operations.len();
666            operations = parser.exclude_operations(operations, exclude)?;
667            let excluded_count = before_count - operations.len();
668            if excluded_count > 0 {
669                TerminalReporter::print_progress(&format!(
670                    "Excluded {} operations matching '{}'",
671                    excluded_count, exclude
672                ));
673            }
674        }
675
676        if operations.is_empty() {
677            return Err(BenchError::Other("No operations found in spec".to_string()));
678        }
679
680        TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
681
682        // Load parameter overrides if provided
683        let param_overrides = if let Some(params_file) = &self.params_file {
684            TerminalReporter::print_progress("Loading parameter overrides...");
685            let overrides = ParameterOverrides::from_file(params_file)?;
686            TerminalReporter::print_success(&format!(
687                "Loaded parameter overrides ({} operation-specific, {} defaults)",
688                overrides.operations.len(),
689                if overrides.defaults.is_empty() { 0 } else { 1 }
690            ));
691            Some(overrides)
692        } else {
693            None
694        };
695
696        // Generate request templates
697        TerminalReporter::print_progress("Generating request templates...");
698        let templates: Vec<_> = operations
699            .iter()
700            .map(|op| {
701                let op_overrides = param_overrides.as_ref().map(|po| {
702                    po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
703                });
704                RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
705            })
706            .collect::<Result<Vec<_>>>()?;
707        TerminalReporter::print_success("Request templates generated");
708
709        // Parse headers
710        let custom_headers = self.parse_headers()?;
711
712        // Resolve base path (CLI option takes priority over spec's servers URL)
713        let base_path = self.resolve_base_path(&parser);
714        if let Some(ref bp) = base_path {
715            TerminalReporter::print_progress(&format!("Using base path: {}", bp));
716        }
717
718        // Generate k6 script
719        TerminalReporter::print_progress("Generating k6 load test script...");
720        let scenario =
721            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
722
723        let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
724
725        // Issue #79 round 6 follow-up — Srikanth reported k6 emitting
726        // "Insufficient VUs, reached 5 active VUs and cannot initialize more"
727        // when running `--rps 100 --vus 5`. With the `constant-arrival-rate`
728        // executor, k6 needs roughly `rps × avg_request_seconds` VUs to keep
729        // up; if `--vus` is too low it can't sustain the rate. Warn pre-flight
730        // so users know to bump `--vus` rather than chase the warning.
731        //
732        // Round 8 (#79): the static 100ms heuristic was wrong for fast targets
733        // (~2ms latency). Probe the actual target first to measure baseline
734        // latency, then derive a more accurate sizing recommendation. Fall
735        // back to the 100ms heuristic only when the probe can't reach the
736        // target (auth-gated endpoints, strict WAFs, etc).
737        //
738        // Round 9 (#79): factor in operation count. k6's constant-arrival-rate
739        // counts ITERATIONS, not requests — and every iteration runs all N
740        // operations sequentially, so required VUs scale with N. Srikanth's
741        // 12-op spec at --rps 100 with 15ms latency needs ~19 VUs, not 3.
742        let num_ops = operations.len() as u32;
743        if let Some(rps) = self.target_rps {
744            let probe =
745                crate::preflight::probe_target_latency(&self.target, 3, self.skip_tls_verify).await;
746
747            let (required_vus, basis) = match probe {
748                Some(p) => (
749                    p.required_vus(rps, num_ops),
750                    format!("avg {:.1}ms (measured)", p.avg_latency.as_secs_f64() * 1000.0),
751                ),
752                None => {
753                    // Static fallback: ~100ms heuristic × num_ops per iteration.
754                    let fallback = (rps as u64)
755                        .saturating_mul(num_ops.max(1) as u64)
756                        .div_ceil(10)
757                        .min(u32::MAX as u64) as u32;
758                    (fallback, "~100ms (default — probe failed)".to_string())
759                }
760            };
761
762            if self.vus < required_vus {
763                // Round 10 (#79): Srikanth's 11422-op spec at --rps 100 produced
764                // a recommendation of ~10,740 VUs, which is absurd in practice.
765                // When the recommendation goes super-linear, the real fix is to
766                // reduce the workload (use --operations filter), not bump VUs.
767                // Cap the suggestion at 1000 and steer the user toward filtering.
768                const VU_RECOMMENDATION_CAP: u32 = 1000;
769                let recommendation = required_vus.max(self.vus + 1);
770                if recommendation > VU_RECOMMENDATION_CAP {
771                    TerminalReporter::print_warning(&format!(
772                        "Workload is very large: --rps {} × {} ops/iteration × {} \
773                         baseline ⇒ ~{} VUs needed end-to-end, far beyond what's \
774                         practical to drive. Two ways to fix:\n  1. Reduce \
775                         operations per iteration with `--operations 'pattern,…'` \
776                         (or `--exclude-operations`) to focus the bench on a \
777                         representative subset.\n  2. Drop `--rps` and use \
778                         `--vus {}` alone — closed-model load runs as fast as \
779                         the VU pool allows, bounded by latency, with no per-\
780                         iteration deadline. Expect 1-iteration coverage of ~{} \
781                         operations in {}s.",
782                        rps,
783                        num_ops,
784                        basis,
785                        recommendation,
786                        self.vus.max(5),
787                        num_ops,
788                        Self::parse_duration(&self.duration).unwrap_or(0),
789                    ));
790                } else {
791                    TerminalReporter::print_warning(&format!(
792                        "--vus {} may be insufficient for --rps {} × {} ops/iteration \
793                         (baseline latency {}). k6's constant-arrival-rate counts ITERATIONS \
794                         and each runs every operation in the spec — required ≈ rps × ops × \
795                         latency_secs VUs. Bump --vus to ~{} if you see \"Insufficient VUs\" \
796                         warnings.",
797                        self.vus, rps, num_ops, basis, recommendation,
798                    ));
799                }
800            } else if probe.is_some() {
801                TerminalReporter::print_progress(&format!(
802                    "Pre-flight probe: target latency {}, {} ops/iteration — --vus {} \
803                     is sufficient for --rps {}",
804                    basis, num_ops, self.vus, rps,
805                ));
806            }
807        }
808
809        let k6_config = K6Config {
810            target_url: self.target.clone(),
811            base_path,
812            scenario,
813            duration_secs: Self::parse_duration(&self.duration)?,
814            max_vus: self.vus,
815            threshold_percentile: self.threshold_percentile.clone(),
816            threshold_ms: self.threshold_ms,
817            max_error_rate: self.max_error_rate,
818            auth_header: self.auth.clone(),
819            custom_headers,
820            skip_tls_verify: self.skip_tls_verify,
821            security_testing_enabled,
822            chunked_request_bodies: self.chunked_request_bodies,
823            target_rps: self.target_rps,
824            no_keep_alive: self.no_keep_alive,
825            // Round 22.3 — wire `--geo-source-ip` / `--geo-source-header`
826            // through to the k6 generator so the rendered script
827            // rotates the forwarded-IP headers per iteration. Pre-fix
828            // these were Vec::new() and the script never set the
829            // headers in bench mode.
830            geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
831                .into_iter()
832                .map(|ip| ip.to_string())
833                .collect(),
834            geo_source_headers: if self.geo_source_headers.is_empty()
835                && !self.geo_source_ips.is_empty()
836            {
837                crate::conformance::self_test::default_geo_source_headers()
838            } else {
839                self.geo_source_headers.clone()
840            },
841        };
842
843        let generator = K6ScriptGenerator::new(k6_config, templates);
844        let mut script = generator.generate()?;
845        TerminalReporter::print_success("k6 script generated");
846
847        // Check if any advanced features are enabled
848        let has_advanced_features = self.data_file.is_some()
849            || self.error_rate.is_some()
850            || self.security_test
851            || self.parallel_create.is_some()
852            || self.wafbench_dir.is_some();
853
854        // Enhance script with advanced features
855        if has_advanced_features {
856            script = self.generate_enhanced_script(&script)?;
857        }
858
859        // Add mock server integration code
860        if mock_config.is_mock_server {
861            let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
862            let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
863            let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
864
865            // Insert mock server code after imports
866            if let Some(import_end) = script.find("export const options") {
867                script.insert_str(
868                    import_end,
869                    &format!(
870                        "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
871                        helper_code, setup_code, teardown_code
872                    ),
873                );
874            }
875        }
876
877        // Validate the generated script
878        TerminalReporter::print_progress("Validating k6 script...");
879        let validation_errors = K6ScriptGenerator::validate_script(&script);
880        if !validation_errors.is_empty() {
881            TerminalReporter::print_error("Script validation failed");
882            for error in &validation_errors {
883                eprintln!("  {}", error);
884            }
885            return Err(BenchError::Other(format!(
886                "Generated k6 script has {} validation error(s). Please check the output above.",
887                validation_errors.len()
888            )));
889        }
890        TerminalReporter::print_success("Script validation passed");
891
892        // Write script to file
893        let script_path = if let Some(output) = &self.script_output {
894            output.clone()
895        } else {
896            self.output.join("k6-script.js")
897        };
898
899        if let Some(parent) = script_path.parent() {
900            std::fs::create_dir_all(parent)?;
901        }
902        std::fs::write(&script_path, &script)?;
903        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
904
905        // If generate-only mode, exit here
906        if self.generate_only {
907            println!("\nScript generated successfully. Run it with:");
908            println!("  k6 run {}", script_path.display());
909            return Ok(());
910        }
911
912        // Execute k6
913        TerminalReporter::print_progress("Executing load test...");
914        let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
915
916        std::fs::create_dir_all(&self.output)?;
917
918        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
919
920        // Print results
921        let duration_secs = Self::parse_duration(&self.duration)?;
922        TerminalReporter::print_summary_full(
923            &results,
924            duration_secs,
925            self.no_keep_alive,
926            Some(num_ops),
927        );
928
929        println!("\nResults saved to: {}", self.output.display());
930
931        Ok(())
932    }
933
934    /// Execute multi-target bench testing
935    async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
936        TerminalReporter::print_progress("Parsing targets file...");
937        let targets = parse_targets_file(targets_file)?;
938        let num_targets = targets.len();
939        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
940
941        if targets.is_empty() {
942            return Err(BenchError::Other("No targets found in file".to_string()));
943        }
944
945        // Determine max concurrency
946        let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
947        let max_concurrency = max_concurrency.min(num_targets); // Don't exceed number of targets
948
949        // Print header for multi-target mode
950        TerminalReporter::print_header(
951            &self.get_spec_display_name(),
952            &format!("{} targets", num_targets),
953            0,
954            &self.scenario,
955            Self::parse_duration(&self.duration)?,
956        );
957
958        // Create parallel executor
959        let executor = ParallelExecutor::new(
960            BenchCommand {
961                // Clone all fields except targets_file (we don't need it in the executor)
962                spec: self.spec.clone(),
963                spec_dir: self.spec_dir.clone(),
964                merge_conflicts: self.merge_conflicts.clone(),
965                spec_mode: self.spec_mode.clone(),
966                dependency_config: self.dependency_config.clone(),
967                target: self.target.clone(), // Not used in multi-target mode, but kept for compatibility
968                base_path: self.base_path.clone(),
969                duration: self.duration.clone(),
970                vus: self.vus,
971                target_rps: self.target_rps,
972                no_keep_alive: self.no_keep_alive,
973                scenario: self.scenario.clone(),
974                operations: self.operations.clone(),
975                exclude_operations: self.exclude_operations.clone(),
976                auth: self.auth.clone(),
977                headers: self.headers.clone(),
978                output: self.output.clone(),
979                generate_only: self.generate_only,
980                script_output: self.script_output.clone(),
981                threshold_percentile: self.threshold_percentile.clone(),
982                threshold_ms: self.threshold_ms,
983                max_error_rate: self.max_error_rate,
984                verbose: self.verbose,
985                skip_tls_verify: self.skip_tls_verify,
986                chunked_request_bodies: self.chunked_request_bodies,
987                targets_file: None,
988                max_concurrency: None,
989                results_format: self.results_format.clone(),
990                params_file: self.params_file.clone(),
991                crud_flow: self.crud_flow,
992                flow_config: self.flow_config.clone(),
993                extract_fields: self.extract_fields.clone(),
994                parallel_create: self.parallel_create,
995                data_file: self.data_file.clone(),
996                data_distribution: self.data_distribution.clone(),
997                data_mappings: self.data_mappings.clone(),
998                per_uri_control: self.per_uri_control,
999                error_rate: self.error_rate,
1000                error_types: self.error_types.clone(),
1001                security_test: self.security_test,
1002                security_payloads: self.security_payloads.clone(),
1003                security_categories: self.security_categories.clone(),
1004                security_target_fields: self.security_target_fields.clone(),
1005                wafbench_dir: self.wafbench_dir.clone(),
1006                wafbench_cycle_all: self.wafbench_cycle_all,
1007                owasp_api_top10: self.owasp_api_top10,
1008                owasp_categories: self.owasp_categories.clone(),
1009                owasp_auth_header: self.owasp_auth_header.clone(),
1010                owasp_auth_token: self.owasp_auth_token.clone(),
1011                owasp_admin_paths: self.owasp_admin_paths.clone(),
1012                owasp_id_fields: self.owasp_id_fields.clone(),
1013                owasp_report: self.owasp_report.clone(),
1014                owasp_report_format: self.owasp_report_format.clone(),
1015                owasp_iterations: self.owasp_iterations,
1016                conformance: false,
1017                conformance_api_key: None,
1018                conformance_basic_auth: None,
1019                conformance_report: PathBuf::from("conformance-report.json"),
1020                conformance_categories: None,
1021                conformance_report_format: "json".to_string(),
1022                conformance_headers: vec![],
1023                conformance_all_operations: false,
1024                conformance_custom: None,
1025                conformance_delay_ms: 0,
1026                use_k6: false,
1027                conformance_custom_filter: None,
1028                export_requests: false,
1029                validate_requests: false,
1030                conformance_self_test: false,
1031                conformance_self_test_capture: false,
1032                conformance_self_test_iterations: 1,
1033                conformance_self_test_duration: None,
1034                validate_response_schemas: false,
1035                // Issue #79 r54 (Srikanth on 0.3.200): these were hardcoded to
1036                // empty, so `--source-ip` / `--geo-source-ip` were silently
1037                // dropped in multi-target (`--targets-file`) mode. Carry them
1038                // through to the ParallelExecutor so k6 gets `--local-ips`.
1039                source_ips: self.source_ips.clone(),
1040                geo_source_ips: self.geo_source_ips.clone(),
1041                geo_source_headers: self.geo_source_headers.clone(),
1042                report_missed_cap: None,
1043            },
1044            targets,
1045            max_concurrency,
1046        );
1047
1048        // Execute all targets
1049        let start_time = std::time::Instant::now();
1050        let aggregated_results = executor.execute_all().await?;
1051        let elapsed = start_time.elapsed();
1052
1053        // Organize and report results
1054        self.report_multi_target_results(&aggregated_results, elapsed)?;
1055
1056        Ok(())
1057    }
1058
1059    /// Report results for multi-target execution
1060    fn report_multi_target_results(
1061        &self,
1062        results: &AggregatedResults,
1063        elapsed: std::time::Duration,
1064    ) -> Result<()> {
1065        // Print summary
1066        TerminalReporter::print_multi_target_summary(results);
1067
1068        // Print elapsed time
1069        let total_secs = elapsed.as_secs();
1070        let hours = total_secs / 3600;
1071        let minutes = (total_secs % 3600) / 60;
1072        let seconds = total_secs % 60;
1073        if hours > 0 {
1074            println!("\n  Total Elapsed Time:   {}h {}m {}s", hours, minutes, seconds);
1075        } else if minutes > 0 {
1076            println!("\n  Total Elapsed Time:   {}m {}s", minutes, seconds);
1077        } else {
1078            println!("\n  Total Elapsed Time:   {}s", seconds);
1079        }
1080
1081        // Save aggregated summary if requested
1082        if self.results_format == "aggregated" || self.results_format == "both" {
1083            let summary_path = self.output.join("aggregated_summary.json");
1084            let summary_json = serde_json::json!({
1085                "total_elapsed_seconds": elapsed.as_secs(),
1086                "total_targets": results.total_targets,
1087                "successful_targets": results.successful_targets,
1088                "failed_targets": results.failed_targets,
1089                "aggregated_metrics": {
1090                    "total_requests": results.aggregated_metrics.total_requests,
1091                    "total_failed_requests": results.aggregated_metrics.total_failed_requests,
1092                    "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
1093                    "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
1094                    "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
1095                    "error_rate": results.aggregated_metrics.error_rate,
1096                    "total_rps": results.aggregated_metrics.total_rps,
1097                    "avg_rps": results.aggregated_metrics.avg_rps,
1098                    "total_vus_max": results.aggregated_metrics.total_vus_max,
1099                },
1100                "target_results": results.target_results.iter().map(|r| {
1101                    serde_json::json!({
1102                        "target_url": r.target_url,
1103                        "target_index": r.target_index,
1104                        "success": r.success,
1105                        "error": r.error,
1106                        "total_requests": r.results.total_requests,
1107                        "failed_requests": r.results.failed_requests,
1108                        "avg_duration_ms": r.results.avg_duration_ms,
1109                        "min_duration_ms": r.results.min_duration_ms,
1110                        "med_duration_ms": r.results.med_duration_ms,
1111                        "p90_duration_ms": r.results.p90_duration_ms,
1112                        "p95_duration_ms": r.results.p95_duration_ms,
1113                        "p99_duration_ms": r.results.p99_duration_ms,
1114                        "max_duration_ms": r.results.max_duration_ms,
1115                        "rps": r.results.rps,
1116                        "vus_max": r.results.vus_max,
1117                        "output_dir": r.output_dir.to_string_lossy(),
1118                    })
1119                }).collect::<Vec<_>>(),
1120            });
1121
1122            std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
1123            TerminalReporter::print_success(&format!(
1124                "Aggregated summary saved to: {}",
1125                summary_path.display()
1126            ));
1127        }
1128
1129        // Write CSV with all per-target results for easy parsing
1130        let csv_path = self.output.join("all_targets.csv");
1131        let mut csv = String::from(
1132            "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
1133        );
1134        for r in &results.target_results {
1135            csv.push_str(&format!(
1136                "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
1137                r.target_url,
1138                r.success,
1139                r.results.total_requests,
1140                r.results.failed_requests,
1141                r.results.rps,
1142                r.results.vus_max,
1143                r.results.min_duration_ms,
1144                r.results.avg_duration_ms,
1145                r.results.med_duration_ms,
1146                r.results.p90_duration_ms,
1147                r.results.p95_duration_ms,
1148                r.results.p99_duration_ms,
1149                r.results.max_duration_ms,
1150                r.error.as_deref().unwrap_or(""),
1151            ));
1152        }
1153        let _ = std::fs::write(&csv_path, &csv);
1154
1155        println!("\nResults saved to: {}", self.output.display());
1156        println!("  - Per-target results: {}", self.output.join("target_*").display());
1157        println!("  - All targets CSV:    {}", csv_path.display());
1158        if self.results_format == "aggregated" || self.results_format == "both" {
1159            println!(
1160                "  - Aggregated summary: {}",
1161                self.output.join("aggregated_summary.json").display()
1162            );
1163        }
1164
1165        Ok(())
1166    }
1167
1168    /// Parse duration string (e.g., "30s", "5m", "1h") to seconds
1169    pub fn parse_duration(duration: &str) -> Result<u64> {
1170        let duration = duration.trim();
1171
1172        if let Some(secs) = duration.strip_suffix('s') {
1173            secs.parse::<u64>()
1174                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1175        } else if let Some(mins) = duration.strip_suffix('m') {
1176            mins.parse::<u64>()
1177                .map(|m| m * 60)
1178                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1179        } else if let Some(hours) = duration.strip_suffix('h') {
1180            hours
1181                .parse::<u64>()
1182                .map(|h| h * 3600)
1183                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1184        } else {
1185            // Try parsing as seconds without suffix
1186            duration
1187                .parse::<u64>()
1188                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1189        }
1190    }
1191
1192    /// Parse headers from the repeated `--headers "Key:Value"` flags.
1193    pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
1194        let mut headers = parse_header_string(&self.headers)?;
1195
1196        // Round 47 (#79) — Srikanth on 0.3.191 tried
1197        // `mockforge bench --conformance-basic-auth user:pass --spec ...`
1198        // (no `--conformance` flag) and the auth never landed because
1199        // the bench path only reads `self.headers`. Fold the
1200        // conformance auth shortcuts (basic, api-key, the round-46
1201        // --auth-bearer collected in `conformance_headers`) into the
1202        // shared header map so the SAME auth flags work whether the
1203        // run is plain bench, conformance, or self-test. De-dup
1204        // case-insensitively on header name; explicit
1205        // `--header "Authorization: ..."` keeps priority.
1206        let already_has = |hs: &HashMap<String, String>, name: &str| -> bool {
1207            hs.keys().any(|k| k.eq_ignore_ascii_case(name))
1208        };
1209
1210        if !already_has(&headers, "Authorization") {
1211            if let Some(b) = self.conformance_basic_auth.as_ref().filter(|s| !s.is_empty()) {
1212                use base64::Engine as _;
1213                let encoded = base64::engine::general_purpose::STANDARD.encode(b.as_bytes());
1214                headers.insert("Authorization".to_string(), format!("Basic {}", encoded));
1215            }
1216        }
1217
1218        // `--conformance-headers "Name: value"` was already conformance-
1219        // only; reuse the same flag for bench. Round 46's --auth-bearer
1220        // injects `Authorization: Bearer ...` into this list at CLI
1221        // dispatch time, so this single pass also picks up the bearer
1222        // token for plain bench.
1223        for line in &self.conformance_headers {
1224            let Some((name, value)) = line.split_once(':') else {
1225                continue;
1226            };
1227            let name = name.trim();
1228            let value = value.trim();
1229            if name.is_empty() || already_has(&headers, name) {
1230                continue;
1231            }
1232            headers.insert(name.to_string(), value.to_string());
1233        }
1234
1235        // `--conformance-api-key` is conformance-test-specific (probe
1236        // pattern over multiple header names). Forwarding it to plain
1237        // bench as a simple header would mislead a user expecting the
1238        // probe behaviour, so we leave that path conformance-only and
1239        // surface a one-line note when it's set without `--conformance`.
1240        if !self.conformance && self.conformance_api_key.is_some() {
1241            TerminalReporter::print_warning(
1242                "--conformance-api-key only fires under --conformance. For plain bench use --header 'X-API-Key: ...'.",
1243            );
1244        }
1245
1246        Ok(headers)
1247    }
1248
1249    fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
1250        let extracted_path = output_dir.join("extracted_values.json");
1251        if !extracted_path.exists() {
1252            return Ok(ExtractedValues::new());
1253        }
1254
1255        let content = std::fs::read_to_string(&extracted_path)
1256            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1257        let parsed: serde_json::Value = serde_json::from_str(&content)
1258            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1259
1260        let mut extracted = ExtractedValues::new();
1261        if let Some(values) = parsed.as_object() {
1262            for (key, value) in values {
1263                extracted.set(key.clone(), value.clone());
1264            }
1265        }
1266
1267        Ok(extracted)
1268    }
1269
1270    /// Resolve the effective base path for API endpoints
1271    ///
1272    /// Priority:
1273    /// 1. CLI --base-path option (if provided, even if empty string)
1274    /// 2. Base path extracted from OpenAPI spec's servers URL
1275    /// 3. None (no base path)
1276    ///
1277    /// An empty string from CLI explicitly disables base path.
1278    fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
1279        // CLI option takes priority (including empty string to disable)
1280        if let Some(cli_base_path) = &self.base_path {
1281            if cli_base_path.is_empty() {
1282                // Empty string explicitly means "no base path"
1283                return None;
1284            }
1285            return Some(cli_base_path.clone());
1286        }
1287
1288        // Fall back to spec's base path
1289        parser.get_base_path()
1290    }
1291
1292    /// Build mock server integration configuration
1293    async fn build_mock_config(&self) -> MockIntegrationConfig {
1294        // Check if target looks like a mock server
1295        if MockServerDetector::looks_like_mock_server(&self.target) {
1296            // Try to detect if it's actually a MockForge server
1297            if let Ok(info) = MockServerDetector::detect(&self.target).await {
1298                if info.is_mockforge {
1299                    TerminalReporter::print_success(&format!(
1300                        "Detected MockForge server (version: {})",
1301                        info.version.as_deref().unwrap_or("unknown")
1302                    ));
1303                    return MockIntegrationConfig::mock_server();
1304                }
1305            }
1306        }
1307        MockIntegrationConfig::real_api()
1308    }
1309
1310    /// Build CRUD flow configuration
1311    fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
1312        if !self.crud_flow {
1313            return None;
1314        }
1315
1316        // If flow_config file is provided, load it
1317        if let Some(config_path) = &self.flow_config {
1318            match CrudFlowConfig::from_file(config_path) {
1319                Ok(config) => return Some(config),
1320                Err(e) => {
1321                    TerminalReporter::print_warning(&format!(
1322                        "Failed to load flow config: {}. Using auto-detection.",
1323                        e
1324                    ));
1325                }
1326            }
1327        }
1328
1329        // Parse extract fields
1330        let extract_fields = self
1331            .extract_fields
1332            .as_ref()
1333            .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
1334            .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
1335
1336        Some(CrudFlowConfig {
1337            flows: Vec::new(), // Will be auto-detected
1338            default_extract_fields: extract_fields,
1339        })
1340    }
1341
1342    /// Build data-driven testing configuration
1343    fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
1344        let data_file = self.data_file.as_ref()?;
1345
1346        let distribution = DataDistribution::from_str(&self.data_distribution)
1347            .unwrap_or(DataDistribution::UniquePerVu);
1348
1349        let mappings = self
1350            .data_mappings
1351            .as_ref()
1352            .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
1353            .unwrap_or_default();
1354
1355        Some(DataDrivenConfig {
1356            file_path: data_file.to_string_lossy().to_string(),
1357            distribution,
1358            mappings,
1359            csv_has_header: true,
1360            per_uri_control: self.per_uri_control,
1361            per_uri_columns: crate::data_driven::PerUriColumns::default(),
1362        })
1363    }
1364
1365    /// Build invalid data testing configuration
1366    fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
1367        let error_rate = self.error_rate?;
1368
1369        let error_types = self
1370            .error_types
1371            .as_ref()
1372            .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
1373            .unwrap_or_default();
1374
1375        Some(InvalidDataConfig {
1376            error_rate,
1377            error_types,
1378            target_fields: Vec::new(),
1379        })
1380    }
1381
1382    /// Build security testing configuration
1383    fn build_security_config(&self) -> Option<SecurityTestConfig> {
1384        if !self.security_test {
1385            return None;
1386        }
1387
1388        let categories = self
1389            .security_categories
1390            .as_ref()
1391            .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1392            .unwrap_or_else(|| {
1393                let mut default = HashSet::new();
1394                default.insert(SecurityCategory::SqlInjection);
1395                default.insert(SecurityCategory::Xss);
1396                default
1397            });
1398
1399        let target_fields = self
1400            .security_target_fields
1401            .as_ref()
1402            .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1403            .unwrap_or_default();
1404
1405        let custom_payloads_file =
1406            self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1407
1408        Some(SecurityTestConfig {
1409            enabled: true,
1410            categories,
1411            target_fields,
1412            custom_payloads_file,
1413            include_high_risk: false,
1414        })
1415    }
1416
1417    /// Build parallel execution configuration
1418    fn build_parallel_config(&self) -> Option<ParallelConfig> {
1419        let count = self.parallel_create?;
1420
1421        Some(ParallelConfig::new(count))
1422    }
1423
1424    /// Load WAFBench payloads from the specified directory or pattern
1425    fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
1426        let Some(ref wafbench_dir) = self.wafbench_dir else {
1427            return Vec::new();
1428        };
1429
1430        let mut loader = WafBenchLoader::new();
1431
1432        if let Err(e) = loader.load_from_pattern(wafbench_dir) {
1433            TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
1434            return Vec::new();
1435        }
1436
1437        let stats = loader.stats();
1438
1439        if stats.files_processed == 0 {
1440            TerminalReporter::print_warning(&format!(
1441                "No WAFBench YAML files found matching '{}'",
1442                wafbench_dir
1443            ));
1444            // Also report any parse errors that may explain why no files were processed
1445            if !stats.parse_errors.is_empty() {
1446                TerminalReporter::print_warning("Some files were found but failed to parse:");
1447                for error in &stats.parse_errors {
1448                    TerminalReporter::print_warning(&format!("  - {}", error));
1449                }
1450            }
1451            return Vec::new();
1452        }
1453
1454        TerminalReporter::print_progress(&format!(
1455            "Loaded {} WAFBench files, {} test cases, {} payloads",
1456            stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1457        ));
1458
1459        // Print category breakdown
1460        for (category, count) in &stats.by_category {
1461            TerminalReporter::print_progress(&format!("  - {}: {} tests", category, count));
1462        }
1463
1464        // Report any parse errors
1465        for error in &stats.parse_errors {
1466            TerminalReporter::print_warning(&format!("  Parse error: {}", error));
1467        }
1468
1469        loader.to_security_payloads()
1470    }
1471
1472    /// Generate enhanced k6 script with advanced features
1473    pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1474        let mut enhanced_script = base_script.to_string();
1475        let mut additional_code = String::new();
1476
1477        // Add data-driven testing code
1478        if let Some(config) = self.build_data_driven_config() {
1479            TerminalReporter::print_progress("Adding data-driven testing support...");
1480            additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1481            additional_code.push('\n');
1482            TerminalReporter::print_success("Data-driven testing enabled");
1483        }
1484
1485        // Add invalid data generation code
1486        if let Some(config) = self.build_invalid_data_config() {
1487            TerminalReporter::print_progress("Adding invalid data testing support...");
1488            additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1489            additional_code.push('\n');
1490            additional_code
1491                .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1492            additional_code.push('\n');
1493            additional_code
1494                .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1495            additional_code.push('\n');
1496            TerminalReporter::print_success(&format!(
1497                "Invalid data testing enabled ({}% error rate)",
1498                (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1499            ));
1500        }
1501
1502        // Add security testing code
1503        let security_config = self.build_security_config();
1504        let wafbench_payloads = self.load_wafbench_payloads();
1505        let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1506
1507        if security_config.is_some() || !wafbench_payloads.is_empty() {
1508            TerminalReporter::print_progress("Adding security testing support...");
1509
1510            // Combine built-in payloads with WAFBench payloads
1511            let mut payload_list: Vec<SecurityPayload> = Vec::new();
1512
1513            if let Some(ref config) = security_config {
1514                payload_list.extend(SecurityPayloads::get_payloads(config));
1515            }
1516
1517            // Add WAFBench payloads
1518            if !wafbench_payloads.is_empty() {
1519                TerminalReporter::print_progress(&format!(
1520                    "Loading {} WAFBench attack patterns...",
1521                    wafbench_payloads.len()
1522                ));
1523                payload_list.extend(wafbench_payloads);
1524            }
1525
1526            let target_fields =
1527                security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1528
1529            additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1530                &payload_list,
1531                self.wafbench_cycle_all,
1532            ));
1533            additional_code.push('\n');
1534            additional_code
1535                .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1536            additional_code.push('\n');
1537            additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1538            additional_code.push('\n');
1539
1540            let mode = if self.wafbench_cycle_all {
1541                "cycle-all"
1542            } else {
1543                "random"
1544            };
1545            TerminalReporter::print_success(&format!(
1546                "Security testing enabled ({} payloads, {} mode)",
1547                payload_list.len(),
1548                mode
1549            ));
1550        } else if security_requested {
1551            // User requested security testing (e.g., --wafbench-dir) but no payloads were loaded.
1552            // The template has security_testing_enabled=true so it renders calling code.
1553            // We must inject stub definitions to avoid undefined function references.
1554            TerminalReporter::print_warning(
1555                "Security testing was requested but no payloads were loaded. \
1556                 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1557            );
1558            additional_code
1559                .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1560            additional_code.push('\n');
1561            additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1562            additional_code.push('\n');
1563        }
1564
1565        // Add parallel execution code
1566        if let Some(config) = self.build_parallel_config() {
1567            TerminalReporter::print_progress("Adding parallel execution support...");
1568            additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1569            additional_code.push('\n');
1570            TerminalReporter::print_success(&format!(
1571                "Parallel execution enabled (count: {})",
1572                config.count
1573            ));
1574        }
1575
1576        // Insert additional code after the imports section
1577        if !additional_code.is_empty() {
1578            // Find the end of the import section
1579            if let Some(import_end) = enhanced_script.find("export const options") {
1580                enhanced_script.insert_str(
1581                    import_end,
1582                    &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1583                );
1584            }
1585        }
1586
1587        Ok(enhanced_script)
1588    }
1589
1590    /// Execute specs sequentially with dependency ordering and value passing
1591    async fn execute_sequential_specs(&self) -> Result<()> {
1592        TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1593
1594        // Load all specs (without merging)
1595        let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1596
1597        if !self.spec.is_empty() {
1598            let specs = load_specs_from_files(self.spec.clone())
1599                .await
1600                .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1601            all_specs.extend(specs);
1602        }
1603
1604        if let Some(spec_dir) = &self.spec_dir {
1605            let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1606                BenchError::Other(format!("Failed to load specs from directory: {}", e))
1607            })?;
1608            all_specs.extend(dir_specs);
1609        }
1610
1611        if all_specs.is_empty() {
1612            return Err(BenchError::Other(
1613                "No spec files found for sequential execution".to_string(),
1614            ));
1615        }
1616
1617        TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1618
1619        // Load dependency config or auto-detect
1620        let execution_order = if let Some(config_path) = &self.dependency_config {
1621            TerminalReporter::print_progress("Loading dependency configuration...");
1622            let config = SpecDependencyConfig::from_file(config_path)?;
1623
1624            if !config.disable_auto_detect && config.execution_order.is_empty() {
1625                // Auto-detect if config doesn't specify order
1626                self.detect_and_sort_specs(&all_specs)?
1627            } else {
1628                // Use configured order
1629                config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1630            }
1631        } else {
1632            // Auto-detect dependencies
1633            self.detect_and_sort_specs(&all_specs)?
1634        };
1635
1636        TerminalReporter::print_success(&format!(
1637            "Execution order: {}",
1638            execution_order
1639                .iter()
1640                .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1641                .collect::<Vec<_>>()
1642                .join(" → ")
1643        ));
1644
1645        // Execute each spec in order
1646        let mut extracted_values = ExtractedValues::new();
1647        let total_specs = execution_order.len();
1648
1649        for (index, spec_path) in execution_order.iter().enumerate() {
1650            let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1651
1652            TerminalReporter::print_progress(&format!(
1653                "[{}/{}] Executing spec: {}",
1654                index + 1,
1655                total_specs,
1656                spec_name
1657            ));
1658
1659            // Find the spec in our loaded specs (match by full path or filename)
1660            let spec = all_specs
1661                .iter()
1662                .find(|(p, _)| {
1663                    p == spec_path
1664                        || p.file_name() == spec_path.file_name()
1665                        || p.file_name() == Some(spec_path.as_os_str())
1666                })
1667                .map(|(_, s)| s.clone())
1668                .ok_or_else(|| {
1669                    BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1670                })?;
1671
1672            // Execute this spec with any extracted values from previous specs
1673            let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1674
1675            // Merge extracted values for the next spec
1676            extracted_values.merge(&new_values);
1677
1678            TerminalReporter::print_success(&format!(
1679                "[{}/{}] Completed: {} (extracted {} values)",
1680                index + 1,
1681                total_specs,
1682                spec_name,
1683                new_values.values.len()
1684            ));
1685        }
1686
1687        TerminalReporter::print_success(&format!(
1688            "Sequential execution complete: {} specs executed",
1689            total_specs
1690        ));
1691
1692        Ok(())
1693    }
1694
1695    /// Detect dependencies and return topologically sorted spec paths
1696    fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1697        TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1698
1699        let mut detector = DependencyDetector::new();
1700        let dependencies = detector.detect_dependencies(specs);
1701
1702        if dependencies.is_empty() {
1703            TerminalReporter::print_progress("No dependencies detected, using file order");
1704            return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1705        }
1706
1707        TerminalReporter::print_progress(&format!(
1708            "Detected {} cross-spec dependencies",
1709            dependencies.len()
1710        ));
1711
1712        for dep in &dependencies {
1713            TerminalReporter::print_progress(&format!(
1714                "  {} → {} (via field '{}')",
1715                dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1716                dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1717                dep.field_name
1718            ));
1719        }
1720
1721        topological_sort(specs, &dependencies)
1722    }
1723
1724    /// Execute a single spec and extract values for dependent specs
1725    async fn execute_single_spec(
1726        &self,
1727        spec: &OpenApiSpec,
1728        spec_name: &str,
1729        _external_values: &ExtractedValues,
1730    ) -> Result<ExtractedValues> {
1731        let parser = SpecParser::from_spec(spec.clone());
1732
1733        // For now, we execute in CRUD flow mode if enabled, otherwise standard mode
1734        if self.crud_flow {
1735            // Execute CRUD flow and extract values
1736            self.execute_crud_flow_with_extraction(&parser, spec_name).await
1737        } else {
1738            // Execute standard benchmark (no value extraction in non-CRUD mode)
1739            self.execute_standard_spec(&parser, spec_name).await?;
1740            Ok(ExtractedValues::new())
1741        }
1742    }
1743
1744    /// Execute CRUD flow with value extraction for sequential mode
1745    async fn execute_crud_flow_with_extraction(
1746        &self,
1747        parser: &SpecParser,
1748        spec_name: &str,
1749    ) -> Result<ExtractedValues> {
1750        let operations = parser.get_operations();
1751        let flows = CrudFlowDetector::detect_flows(&operations);
1752
1753        if flows.is_empty() {
1754            TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1755            return Ok(ExtractedValues::new());
1756        }
1757
1758        TerminalReporter::print_progress(&format!(
1759            "  {} CRUD flow(s) in {}",
1760            flows.len(),
1761            spec_name
1762        ));
1763
1764        // Generate and execute the CRUD flow script
1765        let mut handlebars = handlebars::Handlebars::new();
1766        // Register json helper for serializing arrays/objects in templates
1767        handlebars.register_helper(
1768            "json",
1769            Box::new(
1770                |h: &handlebars::Helper,
1771                 _: &handlebars::Handlebars,
1772                 _: &handlebars::Context,
1773                 _: &mut handlebars::RenderContext,
1774                 out: &mut dyn handlebars::Output|
1775                 -> handlebars::HelperResult {
1776                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1777                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1778                    Ok(())
1779                },
1780            ),
1781        );
1782        let template = include_str!("templates/k6_crud_flow.hbs");
1783        let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1784
1785        let custom_headers = self.parse_headers()?;
1786        let config = self.build_crud_flow_config().unwrap_or_default();
1787
1788        // Load parameter overrides if provided (for body configurations)
1789        let param_overrides = if let Some(params_file) = &self.params_file {
1790            let overrides = ParameterOverrides::from_file(params_file)?;
1791            Some(overrides)
1792        } else {
1793            None
1794        };
1795
1796        // Generate stages from scenario
1797        let duration_secs = Self::parse_duration(&self.duration)?;
1798        let scenario =
1799            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1800        let stages = scenario.generate_stages(duration_secs, self.vus);
1801
1802        // Resolve base path (CLI option takes priority over spec's servers URL)
1803        let api_base_path = self.resolve_base_path(parser);
1804
1805        // Build headers JSON string for the template
1806        let mut all_headers = custom_headers.clone();
1807        if let Some(auth) = &self.auth {
1808            all_headers.insert("Authorization".to_string(), auth.clone());
1809        }
1810        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1811
1812        // Track all dynamic placeholders across all operations
1813        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1814
1815        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1816            // Use the metric-name sanitizer (caps at 112 chars + hash suffix)
1817            // so deeply nested flow names don't blow past k6's 128-char limit
1818            // when concatenated with `_step{i}_latency`. See issue #79.
1819            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1820            serde_json::json!({
1821                "name": sanitized_name.clone(),
1822                "display_name": f.name,
1823                "base_path": f.base_path,
1824                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1825                    // Parse operation to get method and path
1826                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1827                    let method_raw = if !parts.is_empty() {
1828                        parts[0].to_uppercase()
1829                    } else {
1830                        "GET".to_string()
1831                    };
1832                    let method = if !parts.is_empty() {
1833                        let m = parts[0].to_lowercase();
1834                        // k6 uses 'del' for DELETE
1835                        if m == "delete" { "del".to_string() } else { m }
1836                    } else {
1837                        "get".to_string()
1838                    };
1839                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1840                    // Prepend API base path if configured
1841                    let path = if let Some(ref bp) = api_base_path {
1842                        format!("{}{}", bp, raw_path)
1843                    } else {
1844                        raw_path.to_string()
1845                    };
1846                    let is_get_or_head = method == "get" || method == "head";
1847                    // POST, PUT, PATCH typically have bodies
1848                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1849
1850                    // Look up body from params file if available
1851                    let body_value = if has_body {
1852                        param_overrides.as_ref()
1853                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1854                            .and_then(|oo| oo.body)
1855                            .unwrap_or_else(|| serde_json::json!({}))
1856                    } else {
1857                        serde_json::json!({})
1858                    };
1859
1860                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
1861                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1862
1863                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
1864                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1865                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1866
1867                    serde_json::json!({
1868                        "operation": s.operation,
1869                        "method": method,
1870                        "path": path,
1871                        "extract": s.extract,
1872                        "use_values": s.use_values,
1873                        "use_body": s.use_body,
1874                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1875                        "inject_attacks": s.inject_attacks,
1876                        "attack_types": s.attack_types,
1877                        "description": s.description,
1878                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1879                        "is_get_or_head": is_get_or_head,
1880                        "has_body": has_body,
1881                        "body": processed_body.value,
1882                        "body_is_dynamic": body_is_dynamic,
1883                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1884                    })
1885                }).collect::<Vec<_>>(),
1886            })
1887        }).collect();
1888
1889        // Collect all placeholders from all steps
1890        for flow_data in &flows_data {
1891            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1892                for step in steps {
1893                    if let Some(placeholders_arr) =
1894                        step.get("_placeholders").and_then(|p| p.as_array())
1895                    {
1896                        for p_str in placeholders_arr {
1897                            if let Some(p_name) = p_str.as_str() {
1898                                match p_name {
1899                                    "VU" => {
1900                                        all_placeholders.insert(DynamicPlaceholder::VU);
1901                                    }
1902                                    "Iteration" => {
1903                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
1904                                    }
1905                                    "Timestamp" => {
1906                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
1907                                    }
1908                                    "UUID" => {
1909                                        all_placeholders.insert(DynamicPlaceholder::UUID);
1910                                    }
1911                                    "Random" => {
1912                                        all_placeholders.insert(DynamicPlaceholder::Random);
1913                                    }
1914                                    "Counter" => {
1915                                        all_placeholders.insert(DynamicPlaceholder::Counter);
1916                                    }
1917                                    "Date" => {
1918                                        all_placeholders.insert(DynamicPlaceholder::Date);
1919                                    }
1920                                    "VuIter" => {
1921                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
1922                                    }
1923                                    _ => {}
1924                                }
1925                            }
1926                        }
1927                    }
1928                }
1929            }
1930        }
1931
1932        // Get required imports and globals based on placeholders used
1933        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1934        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1935
1936        // Check if security testing is enabled
1937        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1938
1939        let data = serde_json::json!({
1940            "base_url": self.target,
1941            "flows": flows_data,
1942            "extract_fields": config.default_extract_fields,
1943            "duration_secs": duration_secs,
1944            "max_vus": self.vus,
1945            "auth_header": self.auth,
1946            "custom_headers": custom_headers,
1947            "skip_tls_verify": self.skip_tls_verify,
1948            // Add missing template fields
1949            "stages": stages.iter().map(|s| serde_json::json!({
1950                "duration": s.duration,
1951                "target": s.target,
1952            })).collect::<Vec<_>>(),
1953            "threshold_percentile": self.threshold_percentile,
1954            "threshold_ms": self.threshold_ms,
1955            "max_error_rate": self.max_error_rate,
1956            "headers": headers_json,
1957            "dynamic_imports": required_imports,
1958            "dynamic_globals": required_globals,
1959            "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
1960            // Security testing settings
1961            "security_testing_enabled": security_testing_enabled,
1962            "has_custom_headers": !custom_headers.is_empty(),
1963        });
1964
1965        let mut script = handlebars
1966            .render_template(template, &data)
1967            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1968
1969        // Enhance script with security testing support if enabled
1970        if security_testing_enabled {
1971            script = self.generate_enhanced_script(&script)?;
1972        }
1973
1974        // Write and execute script
1975        let script_path =
1976            self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
1977
1978        std::fs::create_dir_all(self.output.clone())?;
1979        std::fs::write(&script_path, &script)?;
1980
1981        if !self.generate_only {
1982            let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
1983            std::fs::create_dir_all(&output_dir)?;
1984
1985            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1986
1987            let extracted = Self::parse_extracted_values(&output_dir)?;
1988            TerminalReporter::print_progress(&format!(
1989                "  Extracted {} value(s) from {}",
1990                extracted.values.len(),
1991                spec_name
1992            ));
1993            return Ok(extracted);
1994        }
1995
1996        Ok(ExtractedValues::new())
1997    }
1998
1999    /// Execute standard (non-CRUD) spec benchmark
2000    async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
2001        let mut operations = if let Some(filter) = &self.operations {
2002            parser.filter_operations(filter)?
2003        } else {
2004            parser.get_operations()
2005        };
2006
2007        if let Some(exclude) = &self.exclude_operations {
2008            operations = parser.exclude_operations(operations, exclude)?;
2009        }
2010
2011        if operations.is_empty() {
2012            TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
2013            return Ok(());
2014        }
2015
2016        TerminalReporter::print_progress(&format!(
2017            "  {} operations in {}",
2018            operations.len(),
2019            spec_name
2020        ));
2021
2022        // Generate request templates
2023        let templates: Vec<_> = operations
2024            .iter()
2025            .map(RequestGenerator::generate_template)
2026            .collect::<Result<Vec<_>>>()?;
2027
2028        // Parse headers
2029        let custom_headers = self.parse_headers()?;
2030
2031        // Resolve base path
2032        let base_path = self.resolve_base_path(parser);
2033
2034        // Generate k6 script
2035        let scenario =
2036            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2037
2038        let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
2039
2040        let k6_config = K6Config {
2041            target_url: self.target.clone(),
2042            base_path,
2043            scenario,
2044            duration_secs: Self::parse_duration(&self.duration)?,
2045            max_vus: self.vus,
2046            threshold_percentile: self.threshold_percentile.clone(),
2047            threshold_ms: self.threshold_ms,
2048            max_error_rate: self.max_error_rate,
2049            auth_header: self.auth.clone(),
2050            custom_headers,
2051            skip_tls_verify: self.skip_tls_verify,
2052            security_testing_enabled,
2053            chunked_request_bodies: self.chunked_request_bodies,
2054            target_rps: self.target_rps,
2055            no_keep_alive: self.no_keep_alive,
2056            // Round 22.3 — see other K6Config site above.
2057            geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
2058                .into_iter()
2059                .map(|ip| ip.to_string())
2060                .collect(),
2061            geo_source_headers: if self.geo_source_headers.is_empty()
2062                && !self.geo_source_ips.is_empty()
2063            {
2064                crate::conformance::self_test::default_geo_source_headers()
2065            } else {
2066                self.geo_source_headers.clone()
2067            },
2068        };
2069
2070        let generator = K6ScriptGenerator::new(k6_config, templates);
2071        let mut script = generator.generate()?;
2072
2073        // Enhance script with advanced features (security testing, etc.)
2074        let has_advanced_features = self.data_file.is_some()
2075            || self.error_rate.is_some()
2076            || self.security_test
2077            || self.parallel_create.is_some()
2078            || self.wafbench_dir.is_some();
2079
2080        if has_advanced_features {
2081            script = self.generate_enhanced_script(&script)?;
2082        }
2083
2084        // Write and execute script
2085        let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
2086
2087        std::fs::create_dir_all(self.output.clone())?;
2088        std::fs::write(&script_path, &script)?;
2089
2090        if !self.generate_only {
2091            let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2092            let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
2093            std::fs::create_dir_all(&output_dir)?;
2094
2095            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2096        }
2097
2098        Ok(())
2099    }
2100
2101    /// Execute CRUD flow testing mode
2102    async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
2103        // Check if a custom flow config is provided
2104        let config = self.build_crud_flow_config().unwrap_or_default();
2105
2106        // Use flows from config if provided, otherwise auto-detect
2107        let flows = if !config.flows.is_empty() {
2108            TerminalReporter::print_progress("Using custom flow configuration...");
2109            config.flows.clone()
2110        } else {
2111            TerminalReporter::print_progress("Detecting CRUD operations...");
2112            let operations = parser.get_operations();
2113            CrudFlowDetector::detect_flows(&operations)
2114        };
2115
2116        if flows.is_empty() {
2117            return Err(BenchError::Other(
2118                "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
2119            ));
2120        }
2121
2122        if config.flows.is_empty() {
2123            TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
2124        } else {
2125            TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
2126        }
2127
2128        for flow in &flows {
2129            TerminalReporter::print_progress(&format!(
2130                "  - {}: {} steps",
2131                flow.name,
2132                flow.steps.len()
2133            ));
2134        }
2135
2136        // Generate CRUD flow script
2137        let mut handlebars = handlebars::Handlebars::new();
2138        // Register json helper for serializing arrays/objects in templates
2139        handlebars.register_helper(
2140            "json",
2141            Box::new(
2142                |h: &handlebars::Helper,
2143                 _: &handlebars::Handlebars,
2144                 _: &handlebars::Context,
2145                 _: &mut handlebars::RenderContext,
2146                 out: &mut dyn handlebars::Output|
2147                 -> handlebars::HelperResult {
2148                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
2149                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
2150                    Ok(())
2151                },
2152            ),
2153        );
2154        let template = include_str!("templates/k6_crud_flow.hbs");
2155
2156        let custom_headers = self.parse_headers()?;
2157
2158        // Load parameter overrides if provided (for body configurations)
2159        let param_overrides = if let Some(params_file) = &self.params_file {
2160            TerminalReporter::print_progress("Loading parameter overrides...");
2161            let overrides = ParameterOverrides::from_file(params_file)?;
2162            TerminalReporter::print_success(&format!(
2163                "Loaded parameter overrides ({} operation-specific, {} defaults)",
2164                overrides.operations.len(),
2165                if overrides.defaults.is_empty() { 0 } else { 1 }
2166            ));
2167            Some(overrides)
2168        } else {
2169            None
2170        };
2171
2172        // Generate stages from scenario
2173        let duration_secs = Self::parse_duration(&self.duration)?;
2174        let scenario =
2175            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2176        let stages = scenario.generate_stages(duration_secs, self.vus);
2177
2178        // Resolve base path (CLI option takes priority over spec's servers URL)
2179        let api_base_path = self.resolve_base_path(parser);
2180        if let Some(ref bp) = api_base_path {
2181            TerminalReporter::print_progress(&format!("Using base path: {}", bp));
2182        }
2183
2184        // Build headers JSON string for the template
2185        let mut all_headers = custom_headers.clone();
2186        if let Some(auth) = &self.auth {
2187            all_headers.insert("Authorization".to_string(), auth.clone());
2188        }
2189        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
2190
2191        // Track all dynamic placeholders across all operations
2192        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
2193
2194        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
2195            // Sanitize flow name for use as JavaScript variable and k6 metric names.
2196            // Use the metric-name sanitizer (caps at 112 chars + hash suffix) so
2197            // deeply nested flow names don't blow past k6's 128-char limit when
2198            // concatenated with `_step{i}_latency`. See issue #79.
2199            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
2200            serde_json::json!({
2201                "name": sanitized_name.clone(),  // Use sanitized name for variable names
2202                "display_name": f.name,          // Keep original for comments/display
2203                "base_path": f.base_path,
2204                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
2205                    // Parse operation to get method and path
2206                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
2207                    let method_raw = if !parts.is_empty() {
2208                        parts[0].to_uppercase()
2209                    } else {
2210                        "GET".to_string()
2211                    };
2212                    let method = if !parts.is_empty() {
2213                        let m = parts[0].to_lowercase();
2214                        // k6 uses 'del' for DELETE
2215                        if m == "delete" { "del".to_string() } else { m }
2216                    } else {
2217                        "get".to_string()
2218                    };
2219                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
2220                    // Prepend API base path if configured
2221                    let path = if let Some(ref bp) = api_base_path {
2222                        format!("{}{}", bp, raw_path)
2223                    } else {
2224                        raw_path.to_string()
2225                    };
2226                    let is_get_or_head = method == "get" || method == "head";
2227                    // POST, PUT, PATCH typically have bodies
2228                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
2229
2230                    // Look up body from params file if available (use raw_path for matching)
2231                    let body_value = if has_body {
2232                        param_overrides.as_ref()
2233                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
2234                            .and_then(|oo| oo.body)
2235                            .unwrap_or_else(|| serde_json::json!({}))
2236                    } else {
2237                        serde_json::json!({})
2238                    };
2239
2240                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
2241                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
2242                    // Note: all_placeholders is captured by the closure but we can't mutate it directly
2243                    // We'll collect placeholders separately below
2244
2245                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
2246                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
2247                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
2248
2249                    serde_json::json!({
2250                        "operation": s.operation,
2251                        "method": method,
2252                        "path": path,
2253                        "extract": s.extract,
2254                        "use_values": s.use_values,
2255                        "use_body": s.use_body,
2256                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
2257                        "inject_attacks": s.inject_attacks,
2258                        "attack_types": s.attack_types,
2259                        "description": s.description,
2260                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
2261                        "is_get_or_head": is_get_or_head,
2262                        "has_body": has_body,
2263                        "body": processed_body.value,
2264                        "body_is_dynamic": body_is_dynamic,
2265                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
2266                    })
2267                }).collect::<Vec<_>>(),
2268            })
2269        }).collect();
2270
2271        // Collect all placeholders from all steps
2272        for flow_data in &flows_data {
2273            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
2274                for step in steps {
2275                    if let Some(placeholders_arr) =
2276                        step.get("_placeholders").and_then(|p| p.as_array())
2277                    {
2278                        for p_str in placeholders_arr {
2279                            if let Some(p_name) = p_str.as_str() {
2280                                // Parse placeholder from debug string
2281                                match p_name {
2282                                    "VU" => {
2283                                        all_placeholders.insert(DynamicPlaceholder::VU);
2284                                    }
2285                                    "Iteration" => {
2286                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
2287                                    }
2288                                    "Timestamp" => {
2289                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
2290                                    }
2291                                    "UUID" => {
2292                                        all_placeholders.insert(DynamicPlaceholder::UUID);
2293                                    }
2294                                    "Random" => {
2295                                        all_placeholders.insert(DynamicPlaceholder::Random);
2296                                    }
2297                                    "Counter" => {
2298                                        all_placeholders.insert(DynamicPlaceholder::Counter);
2299                                    }
2300                                    "Date" => {
2301                                        all_placeholders.insert(DynamicPlaceholder::Date);
2302                                    }
2303                                    "VuIter" => {
2304                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
2305                                    }
2306                                    _ => {}
2307                                }
2308                            }
2309                        }
2310                    }
2311                }
2312            }
2313        }
2314
2315        // Get required imports and globals based on placeholders used
2316        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
2317        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
2318
2319        // Build invalid data config if error injection is enabled
2320        let invalid_data_config = self.build_invalid_data_config();
2321        let error_injection_enabled = invalid_data_config.is_some();
2322        let error_rate = self.error_rate.unwrap_or(0.0);
2323        let error_types: Vec<String> = invalid_data_config
2324            .as_ref()
2325            .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
2326            .unwrap_or_default();
2327
2328        if error_injection_enabled {
2329            TerminalReporter::print_progress(&format!(
2330                "Error injection enabled ({}% rate)",
2331                (error_rate * 100.0) as u32
2332            ));
2333        }
2334
2335        // Check if security testing is enabled
2336        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
2337
2338        let data = serde_json::json!({
2339            "base_url": self.target,
2340            "flows": flows_data,
2341            "extract_fields": config.default_extract_fields,
2342            "duration_secs": duration_secs,
2343            "max_vus": self.vus,
2344            "auth_header": self.auth,
2345            "custom_headers": custom_headers,
2346            "skip_tls_verify": self.skip_tls_verify,
2347            // Add missing template fields
2348            "stages": stages.iter().map(|s| serde_json::json!({
2349                "duration": s.duration,
2350                "target": s.target,
2351            })).collect::<Vec<_>>(),
2352            "threshold_percentile": self.threshold_percentile,
2353            "threshold_ms": self.threshold_ms,
2354            "max_error_rate": self.max_error_rate,
2355            "headers": headers_json,
2356            "dynamic_imports": required_imports,
2357            "dynamic_globals": required_globals,
2358            "extracted_values_output_path": self
2359                .output
2360                .join("crud_flow_extracted_values.json")
2361                .to_string_lossy(),
2362            // Error injection settings
2363            "error_injection_enabled": error_injection_enabled,
2364            "error_rate": error_rate,
2365            "error_types": error_types,
2366            // Security testing settings
2367            "security_testing_enabled": security_testing_enabled,
2368            "has_custom_headers": !custom_headers.is_empty(),
2369        });
2370
2371        let mut script = handlebars
2372            .render_template(template, &data)
2373            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2374
2375        // Enhance script with security testing support if enabled
2376        if security_testing_enabled {
2377            script = self.generate_enhanced_script(&script)?;
2378        }
2379
2380        // Validate the generated CRUD flow script
2381        TerminalReporter::print_progress("Validating CRUD flow script...");
2382        let validation_errors = K6ScriptGenerator::validate_script(&script);
2383        if !validation_errors.is_empty() {
2384            TerminalReporter::print_error("CRUD flow script validation failed");
2385            for error in &validation_errors {
2386                eprintln!("  {}", error);
2387            }
2388            return Err(BenchError::Other(format!(
2389                "CRUD flow script validation failed with {} error(s)",
2390                validation_errors.len()
2391            )));
2392        }
2393
2394        TerminalReporter::print_success("CRUD flow script generated");
2395
2396        // Write and execute script
2397        let script_path = if let Some(output) = &self.script_output {
2398            output.clone()
2399        } else {
2400            self.output.join("k6-crud-flow-script.js")
2401        };
2402
2403        if let Some(parent) = script_path.parent() {
2404            std::fs::create_dir_all(parent)?;
2405        }
2406        std::fs::write(&script_path, &script)?;
2407        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2408
2409        if self.generate_only {
2410            println!("\nScript generated successfully. Run it with:");
2411            println!("  k6 run {}", script_path.display());
2412            return Ok(());
2413        }
2414
2415        // Execute k6
2416        TerminalReporter::print_progress("Executing CRUD flow test...");
2417        let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2418        std::fs::create_dir_all(&self.output)?;
2419
2420        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2421
2422        let duration_secs = Self::parse_duration(&self.duration)?;
2423        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2424
2425        Ok(())
2426    }
2427
2428    /// Execute OpenAPI 3.0.0 conformance testing mode
2429    async fn execute_conformance_test(&self) -> Result<()> {
2430        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2431        use crate::conformance::report::ConformanceReport;
2432        use crate::conformance::spec::ConformanceFeature;
2433
2434        TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2435
2436        // Conformance testing is a functional correctness check (1 VU, 1 iteration).
2437        // --vus and -d flags are always ignored in this mode.
2438        TerminalReporter::print_progress(
2439            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2440        );
2441
2442        // Parse category filter
2443        let categories = self.conformance_categories.as_ref().map(|cats_str| {
2444            cats_str
2445                .split(',')
2446                .filter_map(|s| {
2447                    let trimmed = s.trim();
2448                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2449                        Some(canonical.to_string())
2450                    } else {
2451                        TerminalReporter::print_warning(&format!(
2452                            "Unknown conformance category: '{}'. Valid categories: {}",
2453                            trimmed,
2454                            ConformanceFeature::cli_category_names()
2455                                .iter()
2456                                .map(|(cli, _)| *cli)
2457                                .collect::<Vec<_>>()
2458                                .join(", ")
2459                        ));
2460                        None
2461                    }
2462                })
2463                .collect::<Vec<String>>()
2464        });
2465
2466        // Parse custom headers from "Key: Value" format
2467        let custom_headers: Vec<(String, String)> = self
2468            .conformance_headers
2469            .iter()
2470            .filter_map(|h| {
2471                let (name, value) = h.split_once(':')?;
2472                Some((name.trim().to_string(), value.trim().to_string()))
2473            })
2474            .collect();
2475
2476        if !custom_headers.is_empty() {
2477            TerminalReporter::print_progress(&format!(
2478                "Using {} custom header(s) for authentication",
2479                custom_headers.len()
2480            ));
2481        }
2482
2483        if self.conformance_delay_ms > 0 {
2484            TerminalReporter::print_progress(&format!(
2485                "Using {}ms delay between conformance requests",
2486                self.conformance_delay_ms
2487            ));
2488        }
2489
2490        // Ensure output dir exists so canonicalize works for the report path
2491        std::fs::create_dir_all(&self.output)?;
2492
2493        let config = ConformanceConfig {
2494            target_url: self.target.clone(),
2495            api_key: self.conformance_api_key.clone(),
2496            basic_auth: self.conformance_basic_auth.clone(),
2497            skip_tls_verify: self.skip_tls_verify,
2498            categories,
2499            base_path: self.base_path.clone(),
2500            custom_headers,
2501            output_dir: Some(self.output.clone()),
2502            all_operations: self.conformance_all_operations,
2503            custom_checks_file: self.conformance_custom.clone(),
2504            request_delay_ms: self.conformance_delay_ms,
2505            custom_filter: self.conformance_custom_filter.clone(),
2506            export_requests: self.export_requests,
2507            validate_requests: self.validate_requests,
2508        };
2509
2510        // Branch: spec-driven mode vs reference mode
2511        // Annotate operations if spec is provided (used by both native and k6 paths)
2512        // Round 18.1 — resolve the spec's base path so the self-test
2513        // path can prepend it to every URL. Pre-fix, self-test
2514        // ignored `--base-path /api` and hit the bare spec path,
2515        // returning 404 for every request on specs whose server is
2516        // proxied behind a base prefix.
2517        let mut resolved_base_path: Option<String> = None;
2518        let annotated_ops = if !self.spec.is_empty() {
2519            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2520            let parser = SpecParser::from_file(&self.spec[0]).await?;
2521            resolved_base_path = self.resolve_base_path(&parser);
2522
2523            // Issue #79 round 12 — Srikanth ran `--conformance --operations "GET,POST"`
2524            // and saw DELETE/PATCH exercised anyway. Conformance silently ignored
2525            // the filter. Apply it (and `--exclude-operations`) the same way the
2526            // regular bench path does so users can scope the run.
2527            let mut operations = if let Some(filter) = &self.operations {
2528                parser.filter_operations(filter)?
2529            } else {
2530                parser.get_operations()
2531            };
2532            if let Some(exclude) = &self.exclude_operations {
2533                let before_count = operations.len();
2534                operations = parser.exclude_operations(operations, exclude)?;
2535                let excluded_count = before_count - operations.len();
2536                if excluded_count > 0 {
2537                    TerminalReporter::print_progress(&format!(
2538                        "Excluded {} operations matching '{}'",
2539                        excluded_count, exclude
2540                    ));
2541                }
2542            }
2543
2544            let annotated =
2545                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2546                    &operations,
2547                    parser.spec(),
2548                );
2549            TerminalReporter::print_success(&format!(
2550                "Analyzed {} operations, found {} feature annotations",
2551                operations.len(),
2552                annotated.iter().map(|a| a.features.len()).sum::<usize>()
2553            ));
2554            Some(annotated)
2555        } else {
2556            None
2557        };
2558
2559        // Issue #79 round 13 (4) — `--conformance-self-test` replaces
2560        // the standard conformance run with a positive + per-category
2561        // negative driver that verifies the server actually rejects
2562        // bad requests with 4xx. Wires the spec-annotated operations
2563        // through `conformance::self_test::run_self_test` and prints
2564        // the resulting pass/fail matrix.
2565        if self.conformance_self_test {
2566            let Some(ops) = annotated_ops else {
2567                TerminalReporter::print_error(
2568                    "--conformance-self-test requires --spec; no operations to test",
2569                );
2570                return Ok(());
2571            };
2572            let cfg = crate::conformance::self_test::SelfTestConfig {
2573                target_url: self.target.clone(),
2574                skip_tls_verify: self.skip_tls_verify,
2575                timeout: std::time::Duration::from_secs(30),
2576                // `custom_headers` was already moved into the
2577                // `ConformanceConfig` above; re-derive from `self` so
2578                // we don't borrow it twice.
2579                extra_headers: self
2580                    .conformance_headers
2581                    .iter()
2582                    .filter_map(|h| {
2583                        let (n, v) = h.split_once(':')?;
2584                        Some((n.trim().to_string(), v.trim().to_string()))
2585                    })
2586                    .collect(),
2587                delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
2588                // Round 18.1 — honour `--base-path` (or the spec's
2589                // own first server prefix) so a deployment served
2590                // under a path-prefix doesn't 404 every positive.
2591                base_path: resolved_base_path.clone(),
2592                // Round 18.5 — GEODB multi-source-IP. Parse CLI IP
2593                // lists (malformed entries log a warning and are
2594                // dropped). Empty lists keep pre-18.5 behaviour.
2595                source_ips: parse_ip_list(&self.source_ips, "source-ip"),
2596                geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
2597                geo_source_headers: if self.geo_source_headers.is_empty() {
2598                    crate::conformance::self_test::default_geo_source_headers()
2599                } else {
2600                    self.geo_source_headers.clone()
2601                },
2602                // Round 23 (c-iii) — opt-in request/response capture.
2603                // Constructed here so the sink Arc outlives the run and
2604                // we can drain it for the JSONL write below.
2605                capture: if self.conformance_self_test_capture
2606                    || self.validate_response_schemas
2607                    || self.validate_requests
2608                {
2609                    // Schema validation reads the captured response
2610                    // body, so opt the user into capture implicitly
2611                    // when they ask for validation. The on-disk
2612                    // JSONL/HTML files only get written if the user
2613                    // also passed `--conformance-self-test-capture`.
2614                    // Round 56 (#79) — `--validate-requests` reads the
2615                    // captured *request* the same way, so enable capture
2616                    // for it too. Without this, `--validate-requests` on
2617                    // a self-test run had no in-memory requests to walk
2618                    // and silently wrote no violations file.
2619                    Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
2620                } else {
2621                    None
2622                },
2623                validate_response_schemas: self.validate_response_schemas,
2624                // Round 33 (#823) — basename of the spec, stamped on
2625                // every capture entry so the per-endpoint summary can
2626                // attribute rows back to the right spec on multi-spec
2627                // runs. Falls back to the full path string if the
2628                // basename can't be derived.
2629                spec_label: self.spec.first().map(|p| {
2630                    p.file_name()
2631                        .map(|s| s.to_string_lossy().into_owned())
2632                        .unwrap_or_else(|| p.to_string_lossy().into_owned())
2633                }),
2634                // Round 47 (#79) — always allocate the network-events
2635                // sink for self-test so the file is always written
2636                // (empty array when nothing failed — the cleanest
2637                // possible signal that connectivity stayed up). Caller
2638                // pays one Arc clone per probe, which is in the noise
2639                // next to the HTTP round-trip.
2640                network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
2641                current_iteration: 1,
2642            };
2643            let capture_sink = cfg.capture.clone();
2644            let network_events_sink = cfg.network_events.clone();
2645            TerminalReporter::print_progress(&format!(
2646                "Self-test mode: driving {} operations with positive + per-category negative cases",
2647                ops.len()
2648            ));
2649            // Round 47 (#79) — repeat the matrix per --conformance-
2650            // self-test-iterations / --conformance-self-test-duration.
2651            // Duration wins when both are set; iterations becomes the
2652            // floor so the matrix always runs at least the configured
2653            // number of times. Reports from each iteration are merged
2654            // by the per-category counter sum on the report itself.
2655            let target_iterations = self.conformance_self_test_iterations.max(1);
2656            let duration_budget = self
2657                .conformance_self_test_duration
2658                .as_ref()
2659                .map(|s| Self::parse_duration(s))
2660                .transpose()?
2661                .map(std::time::Duration::from_secs);
2662            let start = std::time::Instant::now();
2663            // Round 49 (#79) — Srikanth on 0.3.193: a 5m budget ran
2664            // 5:46 because the loop only checked the deadline AFTER a
2665            // full iteration completed. Pass the absolute deadline
2666            // into `run_self_test_with_deadline` so the runner can
2667            // break out mid-iteration the moment the budget elapses.
2668            // Iterations bound stays inclusive (so a duration-only run
2669            // doesn't loop forever on a fast spec) but stops EARLY when
2670            // the deadline hits first.
2671            let deadline = duration_budget.map(|d| start + d);
2672            // Round 49 — stamp `current_iteration` on cfg before each
2673            // pass so CaseCapture's `iteration` field carries the
2674            // loop counter (1-indexed).
2675            let mut cfg = cfg;
2676            cfg.current_iteration = 1;
2677            let mut report =
2678                crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
2679                    .await
2680                    .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2681            let mut iter_done: u32 = 1;
2682            loop {
2683                let by_iter = iter_done >= target_iterations;
2684                let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
2685                if by_iter && by_dur {
2686                    break;
2687                }
2688                cfg.current_iteration = iter_done.saturating_add(1);
2689                let next = crate::conformance::self_test::run_self_test_with_deadline(
2690                    &ops, &cfg, deadline,
2691                )
2692                .await
2693                .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2694                report.merge_iteration(next);
2695                iter_done = iter_done.saturating_add(1);
2696            }
2697            if iter_done > 1 {
2698                TerminalReporter::print_progress(&format!(
2699                    "Self-test repeated {} iteration(s) ({:.1?} elapsed)",
2700                    iter_done,
2701                    start.elapsed(),
2702                ));
2703            }
2704            // Round 23 (c-iii) — drain the capture sink into a JSONL
2705            // file next to the JSON/HTML report. One CaseCapture per
2706            // line so the file is grep-able / streamable. Round 24
2707            // (d) — also emit a self-contained HTML viewer at
2708            // `conformance-self-test-requests.html` for users who
2709            // want to browse the capture without piping through `jq`.
2710            // Round 32 (#79 / Srikanth) — derive the per-endpoint
2711            // traffic summary from the same in-memory capture sink so
2712            // we don't re-parse the JSONL from disk later.
2713            let per_endpoint_summary: Vec<
2714                crate::conformance::per_endpoint_summary::PerEndpointSummary,
2715            >;
2716            if let Some(sink) = capture_sink {
2717                if let Ok(guard) = sink.lock() {
2718                    let jsonl_path = self.output.join("conformance-self-test-requests.jsonl");
2719                    let mut lines = String::with_capacity(guard.len() * 256);
2720                    for entry in guard.iter() {
2721                        if let Ok(line) = serde_json::to_string(entry) {
2722                            lines.push_str(&line);
2723                            lines.push('\n');
2724                        }
2725                    }
2726                    let _ = std::fs::write(&jsonl_path, lines);
2727                    let html_path = self.output.join("conformance-self-test-requests.html");
2728                    let html =
2729                        crate::conformance::capture_html::render_capture_html(guard.as_slice());
2730                    let _ = std::fs::write(&html_path, html);
2731
2732                    // Round 32 — per-endpoint summary derived once from
2733                    // the same slice. Written as a JSON sidecar for
2734                    // automation and spliced into the HTML report below.
2735                    per_endpoint_summary =
2736                        crate::conformance::per_endpoint_summary::build_summary(guard.as_slice());
2737                    let summary_path = self.output.join("conformance-per-endpoint.json");
2738                    if let Ok(json) = serde_json::to_string_pretty(&per_endpoint_summary) {
2739                        let _ = std::fs::write(&summary_path, json);
2740                        TerminalReporter::print_progress(&format!(
2741                            "Self-test request/response capture written to {} ({} entries) + {} + {}",
2742                            jsonl_path.display(),
2743                            guard.len(),
2744                            html_path.display(),
2745                            summary_path.display(),
2746                        ));
2747                    } else {
2748                        TerminalReporter::print_progress(&format!(
2749                            "Self-test request/response capture written to {} ({} entries) + {}",
2750                            jsonl_path.display(),
2751                            guard.len(),
2752                            html_path.display(),
2753                        ));
2754                    }
2755                } else {
2756                    per_endpoint_summary = Vec::new();
2757                }
2758            } else {
2759                per_endpoint_summary = Vec::new();
2760            }
2761            TerminalReporter::print_progress(&report.render_summary());
2762            // Round 47 (#79) — drain the self-test wire-level
2763            // network-events sink into `conformance-network-events.json`
2764            // so the user has the same grep-able file the native
2765            // executor's r46 path produces. Empty array when nothing
2766            // failed (the cleanest signal that connectivity stayed up
2767            // throughout the run).
2768            if let Some(sink) = network_events_sink {
2769                if let Ok(guard) = sink.lock() {
2770                    let path = self.output.join("conformance-network-events.json");
2771                    if let Ok(json) = serde_json::to_string_pretty(&*guard) {
2772                        let _ = std::fs::write(&path, json);
2773                        if guard.is_empty() {
2774                            TerminalReporter::print_progress(
2775                                "No wire-level network failures during self-test (file written empty)",
2776                            );
2777                        } else {
2778                            TerminalReporter::print_warning(&format!(
2779                                "Recorded {} wire-level network event(s) to {}",
2780                                guard.len(),
2781                                path.display()
2782                            ));
2783                        }
2784                    }
2785                }
2786            }
2787            // Persist the JSON report alongside the regular conformance
2788            // report so it's grep-able next to the buffer dump from the
2789            // admin endpoint.
2790            let json_path = self.output.join("conformance-self-test.json");
2791            if let Ok(json) = serde_json::to_string_pretty(&report) {
2792                let _ = std::fs::write(&json_path, json);
2793                TerminalReporter::print_progress(&format!(
2794                    "Self-test report written to {}",
2795                    json_path.display()
2796                ));
2797            }
2798            // Round 18.1 — surface the "every positive failed with
2799            // the same status" case loudly. Without this, a user
2800            // who forgot `--base-path /api` saw 404 for every
2801            // request, but the per-category negative rollup looked
2802            // all-green (because 404 is in the 4xx range the
2803            // negatives expect). Now the run is correctly called
2804            // out as misconfigured before showing the (meaningless)
2805            // negative results.
2806            if let Some(status) = report.detect_target_misconfiguration() {
2807                let hint = match status {
2808                    404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
2809                    401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
2810                    _ => "",
2811                };
2812                TerminalReporter::print_warning(&format!(
2813                    "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
2814                ));
2815            } else if !report.all_passed() {
2816                TerminalReporter::print_warning(
2817                    "Self-test detected gaps — server let through at least one request that should have been a 4xx",
2818                );
2819            } else {
2820                TerminalReporter::print_success(
2821                    "Self-test passed — all positive cases accepted and all negative cases rejected",
2822                );
2823            }
2824            // Round 17.6 — emit a self-contained HTML report alongside
2825            // the JSON. Groups by category and surfaces the missed-
2826            // negative list directly so a user doesn't need to grep
2827            // through the JSON to find which routes failed which
2828            // checks. Optionally folds in a round-17.4 spec audit
2829            // report if one exists in the same output directory.
2830            let html_path = self.output.join("conformance-report.html");
2831            let audit_path = self.output.join("conformance-spec-audit.json");
2832            let audit_value = std::fs::read_to_string(&audit_path)
2833                .ok()
2834                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
2835            // Round 21.1 — `--report-missed-cap N` lets the user
2836            // override the default 200-row HTML drill-down cap.
2837            // `--report-missed-cap 0` maps to `None` (no cap; show
2838            // everything). The JSON report always has the full set.
2839            let render_opts = crate::conformance::report_html::RenderOptions {
2840                missed_cap: match self.report_missed_cap {
2841                    Some(0) => None,
2842                    Some(n) => Some(n as usize),
2843                    None => Some(200),
2844                },
2845            };
2846            let mut html = crate::conformance::report_html::render_html_with_options(
2847                &report,
2848                audit_value.as_ref(),
2849                &render_opts,
2850            );
2851            // Round 32 (#79 / Srikanth) — splice the per-endpoint
2852            // summary just before the closing `</body>` so it lands at
2853            // the bottom of the report. Empty summary renders as an
2854            // empty string so we don't even introduce an extra newline
2855            // when there were no captures.
2856            let summary_section = crate::conformance::per_endpoint_summary::render_html_section(
2857                &per_endpoint_summary,
2858            );
2859            if !summary_section.is_empty() {
2860                if let Some(idx) = html.rfind("</body>") {
2861                    html.insert_str(idx, &summary_section);
2862                } else {
2863                    html.push_str(&summary_section);
2864                }
2865            }
2866            if std::fs::write(&html_path, html).is_ok() {
2867                TerminalReporter::print_progress(&format!(
2868                    "HTML report written to {}",
2869                    html_path.display()
2870                ));
2871            }
2872
2873            // Round 56 (#79) — Srikanth on 0.3.203: "parameter violations
2874            // are still absent from the logs." Root cause: the SINGLE-target
2875            // self-test returned here without ever walking the emitted
2876            // requests. Only the r49 multi-target self-test path (the
2877            // `--targets-file` workflow) called the validator. Mirror that
2878            // wiring here so a plain single-target self-test also writes
2879            // `conformance-request-violations.json`. The validator reads the
2880            // capture we just drained to the JSONL; my r56 change to
2881            // `request_validator.rs` records each `parameters:*` negative as
2882            // a `parameter_negative_probe` even when the emitted request is
2883            // spec-valid (so the three parameter probes stop being silent).
2884            if self.validate_requests && !self.spec.is_empty() {
2885                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
2886                    &self.spec,
2887                    &self.output,
2888                    self.base_path.as_deref(),
2889                )
2890                .await?;
2891                if n > 0 {
2892                    TerminalReporter::print_warning(&format!(
2893                        "{} emitted request(s) recorded against the spec — see conformance-request-violations.json",
2894                        n
2895                    ));
2896                }
2897            }
2898            return Ok(());
2899        }
2900
2901        // Request validation against OpenAPI spec (if --validate-requests is set)
2902        if self.validate_requests && !self.spec.is_empty() {
2903            TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2904            let violation_count = crate::conformance::request_validator::run_request_validation(
2905                &self.spec,
2906                self.conformance_custom.as_deref(),
2907                self.base_path.as_deref(),
2908                &self.output,
2909            )
2910            .await?;
2911            if violation_count > 0 {
2912                TerminalReporter::print_warning(&format!(
2913                    "{} request validation violation(s) found — see conformance-request-violations.json",
2914                    violation_count
2915                ));
2916            } else {
2917                TerminalReporter::print_success("All requests conform to the OpenAPI spec");
2918            }
2919        }
2920
2921        // If generate-only OR --use-k6, use the k6 script generation path
2922        if self.generate_only || self.use_k6 {
2923            let script = if let Some(annotated) = &annotated_ops {
2924                let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2925                    config,
2926                    annotated.clone(),
2927                );
2928                let op_count = gen.operation_count();
2929                let (script, check_count) = gen.generate()?;
2930                TerminalReporter::print_success(&format!(
2931                    "Conformance: {} operations analyzed, {} unique checks generated",
2932                    op_count, check_count
2933                ));
2934                script
2935            } else {
2936                let generator = ConformanceGenerator::new(config);
2937                generator.generate()?
2938            };
2939
2940            let script_path = self.output.join("k6-conformance.js");
2941            std::fs::write(&script_path, &script).map_err(|e| {
2942                BenchError::Other(format!("Failed to write conformance script: {}", e))
2943            })?;
2944            TerminalReporter::print_success(&format!(
2945                "Conformance script generated: {}",
2946                script_path.display()
2947            ));
2948
2949            if self.generate_only {
2950                println!("\nScript generated. Run with:");
2951                println!("  k6 run {}", script_path.display());
2952                return Ok(());
2953            }
2954
2955            // --use-k6: execute via k6
2956            if !K6Executor::is_k6_installed() {
2957                TerminalReporter::print_error("k6 is not installed");
2958                TerminalReporter::print_warning(
2959                    "Install k6 from: https://k6.io/docs/get-started/installation/",
2960                );
2961                return Err(BenchError::K6NotFound);
2962            }
2963
2964            TerminalReporter::print_progress("Running conformance tests via k6...");
2965            let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2966            executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2967
2968            let report_path = self.output.join("conformance-report.json");
2969            if report_path.exists() {
2970                let report = ConformanceReport::from_file(&report_path)?;
2971                report.print_report_with_options(self.conformance_all_operations);
2972                self.save_conformance_report(&report, &report_path)?;
2973            } else {
2974                TerminalReporter::print_warning(
2975                    "Conformance report not generated (k6 handleSummary may not have run)",
2976                );
2977            }
2978
2979            // Round 44 (#79) — Srikanth on 0.3.188: "Any reason why
2980            // validate-requests in mockforge client are not catching
2981            // all this query param or body params or path params
2982            // violation issues and record in conformance-request-
2983            // failure logs?" The custom-YAML validator only checks
2984            // the YAML shape at config time. Now, when both
2985            // `--validate-requests` and `--export-requests` are set,
2986            // also walk the emitted `conformance-requests.json` and
2987            // validate each actual wire-level request against the
2988            // spec. Violations are appended to
2989            // `conformance-request-violations.json`.
2990            if self.validate_requests && self.export_requests && !self.spec.is_empty() {
2991                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
2992                    &self.spec,
2993                    &self.output,
2994                    self.base_path.as_deref(),
2995                )
2996                .await?;
2997                if n > 0 {
2998                    TerminalReporter::print_warning(&format!(
2999                        "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3000                        n
3001                    ));
3002                }
3003            }
3004
3005            return Ok(());
3006        }
3007
3008        // Default: Native Rust executor (no k6 dependency)
3009        TerminalReporter::print_progress("Running conformance tests (native executor)...");
3010
3011        let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3012
3013        // Round 39 (#79) — when the user passed `--conformance-custom`
3014        // WITHOUT `--spec` and without `--conformance-self-test`, fire
3015        // only the YAML's checks. The built-in 47 reference checks
3016        // (`param:path:string`, etc.) hit `/conformance/...` paths that
3017        // do not exist on a real target, so a custom-only run against
3018        // a remote API produced a flood of irrelevant 404s in the
3019        // request log. Srikanth on 0.3.183: "In the exported request I
3020        // see it is sending request to api/conformance/params/hello
3021        // and some other URLs".
3022        let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3023        executor = if let Some(annotated) = &annotated_ops {
3024            executor.with_spec_driven_checks(annotated)
3025        } else if custom_only {
3026            executor
3027        } else {
3028            executor.with_reference_checks()
3029        };
3030        executor = executor.with_custom_checks()?;
3031
3032        TerminalReporter::print_success(&format!(
3033            "Executing {} conformance checks...",
3034            executor.check_count()
3035        ));
3036
3037        let report = executor.execute().await?;
3038        report.print_report_with_options(self.conformance_all_operations);
3039
3040        // Save failure details to a separate file for easy debugging
3041        let failure_details = report.failure_details();
3042        if !failure_details.is_empty() {
3043            let details_path = self.output.join("conformance-failure-details.json");
3044            if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3045                let _ = std::fs::write(&details_path, json);
3046                TerminalReporter::print_success(&format!(
3047                    "Failure details saved to: {}",
3048                    details_path.display()
3049                ));
3050            }
3051        }
3052
3053        // Save report
3054        let report_path = self.output.join("conformance-report.json");
3055        let report_json = serde_json::to_string_pretty(&report.to_json())
3056            .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3057        std::fs::write(&report_path, &report_json)
3058            .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3059        TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
3060
3061        self.save_conformance_report(&report, &report_path)?;
3062
3063        // Round 45 (#79) — Srikanth on 0.3.189: "I am still not seeing
3064        // any conformance failure logs or conformance-request logs are
3065        // also not capturing any failures info." His command does NOT
3066        // pass `--use-k6`, so the round-44 wiring (which only ran on
3067        // the k6 branch) never fired. Mirror the same retrospective
3068        // pass here: when both `--validate-requests` and
3069        // `--export-requests` are set, walk the native executor's
3070        // freshly-written `conformance-requests.json` and validate
3071        // each emitted request against the spec. Violations are
3072        // appended to `conformance-request-violations.json`.
3073        if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3074            let n =
3075                crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3076                    &self.spec,
3077                    &self.output,
3078                    self.base_path.as_deref(),
3079                )
3080                .await?;
3081            if n > 0 {
3082                TerminalReporter::print_warning(&format!(
3083                    "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3084                    n
3085                ));
3086            }
3087        }
3088
3089        Ok(())
3090    }
3091
3092    /// Save conformance report in the requested format (SARIF or JSON copy)
3093    fn save_conformance_report(
3094        &self,
3095        report: &crate::conformance::report::ConformanceReport,
3096        report_path: &Path,
3097    ) -> Result<()> {
3098        if self.conformance_report_format == "sarif" {
3099            use crate::conformance::sarif::ConformanceSarifReport;
3100            ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
3101            TerminalReporter::print_success(&format!(
3102                "SARIF report saved to: {}",
3103                self.conformance_report.display()
3104            ));
3105        } else if self.conformance_report != *report_path {
3106            std::fs::copy(report_path, &self.conformance_report)?;
3107            TerminalReporter::print_success(&format!(
3108                "Report saved to: {}",
3109                self.conformance_report.display()
3110            ));
3111        }
3112        Ok(())
3113    }
3114
3115    /// Round 48 (#79) — Srikanth on 0.3.192: "I ran following commands
3116    /// to test conformance-sef-test duration, but the test ended
3117    /// immediately" with `--targets-file vs_list1.json`. The multi-
3118    /// target dispatch returned before reaching the round-47 self-test
3119    /// iteration loop. This helper runs the self-test driver against
3120    /// every target listed in `targets_file` honouring the same
3121    /// `--conformance-self-test-iterations` and `--conformance-self-
3122    /// test-duration` knobs the single-target path got. One self-test
3123    /// report file per target plus a `conformance-network-events.json`
3124    /// per target so a user can attribute wire failures back to the
3125    /// target they happened against.
3126    async fn execute_multi_target_self_test(&self, targets_file: &Path) -> Result<()> {
3127        use crate::conformance::self_test::SelfTestConfig;
3128
3129        TerminalReporter::print_progress("Multi-target conformance self-test mode");
3130        let targets = parse_targets_file(targets_file)?;
3131        if targets.is_empty() {
3132            return Err(BenchError::Other("No targets found in file".to_string()));
3133        }
3134        TerminalReporter::print_success(&format!("Loaded {} target(s)", targets.len()));
3135
3136        // Spec is shared across targets — load once.
3137        let annotated_ops = if !self.spec.is_empty() {
3138            let parser = SpecParser::from_file(&self.spec[0]).await?;
3139            let operations = parser.get_operations();
3140            Some(
3141                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3142                    &operations,
3143                    parser.spec(),
3144                ),
3145            )
3146        } else {
3147            return Err(BenchError::Other("--conformance-self-test requires --spec".to_string()));
3148        };
3149        let Some(ops) = annotated_ops else {
3150            unreachable!()
3151        };
3152
3153        std::fs::create_dir_all(&self.output)?;
3154        let resolved_base_path = self.base_path.clone();
3155        let target_iterations = self.conformance_self_test_iterations.max(1);
3156        let duration_budget = self
3157            .conformance_self_test_duration
3158            .as_ref()
3159            .map(|s| Self::parse_duration(s))
3160            .transpose()?
3161            .map(std::time::Duration::from_secs);
3162
3163        for (idx, target) in targets.iter().enumerate() {
3164            let target_dir = self.output.join(format!("target_{}", idx));
3165            std::fs::create_dir_all(&target_dir)?;
3166            TerminalReporter::print_progress(&format!(
3167                "[target {}/{}] {}",
3168                idx + 1,
3169                targets.len(),
3170                target.url
3171            ));
3172
3173            let merged_headers: Vec<(String, String)> = self
3174                .conformance_headers
3175                .iter()
3176                .filter_map(|h| {
3177                    let (n, v) = h.split_once(':')?;
3178                    Some((n.trim().to_string(), v.trim().to_string()))
3179                })
3180                .collect();
3181
3182            let cfg = SelfTestConfig {
3183                target_url: target.url.clone(),
3184                skip_tls_verify: self.skip_tls_verify,
3185                timeout: std::time::Duration::from_secs(30),
3186                extra_headers: merged_headers,
3187                delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
3188                base_path: resolved_base_path.clone(),
3189                source_ips: parse_ip_list(&self.source_ips, "source-ip"),
3190                geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
3191                geo_source_headers: if self.geo_source_headers.is_empty() {
3192                    crate::conformance::self_test::default_geo_source_headers()
3193                } else {
3194                    self.geo_source_headers.clone()
3195                },
3196                capture: if self.conformance_self_test_capture
3197                    || self.validate_response_schemas
3198                    || self.validate_requests
3199                {
3200                    // Round 56 (#79) — mirror the single-target path: the
3201                    // per-target request validator needs the captured
3202                    // requests, so `--validate-requests` implies capture.
3203                    Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
3204                } else {
3205                    None
3206                },
3207                validate_response_schemas: self.validate_response_schemas,
3208                spec_label: self.spec.first().map(|p| {
3209                    p.file_name()
3210                        .map(|s| s.to_string_lossy().into_owned())
3211                        .unwrap_or_else(|| p.to_string_lossy().into_owned())
3212                }),
3213                network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
3214                current_iteration: 1,
3215            };
3216            let capture_sink = cfg.capture.clone();
3217            let network_events_sink = cfg.network_events.clone();
3218
3219            let start = std::time::Instant::now();
3220            // Round 49 — pass an absolute deadline down so the loop
3221            // can break out mid-iteration once the budget elapses
3222            // instead of overshooting by a full pass.
3223            let deadline = duration_budget.map(|d| start + d);
3224            // Round 49 — stamp `current_iteration` on cfg before each
3225            // pass so CaseCapture's `iteration` field carries the
3226            // loop counter (1-indexed).
3227            let mut cfg = cfg;
3228            cfg.current_iteration = 1;
3229            let mut report =
3230                crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
3231                    .await
3232                    .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3233            let mut iter_done: u32 = 1;
3234            loop {
3235                let by_iter = iter_done >= target_iterations;
3236                let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
3237                if by_iter && by_dur {
3238                    break;
3239                }
3240                cfg.current_iteration = iter_done.saturating_add(1);
3241                let next = crate::conformance::self_test::run_self_test_with_deadline(
3242                    &ops, &cfg, deadline,
3243                )
3244                .await
3245                .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3246                report.merge_iteration(next);
3247                iter_done = iter_done.saturating_add(1);
3248            }
3249            if iter_done > 1 {
3250                TerminalReporter::print_progress(&format!(
3251                    "  ran {} iteration(s) in {:.1?}",
3252                    iter_done,
3253                    start.elapsed(),
3254                ));
3255            }
3256
3257            // Drain the per-target sinks.
3258            if let Some(sink) = capture_sink {
3259                if let Ok(guard) = sink.lock() {
3260                    let jsonl = target_dir.join("conformance-self-test-requests.jsonl");
3261                    let mut lines = String::with_capacity(guard.len() * 256);
3262                    for entry in guard.iter() {
3263                        if let Ok(line) = serde_json::to_string(entry) {
3264                            lines.push_str(&line);
3265                            lines.push('\n');
3266                        }
3267                    }
3268                    let _ = std::fs::write(&jsonl, lines);
3269                }
3270            }
3271            if let Some(sink) = network_events_sink {
3272                if let Ok(guard) = sink.lock() {
3273                    let path = target_dir.join("conformance-network-events.json");
3274                    if let Ok(json) = serde_json::to_string_pretty(&*guard) {
3275                        let _ = std::fs::write(&path, json);
3276                        if !guard.is_empty() {
3277                            TerminalReporter::print_warning(&format!(
3278                                "  recorded {} wire-level network event(s)",
3279                                guard.len()
3280                            ));
3281                        }
3282                    }
3283                }
3284            }
3285
3286            let json_path = target_dir.join("conformance-self-test.json");
3287            if let Ok(json) = serde_json::to_string_pretty(&report) {
3288                let _ = std::fs::write(&json_path, json);
3289            }
3290            TerminalReporter::print_progress(&report.render_summary());
3291
3292            // Round 49 (#79) — Srikanth on 0.3.193: "I am not seeing
3293            // any violation requests logs when running [self-test
3294            // + --targets-file]". `validate_emitted_requests` was
3295            // only wired into the bench-export path; self-test
3296            // writes its captures to `conformance-self-test-
3297            // requests.jsonl` instead. The validator now reads that
3298            // file too (see the JSONL branch in request_validator.rs),
3299            // so we just need to invoke it here per-target.
3300            if self.validate_requests {
3301                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3302                    &self.spec,
3303                    &target_dir,
3304                    self.base_path.as_deref(),
3305                )
3306                .await?;
3307                if n > 0 {
3308                    TerminalReporter::print_warning(&format!(
3309                        "  {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3310                        n,
3311                        target_dir.display(),
3312                    ));
3313                }
3314            }
3315        }
3316
3317        Ok(())
3318    }
3319
3320    /// Execute conformance tests against multiple targets from a targets file.
3321    ///
3322    /// Uses the native `NativeConformanceExecutor` (no k6 dependency). Targets are
3323    /// tested sequentially to avoid overwhelming them, and per-target headers from
3324    /// the targets file are merged with the base `--conformance-header` headers.
3325    async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
3326        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
3327        use crate::conformance::report::ConformanceReport;
3328        use crate::conformance::spec::ConformanceFeature;
3329
3330        TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
3331
3332        // Parse targets file
3333        TerminalReporter::print_progress("Parsing targets file...");
3334        let targets = parse_targets_file(targets_file)?;
3335        let num_targets = targets.len();
3336        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
3337
3338        if targets.is_empty() {
3339            return Err(BenchError::Other("No targets found in file".to_string()));
3340        }
3341
3342        TerminalReporter::print_progress(
3343            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
3344        );
3345
3346        // Parse category filter (shared across all targets)
3347        let categories = self.conformance_categories.as_ref().map(|cats_str| {
3348            cats_str
3349                .split(',')
3350                .filter_map(|s| {
3351                    let trimmed = s.trim();
3352                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
3353                        Some(canonical.to_string())
3354                    } else {
3355                        TerminalReporter::print_warning(&format!(
3356                            "Unknown conformance category: '{}'. Valid categories: {}",
3357                            trimmed,
3358                            ConformanceFeature::cli_category_names()
3359                                .iter()
3360                                .map(|(cli, _)| *cli)
3361                                .collect::<Vec<_>>()
3362                                .join(", ")
3363                        ));
3364                        None
3365                    }
3366                })
3367                .collect::<Vec<String>>()
3368        });
3369
3370        // Parse base custom headers from --conformance-header flags
3371        let base_custom_headers: Vec<(String, String)> = self
3372            .conformance_headers
3373            .iter()
3374            .filter_map(|h| {
3375                let (name, value) = h.split_once(':')?;
3376                Some((name.trim().to_string(), value.trim().to_string()))
3377            })
3378            .collect();
3379
3380        if !base_custom_headers.is_empty() {
3381            TerminalReporter::print_progress(&format!(
3382                "Using {} base custom header(s) for authentication",
3383                base_custom_headers.len()
3384            ));
3385        }
3386
3387        // Load spec once if provided (shared across all targets)
3388        let annotated_ops = if !self.spec.is_empty() {
3389            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
3390            let parser = SpecParser::from_file(&self.spec[0]).await?;
3391            let operations = parser.get_operations();
3392            let annotated =
3393                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3394                    &operations,
3395                    parser.spec(),
3396                );
3397            TerminalReporter::print_success(&format!(
3398                "Analyzed {} operations, found {} feature annotations",
3399                operations.len(),
3400                annotated.iter().map(|a| a.features.len()).sum::<usize>()
3401            ));
3402            Some(annotated)
3403        } else {
3404            None
3405        };
3406
3407        // Ensure output dir exists
3408        std::fs::create_dir_all(&self.output)?;
3409
3410        // Collect per-target results for the summary
3411        struct TargetResult {
3412            url: String,
3413            passed: usize,
3414            failed: usize,
3415            elapsed: std::time::Duration,
3416            report_json: serde_json::Value,
3417            owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
3418        }
3419
3420        let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
3421        let total_start = std::time::Instant::now();
3422
3423        for (idx, target) in targets.iter().enumerate() {
3424            tracing::info!(
3425                "Running conformance tests against target {}/{}: {}",
3426                idx + 1,
3427                num_targets,
3428                target.url
3429            );
3430            TerminalReporter::print_progress(&format!(
3431                "\n--- Target {}/{}: {} ---",
3432                idx + 1,
3433                num_targets,
3434                target.url
3435            ));
3436
3437            // Merge base headers with per-target headers
3438            let mut merged_headers = base_custom_headers.clone();
3439            if let Some(ref target_headers) = target.headers {
3440                for (name, value) in target_headers {
3441                    // Per-target headers override base headers with the same name
3442                    if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
3443                        existing.1 = value.clone();
3444                    } else {
3445                        merged_headers.push((name.clone(), value.clone()));
3446                    }
3447                }
3448            }
3449            // Add auth header if present on target
3450            if let Some(ref auth) = target.auth {
3451                if let Some(existing) =
3452                    merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
3453                {
3454                    existing.1 = auth.clone();
3455                } else {
3456                    merged_headers.push(("Authorization".to_string(), auth.clone()));
3457                }
3458            }
3459
3460            // Per-target output dir (used by both native and k6 paths).
3461            // Created before the config so we can point the k6 script's
3462            // handleSummary at the per-target directory rather than the shared
3463            // parent output dir (otherwise every target would overwrite the
3464            // same conformance-report.json).
3465            let target_dir = self.output.join(format!("target_{}", idx));
3466            std::fs::create_dir_all(&target_dir)?;
3467
3468            let config = ConformanceConfig {
3469                target_url: target.url.clone(),
3470                api_key: self.conformance_api_key.clone(),
3471                basic_auth: self.conformance_basic_auth.clone(),
3472                skip_tls_verify: self.skip_tls_verify,
3473                categories: categories.clone(),
3474                base_path: self.base_path.clone(),
3475                custom_headers: merged_headers,
3476                output_dir: Some(target_dir.clone()),
3477                all_operations: self.conformance_all_operations,
3478                custom_checks_file: self.conformance_custom.clone(),
3479                request_delay_ms: self.conformance_delay_ms,
3480                custom_filter: self.conformance_custom_filter.clone(),
3481                export_requests: self.export_requests,
3482                validate_requests: self.validate_requests,
3483            };
3484
3485            let target_start = std::time::Instant::now();
3486            let report = if self.use_k6 {
3487                if !K6Executor::is_k6_installed() {
3488                    TerminalReporter::print_error("k6 is not installed");
3489                    TerminalReporter::print_warning(
3490                        "Install k6 from: https://k6.io/docs/get-started/installation/",
3491                    );
3492                    return Err(BenchError::K6NotFound);
3493                }
3494
3495                let script = if let Some(ref annotated) = annotated_ops {
3496                    let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3497                        config.clone(),
3498                        annotated.clone(),
3499                    );
3500                    let (script, _check_count) = gen.generate()?;
3501                    script
3502                } else {
3503                    let generator = ConformanceGenerator::new(config.clone());
3504                    generator.generate()?
3505                };
3506
3507                let script_path = target_dir.join("k6-conformance.js");
3508                std::fs::write(&script_path, &script).map_err(|e| {
3509                    BenchError::Other(format!("Failed to write conformance script: {}", e))
3510                })?;
3511                TerminalReporter::print_success(&format!(
3512                    "Conformance script generated: {}",
3513                    script_path.display()
3514                ));
3515
3516                TerminalReporter::print_progress(&format!(
3517                    "Running conformance tests via k6 against {}...",
3518                    target.url
3519                ));
3520                let k6 = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
3521                // Unique k6 API port per target to avoid collisions.
3522                let api_port = 6565u16.saturating_add(idx as u16);
3523                k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
3524                    .await?;
3525
3526                let report_path = target_dir.join("conformance-report.json");
3527                if report_path.exists() {
3528                    ConformanceReport::from_file(&report_path)?
3529                } else {
3530                    TerminalReporter::print_warning(&format!(
3531                        "Conformance report not generated for target {} (k6 handleSummary may not have run)",
3532                        target.url
3533                    ));
3534                    continue;
3535                }
3536            } else {
3537                let mut executor =
3538                    crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3539
3540                // Round 39 (#79) — see custom_only comment above; same
3541                // logic applied to the multi-target branch.
3542                let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3543                executor = if let Some(ref annotated) = annotated_ops {
3544                    executor.with_spec_driven_checks(annotated)
3545                } else if custom_only {
3546                    executor
3547                } else {
3548                    executor.with_reference_checks()
3549                };
3550                executor = executor.with_custom_checks()?;
3551
3552                TerminalReporter::print_success(&format!(
3553                    "Executing {} conformance checks against {}...",
3554                    executor.check_count(),
3555                    target.url
3556                ));
3557
3558                executor.execute().await?
3559            };
3560            let target_elapsed = target_start.elapsed();
3561
3562            let report_json = report.to_json();
3563
3564            // Extract pass/fail from the summary in the JSON
3565            let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
3566            let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
3567            let total_checks = passed + failed;
3568            let rate = if total_checks == 0 {
3569                0.0
3570            } else {
3571                (passed as f64 / total_checks as f64) * 100.0
3572            };
3573
3574            TerminalReporter::print_success(&format!(
3575                "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
3576                target.url,
3577                passed,
3578                total_checks,
3579                rate,
3580                target_elapsed.as_secs_f64()
3581            ));
3582
3583            // Save per-target report (target_dir created above)
3584            let target_report_path = target_dir.join("conformance-report.json");
3585            let report_str = serde_json::to_string_pretty(&report_json)
3586                .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3587            std::fs::write(&target_report_path, &report_str)
3588                .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3589
3590            // Save failure details if any
3591            let failure_details = report.failure_details();
3592            if !failure_details.is_empty() {
3593                let details_path = target_dir.join("conformance-failure-details.json");
3594                if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3595                    let _ = std::fs::write(&details_path, json);
3596                }
3597            }
3598
3599            // Round 45 (#79) — Srikanth on 0.3.189: `conformance-request-
3600            // violations.json` was never being written in his
3601            // multi-target self-test run. The round-44 wiring sat on
3602            // the single-target branch only. Mirror it here, per
3603            // target_dir, so the multi-target case (his typical
3604            // workflow) actually surfaces wire-level violations.
3605            if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3606                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3607                    &self.spec,
3608                    &target_dir,
3609                    self.base_path.as_deref(),
3610                )
3611                .await?;
3612                if n > 0 {
3613                    TerminalReporter::print_warning(&format!(
3614                        "Target {}: {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3615                        target.url,
3616                        n,
3617                        target_dir.display()
3618                    ));
3619                }
3620            }
3621
3622            // Compute OWASP coverage for this target
3623            let owasp_coverage = report.owasp_coverage_data();
3624
3625            target_results.push(TargetResult {
3626                url: target.url.clone(),
3627                passed,
3628                failed,
3629                elapsed: target_elapsed,
3630                report_json,
3631                owasp_coverage,
3632            });
3633        }
3634
3635        let total_elapsed = total_start.elapsed();
3636
3637        // Print summary table
3638        println!("\n{}", "=".repeat(80));
3639        println!("  Multi-Target Conformance Summary");
3640        println!("{}", "=".repeat(80));
3641        println!(
3642            "  {:<40} {:>8} {:>8} {:>8} {:>8}",
3643            "Target URL", "Passed", "Failed", "Rate", "Time"
3644        );
3645        println!("  {}", "-".repeat(76));
3646
3647        let mut total_passed = 0usize;
3648        let mut total_failed = 0usize;
3649
3650        for result in &target_results {
3651            let total_checks = result.passed + result.failed;
3652            let rate = if total_checks == 0 {
3653                0.0
3654            } else {
3655                (result.passed as f64 / total_checks as f64) * 100.0
3656            };
3657
3658            // Truncate long URLs for display
3659            let display_url = if result.url.len() > 38 {
3660                format!("{}...", &result.url[..35])
3661            } else {
3662                result.url.clone()
3663            };
3664
3665            println!(
3666                "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3667                display_url,
3668                result.passed,
3669                result.failed,
3670                rate,
3671                result.elapsed.as_secs_f64()
3672            );
3673
3674            total_passed += result.passed;
3675            total_failed += result.failed;
3676        }
3677
3678        let grand_total = total_passed + total_failed;
3679        let overall_rate = if grand_total == 0 {
3680            0.0
3681        } else {
3682            (total_passed as f64 / grand_total as f64) * 100.0
3683        };
3684
3685        println!("  {}", "-".repeat(76));
3686        println!(
3687            "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3688            format!("TOTAL ({} targets)", num_targets),
3689            total_passed,
3690            total_failed,
3691            overall_rate,
3692            total_elapsed.as_secs_f64()
3693        );
3694        println!("{}", "=".repeat(80));
3695
3696        // Print per-target OWASP coverage
3697        for result in &target_results {
3698            println!("\n  OWASP API Security Top 10 Coverage for {}:", result.url);
3699            for entry in &result.owasp_coverage {
3700                let status = if !entry.tested {
3701                    "-"
3702                } else if entry.all_passed {
3703                    "pass"
3704                } else {
3705                    "FAIL"
3706                };
3707                let via = if entry.via_categories.is_empty() {
3708                    String::new()
3709                } else {
3710                    format!(" (via {})", entry.via_categories.join(", "))
3711                };
3712                println!("    {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
3713            }
3714        }
3715
3716        // Save combined summary
3717        let per_target_summaries: Vec<serde_json::Value> = target_results
3718            .iter()
3719            .enumerate()
3720            .map(|(idx, r)| {
3721                let total_checks = r.passed + r.failed;
3722                let rate = if total_checks == 0 {
3723                    0.0
3724                } else {
3725                    (r.passed as f64 / total_checks as f64) * 100.0
3726                };
3727                let owasp_json: Vec<serde_json::Value> = r
3728                    .owasp_coverage
3729                    .iter()
3730                    .map(|e| {
3731                        serde_json::json!({
3732                            "id": e.id,
3733                            "name": e.name,
3734                            "tested": e.tested,
3735                            "all_passed": e.all_passed,
3736                            "via_categories": e.via_categories,
3737                        })
3738                    })
3739                    .collect();
3740                serde_json::json!({
3741                    "target_url": r.url,
3742                    "target_index": idx,
3743                    "checks_passed": r.passed,
3744                    "checks_failed": r.failed,
3745                    "total_checks": total_checks,
3746                    "pass_rate": rate,
3747                    "elapsed_seconds": r.elapsed.as_secs_f64(),
3748                    "report": r.report_json,
3749                    "owasp_coverage": owasp_json,
3750                })
3751            })
3752            .collect();
3753
3754        let combined_summary = serde_json::json!({
3755            "total_targets": num_targets,
3756            "total_checks_passed": total_passed,
3757            "total_checks_failed": total_failed,
3758            "overall_pass_rate": overall_rate,
3759            "total_elapsed_seconds": total_elapsed.as_secs_f64(),
3760            "targets": per_target_summaries,
3761        });
3762
3763        let summary_path = self.output.join("multi-target-conformance-summary.json");
3764        let summary_str = serde_json::to_string_pretty(&combined_summary)
3765            .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
3766        std::fs::write(&summary_path, &summary_str)
3767            .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
3768        TerminalReporter::print_success(&format!(
3769            "Combined summary saved to: {}",
3770            summary_path.display()
3771        ));
3772
3773        Ok(())
3774    }
3775
3776    /// Execute OWASP API Security Top 10 testing mode
3777    async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
3778        TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
3779
3780        // Parse custom headers from CLI
3781        let custom_headers = self.parse_headers()?;
3782
3783        // Build OWASP configuration from CLI options
3784        let mut config = OwaspApiConfig::new()
3785            .with_auth_header(&self.owasp_auth_header)
3786            .with_verbose(self.verbose)
3787            .with_insecure(self.skip_tls_verify)
3788            .with_concurrency(self.vus as usize)
3789            .with_iterations(self.owasp_iterations as usize)
3790            .with_base_path(self.base_path.clone())
3791            .with_custom_headers(custom_headers);
3792
3793        // Set valid auth token if provided
3794        if let Some(ref token) = self.owasp_auth_token {
3795            config = config.with_valid_auth_token(token);
3796        }
3797
3798        // Parse categories if provided
3799        if let Some(ref cats_str) = self.owasp_categories {
3800            let categories: Vec<OwaspCategory> = cats_str
3801                .split(',')
3802                .filter_map(|s| {
3803                    let trimmed = s.trim();
3804                    match trimmed.parse::<OwaspCategory>() {
3805                        Ok(cat) => Some(cat),
3806                        Err(e) => {
3807                            TerminalReporter::print_warning(&e);
3808                            None
3809                        }
3810                    }
3811                })
3812                .collect();
3813
3814            if !categories.is_empty() {
3815                config = config.with_categories(categories);
3816            }
3817        }
3818
3819        // Load admin paths from file if provided
3820        if let Some(ref admin_paths_file) = self.owasp_admin_paths {
3821            config.admin_paths_file = Some(admin_paths_file.clone());
3822            if let Err(e) = config.load_admin_paths() {
3823                TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
3824            }
3825        }
3826
3827        // Set ID fields if provided
3828        if let Some(ref id_fields_str) = self.owasp_id_fields {
3829            let id_fields: Vec<String> = id_fields_str
3830                .split(',')
3831                .map(|s| s.trim().to_string())
3832                .filter(|s| !s.is_empty())
3833                .collect();
3834            if !id_fields.is_empty() {
3835                config = config.with_id_fields(id_fields);
3836            }
3837        }
3838
3839        // Set report path and format
3840        if let Some(ref report_path) = self.owasp_report {
3841            config = config.with_report_path(report_path);
3842        }
3843        if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
3844            config = config.with_report_format(format);
3845        }
3846
3847        // Print configuration summary
3848        let categories = config.categories_to_test();
3849        TerminalReporter::print_success(&format!(
3850            "Testing {} OWASP categories: {}",
3851            categories.len(),
3852            categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
3853        ));
3854
3855        if config.valid_auth_token.is_some() {
3856            TerminalReporter::print_progress("Using provided auth token for baseline requests");
3857        }
3858
3859        // Create the OWASP generator
3860        TerminalReporter::print_progress("Generating OWASP security test script...");
3861        let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
3862
3863        // Generate the script
3864        let script = generator.generate()?;
3865        TerminalReporter::print_success("OWASP security test script generated");
3866
3867        // Write script to file
3868        let script_path = if let Some(output) = &self.script_output {
3869            output.clone()
3870        } else {
3871            self.output.join("k6-owasp-security-test.js")
3872        };
3873
3874        if let Some(parent) = script_path.parent() {
3875            std::fs::create_dir_all(parent)?;
3876        }
3877        std::fs::write(&script_path, &script)?;
3878        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
3879
3880        // If generate-only mode, exit here
3881        if self.generate_only {
3882            println!("\nOWASP security test script generated. Run it with:");
3883            println!("  k6 run {}", script_path.display());
3884            return Ok(());
3885        }
3886
3887        // Execute k6
3888        TerminalReporter::print_progress("Executing OWASP security tests...");
3889        let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
3890        std::fs::create_dir_all(&self.output)?;
3891
3892        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3893
3894        let duration_secs = Self::parse_duration(&self.duration)?;
3895        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
3896
3897        println!("\nOWASP security test results saved to: {}", self.output.display());
3898
3899        Ok(())
3900    }
3901}
3902
3903#[cfg(test)]
3904mod tests {
3905    use super::*;
3906    use tempfile::tempdir;
3907
3908    #[test]
3909    fn test_parse_duration() {
3910        assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
3911        assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
3912        assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
3913        assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
3914    }
3915
3916    /// Round 22.4 — `start-end` IPv4 range syntax for non-power-of-2
3917    /// ranges. Srikanth (h): `--source-ip 10.0.0.5-10.0.0.27` for 23
3918    /// hosts without finding a clean prefix.
3919    #[test]
3920    fn parse_ip_list_ipv4_range_inclusive() {
3921        let v = parse_ip_list(&["10.0.0.5-10.0.0.27".into()], "source-ip");
3922        assert_eq!(v.len(), 23);
3923        assert_eq!(v.first().unwrap().to_string(), "10.0.0.5");
3924        assert_eq!(v.last().unwrap().to_string(), "10.0.0.27");
3925    }
3926
3927    /// Round 22.4 — range with start > end is rejected with a warning
3928    /// (returns nothing for that entry rather than wrapping around).
3929    #[test]
3930    fn parse_ip_list_range_rejects_backwards() {
3931        let v = parse_ip_list(&["10.0.0.10-10.0.0.5".into()], "source-ip");
3932        assert!(v.is_empty(), "backwards range should produce no IPs; got {v:?}");
3933    }
3934
3935    /// Round 22.4 — IPv6 ranges are intentionally rejected because
3936    /// `2001:db8::1-2001:db8::5` would ambiguously parse against the
3937    /// address literal's `:` separators. Users use CIDR for IPv6.
3938    #[test]
3939    fn parse_ip_list_rejects_ipv6_range_syntax() {
3940        let v = parse_ip_list(&["2001:db8::1-2001:db8::5".into()], "geo-source-ip");
3941        assert!(v.is_empty(), "IPv6 range should be rejected; got {v:?}");
3942    }
3943
3944    /// Round 22.4 — range cap is the same 256 host limit as CIDR.
3945    #[test]
3946    fn parse_ip_list_range_capped_at_256() {
3947        let v = parse_ip_list(&["10.0.0.0-10.0.5.0".into()], "source-ip");
3948        assert_eq!(v.len(), 256);
3949        assert_eq!(v.first().unwrap().to_string(), "10.0.0.0");
3950    }
3951
3952    /// Round 19 — single IPs and comma-separated lists already
3953    /// worked in 18.5; this regression-locks the parse paths.
3954    #[test]
3955    fn parse_ip_list_plain_and_comma() {
3956        let v = parse_ip_list(&["10.0.0.5".into(), "10.0.0.6,10.0.0.7".into()], "source-ip");
3957        assert_eq!(v.len(), 3);
3958        assert_eq!(v[0].to_string(), "10.0.0.5");
3959        assert_eq!(v[2].to_string(), "10.0.0.7");
3960    }
3961
3962    /// Round 19 — IPv4 CIDR expands to host count up to the cap.
3963    /// `/29` = 8 hosts (well under cap), all 8 enumerated.
3964    #[test]
3965    fn parse_ip_list_ipv4_cidr_29_expands_to_8() {
3966        let v = parse_ip_list(&["10.0.0.0/29".into()], "source-ip");
3967        assert_eq!(v.len(), 8);
3968        assert_eq!(v[0].to_string(), "10.0.0.0");
3969        assert_eq!(v[7].to_string(), "10.0.0.7");
3970    }
3971
3972    /// Round 19 — IPv4 CIDR larger than the cap is truncated, not
3973    /// rejected. Cap is 256; `/8` would be 16M without the guard.
3974    #[test]
3975    fn parse_ip_list_ipv4_cidr_8_capped_at_256() {
3976        let v = parse_ip_list(&["10.0.0.0/8".into()], "source-ip");
3977        assert_eq!(v.len(), 256);
3978        assert_eq!(v[0].to_string(), "10.0.0.0");
3979        assert_eq!(v[255].to_string(), "10.0.0.255");
3980    }
3981
3982    /// Round 19 — IPv6 CIDR also expands. `/126` = 4 hosts.
3983    #[test]
3984    fn parse_ip_list_ipv6_cidr_126_expands_to_4() {
3985        let v = parse_ip_list(&["2001:db8::/126".into()], "geo-source-ip");
3986        assert_eq!(v.len(), 4);
3987        assert!(v[0].is_ipv6());
3988        assert_eq!(v[0].to_string(), "2001:db8::");
3989        assert_eq!(v[3].to_string(), "2001:db8::3");
3990    }
3991
3992    /// Round 19 — mixed IPv4 + IPv6 + CIDR in one call works.
3993    #[test]
3994    fn parse_ip_list_mixed_v4_v6_cidr() {
3995        let v = parse_ip_list(&["10.0.0.0/30,2001:db8::1,203.0.113.42".into()], "geo-source-ip");
3996        assert_eq!(v.len(), 6); // 4 from /30 + 1 + 1
3997        assert!(v.iter().any(|ip| ip.to_string() == "2001:db8::1"));
3998        assert!(v.iter().any(|ip| ip.to_string() == "203.0.113.42"));
3999    }
4000
4001    /// Round 19 — malformed entries log and skip; the run continues
4002    /// with whatever resolved.
4003    #[test]
4004    fn parse_ip_list_skips_malformed() {
4005        let v = parse_ip_list(
4006            &[
4007                "10.0.0.5".into(),
4008                "not-an-ip".into(),
4009                "10.0.0.6".into(),
4010                "/24".into(),
4011                "1.2.3.4/200".into(),
4012            ],
4013            "source-ip",
4014        );
4015        assert_eq!(v.len(), 2);
4016        assert_eq!(v[0].to_string(), "10.0.0.5");
4017        assert_eq!(v[1].to_string(), "10.0.0.6");
4018    }
4019
4020    #[test]
4021    fn test_parse_duration_invalid() {
4022        assert!(BenchCommand::parse_duration("invalid").is_err());
4023        assert!(BenchCommand::parse_duration("30x").is_err());
4024    }
4025
4026    #[test]
4027    fn test_parse_headers() {
4028        let cmd = BenchCommand {
4029            spec: vec![PathBuf::from("test.yaml")],
4030            spec_dir: None,
4031            merge_conflicts: "error".to_string(),
4032            spec_mode: "merge".to_string(),
4033            dependency_config: None,
4034            target: "http://localhost".to_string(),
4035            base_path: None,
4036            duration: "1m".to_string(),
4037            vus: 10,
4038            scenario: "ramp-up".to_string(),
4039            operations: None,
4040            exclude_operations: None,
4041            auth: None,
4042            headers: vec![
4043                "X-API-Key:test123".to_string(),
4044                "X-Client-ID:client456".to_string(),
4045            ],
4046            output: PathBuf::from("output"),
4047            generate_only: false,
4048            script_output: None,
4049            threshold_percentile: "p(95)".to_string(),
4050            threshold_ms: 500,
4051            max_error_rate: 0.05,
4052            verbose: false,
4053            skip_tls_verify: false,
4054            chunked_request_bodies: false,
4055            target_rps: None,
4056            no_keep_alive: false,
4057            targets_file: None,
4058            max_concurrency: None,
4059            results_format: "both".to_string(),
4060            params_file: None,
4061            crud_flow: false,
4062            flow_config: None,
4063            extract_fields: None,
4064            parallel_create: None,
4065            data_file: None,
4066            data_distribution: "unique-per-vu".to_string(),
4067            data_mappings: None,
4068            per_uri_control: false,
4069            error_rate: None,
4070            error_types: None,
4071            security_test: false,
4072            security_payloads: None,
4073            security_categories: None,
4074            security_target_fields: None,
4075            wafbench_dir: None,
4076            wafbench_cycle_all: false,
4077            owasp_api_top10: false,
4078            owasp_categories: None,
4079            owasp_auth_header: "Authorization".to_string(),
4080            owasp_auth_token: None,
4081            owasp_admin_paths: None,
4082            owasp_id_fields: None,
4083            owasp_report: None,
4084            owasp_report_format: "json".to_string(),
4085            owasp_iterations: 1,
4086            conformance: false,
4087            conformance_api_key: None,
4088            conformance_basic_auth: None,
4089            conformance_report: PathBuf::from("conformance-report.json"),
4090            conformance_categories: None,
4091            conformance_report_format: "json".to_string(),
4092            conformance_headers: vec![],
4093            conformance_all_operations: false,
4094            conformance_custom: None,
4095            conformance_delay_ms: 0,
4096            use_k6: false,
4097            conformance_custom_filter: None,
4098            export_requests: false,
4099            validate_requests: false,
4100            conformance_self_test: false,
4101            conformance_self_test_capture: false,
4102            conformance_self_test_iterations: 1,
4103            conformance_self_test_duration: None,
4104            validate_response_schemas: false,
4105            source_ips: Vec::new(),
4106            geo_source_ips: Vec::new(),
4107            geo_source_headers: Vec::new(),
4108            report_missed_cap: None,
4109        };
4110
4111        let headers = cmd.parse_headers().unwrap();
4112        assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
4113        assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
4114    }
4115
4116    #[test]
4117    fn test_parse_header_string_preserves_comma_in_value() {
4118        // #761: with one header per --headers flag, a comma in the value (e.g. a
4119        // Cookie expiry date) is preserved instead of being split into junk pairs.
4120        let inputs = vec![
4121            "Cookie:session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string(),
4122            "X-Trace:1".to_string(),
4123        ];
4124        let headers = parse_header_string(&inputs).unwrap();
4125        assert_eq!(
4126            headers.get("Cookie"),
4127            Some(&"session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string())
4128        );
4129        assert_eq!(headers.get("X-Trace"), Some(&"1".to_string()));
4130    }
4131
4132    #[test]
4133    fn test_get_spec_display_name() {
4134        let cmd = BenchCommand {
4135            spec: vec![PathBuf::from("test.yaml")],
4136            spec_dir: None,
4137            merge_conflicts: "error".to_string(),
4138            spec_mode: "merge".to_string(),
4139            dependency_config: None,
4140            target: "http://localhost".to_string(),
4141            base_path: None,
4142            duration: "1m".to_string(),
4143            vus: 10,
4144            scenario: "ramp-up".to_string(),
4145            operations: None,
4146            exclude_operations: None,
4147            auth: None,
4148            headers: Vec::new(),
4149            output: PathBuf::from("output"),
4150            generate_only: false,
4151            script_output: None,
4152            threshold_percentile: "p(95)".to_string(),
4153            threshold_ms: 500,
4154            max_error_rate: 0.05,
4155            verbose: false,
4156            skip_tls_verify: false,
4157            chunked_request_bodies: false,
4158            target_rps: None,
4159            no_keep_alive: false,
4160            targets_file: None,
4161            max_concurrency: None,
4162            results_format: "both".to_string(),
4163            params_file: None,
4164            crud_flow: false,
4165            flow_config: None,
4166            extract_fields: None,
4167            parallel_create: None,
4168            data_file: None,
4169            data_distribution: "unique-per-vu".to_string(),
4170            data_mappings: None,
4171            per_uri_control: false,
4172            error_rate: None,
4173            error_types: None,
4174            security_test: false,
4175            security_payloads: None,
4176            security_categories: None,
4177            security_target_fields: None,
4178            wafbench_dir: None,
4179            wafbench_cycle_all: false,
4180            owasp_api_top10: false,
4181            owasp_categories: None,
4182            owasp_auth_header: "Authorization".to_string(),
4183            owasp_auth_token: None,
4184            owasp_admin_paths: None,
4185            owasp_id_fields: None,
4186            owasp_report: None,
4187            owasp_report_format: "json".to_string(),
4188            owasp_iterations: 1,
4189            conformance: false,
4190            conformance_api_key: None,
4191            conformance_basic_auth: None,
4192            conformance_report: PathBuf::from("conformance-report.json"),
4193            conformance_categories: None,
4194            conformance_report_format: "json".to_string(),
4195            conformance_headers: vec![],
4196            conformance_all_operations: false,
4197            conformance_custom: None,
4198            conformance_delay_ms: 0,
4199            use_k6: false,
4200            conformance_custom_filter: None,
4201            export_requests: false,
4202            validate_requests: false,
4203            conformance_self_test: false,
4204            conformance_self_test_capture: false,
4205            conformance_self_test_iterations: 1,
4206            conformance_self_test_duration: None,
4207            validate_response_schemas: false,
4208            source_ips: Vec::new(),
4209            geo_source_ips: Vec::new(),
4210            geo_source_headers: Vec::new(),
4211            report_missed_cap: None,
4212        };
4213
4214        assert_eq!(cmd.get_spec_display_name(), "test.yaml");
4215
4216        // Test multiple specs
4217        let cmd_multi = BenchCommand {
4218            spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
4219            spec_dir: None,
4220            merge_conflicts: "error".to_string(),
4221            spec_mode: "merge".to_string(),
4222            dependency_config: None,
4223            target: "http://localhost".to_string(),
4224            base_path: None,
4225            duration: "1m".to_string(),
4226            vus: 10,
4227            scenario: "ramp-up".to_string(),
4228            operations: None,
4229            exclude_operations: None,
4230            auth: None,
4231            headers: Vec::new(),
4232            output: PathBuf::from("output"),
4233            generate_only: false,
4234            script_output: None,
4235            threshold_percentile: "p(95)".to_string(),
4236            threshold_ms: 500,
4237            max_error_rate: 0.05,
4238            verbose: false,
4239            skip_tls_verify: false,
4240            chunked_request_bodies: false,
4241            target_rps: None,
4242            no_keep_alive: false,
4243            targets_file: None,
4244            max_concurrency: None,
4245            results_format: "both".to_string(),
4246            params_file: None,
4247            crud_flow: false,
4248            flow_config: None,
4249            extract_fields: None,
4250            parallel_create: None,
4251            data_file: None,
4252            data_distribution: "unique-per-vu".to_string(),
4253            data_mappings: None,
4254            per_uri_control: false,
4255            error_rate: None,
4256            error_types: None,
4257            security_test: false,
4258            security_payloads: None,
4259            security_categories: None,
4260            security_target_fields: None,
4261            wafbench_dir: None,
4262            wafbench_cycle_all: false,
4263            owasp_api_top10: false,
4264            owasp_categories: None,
4265            owasp_auth_header: "Authorization".to_string(),
4266            owasp_auth_token: None,
4267            owasp_admin_paths: None,
4268            owasp_id_fields: None,
4269            owasp_report: None,
4270            owasp_report_format: "json".to_string(),
4271            owasp_iterations: 1,
4272            conformance: false,
4273            conformance_api_key: None,
4274            conformance_basic_auth: None,
4275            conformance_report: PathBuf::from("conformance-report.json"),
4276            conformance_categories: None,
4277            conformance_report_format: "json".to_string(),
4278            conformance_headers: vec![],
4279            conformance_all_operations: false,
4280            conformance_custom: None,
4281            conformance_delay_ms: 0,
4282            use_k6: false,
4283            conformance_custom_filter: None,
4284            export_requests: false,
4285            validate_requests: false,
4286            conformance_self_test: false,
4287            conformance_self_test_capture: false,
4288            conformance_self_test_iterations: 1,
4289            conformance_self_test_duration: None,
4290            validate_response_schemas: false,
4291            source_ips: Vec::new(),
4292            geo_source_ips: Vec::new(),
4293            geo_source_headers: Vec::new(),
4294            report_missed_cap: None,
4295        };
4296
4297        assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
4298    }
4299
4300    #[test]
4301    fn test_parse_extracted_values_from_output_dir() {
4302        let dir = tempdir().unwrap();
4303        let path = dir.path().join("extracted_values.json");
4304        std::fs::write(
4305            &path,
4306            r#"{
4307  "pool_id": "abc123",
4308  "count": 0,
4309  "enabled": false,
4310  "metadata": { "owner": "team-a" }
4311}"#,
4312        )
4313        .unwrap();
4314
4315        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4316        assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
4317        assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
4318        assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
4319        assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
4320    }
4321
4322    #[test]
4323    fn test_parse_extracted_values_missing_file() {
4324        let dir = tempdir().unwrap();
4325        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4326        assert!(extracted.values.is_empty());
4327    }
4328}