Skip to main content

mockforge_bench/conformance/
spec_driven.rs

1//! Spec-driven conformance testing
2//!
3//! Analyzes the user's OpenAPI spec to determine which features their API uses,
4//! then generates k6 conformance tests against their real endpoints.
5
6use super::generator::ConformanceConfig;
7use super::schema_validator::SchemaValidatorGenerator;
8use super::spec::ConformanceFeature;
9use crate::error::Result;
10use crate::request_gen::RequestGenerator;
11use crate::spec_parser::ApiOperation;
12use openapiv3::{
13    OpenAPI, Operation, Parameter, ParameterSchemaOrContent, ReferenceOr, RequestBody, Response,
14    Schema, SchemaKind, SecurityScheme, StringFormat, Type, VariantOrUnknownOrEmpty,
15};
16use std::collections::HashSet;
17
18/// Resolve `$ref` references against the OpenAPI components
19mod ref_resolver {
20    use super::*;
21
22    pub fn resolve_parameter<'a>(
23        param_ref: &'a ReferenceOr<Parameter>,
24        spec: &'a OpenAPI,
25    ) -> Option<&'a Parameter> {
26        match param_ref {
27            ReferenceOr::Item(param) => Some(param),
28            ReferenceOr::Reference { reference } => {
29                let name = reference.strip_prefix("#/components/parameters/")?;
30                let components = spec.components.as_ref()?;
31                match components.parameters.get(name)? {
32                    ReferenceOr::Item(param) => Some(param),
33                    ReferenceOr::Reference {
34                        reference: inner_ref,
35                    } => {
36                        // One level of recursive resolution
37                        let inner_name = inner_ref.strip_prefix("#/components/parameters/")?;
38                        match components.parameters.get(inner_name)? {
39                            ReferenceOr::Item(param) => Some(param),
40                            ReferenceOr::Reference { .. } => None,
41                        }
42                    }
43                }
44            }
45        }
46    }
47
48    pub fn resolve_request_body<'a>(
49        body_ref: &'a ReferenceOr<RequestBody>,
50        spec: &'a OpenAPI,
51    ) -> Option<&'a RequestBody> {
52        match body_ref {
53            ReferenceOr::Item(body) => Some(body),
54            ReferenceOr::Reference { reference } => {
55                let name = reference.strip_prefix("#/components/requestBodies/")?;
56                let components = spec.components.as_ref()?;
57                match components.request_bodies.get(name)? {
58                    ReferenceOr::Item(body) => Some(body),
59                    ReferenceOr::Reference {
60                        reference: inner_ref,
61                    } => {
62                        // One level of recursive resolution
63                        let inner_name = inner_ref.strip_prefix("#/components/requestBodies/")?;
64                        match components.request_bodies.get(inner_name)? {
65                            ReferenceOr::Item(body) => Some(body),
66                            ReferenceOr::Reference { .. } => None,
67                        }
68                    }
69                }
70            }
71        }
72    }
73
74    pub fn resolve_schema<'a>(
75        schema_ref: &'a ReferenceOr<Schema>,
76        spec: &'a OpenAPI,
77    ) -> Option<&'a Schema> {
78        resolve_schema_with_visited(schema_ref, spec, &mut HashSet::new())
79    }
80
81    fn resolve_schema_with_visited<'a>(
82        schema_ref: &'a ReferenceOr<Schema>,
83        spec: &'a OpenAPI,
84        visited: &mut HashSet<String>,
85    ) -> Option<&'a Schema> {
86        match schema_ref {
87            ReferenceOr::Item(schema) => Some(schema),
88            ReferenceOr::Reference { reference } => {
89                if !visited.insert(reference.clone()) {
90                    return None; // Cycle detected
91                }
92                let name = reference.strip_prefix("#/components/schemas/")?;
93                let components = spec.components.as_ref()?;
94                let nested = components.schemas.get(name)?;
95                resolve_schema_with_visited(nested, spec, visited)
96            }
97        }
98    }
99
100    /// Resolve a boxed schema reference (used by array items and object properties)
101    pub fn resolve_boxed_schema<'a>(
102        schema_ref: &'a ReferenceOr<Box<Schema>>,
103        spec: &'a OpenAPI,
104    ) -> Option<&'a Schema> {
105        match schema_ref {
106            ReferenceOr::Item(schema) => Some(schema.as_ref()),
107            ReferenceOr::Reference { reference } => {
108                // Delegate to the regular schema resolver
109                let name = reference.strip_prefix("#/components/schemas/")?;
110                let components = spec.components.as_ref()?;
111                let nested = components.schemas.get(name)?;
112                resolve_schema_with_visited(nested, spec, &mut HashSet::new())
113            }
114        }
115    }
116
117    pub fn resolve_response<'a>(
118        resp_ref: &'a ReferenceOr<Response>,
119        spec: &'a OpenAPI,
120    ) -> Option<&'a Response> {
121        match resp_ref {
122            ReferenceOr::Item(resp) => Some(resp),
123            ReferenceOr::Reference { reference } => {
124                let name = reference.strip_prefix("#/components/responses/")?;
125                let components = spec.components.as_ref()?;
126                match components.responses.get(name)? {
127                    ReferenceOr::Item(resp) => Some(resp),
128                    ReferenceOr::Reference {
129                        reference: inner_ref,
130                    } => {
131                        // One level of recursive resolution
132                        let inner_name = inner_ref.strip_prefix("#/components/responses/")?;
133                        match components.responses.get(inner_name)? {
134                            ReferenceOr::Item(resp) => Some(resp),
135                            ReferenceOr::Reference { .. } => None,
136                        }
137                    }
138                }
139            }
140        }
141    }
142}
143
144/// Resolved security scheme details for an operation
145#[derive(Debug, Clone)]
146pub enum SecuritySchemeInfo {
147    /// HTTP Bearer token
148    Bearer,
149    /// HTTP Basic auth
150    Basic,
151    /// API Key in header, query, or cookie
152    ApiKey {
153        location: ApiKeyLocation,
154        name: String,
155    },
156}
157
158/// Where an API key is transmitted
159#[derive(Debug, Clone, PartialEq)]
160pub enum ApiKeyLocation {
161    Header,
162    Query,
163    Cookie,
164}
165
166/// An API operation annotated with the conformance features it exercises
167#[derive(Debug, Clone)]
168pub struct AnnotatedOperation {
169    pub path: String,
170    pub method: String,
171    pub features: Vec<ConformanceFeature>,
172    pub request_body_content_type: Option<String>,
173    pub sample_body: Option<String>,
174    pub query_params: Vec<(String, String)>,
175    pub header_params: Vec<(String, String)>,
176    pub path_params: Vec<(String, String)>,
177    /// Response schema for validation (JSON string of the schema)
178    pub response_schema: Option<Schema>,
179    /// Round 25 — per-status response schemas (JSON values, pre-resolved
180    /// via $ref). Keyed by HTTP status code, populated for every
181    /// status declared in the spec that has an `application/json`
182    /// body. The self-test driver looks up the schema for each probe's
183    /// actual status and validates the response body against it
184    /// (closing round 21.3 / Srikanth's a2 / a3 ask). Empty when the
185    /// spec declares no JSON response bodies.
186    pub response_schemas: std::collections::BTreeMap<u16, serde_json::Value>,
187    /// Round 17.2 — the resolved request-body schema (application/json).
188    /// Used by the self-test driver to synthesise schema-aware
189    /// negative mutations (wrong type, min/max bounds, pattern, enum
190    /// out-of-range, required-field removal). Independent of
191    /// `sample_body`, which is the positive payload.
192    pub request_body_schema: Option<Schema>,
193    /// Security scheme details resolved from the OpenAPI spec
194    pub security_schemes: Vec<SecuritySchemeInfo>,
195}
196
197/// Generates spec-driven conformance k6 scripts
198pub struct SpecDrivenConformanceGenerator {
199    config: ConformanceConfig,
200    operations: Vec<AnnotatedOperation>,
201}
202
203impl SpecDrivenConformanceGenerator {
204    pub fn new(config: ConformanceConfig, operations: Vec<AnnotatedOperation>) -> Self {
205        Self { config, operations }
206    }
207
208    /// Annotate a list of API operations with conformance features
209    pub fn annotate_operations(
210        operations: &[ApiOperation],
211        spec: &OpenAPI,
212    ) -> Vec<AnnotatedOperation> {
213        operations.iter().map(|op| Self::annotate_operation(op, spec)).collect()
214    }
215
216    /// Analyze an operation and determine which conformance features it exercises
217    fn annotate_operation(op: &ApiOperation, spec: &OpenAPI) -> AnnotatedOperation {
218        let mut features = Vec::new();
219        let mut query_params = Vec::new();
220        let mut header_params = Vec::new();
221        let mut path_params = Vec::new();
222
223        // Detect HTTP method feature
224        match op.method.to_uppercase().as_str() {
225            "GET" => features.push(ConformanceFeature::MethodGet),
226            "POST" => features.push(ConformanceFeature::MethodPost),
227            "PUT" => features.push(ConformanceFeature::MethodPut),
228            "PATCH" => features.push(ConformanceFeature::MethodPatch),
229            "DELETE" => features.push(ConformanceFeature::MethodDelete),
230            "HEAD" => features.push(ConformanceFeature::MethodHead),
231            "OPTIONS" => features.push(ConformanceFeature::MethodOptions),
232            _ => {}
233        }
234
235        // Detect parameter features (resolves $ref)
236        for param_ref in &op.operation.parameters {
237            if let Some(param) = ref_resolver::resolve_parameter(param_ref, spec) {
238                Self::annotate_parameter(
239                    param,
240                    spec,
241                    &mut features,
242                    &mut query_params,
243                    &mut header_params,
244                    &mut path_params,
245                );
246            }
247        }
248
249        // Detect path parameters from the path template itself
250        for segment in op.path.split('/') {
251            if segment.starts_with('{') && segment.ends_with('}') {
252                let name = &segment[1..segment.len() - 1];
253                // Only add if not already found from parameters
254                if !path_params.iter().any(|(n, _)| n == name) {
255                    path_params.push((name.to_string(), "test-value".to_string()));
256                    // Determine type from path params we didn't already handle
257                    if !features.contains(&ConformanceFeature::PathParamString)
258                        && !features.contains(&ConformanceFeature::PathParamInteger)
259                    {
260                        features.push(ConformanceFeature::PathParamString);
261                    }
262                }
263            }
264        }
265
266        // Detect request body features (resolves $ref)
267        let mut request_body_content_type = None;
268        let mut sample_body = None;
269        let mut request_body_schema: Option<Schema> = None;
270
271        let resolved_body = op
272            .operation
273            .request_body
274            .as_ref()
275            .and_then(|b| ref_resolver::resolve_request_body(b, spec));
276
277        if let Some(body) = resolved_body {
278            for (content_type, _media) in &body.content {
279                match content_type.as_str() {
280                    "application/json" => {
281                        features.push(ConformanceFeature::BodyJson);
282                        request_body_content_type = Some("application/json".to_string());
283                        // Generate sample body from schema
284                        if let Ok(template) = RequestGenerator::generate_template(op) {
285                            if let Some(body_val) = &template.body {
286                                sample_body = Some(body_val.to_string());
287                            }
288                        }
289                    }
290                    "application/x-www-form-urlencoded" => {
291                        features.push(ConformanceFeature::BodyFormUrlencoded);
292                        request_body_content_type =
293                            Some("application/x-www-form-urlencoded".to_string());
294                    }
295                    "multipart/form-data" => {
296                        features.push(ConformanceFeature::BodyMultipart);
297                        request_body_content_type = Some("multipart/form-data".to_string());
298                    }
299                    _ => {}
300                }
301            }
302
303            // Detect schema features in request body (resolves $ref in schema)
304            if let Some(media) = body.content.get("application/json") {
305                if let Some(schema_ref) = &media.schema {
306                    if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
307                        Self::annotate_schema(schema, spec, &mut features);
308                        // Round 17.2 — keep a clone of the resolved
309                        // body schema so the self-test driver can walk
310                        // it to synthesise schema-aware negatives.
311                        request_body_schema = Some(schema.clone());
312                    }
313                }
314            }
315        }
316
317        // Detect response code features
318        Self::annotate_responses(&op.operation, spec, &mut features);
319
320        // Extract response schema for validation (resolves $ref)
321        let response_schema = Self::extract_response_schema(&op.operation, spec);
322        if response_schema.is_some() {
323            features.push(ConformanceFeature::ResponseValidation);
324        }
325        // Round 25 — per-status response schema map (closes round 21.3).
326        let response_schemas = Self::extract_response_schemas_per_status(&op.operation, spec);
327
328        // Detect content negotiation (response with multiple content types)
329        Self::annotate_content_negotiation(&op.operation, spec, &mut features);
330
331        // Detect security features and resolve scheme details
332        let mut security_schemes = Vec::new();
333        Self::annotate_security(&op.operation, spec, &mut features, &mut security_schemes);
334
335        // Deduplicate features
336        features.sort_by_key(|f| f.check_name());
337        features.dedup_by_key(|f| f.check_name());
338
339        AnnotatedOperation {
340            path: op.path.clone(),
341            method: op.method.to_uppercase(),
342            features,
343            request_body_content_type,
344            sample_body,
345            query_params,
346            header_params,
347            path_params,
348            response_schema,
349            response_schemas,
350            request_body_schema,
351            security_schemes,
352        }
353    }
354
355    /// Annotate parameter features
356    fn annotate_parameter(
357        param: &Parameter,
358        spec: &OpenAPI,
359        features: &mut Vec<ConformanceFeature>,
360        query_params: &mut Vec<(String, String)>,
361        header_params: &mut Vec<(String, String)>,
362        path_params: &mut Vec<(String, String)>,
363    ) {
364        let (location, data) = match param {
365            Parameter::Query { parameter_data, .. } => ("query", parameter_data),
366            Parameter::Path { parameter_data, .. } => ("path", parameter_data),
367            Parameter::Header { parameter_data, .. } => ("header", parameter_data),
368            Parameter::Cookie { .. } => {
369                features.push(ConformanceFeature::CookieParam);
370                return;
371            }
372        };
373
374        // Detect type from schema
375        let is_integer = Self::param_schema_is_integer(data, spec);
376        let is_array = Self::param_schema_is_array(data, spec);
377
378        // Generate sample value
379        let sample = if is_integer {
380            "42".to_string()
381        } else if is_array {
382            "a,b".to_string()
383        } else {
384            "test-value".to_string()
385        };
386
387        match location {
388            "path" => {
389                if is_integer {
390                    features.push(ConformanceFeature::PathParamInteger);
391                } else {
392                    features.push(ConformanceFeature::PathParamString);
393                }
394                path_params.push((data.name.clone(), sample));
395            }
396            "query" => {
397                if is_array {
398                    features.push(ConformanceFeature::QueryParamArray);
399                } else if is_integer {
400                    features.push(ConformanceFeature::QueryParamInteger);
401                } else {
402                    features.push(ConformanceFeature::QueryParamString);
403                }
404                query_params.push((data.name.clone(), sample));
405            }
406            "header" => {
407                features.push(ConformanceFeature::HeaderParam);
408                header_params.push((data.name.clone(), sample));
409            }
410            _ => {}
411        }
412
413        // Check for constraint features on the parameter (resolves $ref)
414        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
415            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
416                Self::annotate_schema(schema, spec, features);
417            }
418        }
419
420        // Required/optional
421        if data.required {
422            features.push(ConformanceFeature::ConstraintRequired);
423        } else {
424            features.push(ConformanceFeature::ConstraintOptional);
425        }
426    }
427
428    fn param_schema_is_integer(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
429        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
430            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
431                return matches!(&schema.schema_kind, SchemaKind::Type(Type::Integer(_)));
432            }
433        }
434        false
435    }
436
437    fn param_schema_is_array(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
438        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
439            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
440                return matches!(&schema.schema_kind, SchemaKind::Type(Type::Array(_)));
441            }
442        }
443        false
444    }
445
446    /// Annotate schema-level features (types, composition, formats, constraints)
447    fn annotate_schema(schema: &Schema, spec: &OpenAPI, features: &mut Vec<ConformanceFeature>) {
448        match &schema.schema_kind {
449            SchemaKind::Type(Type::String(s)) => {
450                features.push(ConformanceFeature::SchemaString);
451                // Check format
452                match &s.format {
453                    VariantOrUnknownOrEmpty::Item(StringFormat::Date) => {
454                        features.push(ConformanceFeature::FormatDate);
455                    }
456                    VariantOrUnknownOrEmpty::Item(StringFormat::DateTime) => {
457                        features.push(ConformanceFeature::FormatDateTime);
458                    }
459                    VariantOrUnknownOrEmpty::Unknown(fmt) => match fmt.as_str() {
460                        "email" => features.push(ConformanceFeature::FormatEmail),
461                        "uuid" => features.push(ConformanceFeature::FormatUuid),
462                        "uri" | "url" => features.push(ConformanceFeature::FormatUri),
463                        "ipv4" => features.push(ConformanceFeature::FormatIpv4),
464                        "ipv6" => features.push(ConformanceFeature::FormatIpv6),
465                        _ => {}
466                    },
467                    _ => {}
468                }
469                // Check constraints
470                if s.pattern.is_some() {
471                    features.push(ConformanceFeature::ConstraintPattern);
472                }
473                if !s.enumeration.is_empty() {
474                    features.push(ConformanceFeature::ConstraintEnum);
475                }
476                if s.min_length.is_some() || s.max_length.is_some() {
477                    features.push(ConformanceFeature::ConstraintMinMax);
478                }
479            }
480            SchemaKind::Type(Type::Integer(i)) => {
481                features.push(ConformanceFeature::SchemaInteger);
482                if i.minimum.is_some() || i.maximum.is_some() {
483                    features.push(ConformanceFeature::ConstraintMinMax);
484                }
485                if !i.enumeration.is_empty() {
486                    features.push(ConformanceFeature::ConstraintEnum);
487                }
488            }
489            SchemaKind::Type(Type::Number(n)) => {
490                features.push(ConformanceFeature::SchemaNumber);
491                if n.minimum.is_some() || n.maximum.is_some() {
492                    features.push(ConformanceFeature::ConstraintMinMax);
493                }
494            }
495            SchemaKind::Type(Type::Boolean(_)) => {
496                features.push(ConformanceFeature::SchemaBoolean);
497            }
498            SchemaKind::Type(Type::Array(arr)) => {
499                features.push(ConformanceFeature::SchemaArray);
500                if let Some(item_ref) = &arr.items {
501                    if let Some(item_schema) = ref_resolver::resolve_boxed_schema(item_ref, spec) {
502                        Self::annotate_schema(item_schema, spec, features);
503                    }
504                }
505            }
506            SchemaKind::Type(Type::Object(obj)) => {
507                features.push(ConformanceFeature::SchemaObject);
508                // Check required fields
509                if !obj.required.is_empty() {
510                    features.push(ConformanceFeature::ConstraintRequired);
511                }
512                // Walk properties (resolves $ref)
513                for (_name, prop_ref) in &obj.properties {
514                    if let Some(prop_schema) = ref_resolver::resolve_boxed_schema(prop_ref, spec) {
515                        Self::annotate_schema(prop_schema, spec, features);
516                    }
517                }
518            }
519            SchemaKind::OneOf { .. } => {
520                features.push(ConformanceFeature::CompositionOneOf);
521            }
522            SchemaKind::AnyOf { .. } => {
523                features.push(ConformanceFeature::CompositionAnyOf);
524            }
525            SchemaKind::AllOf { .. } => {
526                features.push(ConformanceFeature::CompositionAllOf);
527            }
528            _ => {}
529        }
530    }
531
532    /// Detect response code features (resolves $ref in responses)
533    fn annotate_responses(
534        operation: &Operation,
535        spec: &OpenAPI,
536        features: &mut Vec<ConformanceFeature>,
537    ) {
538        for (status_code, resp_ref) in &operation.responses.responses {
539            // Only count features for responses that actually resolve
540            if ref_resolver::resolve_response(resp_ref, spec).is_some() {
541                match status_code {
542                    openapiv3::StatusCode::Code(200) => {
543                        features.push(ConformanceFeature::Response200)
544                    }
545                    openapiv3::StatusCode::Code(201) => {
546                        features.push(ConformanceFeature::Response201)
547                    }
548                    openapiv3::StatusCode::Code(204) => {
549                        features.push(ConformanceFeature::Response204)
550                    }
551                    openapiv3::StatusCode::Code(400) => {
552                        features.push(ConformanceFeature::Response400)
553                    }
554                    openapiv3::StatusCode::Code(404) => {
555                        features.push(ConformanceFeature::Response404)
556                    }
557                    _ => {}
558                }
559            }
560        }
561    }
562
563    /// Round 25 — extract every declared response schema, keyed by the
564    /// numeric HTTP status. Only `application/json` content types are
565    /// included (the only body shape the self-test validates against).
566    /// Used by the self-test driver to validate response bodies against
567    /// the schema for the ACTUAL status returned, not just 200.
568    fn extract_response_schemas_per_status(
569        operation: &Operation,
570        spec: &OpenAPI,
571    ) -> std::collections::BTreeMap<u16, serde_json::Value> {
572        let mut out = std::collections::BTreeMap::new();
573        for (code, resp_ref) in &operation.responses.responses {
574            let openapiv3::StatusCode::Code(n) = code else {
575                continue; // skip "default" / range responses for now
576            };
577            let Some(response) = ref_resolver::resolve_response(resp_ref, spec) else {
578                continue;
579            };
580            let Some(media) = response.content.get("application/json") else {
581                continue;
582            };
583            let Some(schema_ref) = &media.schema else {
584                continue;
585            };
586            let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) else {
587                continue;
588            };
589            // Convert openapiv3::Schema → serde_json::Value so the
590            // validation step can hand it straight to jsonschema.
591            if let Ok(value) = serde_json::to_value(schema) {
592                out.insert(*n, value);
593            }
594        }
595        out
596    }
597
598    /// Extract the response schema for the primary success response (200 or 201)
599    /// Resolves $ref for both the response and the schema within it.
600    fn extract_response_schema(operation: &Operation, spec: &OpenAPI) -> Option<Schema> {
601        // Try 200 first, then 201
602        for code in [200u16, 201] {
603            if let Some(resp_ref) =
604                operation.responses.responses.get(&openapiv3::StatusCode::Code(code))
605            {
606                if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
607                    if let Some(media) = response.content.get("application/json") {
608                        if let Some(schema_ref) = &media.schema {
609                            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
610                                return Some(schema.clone());
611                            }
612                        }
613                    }
614                }
615            }
616        }
617        None
618    }
619
620    /// Detect content negotiation: response supports multiple content types
621    fn annotate_content_negotiation(
622        operation: &Operation,
623        spec: &OpenAPI,
624        features: &mut Vec<ConformanceFeature>,
625    ) {
626        for (_status_code, resp_ref) in &operation.responses.responses {
627            if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
628                if response.content.len() > 1 {
629                    features.push(ConformanceFeature::ContentNegotiation);
630                    return; // Only need to detect once per operation
631                }
632            }
633        }
634    }
635
636    /// Detect security scheme features.
637    /// Checks operation-level security first, falling back to global security requirements.
638    /// Resolves scheme names against SecurityScheme definitions in components.
639    fn annotate_security(
640        operation: &Operation,
641        spec: &OpenAPI,
642        features: &mut Vec<ConformanceFeature>,
643        security_schemes: &mut Vec<SecuritySchemeInfo>,
644    ) {
645        // Use operation-level security if present, otherwise fall back to global
646        let security_reqs = operation.security.as_ref().or(spec.security.as_ref());
647
648        if let Some(security) = security_reqs {
649            for security_req in security {
650                for scheme_name in security_req.keys() {
651                    // Try to resolve the scheme from components for accurate type detection
652                    if let Some(resolved) = Self::resolve_security_scheme(scheme_name, spec) {
653                        match resolved {
654                            SecurityScheme::HTTP { ref scheme, .. } => {
655                                if scheme.eq_ignore_ascii_case("bearer") {
656                                    features.push(ConformanceFeature::SecurityBearer);
657                                    security_schemes.push(SecuritySchemeInfo::Bearer);
658                                } else if scheme.eq_ignore_ascii_case("basic") {
659                                    features.push(ConformanceFeature::SecurityBasic);
660                                    security_schemes.push(SecuritySchemeInfo::Basic);
661                                }
662                            }
663                            SecurityScheme::APIKey { location, name, .. } => {
664                                features.push(ConformanceFeature::SecurityApiKey);
665                                let loc = match location {
666                                    openapiv3::APIKeyLocation::Query => ApiKeyLocation::Query,
667                                    openapiv3::APIKeyLocation::Header => ApiKeyLocation::Header,
668                                    openapiv3::APIKeyLocation::Cookie => ApiKeyLocation::Cookie,
669                                };
670                                security_schemes.push(SecuritySchemeInfo::ApiKey {
671                                    location: loc,
672                                    name: name.clone(),
673                                });
674                            }
675                            // OAuth2 and OpenIDConnect don't map to our current feature set
676                            _ => {}
677                        }
678                    } else {
679                        // Fallback: heuristic name matching for unresolvable schemes
680                        let name_lower = scheme_name.to_lowercase();
681                        if name_lower.contains("bearer") || name_lower.contains("jwt") {
682                            features.push(ConformanceFeature::SecurityBearer);
683                            security_schemes.push(SecuritySchemeInfo::Bearer);
684                        } else if name_lower.contains("api") && name_lower.contains("key") {
685                            features.push(ConformanceFeature::SecurityApiKey);
686                            security_schemes.push(SecuritySchemeInfo::ApiKey {
687                                location: ApiKeyLocation::Header,
688                                name: "X-API-Key".to_string(),
689                            });
690                        } else if name_lower.contains("basic") {
691                            features.push(ConformanceFeature::SecurityBasic);
692                            security_schemes.push(SecuritySchemeInfo::Basic);
693                        }
694                    }
695                }
696            }
697        }
698    }
699
700    /// Resolve a security scheme name to its SecurityScheme definition
701    fn resolve_security_scheme<'a>(name: &str, spec: &'a OpenAPI) -> Option<&'a SecurityScheme> {
702        let components = spec.components.as_ref()?;
703        match components.security_schemes.get(name)? {
704            ReferenceOr::Item(scheme) => Some(scheme),
705            ReferenceOr::Reference { .. } => None,
706        }
707    }
708
709    /// Returns the number of operations being tested
710    pub fn operation_count(&self) -> usize {
711        self.operations.len()
712    }
713
714    /// Generate the k6 conformance script.
715    /// Returns (script, check_count) where check_count is the number of unique checks emitted.
716    pub fn generate(&self) -> Result<(String, usize)> {
717        let mut script = String::with_capacity(16384);
718
719        // Imports
720        script.push_str("import http from 'k6/http';\n");
721        script.push_str("import { check, group } from 'k6';\n");
722        if self.config.request_delay_ms > 0 {
723            script.push_str("import { sleep } from 'k6';\n");
724        }
725        script.push('\n');
726
727        // Tell k6 that all HTTP status codes are "expected" in conformance mode.
728        // Without this, k6 counts 4xx responses (e.g. intentional 404 tests) as
729        // http_req_failed errors, producing a misleading error rate percentage.
730        script.push_str(
731            "http.setResponseCallback(http.expectedStatuses({ min: 100, max: 599 }));\n\n",
732        );
733
734        // Options
735        script.push_str("export const options = {\n");
736        script.push_str("  vus: 1,\n");
737        script.push_str("  iterations: 1,\n");
738        if self.config.skip_tls_verify {
739            script.push_str("  insecureSkipTLSVerify: true,\n");
740        }
741        script.push_str("  thresholds: {\n");
742        script.push_str("    checks: ['rate>0'],\n");
743        script.push_str("  },\n");
744        script.push_str("};\n\n");
745
746        // Base URL (includes base_path if configured)
747        script.push_str(&format!("const BASE_URL = '{}';\n\n", self.config.effective_base_url()));
748        script.push_str("const JSON_HEADERS = { 'Content-Type': 'application/json' };\n\n");
749
750        // Failure detail collector — logs req/res info for failed checks via console.log
751        // (k6's handleSummary runs in a separate JS context, so we can't use module-level arrays)
752        script
753            .push_str("function __captureFailure(checkName, res, expected, schemaViolations) {\n");
754        script.push_str("  let bodyStr = '';\n");
755        script.push_str("  try { bodyStr = res.body ? res.body.substring(0, 2000) : ''; } catch(e) { bodyStr = '<unreadable>'; }\n");
756        script.push_str("  let reqHeaders = {};\n");
757        script.push_str(
758            "  if (res.request && res.request.headers) { reqHeaders = res.request.headers; }\n",
759        );
760        script.push_str("  let reqBody = '';\n");
761        script.push_str("  if (res.request && res.request.body) { try { reqBody = res.request.body.substring(0, 2000); } catch(e) {} }\n");
762        script.push_str("  let payload = {\n");
763        script.push_str("    check: checkName,\n");
764        script.push_str("    request: {\n");
765        script.push_str("      method: res.request ? res.request.method : 'unknown',\n");
766        script.push_str("      url: res.request ? res.request.url : res.url || 'unknown',\n");
767        script.push_str("      headers: reqHeaders,\n");
768        script.push_str("      body: reqBody,\n");
769        script.push_str("    },\n");
770        script.push_str("    response: {\n");
771        script.push_str("      status: res.status,\n");
772        script.push_str("      headers: res.headers ? Object.fromEntries(Object.entries(res.headers).slice(0, 20)) : {},\n");
773        script.push_str("      body: bodyStr,\n");
774        script.push_str("    },\n");
775        script.push_str("    expected: expected,\n");
776        script.push_str("  };\n");
777        script.push_str("  if (schemaViolations && schemaViolations.length > 0) { payload.schema_violations = schemaViolations; }\n");
778        script.push_str("  console.log('MOCKFORGE_FAILURE:' + JSON.stringify(payload));\n");
779        script.push_str("}\n\n");
780
781        // Request/response capture for --export-requests
782        if self.config.export_requests {
783            script.push_str("function __captureExchange(checkName, res) {\n");
784            script.push_str("  let bodyStr = '';\n");
785            script.push_str("  try { bodyStr = res.body ? res.body.substring(0, 2000) : ''; } catch(e) { bodyStr = '<unreadable>'; }\n");
786            script.push_str("  let reqHeaders = {};\n");
787            script.push_str(
788                "  if (res.request && res.request.headers) { reqHeaders = res.request.headers; }\n",
789            );
790            script.push_str("  let reqBody = '';\n");
791            script.push_str("  if (res.request && res.request.body) { try { reqBody = res.request.body.substring(0, 2000); } catch(e) {} }\n");
792            script.push_str("  console.log('MOCKFORGE_EXCHANGE:' + JSON.stringify({\n");
793            script.push_str("    check: checkName,\n");
794            script.push_str("    request: {\n");
795            script.push_str("      method: res.request ? res.request.method : 'unknown',\n");
796            script.push_str("      url: res.request ? res.request.url : res.url || 'unknown',\n");
797            script.push_str("      headers: reqHeaders,\n");
798            script.push_str("      body: reqBody,\n");
799            script.push_str("    },\n");
800            script.push_str("    response: {\n");
801            script.push_str("      status: res.status,\n");
802            script.push_str("      headers: res.headers ? Object.fromEntries(Object.entries(res.headers).slice(0, 30)) : {},\n");
803            script.push_str("      body: bodyStr,\n");
804            script.push_str("    },\n");
805            script.push_str("  }));\n");
806            script.push_str("}\n\n");
807        }
808
809        // Default function
810        script.push_str("export default function () {\n");
811
812        if self.config.has_cookie_header() {
813            script.push_str(
814                "  // Clear cookie jar to prevent server Set-Cookie from duplicating custom Cookie header\n",
815            );
816            script.push_str("  http.cookieJar().clear(BASE_URL);\n\n");
817        }
818
819        // Group operations by category
820        let mut category_ops: std::collections::BTreeMap<
821            &'static str,
822            Vec<(&AnnotatedOperation, &ConformanceFeature)>,
823        > = std::collections::BTreeMap::new();
824
825        for op in &self.operations {
826            for feature in &op.features {
827                let category = feature.category();
828                if self.config.should_include_category(category) {
829                    category_ops.entry(category).or_default().push((op, feature));
830                }
831            }
832        }
833
834        // Emit grouped tests
835        let mut total_checks = 0usize;
836        for (category, ops) in &category_ops {
837            script.push_str(&format!("  group('{}', function () {{\n", category));
838
839            if self.config.all_operations {
840                // All-operations mode: test every operation, using path-qualified check names
841                let mut emitted_checks: HashSet<String> = HashSet::new();
842                for (op, feature) in ops {
843                    let qualified = format!("{}:{}", feature.check_name(), op.path);
844                    if emitted_checks.insert(qualified.clone()) {
845                        self.emit_check_named(&mut script, op, feature, &qualified);
846                        total_checks += 1;
847                    }
848                }
849            } else {
850                // Default: one representative operation per feature, with path-qualified
851                // check names so failures identify which endpoint was tested.
852                let mut emitted_features: HashSet<&str> = HashSet::new();
853                for (op, feature) in ops {
854                    if emitted_features.insert(feature.check_name()) {
855                        let qualified = format!("{}:{}", feature.check_name(), op.path);
856                        self.emit_check_named(&mut script, op, feature, &qualified);
857                        total_checks += 1;
858                    }
859                }
860            }
861
862            script.push_str("  });\n\n");
863        }
864
865        // Custom checks from YAML file
866        if let Some(custom_group) = self.config.generate_custom_group()? {
867            script.push_str(&custom_group);
868        }
869
870        script.push_str("}\n\n");
871
872        // handleSummary
873        self.generate_handle_summary(&mut script);
874
875        Ok((script, total_checks))
876    }
877
878    /// Emit a single k6 check for an operation + feature with a custom check name
879    fn emit_check_named(
880        &self,
881        script: &mut String,
882        op: &AnnotatedOperation,
883        feature: &ConformanceFeature,
884        check_name: &str,
885    ) {
886        // Escape single quotes in check name since it's embedded in JS single-quoted strings
887        let check_name = check_name.replace('\'', "\\'");
888        let check_name = check_name.as_str();
889
890        script.push_str("    {\n");
891
892        // Build the URL path with parameters substituted
893        let mut url_path = op.path.clone();
894        for (name, value) in &op.path_params {
895            url_path = url_path.replace(&format!("{{{}}}", name), value);
896        }
897
898        // Build query string
899        if !op.query_params.is_empty() {
900            let qs: Vec<String> =
901                op.query_params.iter().map(|(k, v)| format!("{}={}", k, v)).collect();
902            url_path = format!("{}?{}", url_path, qs.join("&"));
903        }
904
905        let full_url = format!("${{BASE_URL}}{}", url_path);
906
907        // Build effective headers: merge spec-derived headers with custom headers.
908        // Custom headers override spec-derived ones with the same name.
909        let mut effective_headers = self.effective_headers(&op.header_params);
910
911        // For non-default response code checks, add header to tell the mock server
912        // which status code to return (the server defaults to the first declared status)
913        if matches!(feature, ConformanceFeature::Response400 | ConformanceFeature::Response404) {
914            let expected_code = match feature {
915                ConformanceFeature::Response400 => "400",
916                ConformanceFeature::Response404 => "404",
917                _ => unreachable!(),
918            };
919            effective_headers
920                .push(("X-Mockforge-Response-Status".to_string(), expected_code.to_string()));
921        }
922
923        // For security checks AND for all requests on endpoints with security requirements,
924        // inject auth credentials so the server doesn't reject with 401.
925        // Only inject if the user hasn't already provided the header via --custom-headers.
926        let needs_auth = matches!(
927            feature,
928            ConformanceFeature::SecurityBearer
929                | ConformanceFeature::SecurityBasic
930                | ConformanceFeature::SecurityApiKey
931        ) || !op.security_schemes.is_empty();
932
933        if needs_auth {
934            self.inject_security_headers(&op.security_schemes, &mut effective_headers);
935        }
936
937        let has_headers = !effective_headers.is_empty();
938        let headers_obj = if has_headers {
939            Self::format_headers(&effective_headers)
940        } else {
941            String::new()
942        };
943
944        // Determine HTTP method and emit request
945        match op.method.as_str() {
946            "GET" => {
947                if has_headers {
948                    script.push_str(&format!(
949                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
950                        full_url, headers_obj
951                    ));
952                } else {
953                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
954                }
955            }
956            "POST" => {
957                self.emit_request_with_body(script, "post", &full_url, op, &effective_headers);
958            }
959            "PUT" => {
960                self.emit_request_with_body(script, "put", &full_url, op, &effective_headers);
961            }
962            "PATCH" => {
963                self.emit_request_with_body(script, "patch", &full_url, op, &effective_headers);
964            }
965            "DELETE" => {
966                if has_headers {
967                    script.push_str(&format!(
968                        "      let res = http.del(`{}`, null, {{ headers: {} }});\n",
969                        full_url, headers_obj
970                    ));
971                } else {
972                    script.push_str(&format!("      let res = http.del(`{}`);\n", full_url));
973                }
974            }
975            "HEAD" => {
976                if has_headers {
977                    script.push_str(&format!(
978                        "      let res = http.head(`{}`, {{ headers: {} }});\n",
979                        full_url, headers_obj
980                    ));
981                } else {
982                    script.push_str(&format!("      let res = http.head(`{}`);\n", full_url));
983                }
984            }
985            "OPTIONS" => {
986                if has_headers {
987                    script.push_str(&format!(
988                        "      let res = http.options(`{}`, null, {{ headers: {} }});\n",
989                        full_url, headers_obj
990                    ));
991                } else {
992                    script.push_str(&format!("      let res = http.options(`{}`);\n", full_url));
993                }
994            }
995            _ => {
996                if has_headers {
997                    script.push_str(&format!(
998                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
999                        full_url, headers_obj
1000                    ));
1001                } else {
1002                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
1003                }
1004            }
1005        }
1006
1007        // Capture request/response for --export-requests
1008        // (check_name is already escaped at line 829 above — don't double-escape)
1009        if self.config.export_requests {
1010            script.push_str(&format!(
1011                "      if (typeof __captureExchange === 'function') __captureExchange('{}', res);\n",
1012                check_name
1013            ));
1014        }
1015
1016        // Check: emit assertion based on feature type, with failure detail capture
1017        if matches!(
1018            feature,
1019            ConformanceFeature::Response200
1020                | ConformanceFeature::Response201
1021                | ConformanceFeature::Response204
1022                | ConformanceFeature::Response400
1023                | ConformanceFeature::Response404
1024        ) {
1025            let expected_code = match feature {
1026                ConformanceFeature::Response200 => 200,
1027                ConformanceFeature::Response201 => 201,
1028                ConformanceFeature::Response204 => 204,
1029                ConformanceFeature::Response400 => 400,
1030                ConformanceFeature::Response404 => 404,
1031                _ => 200,
1032            };
1033            script.push_str(&format!(
1034                "      {{ let ok = check(res, {{ '{}': (r) => r.status === {} }}); if (!ok) __captureFailure('{}', res, 'status === {}'); }}\n",
1035                check_name, expected_code, check_name, expected_code
1036            ));
1037        } else if matches!(feature, ConformanceFeature::ResponseValidation) {
1038            // Response schema validation — validate the response body against the schema
1039            // Uses inline field-level error collection so failure details include
1040            // which specific fields violated the schema (field_path, violation_type,
1041            // expected, actual) — matching the Ajv `errors` array structure.
1042            if let Some(schema) = &op.response_schema {
1043                let validation_js = SchemaValidatorGenerator::generate_validation(schema);
1044                let schema_json = serde_json::to_string(schema).unwrap_or_default();
1045                // Escape backticks and backslashes for JS template literal safety
1046                let schema_json_escaped = schema_json.replace('\\', "\\\\").replace('`', "\\`");
1047                script.push_str(&format!(
1048                    concat!(
1049                        "      try {{\n",
1050                        "        let body = res.json();\n",
1051                        "        let ok = check(res, {{ '{check}': (r) => ( {validation} ) }});\n",
1052                        "        if (!ok) {{\n",
1053                        "          let __violations = [];\n",
1054                        "          try {{\n",
1055                        "            let __schema = JSON.parse(`{schema}`);\n",
1056                        "            function __collectErrors(schema, data, path) {{\n",
1057                        "              if (!schema || typeof schema !== 'object') return;\n",
1058                        "              let st = schema.type || (schema.schema_kind && schema.schema_kind.Type && Object.keys(schema.schema_kind.Type)[0]);\n",
1059                        "              if (st) {{ st = st.toLowerCase(); }}\n",
1060                        "              if (st === 'object') {{\n",
1061                        "                if (typeof data !== 'object' || data === null) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'object', actual: typeof data }}); return; }}\n",
1062                        "                let props = schema.properties || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Object && schema.schema_kind.Type.Object.properties) || {{}};\n",
1063                        "                let req = schema.required || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Object && schema.schema_kind.Type.Object.required) || [];\n",
1064                        "                for (let f of req) {{ if (!(f in data)) {{ __violations.push({{ field_path: path + '/' + f, violation_type: 'required', expected: 'present', actual: 'missing' }}); }} }}\n",
1065                        "                for (let [k, v] of Object.entries(props)) {{ if (data[k] !== undefined) {{ let ps = v.Item || v; __collectErrors(ps, data[k], path + '/' + k); }} }}\n",
1066                        "              }} else if (st === 'array') {{\n",
1067                        "                if (!Array.isArray(data)) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'array', actual: typeof data }}); return; }}\n",
1068                        "                let items = schema.items || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Array && schema.schema_kind.Type.Array.items);\n",
1069                        "                if (items) {{ let is = items.Item || items; for (let i = 0; i < Math.min(data.length, 5); i++) {{ __collectErrors(is, data[i], path + '/' + i); }} }}\n",
1070                        "              }} else if (st === 'string') {{\n",
1071                        "                if (typeof data !== 'string') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'string', actual: typeof data }}); }}\n",
1072                        "              }} else if (st === 'integer') {{\n",
1073                        "                if (typeof data !== 'number' || !Number.isInteger(data)) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'integer', actual: typeof data }}); }}\n",
1074                        "              }} else if (st === 'number') {{\n",
1075                        "                if (typeof data !== 'number') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'number', actual: typeof data }}); }}\n",
1076                        "              }} else if (st === 'boolean') {{\n",
1077                        "                if (typeof data !== 'boolean') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'boolean', actual: typeof data }}); }}\n",
1078                        "              }}\n",
1079                        "            }}\n",
1080                        "            __collectErrors(__schema, body, '');\n",
1081                        "          }} catch(_e) {{}}\n",
1082                        "          __captureFailure('{check}', res, 'schema validation', __violations);\n",
1083                        "        }}\n",
1084                        "      }} catch(e) {{ check(res, {{ '{check}': () => false }}); __captureFailure('{check}', res, 'JSON parse failed: ' + e.message); }}\n",
1085                    ),
1086                    check = check_name,
1087                    validation = validation_js,
1088                    schema = schema_json_escaped,
1089                ));
1090            }
1091        } else if matches!(
1092            feature,
1093            ConformanceFeature::SecurityBearer
1094                | ConformanceFeature::SecurityBasic
1095                | ConformanceFeature::SecurityApiKey
1096        ) {
1097            // Security checks verify the server accepts the auth credentials (not 401/403)
1098            script.push_str(&format!(
1099                "      {{ let ok = check(res, {{ '{}': (r) => r.status >= 200 && r.status < 400 }}); if (!ok) __captureFailure('{}', res, 'status >= 200 && status < 400 (auth accepted)'); }}\n",
1100                check_name, check_name
1101            ));
1102        } else {
1103            script.push_str(&format!(
1104                "      {{ let ok = check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }}); if (!ok) __captureFailure('{}', res, 'status >= 200 && status < 500'); }}\n",
1105                check_name, check_name
1106            ));
1107        }
1108
1109        // Clear cookie jar after each request to prevent Set-Cookie leaking
1110        let has_cookie = self.config.has_cookie_header()
1111            || effective_headers.iter().any(|(h, _)| h.eq_ignore_ascii_case("Cookie"));
1112        if has_cookie {
1113            script.push_str("      http.cookieJar().clear(BASE_URL);\n");
1114        }
1115
1116        script.push_str("    }\n");
1117
1118        // Rate-limit delay between checks (to avoid 429 from target API)
1119        if self.config.request_delay_ms > 0 {
1120            script.push_str(&format!(
1121                "    sleep({:.3});\n",
1122                self.config.request_delay_ms as f64 / 1000.0
1123            ));
1124        }
1125    }
1126
1127    /// Emit an HTTP request with a body
1128    fn emit_request_with_body(
1129        &self,
1130        script: &mut String,
1131        method: &str,
1132        url: &str,
1133        op: &AnnotatedOperation,
1134        effective_headers: &[(String, String)],
1135    ) {
1136        if let Some(body) = &op.sample_body {
1137            let escaped_body = body.replace('\'', "\\'");
1138            let headers = if !effective_headers.is_empty() {
1139                format!(
1140                    "Object.assign({{}}, JSON_HEADERS, {})",
1141                    Self::format_headers(effective_headers)
1142                )
1143            } else {
1144                "JSON_HEADERS".to_string()
1145            };
1146            script.push_str(&format!(
1147                "      let res = http.{}(`{}`, '{}', {{ headers: {} }});\n",
1148                method, url, escaped_body, headers
1149            ));
1150        } else if !effective_headers.is_empty() {
1151            script.push_str(&format!(
1152                "      let res = http.{}(`{}`, null, {{ headers: {} }});\n",
1153                method,
1154                url,
1155                Self::format_headers(effective_headers)
1156            ));
1157        } else {
1158            script.push_str(&format!("      let res = http.{}(`{}`, null);\n", method, url));
1159        }
1160    }
1161
1162    /// Build effective headers by merging spec-derived headers with custom headers.
1163    /// Custom headers override spec-derived ones with the same name (case-insensitive).
1164    /// Custom headers not in the spec are appended.
1165    fn effective_headers(&self, spec_headers: &[(String, String)]) -> Vec<(String, String)> {
1166        let custom = &self.config.custom_headers;
1167        if custom.is_empty() {
1168            return spec_headers.to_vec();
1169        }
1170
1171        let mut result: Vec<(String, String)> = Vec::new();
1172
1173        // Start with spec headers, replacing values if a custom header matches
1174        for (name, value) in spec_headers {
1175            if let Some((_, custom_val)) =
1176                custom.iter().find(|(cn, _)| cn.eq_ignore_ascii_case(name))
1177            {
1178                result.push((name.clone(), custom_val.clone()));
1179            } else {
1180                result.push((name.clone(), value.clone()));
1181            }
1182        }
1183
1184        // Append custom headers that aren't already in spec headers
1185        for (name, value) in custom {
1186            if !spec_headers.iter().any(|(sn, _)| sn.eq_ignore_ascii_case(name)) {
1187                result.push((name.clone(), value.clone()));
1188            }
1189        }
1190
1191        result
1192    }
1193
1194    /// Inject security headers based on resolved security scheme details.
1195    /// Respects user-provided custom headers (won't overwrite if already set).
1196    fn inject_security_headers(
1197        &self,
1198        schemes: &[SecuritySchemeInfo],
1199        headers: &mut Vec<(String, String)>,
1200    ) {
1201        let mut to_add: Vec<(String, String)> = Vec::new();
1202
1203        let has_header = |name: &str, headers: &[(String, String)]| {
1204            headers.iter().any(|(h, _)| h.eq_ignore_ascii_case(name))
1205                || self.config.custom_headers.iter().any(|(h, _)| h.eq_ignore_ascii_case(name))
1206        };
1207
1208        // If user provides Cookie header, they're using session-based auth — skip auto auth
1209        let has_cookie_auth = has_header("Cookie", headers);
1210
1211        for scheme in schemes {
1212            match scheme {
1213                SecuritySchemeInfo::Bearer => {
1214                    if !has_cookie_auth && !has_header("Authorization", headers) {
1215                        // MockForge mock server accepts any Bearer token
1216                        to_add.push((
1217                            "Authorization".to_string(),
1218                            "Bearer mockforge-conformance-test-token".to_string(),
1219                        ));
1220                    }
1221                }
1222                SecuritySchemeInfo::Basic => {
1223                    if !has_cookie_auth && !has_header("Authorization", headers) {
1224                        let creds = self.config.basic_auth.as_deref().unwrap_or("test:test");
1225                        use base64::Engine;
1226                        let encoded =
1227                            base64::engine::general_purpose::STANDARD.encode(creds.as_bytes());
1228                        to_add.push(("Authorization".to_string(), format!("Basic {}", encoded)));
1229                    }
1230                }
1231                SecuritySchemeInfo::ApiKey { location, name } => match location {
1232                    ApiKeyLocation::Header => {
1233                        if !has_header(name, headers) {
1234                            let key = self
1235                                .config
1236                                .api_key
1237                                .as_deref()
1238                                .unwrap_or("mockforge-conformance-test-key");
1239                            to_add.push((name.clone(), key.to_string()));
1240                        }
1241                    }
1242                    ApiKeyLocation::Cookie => {
1243                        if !has_header("Cookie", headers) {
1244                            to_add.push((
1245                                "Cookie".to_string(),
1246                                format!("{}=mockforge-conformance-test-session", name),
1247                            ));
1248                        }
1249                    }
1250                    ApiKeyLocation::Query => {
1251                        // Query params are handled via URL, not headers — skip here
1252                    }
1253                },
1254            }
1255        }
1256
1257        headers.extend(to_add);
1258    }
1259
1260    /// Format header params as a JS object literal
1261    fn format_headers(headers: &[(String, String)]) -> String {
1262        let entries: Vec<String> = headers
1263            .iter()
1264            .map(|(k, v)| format!("'{}': '{}'", k, v.replace('\'', "\\'")))
1265            .collect();
1266        format!("{{ {} }}", entries.join(", "))
1267    }
1268
1269    /// handleSummary — same format as reference mode for report compatibility
1270    fn generate_handle_summary(&self, script: &mut String) {
1271        // Use absolute path for report output so k6 writes where the CLI expects
1272        let report_path = match &self.config.output_dir {
1273            Some(dir) => {
1274                let abs = std::fs::canonicalize(dir)
1275                    .unwrap_or_else(|_| dir.clone())
1276                    .join("conformance-report.json");
1277                abs.to_string_lossy().to_string()
1278            }
1279            None => "conformance-report.json".to_string(),
1280        };
1281
1282        script.push_str("export function handleSummary(data) {\n");
1283        script.push_str("  let checks = {};\n");
1284        script.push_str("  if (data.metrics && data.metrics.checks) {\n");
1285        script.push_str("    checks.overall_pass_rate = data.metrics.checks.values.rate;\n");
1286        script.push_str("  }\n");
1287        script.push_str("  let checkResults = {};\n");
1288        script.push_str("  function walkGroups(group) {\n");
1289        script.push_str("    if (group.checks) {\n");
1290        script.push_str("      for (let checkObj of group.checks) {\n");
1291        script.push_str("        checkResults[checkObj.name] = {\n");
1292        script.push_str("          passes: checkObj.passes,\n");
1293        script.push_str("          fails: checkObj.fails,\n");
1294        script.push_str("        };\n");
1295        script.push_str("      }\n");
1296        script.push_str("    }\n");
1297        script.push_str("    if (group.groups) {\n");
1298        script.push_str("      for (let subGroup of group.groups) {\n");
1299        script.push_str("        walkGroups(subGroup);\n");
1300        script.push_str("      }\n");
1301        script.push_str("    }\n");
1302        script.push_str("  }\n");
1303        script.push_str("  if (data.root_group) {\n");
1304        script.push_str("    walkGroups(data.root_group);\n");
1305        script.push_str("  }\n");
1306        script.push_str("  return {\n");
1307        script.push_str(&format!(
1308            "    '{}': JSON.stringify({{ checks: checkResults, overall: checks }}, null, 2),\n",
1309            report_path
1310        ));
1311        script.push_str("    'summary.json': JSON.stringify(data),\n");
1312        script.push_str("    stdout: textSummary(data, { indent: '  ', enableColors: true }),\n");
1313        script.push_str("  };\n");
1314        script.push_str("}\n\n");
1315        script.push_str("function textSummary(data, opts) {\n");
1316        script.push_str("  return JSON.stringify(data, null, 2);\n");
1317        script.push_str("}\n");
1318    }
1319}
1320
1321#[cfg(test)]
1322mod tests {
1323    use super::*;
1324    use openapiv3::{
1325        Operation, ParameterData, ParameterSchemaOrContent, PathStyle, Response, Schema,
1326        SchemaData, SchemaKind, StringType, Type,
1327    };
1328
1329    fn make_op(method: &str, path: &str, operation: Operation) -> ApiOperation {
1330        ApiOperation {
1331            method: method.to_string(),
1332            path: path.to_string(),
1333            operation,
1334            operation_id: None,
1335        }
1336    }
1337
1338    fn empty_spec() -> OpenAPI {
1339        OpenAPI::default()
1340    }
1341
1342    #[test]
1343    fn test_annotate_get_with_path_param() {
1344        let mut op = Operation::default();
1345        op.parameters.push(ReferenceOr::Item(Parameter::Path {
1346            parameter_data: ParameterData {
1347                name: "id".to_string(),
1348                description: None,
1349                required: true,
1350                deprecated: None,
1351                format: ParameterSchemaOrContent::Schema(ReferenceOr::Item(Schema {
1352                    schema_data: SchemaData::default(),
1353                    schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1354                })),
1355                example: None,
1356                examples: Default::default(),
1357                explode: None,
1358                extensions: Default::default(),
1359            },
1360            style: PathStyle::Simple,
1361        }));
1362
1363        let api_op = make_op("get", "/users/{id}", op);
1364        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1365
1366        assert!(annotated.features.contains(&ConformanceFeature::MethodGet));
1367        assert!(annotated.features.contains(&ConformanceFeature::PathParamString));
1368        assert!(annotated.features.contains(&ConformanceFeature::ConstraintRequired));
1369        assert_eq!(annotated.path_params.len(), 1);
1370        assert_eq!(annotated.path_params[0].0, "id");
1371    }
1372
1373    #[test]
1374    fn test_annotate_post_with_json_body() {
1375        let mut op = Operation::default();
1376        let mut body = RequestBody {
1377            required: true,
1378            ..Default::default()
1379        };
1380        body.content
1381            .insert("application/json".to_string(), openapiv3::MediaType::default());
1382        op.request_body = Some(ReferenceOr::Item(body));
1383
1384        let api_op = make_op("post", "/items", op);
1385        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1386
1387        assert!(annotated.features.contains(&ConformanceFeature::MethodPost));
1388        assert!(annotated.features.contains(&ConformanceFeature::BodyJson));
1389    }
1390
1391    #[test]
1392    fn test_annotate_response_codes() {
1393        let mut op = Operation::default();
1394        op.responses
1395            .responses
1396            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(Response::default()));
1397        op.responses
1398            .responses
1399            .insert(openapiv3::StatusCode::Code(404), ReferenceOr::Item(Response::default()));
1400
1401        let api_op = make_op("get", "/items", op);
1402        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1403
1404        assert!(annotated.features.contains(&ConformanceFeature::Response200));
1405        assert!(annotated.features.contains(&ConformanceFeature::Response404));
1406    }
1407
1408    #[test]
1409    fn test_generate_spec_driven_script() {
1410        let config = ConformanceConfig {
1411            target_url: "http://localhost:3000".to_string(),
1412            api_key: None,
1413            basic_auth: None,
1414            skip_tls_verify: false,
1415            categories: None,
1416            base_path: None,
1417            custom_headers: vec![],
1418            output_dir: None,
1419            all_operations: false,
1420            custom_checks_file: None,
1421            request_delay_ms: 0,
1422            custom_filter: None,
1423            export_requests: false,
1424            validate_requests: false,
1425        };
1426
1427        let operations = vec![AnnotatedOperation {
1428            path: "/users/{id}".to_string(),
1429            method: "GET".to_string(),
1430            features: vec![
1431                ConformanceFeature::MethodGet,
1432                ConformanceFeature::PathParamString,
1433            ],
1434            request_body_content_type: None,
1435            sample_body: None,
1436            query_params: vec![],
1437            header_params: vec![],
1438            path_params: vec![("id".to_string(), "test-value".to_string())],
1439            response_schema: None,
1440            response_schemas: std::collections::BTreeMap::new(),
1441            request_body_schema: None,
1442            security_schemes: vec![],
1443        }];
1444
1445        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1446        let (script, _check_count) = gen.generate().unwrap();
1447
1448        assert!(script.contains("import http from 'k6/http'"));
1449        assert!(script.contains("/users/test-value"));
1450        assert!(script.contains("param:path:string"));
1451        assert!(script.contains("method:GET"));
1452        assert!(script.contains("handleSummary"));
1453    }
1454
1455    #[test]
1456    fn test_generate_with_category_filter() {
1457        let config = ConformanceConfig {
1458            target_url: "http://localhost:3000".to_string(),
1459            api_key: None,
1460            basic_auth: None,
1461            skip_tls_verify: false,
1462            categories: Some(vec!["Parameters".to_string()]),
1463            base_path: None,
1464            custom_headers: vec![],
1465            output_dir: None,
1466            all_operations: false,
1467            custom_checks_file: None,
1468            request_delay_ms: 0,
1469            custom_filter: None,
1470            export_requests: false,
1471            validate_requests: false,
1472        };
1473
1474        let operations = vec![AnnotatedOperation {
1475            path: "/users/{id}".to_string(),
1476            method: "GET".to_string(),
1477            features: vec![
1478                ConformanceFeature::MethodGet,
1479                ConformanceFeature::PathParamString,
1480            ],
1481            request_body_content_type: None,
1482            sample_body: None,
1483            query_params: vec![],
1484            header_params: vec![],
1485            path_params: vec![("id".to_string(), "1".to_string())],
1486            response_schema: None,
1487            response_schemas: std::collections::BTreeMap::new(),
1488            request_body_schema: None,
1489            security_schemes: vec![],
1490        }];
1491
1492        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1493        let (script, _check_count) = gen.generate().unwrap();
1494
1495        assert!(script.contains("group('Parameters'"));
1496        assert!(!script.contains("group('HTTP Methods'"));
1497    }
1498
1499    #[test]
1500    fn test_annotate_response_validation() {
1501        use openapiv3::ObjectType;
1502
1503        // Operation with a 200 response that has a JSON schema
1504        let mut op = Operation::default();
1505        let mut response = Response::default();
1506        let mut media = openapiv3::MediaType::default();
1507        let mut obj_type = ObjectType::default();
1508        obj_type.properties.insert(
1509            "name".to_string(),
1510            ReferenceOr::Item(Box::new(Schema {
1511                schema_data: SchemaData::default(),
1512                schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1513            })),
1514        );
1515        obj_type.required = vec!["name".to_string()];
1516        media.schema = Some(ReferenceOr::Item(Schema {
1517            schema_data: SchemaData::default(),
1518            schema_kind: SchemaKind::Type(Type::Object(obj_type)),
1519        }));
1520        response.content.insert("application/json".to_string(), media);
1521        op.responses
1522            .responses
1523            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1524
1525        let api_op = make_op("get", "/users", op);
1526        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1527
1528        assert!(
1529            annotated.features.contains(&ConformanceFeature::ResponseValidation),
1530            "Should detect ResponseValidation when response has a JSON schema"
1531        );
1532        assert!(annotated.response_schema.is_some(), "Should extract the response schema");
1533
1534        // Verify generated script includes schema validation with try-catch
1535        let config = ConformanceConfig {
1536            target_url: "http://localhost:3000".to_string(),
1537            api_key: None,
1538            basic_auth: None,
1539            skip_tls_verify: false,
1540            categories: None,
1541            base_path: None,
1542            custom_headers: vec![],
1543            output_dir: None,
1544            all_operations: false,
1545            custom_checks_file: None,
1546            request_delay_ms: 0,
1547            custom_filter: None,
1548            export_requests: false,
1549            validate_requests: false,
1550        };
1551        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1552        let (script, _check_count) = gen.generate().unwrap();
1553
1554        assert!(
1555            script.contains("response:schema:validation"),
1556            "Script should contain the validation check name"
1557        );
1558        assert!(script.contains("try {"), "Script should wrap validation in try-catch");
1559        assert!(script.contains("res.json()"), "Script should parse response as JSON");
1560    }
1561
1562    #[test]
1563    fn test_annotate_global_security() {
1564        // Spec with global security requirement, operation without its own security
1565        let op = Operation::default();
1566        let mut spec = OpenAPI::default();
1567        let mut global_req = openapiv3::SecurityRequirement::new();
1568        global_req.insert("bearerAuth".to_string(), vec![]);
1569        spec.security = Some(vec![global_req]);
1570        // Define the security scheme in components
1571        let mut components = openapiv3::Components::default();
1572        components.security_schemes.insert(
1573            "bearerAuth".to_string(),
1574            ReferenceOr::Item(SecurityScheme::HTTP {
1575                scheme: "bearer".to_string(),
1576                bearer_format: Some("JWT".to_string()),
1577                description: None,
1578                extensions: Default::default(),
1579            }),
1580        );
1581        spec.components = Some(components);
1582
1583        let api_op = make_op("get", "/protected", op);
1584        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1585
1586        assert!(
1587            annotated.features.contains(&ConformanceFeature::SecurityBearer),
1588            "Should detect SecurityBearer from global security + components"
1589        );
1590    }
1591
1592    #[test]
1593    fn test_annotate_security_scheme_resolution() {
1594        // Test that security scheme type is resolved from components, not just name heuristic
1595        let mut op = Operation::default();
1596        // Use a generic name that wouldn't match name heuristics
1597        let mut req = openapiv3::SecurityRequirement::new();
1598        req.insert("myAuth".to_string(), vec![]);
1599        op.security = Some(vec![req]);
1600
1601        let mut spec = OpenAPI::default();
1602        let mut components = openapiv3::Components::default();
1603        components.security_schemes.insert(
1604            "myAuth".to_string(),
1605            ReferenceOr::Item(SecurityScheme::APIKey {
1606                location: openapiv3::APIKeyLocation::Header,
1607                name: "X-API-Key".to_string(),
1608                description: None,
1609                extensions: Default::default(),
1610            }),
1611        );
1612        spec.components = Some(components);
1613
1614        let api_op = make_op("get", "/data", op);
1615        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1616
1617        assert!(
1618            annotated.features.contains(&ConformanceFeature::SecurityApiKey),
1619            "Should detect SecurityApiKey from SecurityScheme::APIKey, not name heuristic"
1620        );
1621    }
1622
1623    #[test]
1624    fn test_annotate_content_negotiation() {
1625        let mut op = Operation::default();
1626        let mut response = Response::default();
1627        // Response with multiple content types
1628        response
1629            .content
1630            .insert("application/json".to_string(), openapiv3::MediaType::default());
1631        response
1632            .content
1633            .insert("application/xml".to_string(), openapiv3::MediaType::default());
1634        op.responses
1635            .responses
1636            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1637
1638        let api_op = make_op("get", "/items", op);
1639        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1640
1641        assert!(
1642            annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1643            "Should detect ContentNegotiation when response has multiple content types"
1644        );
1645    }
1646
1647    #[test]
1648    fn test_no_content_negotiation_for_single_type() {
1649        let mut op = Operation::default();
1650        let mut response = Response::default();
1651        response
1652            .content
1653            .insert("application/json".to_string(), openapiv3::MediaType::default());
1654        op.responses
1655            .responses
1656            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1657
1658        let api_op = make_op("get", "/items", op);
1659        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1660
1661        assert!(
1662            !annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1663            "Should NOT detect ContentNegotiation for a single content type"
1664        );
1665    }
1666
1667    #[test]
1668    fn test_spec_driven_with_base_path() {
1669        let annotated = AnnotatedOperation {
1670            path: "/users".to_string(),
1671            method: "GET".to_string(),
1672            features: vec![ConformanceFeature::MethodGet],
1673            path_params: vec![],
1674            query_params: vec![],
1675            header_params: vec![],
1676            request_body_content_type: None,
1677            sample_body: None,
1678            response_schema: None,
1679            response_schemas: std::collections::BTreeMap::new(),
1680            request_body_schema: None,
1681            security_schemes: vec![],
1682        };
1683        let config = ConformanceConfig {
1684            target_url: "https://192.168.2.86/".to_string(),
1685            api_key: None,
1686            basic_auth: None,
1687            skip_tls_verify: true,
1688            categories: None,
1689            base_path: Some("/api".to_string()),
1690            custom_headers: vec![],
1691            output_dir: None,
1692            all_operations: false,
1693            custom_checks_file: None,
1694            request_delay_ms: 0,
1695            custom_filter: None,
1696            export_requests: false,
1697            validate_requests: false,
1698        };
1699        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1700        let (script, _check_count) = gen.generate().unwrap();
1701
1702        assert!(
1703            script.contains("const BASE_URL = 'https://192.168.2.86/api'"),
1704            "BASE_URL should include the base_path. Got: {}",
1705            script.lines().find(|l| l.contains("BASE_URL")).unwrap_or("not found")
1706        );
1707    }
1708
1709    #[test]
1710    fn test_spec_driven_with_custom_headers() {
1711        let annotated = AnnotatedOperation {
1712            path: "/users".to_string(),
1713            method: "GET".to_string(),
1714            features: vec![ConformanceFeature::MethodGet],
1715            path_params: vec![],
1716            query_params: vec![],
1717            header_params: vec![
1718                ("X-Avi-Tenant".to_string(), "test-value".to_string()),
1719                ("X-CSRFToken".to_string(), "test-value".to_string()),
1720            ],
1721            request_body_content_type: None,
1722            sample_body: None,
1723            response_schema: None,
1724            response_schemas: std::collections::BTreeMap::new(),
1725            request_body_schema: None,
1726            security_schemes: vec![],
1727        };
1728        let config = ConformanceConfig {
1729            target_url: "https://192.168.2.86/".to_string(),
1730            api_key: None,
1731            basic_auth: None,
1732            skip_tls_verify: true,
1733            categories: None,
1734            base_path: Some("/api".to_string()),
1735            custom_headers: vec![
1736                ("X-Avi-Tenant".to_string(), "admin".to_string()),
1737                ("X-CSRFToken".to_string(), "real-csrf-token".to_string()),
1738                ("Cookie".to_string(), "sessionid=abc123".to_string()),
1739            ],
1740            output_dir: None,
1741            all_operations: false,
1742            custom_checks_file: None,
1743            request_delay_ms: 0,
1744            custom_filter: None,
1745            export_requests: false,
1746            validate_requests: false,
1747        };
1748        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1749        let (script, _check_count) = gen.generate().unwrap();
1750
1751        // Custom headers should override spec-derived test-value placeholders
1752        assert!(
1753            script.contains("'X-Avi-Tenant': 'admin'"),
1754            "Should use custom value for X-Avi-Tenant, not test-value"
1755        );
1756        assert!(
1757            script.contains("'X-CSRFToken': 'real-csrf-token'"),
1758            "Should use custom value for X-CSRFToken, not test-value"
1759        );
1760        // Custom headers not in spec should be appended
1761        assert!(
1762            script.contains("'Cookie': 'sessionid=abc123'"),
1763            "Should include Cookie header from custom_headers"
1764        );
1765        // test-value should NOT appear
1766        assert!(
1767            !script.contains("'test-value'"),
1768            "test-value placeholders should be replaced by custom values"
1769        );
1770    }
1771
1772    #[test]
1773    fn test_effective_headers_merging() {
1774        let config = ConformanceConfig {
1775            target_url: "http://localhost".to_string(),
1776            api_key: None,
1777            basic_auth: None,
1778            skip_tls_verify: false,
1779            categories: None,
1780            base_path: None,
1781            custom_headers: vec![
1782                ("X-Auth".to_string(), "real-token".to_string()),
1783                ("Cookie".to_string(), "session=abc".to_string()),
1784            ],
1785            output_dir: None,
1786            all_operations: false,
1787            custom_checks_file: None,
1788            request_delay_ms: 0,
1789            custom_filter: None,
1790            export_requests: false,
1791            validate_requests: false,
1792        };
1793        let gen = SpecDrivenConformanceGenerator::new(config, vec![]);
1794
1795        // Spec headers with a matching custom header
1796        let spec_headers = vec![
1797            ("X-Auth".to_string(), "test-value".to_string()),
1798            ("X-Other".to_string(), "keep-this".to_string()),
1799        ];
1800        let effective = gen.effective_headers(&spec_headers);
1801
1802        // X-Auth should be overridden
1803        assert_eq!(effective[0], ("X-Auth".to_string(), "real-token".to_string()));
1804        // X-Other should be kept as-is
1805        assert_eq!(effective[1], ("X-Other".to_string(), "keep-this".to_string()));
1806        // Cookie should be appended
1807        assert_eq!(effective[2], ("Cookie".to_string(), "session=abc".to_string()));
1808    }
1809}