Skip to main content

mockforge_bench/
command.rs

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