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 || self.validate_response_schemas {
2606                    // Schema validation reads the captured response
2607                    // body, so opt the user into capture implicitly
2608                    // when they ask for validation. The on-disk
2609                    // JSONL/HTML files only get written if the user
2610                    // also passed `--conformance-self-test-capture`.
2611                    Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
2612                } else {
2613                    None
2614                },
2615                validate_response_schemas: self.validate_response_schemas,
2616                // Round 33 (#823) — basename of the spec, stamped on
2617                // every capture entry so the per-endpoint summary can
2618                // attribute rows back to the right spec on multi-spec
2619                // runs. Falls back to the full path string if the
2620                // basename can't be derived.
2621                spec_label: self.spec.first().map(|p| {
2622                    p.file_name()
2623                        .map(|s| s.to_string_lossy().into_owned())
2624                        .unwrap_or_else(|| p.to_string_lossy().into_owned())
2625                }),
2626                // Round 47 (#79) — always allocate the network-events
2627                // sink for self-test so the file is always written
2628                // (empty array when nothing failed — the cleanest
2629                // possible signal that connectivity stayed up). Caller
2630                // pays one Arc clone per probe, which is in the noise
2631                // next to the HTTP round-trip.
2632                network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
2633                current_iteration: 1,
2634            };
2635            let capture_sink = cfg.capture.clone();
2636            let network_events_sink = cfg.network_events.clone();
2637            TerminalReporter::print_progress(&format!(
2638                "Self-test mode: driving {} operations with positive + per-category negative cases",
2639                ops.len()
2640            ));
2641            // Round 47 (#79) — repeat the matrix per --conformance-
2642            // self-test-iterations / --conformance-self-test-duration.
2643            // Duration wins when both are set; iterations becomes the
2644            // floor so the matrix always runs at least the configured
2645            // number of times. Reports from each iteration are merged
2646            // by the per-category counter sum on the report itself.
2647            let target_iterations = self.conformance_self_test_iterations.max(1);
2648            let duration_budget = self
2649                .conformance_self_test_duration
2650                .as_ref()
2651                .map(|s| Self::parse_duration(s))
2652                .transpose()?
2653                .map(std::time::Duration::from_secs);
2654            let start = std::time::Instant::now();
2655            // Round 49 (#79) — Srikanth on 0.3.193: a 5m budget ran
2656            // 5:46 because the loop only checked the deadline AFTER a
2657            // full iteration completed. Pass the absolute deadline
2658            // into `run_self_test_with_deadline` so the runner can
2659            // break out mid-iteration the moment the budget elapses.
2660            // Iterations bound stays inclusive (so a duration-only run
2661            // doesn't loop forever on a fast spec) but stops EARLY when
2662            // the deadline hits first.
2663            let deadline = duration_budget.map(|d| start + d);
2664            // Round 49 — stamp `current_iteration` on cfg before each
2665            // pass so CaseCapture's `iteration` field carries the
2666            // loop counter (1-indexed).
2667            let mut cfg = cfg;
2668            cfg.current_iteration = 1;
2669            let mut report =
2670                crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
2671                    .await
2672                    .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2673            let mut iter_done: u32 = 1;
2674            loop {
2675                let by_iter = iter_done >= target_iterations;
2676                let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
2677                if by_iter && by_dur {
2678                    break;
2679                }
2680                cfg.current_iteration = iter_done.saturating_add(1);
2681                let next = crate::conformance::self_test::run_self_test_with_deadline(
2682                    &ops, &cfg, deadline,
2683                )
2684                .await
2685                .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2686                report.merge_iteration(next);
2687                iter_done = iter_done.saturating_add(1);
2688            }
2689            if iter_done > 1 {
2690                TerminalReporter::print_progress(&format!(
2691                    "Self-test repeated {} iteration(s) ({:.1?} elapsed)",
2692                    iter_done,
2693                    start.elapsed(),
2694                ));
2695            }
2696            // Round 23 (c-iii) — drain the capture sink into a JSONL
2697            // file next to the JSON/HTML report. One CaseCapture per
2698            // line so the file is grep-able / streamable. Round 24
2699            // (d) — also emit a self-contained HTML viewer at
2700            // `conformance-self-test-requests.html` for users who
2701            // want to browse the capture without piping through `jq`.
2702            // Round 32 (#79 / Srikanth) — derive the per-endpoint
2703            // traffic summary from the same in-memory capture sink so
2704            // we don't re-parse the JSONL from disk later.
2705            let per_endpoint_summary: Vec<
2706                crate::conformance::per_endpoint_summary::PerEndpointSummary,
2707            >;
2708            if let Some(sink) = capture_sink {
2709                if let Ok(guard) = sink.lock() {
2710                    let jsonl_path = self.output.join("conformance-self-test-requests.jsonl");
2711                    let mut lines = String::with_capacity(guard.len() * 256);
2712                    for entry in guard.iter() {
2713                        if let Ok(line) = serde_json::to_string(entry) {
2714                            lines.push_str(&line);
2715                            lines.push('\n');
2716                        }
2717                    }
2718                    let _ = std::fs::write(&jsonl_path, lines);
2719                    let html_path = self.output.join("conformance-self-test-requests.html");
2720                    let html =
2721                        crate::conformance::capture_html::render_capture_html(guard.as_slice());
2722                    let _ = std::fs::write(&html_path, html);
2723
2724                    // Round 32 — per-endpoint summary derived once from
2725                    // the same slice. Written as a JSON sidecar for
2726                    // automation and spliced into the HTML report below.
2727                    per_endpoint_summary =
2728                        crate::conformance::per_endpoint_summary::build_summary(guard.as_slice());
2729                    let summary_path = self.output.join("conformance-per-endpoint.json");
2730                    if let Ok(json) = serde_json::to_string_pretty(&per_endpoint_summary) {
2731                        let _ = std::fs::write(&summary_path, json);
2732                        TerminalReporter::print_progress(&format!(
2733                            "Self-test request/response capture written to {} ({} entries) + {} + {}",
2734                            jsonl_path.display(),
2735                            guard.len(),
2736                            html_path.display(),
2737                            summary_path.display(),
2738                        ));
2739                    } else {
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                        ));
2746                    }
2747                } else {
2748                    per_endpoint_summary = Vec::new();
2749                }
2750            } else {
2751                per_endpoint_summary = Vec::new();
2752            }
2753            TerminalReporter::print_progress(&report.render_summary());
2754            // Round 47 (#79) — drain the self-test wire-level
2755            // network-events sink into `conformance-network-events.json`
2756            // so the user has the same grep-able file the native
2757            // executor's r46 path produces. Empty array when nothing
2758            // failed (the cleanest signal that connectivity stayed up
2759            // throughout the run).
2760            if let Some(sink) = network_events_sink {
2761                if let Ok(guard) = sink.lock() {
2762                    let path = self.output.join("conformance-network-events.json");
2763                    if let Ok(json) = serde_json::to_string_pretty(&*guard) {
2764                        let _ = std::fs::write(&path, json);
2765                        if guard.is_empty() {
2766                            TerminalReporter::print_progress(
2767                                "No wire-level network failures during self-test (file written empty)",
2768                            );
2769                        } else {
2770                            TerminalReporter::print_warning(&format!(
2771                                "Recorded {} wire-level network event(s) to {}",
2772                                guard.len(),
2773                                path.display()
2774                            ));
2775                        }
2776                    }
2777                }
2778            }
2779            // Persist the JSON report alongside the regular conformance
2780            // report so it's grep-able next to the buffer dump from the
2781            // admin endpoint.
2782            let json_path = self.output.join("conformance-self-test.json");
2783            if let Ok(json) = serde_json::to_string_pretty(&report) {
2784                let _ = std::fs::write(&json_path, json);
2785                TerminalReporter::print_progress(&format!(
2786                    "Self-test report written to {}",
2787                    json_path.display()
2788                ));
2789            }
2790            // Round 18.1 — surface the "every positive failed with
2791            // the same status" case loudly. Without this, a user
2792            // who forgot `--base-path /api` saw 404 for every
2793            // request, but the per-category negative rollup looked
2794            // all-green (because 404 is in the 4xx range the
2795            // negatives expect). Now the run is correctly called
2796            // out as misconfigured before showing the (meaningless)
2797            // negative results.
2798            if let Some(status) = report.detect_target_misconfiguration() {
2799                let hint = match status {
2800                    404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
2801                    401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
2802                    _ => "",
2803                };
2804                TerminalReporter::print_warning(&format!(
2805                    "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
2806                ));
2807            } else if !report.all_passed() {
2808                TerminalReporter::print_warning(
2809                    "Self-test detected gaps — server let through at least one request that should have been a 4xx",
2810                );
2811            } else {
2812                TerminalReporter::print_success(
2813                    "Self-test passed — all positive cases accepted and all negative cases rejected",
2814                );
2815            }
2816            // Round 17.6 — emit a self-contained HTML report alongside
2817            // the JSON. Groups by category and surfaces the missed-
2818            // negative list directly so a user doesn't need to grep
2819            // through the JSON to find which routes failed which
2820            // checks. Optionally folds in a round-17.4 spec audit
2821            // report if one exists in the same output directory.
2822            let html_path = self.output.join("conformance-report.html");
2823            let audit_path = self.output.join("conformance-spec-audit.json");
2824            let audit_value = std::fs::read_to_string(&audit_path)
2825                .ok()
2826                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
2827            // Round 21.1 — `--report-missed-cap N` lets the user
2828            // override the default 200-row HTML drill-down cap.
2829            // `--report-missed-cap 0` maps to `None` (no cap; show
2830            // everything). The JSON report always has the full set.
2831            let render_opts = crate::conformance::report_html::RenderOptions {
2832                missed_cap: match self.report_missed_cap {
2833                    Some(0) => None,
2834                    Some(n) => Some(n as usize),
2835                    None => Some(200),
2836                },
2837            };
2838            let mut html = crate::conformance::report_html::render_html_with_options(
2839                &report,
2840                audit_value.as_ref(),
2841                &render_opts,
2842            );
2843            // Round 32 (#79 / Srikanth) — splice the per-endpoint
2844            // summary just before the closing `</body>` so it lands at
2845            // the bottom of the report. Empty summary renders as an
2846            // empty string so we don't even introduce an extra newline
2847            // when there were no captures.
2848            let summary_section = crate::conformance::per_endpoint_summary::render_html_section(
2849                &per_endpoint_summary,
2850            );
2851            if !summary_section.is_empty() {
2852                if let Some(idx) = html.rfind("</body>") {
2853                    html.insert_str(idx, &summary_section);
2854                } else {
2855                    html.push_str(&summary_section);
2856                }
2857            }
2858            if std::fs::write(&html_path, html).is_ok() {
2859                TerminalReporter::print_progress(&format!(
2860                    "HTML report written to {}",
2861                    html_path.display()
2862                ));
2863            }
2864            return Ok(());
2865        }
2866
2867        // Request validation against OpenAPI spec (if --validate-requests is set)
2868        if self.validate_requests && !self.spec.is_empty() {
2869            TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2870            let violation_count = crate::conformance::request_validator::run_request_validation(
2871                &self.spec,
2872                self.conformance_custom.as_deref(),
2873                self.base_path.as_deref(),
2874                &self.output,
2875            )
2876            .await?;
2877            if violation_count > 0 {
2878                TerminalReporter::print_warning(&format!(
2879                    "{} request validation violation(s) found — see conformance-request-violations.json",
2880                    violation_count
2881                ));
2882            } else {
2883                TerminalReporter::print_success("All requests conform to the OpenAPI spec");
2884            }
2885        }
2886
2887        // If generate-only OR --use-k6, use the k6 script generation path
2888        if self.generate_only || self.use_k6 {
2889            let script = if let Some(annotated) = &annotated_ops {
2890                let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2891                    config,
2892                    annotated.clone(),
2893                );
2894                let op_count = gen.operation_count();
2895                let (script, check_count) = gen.generate()?;
2896                TerminalReporter::print_success(&format!(
2897                    "Conformance: {} operations analyzed, {} unique checks generated",
2898                    op_count, check_count
2899                ));
2900                script
2901            } else {
2902                let generator = ConformanceGenerator::new(config);
2903                generator.generate()?
2904            };
2905
2906            let script_path = self.output.join("k6-conformance.js");
2907            std::fs::write(&script_path, &script).map_err(|e| {
2908                BenchError::Other(format!("Failed to write conformance script: {}", e))
2909            })?;
2910            TerminalReporter::print_success(&format!(
2911                "Conformance script generated: {}",
2912                script_path.display()
2913            ));
2914
2915            if self.generate_only {
2916                println!("\nScript generated. Run with:");
2917                println!("  k6 run {}", script_path.display());
2918                return Ok(());
2919            }
2920
2921            // --use-k6: execute via k6
2922            if !K6Executor::is_k6_installed() {
2923                TerminalReporter::print_error("k6 is not installed");
2924                TerminalReporter::print_warning(
2925                    "Install k6 from: https://k6.io/docs/get-started/installation/",
2926                );
2927                return Err(BenchError::K6NotFound);
2928            }
2929
2930            TerminalReporter::print_progress("Running conformance tests via k6...");
2931            let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2932            executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2933
2934            let report_path = self.output.join("conformance-report.json");
2935            if report_path.exists() {
2936                let report = ConformanceReport::from_file(&report_path)?;
2937                report.print_report_with_options(self.conformance_all_operations);
2938                self.save_conformance_report(&report, &report_path)?;
2939            } else {
2940                TerminalReporter::print_warning(
2941                    "Conformance report not generated (k6 handleSummary may not have run)",
2942                );
2943            }
2944
2945            // Round 44 (#79) — Srikanth on 0.3.188: "Any reason why
2946            // validate-requests in mockforge client are not catching
2947            // all this query param or body params or path params
2948            // violation issues and record in conformance-request-
2949            // failure logs?" The custom-YAML validator only checks
2950            // the YAML shape at config time. Now, when both
2951            // `--validate-requests` and `--export-requests` are set,
2952            // also walk the emitted `conformance-requests.json` and
2953            // validate each actual wire-level request against the
2954            // spec. Violations are appended to
2955            // `conformance-request-violations.json`.
2956            if self.validate_requests && self.export_requests && !self.spec.is_empty() {
2957                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
2958                    &self.spec,
2959                    &self.output,
2960                    self.base_path.as_deref(),
2961                )
2962                .await?;
2963                if n > 0 {
2964                    TerminalReporter::print_warning(&format!(
2965                        "{} emitted request(s) violated the spec — see conformance-request-violations.json",
2966                        n
2967                    ));
2968                }
2969            }
2970
2971            return Ok(());
2972        }
2973
2974        // Default: Native Rust executor (no k6 dependency)
2975        TerminalReporter::print_progress("Running conformance tests (native executor)...");
2976
2977        let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2978
2979        // Round 39 (#79) — when the user passed `--conformance-custom`
2980        // WITHOUT `--spec` and without `--conformance-self-test`, fire
2981        // only the YAML's checks. The built-in 47 reference checks
2982        // (`param:path:string`, etc.) hit `/conformance/...` paths that
2983        // do not exist on a real target, so a custom-only run against
2984        // a remote API produced a flood of irrelevant 404s in the
2985        // request log. Srikanth on 0.3.183: "In the exported request I
2986        // see it is sending request to api/conformance/params/hello
2987        // and some other URLs".
2988        let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
2989        executor = if let Some(annotated) = &annotated_ops {
2990            executor.with_spec_driven_checks(annotated)
2991        } else if custom_only {
2992            executor
2993        } else {
2994            executor.with_reference_checks()
2995        };
2996        executor = executor.with_custom_checks()?;
2997
2998        TerminalReporter::print_success(&format!(
2999            "Executing {} conformance checks...",
3000            executor.check_count()
3001        ));
3002
3003        let report = executor.execute().await?;
3004        report.print_report_with_options(self.conformance_all_operations);
3005
3006        // Save failure details to a separate file for easy debugging
3007        let failure_details = report.failure_details();
3008        if !failure_details.is_empty() {
3009            let details_path = self.output.join("conformance-failure-details.json");
3010            if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3011                let _ = std::fs::write(&details_path, json);
3012                TerminalReporter::print_success(&format!(
3013                    "Failure details saved to: {}",
3014                    details_path.display()
3015                ));
3016            }
3017        }
3018
3019        // Save report
3020        let report_path = self.output.join("conformance-report.json");
3021        let report_json = serde_json::to_string_pretty(&report.to_json())
3022            .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3023        std::fs::write(&report_path, &report_json)
3024            .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3025        TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
3026
3027        self.save_conformance_report(&report, &report_path)?;
3028
3029        // Round 45 (#79) — Srikanth on 0.3.189: "I am still not seeing
3030        // any conformance failure logs or conformance-request logs are
3031        // also not capturing any failures info." His command does NOT
3032        // pass `--use-k6`, so the round-44 wiring (which only ran on
3033        // the k6 branch) never fired. Mirror the same retrospective
3034        // pass here: when both `--validate-requests` and
3035        // `--export-requests` are set, walk the native executor's
3036        // freshly-written `conformance-requests.json` and validate
3037        // each emitted request against the spec. Violations are
3038        // appended to `conformance-request-violations.json`.
3039        if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3040            let n =
3041                crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3042                    &self.spec,
3043                    &self.output,
3044                    self.base_path.as_deref(),
3045                )
3046                .await?;
3047            if n > 0 {
3048                TerminalReporter::print_warning(&format!(
3049                    "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3050                    n
3051                ));
3052            }
3053        }
3054
3055        Ok(())
3056    }
3057
3058    /// Save conformance report in the requested format (SARIF or JSON copy)
3059    fn save_conformance_report(
3060        &self,
3061        report: &crate::conformance::report::ConformanceReport,
3062        report_path: &Path,
3063    ) -> Result<()> {
3064        if self.conformance_report_format == "sarif" {
3065            use crate::conformance::sarif::ConformanceSarifReport;
3066            ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
3067            TerminalReporter::print_success(&format!(
3068                "SARIF report saved to: {}",
3069                self.conformance_report.display()
3070            ));
3071        } else if self.conformance_report != *report_path {
3072            std::fs::copy(report_path, &self.conformance_report)?;
3073            TerminalReporter::print_success(&format!(
3074                "Report saved to: {}",
3075                self.conformance_report.display()
3076            ));
3077        }
3078        Ok(())
3079    }
3080
3081    /// Round 48 (#79) — Srikanth on 0.3.192: "I ran following commands
3082    /// to test conformance-sef-test duration, but the test ended
3083    /// immediately" with `--targets-file vs_list1.json`. The multi-
3084    /// target dispatch returned before reaching the round-47 self-test
3085    /// iteration loop. This helper runs the self-test driver against
3086    /// every target listed in `targets_file` honouring the same
3087    /// `--conformance-self-test-iterations` and `--conformance-self-
3088    /// test-duration` knobs the single-target path got. One self-test
3089    /// report file per target plus a `conformance-network-events.json`
3090    /// per target so a user can attribute wire failures back to the
3091    /// target they happened against.
3092    async fn execute_multi_target_self_test(&self, targets_file: &Path) -> Result<()> {
3093        use crate::conformance::self_test::SelfTestConfig;
3094
3095        TerminalReporter::print_progress("Multi-target conformance self-test mode");
3096        let targets = parse_targets_file(targets_file)?;
3097        if targets.is_empty() {
3098            return Err(BenchError::Other("No targets found in file".to_string()));
3099        }
3100        TerminalReporter::print_success(&format!("Loaded {} target(s)", targets.len()));
3101
3102        // Spec is shared across targets — load once.
3103        let annotated_ops = if !self.spec.is_empty() {
3104            let parser = SpecParser::from_file(&self.spec[0]).await?;
3105            let operations = parser.get_operations();
3106            Some(
3107                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3108                    &operations,
3109                    parser.spec(),
3110                ),
3111            )
3112        } else {
3113            return Err(BenchError::Other("--conformance-self-test requires --spec".to_string()));
3114        };
3115        let Some(ops) = annotated_ops else {
3116            unreachable!()
3117        };
3118
3119        std::fs::create_dir_all(&self.output)?;
3120        let resolved_base_path = self.base_path.clone();
3121        let target_iterations = self.conformance_self_test_iterations.max(1);
3122        let duration_budget = self
3123            .conformance_self_test_duration
3124            .as_ref()
3125            .map(|s| Self::parse_duration(s))
3126            .transpose()?
3127            .map(std::time::Duration::from_secs);
3128
3129        for (idx, target) in targets.iter().enumerate() {
3130            let target_dir = self.output.join(format!("target_{}", idx));
3131            std::fs::create_dir_all(&target_dir)?;
3132            TerminalReporter::print_progress(&format!(
3133                "[target {}/{}] {}",
3134                idx + 1,
3135                targets.len(),
3136                target.url
3137            ));
3138
3139            let merged_headers: Vec<(String, String)> = self
3140                .conformance_headers
3141                .iter()
3142                .filter_map(|h| {
3143                    let (n, v) = h.split_once(':')?;
3144                    Some((n.trim().to_string(), v.trim().to_string()))
3145                })
3146                .collect();
3147
3148            let cfg = SelfTestConfig {
3149                target_url: target.url.clone(),
3150                skip_tls_verify: self.skip_tls_verify,
3151                timeout: std::time::Duration::from_secs(30),
3152                extra_headers: merged_headers,
3153                delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
3154                base_path: resolved_base_path.clone(),
3155                source_ips: parse_ip_list(&self.source_ips, "source-ip"),
3156                geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
3157                geo_source_headers: if self.geo_source_headers.is_empty() {
3158                    crate::conformance::self_test::default_geo_source_headers()
3159                } else {
3160                    self.geo_source_headers.clone()
3161                },
3162                capture: if self.conformance_self_test_capture || self.validate_response_schemas {
3163                    Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
3164                } else {
3165                    None
3166                },
3167                validate_response_schemas: self.validate_response_schemas,
3168                spec_label: self.spec.first().map(|p| {
3169                    p.file_name()
3170                        .map(|s| s.to_string_lossy().into_owned())
3171                        .unwrap_or_else(|| p.to_string_lossy().into_owned())
3172                }),
3173                network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
3174                current_iteration: 1,
3175            };
3176            let capture_sink = cfg.capture.clone();
3177            let network_events_sink = cfg.network_events.clone();
3178
3179            let start = std::time::Instant::now();
3180            // Round 49 — pass an absolute deadline down so the loop
3181            // can break out mid-iteration once the budget elapses
3182            // instead of overshooting by a full pass.
3183            let deadline = duration_budget.map(|d| start + d);
3184            // Round 49 — stamp `current_iteration` on cfg before each
3185            // pass so CaseCapture's `iteration` field carries the
3186            // loop counter (1-indexed).
3187            let mut cfg = cfg;
3188            cfg.current_iteration = 1;
3189            let mut report =
3190                crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
3191                    .await
3192                    .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3193            let mut iter_done: u32 = 1;
3194            loop {
3195                let by_iter = iter_done >= target_iterations;
3196                let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
3197                if by_iter && by_dur {
3198                    break;
3199                }
3200                cfg.current_iteration = iter_done.saturating_add(1);
3201                let next = crate::conformance::self_test::run_self_test_with_deadline(
3202                    &ops, &cfg, deadline,
3203                )
3204                .await
3205                .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3206                report.merge_iteration(next);
3207                iter_done = iter_done.saturating_add(1);
3208            }
3209            if iter_done > 1 {
3210                TerminalReporter::print_progress(&format!(
3211                    "  ran {} iteration(s) in {:.1?}",
3212                    iter_done,
3213                    start.elapsed(),
3214                ));
3215            }
3216
3217            // Drain the per-target sinks.
3218            if let Some(sink) = capture_sink {
3219                if let Ok(guard) = sink.lock() {
3220                    let jsonl = target_dir.join("conformance-self-test-requests.jsonl");
3221                    let mut lines = String::with_capacity(guard.len() * 256);
3222                    for entry in guard.iter() {
3223                        if let Ok(line) = serde_json::to_string(entry) {
3224                            lines.push_str(&line);
3225                            lines.push('\n');
3226                        }
3227                    }
3228                    let _ = std::fs::write(&jsonl, lines);
3229                }
3230            }
3231            if let Some(sink) = network_events_sink {
3232                if let Ok(guard) = sink.lock() {
3233                    let path = target_dir.join("conformance-network-events.json");
3234                    if let Ok(json) = serde_json::to_string_pretty(&*guard) {
3235                        let _ = std::fs::write(&path, json);
3236                        if !guard.is_empty() {
3237                            TerminalReporter::print_warning(&format!(
3238                                "  recorded {} wire-level network event(s)",
3239                                guard.len()
3240                            ));
3241                        }
3242                    }
3243                }
3244            }
3245
3246            let json_path = target_dir.join("conformance-self-test.json");
3247            if let Ok(json) = serde_json::to_string_pretty(&report) {
3248                let _ = std::fs::write(&json_path, json);
3249            }
3250            TerminalReporter::print_progress(&report.render_summary());
3251
3252            // Round 49 (#79) — Srikanth on 0.3.193: "I am not seeing
3253            // any violation requests logs when running [self-test
3254            // + --targets-file]". `validate_emitted_requests` was
3255            // only wired into the bench-export path; self-test
3256            // writes its captures to `conformance-self-test-
3257            // requests.jsonl` instead. The validator now reads that
3258            // file too (see the JSONL branch in request_validator.rs),
3259            // so we just need to invoke it here per-target.
3260            if self.validate_requests {
3261                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3262                    &self.spec,
3263                    &target_dir,
3264                    self.base_path.as_deref(),
3265                )
3266                .await?;
3267                if n > 0 {
3268                    TerminalReporter::print_warning(&format!(
3269                        "  {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3270                        n,
3271                        target_dir.display(),
3272                    ));
3273                }
3274            }
3275        }
3276
3277        Ok(())
3278    }
3279
3280    /// Execute conformance tests against multiple targets from a targets file.
3281    ///
3282    /// Uses the native `NativeConformanceExecutor` (no k6 dependency). Targets are
3283    /// tested sequentially to avoid overwhelming them, and per-target headers from
3284    /// the targets file are merged with the base `--conformance-header` headers.
3285    async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
3286        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
3287        use crate::conformance::report::ConformanceReport;
3288        use crate::conformance::spec::ConformanceFeature;
3289
3290        TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
3291
3292        // Parse targets file
3293        TerminalReporter::print_progress("Parsing targets file...");
3294        let targets = parse_targets_file(targets_file)?;
3295        let num_targets = targets.len();
3296        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
3297
3298        if targets.is_empty() {
3299            return Err(BenchError::Other("No targets found in file".to_string()));
3300        }
3301
3302        TerminalReporter::print_progress(
3303            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
3304        );
3305
3306        // Parse category filter (shared across all targets)
3307        let categories = self.conformance_categories.as_ref().map(|cats_str| {
3308            cats_str
3309                .split(',')
3310                .filter_map(|s| {
3311                    let trimmed = s.trim();
3312                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
3313                        Some(canonical.to_string())
3314                    } else {
3315                        TerminalReporter::print_warning(&format!(
3316                            "Unknown conformance category: '{}'. Valid categories: {}",
3317                            trimmed,
3318                            ConformanceFeature::cli_category_names()
3319                                .iter()
3320                                .map(|(cli, _)| *cli)
3321                                .collect::<Vec<_>>()
3322                                .join(", ")
3323                        ));
3324                        None
3325                    }
3326                })
3327                .collect::<Vec<String>>()
3328        });
3329
3330        // Parse base custom headers from --conformance-header flags
3331        let base_custom_headers: Vec<(String, String)> = self
3332            .conformance_headers
3333            .iter()
3334            .filter_map(|h| {
3335                let (name, value) = h.split_once(':')?;
3336                Some((name.trim().to_string(), value.trim().to_string()))
3337            })
3338            .collect();
3339
3340        if !base_custom_headers.is_empty() {
3341            TerminalReporter::print_progress(&format!(
3342                "Using {} base custom header(s) for authentication",
3343                base_custom_headers.len()
3344            ));
3345        }
3346
3347        // Load spec once if provided (shared across all targets)
3348        let annotated_ops = if !self.spec.is_empty() {
3349            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
3350            let parser = SpecParser::from_file(&self.spec[0]).await?;
3351            let operations = parser.get_operations();
3352            let annotated =
3353                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3354                    &operations,
3355                    parser.spec(),
3356                );
3357            TerminalReporter::print_success(&format!(
3358                "Analyzed {} operations, found {} feature annotations",
3359                operations.len(),
3360                annotated.iter().map(|a| a.features.len()).sum::<usize>()
3361            ));
3362            Some(annotated)
3363        } else {
3364            None
3365        };
3366
3367        // Ensure output dir exists
3368        std::fs::create_dir_all(&self.output)?;
3369
3370        // Collect per-target results for the summary
3371        struct TargetResult {
3372            url: String,
3373            passed: usize,
3374            failed: usize,
3375            elapsed: std::time::Duration,
3376            report_json: serde_json::Value,
3377            owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
3378        }
3379
3380        let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
3381        let total_start = std::time::Instant::now();
3382
3383        for (idx, target) in targets.iter().enumerate() {
3384            tracing::info!(
3385                "Running conformance tests against target {}/{}: {}",
3386                idx + 1,
3387                num_targets,
3388                target.url
3389            );
3390            TerminalReporter::print_progress(&format!(
3391                "\n--- Target {}/{}: {} ---",
3392                idx + 1,
3393                num_targets,
3394                target.url
3395            ));
3396
3397            // Merge base headers with per-target headers
3398            let mut merged_headers = base_custom_headers.clone();
3399            if let Some(ref target_headers) = target.headers {
3400                for (name, value) in target_headers {
3401                    // Per-target headers override base headers with the same name
3402                    if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
3403                        existing.1 = value.clone();
3404                    } else {
3405                        merged_headers.push((name.clone(), value.clone()));
3406                    }
3407                }
3408            }
3409            // Add auth header if present on target
3410            if let Some(ref auth) = target.auth {
3411                if let Some(existing) =
3412                    merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
3413                {
3414                    existing.1 = auth.clone();
3415                } else {
3416                    merged_headers.push(("Authorization".to_string(), auth.clone()));
3417                }
3418            }
3419
3420            // Per-target output dir (used by both native and k6 paths).
3421            // Created before the config so we can point the k6 script's
3422            // handleSummary at the per-target directory rather than the shared
3423            // parent output dir (otherwise every target would overwrite the
3424            // same conformance-report.json).
3425            let target_dir = self.output.join(format!("target_{}", idx));
3426            std::fs::create_dir_all(&target_dir)?;
3427
3428            let config = ConformanceConfig {
3429                target_url: target.url.clone(),
3430                api_key: self.conformance_api_key.clone(),
3431                basic_auth: self.conformance_basic_auth.clone(),
3432                skip_tls_verify: self.skip_tls_verify,
3433                categories: categories.clone(),
3434                base_path: self.base_path.clone(),
3435                custom_headers: merged_headers,
3436                output_dir: Some(target_dir.clone()),
3437                all_operations: self.conformance_all_operations,
3438                custom_checks_file: self.conformance_custom.clone(),
3439                request_delay_ms: self.conformance_delay_ms,
3440                custom_filter: self.conformance_custom_filter.clone(),
3441                export_requests: self.export_requests,
3442                validate_requests: self.validate_requests,
3443            };
3444
3445            let target_start = std::time::Instant::now();
3446            let report = if self.use_k6 {
3447                if !K6Executor::is_k6_installed() {
3448                    TerminalReporter::print_error("k6 is not installed");
3449                    TerminalReporter::print_warning(
3450                        "Install k6 from: https://k6.io/docs/get-started/installation/",
3451                    );
3452                    return Err(BenchError::K6NotFound);
3453                }
3454
3455                let script = if let Some(ref annotated) = annotated_ops {
3456                    let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3457                        config.clone(),
3458                        annotated.clone(),
3459                    );
3460                    let (script, _check_count) = gen.generate()?;
3461                    script
3462                } else {
3463                    let generator = ConformanceGenerator::new(config.clone());
3464                    generator.generate()?
3465                };
3466
3467                let script_path = target_dir.join("k6-conformance.js");
3468                std::fs::write(&script_path, &script).map_err(|e| {
3469                    BenchError::Other(format!("Failed to write conformance script: {}", e))
3470                })?;
3471                TerminalReporter::print_success(&format!(
3472                    "Conformance script generated: {}",
3473                    script_path.display()
3474                ));
3475
3476                TerminalReporter::print_progress(&format!(
3477                    "Running conformance tests via k6 against {}...",
3478                    target.url
3479                ));
3480                let k6 = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
3481                // Unique k6 API port per target to avoid collisions.
3482                let api_port = 6565u16.saturating_add(idx as u16);
3483                k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
3484                    .await?;
3485
3486                let report_path = target_dir.join("conformance-report.json");
3487                if report_path.exists() {
3488                    ConformanceReport::from_file(&report_path)?
3489                } else {
3490                    TerminalReporter::print_warning(&format!(
3491                        "Conformance report not generated for target {} (k6 handleSummary may not have run)",
3492                        target.url
3493                    ));
3494                    continue;
3495                }
3496            } else {
3497                let mut executor =
3498                    crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3499
3500                // Round 39 (#79) — see custom_only comment above; same
3501                // logic applied to the multi-target branch.
3502                let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3503                executor = if let Some(ref annotated) = annotated_ops {
3504                    executor.with_spec_driven_checks(annotated)
3505                } else if custom_only {
3506                    executor
3507                } else {
3508                    executor.with_reference_checks()
3509                };
3510                executor = executor.with_custom_checks()?;
3511
3512                TerminalReporter::print_success(&format!(
3513                    "Executing {} conformance checks against {}...",
3514                    executor.check_count(),
3515                    target.url
3516                ));
3517
3518                executor.execute().await?
3519            };
3520            let target_elapsed = target_start.elapsed();
3521
3522            let report_json = report.to_json();
3523
3524            // Extract pass/fail from the summary in the JSON
3525            let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
3526            let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
3527            let total_checks = passed + failed;
3528            let rate = if total_checks == 0 {
3529                0.0
3530            } else {
3531                (passed as f64 / total_checks as f64) * 100.0
3532            };
3533
3534            TerminalReporter::print_success(&format!(
3535                "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
3536                target.url,
3537                passed,
3538                total_checks,
3539                rate,
3540                target_elapsed.as_secs_f64()
3541            ));
3542
3543            // Save per-target report (target_dir created above)
3544            let target_report_path = target_dir.join("conformance-report.json");
3545            let report_str = serde_json::to_string_pretty(&report_json)
3546                .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3547            std::fs::write(&target_report_path, &report_str)
3548                .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3549
3550            // Save failure details if any
3551            let failure_details = report.failure_details();
3552            if !failure_details.is_empty() {
3553                let details_path = target_dir.join("conformance-failure-details.json");
3554                if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3555                    let _ = std::fs::write(&details_path, json);
3556                }
3557            }
3558
3559            // Round 45 (#79) — Srikanth on 0.3.189: `conformance-request-
3560            // violations.json` was never being written in his
3561            // multi-target self-test run. The round-44 wiring sat on
3562            // the single-target branch only. Mirror it here, per
3563            // target_dir, so the multi-target case (his typical
3564            // workflow) actually surfaces wire-level violations.
3565            if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3566                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3567                    &self.spec,
3568                    &target_dir,
3569                    self.base_path.as_deref(),
3570                )
3571                .await?;
3572                if n > 0 {
3573                    TerminalReporter::print_warning(&format!(
3574                        "Target {}: {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3575                        target.url,
3576                        n,
3577                        target_dir.display()
3578                    ));
3579                }
3580            }
3581
3582            // Compute OWASP coverage for this target
3583            let owasp_coverage = report.owasp_coverage_data();
3584
3585            target_results.push(TargetResult {
3586                url: target.url.clone(),
3587                passed,
3588                failed,
3589                elapsed: target_elapsed,
3590                report_json,
3591                owasp_coverage,
3592            });
3593        }
3594
3595        let total_elapsed = total_start.elapsed();
3596
3597        // Print summary table
3598        println!("\n{}", "=".repeat(80));
3599        println!("  Multi-Target Conformance Summary");
3600        println!("{}", "=".repeat(80));
3601        println!(
3602            "  {:<40} {:>8} {:>8} {:>8} {:>8}",
3603            "Target URL", "Passed", "Failed", "Rate", "Time"
3604        );
3605        println!("  {}", "-".repeat(76));
3606
3607        let mut total_passed = 0usize;
3608        let mut total_failed = 0usize;
3609
3610        for result in &target_results {
3611            let total_checks = result.passed + result.failed;
3612            let rate = if total_checks == 0 {
3613                0.0
3614            } else {
3615                (result.passed as f64 / total_checks as f64) * 100.0
3616            };
3617
3618            // Truncate long URLs for display
3619            let display_url = if result.url.len() > 38 {
3620                format!("{}...", &result.url[..35])
3621            } else {
3622                result.url.clone()
3623            };
3624
3625            println!(
3626                "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3627                display_url,
3628                result.passed,
3629                result.failed,
3630                rate,
3631                result.elapsed.as_secs_f64()
3632            );
3633
3634            total_passed += result.passed;
3635            total_failed += result.failed;
3636        }
3637
3638        let grand_total = total_passed + total_failed;
3639        let overall_rate = if grand_total == 0 {
3640            0.0
3641        } else {
3642            (total_passed as f64 / grand_total as f64) * 100.0
3643        };
3644
3645        println!("  {}", "-".repeat(76));
3646        println!(
3647            "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3648            format!("TOTAL ({} targets)", num_targets),
3649            total_passed,
3650            total_failed,
3651            overall_rate,
3652            total_elapsed.as_secs_f64()
3653        );
3654        println!("{}", "=".repeat(80));
3655
3656        // Print per-target OWASP coverage
3657        for result in &target_results {
3658            println!("\n  OWASP API Security Top 10 Coverage for {}:", result.url);
3659            for entry in &result.owasp_coverage {
3660                let status = if !entry.tested {
3661                    "-"
3662                } else if entry.all_passed {
3663                    "pass"
3664                } else {
3665                    "FAIL"
3666                };
3667                let via = if entry.via_categories.is_empty() {
3668                    String::new()
3669                } else {
3670                    format!(" (via {})", entry.via_categories.join(", "))
3671                };
3672                println!("    {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
3673            }
3674        }
3675
3676        // Save combined summary
3677        let per_target_summaries: Vec<serde_json::Value> = target_results
3678            .iter()
3679            .enumerate()
3680            .map(|(idx, r)| {
3681                let total_checks = r.passed + r.failed;
3682                let rate = if total_checks == 0 {
3683                    0.0
3684                } else {
3685                    (r.passed as f64 / total_checks as f64) * 100.0
3686                };
3687                let owasp_json: Vec<serde_json::Value> = r
3688                    .owasp_coverage
3689                    .iter()
3690                    .map(|e| {
3691                        serde_json::json!({
3692                            "id": e.id,
3693                            "name": e.name,
3694                            "tested": e.tested,
3695                            "all_passed": e.all_passed,
3696                            "via_categories": e.via_categories,
3697                        })
3698                    })
3699                    .collect();
3700                serde_json::json!({
3701                    "target_url": r.url,
3702                    "target_index": idx,
3703                    "checks_passed": r.passed,
3704                    "checks_failed": r.failed,
3705                    "total_checks": total_checks,
3706                    "pass_rate": rate,
3707                    "elapsed_seconds": r.elapsed.as_secs_f64(),
3708                    "report": r.report_json,
3709                    "owasp_coverage": owasp_json,
3710                })
3711            })
3712            .collect();
3713
3714        let combined_summary = serde_json::json!({
3715            "total_targets": num_targets,
3716            "total_checks_passed": total_passed,
3717            "total_checks_failed": total_failed,
3718            "overall_pass_rate": overall_rate,
3719            "total_elapsed_seconds": total_elapsed.as_secs_f64(),
3720            "targets": per_target_summaries,
3721        });
3722
3723        let summary_path = self.output.join("multi-target-conformance-summary.json");
3724        let summary_str = serde_json::to_string_pretty(&combined_summary)
3725            .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
3726        std::fs::write(&summary_path, &summary_str)
3727            .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
3728        TerminalReporter::print_success(&format!(
3729            "Combined summary saved to: {}",
3730            summary_path.display()
3731        ));
3732
3733        Ok(())
3734    }
3735
3736    /// Execute OWASP API Security Top 10 testing mode
3737    async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
3738        TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
3739
3740        // Parse custom headers from CLI
3741        let custom_headers = self.parse_headers()?;
3742
3743        // Build OWASP configuration from CLI options
3744        let mut config = OwaspApiConfig::new()
3745            .with_auth_header(&self.owasp_auth_header)
3746            .with_verbose(self.verbose)
3747            .with_insecure(self.skip_tls_verify)
3748            .with_concurrency(self.vus as usize)
3749            .with_iterations(self.owasp_iterations as usize)
3750            .with_base_path(self.base_path.clone())
3751            .with_custom_headers(custom_headers);
3752
3753        // Set valid auth token if provided
3754        if let Some(ref token) = self.owasp_auth_token {
3755            config = config.with_valid_auth_token(token);
3756        }
3757
3758        // Parse categories if provided
3759        if let Some(ref cats_str) = self.owasp_categories {
3760            let categories: Vec<OwaspCategory> = cats_str
3761                .split(',')
3762                .filter_map(|s| {
3763                    let trimmed = s.trim();
3764                    match trimmed.parse::<OwaspCategory>() {
3765                        Ok(cat) => Some(cat),
3766                        Err(e) => {
3767                            TerminalReporter::print_warning(&e);
3768                            None
3769                        }
3770                    }
3771                })
3772                .collect();
3773
3774            if !categories.is_empty() {
3775                config = config.with_categories(categories);
3776            }
3777        }
3778
3779        // Load admin paths from file if provided
3780        if let Some(ref admin_paths_file) = self.owasp_admin_paths {
3781            config.admin_paths_file = Some(admin_paths_file.clone());
3782            if let Err(e) = config.load_admin_paths() {
3783                TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
3784            }
3785        }
3786
3787        // Set ID fields if provided
3788        if let Some(ref id_fields_str) = self.owasp_id_fields {
3789            let id_fields: Vec<String> = id_fields_str
3790                .split(',')
3791                .map(|s| s.trim().to_string())
3792                .filter(|s| !s.is_empty())
3793                .collect();
3794            if !id_fields.is_empty() {
3795                config = config.with_id_fields(id_fields);
3796            }
3797        }
3798
3799        // Set report path and format
3800        if let Some(ref report_path) = self.owasp_report {
3801            config = config.with_report_path(report_path);
3802        }
3803        if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
3804            config = config.with_report_format(format);
3805        }
3806
3807        // Print configuration summary
3808        let categories = config.categories_to_test();
3809        TerminalReporter::print_success(&format!(
3810            "Testing {} OWASP categories: {}",
3811            categories.len(),
3812            categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
3813        ));
3814
3815        if config.valid_auth_token.is_some() {
3816            TerminalReporter::print_progress("Using provided auth token for baseline requests");
3817        }
3818
3819        // Create the OWASP generator
3820        TerminalReporter::print_progress("Generating OWASP security test script...");
3821        let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
3822
3823        // Generate the script
3824        let script = generator.generate()?;
3825        TerminalReporter::print_success("OWASP security test script generated");
3826
3827        // Write script to file
3828        let script_path = if let Some(output) = &self.script_output {
3829            output.clone()
3830        } else {
3831            self.output.join("k6-owasp-security-test.js")
3832        };
3833
3834        if let Some(parent) = script_path.parent() {
3835            std::fs::create_dir_all(parent)?;
3836        }
3837        std::fs::write(&script_path, &script)?;
3838        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
3839
3840        // If generate-only mode, exit here
3841        if self.generate_only {
3842            println!("\nOWASP security test script generated. Run it with:");
3843            println!("  k6 run {}", script_path.display());
3844            return Ok(());
3845        }
3846
3847        // Execute k6
3848        TerminalReporter::print_progress("Executing OWASP security tests...");
3849        let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
3850        std::fs::create_dir_all(&self.output)?;
3851
3852        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3853
3854        let duration_secs = Self::parse_duration(&self.duration)?;
3855        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
3856
3857        println!("\nOWASP security test results saved to: {}", self.output.display());
3858
3859        Ok(())
3860    }
3861}
3862
3863#[cfg(test)]
3864mod tests {
3865    use super::*;
3866    use tempfile::tempdir;
3867
3868    #[test]
3869    fn test_parse_duration() {
3870        assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
3871        assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
3872        assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
3873        assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
3874    }
3875
3876    /// Round 22.4 — `start-end` IPv4 range syntax for non-power-of-2
3877    /// ranges. Srikanth (h): `--source-ip 10.0.0.5-10.0.0.27` for 23
3878    /// hosts without finding a clean prefix.
3879    #[test]
3880    fn parse_ip_list_ipv4_range_inclusive() {
3881        let v = parse_ip_list(&["10.0.0.5-10.0.0.27".into()], "source-ip");
3882        assert_eq!(v.len(), 23);
3883        assert_eq!(v.first().unwrap().to_string(), "10.0.0.5");
3884        assert_eq!(v.last().unwrap().to_string(), "10.0.0.27");
3885    }
3886
3887    /// Round 22.4 — range with start > end is rejected with a warning
3888    /// (returns nothing for that entry rather than wrapping around).
3889    #[test]
3890    fn parse_ip_list_range_rejects_backwards() {
3891        let v = parse_ip_list(&["10.0.0.10-10.0.0.5".into()], "source-ip");
3892        assert!(v.is_empty(), "backwards range should produce no IPs; got {v:?}");
3893    }
3894
3895    /// Round 22.4 — IPv6 ranges are intentionally rejected because
3896    /// `2001:db8::1-2001:db8::5` would ambiguously parse against the
3897    /// address literal's `:` separators. Users use CIDR for IPv6.
3898    #[test]
3899    fn parse_ip_list_rejects_ipv6_range_syntax() {
3900        let v = parse_ip_list(&["2001:db8::1-2001:db8::5".into()], "geo-source-ip");
3901        assert!(v.is_empty(), "IPv6 range should be rejected; got {v:?}");
3902    }
3903
3904    /// Round 22.4 — range cap is the same 256 host limit as CIDR.
3905    #[test]
3906    fn parse_ip_list_range_capped_at_256() {
3907        let v = parse_ip_list(&["10.0.0.0-10.0.5.0".into()], "source-ip");
3908        assert_eq!(v.len(), 256);
3909        assert_eq!(v.first().unwrap().to_string(), "10.0.0.0");
3910    }
3911
3912    /// Round 19 — single IPs and comma-separated lists already
3913    /// worked in 18.5; this regression-locks the parse paths.
3914    #[test]
3915    fn parse_ip_list_plain_and_comma() {
3916        let v = parse_ip_list(&["10.0.0.5".into(), "10.0.0.6,10.0.0.7".into()], "source-ip");
3917        assert_eq!(v.len(), 3);
3918        assert_eq!(v[0].to_string(), "10.0.0.5");
3919        assert_eq!(v[2].to_string(), "10.0.0.7");
3920    }
3921
3922    /// Round 19 — IPv4 CIDR expands to host count up to the cap.
3923    /// `/29` = 8 hosts (well under cap), all 8 enumerated.
3924    #[test]
3925    fn parse_ip_list_ipv4_cidr_29_expands_to_8() {
3926        let v = parse_ip_list(&["10.0.0.0/29".into()], "source-ip");
3927        assert_eq!(v.len(), 8);
3928        assert_eq!(v[0].to_string(), "10.0.0.0");
3929        assert_eq!(v[7].to_string(), "10.0.0.7");
3930    }
3931
3932    /// Round 19 — IPv4 CIDR larger than the cap is truncated, not
3933    /// rejected. Cap is 256; `/8` would be 16M without the guard.
3934    #[test]
3935    fn parse_ip_list_ipv4_cidr_8_capped_at_256() {
3936        let v = parse_ip_list(&["10.0.0.0/8".into()], "source-ip");
3937        assert_eq!(v.len(), 256);
3938        assert_eq!(v[0].to_string(), "10.0.0.0");
3939        assert_eq!(v[255].to_string(), "10.0.0.255");
3940    }
3941
3942    /// Round 19 — IPv6 CIDR also expands. `/126` = 4 hosts.
3943    #[test]
3944    fn parse_ip_list_ipv6_cidr_126_expands_to_4() {
3945        let v = parse_ip_list(&["2001:db8::/126".into()], "geo-source-ip");
3946        assert_eq!(v.len(), 4);
3947        assert!(v[0].is_ipv6());
3948        assert_eq!(v[0].to_string(), "2001:db8::");
3949        assert_eq!(v[3].to_string(), "2001:db8::3");
3950    }
3951
3952    /// Round 19 — mixed IPv4 + IPv6 + CIDR in one call works.
3953    #[test]
3954    fn parse_ip_list_mixed_v4_v6_cidr() {
3955        let v = parse_ip_list(&["10.0.0.0/30,2001:db8::1,203.0.113.42".into()], "geo-source-ip");
3956        assert_eq!(v.len(), 6); // 4 from /30 + 1 + 1
3957        assert!(v.iter().any(|ip| ip.to_string() == "2001:db8::1"));
3958        assert!(v.iter().any(|ip| ip.to_string() == "203.0.113.42"));
3959    }
3960
3961    /// Round 19 — malformed entries log and skip; the run continues
3962    /// with whatever resolved.
3963    #[test]
3964    fn parse_ip_list_skips_malformed() {
3965        let v = parse_ip_list(
3966            &[
3967                "10.0.0.5".into(),
3968                "not-an-ip".into(),
3969                "10.0.0.6".into(),
3970                "/24".into(),
3971                "1.2.3.4/200".into(),
3972            ],
3973            "source-ip",
3974        );
3975        assert_eq!(v.len(), 2);
3976        assert_eq!(v[0].to_string(), "10.0.0.5");
3977        assert_eq!(v[1].to_string(), "10.0.0.6");
3978    }
3979
3980    #[test]
3981    fn test_parse_duration_invalid() {
3982        assert!(BenchCommand::parse_duration("invalid").is_err());
3983        assert!(BenchCommand::parse_duration("30x").is_err());
3984    }
3985
3986    #[test]
3987    fn test_parse_headers() {
3988        let cmd = BenchCommand {
3989            spec: vec![PathBuf::from("test.yaml")],
3990            spec_dir: None,
3991            merge_conflicts: "error".to_string(),
3992            spec_mode: "merge".to_string(),
3993            dependency_config: None,
3994            target: "http://localhost".to_string(),
3995            base_path: None,
3996            duration: "1m".to_string(),
3997            vus: 10,
3998            scenario: "ramp-up".to_string(),
3999            operations: None,
4000            exclude_operations: None,
4001            auth: None,
4002            headers: vec![
4003                "X-API-Key:test123".to_string(),
4004                "X-Client-ID:client456".to_string(),
4005            ],
4006            output: PathBuf::from("output"),
4007            generate_only: false,
4008            script_output: None,
4009            threshold_percentile: "p(95)".to_string(),
4010            threshold_ms: 500,
4011            max_error_rate: 0.05,
4012            verbose: false,
4013            skip_tls_verify: false,
4014            chunked_request_bodies: false,
4015            target_rps: None,
4016            no_keep_alive: false,
4017            targets_file: None,
4018            max_concurrency: None,
4019            results_format: "both".to_string(),
4020            params_file: None,
4021            crud_flow: false,
4022            flow_config: None,
4023            extract_fields: None,
4024            parallel_create: None,
4025            data_file: None,
4026            data_distribution: "unique-per-vu".to_string(),
4027            data_mappings: None,
4028            per_uri_control: false,
4029            error_rate: None,
4030            error_types: None,
4031            security_test: false,
4032            security_payloads: None,
4033            security_categories: None,
4034            security_target_fields: None,
4035            wafbench_dir: None,
4036            wafbench_cycle_all: false,
4037            owasp_api_top10: false,
4038            owasp_categories: None,
4039            owasp_auth_header: "Authorization".to_string(),
4040            owasp_auth_token: None,
4041            owasp_admin_paths: None,
4042            owasp_id_fields: None,
4043            owasp_report: None,
4044            owasp_report_format: "json".to_string(),
4045            owasp_iterations: 1,
4046            conformance: false,
4047            conformance_api_key: None,
4048            conformance_basic_auth: None,
4049            conformance_report: PathBuf::from("conformance-report.json"),
4050            conformance_categories: None,
4051            conformance_report_format: "json".to_string(),
4052            conformance_headers: vec![],
4053            conformance_all_operations: false,
4054            conformance_custom: None,
4055            conformance_delay_ms: 0,
4056            use_k6: false,
4057            conformance_custom_filter: None,
4058            export_requests: false,
4059            validate_requests: false,
4060            conformance_self_test: false,
4061            conformance_self_test_capture: false,
4062            conformance_self_test_iterations: 1,
4063            conformance_self_test_duration: None,
4064            validate_response_schemas: false,
4065            source_ips: Vec::new(),
4066            geo_source_ips: Vec::new(),
4067            geo_source_headers: Vec::new(),
4068            report_missed_cap: None,
4069        };
4070
4071        let headers = cmd.parse_headers().unwrap();
4072        assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
4073        assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
4074    }
4075
4076    #[test]
4077    fn test_parse_header_string_preserves_comma_in_value() {
4078        // #761: with one header per --headers flag, a comma in the value (e.g. a
4079        // Cookie expiry date) is preserved instead of being split into junk pairs.
4080        let inputs = vec![
4081            "Cookie:session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string(),
4082            "X-Trace:1".to_string(),
4083        ];
4084        let headers = parse_header_string(&inputs).unwrap();
4085        assert_eq!(
4086            headers.get("Cookie"),
4087            Some(&"session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string())
4088        );
4089        assert_eq!(headers.get("X-Trace"), Some(&"1".to_string()));
4090    }
4091
4092    #[test]
4093    fn test_get_spec_display_name() {
4094        let cmd = BenchCommand {
4095            spec: vec![PathBuf::from("test.yaml")],
4096            spec_dir: None,
4097            merge_conflicts: "error".to_string(),
4098            spec_mode: "merge".to_string(),
4099            dependency_config: None,
4100            target: "http://localhost".to_string(),
4101            base_path: None,
4102            duration: "1m".to_string(),
4103            vus: 10,
4104            scenario: "ramp-up".to_string(),
4105            operations: None,
4106            exclude_operations: None,
4107            auth: None,
4108            headers: Vec::new(),
4109            output: PathBuf::from("output"),
4110            generate_only: false,
4111            script_output: None,
4112            threshold_percentile: "p(95)".to_string(),
4113            threshold_ms: 500,
4114            max_error_rate: 0.05,
4115            verbose: false,
4116            skip_tls_verify: false,
4117            chunked_request_bodies: false,
4118            target_rps: None,
4119            no_keep_alive: false,
4120            targets_file: None,
4121            max_concurrency: None,
4122            results_format: "both".to_string(),
4123            params_file: None,
4124            crud_flow: false,
4125            flow_config: None,
4126            extract_fields: None,
4127            parallel_create: None,
4128            data_file: None,
4129            data_distribution: "unique-per-vu".to_string(),
4130            data_mappings: None,
4131            per_uri_control: false,
4132            error_rate: None,
4133            error_types: None,
4134            security_test: false,
4135            security_payloads: None,
4136            security_categories: None,
4137            security_target_fields: None,
4138            wafbench_dir: None,
4139            wafbench_cycle_all: false,
4140            owasp_api_top10: false,
4141            owasp_categories: None,
4142            owasp_auth_header: "Authorization".to_string(),
4143            owasp_auth_token: None,
4144            owasp_admin_paths: None,
4145            owasp_id_fields: None,
4146            owasp_report: None,
4147            owasp_report_format: "json".to_string(),
4148            owasp_iterations: 1,
4149            conformance: false,
4150            conformance_api_key: None,
4151            conformance_basic_auth: None,
4152            conformance_report: PathBuf::from("conformance-report.json"),
4153            conformance_categories: None,
4154            conformance_report_format: "json".to_string(),
4155            conformance_headers: vec![],
4156            conformance_all_operations: false,
4157            conformance_custom: None,
4158            conformance_delay_ms: 0,
4159            use_k6: false,
4160            conformance_custom_filter: None,
4161            export_requests: false,
4162            validate_requests: false,
4163            conformance_self_test: false,
4164            conformance_self_test_capture: false,
4165            conformance_self_test_iterations: 1,
4166            conformance_self_test_duration: None,
4167            validate_response_schemas: false,
4168            source_ips: Vec::new(),
4169            geo_source_ips: Vec::new(),
4170            geo_source_headers: Vec::new(),
4171            report_missed_cap: None,
4172        };
4173
4174        assert_eq!(cmd.get_spec_display_name(), "test.yaml");
4175
4176        // Test multiple specs
4177        let cmd_multi = BenchCommand {
4178            spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
4179            spec_dir: None,
4180            merge_conflicts: "error".to_string(),
4181            spec_mode: "merge".to_string(),
4182            dependency_config: None,
4183            target: "http://localhost".to_string(),
4184            base_path: None,
4185            duration: "1m".to_string(),
4186            vus: 10,
4187            scenario: "ramp-up".to_string(),
4188            operations: None,
4189            exclude_operations: None,
4190            auth: None,
4191            headers: Vec::new(),
4192            output: PathBuf::from("output"),
4193            generate_only: false,
4194            script_output: None,
4195            threshold_percentile: "p(95)".to_string(),
4196            threshold_ms: 500,
4197            max_error_rate: 0.05,
4198            verbose: false,
4199            skip_tls_verify: false,
4200            chunked_request_bodies: false,
4201            target_rps: None,
4202            no_keep_alive: false,
4203            targets_file: None,
4204            max_concurrency: None,
4205            results_format: "both".to_string(),
4206            params_file: None,
4207            crud_flow: false,
4208            flow_config: None,
4209            extract_fields: None,
4210            parallel_create: None,
4211            data_file: None,
4212            data_distribution: "unique-per-vu".to_string(),
4213            data_mappings: None,
4214            per_uri_control: false,
4215            error_rate: None,
4216            error_types: None,
4217            security_test: false,
4218            security_payloads: None,
4219            security_categories: None,
4220            security_target_fields: None,
4221            wafbench_dir: None,
4222            wafbench_cycle_all: false,
4223            owasp_api_top10: false,
4224            owasp_categories: None,
4225            owasp_auth_header: "Authorization".to_string(),
4226            owasp_auth_token: None,
4227            owasp_admin_paths: None,
4228            owasp_id_fields: None,
4229            owasp_report: None,
4230            owasp_report_format: "json".to_string(),
4231            owasp_iterations: 1,
4232            conformance: false,
4233            conformance_api_key: None,
4234            conformance_basic_auth: None,
4235            conformance_report: PathBuf::from("conformance-report.json"),
4236            conformance_categories: None,
4237            conformance_report_format: "json".to_string(),
4238            conformance_headers: vec![],
4239            conformance_all_operations: false,
4240            conformance_custom: None,
4241            conformance_delay_ms: 0,
4242            use_k6: false,
4243            conformance_custom_filter: None,
4244            export_requests: false,
4245            validate_requests: false,
4246            conformance_self_test: false,
4247            conformance_self_test_capture: false,
4248            conformance_self_test_iterations: 1,
4249            conformance_self_test_duration: None,
4250            validate_response_schemas: false,
4251            source_ips: Vec::new(),
4252            geo_source_ips: Vec::new(),
4253            geo_source_headers: Vec::new(),
4254            report_missed_cap: None,
4255        };
4256
4257        assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
4258    }
4259
4260    #[test]
4261    fn test_parse_extracted_values_from_output_dir() {
4262        let dir = tempdir().unwrap();
4263        let path = dir.path().join("extracted_values.json");
4264        std::fs::write(
4265            &path,
4266            r#"{
4267  "pool_id": "abc123",
4268  "count": 0,
4269  "enabled": false,
4270  "metadata": { "owner": "team-a" }
4271}"#,
4272        )
4273        .unwrap();
4274
4275        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4276        assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
4277        assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
4278        assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
4279        assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
4280    }
4281
4282    #[test]
4283    fn test_parse_extracted_values_missing_file() {
4284        let dir = tempdir().unwrap();
4285        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4286        assert!(extracted.values.is_empty());
4287    }
4288}