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                // Round 64 (#79) — Srikanth on 0.3.210 ran
1051                //   mockforge bench --use-k6 --targets-file vs_list3.json \
1052                //     --conformance-basic-auth user:pass ...
1053                // and the credentials never reached the wire (absent from both
1054                // his PCAP and his proxy). Round 47 taught `parse_headers()` to
1055                // fold these auth shortcuts into the shared header map so the
1056                // same flags work for plain bench, but THIS clone nulled them
1057                // out before `ParallelExecutor` ever called `parse_headers()`.
1058                // Single-target worked; multi-target silently sent nothing.
1059                //
1060                // These three must survive the clone or the fold has nothing to
1061                // fold. `conformance_api_key` is still conformance-only by
1062                // design (see parse_headers), but is preserved so multi-target
1063                // emits the same "only fires under --conformance" warning as
1064                // single-target rather than swallowing the flag.
1065                conformance_api_key: self.conformance_api_key.clone(),
1066                conformance_basic_auth: self.conformance_basic_auth.clone(),
1067                conformance_report: PathBuf::from("conformance-report.json"),
1068                conformance_categories: None,
1069                conformance_report_format: "json".to_string(),
1070                // Also carries round 46's `--auth-bearer`, which CLI dispatch
1071                // injects here as `Authorization: Bearer ...`. Dropping it had
1072                // the same effect as dropping --conformance-basic-auth.
1073                conformance_headers: self.conformance_headers.clone(),
1074                conformance_all_operations: false,
1075                conformance_custom: None,
1076                conformance_delay_ms: 0,
1077                use_k6: false,
1078                conformance_custom_filter: None,
1079                export_requests: false,
1080                validate_requests: false,
1081                conformance_self_test: false,
1082                conformance_self_test_capture: false,
1083                conformance_self_test_iterations: 1,
1084                conformance_self_test_duration: None,
1085                validate_response_schemas: false,
1086                // Issue #79 r54 (Srikanth on 0.3.200): these were hardcoded to
1087                // empty, so `--source-ip` / `--geo-source-ip` were silently
1088                // dropped in multi-target (`--targets-file`) mode. Carry them
1089                // through to the ParallelExecutor so k6 gets `--local-ips`.
1090                source_ips: self.source_ips.clone(),
1091                geo_source_ips: self.geo_source_ips.clone(),
1092                geo_source_headers: self.geo_source_headers.clone(),
1093                report_missed_cap: None,
1094                // Round 57 (#79) — carry the flag through; the multi-target
1095                // plain-load path already discards by default (r56), so this
1096                // keeps the per-target command faithful to the parent.
1097                discard_response_bodies: self.discard_response_bodies,
1098                // Round 61 (#79) — carry the DNS policy so per-target k6 runs
1099                // resolve hostnames with the same IPv6/IPv4 preference.
1100                dns_policy: self.dns_policy.clone(),
1101            },
1102            targets,
1103            max_concurrency,
1104        );
1105
1106        // Execute all targets
1107        let start_time = std::time::Instant::now();
1108        let aggregated_results = executor.execute_all().await?;
1109        let elapsed = start_time.elapsed();
1110
1111        // Organize and report results
1112        self.report_multi_target_results(&aggregated_results, elapsed)?;
1113
1114        Ok(())
1115    }
1116
1117    /// Report results for multi-target execution
1118    fn report_multi_target_results(
1119        &self,
1120        results: &AggregatedResults,
1121        elapsed: std::time::Duration,
1122    ) -> Result<()> {
1123        // Print summary
1124        TerminalReporter::print_multi_target_summary(results);
1125
1126        // Print elapsed time
1127        let total_secs = elapsed.as_secs();
1128        let hours = total_secs / 3600;
1129        let minutes = (total_secs % 3600) / 60;
1130        let seconds = total_secs % 60;
1131        if hours > 0 {
1132            println!("\n  Total Elapsed Time:   {}h {}m {}s", hours, minutes, seconds);
1133        } else if minutes > 0 {
1134            println!("\n  Total Elapsed Time:   {}m {}s", minutes, seconds);
1135        } else {
1136            println!("\n  Total Elapsed Time:   {}s", seconds);
1137        }
1138
1139        // Save aggregated summary if requested
1140        if self.results_format == "aggregated" || self.results_format == "both" {
1141            let summary_path = self.output.join("aggregated_summary.json");
1142            let summary_json = serde_json::json!({
1143                "total_elapsed_seconds": elapsed.as_secs(),
1144                "total_targets": results.total_targets,
1145                "successful_targets": results.successful_targets,
1146                "failed_targets": results.failed_targets,
1147                "aggregated_metrics": {
1148                    "total_requests": results.aggregated_metrics.total_requests,
1149                    "total_failed_requests": results.aggregated_metrics.total_failed_requests,
1150                    "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
1151                    "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
1152                    "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
1153                    "error_rate": results.aggregated_metrics.error_rate,
1154                    "total_rps": results.aggregated_metrics.total_rps,
1155                    "avg_rps": results.aggregated_metrics.avg_rps,
1156                    "total_vus_max": results.aggregated_metrics.total_vus_max,
1157                },
1158                "target_results": results.target_results.iter().map(|r| {
1159                    serde_json::json!({
1160                        "target_url": r.target_url,
1161                        "target_index": r.target_index,
1162                        "success": r.success,
1163                        "error": r.error,
1164                        "total_requests": r.results.total_requests,
1165                        "failed_requests": r.results.failed_requests,
1166                        "avg_duration_ms": r.results.avg_duration_ms,
1167                        "min_duration_ms": r.results.min_duration_ms,
1168                        "med_duration_ms": r.results.med_duration_ms,
1169                        "p90_duration_ms": r.results.p90_duration_ms,
1170                        "p95_duration_ms": r.results.p95_duration_ms,
1171                        "p99_duration_ms": r.results.p99_duration_ms,
1172                        "max_duration_ms": r.results.max_duration_ms,
1173                        "rps": r.results.rps,
1174                        "vus_max": r.results.vus_max,
1175                        "output_dir": r.output_dir.to_string_lossy(),
1176                    })
1177                }).collect::<Vec<_>>(),
1178            });
1179
1180            std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
1181            TerminalReporter::print_success(&format!(
1182                "Aggregated summary saved to: {}",
1183                summary_path.display()
1184            ));
1185        }
1186
1187        // Write CSV with all per-target results for easy parsing
1188        let csv_path = self.output.join("all_targets.csv");
1189        let mut csv = String::from(
1190            "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
1191        );
1192        for r in &results.target_results {
1193            csv.push_str(&format!(
1194                "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
1195                r.target_url,
1196                r.success,
1197                r.results.total_requests,
1198                r.results.failed_requests,
1199                r.results.rps,
1200                r.results.vus_max,
1201                r.results.min_duration_ms,
1202                r.results.avg_duration_ms,
1203                r.results.med_duration_ms,
1204                r.results.p90_duration_ms,
1205                r.results.p95_duration_ms,
1206                r.results.p99_duration_ms,
1207                r.results.max_duration_ms,
1208                r.error.as_deref().unwrap_or(""),
1209            ));
1210        }
1211        let _ = std::fs::write(&csv_path, &csv);
1212
1213        println!("\nResults saved to: {}", self.output.display());
1214        println!("  - Per-target results: {}", self.output.join("target_*").display());
1215        println!("  - All targets CSV:    {}", csv_path.display());
1216        if self.results_format == "aggregated" || self.results_format == "both" {
1217            println!(
1218                "  - Aggregated summary: {}",
1219                self.output.join("aggregated_summary.json").display()
1220            );
1221        }
1222
1223        Ok(())
1224    }
1225
1226    /// Parse duration string (e.g., "30s", "5m", "1h") to seconds
1227    pub fn parse_duration(duration: &str) -> Result<u64> {
1228        let duration = duration.trim();
1229
1230        if let Some(secs) = duration.strip_suffix('s') {
1231            secs.parse::<u64>()
1232                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1233        } else if let Some(mins) = duration.strip_suffix('m') {
1234            mins.parse::<u64>()
1235                .map(|m| m * 60)
1236                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1237        } else if let Some(hours) = duration.strip_suffix('h') {
1238            hours
1239                .parse::<u64>()
1240                .map(|h| h * 3600)
1241                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1242        } else {
1243            // Try parsing as seconds without suffix
1244            duration
1245                .parse::<u64>()
1246                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1247        }
1248    }
1249
1250    /// Parse headers from the repeated `--headers "Key:Value"` flags.
1251    pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
1252        let mut headers = parse_header_string(&self.headers)?;
1253
1254        // Round 47 (#79) — Srikanth on 0.3.191 tried
1255        // `mockforge bench --conformance-basic-auth user:pass --spec ...`
1256        // (no `--conformance` flag) and the auth never landed because
1257        // the bench path only reads `self.headers`. Fold the
1258        // conformance auth shortcuts (basic, api-key, the round-46
1259        // --auth-bearer collected in `conformance_headers`) into the
1260        // shared header map so the SAME auth flags work whether the
1261        // run is plain bench, conformance, or self-test. De-dup
1262        // case-insensitively on header name; explicit
1263        // `--header "Authorization: ..."` keeps priority.
1264        let already_has = |hs: &HashMap<String, String>, name: &str| -> bool {
1265            hs.keys().any(|k| k.eq_ignore_ascii_case(name))
1266        };
1267
1268        if !already_has(&headers, "Authorization") {
1269            if let Some(b) = self.conformance_basic_auth.as_ref().filter(|s| !s.is_empty()) {
1270                use base64::Engine as _;
1271                let encoded = base64::engine::general_purpose::STANDARD.encode(b.as_bytes());
1272                headers.insert("Authorization".to_string(), format!("Basic {}", encoded));
1273            }
1274        }
1275
1276        // `--conformance-headers "Name: value"` was already conformance-
1277        // only; reuse the same flag for bench. Round 46's --auth-bearer
1278        // injects `Authorization: Bearer ...` into this list at CLI
1279        // dispatch time, so this single pass also picks up the bearer
1280        // token for plain bench.
1281        for line in &self.conformance_headers {
1282            let Some((name, value)) = line.split_once(':') else {
1283                continue;
1284            };
1285            let name = name.trim();
1286            let value = value.trim();
1287            if name.is_empty() || already_has(&headers, name) {
1288                continue;
1289            }
1290            headers.insert(name.to_string(), value.to_string());
1291        }
1292
1293        // `--conformance-api-key` is conformance-test-specific (probe
1294        // pattern over multiple header names). Forwarding it to plain
1295        // bench as a simple header would mislead a user expecting the
1296        // probe behaviour, so we leave that path conformance-only and
1297        // surface a one-line note when it's set without `--conformance`.
1298        if !self.conformance && self.conformance_api_key.is_some() {
1299            TerminalReporter::print_warning(
1300                "--conformance-api-key only fires under --conformance. For plain bench use --header 'X-API-Key: ...'.",
1301            );
1302        }
1303
1304        Ok(headers)
1305    }
1306
1307    fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
1308        let extracted_path = output_dir.join("extracted_values.json");
1309        if !extracted_path.exists() {
1310            return Ok(ExtractedValues::new());
1311        }
1312
1313        let content = std::fs::read_to_string(&extracted_path)
1314            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1315        let parsed: serde_json::Value = serde_json::from_str(&content)
1316            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1317
1318        let mut extracted = ExtractedValues::new();
1319        if let Some(values) = parsed.as_object() {
1320            for (key, value) in values {
1321                extracted.set(key.clone(), value.clone());
1322            }
1323        }
1324
1325        Ok(extracted)
1326    }
1327
1328    /// Resolve the effective base path for API endpoints
1329    ///
1330    /// Priority:
1331    /// 1. CLI --base-path option (if provided, even if empty string)
1332    /// 2. Base path extracted from OpenAPI spec's servers URL
1333    /// 3. None (no base path)
1334    ///
1335    /// An empty string from CLI explicitly disables base path.
1336    fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
1337        // CLI option takes priority (including empty string to disable)
1338        if let Some(cli_base_path) = &self.base_path {
1339            if cli_base_path.is_empty() {
1340                // Empty string explicitly means "no base path"
1341                return None;
1342            }
1343            return Some(cli_base_path.clone());
1344        }
1345
1346        // Fall back to spec's base path
1347        parser.get_base_path()
1348    }
1349
1350    /// Build mock server integration configuration
1351    async fn build_mock_config(&self) -> MockIntegrationConfig {
1352        // Check if target looks like a mock server
1353        if MockServerDetector::looks_like_mock_server(&self.target) {
1354            // Try to detect if it's actually a MockForge server
1355            if let Ok(info) = MockServerDetector::detect(&self.target).await {
1356                if info.is_mockforge {
1357                    TerminalReporter::print_success(&format!(
1358                        "Detected MockForge server (version: {})",
1359                        info.version.as_deref().unwrap_or("unknown")
1360                    ));
1361                    return MockIntegrationConfig::mock_server();
1362                }
1363            }
1364        }
1365        MockIntegrationConfig::real_api()
1366    }
1367
1368    /// Build CRUD flow configuration
1369    fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
1370        if !self.crud_flow {
1371            return None;
1372        }
1373
1374        // If flow_config file is provided, load it
1375        if let Some(config_path) = &self.flow_config {
1376            match CrudFlowConfig::from_file(config_path) {
1377                Ok(config) => return Some(config),
1378                Err(e) => {
1379                    TerminalReporter::print_warning(&format!(
1380                        "Failed to load flow config: {}. Using auto-detection.",
1381                        e
1382                    ));
1383                }
1384            }
1385        }
1386
1387        // Parse extract fields
1388        let extract_fields = self
1389            .extract_fields
1390            .as_ref()
1391            .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
1392            .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
1393
1394        Some(CrudFlowConfig {
1395            flows: Vec::new(), // Will be auto-detected
1396            default_extract_fields: extract_fields,
1397        })
1398    }
1399
1400    /// Build data-driven testing configuration
1401    fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
1402        let data_file = self.data_file.as_ref()?;
1403
1404        let distribution = DataDistribution::from_str(&self.data_distribution)
1405            .unwrap_or(DataDistribution::UniquePerVu);
1406
1407        let mappings = self
1408            .data_mappings
1409            .as_ref()
1410            .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
1411            .unwrap_or_default();
1412
1413        Some(DataDrivenConfig {
1414            file_path: data_file.to_string_lossy().to_string(),
1415            distribution,
1416            mappings,
1417            csv_has_header: true,
1418            per_uri_control: self.per_uri_control,
1419            per_uri_columns: crate::data_driven::PerUriColumns::default(),
1420        })
1421    }
1422
1423    /// Build invalid data testing configuration
1424    fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
1425        let error_rate = self.error_rate?;
1426
1427        let error_types = self
1428            .error_types
1429            .as_ref()
1430            .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
1431            .unwrap_or_default();
1432
1433        Some(InvalidDataConfig {
1434            error_rate,
1435            error_types,
1436            target_fields: Vec::new(),
1437        })
1438    }
1439
1440    /// Build security testing configuration
1441    fn build_security_config(&self) -> Option<SecurityTestConfig> {
1442        if !self.security_test {
1443            return None;
1444        }
1445
1446        let categories = self
1447            .security_categories
1448            .as_ref()
1449            .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1450            .unwrap_or_else(|| {
1451                let mut default = HashSet::new();
1452                default.insert(SecurityCategory::SqlInjection);
1453                default.insert(SecurityCategory::Xss);
1454                default
1455            });
1456
1457        let target_fields = self
1458            .security_target_fields
1459            .as_ref()
1460            .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1461            .unwrap_or_default();
1462
1463        let custom_payloads_file =
1464            self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1465
1466        Some(SecurityTestConfig {
1467            enabled: true,
1468            categories,
1469            target_fields,
1470            custom_payloads_file,
1471            include_high_risk: false,
1472        })
1473    }
1474
1475    /// Build parallel execution configuration
1476    fn build_parallel_config(&self) -> Option<ParallelConfig> {
1477        let count = self.parallel_create?;
1478
1479        Some(ParallelConfig::new(count))
1480    }
1481
1482    /// Load WAFBench payloads from the specified directory or pattern
1483    fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
1484        let Some(ref wafbench_dir) = self.wafbench_dir else {
1485            return Vec::new();
1486        };
1487
1488        let mut loader = WafBenchLoader::new();
1489
1490        if let Err(e) = loader.load_from_pattern(wafbench_dir) {
1491            TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
1492            return Vec::new();
1493        }
1494
1495        let stats = loader.stats();
1496
1497        if stats.files_processed == 0 {
1498            TerminalReporter::print_warning(&format!(
1499                "No WAFBench YAML files found matching '{}'",
1500                wafbench_dir
1501            ));
1502            // Also report any parse errors that may explain why no files were processed
1503            if !stats.parse_errors.is_empty() {
1504                TerminalReporter::print_warning("Some files were found but failed to parse:");
1505                for error in &stats.parse_errors {
1506                    TerminalReporter::print_warning(&format!("  - {}", error));
1507                }
1508            }
1509            return Vec::new();
1510        }
1511
1512        TerminalReporter::print_progress(&format!(
1513            "Loaded {} WAFBench files, {} test cases, {} payloads",
1514            stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1515        ));
1516
1517        // Print category breakdown
1518        for (category, count) in &stats.by_category {
1519            TerminalReporter::print_progress(&format!("  - {}: {} tests", category, count));
1520        }
1521
1522        // Report any parse errors
1523        for error in &stats.parse_errors {
1524            TerminalReporter::print_warning(&format!("  Parse error: {}", error));
1525        }
1526
1527        loader.to_security_payloads()
1528    }
1529
1530    /// Generate enhanced k6 script with advanced features
1531    pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1532        let mut enhanced_script = base_script.to_string();
1533        let mut additional_code = String::new();
1534
1535        // Add data-driven testing code
1536        if let Some(config) = self.build_data_driven_config() {
1537            TerminalReporter::print_progress("Adding data-driven testing support...");
1538            additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1539            additional_code.push('\n');
1540            TerminalReporter::print_success("Data-driven testing enabled");
1541        }
1542
1543        // Add invalid data generation code
1544        if let Some(config) = self.build_invalid_data_config() {
1545            TerminalReporter::print_progress("Adding invalid data testing support...");
1546            additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1547            additional_code.push('\n');
1548            additional_code
1549                .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1550            additional_code.push('\n');
1551            additional_code
1552                .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1553            additional_code.push('\n');
1554            TerminalReporter::print_success(&format!(
1555                "Invalid data testing enabled ({}% error rate)",
1556                (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1557            ));
1558        }
1559
1560        // Add security testing code
1561        let security_config = self.build_security_config();
1562        let wafbench_payloads = self.load_wafbench_payloads();
1563        let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1564
1565        if security_config.is_some() || !wafbench_payloads.is_empty() {
1566            TerminalReporter::print_progress("Adding security testing support...");
1567
1568            // Combine built-in payloads with WAFBench payloads
1569            let mut payload_list: Vec<SecurityPayload> = Vec::new();
1570
1571            if let Some(ref config) = security_config {
1572                payload_list.extend(SecurityPayloads::get_payloads(config));
1573            }
1574
1575            // Add WAFBench payloads
1576            if !wafbench_payloads.is_empty() {
1577                TerminalReporter::print_progress(&format!(
1578                    "Loading {} WAFBench attack patterns...",
1579                    wafbench_payloads.len()
1580                ));
1581                payload_list.extend(wafbench_payloads);
1582            }
1583
1584            let target_fields =
1585                security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1586
1587            additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1588                &payload_list,
1589                self.wafbench_cycle_all,
1590            ));
1591            additional_code.push('\n');
1592            additional_code
1593                .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1594            additional_code.push('\n');
1595            additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1596            additional_code.push('\n');
1597
1598            let mode = if self.wafbench_cycle_all {
1599                "cycle-all"
1600            } else {
1601                "random"
1602            };
1603            TerminalReporter::print_success(&format!(
1604                "Security testing enabled ({} payloads, {} mode)",
1605                payload_list.len(),
1606                mode
1607            ));
1608        } else if security_requested {
1609            // User requested security testing (e.g., --wafbench-dir) but no payloads were loaded.
1610            // The template has security_testing_enabled=true so it renders calling code.
1611            // We must inject stub definitions to avoid undefined function references.
1612            TerminalReporter::print_warning(
1613                "Security testing was requested but no payloads were loaded. \
1614                 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1615            );
1616            additional_code
1617                .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1618            additional_code.push('\n');
1619            additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1620            additional_code.push('\n');
1621        }
1622
1623        // Add parallel execution code
1624        if let Some(config) = self.build_parallel_config() {
1625            TerminalReporter::print_progress("Adding parallel execution support...");
1626            additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1627            additional_code.push('\n');
1628            TerminalReporter::print_success(&format!(
1629                "Parallel execution enabled (count: {})",
1630                config.count
1631            ));
1632        }
1633
1634        // Insert additional code after the imports section
1635        if !additional_code.is_empty() {
1636            // Find the end of the import section
1637            if let Some(import_end) = enhanced_script.find("export const options") {
1638                enhanced_script.insert_str(
1639                    import_end,
1640                    &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1641                );
1642            }
1643        }
1644
1645        Ok(enhanced_script)
1646    }
1647
1648    /// Execute specs sequentially with dependency ordering and value passing
1649    async fn execute_sequential_specs(&self) -> Result<()> {
1650        TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1651
1652        // Load all specs (without merging)
1653        let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1654
1655        if !self.spec.is_empty() {
1656            let specs = load_specs_from_files(self.spec.clone())
1657                .await
1658                .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1659            all_specs.extend(specs);
1660        }
1661
1662        if let Some(spec_dir) = &self.spec_dir {
1663            let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1664                BenchError::Other(format!("Failed to load specs from directory: {}", e))
1665            })?;
1666            all_specs.extend(dir_specs);
1667        }
1668
1669        if all_specs.is_empty() {
1670            return Err(BenchError::Other(
1671                "No spec files found for sequential execution".to_string(),
1672            ));
1673        }
1674
1675        TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1676
1677        // Load dependency config or auto-detect
1678        let execution_order = if let Some(config_path) = &self.dependency_config {
1679            TerminalReporter::print_progress("Loading dependency configuration...");
1680            let config = SpecDependencyConfig::from_file(config_path)?;
1681
1682            if !config.disable_auto_detect && config.execution_order.is_empty() {
1683                // Auto-detect if config doesn't specify order
1684                self.detect_and_sort_specs(&all_specs)?
1685            } else {
1686                // Use configured order
1687                config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1688            }
1689        } else {
1690            // Auto-detect dependencies
1691            self.detect_and_sort_specs(&all_specs)?
1692        };
1693
1694        TerminalReporter::print_success(&format!(
1695            "Execution order: {}",
1696            execution_order
1697                .iter()
1698                .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1699                .collect::<Vec<_>>()
1700                .join(" → ")
1701        ));
1702
1703        // Execute each spec in order
1704        let mut extracted_values = ExtractedValues::new();
1705        let total_specs = execution_order.len();
1706
1707        for (index, spec_path) in execution_order.iter().enumerate() {
1708            let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1709
1710            TerminalReporter::print_progress(&format!(
1711                "[{}/{}] Executing spec: {}",
1712                index + 1,
1713                total_specs,
1714                spec_name
1715            ));
1716
1717            // Find the spec in our loaded specs (match by full path or filename)
1718            let spec = all_specs
1719                .iter()
1720                .find(|(p, _)| {
1721                    p == spec_path
1722                        || p.file_name() == spec_path.file_name()
1723                        || p.file_name() == Some(spec_path.as_os_str())
1724                })
1725                .map(|(_, s)| s.clone())
1726                .ok_or_else(|| {
1727                    BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1728                })?;
1729
1730            // Execute this spec with any extracted values from previous specs
1731            let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1732
1733            // Merge extracted values for the next spec
1734            extracted_values.merge(&new_values);
1735
1736            TerminalReporter::print_success(&format!(
1737                "[{}/{}] Completed: {} (extracted {} values)",
1738                index + 1,
1739                total_specs,
1740                spec_name,
1741                new_values.values.len()
1742            ));
1743        }
1744
1745        TerminalReporter::print_success(&format!(
1746            "Sequential execution complete: {} specs executed",
1747            total_specs
1748        ));
1749
1750        Ok(())
1751    }
1752
1753    /// Detect dependencies and return topologically sorted spec paths
1754    fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1755        TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1756
1757        let mut detector = DependencyDetector::new();
1758        let dependencies = detector.detect_dependencies(specs);
1759
1760        if dependencies.is_empty() {
1761            TerminalReporter::print_progress("No dependencies detected, using file order");
1762            return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1763        }
1764
1765        TerminalReporter::print_progress(&format!(
1766            "Detected {} cross-spec dependencies",
1767            dependencies.len()
1768        ));
1769
1770        for dep in &dependencies {
1771            TerminalReporter::print_progress(&format!(
1772                "  {} → {} (via field '{}')",
1773                dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1774                dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1775                dep.field_name
1776            ));
1777        }
1778
1779        topological_sort(specs, &dependencies)
1780    }
1781
1782    /// Execute a single spec and extract values for dependent specs
1783    async fn execute_single_spec(
1784        &self,
1785        spec: &OpenApiSpec,
1786        spec_name: &str,
1787        _external_values: &ExtractedValues,
1788    ) -> Result<ExtractedValues> {
1789        let parser = SpecParser::from_spec(spec.clone());
1790
1791        // For now, we execute in CRUD flow mode if enabled, otherwise standard mode
1792        if self.crud_flow {
1793            // Execute CRUD flow and extract values
1794            self.execute_crud_flow_with_extraction(&parser, spec_name).await
1795        } else {
1796            // Execute standard benchmark (no value extraction in non-CRUD mode)
1797            self.execute_standard_spec(&parser, spec_name).await?;
1798            Ok(ExtractedValues::new())
1799        }
1800    }
1801
1802    /// Execute CRUD flow with value extraction for sequential mode
1803    async fn execute_crud_flow_with_extraction(
1804        &self,
1805        parser: &SpecParser,
1806        spec_name: &str,
1807    ) -> Result<ExtractedValues> {
1808        let operations = parser.get_operations();
1809        let flows = CrudFlowDetector::detect_flows(&operations);
1810
1811        if flows.is_empty() {
1812            TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1813            return Ok(ExtractedValues::new());
1814        }
1815
1816        TerminalReporter::print_progress(&format!(
1817            "  {} CRUD flow(s) in {}",
1818            flows.len(),
1819            spec_name
1820        ));
1821
1822        // Generate and execute the CRUD flow script
1823        let mut handlebars = handlebars::Handlebars::new();
1824        // Register json helper for serializing arrays/objects in templates
1825        handlebars.register_helper(
1826            "json",
1827            Box::new(
1828                |h: &handlebars::Helper,
1829                 _: &handlebars::Handlebars,
1830                 _: &handlebars::Context,
1831                 _: &mut handlebars::RenderContext,
1832                 out: &mut dyn handlebars::Output|
1833                 -> handlebars::HelperResult {
1834                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1835                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1836                    Ok(())
1837                },
1838            ),
1839        );
1840        let template = include_str!("templates/k6_crud_flow.hbs");
1841        let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1842
1843        let custom_headers = self.parse_headers()?;
1844        let config = self.build_crud_flow_config().unwrap_or_default();
1845
1846        // Load parameter overrides if provided (for body configurations)
1847        let param_overrides = if let Some(params_file) = &self.params_file {
1848            let overrides = ParameterOverrides::from_file(params_file)?;
1849            Some(overrides)
1850        } else {
1851            None
1852        };
1853
1854        // Generate stages from scenario
1855        let duration_secs = Self::parse_duration(&self.duration)?;
1856        let scenario =
1857            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1858        let stages = scenario.generate_stages(duration_secs, self.vus);
1859
1860        // Resolve base path (CLI option takes priority over spec's servers URL)
1861        let api_base_path = self.resolve_base_path(parser);
1862
1863        // Build headers JSON string for the template
1864        let mut all_headers = custom_headers.clone();
1865        if let Some(auth) = &self.auth {
1866            all_headers.insert("Authorization".to_string(), auth.clone());
1867        }
1868        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1869
1870        // Track all dynamic placeholders across all operations
1871        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1872
1873        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1874            // Use the metric-name sanitizer (caps at 112 chars + hash suffix)
1875            // so deeply nested flow names don't blow past k6's 128-char limit
1876            // when concatenated with `_step{i}_latency`. See issue #79.
1877            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1878            serde_json::json!({
1879                "name": sanitized_name.clone(),
1880                "display_name": f.name,
1881                "base_path": f.base_path,
1882                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1883                    // Parse operation to get method and path
1884                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1885                    let method_raw = if !parts.is_empty() {
1886                        parts[0].to_uppercase()
1887                    } else {
1888                        "GET".to_string()
1889                    };
1890                    let method = if !parts.is_empty() {
1891                        let m = parts[0].to_lowercase();
1892                        // k6 uses 'del' for DELETE
1893                        if m == "delete" { "del".to_string() } else { m }
1894                    } else {
1895                        "get".to_string()
1896                    };
1897                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1898                    // Prepend API base path if configured
1899                    let path = if let Some(ref bp) = api_base_path {
1900                        format!("{}{}", bp, raw_path)
1901                    } else {
1902                        raw_path.to_string()
1903                    };
1904                    let is_get_or_head = method == "get" || method == "head";
1905                    // POST, PUT, PATCH typically have bodies
1906                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1907
1908                    // Look up body from params file if available
1909                    let body_value = if has_body {
1910                        param_overrides.as_ref()
1911                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1912                            .and_then(|oo| oo.body)
1913                            .unwrap_or_else(|| serde_json::json!({}))
1914                    } else {
1915                        serde_json::json!({})
1916                    };
1917
1918                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
1919                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1920
1921                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
1922                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1923                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1924
1925                    serde_json::json!({
1926                        "operation": s.operation,
1927                        "method": method,
1928                        "path": path,
1929                        "extract": s.extract,
1930                        "use_values": s.use_values,
1931                        "use_body": s.use_body,
1932                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1933                        "inject_attacks": s.inject_attacks,
1934                        "attack_types": s.attack_types,
1935                        "description": s.description,
1936                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1937                        "is_get_or_head": is_get_or_head,
1938                        "has_body": has_body,
1939                        "body": processed_body.value,
1940                        "body_is_dynamic": body_is_dynamic,
1941                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1942                    })
1943                }).collect::<Vec<_>>(),
1944            })
1945        }).collect();
1946
1947        // Collect all placeholders from all steps
1948        for flow_data in &flows_data {
1949            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1950                for step in steps {
1951                    if let Some(placeholders_arr) =
1952                        step.get("_placeholders").and_then(|p| p.as_array())
1953                    {
1954                        for p_str in placeholders_arr {
1955                            if let Some(p_name) = p_str.as_str() {
1956                                match p_name {
1957                                    "VU" => {
1958                                        all_placeholders.insert(DynamicPlaceholder::VU);
1959                                    }
1960                                    "Iteration" => {
1961                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
1962                                    }
1963                                    "Timestamp" => {
1964                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
1965                                    }
1966                                    "UUID" => {
1967                                        all_placeholders.insert(DynamicPlaceholder::UUID);
1968                                    }
1969                                    "Random" => {
1970                                        all_placeholders.insert(DynamicPlaceholder::Random);
1971                                    }
1972                                    "Counter" => {
1973                                        all_placeholders.insert(DynamicPlaceholder::Counter);
1974                                    }
1975                                    "Date" => {
1976                                        all_placeholders.insert(DynamicPlaceholder::Date);
1977                                    }
1978                                    "VuIter" => {
1979                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
1980                                    }
1981                                    _ => {}
1982                                }
1983                            }
1984                        }
1985                    }
1986                }
1987            }
1988        }
1989
1990        // Get required imports and globals based on placeholders used
1991        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1992        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1993
1994        // Check if security testing is enabled
1995        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1996
1997        let data = serde_json::json!({
1998            "base_url": self.target,
1999            "flows": flows_data,
2000            "extract_fields": config.default_extract_fields,
2001            "duration_secs": duration_secs,
2002            "max_vus": self.vus,
2003            "auth_header": self.auth,
2004            "custom_headers": custom_headers,
2005            "skip_tls_verify": self.skip_tls_verify,
2006            // Add missing template fields
2007            "stages": stages.iter().map(|s| serde_json::json!({
2008                "duration": s.duration,
2009                "target": s.target,
2010            })).collect::<Vec<_>>(),
2011            "threshold_percentile": self.threshold_percentile,
2012            "threshold_ms": self.threshold_ms,
2013            "max_error_rate": self.max_error_rate,
2014            "abort_on_error": self.abort_on_error,
2015            "abort_on_error_rate": self.abort_on_error_rate,
2016            "headers": headers_json,
2017            "dynamic_imports": required_imports,
2018            "dynamic_globals": required_globals,
2019            "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
2020            // Security testing settings
2021            "security_testing_enabled": security_testing_enabled,
2022            "has_custom_headers": !custom_headers.is_empty(),
2023        });
2024
2025        let mut script = handlebars
2026            .render_template(template, &data)
2027            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2028
2029        // Enhance script with security testing support if enabled
2030        if security_testing_enabled {
2031            script = self.generate_enhanced_script(&script)?;
2032        }
2033
2034        // Write and execute script
2035        let script_path =
2036            self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
2037
2038        std::fs::create_dir_all(self.output.clone())?;
2039        std::fs::write(&script_path, &script)?;
2040
2041        if !self.generate_only {
2042            let executor = K6Executor::new()?
2043                .with_local_ips(self.source_ips.join(","))
2044                .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
2045            std::fs::create_dir_all(&output_dir)?;
2046
2047            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2048
2049            let extracted = Self::parse_extracted_values(&output_dir)?;
2050            TerminalReporter::print_progress(&format!(
2051                "  Extracted {} value(s) from {}",
2052                extracted.values.len(),
2053                spec_name
2054            ));
2055            return Ok(extracted);
2056        }
2057
2058        Ok(ExtractedValues::new())
2059    }
2060
2061    /// Execute standard (non-CRUD) spec benchmark
2062    async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
2063        let mut operations = if let Some(filter) = &self.operations {
2064            parser.filter_operations(filter)?
2065        } else {
2066            parser.get_operations()
2067        };
2068
2069        if let Some(exclude) = &self.exclude_operations {
2070            operations = parser.exclude_operations(operations, exclude)?;
2071        }
2072
2073        if operations.is_empty() {
2074            TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
2075            return Ok(());
2076        }
2077
2078        TerminalReporter::print_progress(&format!(
2079            "  {} operations in {}",
2080            operations.len(),
2081            spec_name
2082        ));
2083
2084        // Generate request templates
2085        let templates: Vec<_> = operations
2086            .iter()
2087            .map(RequestGenerator::generate_template)
2088            .collect::<Result<Vec<_>>>()?;
2089
2090        // Parse headers
2091        let custom_headers = self.parse_headers()?;
2092
2093        // Resolve base path
2094        let base_path = self.resolve_base_path(parser);
2095
2096        // Generate k6 script
2097        let scenario =
2098            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2099
2100        let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
2101
2102        let k6_config = K6Config {
2103            target_url: self.target.clone(),
2104            base_path,
2105            scenario,
2106            duration_secs: Self::parse_duration(&self.duration)?,
2107            max_vus: self.vus,
2108            threshold_percentile: self.threshold_percentile.clone(),
2109            threshold_ms: self.threshold_ms,
2110            max_error_rate: self.max_error_rate,
2111            auth_header: self.auth.clone(),
2112            custom_headers,
2113            skip_tls_verify: self.skip_tls_verify,
2114            security_testing_enabled,
2115            chunked_request_bodies: self.chunked_request_bodies,
2116            target_rps: self.target_rps,
2117            no_keep_alive: self.no_keep_alive,
2118            // Round 22.3 — see other K6Config site above.
2119            geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
2120                .into_iter()
2121                .map(|ip| ip.to_string())
2122                .collect(),
2123            geo_source_headers: if self.geo_source_headers.is_empty()
2124                && !self.geo_source_ips.is_empty()
2125            {
2126                crate::conformance::self_test::default_geo_source_headers()
2127            } else {
2128                self.geo_source_headers.clone()
2129            },
2130        };
2131
2132        let generator = K6ScriptGenerator::new(k6_config, templates)
2133            .with_abort_valve(self.abort_on_error, self.abort_on_error_rate);
2134        let mut script = generator.generate()?;
2135
2136        // Enhance script with advanced features (security testing, etc.)
2137        let has_advanced_features = self.data_file.is_some()
2138            || self.error_rate.is_some()
2139            || self.security_test
2140            || self.parallel_create.is_some()
2141            || self.wafbench_dir.is_some();
2142
2143        if has_advanced_features {
2144            script = self.generate_enhanced_script(&script)?;
2145        }
2146
2147        // Write and execute script
2148        let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
2149
2150        std::fs::create_dir_all(self.output.clone())?;
2151        std::fs::write(&script_path, &script)?;
2152
2153        if !self.generate_only {
2154            // Round 57 (#79) — honour `--discard-response-bodies` on the
2155            // standard (status-only) load path too.
2156            let executor = K6Executor::new()?
2157                .with_local_ips(self.source_ips.join(","))
2158                .with_dns_policy(self.dns_policy.clone().unwrap_or_default())
2159                .with_discard_response_bodies(self.discard_response_bodies);
2160            let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
2161            std::fs::create_dir_all(&output_dir)?;
2162
2163            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2164        }
2165
2166        Ok(())
2167    }
2168
2169    /// Execute CRUD flow testing mode
2170    async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
2171        // Check if a custom flow config is provided
2172        let config = self.build_crud_flow_config().unwrap_or_default();
2173
2174        // Use flows from config if provided, otherwise auto-detect
2175        let flows = if !config.flows.is_empty() {
2176            TerminalReporter::print_progress("Using custom flow configuration...");
2177            config.flows.clone()
2178        } else {
2179            TerminalReporter::print_progress("Detecting CRUD operations...");
2180            let operations = parser.get_operations();
2181            CrudFlowDetector::detect_flows(&operations)
2182        };
2183
2184        if flows.is_empty() {
2185            return Err(BenchError::Other(
2186                "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
2187            ));
2188        }
2189
2190        if config.flows.is_empty() {
2191            TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
2192        } else {
2193            TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
2194        }
2195
2196        for flow in &flows {
2197            TerminalReporter::print_progress(&format!(
2198                "  - {}: {} steps",
2199                flow.name,
2200                flow.steps.len()
2201            ));
2202        }
2203
2204        // Generate CRUD flow script
2205        let mut handlebars = handlebars::Handlebars::new();
2206        // Register json helper for serializing arrays/objects in templates
2207        handlebars.register_helper(
2208            "json",
2209            Box::new(
2210                |h: &handlebars::Helper,
2211                 _: &handlebars::Handlebars,
2212                 _: &handlebars::Context,
2213                 _: &mut handlebars::RenderContext,
2214                 out: &mut dyn handlebars::Output|
2215                 -> handlebars::HelperResult {
2216                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
2217                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
2218                    Ok(())
2219                },
2220            ),
2221        );
2222        let template = include_str!("templates/k6_crud_flow.hbs");
2223
2224        let custom_headers = self.parse_headers()?;
2225
2226        // Load parameter overrides if provided (for body configurations)
2227        let param_overrides = if let Some(params_file) = &self.params_file {
2228            TerminalReporter::print_progress("Loading parameter overrides...");
2229            let overrides = ParameterOverrides::from_file(params_file)?;
2230            TerminalReporter::print_success(&format!(
2231                "Loaded parameter overrides ({} operation-specific, {} defaults)",
2232                overrides.operations.len(),
2233                if overrides.defaults.is_empty() { 0 } else { 1 }
2234            ));
2235            Some(overrides)
2236        } else {
2237            None
2238        };
2239
2240        // Generate stages from scenario
2241        let duration_secs = Self::parse_duration(&self.duration)?;
2242        let scenario =
2243            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2244        let stages = scenario.generate_stages(duration_secs, self.vus);
2245
2246        // Resolve base path (CLI option takes priority over spec's servers URL)
2247        let api_base_path = self.resolve_base_path(parser);
2248        if let Some(ref bp) = api_base_path {
2249            TerminalReporter::print_progress(&format!("Using base path: {}", bp));
2250        }
2251
2252        // Build headers JSON string for the template
2253        let mut all_headers = custom_headers.clone();
2254        if let Some(auth) = &self.auth {
2255            all_headers.insert("Authorization".to_string(), auth.clone());
2256        }
2257        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
2258
2259        // Track all dynamic placeholders across all operations
2260        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
2261
2262        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
2263            // Sanitize flow name for use as JavaScript variable and k6 metric names.
2264            // Use the metric-name sanitizer (caps at 112 chars + hash suffix) so
2265            // deeply nested flow names don't blow past k6's 128-char limit when
2266            // concatenated with `_step{i}_latency`. See issue #79.
2267            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
2268            serde_json::json!({
2269                "name": sanitized_name.clone(),  // Use sanitized name for variable names
2270                "display_name": f.name,          // Keep original for comments/display
2271                "base_path": f.base_path,
2272                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
2273                    // Parse operation to get method and path
2274                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
2275                    let method_raw = if !parts.is_empty() {
2276                        parts[0].to_uppercase()
2277                    } else {
2278                        "GET".to_string()
2279                    };
2280                    let method = if !parts.is_empty() {
2281                        let m = parts[0].to_lowercase();
2282                        // k6 uses 'del' for DELETE
2283                        if m == "delete" { "del".to_string() } else { m }
2284                    } else {
2285                        "get".to_string()
2286                    };
2287                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
2288                    // Prepend API base path if configured
2289                    let path = if let Some(ref bp) = api_base_path {
2290                        format!("{}{}", bp, raw_path)
2291                    } else {
2292                        raw_path.to_string()
2293                    };
2294                    let is_get_or_head = method == "get" || method == "head";
2295                    // POST, PUT, PATCH typically have bodies
2296                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
2297
2298                    // Look up body from params file if available (use raw_path for matching)
2299                    let body_value = if has_body {
2300                        param_overrides.as_ref()
2301                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
2302                            .and_then(|oo| oo.body)
2303                            .unwrap_or_else(|| serde_json::json!({}))
2304                    } else {
2305                        serde_json::json!({})
2306                    };
2307
2308                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
2309                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
2310                    // Note: all_placeholders is captured by the closure but we can't mutate it directly
2311                    // We'll collect placeholders separately below
2312
2313                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
2314                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
2315                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
2316
2317                    serde_json::json!({
2318                        "operation": s.operation,
2319                        "method": method,
2320                        "path": path,
2321                        "extract": s.extract,
2322                        "use_values": s.use_values,
2323                        "use_body": s.use_body,
2324                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
2325                        "inject_attacks": s.inject_attacks,
2326                        "attack_types": s.attack_types,
2327                        "description": s.description,
2328                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
2329                        "is_get_or_head": is_get_or_head,
2330                        "has_body": has_body,
2331                        "body": processed_body.value,
2332                        "body_is_dynamic": body_is_dynamic,
2333                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
2334                    })
2335                }).collect::<Vec<_>>(),
2336            })
2337        }).collect();
2338
2339        // Collect all placeholders from all steps
2340        for flow_data in &flows_data {
2341            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
2342                for step in steps {
2343                    if let Some(placeholders_arr) =
2344                        step.get("_placeholders").and_then(|p| p.as_array())
2345                    {
2346                        for p_str in placeholders_arr {
2347                            if let Some(p_name) = p_str.as_str() {
2348                                // Parse placeholder from debug string
2349                                match p_name {
2350                                    "VU" => {
2351                                        all_placeholders.insert(DynamicPlaceholder::VU);
2352                                    }
2353                                    "Iteration" => {
2354                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
2355                                    }
2356                                    "Timestamp" => {
2357                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
2358                                    }
2359                                    "UUID" => {
2360                                        all_placeholders.insert(DynamicPlaceholder::UUID);
2361                                    }
2362                                    "Random" => {
2363                                        all_placeholders.insert(DynamicPlaceholder::Random);
2364                                    }
2365                                    "Counter" => {
2366                                        all_placeholders.insert(DynamicPlaceholder::Counter);
2367                                    }
2368                                    "Date" => {
2369                                        all_placeholders.insert(DynamicPlaceholder::Date);
2370                                    }
2371                                    "VuIter" => {
2372                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
2373                                    }
2374                                    _ => {}
2375                                }
2376                            }
2377                        }
2378                    }
2379                }
2380            }
2381        }
2382
2383        // Get required imports and globals based on placeholders used
2384        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
2385        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
2386
2387        // Build invalid data config if error injection is enabled
2388        let invalid_data_config = self.build_invalid_data_config();
2389        let error_injection_enabled = invalid_data_config.is_some();
2390        let error_rate = self.error_rate.unwrap_or(0.0);
2391        let error_types: Vec<String> = invalid_data_config
2392            .as_ref()
2393            .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
2394            .unwrap_or_default();
2395
2396        if error_injection_enabled {
2397            TerminalReporter::print_progress(&format!(
2398                "Error injection enabled ({}% rate)",
2399                (error_rate * 100.0) as u32
2400            ));
2401        }
2402
2403        // Check if security testing is enabled
2404        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
2405
2406        let data = serde_json::json!({
2407            "base_url": self.target,
2408            "flows": flows_data,
2409            "extract_fields": config.default_extract_fields,
2410            "duration_secs": duration_secs,
2411            "max_vus": self.vus,
2412            "auth_header": self.auth,
2413            "custom_headers": custom_headers,
2414            "skip_tls_verify": self.skip_tls_verify,
2415            // Add missing template fields
2416            "stages": stages.iter().map(|s| serde_json::json!({
2417                "duration": s.duration,
2418                "target": s.target,
2419            })).collect::<Vec<_>>(),
2420            "threshold_percentile": self.threshold_percentile,
2421            "threshold_ms": self.threshold_ms,
2422            "max_error_rate": self.max_error_rate,
2423            "abort_on_error": self.abort_on_error,
2424            "abort_on_error_rate": self.abort_on_error_rate,
2425            "headers": headers_json,
2426            "dynamic_imports": required_imports,
2427            "dynamic_globals": required_globals,
2428            "extracted_values_output_path": self
2429                .output
2430                .join("crud_flow_extracted_values.json")
2431                .to_string_lossy(),
2432            // Error injection settings
2433            "error_injection_enabled": error_injection_enabled,
2434            "error_rate": error_rate,
2435            "error_types": error_types,
2436            // Security testing settings
2437            "security_testing_enabled": security_testing_enabled,
2438            "has_custom_headers": !custom_headers.is_empty(),
2439        });
2440
2441        let mut script = handlebars
2442            .render_template(template, &data)
2443            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2444
2445        // Enhance script with security testing support if enabled
2446        if security_testing_enabled {
2447            script = self.generate_enhanced_script(&script)?;
2448        }
2449
2450        // Validate the generated CRUD flow script
2451        TerminalReporter::print_progress("Validating CRUD flow script...");
2452        let validation_errors = K6ScriptGenerator::validate_script(&script);
2453        if !validation_errors.is_empty() {
2454            TerminalReporter::print_error("CRUD flow script validation failed");
2455            for error in &validation_errors {
2456                eprintln!("  {}", error);
2457            }
2458            return Err(BenchError::Other(format!(
2459                "CRUD flow script validation failed with {} error(s)",
2460                validation_errors.len()
2461            )));
2462        }
2463
2464        TerminalReporter::print_success("CRUD flow script generated");
2465
2466        // Write and execute script
2467        let script_path = if let Some(output) = &self.script_output {
2468            output.clone()
2469        } else {
2470            self.output.join("k6-crud-flow-script.js")
2471        };
2472
2473        if let Some(parent) = script_path.parent() {
2474            std::fs::create_dir_all(parent)?;
2475        }
2476        std::fs::write(&script_path, &script)?;
2477        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2478
2479        if self.generate_only {
2480            println!("\nScript generated successfully. Run it with:");
2481            println!("  k6 run {}", script_path.display());
2482            return Ok(());
2483        }
2484
2485        // Execute k6
2486        TerminalReporter::print_progress("Executing CRUD flow test...");
2487        let executor = K6Executor::new()?
2488            .with_local_ips(self.source_ips.join(","))
2489            .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
2490        std::fs::create_dir_all(&self.output)?;
2491
2492        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2493
2494        let duration_secs = Self::parse_duration(&self.duration)?;
2495        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2496
2497        Ok(())
2498    }
2499
2500    /// Execute OpenAPI 3.0.0 conformance testing mode
2501    async fn execute_conformance_test(&self) -> Result<()> {
2502        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2503        use crate::conformance::report::ConformanceReport;
2504        use crate::conformance::spec::ConformanceFeature;
2505
2506        TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2507
2508        // Conformance testing is a functional correctness check (1 VU, 1 iteration).
2509        // --vus and -d flags are always ignored in this mode.
2510        TerminalReporter::print_progress(
2511            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2512        );
2513
2514        // Parse category filter
2515        let categories = self.conformance_categories.as_ref().map(|cats_str| {
2516            cats_str
2517                .split(',')
2518                .filter_map(|s| {
2519                    let trimmed = s.trim();
2520                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2521                        Some(canonical.to_string())
2522                    } else {
2523                        TerminalReporter::print_warning(&format!(
2524                            "Unknown conformance category: '{}'. Valid categories: {}",
2525                            trimmed,
2526                            ConformanceFeature::cli_category_names()
2527                                .iter()
2528                                .map(|(cli, _)| *cli)
2529                                .collect::<Vec<_>>()
2530                                .join(", ")
2531                        ));
2532                        None
2533                    }
2534                })
2535                .collect::<Vec<String>>()
2536        });
2537
2538        // Parse custom headers from "Key: Value" format
2539        let custom_headers: Vec<(String, String)> = self
2540            .conformance_headers
2541            .iter()
2542            .filter_map(|h| {
2543                let (name, value) = h.split_once(':')?;
2544                Some((name.trim().to_string(), value.trim().to_string()))
2545            })
2546            .collect();
2547
2548        if !custom_headers.is_empty() {
2549            TerminalReporter::print_progress(&format!(
2550                "Using {} custom header(s) for authentication",
2551                custom_headers.len()
2552            ));
2553        }
2554
2555        if self.conformance_delay_ms > 0 {
2556            TerminalReporter::print_progress(&format!(
2557                "Using {}ms delay between conformance requests",
2558                self.conformance_delay_ms
2559            ));
2560        }
2561
2562        // Ensure output dir exists so canonicalize works for the report path
2563        std::fs::create_dir_all(&self.output)?;
2564
2565        let config = ConformanceConfig {
2566            target_url: self.target.clone(),
2567            api_key: self.conformance_api_key.clone(),
2568            basic_auth: self.conformance_basic_auth.clone(),
2569            skip_tls_verify: self.skip_tls_verify,
2570            categories,
2571            base_path: self.base_path.clone(),
2572            custom_headers,
2573            output_dir: Some(self.output.clone()),
2574            all_operations: self.conformance_all_operations,
2575            custom_checks_file: self.conformance_custom.clone(),
2576            request_delay_ms: self.conformance_delay_ms,
2577            custom_filter: self.conformance_custom_filter.clone(),
2578            export_requests: self.export_requests,
2579            validate_requests: self.validate_requests,
2580        };
2581
2582        // Branch: spec-driven mode vs reference mode
2583        // Annotate operations if spec is provided (used by both native and k6 paths)
2584        // Round 18.1 — resolve the spec's base path so the self-test
2585        // path can prepend it to every URL. Pre-fix, self-test
2586        // ignored `--base-path /api` and hit the bare spec path,
2587        // returning 404 for every request on specs whose server is
2588        // proxied behind a base prefix.
2589        let mut resolved_base_path: Option<String> = None;
2590        let annotated_ops = if !self.spec.is_empty() {
2591            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2592            let parser = SpecParser::from_file(&self.spec[0]).await?;
2593            resolved_base_path = self.resolve_base_path(&parser);
2594
2595            // Issue #79 round 12 — Srikanth ran `--conformance --operations "GET,POST"`
2596            // and saw DELETE/PATCH exercised anyway. Conformance silently ignored
2597            // the filter. Apply it (and `--exclude-operations`) the same way the
2598            // regular bench path does so users can scope the run.
2599            let mut operations = if let Some(filter) = &self.operations {
2600                parser.filter_operations(filter)?
2601            } else {
2602                parser.get_operations()
2603            };
2604            if let Some(exclude) = &self.exclude_operations {
2605                let before_count = operations.len();
2606                operations = parser.exclude_operations(operations, exclude)?;
2607                let excluded_count = before_count - operations.len();
2608                if excluded_count > 0 {
2609                    TerminalReporter::print_progress(&format!(
2610                        "Excluded {} operations matching '{}'",
2611                        excluded_count, exclude
2612                    ));
2613                }
2614            }
2615
2616            let annotated =
2617                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2618                    &operations,
2619                    parser.spec(),
2620                );
2621            TerminalReporter::print_success(&format!(
2622                "Analyzed {} operations, found {} feature annotations",
2623                operations.len(),
2624                annotated.iter().map(|a| a.features.len()).sum::<usize>()
2625            ));
2626            Some(annotated)
2627        } else {
2628            None
2629        };
2630
2631        // Issue #79 round 13 (4) — `--conformance-self-test` replaces
2632        // the standard conformance run with a positive + per-category
2633        // negative driver that verifies the server actually rejects
2634        // bad requests with 4xx. Wires the spec-annotated operations
2635        // through `conformance::self_test::run_self_test` and prints
2636        // the resulting pass/fail matrix.
2637        if self.conformance_self_test {
2638            let Some(ops) = annotated_ops else {
2639                TerminalReporter::print_error(
2640                    "--conformance-self-test requires --spec; no operations to test",
2641                );
2642                return Ok(());
2643            };
2644            let cfg = crate::conformance::self_test::SelfTestConfig {
2645                target_url: self.target.clone(),
2646                skip_tls_verify: self.skip_tls_verify,
2647                timeout: std::time::Duration::from_secs(30),
2648                // `custom_headers` was already moved into the
2649                // `ConformanceConfig` above; re-derive from `self` so
2650                // we don't borrow it twice.
2651                extra_headers: self
2652                    .conformance_headers
2653                    .iter()
2654                    .filter_map(|h| {
2655                        let (n, v) = h.split_once(':')?;
2656                        Some((n.trim().to_string(), v.trim().to_string()))
2657                    })
2658                    .collect(),
2659                delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
2660                // Round 18.1 — honour `--base-path` (or the spec's
2661                // own first server prefix) so a deployment served
2662                // under a path-prefix doesn't 404 every positive.
2663                base_path: resolved_base_path.clone(),
2664                // Round 18.5 — GEODB multi-source-IP. Parse CLI IP
2665                // lists (malformed entries log a warning and are
2666                // dropped). Empty lists keep pre-18.5 behaviour.
2667                source_ips: parse_ip_list(&self.source_ips, "source-ip"),
2668                geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
2669                geo_source_headers: if self.geo_source_headers.is_empty() {
2670                    crate::conformance::self_test::default_geo_source_headers()
2671                } else {
2672                    self.geo_source_headers.clone()
2673                },
2674                // Round 23 (c-iii) — opt-in request/response capture.
2675                // Constructed here so the sink Arc outlives the run and
2676                // we can drain it for the JSONL write below.
2677                capture: if self.conformance_self_test_capture
2678                    || self.validate_response_schemas
2679                    || self.validate_requests
2680                {
2681                    // Schema validation reads the captured response
2682                    // body, so opt the user into capture implicitly
2683                    // when they ask for validation. The on-disk
2684                    // JSONL/HTML files only get written if the user
2685                    // also passed `--conformance-self-test-capture`.
2686                    // Round 56 (#79) — `--validate-requests` reads the
2687                    // captured *request* the same way, so enable capture
2688                    // for it too. Without this, `--validate-requests` on
2689                    // a self-test run had no in-memory requests to walk
2690                    // and silently wrote no violations file.
2691                    Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
2692                } else {
2693                    None
2694                },
2695                validate_response_schemas: self.validate_response_schemas,
2696                // Round 33 (#823) — basename of the spec, stamped on
2697                // every capture entry so the per-endpoint summary can
2698                // attribute rows back to the right spec on multi-spec
2699                // runs. Falls back to the full path string if the
2700                // basename can't be derived.
2701                spec_label: self.spec.first().map(|p| {
2702                    p.file_name()
2703                        .map(|s| s.to_string_lossy().into_owned())
2704                        .unwrap_or_else(|| p.to_string_lossy().into_owned())
2705                }),
2706                // Round 47 (#79) — always allocate the network-events
2707                // sink for self-test so the file is always written
2708                // (empty array when nothing failed — the cleanest
2709                // possible signal that connectivity stayed up). Caller
2710                // pays one Arc clone per probe, which is in the noise
2711                // next to the HTTP round-trip.
2712                network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
2713                current_iteration: 1,
2714            };
2715            let capture_sink = cfg.capture.clone();
2716            let network_events_sink = cfg.network_events.clone();
2717            TerminalReporter::print_progress(&format!(
2718                "Self-test mode: driving {} operations with positive + per-category negative cases",
2719                ops.len()
2720            ));
2721            // Round 47 (#79) — repeat the matrix per --conformance-
2722            // self-test-iterations / --conformance-self-test-duration.
2723            // Duration wins when both are set; iterations becomes the
2724            // floor so the matrix always runs at least the configured
2725            // number of times. Reports from each iteration are merged
2726            // by the per-category counter sum on the report itself.
2727            let target_iterations = self.conformance_self_test_iterations.max(1);
2728            let duration_budget = self
2729                .conformance_self_test_duration
2730                .as_ref()
2731                .map(|s| Self::parse_duration(s))
2732                .transpose()?
2733                .map(std::time::Duration::from_secs);
2734            let start = std::time::Instant::now();
2735            // Round 49 (#79) — Srikanth on 0.3.193: a 5m budget ran
2736            // 5:46 because the loop only checked the deadline AFTER a
2737            // full iteration completed. Pass the absolute deadline
2738            // into `run_self_test_with_deadline` so the runner can
2739            // break out mid-iteration the moment the budget elapses.
2740            // Iterations bound stays inclusive (so a duration-only run
2741            // doesn't loop forever on a fast spec) but stops EARLY when
2742            // the deadline hits first.
2743            let deadline = duration_budget.map(|d| start + d);
2744            // Round 49 — stamp `current_iteration` on cfg before each
2745            // pass so CaseCapture's `iteration` field carries the
2746            // loop counter (1-indexed).
2747            let mut cfg = cfg;
2748            cfg.current_iteration = 1;
2749            let mut report =
2750                crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
2751                    .await
2752                    .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2753            let mut iter_done: u32 = 1;
2754            loop {
2755                let by_iter = iter_done >= target_iterations;
2756                let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
2757                if by_iter && by_dur {
2758                    break;
2759                }
2760                cfg.current_iteration = iter_done.saturating_add(1);
2761                let next = crate::conformance::self_test::run_self_test_with_deadline(
2762                    &ops, &cfg, deadline,
2763                )
2764                .await
2765                .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2766                report.merge_iteration(next);
2767                iter_done = iter_done.saturating_add(1);
2768            }
2769            if iter_done > 1 {
2770                TerminalReporter::print_progress(&format!(
2771                    "Self-test repeated {} iteration(s) ({:.1?} elapsed)",
2772                    iter_done,
2773                    start.elapsed(),
2774                ));
2775            }
2776            // Round 23 (c-iii) — drain the capture sink into a JSONL
2777            // file next to the JSON/HTML report. One CaseCapture per
2778            // line so the file is grep-able / streamable. Round 24
2779            // (d) — also emit a self-contained HTML viewer at
2780            // `conformance-self-test-requests.html` for users who
2781            // want to browse the capture without piping through `jq`.
2782            // Round 32 (#79 / Srikanth) — derive the per-endpoint
2783            // traffic summary from the same in-memory capture sink so
2784            // we don't re-parse the JSONL from disk later.
2785            let per_endpoint_summary: Vec<
2786                crate::conformance::per_endpoint_summary::PerEndpointSummary,
2787            >;
2788            if let Some(sink) = capture_sink {
2789                if let Ok(guard) = sink.lock() {
2790                    let jsonl_path = self.output.join("conformance-self-test-requests.jsonl");
2791                    let mut lines = String::with_capacity(guard.len() * 256);
2792                    for entry in guard.iter() {
2793                        if let Ok(line) = serde_json::to_string(entry) {
2794                            lines.push_str(&line);
2795                            lines.push('\n');
2796                        }
2797                    }
2798                    let _ = std::fs::write(&jsonl_path, lines);
2799                    let html_path = self.output.join("conformance-self-test-requests.html");
2800                    let html =
2801                        crate::conformance::capture_html::render_capture_html(guard.as_slice());
2802                    let _ = std::fs::write(&html_path, html);
2803
2804                    // Round 32 — per-endpoint summary derived once from
2805                    // the same slice. Written as a JSON sidecar for
2806                    // automation and spliced into the HTML report below.
2807                    per_endpoint_summary =
2808                        crate::conformance::per_endpoint_summary::build_summary(guard.as_slice());
2809                    let summary_path = self.output.join("conformance-per-endpoint.json");
2810                    if let Ok(json) = serde_json::to_string_pretty(&per_endpoint_summary) {
2811                        let _ = std::fs::write(&summary_path, json);
2812                        TerminalReporter::print_progress(&format!(
2813                            "Self-test request/response capture written to {} ({} entries) + {} + {}",
2814                            jsonl_path.display(),
2815                            guard.len(),
2816                            html_path.display(),
2817                            summary_path.display(),
2818                        ));
2819                    } else {
2820                        TerminalReporter::print_progress(&format!(
2821                            "Self-test request/response capture written to {} ({} entries) + {}",
2822                            jsonl_path.display(),
2823                            guard.len(),
2824                            html_path.display(),
2825                        ));
2826                    }
2827                } else {
2828                    per_endpoint_summary = Vec::new();
2829                }
2830            } else {
2831                per_endpoint_summary = Vec::new();
2832            }
2833            TerminalReporter::print_progress(&report.render_summary());
2834            // Round 47 (#79) — drain the self-test wire-level
2835            // network-events sink into `conformance-network-events.json`
2836            // so the user has the same grep-able file the native
2837            // executor's r46 path produces. Empty array when nothing
2838            // failed (the cleanest signal that connectivity stayed up
2839            // throughout the run).
2840            if let Some(sink) = network_events_sink {
2841                if let Ok(guard) = sink.lock() {
2842                    let path = self.output.join("conformance-network-events.json");
2843                    if let Ok(json) = serde_json::to_string_pretty(&*guard) {
2844                        let _ = std::fs::write(&path, json);
2845                        if guard.is_empty() {
2846                            TerminalReporter::print_progress(
2847                                "No wire-level network failures during self-test (file written empty)",
2848                            );
2849                        } else {
2850                            TerminalReporter::print_warning(&format!(
2851                                "Recorded {} wire-level network event(s) to {}",
2852                                guard.len(),
2853                                path.display()
2854                            ));
2855                        }
2856                    }
2857                }
2858            }
2859            // Persist the JSON report alongside the regular conformance
2860            // report so it's grep-able next to the buffer dump from the
2861            // admin endpoint.
2862            let json_path = self.output.join("conformance-self-test.json");
2863            if let Ok(json) = serde_json::to_string_pretty(&report) {
2864                let _ = std::fs::write(&json_path, json);
2865                TerminalReporter::print_progress(&format!(
2866                    "Self-test report written to {}",
2867                    json_path.display()
2868                ));
2869            }
2870            // Round 58 (#79) — write the "definite issues" sidecar so the
2871            // unambiguous problems are grep-able / automatable without eyeballing
2872            // the caught/missed rollup.
2873            let issues = report.definite_issues();
2874            let issues_path = self.output.join("conformance-definite-issues.json");
2875            if let Ok(json) = serde_json::to_string_pretty(&issues) {
2876                if std::fs::write(&issues_path, json).is_ok() && !issues.is_empty() {
2877                    TerminalReporter::print_warning(&format!(
2878                        "{} definite issue(s) — see {}",
2879                        issues.len(),
2880                        issues_path.display()
2881                    ));
2882                }
2883            }
2884            // Round 59 (#79) — owasp injection payloads the target accepted, so
2885            // a WAF tester can grep which URLs let which payloads through.
2886            let owasp_accepted = report.owasp_accepted_probes();
2887            if !owasp_accepted.is_empty() {
2888                let owasp_path = self.output.join("conformance-owasp-accepted.json");
2889                if let Ok(json) = serde_json::to_string_pretty(&owasp_accepted) {
2890                    if std::fs::write(&owasp_path, json).is_ok() {
2891                        TerminalReporter::print_warning(&format!(
2892                            "{} owasp injection probe(s) accepted by the target — see {}",
2893                            owasp_accepted.len(),
2894                            owasp_path.display()
2895                        ));
2896                    }
2897                }
2898            }
2899            // Round 18.1 — surface the "every positive failed with
2900            // the same status" case loudly. Without this, a user
2901            // who forgot `--base-path /api` saw 404 for every
2902            // request, but the per-category negative rollup looked
2903            // all-green (because 404 is in the 4xx range the
2904            // negatives expect). Now the run is correctly called
2905            // out as misconfigured before showing the (meaningless)
2906            // negative results.
2907            if let Some(status) = report.detect_target_misconfiguration() {
2908                let hint = match status {
2909                    404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
2910                    401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
2911                    _ => "",
2912                };
2913                TerminalReporter::print_warning(&format!(
2914                    "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
2915                ));
2916            } else if !report.all_passed() {
2917                TerminalReporter::print_warning(
2918                    "Self-test detected gaps — server let through at least one request that should have been a 4xx",
2919                );
2920            } else {
2921                TerminalReporter::print_success(
2922                    "Self-test passed — all positive cases accepted and all negative cases rejected",
2923                );
2924            }
2925            // Round 17.6 — emit a self-contained HTML report alongside
2926            // the JSON. Groups by category and surfaces the missed-
2927            // negative list directly so a user doesn't need to grep
2928            // through the JSON to find which routes failed which
2929            // checks. Optionally folds in a round-17.4 spec audit
2930            // report if one exists in the same output directory.
2931            let html_path = self.output.join("conformance-report.html");
2932            let audit_path = self.output.join("conformance-spec-audit.json");
2933            let audit_value = std::fs::read_to_string(&audit_path)
2934                .ok()
2935                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
2936            // Round 21.1 — `--report-missed-cap N` lets the user
2937            // override the default 200-row HTML drill-down cap.
2938            // `--report-missed-cap 0` maps to `None` (no cap; show
2939            // everything). The JSON report always has the full set.
2940            let render_opts = crate::conformance::report_html::RenderOptions {
2941                missed_cap: match self.report_missed_cap {
2942                    Some(0) => None,
2943                    Some(n) => Some(n as usize),
2944                    None => Some(200),
2945                },
2946            };
2947            let mut html = crate::conformance::report_html::render_html_with_options(
2948                &report,
2949                audit_value.as_ref(),
2950                &render_opts,
2951            );
2952            // Round 32 (#79 / Srikanth) — splice the per-endpoint
2953            // summary just before the closing `</body>` so it lands at
2954            // the bottom of the report. Empty summary renders as an
2955            // empty string so we don't even introduce an extra newline
2956            // when there were no captures.
2957            let summary_section = crate::conformance::per_endpoint_summary::render_html_section(
2958                &per_endpoint_summary,
2959            );
2960            if !summary_section.is_empty() {
2961                if let Some(idx) = html.rfind("</body>") {
2962                    html.insert_str(idx, &summary_section);
2963                } else {
2964                    html.push_str(&summary_section);
2965                }
2966            }
2967            if std::fs::write(&html_path, html).is_ok() {
2968                TerminalReporter::print_progress(&format!(
2969                    "HTML report written to {}",
2970                    html_path.display()
2971                ));
2972            }
2973
2974            // Round 56 (#79) — Srikanth on 0.3.203: "parameter violations
2975            // are still absent from the logs." Root cause: the SINGLE-target
2976            // self-test returned here without ever walking the emitted
2977            // requests. Only the r49 multi-target self-test path (the
2978            // `--targets-file` workflow) called the validator. Mirror that
2979            // wiring here so a plain single-target self-test also writes
2980            // `conformance-request-violations.json`. The validator reads the
2981            // capture we just drained to the JSONL; my r56 change to
2982            // `request_validator.rs` records each `parameters:*` negative as
2983            // a `parameter_negative_probe` even when the emitted request is
2984            // spec-valid (so the three parameter probes stop being silent).
2985            if self.validate_requests && !self.spec.is_empty() {
2986                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
2987                    &self.spec,
2988                    &self.output,
2989                    self.base_path.as_deref(),
2990                )
2991                .await?;
2992                if n > 0 {
2993                    TerminalReporter::print_warning(&format!(
2994                        "{} emitted request(s) recorded against the spec — see conformance-request-violations.json",
2995                        n
2996                    ));
2997                }
2998            }
2999            return Ok(());
3000        }
3001
3002        // Request validation against OpenAPI spec (if --validate-requests is set)
3003        if self.validate_requests && !self.spec.is_empty() {
3004            TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
3005            let violation_count = crate::conformance::request_validator::run_request_validation(
3006                &self.spec,
3007                self.conformance_custom.as_deref(),
3008                self.base_path.as_deref(),
3009                &self.output,
3010            )
3011            .await?;
3012            if violation_count > 0 {
3013                TerminalReporter::print_warning(&format!(
3014                    "{} request validation violation(s) found — see conformance-request-violations.json",
3015                    violation_count
3016                ));
3017            } else {
3018                TerminalReporter::print_success("All requests conform to the OpenAPI spec");
3019            }
3020        }
3021
3022        // If generate-only OR --use-k6, use the k6 script generation path
3023        if self.generate_only || self.use_k6 {
3024            let script = if let Some(annotated) = &annotated_ops {
3025                let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3026                    config,
3027                    annotated.clone(),
3028                );
3029                let op_count = gen.operation_count();
3030                let (script, check_count) = gen.generate()?;
3031                TerminalReporter::print_success(&format!(
3032                    "Conformance: {} operations analyzed, {} unique checks generated",
3033                    op_count, check_count
3034                ));
3035                script
3036            } else {
3037                let generator = ConformanceGenerator::new(config);
3038                generator.generate()?
3039            };
3040
3041            let script_path = self.output.join("k6-conformance.js");
3042            std::fs::write(&script_path, &script).map_err(|e| {
3043                BenchError::Other(format!("Failed to write conformance script: {}", e))
3044            })?;
3045            TerminalReporter::print_success(&format!(
3046                "Conformance script generated: {}",
3047                script_path.display()
3048            ));
3049
3050            if self.generate_only {
3051                println!("\nScript generated. Run with:");
3052                println!("  k6 run {}", script_path.display());
3053                return Ok(());
3054            }
3055
3056            // --use-k6: execute via k6
3057            if !K6Executor::is_k6_installed() {
3058                TerminalReporter::print_error("k6 is not installed");
3059                TerminalReporter::print_warning(
3060                    "Install k6 from: https://k6.io/docs/get-started/installation/",
3061                );
3062                return Err(BenchError::K6NotFound);
3063            }
3064
3065            TerminalReporter::print_progress("Running conformance tests via k6...");
3066            let executor = K6Executor::new()?
3067                .with_local_ips(self.source_ips.join(","))
3068                .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
3069            executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3070
3071            let report_path = self.output.join("conformance-report.json");
3072            if report_path.exists() {
3073                let report = ConformanceReport::from_file(&report_path)?;
3074                report.print_report_with_options(self.conformance_all_operations);
3075                self.save_conformance_report(&report, &report_path)?;
3076            } else {
3077                TerminalReporter::print_warning(
3078                    "Conformance report not generated (k6 handleSummary may not have run)",
3079                );
3080            }
3081
3082            // Round 44 (#79) — Srikanth on 0.3.188: "Any reason why
3083            // validate-requests in mockforge client are not catching
3084            // all this query param or body params or path params
3085            // violation issues and record in conformance-request-
3086            // failure logs?" The custom-YAML validator only checks
3087            // the YAML shape at config time. Now, when both
3088            // `--validate-requests` and `--export-requests` are set,
3089            // also walk the emitted `conformance-requests.json` and
3090            // validate each actual wire-level request against the
3091            // spec. Violations are appended to
3092            // `conformance-request-violations.json`.
3093            if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3094                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3095                    &self.spec,
3096                    &self.output,
3097                    self.base_path.as_deref(),
3098                )
3099                .await?;
3100                if n > 0 {
3101                    TerminalReporter::print_warning(&format!(
3102                        "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3103                        n
3104                    ));
3105                }
3106            }
3107
3108            return Ok(());
3109        }
3110
3111        // Default: Native Rust executor (no k6 dependency)
3112        TerminalReporter::print_progress("Running conformance tests (native executor)...");
3113
3114        let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3115
3116        // Round 39 (#79) — when the user passed `--conformance-custom`
3117        // WITHOUT `--spec` and without `--conformance-self-test`, fire
3118        // only the YAML's checks. The built-in 47 reference checks
3119        // (`param:path:string`, etc.) hit `/conformance/...` paths that
3120        // do not exist on a real target, so a custom-only run against
3121        // a remote API produced a flood of irrelevant 404s in the
3122        // request log. Srikanth on 0.3.183: "In the exported request I
3123        // see it is sending request to api/conformance/params/hello
3124        // and some other URLs".
3125        let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3126        executor = if let Some(annotated) = &annotated_ops {
3127            executor.with_spec_driven_checks(annotated)
3128        } else if custom_only {
3129            executor
3130        } else {
3131            executor.with_reference_checks()
3132        };
3133        executor = executor.with_custom_checks()?;
3134
3135        TerminalReporter::print_success(&format!(
3136            "Executing {} conformance checks...",
3137            executor.check_count()
3138        ));
3139
3140        let report = executor.execute().await?;
3141        report.print_report_with_options(self.conformance_all_operations);
3142
3143        // Save failure details to a separate file for easy debugging
3144        let failure_details = report.failure_details();
3145        if !failure_details.is_empty() {
3146            let details_path = self.output.join("conformance-failure-details.json");
3147            if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3148                let _ = std::fs::write(&details_path, json);
3149                TerminalReporter::print_success(&format!(
3150                    "Failure details saved to: {}",
3151                    details_path.display()
3152                ));
3153            }
3154        }
3155
3156        // Save report
3157        let report_path = self.output.join("conformance-report.json");
3158        let report_json = serde_json::to_string_pretty(&report.to_json())
3159            .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3160        std::fs::write(&report_path, &report_json)
3161            .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3162        TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
3163
3164        self.save_conformance_report(&report, &report_path)?;
3165
3166        // Round 45 (#79) — Srikanth on 0.3.189: "I am still not seeing
3167        // any conformance failure logs or conformance-request logs are
3168        // also not capturing any failures info." His command does NOT
3169        // pass `--use-k6`, so the round-44 wiring (which only ran on
3170        // the k6 branch) never fired. Mirror the same retrospective
3171        // pass here: when both `--validate-requests` and
3172        // `--export-requests` are set, walk the native executor's
3173        // freshly-written `conformance-requests.json` and validate
3174        // each emitted request against the spec. Violations are
3175        // appended to `conformance-request-violations.json`.
3176        if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3177            let n =
3178                crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3179                    &self.spec,
3180                    &self.output,
3181                    self.base_path.as_deref(),
3182                )
3183                .await?;
3184            if n > 0 {
3185                TerminalReporter::print_warning(&format!(
3186                    "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3187                    n
3188                ));
3189            }
3190        }
3191
3192        Ok(())
3193    }
3194
3195    /// Save conformance report in the requested format (SARIF or JSON copy)
3196    fn save_conformance_report(
3197        &self,
3198        report: &crate::conformance::report::ConformanceReport,
3199        report_path: &Path,
3200    ) -> Result<()> {
3201        if self.conformance_report_format == "sarif" {
3202            use crate::conformance::sarif::ConformanceSarifReport;
3203            ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
3204            TerminalReporter::print_success(&format!(
3205                "SARIF report saved to: {}",
3206                self.conformance_report.display()
3207            ));
3208        } else if self.conformance_report != *report_path {
3209            std::fs::copy(report_path, &self.conformance_report)?;
3210            TerminalReporter::print_success(&format!(
3211                "Report saved to: {}",
3212                self.conformance_report.display()
3213            ));
3214        }
3215        Ok(())
3216    }
3217
3218    /// Round 48 (#79) — Srikanth on 0.3.192: "I ran following commands
3219    /// to test conformance-sef-test duration, but the test ended
3220    /// immediately" with `--targets-file vs_list1.json`. The multi-
3221    /// target dispatch returned before reaching the round-47 self-test
3222    /// iteration loop. This helper runs the self-test driver against
3223    /// every target listed in `targets_file` honouring the same
3224    /// `--conformance-self-test-iterations` and `--conformance-self-
3225    /// test-duration` knobs the single-target path got. One self-test
3226    /// report file per target plus a `conformance-network-events.json`
3227    /// per target so a user can attribute wire failures back to the
3228    /// target they happened against.
3229    async fn execute_multi_target_self_test(&self, targets_file: &Path) -> Result<()> {
3230        use crate::conformance::self_test::SelfTestConfig;
3231
3232        TerminalReporter::print_progress("Multi-target conformance self-test mode");
3233        let targets = parse_targets_file(targets_file)?;
3234        if targets.is_empty() {
3235            return Err(BenchError::Other("No targets found in file".to_string()));
3236        }
3237        TerminalReporter::print_success(&format!("Loaded {} target(s)", targets.len()));
3238
3239        // Spec is shared across targets — load once.
3240        let annotated_ops = if !self.spec.is_empty() {
3241            let parser = SpecParser::from_file(&self.spec[0]).await?;
3242            let operations = parser.get_operations();
3243            Some(
3244                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3245                    &operations,
3246                    parser.spec(),
3247                ),
3248            )
3249        } else {
3250            return Err(BenchError::Other("--conformance-self-test requires --spec".to_string()));
3251        };
3252        let Some(ops) = annotated_ops else {
3253            unreachable!()
3254        };
3255
3256        std::fs::create_dir_all(&self.output)?;
3257        let resolved_base_path = self.base_path.clone();
3258        let target_iterations = self.conformance_self_test_iterations.max(1);
3259        let duration_budget = self
3260            .conformance_self_test_duration
3261            .as_ref()
3262            .map(|s| Self::parse_duration(s))
3263            .transpose()?
3264            .map(std::time::Duration::from_secs);
3265
3266        for (idx, target) in targets.iter().enumerate() {
3267            let target_dir = self.output.join(format!("target_{}", idx));
3268            std::fs::create_dir_all(&target_dir)?;
3269            TerminalReporter::print_progress(&format!(
3270                "[target {}/{}] {}",
3271                idx + 1,
3272                targets.len(),
3273                target.url
3274            ));
3275
3276            let merged_headers: Vec<(String, String)> = self
3277                .conformance_headers
3278                .iter()
3279                .filter_map(|h| {
3280                    let (n, v) = h.split_once(':')?;
3281                    Some((n.trim().to_string(), v.trim().to_string()))
3282                })
3283                .collect();
3284
3285            let cfg = SelfTestConfig {
3286                target_url: target.url.clone(),
3287                skip_tls_verify: self.skip_tls_verify,
3288                timeout: std::time::Duration::from_secs(30),
3289                extra_headers: merged_headers,
3290                delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
3291                base_path: resolved_base_path.clone(),
3292                source_ips: parse_ip_list(&self.source_ips, "source-ip"),
3293                geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
3294                geo_source_headers: if self.geo_source_headers.is_empty() {
3295                    crate::conformance::self_test::default_geo_source_headers()
3296                } else {
3297                    self.geo_source_headers.clone()
3298                },
3299                capture: if self.conformance_self_test_capture
3300                    || self.validate_response_schemas
3301                    || self.validate_requests
3302                {
3303                    // Round 56 (#79) — mirror the single-target path: the
3304                    // per-target request validator needs the captured
3305                    // requests, so `--validate-requests` implies capture.
3306                    Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
3307                } else {
3308                    None
3309                },
3310                validate_response_schemas: self.validate_response_schemas,
3311                spec_label: self.spec.first().map(|p| {
3312                    p.file_name()
3313                        .map(|s| s.to_string_lossy().into_owned())
3314                        .unwrap_or_else(|| p.to_string_lossy().into_owned())
3315                }),
3316                network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
3317                current_iteration: 1,
3318            };
3319            let capture_sink = cfg.capture.clone();
3320            let network_events_sink = cfg.network_events.clone();
3321
3322            let start = std::time::Instant::now();
3323            // Round 49 — pass an absolute deadline down so the loop
3324            // can break out mid-iteration once the budget elapses
3325            // instead of overshooting by a full pass.
3326            let deadline = duration_budget.map(|d| start + d);
3327            // Round 49 — stamp `current_iteration` on cfg before each
3328            // pass so CaseCapture's `iteration` field carries the
3329            // loop counter (1-indexed).
3330            let mut cfg = cfg;
3331            cfg.current_iteration = 1;
3332            let mut report =
3333                crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
3334                    .await
3335                    .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3336            let mut iter_done: u32 = 1;
3337            loop {
3338                let by_iter = iter_done >= target_iterations;
3339                let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
3340                if by_iter && by_dur {
3341                    break;
3342                }
3343                cfg.current_iteration = iter_done.saturating_add(1);
3344                let next = crate::conformance::self_test::run_self_test_with_deadline(
3345                    &ops, &cfg, deadline,
3346                )
3347                .await
3348                .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3349                report.merge_iteration(next);
3350                iter_done = iter_done.saturating_add(1);
3351            }
3352            if iter_done > 1 {
3353                TerminalReporter::print_progress(&format!(
3354                    "  ran {} iteration(s) in {:.1?}",
3355                    iter_done,
3356                    start.elapsed(),
3357                ));
3358            }
3359
3360            // Drain the per-target sinks.
3361            if let Some(sink) = capture_sink {
3362                if let Ok(guard) = sink.lock() {
3363                    let jsonl = target_dir.join("conformance-self-test-requests.jsonl");
3364                    let mut lines = String::with_capacity(guard.len() * 256);
3365                    for entry in guard.iter() {
3366                        if let Ok(line) = serde_json::to_string(entry) {
3367                            lines.push_str(&line);
3368                            lines.push('\n');
3369                        }
3370                    }
3371                    let _ = std::fs::write(&jsonl, lines);
3372                }
3373            }
3374            if let Some(sink) = network_events_sink {
3375                if let Ok(guard) = sink.lock() {
3376                    let path = target_dir.join("conformance-network-events.json");
3377                    if let Ok(json) = serde_json::to_string_pretty(&*guard) {
3378                        let _ = std::fs::write(&path, json);
3379                        if !guard.is_empty() {
3380                            TerminalReporter::print_warning(&format!(
3381                                "  recorded {} wire-level network event(s)",
3382                                guard.len()
3383                            ));
3384                        }
3385                    }
3386                }
3387            }
3388
3389            let json_path = target_dir.join("conformance-self-test.json");
3390            if let Ok(json) = serde_json::to_string_pretty(&report) {
3391                let _ = std::fs::write(&json_path, json);
3392            }
3393            // Round 58 (#79) — per-target "definite issues" sidecar (mirror of
3394            // the single-target path).
3395            let issues = report.definite_issues();
3396            if let Ok(json) = serde_json::to_string_pretty(&issues) {
3397                let issues_path = target_dir.join("conformance-definite-issues.json");
3398                if std::fs::write(&issues_path, json).is_ok() && !issues.is_empty() {
3399                    TerminalReporter::print_warning(&format!(
3400                        "  {} definite issue(s) — see {}",
3401                        issues.len(),
3402                        issues_path.display()
3403                    ));
3404                }
3405            }
3406            // Round 59 (#79) — per-target owasp-accepted sidecar (mirror).
3407            let owasp_accepted = report.owasp_accepted_probes();
3408            if !owasp_accepted.is_empty() {
3409                if let Ok(json) = serde_json::to_string_pretty(&owasp_accepted) {
3410                    let owasp_path = target_dir.join("conformance-owasp-accepted.json");
3411                    if std::fs::write(&owasp_path, json).is_ok() {
3412                        TerminalReporter::print_warning(&format!(
3413                            "  {} owasp injection probe(s) accepted by the target — see {}",
3414                            owasp_accepted.len(),
3415                            owasp_path.display()
3416                        ));
3417                    }
3418                }
3419            }
3420            TerminalReporter::print_progress(&report.render_summary());
3421
3422            // Round 49 (#79) — Srikanth on 0.3.193: "I am not seeing
3423            // any violation requests logs when running [self-test
3424            // + --targets-file]". `validate_emitted_requests` was
3425            // only wired into the bench-export path; self-test
3426            // writes its captures to `conformance-self-test-
3427            // requests.jsonl` instead. The validator now reads that
3428            // file too (see the JSONL branch in request_validator.rs),
3429            // so we just need to invoke it here per-target.
3430            if self.validate_requests {
3431                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3432                    &self.spec,
3433                    &target_dir,
3434                    self.base_path.as_deref(),
3435                )
3436                .await?;
3437                if n > 0 {
3438                    TerminalReporter::print_warning(&format!(
3439                        "  {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3440                        n,
3441                        target_dir.display(),
3442                    ));
3443                }
3444            }
3445        }
3446
3447        Ok(())
3448    }
3449
3450    /// Execute conformance tests against multiple targets from a targets file.
3451    ///
3452    /// Uses the native `NativeConformanceExecutor` (no k6 dependency). Targets are
3453    /// tested sequentially to avoid overwhelming them, and per-target headers from
3454    /// the targets file are merged with the base `--conformance-header` headers.
3455    async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
3456        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
3457        use crate::conformance::report::ConformanceReport;
3458        use crate::conformance::spec::ConformanceFeature;
3459
3460        TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
3461
3462        // Parse targets file
3463        TerminalReporter::print_progress("Parsing targets file...");
3464        let targets = parse_targets_file(targets_file)?;
3465        let num_targets = targets.len();
3466        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
3467
3468        if targets.is_empty() {
3469            return Err(BenchError::Other("No targets found in file".to_string()));
3470        }
3471
3472        TerminalReporter::print_progress(
3473            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
3474        );
3475
3476        // Parse category filter (shared across all targets)
3477        let categories = self.conformance_categories.as_ref().map(|cats_str| {
3478            cats_str
3479                .split(',')
3480                .filter_map(|s| {
3481                    let trimmed = s.trim();
3482                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
3483                        Some(canonical.to_string())
3484                    } else {
3485                        TerminalReporter::print_warning(&format!(
3486                            "Unknown conformance category: '{}'. Valid categories: {}",
3487                            trimmed,
3488                            ConformanceFeature::cli_category_names()
3489                                .iter()
3490                                .map(|(cli, _)| *cli)
3491                                .collect::<Vec<_>>()
3492                                .join(", ")
3493                        ));
3494                        None
3495                    }
3496                })
3497                .collect::<Vec<String>>()
3498        });
3499
3500        // Parse base custom headers from --conformance-header flags
3501        let base_custom_headers: Vec<(String, String)> = self
3502            .conformance_headers
3503            .iter()
3504            .filter_map(|h| {
3505                let (name, value) = h.split_once(':')?;
3506                Some((name.trim().to_string(), value.trim().to_string()))
3507            })
3508            .collect();
3509
3510        if !base_custom_headers.is_empty() {
3511            TerminalReporter::print_progress(&format!(
3512                "Using {} base custom header(s) for authentication",
3513                base_custom_headers.len()
3514            ));
3515        }
3516
3517        // Load spec once if provided (shared across all targets)
3518        let annotated_ops = if !self.spec.is_empty() {
3519            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
3520            let parser = SpecParser::from_file(&self.spec[0]).await?;
3521            let operations = parser.get_operations();
3522            let annotated =
3523                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3524                    &operations,
3525                    parser.spec(),
3526                );
3527            TerminalReporter::print_success(&format!(
3528                "Analyzed {} operations, found {} feature annotations",
3529                operations.len(),
3530                annotated.iter().map(|a| a.features.len()).sum::<usize>()
3531            ));
3532            Some(annotated)
3533        } else {
3534            None
3535        };
3536
3537        // Ensure output dir exists
3538        std::fs::create_dir_all(&self.output)?;
3539
3540        // Collect per-target results for the summary
3541        struct TargetResult {
3542            url: String,
3543            passed: usize,
3544            failed: usize,
3545            elapsed: std::time::Duration,
3546            report_json: serde_json::Value,
3547            owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
3548        }
3549
3550        let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
3551        let total_start = std::time::Instant::now();
3552
3553        for (idx, target) in targets.iter().enumerate() {
3554            tracing::info!(
3555                "Running conformance tests against target {}/{}: {}",
3556                idx + 1,
3557                num_targets,
3558                target.url
3559            );
3560            TerminalReporter::print_progress(&format!(
3561                "\n--- Target {}/{}: {} ---",
3562                idx + 1,
3563                num_targets,
3564                target.url
3565            ));
3566
3567            // Merge base headers with per-target headers
3568            let mut merged_headers = base_custom_headers.clone();
3569            if let Some(ref target_headers) = target.headers {
3570                for (name, value) in target_headers {
3571                    // Per-target headers override base headers with the same name
3572                    if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
3573                        existing.1 = value.clone();
3574                    } else {
3575                        merged_headers.push((name.clone(), value.clone()));
3576                    }
3577                }
3578            }
3579            // Add auth header if present on target
3580            if let Some(ref auth) = target.auth {
3581                if let Some(existing) =
3582                    merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
3583                {
3584                    existing.1 = auth.clone();
3585                } else {
3586                    merged_headers.push(("Authorization".to_string(), auth.clone()));
3587                }
3588            }
3589
3590            // Per-target output dir (used by both native and k6 paths).
3591            // Created before the config so we can point the k6 script's
3592            // handleSummary at the per-target directory rather than the shared
3593            // parent output dir (otherwise every target would overwrite the
3594            // same conformance-report.json).
3595            let target_dir = self.output.join(format!("target_{}", idx));
3596            std::fs::create_dir_all(&target_dir)?;
3597
3598            let config = ConformanceConfig {
3599                target_url: target.url.clone(),
3600                api_key: self.conformance_api_key.clone(),
3601                basic_auth: self.conformance_basic_auth.clone(),
3602                skip_tls_verify: self.skip_tls_verify,
3603                categories: categories.clone(),
3604                base_path: self.base_path.clone(),
3605                custom_headers: merged_headers,
3606                output_dir: Some(target_dir.clone()),
3607                all_operations: self.conformance_all_operations,
3608                custom_checks_file: self.conformance_custom.clone(),
3609                request_delay_ms: self.conformance_delay_ms,
3610                custom_filter: self.conformance_custom_filter.clone(),
3611                export_requests: self.export_requests,
3612                validate_requests: self.validate_requests,
3613            };
3614
3615            let target_start = std::time::Instant::now();
3616            let report = if self.use_k6 {
3617                if !K6Executor::is_k6_installed() {
3618                    TerminalReporter::print_error("k6 is not installed");
3619                    TerminalReporter::print_warning(
3620                        "Install k6 from: https://k6.io/docs/get-started/installation/",
3621                    );
3622                    return Err(BenchError::K6NotFound);
3623                }
3624
3625                let script = if let Some(ref annotated) = annotated_ops {
3626                    let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3627                        config.clone(),
3628                        annotated.clone(),
3629                    );
3630                    let (script, _check_count) = gen.generate()?;
3631                    script
3632                } else {
3633                    let generator = ConformanceGenerator::new(config.clone());
3634                    generator.generate()?
3635                };
3636
3637                let script_path = target_dir.join("k6-conformance.js");
3638                std::fs::write(&script_path, &script).map_err(|e| {
3639                    BenchError::Other(format!("Failed to write conformance script: {}", e))
3640                })?;
3641                TerminalReporter::print_success(&format!(
3642                    "Conformance script generated: {}",
3643                    script_path.display()
3644                ));
3645
3646                TerminalReporter::print_progress(&format!(
3647                    "Running conformance tests via k6 against {}...",
3648                    target.url
3649                ));
3650                let k6 = K6Executor::new()?
3651                    .with_local_ips(self.source_ips.join(","))
3652                    .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
3653                // Unique k6 API port per target to avoid collisions.
3654                let api_port = 6565u16.saturating_add(idx as u16);
3655                k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
3656                    .await?;
3657
3658                let report_path = target_dir.join("conformance-report.json");
3659                if report_path.exists() {
3660                    ConformanceReport::from_file(&report_path)?
3661                } else {
3662                    TerminalReporter::print_warning(&format!(
3663                        "Conformance report not generated for target {} (k6 handleSummary may not have run)",
3664                        target.url
3665                    ));
3666                    continue;
3667                }
3668            } else {
3669                let mut executor =
3670                    crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3671
3672                // Round 39 (#79) — see custom_only comment above; same
3673                // logic applied to the multi-target branch.
3674                let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3675                executor = if let Some(ref annotated) = annotated_ops {
3676                    executor.with_spec_driven_checks(annotated)
3677                } else if custom_only {
3678                    executor
3679                } else {
3680                    executor.with_reference_checks()
3681                };
3682                executor = executor.with_custom_checks()?;
3683
3684                TerminalReporter::print_success(&format!(
3685                    "Executing {} conformance checks against {}...",
3686                    executor.check_count(),
3687                    target.url
3688                ));
3689
3690                executor.execute().await?
3691            };
3692            let target_elapsed = target_start.elapsed();
3693
3694            let report_json = report.to_json();
3695
3696            // Extract pass/fail from the summary in the JSON
3697            let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
3698            let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
3699            let total_checks = passed + failed;
3700            let rate = if total_checks == 0 {
3701                0.0
3702            } else {
3703                (passed as f64 / total_checks as f64) * 100.0
3704            };
3705
3706            TerminalReporter::print_success(&format!(
3707                "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
3708                target.url,
3709                passed,
3710                total_checks,
3711                rate,
3712                target_elapsed.as_secs_f64()
3713            ));
3714
3715            // Save per-target report (target_dir created above)
3716            let target_report_path = target_dir.join("conformance-report.json");
3717            let report_str = serde_json::to_string_pretty(&report_json)
3718                .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3719            std::fs::write(&target_report_path, &report_str)
3720                .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3721
3722            // Save failure details if any
3723            let failure_details = report.failure_details();
3724            if !failure_details.is_empty() {
3725                let details_path = target_dir.join("conformance-failure-details.json");
3726                if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3727                    let _ = std::fs::write(&details_path, json);
3728                }
3729            }
3730
3731            // Round 45 (#79) — Srikanth on 0.3.189: `conformance-request-
3732            // violations.json` was never being written in his
3733            // multi-target self-test run. The round-44 wiring sat on
3734            // the single-target branch only. Mirror it here, per
3735            // target_dir, so the multi-target case (his typical
3736            // workflow) actually surfaces wire-level violations.
3737            if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3738                let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3739                    &self.spec,
3740                    &target_dir,
3741                    self.base_path.as_deref(),
3742                )
3743                .await?;
3744                if n > 0 {
3745                    TerminalReporter::print_warning(&format!(
3746                        "Target {}: {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3747                        target.url,
3748                        n,
3749                        target_dir.display()
3750                    ));
3751                }
3752            }
3753
3754            // Compute OWASP coverage for this target
3755            let owasp_coverage = report.owasp_coverage_data();
3756
3757            target_results.push(TargetResult {
3758                url: target.url.clone(),
3759                passed,
3760                failed,
3761                elapsed: target_elapsed,
3762                report_json,
3763                owasp_coverage,
3764            });
3765        }
3766
3767        let total_elapsed = total_start.elapsed();
3768
3769        // Print summary table
3770        println!("\n{}", "=".repeat(80));
3771        println!("  Multi-Target Conformance Summary");
3772        println!("{}", "=".repeat(80));
3773        println!(
3774            "  {:<40} {:>8} {:>8} {:>8} {:>8}",
3775            "Target URL", "Passed", "Failed", "Rate", "Time"
3776        );
3777        println!("  {}", "-".repeat(76));
3778
3779        let mut total_passed = 0usize;
3780        let mut total_failed = 0usize;
3781
3782        for result in &target_results {
3783            let total_checks = result.passed + result.failed;
3784            let rate = if total_checks == 0 {
3785                0.0
3786            } else {
3787                (result.passed as f64 / total_checks as f64) * 100.0
3788            };
3789
3790            // Truncate long URLs for display
3791            let display_url = if result.url.len() > 38 {
3792                format!("{}...", &result.url[..35])
3793            } else {
3794                result.url.clone()
3795            };
3796
3797            println!(
3798                "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3799                display_url,
3800                result.passed,
3801                result.failed,
3802                rate,
3803                result.elapsed.as_secs_f64()
3804            );
3805
3806            total_passed += result.passed;
3807            total_failed += result.failed;
3808        }
3809
3810        let grand_total = total_passed + total_failed;
3811        let overall_rate = if grand_total == 0 {
3812            0.0
3813        } else {
3814            (total_passed as f64 / grand_total as f64) * 100.0
3815        };
3816
3817        println!("  {}", "-".repeat(76));
3818        println!(
3819            "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3820            format!("TOTAL ({} targets)", num_targets),
3821            total_passed,
3822            total_failed,
3823            overall_rate,
3824            total_elapsed.as_secs_f64()
3825        );
3826        println!("{}", "=".repeat(80));
3827
3828        // Print per-target OWASP coverage
3829        for result in &target_results {
3830            println!("\n  OWASP API Security Top 10 Coverage for {}:", result.url);
3831            for entry in &result.owasp_coverage {
3832                let status = if !entry.tested {
3833                    "-"
3834                } else if entry.all_passed {
3835                    "pass"
3836                } else {
3837                    "FAIL"
3838                };
3839                let via = if entry.via_categories.is_empty() {
3840                    String::new()
3841                } else {
3842                    format!(" (via {})", entry.via_categories.join(", "))
3843                };
3844                println!("    {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
3845            }
3846        }
3847
3848        // Save combined summary
3849        let per_target_summaries: Vec<serde_json::Value> = target_results
3850            .iter()
3851            .enumerate()
3852            .map(|(idx, r)| {
3853                let total_checks = r.passed + r.failed;
3854                let rate = if total_checks == 0 {
3855                    0.0
3856                } else {
3857                    (r.passed as f64 / total_checks as f64) * 100.0
3858                };
3859                let owasp_json: Vec<serde_json::Value> = r
3860                    .owasp_coverage
3861                    .iter()
3862                    .map(|e| {
3863                        serde_json::json!({
3864                            "id": e.id,
3865                            "name": e.name,
3866                            "tested": e.tested,
3867                            "all_passed": e.all_passed,
3868                            "via_categories": e.via_categories,
3869                        })
3870                    })
3871                    .collect();
3872                serde_json::json!({
3873                    "target_url": r.url,
3874                    "target_index": idx,
3875                    "checks_passed": r.passed,
3876                    "checks_failed": r.failed,
3877                    "total_checks": total_checks,
3878                    "pass_rate": rate,
3879                    "elapsed_seconds": r.elapsed.as_secs_f64(),
3880                    "report": r.report_json,
3881                    "owasp_coverage": owasp_json,
3882                })
3883            })
3884            .collect();
3885
3886        let combined_summary = serde_json::json!({
3887            "total_targets": num_targets,
3888            "total_checks_passed": total_passed,
3889            "total_checks_failed": total_failed,
3890            "overall_pass_rate": overall_rate,
3891            "total_elapsed_seconds": total_elapsed.as_secs_f64(),
3892            "targets": per_target_summaries,
3893        });
3894
3895        let summary_path = self.output.join("multi-target-conformance-summary.json");
3896        let summary_str = serde_json::to_string_pretty(&combined_summary)
3897            .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
3898        std::fs::write(&summary_path, &summary_str)
3899            .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
3900        TerminalReporter::print_success(&format!(
3901            "Combined summary saved to: {}",
3902            summary_path.display()
3903        ));
3904
3905        Ok(())
3906    }
3907
3908    /// Execute OWASP API Security Top 10 testing mode
3909    async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
3910        TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
3911
3912        // Parse custom headers from CLI
3913        let custom_headers = self.parse_headers()?;
3914
3915        // Build OWASP configuration from CLI options
3916        let mut config = OwaspApiConfig::new()
3917            .with_auth_header(&self.owasp_auth_header)
3918            .with_verbose(self.verbose)
3919            .with_insecure(self.skip_tls_verify)
3920            .with_concurrency(self.vus as usize)
3921            .with_iterations(self.owasp_iterations as usize)
3922            .with_base_path(self.base_path.clone())
3923            .with_custom_headers(custom_headers);
3924
3925        // Set valid auth token if provided
3926        if let Some(ref token) = self.owasp_auth_token {
3927            config = config.with_valid_auth_token(token);
3928        }
3929
3930        // Parse categories if provided
3931        if let Some(ref cats_str) = self.owasp_categories {
3932            let categories: Vec<OwaspCategory> = cats_str
3933                .split(',')
3934                .filter_map(|s| {
3935                    let trimmed = s.trim();
3936                    match trimmed.parse::<OwaspCategory>() {
3937                        Ok(cat) => Some(cat),
3938                        Err(e) => {
3939                            TerminalReporter::print_warning(&e);
3940                            None
3941                        }
3942                    }
3943                })
3944                .collect();
3945
3946            if !categories.is_empty() {
3947                config = config.with_categories(categories);
3948            }
3949        }
3950
3951        // Load admin paths from file if provided
3952        if let Some(ref admin_paths_file) = self.owasp_admin_paths {
3953            config.admin_paths_file = Some(admin_paths_file.clone());
3954            if let Err(e) = config.load_admin_paths() {
3955                TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
3956            }
3957        }
3958
3959        // Set ID fields if provided
3960        if let Some(ref id_fields_str) = self.owasp_id_fields {
3961            let id_fields: Vec<String> = id_fields_str
3962                .split(',')
3963                .map(|s| s.trim().to_string())
3964                .filter(|s| !s.is_empty())
3965                .collect();
3966            if !id_fields.is_empty() {
3967                config = config.with_id_fields(id_fields);
3968            }
3969        }
3970
3971        // Set report path and format
3972        if let Some(ref report_path) = self.owasp_report {
3973            config = config.with_report_path(report_path);
3974        }
3975        if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
3976            config = config.with_report_format(format);
3977        }
3978
3979        // Print configuration summary
3980        let categories = config.categories_to_test();
3981        TerminalReporter::print_success(&format!(
3982            "Testing {} OWASP categories: {}",
3983            categories.len(),
3984            categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
3985        ));
3986
3987        if config.valid_auth_token.is_some() {
3988            TerminalReporter::print_progress("Using provided auth token for baseline requests");
3989        }
3990
3991        // Create the OWASP generator
3992        TerminalReporter::print_progress("Generating OWASP security test script...");
3993        let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
3994
3995        // Generate the script
3996        let script = generator.generate()?;
3997        TerminalReporter::print_success("OWASP security test script generated");
3998
3999        // Write script to file
4000        let script_path = if let Some(output) = &self.script_output {
4001            output.clone()
4002        } else {
4003            self.output.join("k6-owasp-security-test.js")
4004        };
4005
4006        if let Some(parent) = script_path.parent() {
4007            std::fs::create_dir_all(parent)?;
4008        }
4009        std::fs::write(&script_path, &script)?;
4010        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
4011
4012        // If generate-only mode, exit here
4013        if self.generate_only {
4014            println!("\nOWASP security test script generated. Run it with:");
4015            println!("  k6 run {}", script_path.display());
4016            return Ok(());
4017        }
4018
4019        // Execute k6
4020        TerminalReporter::print_progress("Executing OWASP security tests...");
4021        let executor = K6Executor::new()?
4022            .with_local_ips(self.source_ips.join(","))
4023            .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
4024        std::fs::create_dir_all(&self.output)?;
4025
4026        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
4027
4028        let duration_secs = Self::parse_duration(&self.duration)?;
4029        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
4030
4031        println!("\nOWASP security test results saved to: {}", self.output.display());
4032
4033        Ok(())
4034    }
4035}
4036
4037#[cfg(test)]
4038mod tests {
4039    use super::*;
4040    use tempfile::tempdir;
4041
4042    #[test]
4043    fn test_parse_duration() {
4044        assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
4045        assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
4046        assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
4047        assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
4048    }
4049
4050    /// Round 22.4 — `start-end` IPv4 range syntax for non-power-of-2
4051    /// ranges. Srikanth (h): `--source-ip 10.0.0.5-10.0.0.27` for 23
4052    /// hosts without finding a clean prefix.
4053    #[test]
4054    fn parse_ip_list_ipv4_range_inclusive() {
4055        let v = parse_ip_list(&["10.0.0.5-10.0.0.27".into()], "source-ip");
4056        assert_eq!(v.len(), 23);
4057        assert_eq!(v.first().unwrap().to_string(), "10.0.0.5");
4058        assert_eq!(v.last().unwrap().to_string(), "10.0.0.27");
4059    }
4060
4061    /// Round 22.4 — range with start > end is rejected with a warning
4062    /// (returns nothing for that entry rather than wrapping around).
4063    #[test]
4064    fn parse_ip_list_range_rejects_backwards() {
4065        let v = parse_ip_list(&["10.0.0.10-10.0.0.5".into()], "source-ip");
4066        assert!(v.is_empty(), "backwards range should produce no IPs; got {v:?}");
4067    }
4068
4069    /// Round 22.4 — IPv6 ranges are intentionally rejected because
4070    /// `2001:db8::1-2001:db8::5` would ambiguously parse against the
4071    /// address literal's `:` separators. Users use CIDR for IPv6.
4072    #[test]
4073    fn parse_ip_list_rejects_ipv6_range_syntax() {
4074        let v = parse_ip_list(&["2001:db8::1-2001:db8::5".into()], "geo-source-ip");
4075        assert!(v.is_empty(), "IPv6 range should be rejected; got {v:?}");
4076    }
4077
4078    /// Round 22.4 — range cap is the same 256 host limit as CIDR.
4079    #[test]
4080    fn parse_ip_list_range_capped_at_256() {
4081        let v = parse_ip_list(&["10.0.0.0-10.0.5.0".into()], "source-ip");
4082        assert_eq!(v.len(), 256);
4083        assert_eq!(v.first().unwrap().to_string(), "10.0.0.0");
4084    }
4085
4086    /// Round 19 — single IPs and comma-separated lists already
4087    /// worked in 18.5; this regression-locks the parse paths.
4088    #[test]
4089    fn parse_ip_list_plain_and_comma() {
4090        let v = parse_ip_list(&["10.0.0.5".into(), "10.0.0.6,10.0.0.7".into()], "source-ip");
4091        assert_eq!(v.len(), 3);
4092        assert_eq!(v[0].to_string(), "10.0.0.5");
4093        assert_eq!(v[2].to_string(), "10.0.0.7");
4094    }
4095
4096    /// Round 19 — IPv4 CIDR expands to host count up to the cap.
4097    /// `/29` = 8 hosts (well under cap), all 8 enumerated.
4098    #[test]
4099    fn parse_ip_list_ipv4_cidr_29_expands_to_8() {
4100        let v = parse_ip_list(&["10.0.0.0/29".into()], "source-ip");
4101        assert_eq!(v.len(), 8);
4102        assert_eq!(v[0].to_string(), "10.0.0.0");
4103        assert_eq!(v[7].to_string(), "10.0.0.7");
4104    }
4105
4106    /// Round 19 — IPv4 CIDR larger than the cap is truncated, not
4107    /// rejected. Cap is 256; `/8` would be 16M without the guard.
4108    #[test]
4109    fn parse_ip_list_ipv4_cidr_8_capped_at_256() {
4110        let v = parse_ip_list(&["10.0.0.0/8".into()], "source-ip");
4111        assert_eq!(v.len(), 256);
4112        assert_eq!(v[0].to_string(), "10.0.0.0");
4113        assert_eq!(v[255].to_string(), "10.0.0.255");
4114    }
4115
4116    /// Round 19 — IPv6 CIDR also expands. `/126` = 4 hosts.
4117    #[test]
4118    fn parse_ip_list_ipv6_cidr_126_expands_to_4() {
4119        let v = parse_ip_list(&["2001:db8::/126".into()], "geo-source-ip");
4120        assert_eq!(v.len(), 4);
4121        assert!(v[0].is_ipv6());
4122        assert_eq!(v[0].to_string(), "2001:db8::");
4123        assert_eq!(v[3].to_string(), "2001:db8::3");
4124    }
4125
4126    /// Round 19 — mixed IPv4 + IPv6 + CIDR in one call works.
4127    #[test]
4128    fn parse_ip_list_mixed_v4_v6_cidr() {
4129        let v = parse_ip_list(&["10.0.0.0/30,2001:db8::1,203.0.113.42".into()], "geo-source-ip");
4130        assert_eq!(v.len(), 6); // 4 from /30 + 1 + 1
4131        assert!(v.iter().any(|ip| ip.to_string() == "2001:db8::1"));
4132        assert!(v.iter().any(|ip| ip.to_string() == "203.0.113.42"));
4133    }
4134
4135    /// Round 19 — malformed entries log and skip; the run continues
4136    /// with whatever resolved.
4137    #[test]
4138    fn parse_ip_list_skips_malformed() {
4139        let v = parse_ip_list(
4140            &[
4141                "10.0.0.5".into(),
4142                "not-an-ip".into(),
4143                "10.0.0.6".into(),
4144                "/24".into(),
4145                "1.2.3.4/200".into(),
4146            ],
4147            "source-ip",
4148        );
4149        assert_eq!(v.len(), 2);
4150        assert_eq!(v[0].to_string(), "10.0.0.5");
4151        assert_eq!(v[1].to_string(), "10.0.0.6");
4152    }
4153
4154    #[test]
4155    fn test_parse_duration_invalid() {
4156        assert!(BenchCommand::parse_duration("invalid").is_err());
4157        assert!(BenchCommand::parse_duration("30x").is_err());
4158    }
4159
4160    #[test]
4161    fn test_parse_headers() {
4162        let cmd = BenchCommand {
4163            spec: vec![PathBuf::from("test.yaml")],
4164            spec_dir: None,
4165            merge_conflicts: "error".to_string(),
4166            spec_mode: "merge".to_string(),
4167            dependency_config: None,
4168            target: "http://localhost".to_string(),
4169            base_path: None,
4170            duration: "1m".to_string(),
4171            vus: 10,
4172            scenario: "ramp-up".to_string(),
4173            operations: None,
4174            exclude_operations: None,
4175            auth: None,
4176            headers: vec![
4177                "X-API-Key:test123".to_string(),
4178                "X-Client-ID:client456".to_string(),
4179            ],
4180            output: PathBuf::from("output"),
4181            generate_only: false,
4182            script_output: None,
4183            threshold_percentile: "p(95)".to_string(),
4184            threshold_ms: 500,
4185            max_error_rate: 0.05,
4186            abort_on_error: true,
4187            abort_on_error_rate: 0.95,
4188            verbose: false,
4189            skip_tls_verify: false,
4190            chunked_request_bodies: false,
4191            target_rps: None,
4192            no_keep_alive: false,
4193            targets_file: None,
4194            max_concurrency: None,
4195            results_format: "both".to_string(),
4196            params_file: None,
4197            crud_flow: false,
4198            flow_config: None,
4199            extract_fields: None,
4200            parallel_create: None,
4201            data_file: None,
4202            data_distribution: "unique-per-vu".to_string(),
4203            data_mappings: None,
4204            per_uri_control: false,
4205            error_rate: None,
4206            error_types: None,
4207            security_test: false,
4208            security_payloads: None,
4209            security_categories: None,
4210            security_target_fields: None,
4211            wafbench_dir: None,
4212            wafbench_cycle_all: false,
4213            owasp_api_top10: false,
4214            owasp_categories: None,
4215            owasp_auth_header: "Authorization".to_string(),
4216            owasp_auth_token: None,
4217            owasp_admin_paths: None,
4218            owasp_id_fields: None,
4219            owasp_report: None,
4220            owasp_report_format: "json".to_string(),
4221            owasp_iterations: 1,
4222            conformance: false,
4223            conformance_api_key: None,
4224            conformance_basic_auth: None,
4225            conformance_report: PathBuf::from("conformance-report.json"),
4226            conformance_categories: None,
4227            conformance_report_format: "json".to_string(),
4228            conformance_headers: vec![],
4229            conformance_all_operations: false,
4230            conformance_custom: None,
4231            conformance_delay_ms: 0,
4232            use_k6: false,
4233            conformance_custom_filter: None,
4234            export_requests: false,
4235            validate_requests: false,
4236            conformance_self_test: false,
4237            conformance_self_test_capture: false,
4238            conformance_self_test_iterations: 1,
4239            conformance_self_test_duration: None,
4240            validate_response_schemas: false,
4241            source_ips: Vec::new(),
4242            geo_source_ips: Vec::new(),
4243            geo_source_headers: Vec::new(),
4244            report_missed_cap: None,
4245            discard_response_bodies: false,
4246            dns_policy: None,
4247        };
4248
4249        let headers = cmd.parse_headers().unwrap();
4250        assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
4251        assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
4252    }
4253
4254    #[test]
4255    fn test_parse_header_string_preserves_comma_in_value() {
4256        // #761: with one header per --headers flag, a comma in the value (e.g. a
4257        // Cookie expiry date) is preserved instead of being split into junk pairs.
4258        let inputs = vec![
4259            "Cookie:session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string(),
4260            "X-Trace:1".to_string(),
4261        ];
4262        let headers = parse_header_string(&inputs).unwrap();
4263        assert_eq!(
4264            headers.get("Cookie"),
4265            Some(&"session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string())
4266        );
4267        assert_eq!(headers.get("X-Trace"), Some(&"1".to_string()));
4268    }
4269
4270    /// Round 64 (#79). `execute_multi_target` hand-copies `BenchCommand` field
4271    /// by field into the command it hands `ParallelExecutor`. It nulled the
4272    /// conformance auth shortcuts, so `parse_headers()` — which has folded them
4273    /// into the shared header map since round 47 — had nothing left to fold.
4274    /// Single-target sent `Authorization`; multi-target silently sent nothing,
4275    /// which is what Srikanth saw on 0.3.210: credentials absent from both his
4276    /// PCAP and his proxy.
4277    ///
4278    /// A behavioural test cannot reach that clone (`execute_multi_target` is
4279    /// async, parses a targets file and shells out to k6), so this guards the
4280    /// source: every field `parse_headers()` reads must survive the clone.
4281    /// Field-by-field struct literals silently drop things; this makes the drop
4282    /// a test failure instead of an empty Authorization header.
4283    #[test]
4284    fn multi_target_clone_preserves_fields_parse_headers_reads() {
4285        let src = include_str!("command.rs");
4286
4287        let fn_start = src
4288            .find("async fn execute_multi_target(")
4289            .expect("execute_multi_target should exist");
4290        let block_start = src[fn_start..]
4291            .find("ParallelExecutor::new(")
4292            .map(|i| i + fn_start)
4293            .expect("multi-target path should build a ParallelExecutor");
4294        // The BenchCommand literal ends at the executor call's closing paren.
4295        let block_end = src[block_start..]
4296            .find("\n        );")
4297            .map(|i| i + block_start)
4298            .expect("ParallelExecutor::new(..) should be closed");
4299        let block = &src[block_start..block_end];
4300
4301        // Read straight out of parse_headers' doc/body contract: these are the
4302        // auth-bearing inputs it folds. Keep in sync if that fold grows.
4303        for field in ["conformance_basic_auth", "conformance_headers"] {
4304            for zeroed in [format!("{field}: None"), format!("{field}: vec![]")] {
4305                assert!(
4306                    !block.contains(&zeroed),
4307                    "execute_multi_target zeroes `{zeroed}`. parse_headers() folds `{field}` \
4308                     into the header map, so zeroing it here strips auth from every \
4309                     multi-target run while single-target keeps working (#79 round 64)."
4310                );
4311            }
4312            let passthrough = format!("{field}: self.{field}.clone()");
4313            assert!(
4314                block.contains(&passthrough),
4315                "execute_multi_target must carry `{field}` through as `{passthrough}` so \
4316                 parse_headers() can fold it (#79 round 64)."
4317            );
4318        }
4319    }
4320
4321    #[test]
4322    fn test_get_spec_display_name() {
4323        let cmd = BenchCommand {
4324            spec: vec![PathBuf::from("test.yaml")],
4325            spec_dir: None,
4326            merge_conflicts: "error".to_string(),
4327            spec_mode: "merge".to_string(),
4328            dependency_config: None,
4329            target: "http://localhost".to_string(),
4330            base_path: None,
4331            duration: "1m".to_string(),
4332            vus: 10,
4333            scenario: "ramp-up".to_string(),
4334            operations: None,
4335            exclude_operations: None,
4336            auth: None,
4337            headers: Vec::new(),
4338            output: PathBuf::from("output"),
4339            generate_only: false,
4340            script_output: None,
4341            threshold_percentile: "p(95)".to_string(),
4342            threshold_ms: 500,
4343            max_error_rate: 0.05,
4344            abort_on_error: true,
4345            abort_on_error_rate: 0.95,
4346            verbose: false,
4347            skip_tls_verify: false,
4348            chunked_request_bodies: false,
4349            target_rps: None,
4350            no_keep_alive: false,
4351            targets_file: None,
4352            max_concurrency: None,
4353            results_format: "both".to_string(),
4354            params_file: None,
4355            crud_flow: false,
4356            flow_config: None,
4357            extract_fields: None,
4358            parallel_create: None,
4359            data_file: None,
4360            data_distribution: "unique-per-vu".to_string(),
4361            data_mappings: None,
4362            per_uri_control: false,
4363            error_rate: None,
4364            error_types: None,
4365            security_test: false,
4366            security_payloads: None,
4367            security_categories: None,
4368            security_target_fields: None,
4369            wafbench_dir: None,
4370            wafbench_cycle_all: false,
4371            owasp_api_top10: false,
4372            owasp_categories: None,
4373            owasp_auth_header: "Authorization".to_string(),
4374            owasp_auth_token: None,
4375            owasp_admin_paths: None,
4376            owasp_id_fields: None,
4377            owasp_report: None,
4378            owasp_report_format: "json".to_string(),
4379            owasp_iterations: 1,
4380            conformance: false,
4381            conformance_api_key: None,
4382            conformance_basic_auth: None,
4383            conformance_report: PathBuf::from("conformance-report.json"),
4384            conformance_categories: None,
4385            conformance_report_format: "json".to_string(),
4386            conformance_headers: vec![],
4387            conformance_all_operations: false,
4388            conformance_custom: None,
4389            conformance_delay_ms: 0,
4390            use_k6: false,
4391            conformance_custom_filter: None,
4392            export_requests: false,
4393            validate_requests: false,
4394            conformance_self_test: false,
4395            conformance_self_test_capture: false,
4396            conformance_self_test_iterations: 1,
4397            conformance_self_test_duration: None,
4398            validate_response_schemas: false,
4399            source_ips: Vec::new(),
4400            geo_source_ips: Vec::new(),
4401            geo_source_headers: Vec::new(),
4402            report_missed_cap: None,
4403            discard_response_bodies: false,
4404            dns_policy: None,
4405        };
4406
4407        assert_eq!(cmd.get_spec_display_name(), "test.yaml");
4408
4409        // Test multiple specs
4410        let cmd_multi = BenchCommand {
4411            spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
4412            spec_dir: None,
4413            merge_conflicts: "error".to_string(),
4414            spec_mode: "merge".to_string(),
4415            dependency_config: None,
4416            target: "http://localhost".to_string(),
4417            base_path: None,
4418            duration: "1m".to_string(),
4419            vus: 10,
4420            scenario: "ramp-up".to_string(),
4421            operations: None,
4422            exclude_operations: None,
4423            auth: None,
4424            headers: Vec::new(),
4425            output: PathBuf::from("output"),
4426            generate_only: false,
4427            script_output: None,
4428            threshold_percentile: "p(95)".to_string(),
4429            threshold_ms: 500,
4430            max_error_rate: 0.05,
4431            abort_on_error: true,
4432            abort_on_error_rate: 0.95,
4433            verbose: false,
4434            skip_tls_verify: false,
4435            chunked_request_bodies: false,
4436            target_rps: None,
4437            no_keep_alive: false,
4438            targets_file: None,
4439            max_concurrency: None,
4440            results_format: "both".to_string(),
4441            params_file: None,
4442            crud_flow: false,
4443            flow_config: None,
4444            extract_fields: None,
4445            parallel_create: None,
4446            data_file: None,
4447            data_distribution: "unique-per-vu".to_string(),
4448            data_mappings: None,
4449            per_uri_control: false,
4450            error_rate: None,
4451            error_types: None,
4452            security_test: false,
4453            security_payloads: None,
4454            security_categories: None,
4455            security_target_fields: None,
4456            wafbench_dir: None,
4457            wafbench_cycle_all: false,
4458            owasp_api_top10: false,
4459            owasp_categories: None,
4460            owasp_auth_header: "Authorization".to_string(),
4461            owasp_auth_token: None,
4462            owasp_admin_paths: None,
4463            owasp_id_fields: None,
4464            owasp_report: None,
4465            owasp_report_format: "json".to_string(),
4466            owasp_iterations: 1,
4467            conformance: false,
4468            conformance_api_key: None,
4469            conformance_basic_auth: None,
4470            conformance_report: PathBuf::from("conformance-report.json"),
4471            conformance_categories: None,
4472            conformance_report_format: "json".to_string(),
4473            conformance_headers: vec![],
4474            conformance_all_operations: false,
4475            conformance_custom: None,
4476            conformance_delay_ms: 0,
4477            use_k6: false,
4478            conformance_custom_filter: None,
4479            export_requests: false,
4480            validate_requests: false,
4481            conformance_self_test: false,
4482            conformance_self_test_capture: false,
4483            conformance_self_test_iterations: 1,
4484            conformance_self_test_duration: None,
4485            validate_response_schemas: false,
4486            source_ips: Vec::new(),
4487            geo_source_ips: Vec::new(),
4488            geo_source_headers: Vec::new(),
4489            report_missed_cap: None,
4490            discard_response_bodies: false,
4491            dns_policy: None,
4492        };
4493
4494        assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
4495    }
4496
4497    #[test]
4498    fn test_parse_extracted_values_from_output_dir() {
4499        let dir = tempdir().unwrap();
4500        let path = dir.path().join("extracted_values.json");
4501        std::fs::write(
4502            &path,
4503            r#"{
4504  "pool_id": "abc123",
4505  "count": 0,
4506  "enabled": false,
4507  "metadata": { "owner": "team-a" }
4508}"#,
4509        )
4510        .unwrap();
4511
4512        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4513        assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
4514        assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
4515        assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
4516        assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
4517    }
4518
4519    #[test]
4520    fn test_parse_extracted_values_missing_file() {
4521        let dir = tempdir().unwrap();
4522        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4523        assert!(extracted.values.is_empty());
4524    }
4525}