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