Skip to main content

mockforge_bench/
command.rs

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