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        // Round 51 (#79) — generate a spec-VALID sample so the baseline probe
379        // doesn't trip spurious query/path violations on every probe (Srikanth
380        // on 0.3.196: a `request-body:content-type-mismatch:multipart` probe
381        // reported `query_value_mismatch` on `alt`/`prettyPrint` because every
382        // param was filled with the invalid literal "test-value"). Enum ->
383        // first member; boolean -> "true"; integer/number -> a valid number;
384        // array -> "a,b"; string formats -> a plausible valid value. Negative
385        // probes still override the specific param they attack.
386        let sample = Self::param_sample_value(data, spec);
387
388        match location {
389            "path" => {
390                if is_integer {
391                    features.push(ConformanceFeature::PathParamInteger);
392                } else {
393                    features.push(ConformanceFeature::PathParamString);
394                }
395                path_params.push((data.name.clone(), sample));
396            }
397            "query" => {
398                if is_array {
399                    features.push(ConformanceFeature::QueryParamArray);
400                } else if is_integer {
401                    features.push(ConformanceFeature::QueryParamInteger);
402                } else {
403                    features.push(ConformanceFeature::QueryParamString);
404                }
405                query_params.push((data.name.clone(), sample));
406            }
407            "header" => {
408                features.push(ConformanceFeature::HeaderParam);
409                header_params.push((data.name.clone(), sample));
410            }
411            _ => {}
412        }
413
414        // Check for constraint features on the parameter (resolves $ref)
415        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
416            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
417                Self::annotate_schema(schema, spec, features);
418            }
419        }
420
421        // Required/optional
422        if data.required {
423            features.push(ConformanceFeature::ConstraintRequired);
424        } else {
425            features.push(ConformanceFeature::ConstraintOptional);
426        }
427    }
428
429    fn param_schema_is_integer(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
430        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
431            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
432                return matches!(&schema.schema_kind, SchemaKind::Type(Type::Integer(_)));
433            }
434        }
435        false
436    }
437
438    fn param_schema_is_array(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
439        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
440            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
441                return matches!(&schema.schema_kind, SchemaKind::Type(Type::Array(_)));
442            }
443        }
444        false
445    }
446
447    /// Round 51 (#79) — a spec-VALID sample value for a parameter, used as the
448    /// baseline (positive) filler so a probe that isn't attacking a given
449    /// param doesn't trip a spurious violation on it. Falls back to the generic
450    /// "test-value" only when the schema doesn't constrain the value.
451    fn param_sample_value(data: &openapiv3::ParameterData, spec: &OpenAPI) -> String {
452        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
453            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
454                return Self::schema_sample_value(schema);
455            }
456        }
457        "test-value".to_string()
458    }
459
460    /// Produce a value that satisfies `schema`'s type / enum / format so the
461    /// server-side (and client-side) validator accepts it.
462    fn schema_sample_value(schema: &Schema) -> String {
463        match &schema.schema_kind {
464            SchemaKind::Type(Type::String(s)) => {
465                if let Some(first) = s.enumeration.iter().flatten().next() {
466                    return first.clone();
467                }
468                match &s.format {
469                    VariantOrUnknownOrEmpty::Item(StringFormat::Date) => "2024-01-01".to_string(),
470                    VariantOrUnknownOrEmpty::Item(StringFormat::DateTime) => {
471                        "2024-01-01T00:00:00Z".to_string()
472                    }
473                    VariantOrUnknownOrEmpty::Unknown(fmt) => match fmt.as_str() {
474                        "email" => "user@example.com".to_string(),
475                        "uuid" => "00000000-0000-0000-0000-000000000000".to_string(),
476                        "uri" | "url" => "https://example.com".to_string(),
477                        "ipv4" => "192.0.2.1".to_string(),
478                        "ipv6" => "2001:db8::1".to_string(),
479                        "hostname" => "example.com".to_string(),
480                        "byte" => "dGVzdA==".to_string(),
481                        _ => "test-value".to_string(),
482                    },
483                    _ => "test-value".to_string(),
484                }
485            }
486            SchemaKind::Type(Type::Integer(i)) => i
487                .enumeration
488                .iter()
489                .flatten()
490                .next()
491                .map(|v| v.to_string())
492                .unwrap_or_else(|| "42".to_string()),
493            SchemaKind::Type(Type::Number(n)) => n
494                .enumeration
495                .iter()
496                .flatten()
497                .next()
498                .map(|v| v.to_string())
499                .unwrap_or_else(|| "42".to_string()),
500            SchemaKind::Type(Type::Boolean(_)) => "true".to_string(),
501            SchemaKind::Type(Type::Array(_)) => "a,b".to_string(),
502            _ => "test-value".to_string(),
503        }
504    }
505
506    /// Annotate schema-level features (types, composition, formats, constraints)
507    fn annotate_schema(schema: &Schema, spec: &OpenAPI, features: &mut Vec<ConformanceFeature>) {
508        match &schema.schema_kind {
509            SchemaKind::Type(Type::String(s)) => {
510                features.push(ConformanceFeature::SchemaString);
511                // Check format
512                match &s.format {
513                    VariantOrUnknownOrEmpty::Item(StringFormat::Date) => {
514                        features.push(ConformanceFeature::FormatDate);
515                    }
516                    VariantOrUnknownOrEmpty::Item(StringFormat::DateTime) => {
517                        features.push(ConformanceFeature::FormatDateTime);
518                    }
519                    VariantOrUnknownOrEmpty::Unknown(fmt) => match fmt.as_str() {
520                        "email" => features.push(ConformanceFeature::FormatEmail),
521                        "uuid" => features.push(ConformanceFeature::FormatUuid),
522                        "uri" | "url" => features.push(ConformanceFeature::FormatUri),
523                        "ipv4" => features.push(ConformanceFeature::FormatIpv4),
524                        "ipv6" => features.push(ConformanceFeature::FormatIpv6),
525                        _ => {}
526                    },
527                    _ => {}
528                }
529                // Check constraints
530                if s.pattern.is_some() {
531                    features.push(ConformanceFeature::ConstraintPattern);
532                }
533                if !s.enumeration.is_empty() {
534                    features.push(ConformanceFeature::ConstraintEnum);
535                }
536                if s.min_length.is_some() || s.max_length.is_some() {
537                    features.push(ConformanceFeature::ConstraintMinMax);
538                }
539            }
540            SchemaKind::Type(Type::Integer(i)) => {
541                features.push(ConformanceFeature::SchemaInteger);
542                if i.minimum.is_some() || i.maximum.is_some() {
543                    features.push(ConformanceFeature::ConstraintMinMax);
544                }
545                if !i.enumeration.is_empty() {
546                    features.push(ConformanceFeature::ConstraintEnum);
547                }
548            }
549            SchemaKind::Type(Type::Number(n)) => {
550                features.push(ConformanceFeature::SchemaNumber);
551                if n.minimum.is_some() || n.maximum.is_some() {
552                    features.push(ConformanceFeature::ConstraintMinMax);
553                }
554            }
555            SchemaKind::Type(Type::Boolean(_)) => {
556                features.push(ConformanceFeature::SchemaBoolean);
557            }
558            SchemaKind::Type(Type::Array(arr)) => {
559                features.push(ConformanceFeature::SchemaArray);
560                if let Some(item_ref) = &arr.items {
561                    if let Some(item_schema) = ref_resolver::resolve_boxed_schema(item_ref, spec) {
562                        Self::annotate_schema(item_schema, spec, features);
563                    }
564                }
565            }
566            SchemaKind::Type(Type::Object(obj)) => {
567                features.push(ConformanceFeature::SchemaObject);
568                // Check required fields
569                if !obj.required.is_empty() {
570                    features.push(ConformanceFeature::ConstraintRequired);
571                }
572                // Walk properties (resolves $ref)
573                for (_name, prop_ref) in &obj.properties {
574                    if let Some(prop_schema) = ref_resolver::resolve_boxed_schema(prop_ref, spec) {
575                        Self::annotate_schema(prop_schema, spec, features);
576                    }
577                }
578            }
579            SchemaKind::OneOf { .. } => {
580                features.push(ConformanceFeature::CompositionOneOf);
581            }
582            SchemaKind::AnyOf { .. } => {
583                features.push(ConformanceFeature::CompositionAnyOf);
584            }
585            SchemaKind::AllOf { .. } => {
586                features.push(ConformanceFeature::CompositionAllOf);
587            }
588            _ => {}
589        }
590    }
591
592    /// Detect response code features (resolves $ref in responses)
593    fn annotate_responses(
594        operation: &Operation,
595        spec: &OpenAPI,
596        features: &mut Vec<ConformanceFeature>,
597    ) {
598        for (status_code, resp_ref) in &operation.responses.responses {
599            // Only count features for responses that actually resolve
600            if ref_resolver::resolve_response(resp_ref, spec).is_some() {
601                match status_code {
602                    openapiv3::StatusCode::Code(200) => {
603                        features.push(ConformanceFeature::Response200)
604                    }
605                    openapiv3::StatusCode::Code(201) => {
606                        features.push(ConformanceFeature::Response201)
607                    }
608                    openapiv3::StatusCode::Code(204) => {
609                        features.push(ConformanceFeature::Response204)
610                    }
611                    openapiv3::StatusCode::Code(400) => {
612                        features.push(ConformanceFeature::Response400)
613                    }
614                    openapiv3::StatusCode::Code(404) => {
615                        features.push(ConformanceFeature::Response404)
616                    }
617                    _ => {}
618                }
619            }
620        }
621    }
622
623    /// Round 25 — extract every declared response schema, keyed by the
624    /// numeric HTTP status. Only `application/json` content types are
625    /// included (the only body shape the self-test validates against).
626    /// Used by the self-test driver to validate response bodies against
627    /// the schema for the ACTUAL status returned, not just 200.
628    fn extract_response_schemas_per_status(
629        operation: &Operation,
630        spec: &OpenAPI,
631    ) -> std::collections::BTreeMap<u16, serde_json::Value> {
632        let mut out = std::collections::BTreeMap::new();
633        for (code, resp_ref) in &operation.responses.responses {
634            let openapiv3::StatusCode::Code(n) = code else {
635                continue; // skip "default" / range responses for now
636            };
637            let Some(response) = ref_resolver::resolve_response(resp_ref, spec) else {
638                continue;
639            };
640            let Some(media) = response.content.get("application/json") else {
641                continue;
642            };
643            let Some(schema_ref) = &media.schema else {
644                continue;
645            };
646            let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) else {
647                continue;
648            };
649            // Convert openapiv3::Schema → serde_json::Value so the
650            // validation step can hand it straight to jsonschema.
651            if let Ok(value) = serde_json::to_value(schema) {
652                out.insert(*n, value);
653            }
654        }
655        out
656    }
657
658    /// Extract the response schema for the primary success response (200 or 201)
659    /// Resolves $ref for both the response and the schema within it.
660    fn extract_response_schema(operation: &Operation, spec: &OpenAPI) -> Option<Schema> {
661        // Try 200 first, then 201
662        for code in [200u16, 201] {
663            if let Some(resp_ref) =
664                operation.responses.responses.get(&openapiv3::StatusCode::Code(code))
665            {
666                if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
667                    if let Some(media) = response.content.get("application/json") {
668                        if let Some(schema_ref) = &media.schema {
669                            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
670                                return Some(schema.clone());
671                            }
672                        }
673                    }
674                }
675            }
676        }
677        None
678    }
679
680    /// Detect content negotiation: response supports multiple content types
681    fn annotate_content_negotiation(
682        operation: &Operation,
683        spec: &OpenAPI,
684        features: &mut Vec<ConformanceFeature>,
685    ) {
686        for (_status_code, resp_ref) in &operation.responses.responses {
687            if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
688                if response.content.len() > 1 {
689                    features.push(ConformanceFeature::ContentNegotiation);
690                    return; // Only need to detect once per operation
691                }
692            }
693        }
694    }
695
696    /// Detect security scheme features.
697    /// Checks operation-level security first, falling back to global security requirements.
698    /// Resolves scheme names against SecurityScheme definitions in components.
699    fn annotate_security(
700        operation: &Operation,
701        spec: &OpenAPI,
702        features: &mut Vec<ConformanceFeature>,
703        security_schemes: &mut Vec<SecuritySchemeInfo>,
704    ) {
705        // Use operation-level security if present, otherwise fall back to global
706        let security_reqs = operation.security.as_ref().or(spec.security.as_ref());
707
708        if let Some(security) = security_reqs {
709            for security_req in security {
710                for scheme_name in security_req.keys() {
711                    // Try to resolve the scheme from components for accurate type detection
712                    if let Some(resolved) = Self::resolve_security_scheme(scheme_name, spec) {
713                        match resolved {
714                            SecurityScheme::HTTP { ref scheme, .. } => {
715                                if scheme.eq_ignore_ascii_case("bearer") {
716                                    features.push(ConformanceFeature::SecurityBearer);
717                                    security_schemes.push(SecuritySchemeInfo::Bearer);
718                                } else if scheme.eq_ignore_ascii_case("basic") {
719                                    features.push(ConformanceFeature::SecurityBasic);
720                                    security_schemes.push(SecuritySchemeInfo::Basic);
721                                }
722                            }
723                            SecurityScheme::APIKey { location, name, .. } => {
724                                features.push(ConformanceFeature::SecurityApiKey);
725                                let loc = match location {
726                                    openapiv3::APIKeyLocation::Query => ApiKeyLocation::Query,
727                                    openapiv3::APIKeyLocation::Header => ApiKeyLocation::Header,
728                                    openapiv3::APIKeyLocation::Cookie => ApiKeyLocation::Cookie,
729                                };
730                                security_schemes.push(SecuritySchemeInfo::ApiKey {
731                                    location: loc,
732                                    name: name.clone(),
733                                });
734                            }
735                            // OAuth2 and OpenIDConnect don't map to our current feature set
736                            _ => {}
737                        }
738                    } else {
739                        // Fallback: heuristic name matching for unresolvable schemes
740                        let name_lower = scheme_name.to_lowercase();
741                        if name_lower.contains("bearer") || name_lower.contains("jwt") {
742                            features.push(ConformanceFeature::SecurityBearer);
743                            security_schemes.push(SecuritySchemeInfo::Bearer);
744                        } else if name_lower.contains("api") && name_lower.contains("key") {
745                            features.push(ConformanceFeature::SecurityApiKey);
746                            security_schemes.push(SecuritySchemeInfo::ApiKey {
747                                location: ApiKeyLocation::Header,
748                                name: "X-API-Key".to_string(),
749                            });
750                        } else if name_lower.contains("basic") {
751                            features.push(ConformanceFeature::SecurityBasic);
752                            security_schemes.push(SecuritySchemeInfo::Basic);
753                        }
754                    }
755                }
756            }
757        }
758    }
759
760    /// Resolve a security scheme name to its SecurityScheme definition
761    fn resolve_security_scheme<'a>(name: &str, spec: &'a OpenAPI) -> Option<&'a SecurityScheme> {
762        let components = spec.components.as_ref()?;
763        match components.security_schemes.get(name)? {
764            ReferenceOr::Item(scheme) => Some(scheme),
765            ReferenceOr::Reference { .. } => None,
766        }
767    }
768
769    /// Returns the number of operations being tested
770    pub fn operation_count(&self) -> usize {
771        self.operations.len()
772    }
773
774    /// Generate the k6 conformance script.
775    /// Returns (script, check_count) where check_count is the number of unique checks emitted.
776    pub fn generate(&self) -> Result<(String, usize)> {
777        let mut script = String::with_capacity(16384);
778
779        // Imports
780        script.push_str("import http from 'k6/http';\n");
781        script.push_str("import { check, group } from 'k6';\n");
782        if self.config.request_delay_ms > 0 {
783            script.push_str("import { sleep } from 'k6';\n");
784        }
785        script.push('\n');
786
787        // Tell k6 that all HTTP status codes are "expected" in conformance mode.
788        // Without this, k6 counts 4xx responses (e.g. intentional 404 tests) as
789        // http_req_failed errors, producing a misleading error rate percentage.
790        script.push_str(
791            "http.setResponseCallback(http.expectedStatuses({ min: 100, max: 599 }));\n\n",
792        );
793
794        // Options
795        script.push_str("export const options = {\n");
796        script.push_str("  vus: 1,\n");
797        script.push_str("  iterations: 1,\n");
798        if self.config.skip_tls_verify {
799            script.push_str("  insecureSkipTLSVerify: true,\n");
800        }
801        script.push_str("  thresholds: {\n");
802        script.push_str("    checks: ['rate>0'],\n");
803        script.push_str("  },\n");
804        script.push_str("};\n\n");
805
806        // Base URL (includes base_path if configured)
807        script.push_str(&format!("const BASE_URL = '{}';\n\n", self.config.effective_base_url()));
808        script.push_str("const JSON_HEADERS = { 'Content-Type': 'application/json' };\n\n");
809
810        // Round 39 (#79) — emit init-scope code (e.g. `open()` calls
811        // for file uploads in custom checks) here, before any
812        // function declarations.
813        let custom_emit = self.config.generate_custom_group()?;
814        if let Some(emit) = &custom_emit {
815            if !emit.init_code.is_empty() {
816                script.push_str("// Round 39 (#79) — preloaded upload bytes for custom checks\n");
817                script.push_str(&emit.init_code);
818                script.push('\n');
819            }
820        }
821
822        // Failure detail collector — logs req/res info for failed checks via console.log
823        // (k6's handleSummary runs in a separate JS context, so we can't use module-level arrays)
824        script
825            .push_str("function __captureFailure(checkName, res, expected, schemaViolations) {\n");
826        script.push_str("  let bodyStr = '';\n");
827        script.push_str("  try { if (res.body) { const __n = res.body.length; bodyStr = res.body.substring(0, 65536); if (__n > 65536) bodyStr = bodyStr + ' <truncated at 65536 bytes; full body was ' + __n + ' bytes>'; } else { bodyStr = ''; } } catch(e) { bodyStr = '<unreadable>'; }\n");
828        script.push_str("  let reqHeaders = {};\n");
829        script.push_str(
830            "  if (res.request && res.request.headers) { reqHeaders = res.request.headers; }\n",
831        );
832        script.push_str("  let reqBody = '';\n");
833        script.push_str("  if (res.request && res.request.body) { try { const __m = res.request.body.length; reqBody = res.request.body.substring(0, 65536); if (__m > 65536) reqBody = reqBody + ' <truncated at 65536 bytes; full body was ' + __m + ' bytes>'; } catch(e) {} }\n");
834        script.push_str("  let payload = {\n");
835        script.push_str("    check: checkName,\n");
836        script.push_str("    request: {\n");
837        script.push_str("      method: res.request ? res.request.method : 'unknown',\n");
838        script.push_str("      url: res.request ? res.request.url : res.url || 'unknown',\n");
839        script.push_str("      headers: reqHeaders,\n");
840        script.push_str("      body: reqBody,\n");
841        script.push_str("    },\n");
842        script.push_str("    response: {\n");
843        script.push_str("      status: res.status,\n");
844        script.push_str("      headers: res.headers ? Object.fromEntries(Object.entries(res.headers).slice(0, 20)) : {},\n");
845        script.push_str("      body: bodyStr,\n");
846        script.push_str("    },\n");
847        script.push_str("    expected: expected,\n");
848        script.push_str("  };\n");
849        script.push_str("  if (schemaViolations && schemaViolations.length > 0) { payload.schema_violations = schemaViolations; }\n");
850        script.push_str("  console.log('MOCKFORGE_FAILURE:' + JSON.stringify(payload));\n");
851        script.push_str("}\n\n");
852
853        // Request/response capture for --export-requests
854        //
855        // Round 44 (#79) — mirror the resilience added in `generator.rs`:
856        // wrap the primary stringify in try/catch + fall back to a minimal
857        // entry + last-resort hand-rolled JSON so a multipart body that
858        // contains bytes JSON.stringify can't roundtrip never silently
859        // drops the row from the export. See `generator.rs` for the
860        // surfacing incident.
861        if self.config.export_requests {
862            script.push_str("function __captureExchange(checkName, res) {\n");
863            script.push_str("  try {\n");
864            script.push_str("    let bodyStr = '';\n");
865            script.push_str("    try { if (res.body) { const __n = res.body.length; bodyStr = res.body.substring(0, 65536); if (__n > 65536) bodyStr = bodyStr + ' <truncated at 65536 bytes; full body was ' + __n + ' bytes>'; } else { bodyStr = ''; } } catch(e) { bodyStr = '<unreadable>'; }\n");
866            script.push_str("    let reqHeaders = {};\n");
867            script.push_str(
868                "    if (res.request && res.request.headers) { reqHeaders = res.request.headers; }\n",
869            );
870            // Round 46 (#79) — mirror the structured multipart summary
871            // from `generator.rs`. See the long comment there; same
872            // rationale (k6 logfmt mangling on raw binary bytes).
873            script.push_str("    let reqBody = '';\n");
874            script.push_str("    {\n");
875            script.push_str(
876                "      const ct = (reqHeaders['Content-Type'] || reqHeaders['content-type'] || '').toString();\n",
877            );
878            script.push_str("      const isMultipart = ct.startsWith('multipart/');\n");
879            script.push_str(
880                "      if (isMultipart && res.request && res.request.body) {\n\
881                 \x20\x20\x20\x20\x20\x20\x20\x20try {\n\
882                 \x20\x20\x20\x20\x20\x20\x20\x20  const raw = res.request.body;\n\
883                 \x20\x20\x20\x20\x20\x20\x20\x20  let totalBytes = raw.length;\n\
884                 \x20\x20\x20\x20\x20\x20\x20\x20  let envelopeBytes = 0;\n\
885                 \x20\x20\x20\x20\x20\x20\x20\x20  const boundaryMatch = ct.match(/boundary=([^;]+)/);\n\
886                 \x20\x20\x20\x20\x20\x20\x20\x20  const boundary = boundaryMatch ? boundaryMatch[1].replace(/^\"|\"$/g, '') : '';\n\
887                 \x20\x20\x20\x20\x20\x20\x20\x20  const parts = [];\n\
888                 \x20\x20\x20\x20\x20\x20\x20\x20  if (boundary) {\n\
889                 \x20\x20\x20\x20\x20\x20\x20\x20    const sep = '--' + boundary;\n\
890                 \x20\x20\x20\x20\x20\x20\x20\x20    let cursor = raw.indexOf(sep);\n\
891                 \x20\x20\x20\x20\x20\x20\x20\x20    while (cursor !== -1 && parts.length < 100) {\n\
892                 \x20\x20\x20\x20\x20\x20\x20\x20      const next = raw.indexOf(sep, cursor + sep.length);\n\
893                 \x20\x20\x20\x20\x20\x20\x20\x20      if (next === -1) break;\n\
894                 \x20\x20\x20\x20\x20\x20\x20\x20      const slice = raw.substring(cursor + sep.length, next);\n\
895                 \x20\x20\x20\x20\x20\x20\x20\x20      const headerEnd = slice.indexOf('\\r\\n\\r\\n');\n\
896                 \x20\x20\x20\x20\x20\x20\x20\x20      const partHeaders = headerEnd === -1 ? slice : slice.substring(0, headerEnd);\n\
897                 \x20\x20\x20\x20\x20\x20\x20\x20      const partBody = headerEnd === -1 ? '' : slice.substring(headerEnd + 4);\n\
898                 \x20\x20\x20\x20\x20\x20\x20\x20      // Round 50 #79 — ASCII envelope is byte-accurate (see generator.rs).\n\
899                 \x20\x20\x20\x20\x20\x20\x20\x20      envelopeBytes += sep.length + partHeaders.length + 6;\n\
900                 \x20\x20\x20\x20\x20\x20\x20\x20      const nameMatch = partHeaders.match(/name=\"([^\"]+)\"/);\n\
901                 \x20\x20\x20\x20\x20\x20\x20\x20      const filenameMatch = partHeaders.match(/filename=\"([^\"]+)\"/);\n\
902                 \x20\x20\x20\x20\x20\x20\x20\x20      const partCtMatch = partHeaders.match(/Content-Type:\\s*([^\\r\\n]+)/i);\n\
903                 \x20\x20\x20\x20\x20\x20\x20\x20      parts.push({\n\
904                 \x20\x20\x20\x20\x20\x20\x20\x20        name: nameMatch ? nameMatch[1] : '',\n\
905                 \x20\x20\x20\x20\x20\x20\x20\x20        filename: filenameMatch ? filenameMatch[1] : '',\n\
906                 \x20\x20\x20\x20\x20\x20\x20\x20        contentType: partCtMatch ? partCtMatch[1].trim() : '',\n\
907                 \x20\x20\x20\x20\x20\x20\x20\x20        bytes: Math.max(0, partBody.length - 2),\n\
908                 \x20\x20\x20\x20\x20\x20\x20\x20      });\n\
909                 \x20\x20\x20\x20\x20\x20\x20\x20      cursor = next;\n\
910                 \x20\x20\x20\x20\x20\x20\x20\x20    }\n\
911                 \x20\x20\x20\x20\x20\x20\x20\x20    if (parts.length) { envelopeBytes += sep.length + 4; }\n\
912                 \x20\x20\x20\x20\x20\x20\x20\x20  }\n\
913                 \x20\x20\x20\x20\x20\x20\x20\x20  // Round 47 #79 — overlay on-disk byte counts (see generator.rs).\n\
914                 \x20\x20\x20\x20\x20\x20\x20\x20  const __mfSizes = (globalThis.__mfUploadSizes || {})[checkName] || {};\n\
915                 \x20\x20\x20\x20\x20\x20\x20\x20  let __allKnown = parts.length > 0;\n\
916                 \x20\x20\x20\x20\x20\x20\x20\x20  parts.forEach(function (p) { if (typeof __mfSizes[p.name] === 'number') { p.bytes = __mfSizes[p.name]; } else { __allKnown = false; } });\n\
917                 \x20\x20\x20\x20\x20\x20\x20\x20  const partsTotal = parts.reduce(function (acc, p) { return acc + p.bytes; }, 0);\n\
918                 \x20\x20\x20\x20\x20\x20\x20\x20  if (__allKnown) totalBytes = partsTotal;\n\
919                 \x20\x20\x20\x20\x20\x20\x20\x20  // Round 49/50 #79 — total = disk-sum payload; wire = total +\n\
920                 \x20\x20\x20\x20\x20\x20\x20\x20  // ASCII envelope. raw.length UNDERcounts binary bodies, so\n\
921                 \x20\x20\x20\x20\x20\x20\x20\x20  // never use it for wire when part sizes are known.\n\
922                 \x20\x20\x20\x20\x20\x20\x20\x20  // Round 51 #79 — prefer k6's Content-Length (exact wire body\n\
923                 \x20\x20\x20\x20\x20\x20\x20\x20  // size, matches the proxy) over the reconstructed envelope.\n\
924                 \x20\x20\x20\x20\x20\x20\x20\x20  const __clHdr = parseInt((reqHeaders['Content-Length'] || reqHeaders['content-length'] || ''), 10);\n\
925                 \x20\x20\x20\x20\x20\x20\x20\x20  const wireBytes = (!isNaN(__clHdr) && __clHdr > 0) ? __clHdr : (__allKnown ? (partsTotal + envelopeBytes) : ((typeof raw === 'string' && raw.length) ? raw.length : totalBytes));\n\
926                 \x20\x20\x20\x20\x20\x20\x20\x20  // Round 52 #79 — Srikanth on 0.3.198 still saw a fixed 264-byte gap\n\
927                 \x20\x20\x20\x20\x20\x20\x20\x20  // (proxy 57998271 vs our 57998007). Those 264 bytes are the top-level\n\
928                 \x20\x20\x20\x20\x20\x20\x20\x20  // HTTP request preface (request-line + Host + the script-visible\n\
929                 \x20\x20\x20\x20\x20\x20\x20\x20  // headers + the transport-managed Content-Length + the blank line):\n\
930                 \x20\x20\x20\x20\x20\x20\x20\x20  // his proxy meters the whole request, we reported only the multipart\n\
931                 \x20\x20\x20\x20\x20\x20\x20\x20  // entity body (Content-Length). Reconstruct the header block so\n\
932                 \x20\x20\x20\x20\x20\x20\x20\x20  // `request` reconciles with a full-request byte counter.\n\
933                 \x20\x20\x20\x20\x20\x20\x20\x20  let __hdrBytes = 0;\n\
934                 \x20\x20\x20\x20\x20\x20\x20\x20  try {\n\
935                 \x20\x20\x20\x20\x20\x20\x20\x20    const __method = (res.request && res.request.method) ? res.request.method : 'POST';\n\
936                 \x20\x20\x20\x20\x20\x20\x20\x20    const __url = (res.request && res.request.url) ? res.request.url : '';\n\
937                 \x20\x20\x20\x20\x20\x20\x20\x20    let __rest = __url; const __sch = __rest.indexOf('://'); if (__sch !== -1) __rest = __rest.substring(__sch + 3);\n\
938                 \x20\x20\x20\x20\x20\x20\x20\x20    const __slash = __rest.indexOf('/'); const __host = __slash === -1 ? __rest : __rest.substring(0, __slash); const __pathq = __slash === -1 ? '/' : __rest.substring(__slash);\n\
939                 \x20\x20\x20\x20\x20\x20\x20\x20    __hdrBytes += (__method + ' ' + __pathq + ' HTTP/1.1').length + 2;\n\
940                 \x20\x20\x20\x20\x20\x20\x20\x20    if (__host) __hdrBytes += ('Host: ' + __host).length + 2;\n\
941                 \x20\x20\x20\x20\x20\x20\x20\x20    for (const __hn in reqHeaders) { let __hv = reqHeaders[__hn]; if (Array.isArray(__hv)) __hv = __hv.join(', '); __hdrBytes += (__hn + ': ' + String(__hv)).length + 2; }\n\
942                 \x20\x20\x20\x20\x20\x20\x20\x20    if (!('Content-Length' in reqHeaders) && !('content-length' in reqHeaders)) { __hdrBytes += ('Content-Length: ' + wireBytes).length + 2; }\n\
943                 \x20\x20\x20\x20\x20\x20\x20\x20    __hdrBytes += 2;\n\
944                 \x20\x20\x20\x20\x20\x20\x20\x20  } catch (e) { __hdrBytes = 0; }\n\
945                 \x20\x20\x20\x20\x20\x20\x20\x20  const requestBytes = wireBytes + __hdrBytes;\n\
946                 \x20\x20\x20\x20\x20\x20\x20\x20  const summary = parts.map(function (p) { return '\\'' + p.name + '\\':\\'' + p.filename + '\\' (' + p.contentType + ', ' + p.bytes + ' bytes)'; }).join(', ');\n\
947                 \x20\x20\x20\x20\x20\x20\x20\x20  reqBody = '<multipart/form-data; boundary=' + boundary + '; ' + parts.length + ' part(s); total ' + totalBytes + ' bytes (wire ' + wireBytes + ' bytes w/ envelope' + (__hdrBytes > 0 ? ('; request ' + requestBytes + ' bytes incl ' + __hdrBytes + '-byte header block') : '') + '): ' + summary + '>';\n\
948                 \x20\x20\x20\x20\x20\x20\x20\x20} catch (e) {\n\
949                 \x20\x20\x20\x20\x20\x20\x20\x20  reqBody = '<multipart upload; summary failed: ' + (e && e.message ? e.message : 'unknown') + '>';\n\
950                 \x20\x20\x20\x20\x20\x20\x20\x20}\n\
951                 \x20\x20\x20\x20\x20\x20} else if (isMultipart) {\n\
952                 \x20\x20\x20\x20\x20\x20\x20\x20reqBody = '<multipart upload; body bytes not surfaced by k6 res.request.body>';\n\
953                 \x20\x20\x20\x20\x20\x20} else if (res.request && res.request.body) {\n\
954                 \x20\x20\x20\x20\x20\x20\x20\x20try { const __m = res.request.body.length; reqBody = res.request.body.substring(0, 65536); if (__m > 65536) reqBody = reqBody + ' <truncated at 65536 bytes; full body was ' + __m + ' bytes>'; } catch (e) {}\n\
955                 \x20\x20\x20\x20\x20\x20}\n\
956                 \x20\x20\x20\x20}\n",
957            );
958            // Round 47 (#79) — mirror network-event emit from generator.rs.
959            script.push_str(
960                "    if (res && res.status === 0) {\n\
961                 \x20\x20\x20\x20\x20\x20const ec = (res.error_code != null) ? res.error_code : 0;\n\
962                 \x20\x20\x20\x20\x20\x20const em = (res.error != null) ? String(res.error) : '';\n\
963                 \x20\x20\x20\x20\x20\x20let kind = 'other';\n\
964                 \x20\x20\x20\x20\x20\x20if (ec >= 1200 && ec < 1300) kind = 'connect';\n\
965                 \x20\x20\x20\x20\x20\x20else if (ec >= 1300 && ec < 1400) kind = 'tls';\n\
966                 \x20\x20\x20\x20\x20\x20else if (ec >= 1400 && ec < 1500) kind = 'timeout';\n\
967                 \x20\x20\x20\x20\x20\x20else if (em.toLowerCase().indexOf('eof') !== -1) kind = 'connect';\n                 \x20\x20\x20\x20\x20\x20else if (em.toLowerCase().indexOf('timeout') !== -1) kind = 'timeout';\n\
968                 \x20\x20\x20\x20\x20\x20else if (em.toLowerCase().indexOf('tls') !== -1) kind = 'tls';\n\
969                 \x20\x20\x20\x20\x20\x20else if (em.toLowerCase().indexOf('connect') !== -1 || em.toLowerCase().indexOf('refused') !== -1) kind = 'connect';\n\
970                 \x20\x20\x20\x20\x20\x20console.log('MOCKFORGE_NETWORK_EVENT:' + JSON.stringify({\n\
971                 \x20\x20\x20\x20\x20\x20  timestamp: new Date().toISOString(),\n\
972                 \x20\x20\x20\x20\x20\x20  check: checkName,\n\
973                 \x20\x20\x20\x20\x20\x20  method: res.request ? res.request.method : 'unknown',\n\
974                 \x20\x20\x20\x20\x20\x20  url: res.request ? res.request.url : res.url || 'unknown',\n\
975                 \x20\x20\x20\x20\x20\x20  kind: kind,\n\
976                 \x20\x20\x20\x20\x20\x20  error_code: ec,\n\
977                 \x20\x20\x20\x20\x20\x20  message: em,\n\
978                 \x20\x20\x20\x20\x20\x20}));\n\
979                 \x20\x20\x20\x20}\n",
980            );
981            script.push_str("    console.log('MOCKFORGE_EXCHANGE:' + JSON.stringify({\n");
982            script.push_str("      check: checkName,\n");
983            script.push_str("      request: {\n");
984            script.push_str("        method: res.request ? res.request.method : 'unknown',\n");
985            script.push_str("        url: res.request ? res.request.url : res.url || 'unknown',\n");
986            script.push_str("        headers: reqHeaders,\n");
987            script.push_str("        body: reqBody,\n");
988            script.push_str("      },\n");
989            script.push_str("      response: {\n");
990            script.push_str("        status: res.status,\n");
991            script.push_str("        headers: res.headers ? Object.fromEntries(Object.entries(res.headers).slice(0, 30)) : {},\n");
992            script.push_str("        body: bodyStr,\n");
993            script.push_str("      },\n");
994            script.push_str("    }));\n");
995            script.push_str("  } catch (e) {\n");
996            script.push_str("    try {\n");
997            script.push_str("      console.log('MOCKFORGE_EXCHANGE:' + JSON.stringify({\n");
998            script.push_str("        check: checkName,\n");
999            script.push_str("        request: {\n");
1000            script.push_str(
1001                "          method: (res && res.request) ? res.request.method : 'unknown',\n",
1002            );
1003            script.push_str("          url: (res && res.request) ? res.request.url : (res && res.url) || 'unknown',\n");
1004            script.push_str("          headers: {},\n");
1005            script.push_str("          body: '<exchange capture failed: ' + (e && e.message ? e.message : 'unknown error') + '>',\n");
1006            script.push_str("        },\n");
1007            script.push_str("        response: {\n");
1008            script.push_str("          status: (res && res.status) || 0,\n");
1009            script.push_str("          headers: {},\n");
1010            script.push_str("          body: '',\n");
1011            script.push_str("        },\n");
1012            script.push_str("        _export_error: (e && e.message) ? e.message : String(e),\n");
1013            script.push_str("      }));\n");
1014            script.push_str("    } catch (e2) {\n");
1015            script.push_str("      console.log('MOCKFORGE_EXCHANGE:{\"check\":\"' + checkName + '\",\"request\":{\"method\":\"unknown\",\"url\":\"unknown\",\"headers\":{},\"body\":\"\"},\"response\":{\"status\":0,\"headers\":{},\"body\":\"\"},\"_export_error\":\"double-fault\"}');\n");
1016            script.push_str("    }\n");
1017            script.push_str("  }\n");
1018            script.push_str("}\n\n");
1019        }
1020
1021        // Default function
1022        script.push_str("export default function () {\n");
1023
1024        if self.config.has_cookie_header() {
1025            script.push_str(
1026                "  // Clear cookie jar to prevent server Set-Cookie from duplicating custom Cookie header\n",
1027            );
1028            script.push_str("  http.cookieJar().clear(BASE_URL);\n\n");
1029        }
1030
1031        // Group operations by category
1032        let mut category_ops: std::collections::BTreeMap<
1033            &'static str,
1034            Vec<(&AnnotatedOperation, &ConformanceFeature)>,
1035        > = std::collections::BTreeMap::new();
1036
1037        for op in &self.operations {
1038            for feature in &op.features {
1039                let category = feature.category();
1040                if self.config.should_include_category(category) {
1041                    category_ops.entry(category).or_default().push((op, feature));
1042                }
1043            }
1044        }
1045
1046        // Emit grouped tests
1047        let mut total_checks = 0usize;
1048        for (category, ops) in &category_ops {
1049            script.push_str(&format!("  group('{}', function () {{\n", category));
1050
1051            if self.config.all_operations {
1052                // All-operations mode: test every operation, using path-qualified check names
1053                let mut emitted_checks: HashSet<String> = HashSet::new();
1054                for (op, feature) in ops {
1055                    let qualified = format!("{}:{}", feature.check_name(), op.path);
1056                    if emitted_checks.insert(qualified.clone()) {
1057                        self.emit_check_named(&mut script, op, feature, &qualified);
1058                        total_checks += 1;
1059                    }
1060                }
1061            } else {
1062                // Default: one representative operation per feature, with path-qualified
1063                // check names so failures identify which endpoint was tested.
1064                let mut emitted_features: HashSet<&str> = HashSet::new();
1065                for (op, feature) in ops {
1066                    if emitted_features.insert(feature.check_name()) {
1067                        let qualified = format!("{}:{}", feature.check_name(), op.path);
1068                        self.emit_check_named(&mut script, op, feature, &qualified);
1069                        total_checks += 1;
1070                    }
1071                }
1072            }
1073
1074            script.push_str("  });\n\n");
1075        }
1076
1077        // Custom checks from YAML file — round 39: init-scope code
1078        // was already emitted above; here we just splice the group
1079        // body inside the default function.
1080        if let Some(emit) = custom_emit {
1081            script.push_str(&emit.group_body);
1082        }
1083
1084        script.push_str("}\n\n");
1085
1086        // handleSummary
1087        self.generate_handle_summary(&mut script);
1088
1089        Ok((script, total_checks))
1090    }
1091
1092    /// Emit a single k6 check for an operation + feature with a custom check name
1093    fn emit_check_named(
1094        &self,
1095        script: &mut String,
1096        op: &AnnotatedOperation,
1097        feature: &ConformanceFeature,
1098        check_name: &str,
1099    ) {
1100        // Escape single quotes in check name since it's embedded in JS single-quoted strings
1101        let check_name = check_name.replace('\'', "\\'");
1102        let check_name = check_name.as_str();
1103
1104        script.push_str("    {\n");
1105
1106        // Build the URL path with parameters substituted
1107        let mut url_path = op.path.clone();
1108        for (name, value) in &op.path_params {
1109            url_path = url_path.replace(&format!("{{{}}}", name), value);
1110        }
1111
1112        // Build query string
1113        if !op.query_params.is_empty() {
1114            let qs: Vec<String> =
1115                op.query_params.iter().map(|(k, v)| format!("{}={}", k, v)).collect();
1116            url_path = format!("{}?{}", url_path, qs.join("&"));
1117        }
1118
1119        let full_url = format!("${{BASE_URL}}{}", url_path);
1120
1121        // Build effective headers: merge spec-derived headers with custom headers.
1122        // Custom headers override spec-derived ones with the same name.
1123        let mut effective_headers = self.effective_headers(&op.header_params);
1124
1125        // For non-default response code checks, add header to tell the mock server
1126        // which status code to return (the server defaults to the first declared status)
1127        if matches!(feature, ConformanceFeature::Response400 | ConformanceFeature::Response404) {
1128            let expected_code = match feature {
1129                ConformanceFeature::Response400 => "400",
1130                ConformanceFeature::Response404 => "404",
1131                _ => unreachable!(),
1132            };
1133            effective_headers
1134                .push(("X-Mockforge-Response-Status".to_string(), expected_code.to_string()));
1135        }
1136
1137        // For security checks AND for all requests on endpoints with security requirements,
1138        // inject auth credentials so the server doesn't reject with 401.
1139        // Only inject if the user hasn't already provided the header via --custom-headers.
1140        let needs_auth = matches!(
1141            feature,
1142            ConformanceFeature::SecurityBearer
1143                | ConformanceFeature::SecurityBasic
1144                | ConformanceFeature::SecurityApiKey
1145        ) || !op.security_schemes.is_empty();
1146
1147        if needs_auth {
1148            self.inject_security_headers(&op.security_schemes, &mut effective_headers);
1149        }
1150
1151        let has_headers = !effective_headers.is_empty();
1152        let headers_obj = if has_headers {
1153            Self::format_headers(&effective_headers)
1154        } else {
1155            String::new()
1156        };
1157
1158        // Determine HTTP method and emit request
1159        match op.method.as_str() {
1160            "GET" => {
1161                if has_headers {
1162                    script.push_str(&format!(
1163                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
1164                        full_url, headers_obj
1165                    ));
1166                } else {
1167                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
1168                }
1169            }
1170            "POST" => {
1171                self.emit_request_with_body(script, "post", &full_url, op, &effective_headers);
1172            }
1173            "PUT" => {
1174                self.emit_request_with_body(script, "put", &full_url, op, &effective_headers);
1175            }
1176            "PATCH" => {
1177                self.emit_request_with_body(script, "patch", &full_url, op, &effective_headers);
1178            }
1179            "DELETE" => {
1180                if has_headers {
1181                    script.push_str(&format!(
1182                        "      let res = http.del(`{}`, null, {{ headers: {} }});\n",
1183                        full_url, headers_obj
1184                    ));
1185                } else {
1186                    script.push_str(&format!("      let res = http.del(`{}`);\n", full_url));
1187                }
1188            }
1189            "HEAD" => {
1190                if has_headers {
1191                    script.push_str(&format!(
1192                        "      let res = http.head(`{}`, {{ headers: {} }});\n",
1193                        full_url, headers_obj
1194                    ));
1195                } else {
1196                    script.push_str(&format!("      let res = http.head(`{}`);\n", full_url));
1197                }
1198            }
1199            "OPTIONS" => {
1200                if has_headers {
1201                    script.push_str(&format!(
1202                        "      let res = http.options(`{}`, null, {{ headers: {} }});\n",
1203                        full_url, headers_obj
1204                    ));
1205                } else {
1206                    script.push_str(&format!("      let res = http.options(`{}`);\n", full_url));
1207                }
1208            }
1209            _ => {
1210                if has_headers {
1211                    script.push_str(&format!(
1212                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
1213                        full_url, headers_obj
1214                    ));
1215                } else {
1216                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
1217                }
1218            }
1219        }
1220
1221        // Capture request/response for --export-requests
1222        // (check_name is already escaped at line 829 above — don't double-escape)
1223        if self.config.export_requests {
1224            script.push_str(&format!(
1225                "      if (typeof __captureExchange === 'function') __captureExchange('{}', res);\n",
1226                check_name
1227            ));
1228        }
1229
1230        // Check: emit assertion based on feature type, with failure detail capture
1231        if matches!(
1232            feature,
1233            ConformanceFeature::Response200
1234                | ConformanceFeature::Response201
1235                | ConformanceFeature::Response204
1236                | ConformanceFeature::Response400
1237                | ConformanceFeature::Response404
1238        ) {
1239            let expected_code = match feature {
1240                ConformanceFeature::Response200 => 200,
1241                ConformanceFeature::Response201 => 201,
1242                ConformanceFeature::Response204 => 204,
1243                ConformanceFeature::Response400 => 400,
1244                ConformanceFeature::Response404 => 404,
1245                _ => 200,
1246            };
1247            script.push_str(&format!(
1248                "      {{ let ok = check(res, {{ '{}': (r) => r.status === {} }}); if (!ok) __captureFailure('{}', res, 'status === {}'); }}\n",
1249                check_name, expected_code, check_name, expected_code
1250            ));
1251        } else if matches!(feature, ConformanceFeature::ResponseValidation) {
1252            // Response schema validation — validate the response body against the schema
1253            // Uses inline field-level error collection so failure details include
1254            // which specific fields violated the schema (field_path, violation_type,
1255            // expected, actual) — matching the Ajv `errors` array structure.
1256            if let Some(schema) = &op.response_schema {
1257                let validation_js = SchemaValidatorGenerator::generate_validation(schema);
1258                let schema_json = serde_json::to_string(schema).unwrap_or_default();
1259                // Escape backticks and backslashes for JS template literal safety
1260                let schema_json_escaped = schema_json.replace('\\', "\\\\").replace('`', "\\`");
1261                script.push_str(&format!(
1262                    concat!(
1263                        "      try {{\n",
1264                        "        let body = res.json();\n",
1265                        "        let ok = check(res, {{ '{check}': (r) => ( {validation} ) }});\n",
1266                        "        if (!ok) {{\n",
1267                        "          let __violations = [];\n",
1268                        "          try {{\n",
1269                        "            let __schema = JSON.parse(`{schema}`);\n",
1270                        "            function __collectErrors(schema, data, path) {{\n",
1271                        "              if (!schema || typeof schema !== 'object') return;\n",
1272                        "              let st = schema.type || (schema.schema_kind && schema.schema_kind.Type && Object.keys(schema.schema_kind.Type)[0]);\n",
1273                        "              if (st) {{ st = st.toLowerCase(); }}\n",
1274                        "              if (st === 'object') {{\n",
1275                        "                if (typeof data !== 'object' || data === null) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'object', actual: typeof data }}); return; }}\n",
1276                        "                let props = schema.properties || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Object && schema.schema_kind.Type.Object.properties) || {{}};\n",
1277                        "                let req = schema.required || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Object && schema.schema_kind.Type.Object.required) || [];\n",
1278                        "                for (let f of req) {{ if (!(f in data)) {{ __violations.push({{ field_path: path + '/' + f, violation_type: 'required', expected: 'present', actual: 'missing' }}); }} }}\n",
1279                        "                for (let [k, v] of Object.entries(props)) {{ if (data[k] !== undefined) {{ let ps = v.Item || v; __collectErrors(ps, data[k], path + '/' + k); }} }}\n",
1280                        "              }} else if (st === 'array') {{\n",
1281                        "                if (!Array.isArray(data)) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'array', actual: typeof data }}); return; }}\n",
1282                        "                let items = schema.items || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Array && schema.schema_kind.Type.Array.items);\n",
1283                        "                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",
1284                        "              }} else if (st === 'string') {{\n",
1285                        "                if (typeof data !== 'string') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'string', actual: typeof data }}); }}\n",
1286                        "              }} else if (st === 'integer') {{\n",
1287                        "                if (typeof data !== 'number' || !Number.isInteger(data)) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'integer', actual: typeof data }}); }}\n",
1288                        "              }} else if (st === 'number') {{\n",
1289                        "                if (typeof data !== 'number') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'number', actual: typeof data }}); }}\n",
1290                        "              }} else if (st === 'boolean') {{\n",
1291                        "                if (typeof data !== 'boolean') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'boolean', actual: typeof data }}); }}\n",
1292                        "              }}\n",
1293                        "            }}\n",
1294                        "            __collectErrors(__schema, body, '');\n",
1295                        "          }} catch(_e) {{}}\n",
1296                        "          __captureFailure('{check}', res, 'schema validation', __violations);\n",
1297                        "        }}\n",
1298                        "      }} catch(e) {{ check(res, {{ '{check}': () => false }}); __captureFailure('{check}', res, 'JSON parse failed: ' + e.message); }}\n",
1299                    ),
1300                    check = check_name,
1301                    validation = validation_js,
1302                    schema = schema_json_escaped,
1303                ));
1304            }
1305        } else if matches!(
1306            feature,
1307            ConformanceFeature::SecurityBearer
1308                | ConformanceFeature::SecurityBasic
1309                | ConformanceFeature::SecurityApiKey
1310        ) {
1311            // Security checks verify the server accepts the auth credentials (not 401/403)
1312            script.push_str(&format!(
1313                "      {{ let ok = check(res, {{ '{}': (r) => r.status >= 200 && r.status < 400 }}); if (!ok) __captureFailure('{}', res, 'status >= 200 && status < 400 (auth accepted)'); }}\n",
1314                check_name, check_name
1315            ));
1316        } else {
1317            script.push_str(&format!(
1318                "      {{ let ok = check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }}); if (!ok) __captureFailure('{}', res, 'status >= 200 && status < 500'); }}\n",
1319                check_name, check_name
1320            ));
1321        }
1322
1323        // Clear cookie jar after each request to prevent Set-Cookie leaking
1324        let has_cookie = self.config.has_cookie_header()
1325            || effective_headers.iter().any(|(h, _)| h.eq_ignore_ascii_case("Cookie"));
1326        if has_cookie {
1327            script.push_str("      http.cookieJar().clear(BASE_URL);\n");
1328        }
1329
1330        script.push_str("    }\n");
1331
1332        // Rate-limit delay between checks (to avoid 429 from target API)
1333        if self.config.request_delay_ms > 0 {
1334            script.push_str(&format!(
1335                "    sleep({:.3});\n",
1336                self.config.request_delay_ms as f64 / 1000.0
1337            ));
1338        }
1339    }
1340
1341    /// Emit an HTTP request with a body
1342    fn emit_request_with_body(
1343        &self,
1344        script: &mut String,
1345        method: &str,
1346        url: &str,
1347        op: &AnnotatedOperation,
1348        effective_headers: &[(String, String)],
1349    ) {
1350        if let Some(body) = &op.sample_body {
1351            let escaped_body = body.replace('\'', "\\'");
1352            let headers = if !effective_headers.is_empty() {
1353                format!(
1354                    "Object.assign({{}}, JSON_HEADERS, {})",
1355                    Self::format_headers(effective_headers)
1356                )
1357            } else {
1358                "JSON_HEADERS".to_string()
1359            };
1360            script.push_str(&format!(
1361                "      let res = http.{}(`{}`, '{}', {{ headers: {} }});\n",
1362                method, url, escaped_body, headers
1363            ));
1364        } else if !effective_headers.is_empty() {
1365            script.push_str(&format!(
1366                "      let res = http.{}(`{}`, null, {{ headers: {} }});\n",
1367                method,
1368                url,
1369                Self::format_headers(effective_headers)
1370            ));
1371        } else {
1372            script.push_str(&format!("      let res = http.{}(`{}`, null);\n", method, url));
1373        }
1374    }
1375
1376    /// Build effective headers by merging spec-derived headers with custom headers.
1377    /// Custom headers override spec-derived ones with the same name (case-insensitive).
1378    /// Custom headers not in the spec are appended.
1379    fn effective_headers(&self, spec_headers: &[(String, String)]) -> Vec<(String, String)> {
1380        let custom = &self.config.custom_headers;
1381        if custom.is_empty() {
1382            return spec_headers.to_vec();
1383        }
1384
1385        let mut result: Vec<(String, String)> = Vec::new();
1386
1387        // Start with spec headers, replacing values if a custom header matches
1388        for (name, value) in spec_headers {
1389            if let Some((_, custom_val)) =
1390                custom.iter().find(|(cn, _)| cn.eq_ignore_ascii_case(name))
1391            {
1392                result.push((name.clone(), custom_val.clone()));
1393            } else {
1394                result.push((name.clone(), value.clone()));
1395            }
1396        }
1397
1398        // Append custom headers that aren't already in spec headers
1399        for (name, value) in custom {
1400            if !spec_headers.iter().any(|(sn, _)| sn.eq_ignore_ascii_case(name)) {
1401                result.push((name.clone(), value.clone()));
1402            }
1403        }
1404
1405        result
1406    }
1407
1408    /// Inject security headers based on resolved security scheme details.
1409    /// Respects user-provided custom headers (won't overwrite if already set).
1410    fn inject_security_headers(
1411        &self,
1412        schemes: &[SecuritySchemeInfo],
1413        headers: &mut Vec<(String, String)>,
1414    ) {
1415        let mut to_add: Vec<(String, String)> = Vec::new();
1416
1417        let has_header = |name: &str, headers: &[(String, String)]| {
1418            headers.iter().any(|(h, _)| h.eq_ignore_ascii_case(name))
1419                || self.config.custom_headers.iter().any(|(h, _)| h.eq_ignore_ascii_case(name))
1420        };
1421
1422        // If user provides Cookie header, they're using session-based auth — skip auto auth
1423        let has_cookie_auth = has_header("Cookie", headers);
1424
1425        for scheme in schemes {
1426            match scheme {
1427                SecuritySchemeInfo::Bearer => {
1428                    if !has_cookie_auth && !has_header("Authorization", headers) {
1429                        // MockForge mock server accepts any Bearer token
1430                        to_add.push((
1431                            "Authorization".to_string(),
1432                            "Bearer mockforge-conformance-test-token".to_string(),
1433                        ));
1434                    }
1435                }
1436                SecuritySchemeInfo::Basic => {
1437                    if !has_cookie_auth && !has_header("Authorization", headers) {
1438                        let creds = self.config.basic_auth.as_deref().unwrap_or("test:test");
1439                        use base64::Engine;
1440                        let encoded =
1441                            base64::engine::general_purpose::STANDARD.encode(creds.as_bytes());
1442                        to_add.push(("Authorization".to_string(), format!("Basic {}", encoded)));
1443                    }
1444                }
1445                SecuritySchemeInfo::ApiKey { location, name } => match location {
1446                    ApiKeyLocation::Header => {
1447                        if !has_header(name, headers) {
1448                            let key = self
1449                                .config
1450                                .api_key
1451                                .as_deref()
1452                                .unwrap_or("mockforge-conformance-test-key");
1453                            to_add.push((name.clone(), key.to_string()));
1454                        }
1455                    }
1456                    ApiKeyLocation::Cookie => {
1457                        if !has_header("Cookie", headers) {
1458                            to_add.push((
1459                                "Cookie".to_string(),
1460                                format!("{}=mockforge-conformance-test-session", name),
1461                            ));
1462                        }
1463                    }
1464                    ApiKeyLocation::Query => {
1465                        // Query params are handled via URL, not headers — skip here
1466                    }
1467                },
1468            }
1469        }
1470
1471        headers.extend(to_add);
1472    }
1473
1474    /// Format header params as a JS object literal
1475    fn format_headers(headers: &[(String, String)]) -> String {
1476        let entries: Vec<String> = headers
1477            .iter()
1478            .map(|(k, v)| format!("'{}': '{}'", k, v.replace('\'', "\\'")))
1479            .collect();
1480        format!("{{ {} }}", entries.join(", "))
1481    }
1482
1483    /// handleSummary — same format as reference mode for report compatibility
1484    fn generate_handle_summary(&self, script: &mut String) {
1485        // Use absolute path for report output so k6 writes where the CLI expects
1486        let report_path = match &self.config.output_dir {
1487            Some(dir) => {
1488                let abs = std::fs::canonicalize(dir)
1489                    .unwrap_or_else(|_| dir.clone())
1490                    .join("conformance-report.json");
1491                abs.to_string_lossy().to_string()
1492            }
1493            None => "conformance-report.json".to_string(),
1494        };
1495
1496        script.push_str("export function handleSummary(data) {\n");
1497        script.push_str("  let checks = {};\n");
1498        script.push_str("  if (data.metrics && data.metrics.checks) {\n");
1499        script.push_str("    checks.overall_pass_rate = data.metrics.checks.values.rate;\n");
1500        script.push_str("  }\n");
1501        script.push_str("  let checkResults = {};\n");
1502        script.push_str("  function walkGroups(group) {\n");
1503        script.push_str("    if (group.checks) {\n");
1504        script.push_str("      for (let checkObj of group.checks) {\n");
1505        script.push_str("        checkResults[checkObj.name] = {\n");
1506        script.push_str("          passes: checkObj.passes,\n");
1507        script.push_str("          fails: checkObj.fails,\n");
1508        script.push_str("        };\n");
1509        script.push_str("      }\n");
1510        script.push_str("    }\n");
1511        script.push_str("    if (group.groups) {\n");
1512        script.push_str("      for (let subGroup of group.groups) {\n");
1513        script.push_str("        walkGroups(subGroup);\n");
1514        script.push_str("      }\n");
1515        script.push_str("    }\n");
1516        script.push_str("  }\n");
1517        script.push_str("  if (data.root_group) {\n");
1518        script.push_str("    walkGroups(data.root_group);\n");
1519        script.push_str("  }\n");
1520        script.push_str("  return {\n");
1521        script.push_str(&format!(
1522            "    '{}': JSON.stringify({{ checks: checkResults, overall: checks }}, null, 2),\n",
1523            report_path
1524        ));
1525        script.push_str("    'summary.json': JSON.stringify(data),\n");
1526        script.push_str("    stdout: textSummary(data, { indent: '  ', enableColors: true }),\n");
1527        script.push_str("  };\n");
1528        script.push_str("}\n\n");
1529        script.push_str("function textSummary(data, opts) {\n");
1530        script.push_str("  return JSON.stringify(data, null, 2);\n");
1531        script.push_str("}\n");
1532    }
1533}
1534
1535#[cfg(test)]
1536mod tests {
1537    use super::*;
1538    use openapiv3::{
1539        Operation, ParameterData, ParameterSchemaOrContent, PathStyle, Response, Schema,
1540        SchemaData, SchemaKind, StringType, Type,
1541    };
1542
1543    fn make_op(method: &str, path: &str, operation: Operation) -> ApiOperation {
1544        ApiOperation {
1545            method: method.to_string(),
1546            path: path.to_string(),
1547            operation,
1548            operation_id: None,
1549        }
1550    }
1551
1552    fn empty_spec() -> OpenAPI {
1553        OpenAPI::default()
1554    }
1555
1556    #[test]
1557    fn test_annotate_get_with_path_param() {
1558        let mut op = Operation::default();
1559        op.parameters.push(ReferenceOr::Item(Parameter::Path {
1560            parameter_data: ParameterData {
1561                name: "id".to_string(),
1562                description: None,
1563                required: true,
1564                deprecated: None,
1565                format: ParameterSchemaOrContent::Schema(ReferenceOr::Item(Schema {
1566                    schema_data: SchemaData::default(),
1567                    schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1568                })),
1569                example: None,
1570                examples: Default::default(),
1571                explode: None,
1572                extensions: Default::default(),
1573            },
1574            style: PathStyle::Simple,
1575        }));
1576
1577        let api_op = make_op("get", "/users/{id}", op);
1578        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1579
1580        assert!(annotated.features.contains(&ConformanceFeature::MethodGet));
1581        assert!(annotated.features.contains(&ConformanceFeature::PathParamString));
1582        assert!(annotated.features.contains(&ConformanceFeature::ConstraintRequired));
1583        assert_eq!(annotated.path_params.len(), 1);
1584        assert_eq!(annotated.path_params[0].0, "id");
1585    }
1586
1587    #[test]
1588    fn test_annotate_post_with_json_body() {
1589        let mut op = Operation::default();
1590        let mut body = RequestBody {
1591            required: true,
1592            ..Default::default()
1593        };
1594        body.content
1595            .insert("application/json".to_string(), openapiv3::MediaType::default());
1596        op.request_body = Some(ReferenceOr::Item(body));
1597
1598        let api_op = make_op("post", "/items", op);
1599        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1600
1601        assert!(annotated.features.contains(&ConformanceFeature::MethodPost));
1602        assert!(annotated.features.contains(&ConformanceFeature::BodyJson));
1603    }
1604
1605    #[test]
1606    fn test_annotate_response_codes() {
1607        let mut op = Operation::default();
1608        op.responses
1609            .responses
1610            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(Response::default()));
1611        op.responses
1612            .responses
1613            .insert(openapiv3::StatusCode::Code(404), ReferenceOr::Item(Response::default()));
1614
1615        let api_op = make_op("get", "/items", op);
1616        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1617
1618        assert!(annotated.features.contains(&ConformanceFeature::Response200));
1619        assert!(annotated.features.contains(&ConformanceFeature::Response404));
1620    }
1621
1622    #[test]
1623    fn test_generate_spec_driven_script() {
1624        let config = ConformanceConfig {
1625            target_url: "http://localhost:3000".to_string(),
1626            api_key: None,
1627            basic_auth: None,
1628            skip_tls_verify: false,
1629            categories: None,
1630            base_path: None,
1631            custom_headers: vec![],
1632            output_dir: None,
1633            all_operations: false,
1634            custom_checks_file: None,
1635            request_delay_ms: 0,
1636            custom_filter: None,
1637            export_requests: false,
1638            validate_requests: false,
1639        };
1640
1641        let operations = vec![AnnotatedOperation {
1642            path: "/users/{id}".to_string(),
1643            method: "GET".to_string(),
1644            features: vec![
1645                ConformanceFeature::MethodGet,
1646                ConformanceFeature::PathParamString,
1647            ],
1648            request_body_content_type: None,
1649            sample_body: None,
1650            query_params: vec![],
1651            header_params: vec![],
1652            path_params: vec![("id".to_string(), "test-value".to_string())],
1653            response_schema: None,
1654            response_schemas: std::collections::BTreeMap::new(),
1655            request_body_schema: None,
1656            security_schemes: vec![],
1657        }];
1658
1659        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1660        let (script, _check_count) = gen.generate().unwrap();
1661
1662        assert!(script.contains("import http from 'k6/http'"));
1663        assert!(script.contains("/users/test-value"));
1664        assert!(script.contains("param:path:string"));
1665        assert!(script.contains("method:GET"));
1666        assert!(script.contains("handleSummary"));
1667    }
1668
1669    #[test]
1670    fn test_generate_with_category_filter() {
1671        let config = ConformanceConfig {
1672            target_url: "http://localhost:3000".to_string(),
1673            api_key: None,
1674            basic_auth: None,
1675            skip_tls_verify: false,
1676            categories: Some(vec!["Parameters".to_string()]),
1677            base_path: None,
1678            custom_headers: vec![],
1679            output_dir: None,
1680            all_operations: false,
1681            custom_checks_file: None,
1682            request_delay_ms: 0,
1683            custom_filter: None,
1684            export_requests: false,
1685            validate_requests: false,
1686        };
1687
1688        let operations = vec![AnnotatedOperation {
1689            path: "/users/{id}".to_string(),
1690            method: "GET".to_string(),
1691            features: vec![
1692                ConformanceFeature::MethodGet,
1693                ConformanceFeature::PathParamString,
1694            ],
1695            request_body_content_type: None,
1696            sample_body: None,
1697            query_params: vec![],
1698            header_params: vec![],
1699            path_params: vec![("id".to_string(), "1".to_string())],
1700            response_schema: None,
1701            response_schemas: std::collections::BTreeMap::new(),
1702            request_body_schema: None,
1703            security_schemes: vec![],
1704        }];
1705
1706        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1707        let (script, _check_count) = gen.generate().unwrap();
1708
1709        assert!(script.contains("group('Parameters'"));
1710        assert!(!script.contains("group('HTTP Methods'"));
1711    }
1712
1713    #[test]
1714    fn test_annotate_response_validation() {
1715        use openapiv3::ObjectType;
1716
1717        // Operation with a 200 response that has a JSON schema
1718        let mut op = Operation::default();
1719        let mut response = Response::default();
1720        let mut media = openapiv3::MediaType::default();
1721        let mut obj_type = ObjectType::default();
1722        obj_type.properties.insert(
1723            "name".to_string(),
1724            ReferenceOr::Item(Box::new(Schema {
1725                schema_data: SchemaData::default(),
1726                schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1727            })),
1728        );
1729        obj_type.required = vec!["name".to_string()];
1730        media.schema = Some(ReferenceOr::Item(Schema {
1731            schema_data: SchemaData::default(),
1732            schema_kind: SchemaKind::Type(Type::Object(obj_type)),
1733        }));
1734        response.content.insert("application/json".to_string(), media);
1735        op.responses
1736            .responses
1737            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1738
1739        let api_op = make_op("get", "/users", op);
1740        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1741
1742        assert!(
1743            annotated.features.contains(&ConformanceFeature::ResponseValidation),
1744            "Should detect ResponseValidation when response has a JSON schema"
1745        );
1746        assert!(annotated.response_schema.is_some(), "Should extract the response schema");
1747
1748        // Verify generated script includes schema validation with try-catch
1749        let config = ConformanceConfig {
1750            target_url: "http://localhost:3000".to_string(),
1751            api_key: None,
1752            basic_auth: None,
1753            skip_tls_verify: false,
1754            categories: None,
1755            base_path: None,
1756            custom_headers: vec![],
1757            output_dir: None,
1758            all_operations: false,
1759            custom_checks_file: None,
1760            request_delay_ms: 0,
1761            custom_filter: None,
1762            export_requests: false,
1763            validate_requests: false,
1764        };
1765        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1766        let (script, _check_count) = gen.generate().unwrap();
1767
1768        assert!(
1769            script.contains("response:schema:validation"),
1770            "Script should contain the validation check name"
1771        );
1772        assert!(script.contains("try {"), "Script should wrap validation in try-catch");
1773        assert!(script.contains("res.json()"), "Script should parse response as JSON");
1774    }
1775
1776    #[test]
1777    fn test_annotate_global_security() {
1778        // Spec with global security requirement, operation without its own security
1779        let op = Operation::default();
1780        let mut spec = OpenAPI::default();
1781        let mut global_req = openapiv3::SecurityRequirement::new();
1782        global_req.insert("bearerAuth".to_string(), vec![]);
1783        spec.security = Some(vec![global_req]);
1784        // Define the security scheme in components
1785        let mut components = openapiv3::Components::default();
1786        components.security_schemes.insert(
1787            "bearerAuth".to_string(),
1788            ReferenceOr::Item(SecurityScheme::HTTP {
1789                scheme: "bearer".to_string(),
1790                bearer_format: Some("JWT".to_string()),
1791                description: None,
1792                extensions: Default::default(),
1793            }),
1794        );
1795        spec.components = Some(components);
1796
1797        let api_op = make_op("get", "/protected", op);
1798        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1799
1800        assert!(
1801            annotated.features.contains(&ConformanceFeature::SecurityBearer),
1802            "Should detect SecurityBearer from global security + components"
1803        );
1804    }
1805
1806    #[test]
1807    fn test_annotate_security_scheme_resolution() {
1808        // Test that security scheme type is resolved from components, not just name heuristic
1809        let mut op = Operation::default();
1810        // Use a generic name that wouldn't match name heuristics
1811        let mut req = openapiv3::SecurityRequirement::new();
1812        req.insert("myAuth".to_string(), vec![]);
1813        op.security = Some(vec![req]);
1814
1815        let mut spec = OpenAPI::default();
1816        let mut components = openapiv3::Components::default();
1817        components.security_schemes.insert(
1818            "myAuth".to_string(),
1819            ReferenceOr::Item(SecurityScheme::APIKey {
1820                location: openapiv3::APIKeyLocation::Header,
1821                name: "X-API-Key".to_string(),
1822                description: None,
1823                extensions: Default::default(),
1824            }),
1825        );
1826        spec.components = Some(components);
1827
1828        let api_op = make_op("get", "/data", op);
1829        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1830
1831        assert!(
1832            annotated.features.contains(&ConformanceFeature::SecurityApiKey),
1833            "Should detect SecurityApiKey from SecurityScheme::APIKey, not name heuristic"
1834        );
1835    }
1836
1837    #[test]
1838    fn test_annotate_content_negotiation() {
1839        let mut op = Operation::default();
1840        let mut response = Response::default();
1841        // Response with multiple content types
1842        response
1843            .content
1844            .insert("application/json".to_string(), openapiv3::MediaType::default());
1845        response
1846            .content
1847            .insert("application/xml".to_string(), openapiv3::MediaType::default());
1848        op.responses
1849            .responses
1850            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1851
1852        let api_op = make_op("get", "/items", op);
1853        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1854
1855        assert!(
1856            annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1857            "Should detect ContentNegotiation when response has multiple content types"
1858        );
1859    }
1860
1861    #[test]
1862    fn test_no_content_negotiation_for_single_type() {
1863        let mut op = Operation::default();
1864        let mut response = Response::default();
1865        response
1866            .content
1867            .insert("application/json".to_string(), openapiv3::MediaType::default());
1868        op.responses
1869            .responses
1870            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1871
1872        let api_op = make_op("get", "/items", op);
1873        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1874
1875        assert!(
1876            !annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1877            "Should NOT detect ContentNegotiation for a single content type"
1878        );
1879    }
1880
1881    #[test]
1882    fn test_spec_driven_with_base_path() {
1883        let annotated = AnnotatedOperation {
1884            path: "/users".to_string(),
1885            method: "GET".to_string(),
1886            features: vec![ConformanceFeature::MethodGet],
1887            path_params: vec![],
1888            query_params: vec![],
1889            header_params: vec![],
1890            request_body_content_type: None,
1891            sample_body: None,
1892            response_schema: None,
1893            response_schemas: std::collections::BTreeMap::new(),
1894            request_body_schema: None,
1895            security_schemes: vec![],
1896        };
1897        let config = ConformanceConfig {
1898            target_url: "https://192.168.2.86/".to_string(),
1899            api_key: None,
1900            basic_auth: None,
1901            skip_tls_verify: true,
1902            categories: None,
1903            base_path: Some("/api".to_string()),
1904            custom_headers: vec![],
1905            output_dir: None,
1906            all_operations: false,
1907            custom_checks_file: None,
1908            request_delay_ms: 0,
1909            custom_filter: None,
1910            export_requests: false,
1911            validate_requests: false,
1912        };
1913        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1914        let (script, _check_count) = gen.generate().unwrap();
1915
1916        assert!(
1917            script.contains("const BASE_URL = 'https://192.168.2.86/api'"),
1918            "BASE_URL should include the base_path. Got: {}",
1919            script.lines().find(|l| l.contains("BASE_URL")).unwrap_or("not found")
1920        );
1921    }
1922
1923    #[test]
1924    fn test_spec_driven_with_custom_headers() {
1925        let annotated = AnnotatedOperation {
1926            path: "/users".to_string(),
1927            method: "GET".to_string(),
1928            features: vec![ConformanceFeature::MethodGet],
1929            path_params: vec![],
1930            query_params: vec![],
1931            header_params: vec![
1932                ("X-Avi-Tenant".to_string(), "test-value".to_string()),
1933                ("X-CSRFToken".to_string(), "test-value".to_string()),
1934            ],
1935            request_body_content_type: None,
1936            sample_body: None,
1937            response_schema: None,
1938            response_schemas: std::collections::BTreeMap::new(),
1939            request_body_schema: None,
1940            security_schemes: vec![],
1941        };
1942        let config = ConformanceConfig {
1943            target_url: "https://192.168.2.86/".to_string(),
1944            api_key: None,
1945            basic_auth: None,
1946            skip_tls_verify: true,
1947            categories: None,
1948            base_path: Some("/api".to_string()),
1949            custom_headers: vec![
1950                ("X-Avi-Tenant".to_string(), "admin".to_string()),
1951                ("X-CSRFToken".to_string(), "real-csrf-token".to_string()),
1952                ("Cookie".to_string(), "sessionid=abc123".to_string()),
1953            ],
1954            output_dir: None,
1955            all_operations: false,
1956            custom_checks_file: None,
1957            request_delay_ms: 0,
1958            custom_filter: None,
1959            export_requests: false,
1960            validate_requests: false,
1961        };
1962        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1963        let (script, _check_count) = gen.generate().unwrap();
1964
1965        // Custom headers should override spec-derived test-value placeholders
1966        assert!(
1967            script.contains("'X-Avi-Tenant': 'admin'"),
1968            "Should use custom value for X-Avi-Tenant, not test-value"
1969        );
1970        assert!(
1971            script.contains("'X-CSRFToken': 'real-csrf-token'"),
1972            "Should use custom value for X-CSRFToken, not test-value"
1973        );
1974        // Custom headers not in spec should be appended
1975        assert!(
1976            script.contains("'Cookie': 'sessionid=abc123'"),
1977            "Should include Cookie header from custom_headers"
1978        );
1979        // test-value should NOT appear
1980        assert!(
1981            !script.contains("'test-value'"),
1982            "test-value placeholders should be replaced by custom values"
1983        );
1984    }
1985
1986    #[test]
1987    fn test_effective_headers_merging() {
1988        let config = ConformanceConfig {
1989            target_url: "http://localhost".to_string(),
1990            api_key: None,
1991            basic_auth: None,
1992            skip_tls_verify: false,
1993            categories: None,
1994            base_path: None,
1995            custom_headers: vec![
1996                ("X-Auth".to_string(), "real-token".to_string()),
1997                ("Cookie".to_string(), "session=abc".to_string()),
1998            ],
1999            output_dir: None,
2000            all_operations: false,
2001            custom_checks_file: None,
2002            request_delay_ms: 0,
2003            custom_filter: None,
2004            export_requests: false,
2005            validate_requests: false,
2006        };
2007        let gen = SpecDrivenConformanceGenerator::new(config, vec![]);
2008
2009        // Spec headers with a matching custom header
2010        let spec_headers = vec![
2011            ("X-Auth".to_string(), "test-value".to_string()),
2012            ("X-Other".to_string(), "keep-this".to_string()),
2013        ];
2014        let effective = gen.effective_headers(&spec_headers);
2015
2016        // X-Auth should be overridden
2017        assert_eq!(effective[0], ("X-Auth".to_string(), "real-token".to_string()));
2018        // X-Other should be kept as-is
2019        assert_eq!(effective[1], ("X-Other".to_string(), "keep-this".to_string()));
2020        // Cookie should be appended
2021        assert_eq!(effective[2], ("Cookie".to_string(), "session=abc".to_string()));
2022    }
2023
2024    // Round 51 (#79) — baseline param filler must be spec-VALID so a probe that
2025    // isn't attacking a given param doesn't trip a spurious violation on it.
2026    #[test]
2027    fn param_sample_value_respects_enum_boolean_and_type() {
2028        use openapiv3::{BooleanType, IntegerType, NumberType, VariantOrUnknownOrEmpty};
2029
2030        // Enum string (like Apigee's `alt`: json|media|proto) -> first member.
2031        let st = StringType {
2032            enumeration: vec![Some("json".into()), Some("media".into())],
2033            ..Default::default()
2034        };
2035        let enum_schema = Schema {
2036            schema_data: SchemaData::default(),
2037            schema_kind: SchemaKind::Type(Type::String(st)),
2038        };
2039        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&enum_schema), "json");
2040
2041        // Boolean (like `prettyPrint`) -> "true", not "test-value".
2042        let bool_schema = Schema {
2043            schema_data: SchemaData::default(),
2044            schema_kind: SchemaKind::Type(Type::Boolean(BooleanType::default())),
2045        };
2046        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&bool_schema), "true");
2047
2048        // Integer -> a number.
2049        let int_schema = Schema {
2050            schema_data: SchemaData::default(),
2051            schema_kind: SchemaKind::Type(Type::Integer(IntegerType::default())),
2052        };
2053        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&int_schema), "42");
2054
2055        // Number -> a number.
2056        let num_schema = Schema {
2057            schema_data: SchemaData::default(),
2058            schema_kind: SchemaKind::Type(Type::Number(NumberType::default())),
2059        };
2060        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&num_schema), "42");
2061
2062        // Plain string with no constraints -> the generic filler.
2063        let plain = Schema {
2064            schema_data: SchemaData::default(),
2065            schema_kind: SchemaKind::Type(Type::String(StringType::default())),
2066        };
2067        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&plain), "test-value");
2068
2069        // String with a known format -> a value valid for that format.
2070        let email = StringType {
2071            format: VariantOrUnknownOrEmpty::Unknown("email".into()),
2072            ..Default::default()
2073        };
2074        let email_schema = Schema {
2075            schema_data: SchemaData::default(),
2076            schema_kind: SchemaKind::Type(Type::String(email)),
2077        };
2078        assert_eq!(
2079            SpecDrivenConformanceGenerator::schema_sample_value(&email_schema),
2080            "user@example.com"
2081        );
2082    }
2083}