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\x20// Round 63 (#79) — target-side HTTP protocol violation (e.g. more bytes than the declared Content-Length). Checked last so it only re-labels what would be 'other'.\n\
971                 \x20\x20\x20\x20\x20\x20else if (em.toLowerCase().indexOf('declared content-length') !== -1 || em.toLowerCase().indexOf('malformed') !== -1 || em.toLowerCase().indexOf('protocol error') !== -1 || em.toLowerCase().indexOf('invalid header') !== -1) kind = 'protocol';\n\
972                 \x20\x20\x20\x20\x20\x20console.log('MOCKFORGE_NETWORK_EVENT:' + JSON.stringify({\n\
973                 \x20\x20\x20\x20\x20\x20  timestamp: new Date().toISOString(),\n\
974                 \x20\x20\x20\x20\x20\x20  check: checkName,\n\
975                 \x20\x20\x20\x20\x20\x20  method: res.request ? res.request.method : 'unknown',\n\
976                 \x20\x20\x20\x20\x20\x20  url: res.request ? res.request.url : res.url || 'unknown',\n\
977                 \x20\x20\x20\x20\x20\x20  kind: kind,\n\
978                 \x20\x20\x20\x20\x20\x20  error_code: ec,\n\
979                 \x20\x20\x20\x20\x20\x20  message: em,\n\
980                 \x20\x20\x20\x20\x20\x20}));\n\
981                 \x20\x20\x20\x20}\n",
982            );
983            script.push_str("    console.log('MOCKFORGE_EXCHANGE:' + JSON.stringify({\n");
984            script.push_str("      check: checkName,\n");
985            script.push_str("      request: {\n");
986            script.push_str("        method: res.request ? res.request.method : 'unknown',\n");
987            script.push_str("        url: res.request ? res.request.url : res.url || 'unknown',\n");
988            script.push_str("        headers: reqHeaders,\n");
989            script.push_str("        body: reqBody,\n");
990            script.push_str("      },\n");
991            script.push_str("      response: {\n");
992            script.push_str("        status: res.status,\n");
993            script.push_str("        headers: res.headers ? Object.fromEntries(Object.entries(res.headers).slice(0, 30)) : {},\n");
994            script.push_str("        body: bodyStr,\n");
995            script.push_str("      },\n");
996            script.push_str("    }));\n");
997            script.push_str("  } catch (e) {\n");
998            script.push_str("    try {\n");
999            script.push_str("      console.log('MOCKFORGE_EXCHANGE:' + JSON.stringify({\n");
1000            script.push_str("        check: checkName,\n");
1001            script.push_str("        request: {\n");
1002            script.push_str(
1003                "          method: (res && res.request) ? res.request.method : 'unknown',\n",
1004            );
1005            script.push_str("          url: (res && res.request) ? res.request.url : (res && res.url) || 'unknown',\n");
1006            script.push_str("          headers: {},\n");
1007            script.push_str("          body: '<exchange capture failed: ' + (e && e.message ? e.message : 'unknown error') + '>',\n");
1008            script.push_str("        },\n");
1009            script.push_str("        response: {\n");
1010            script.push_str("          status: (res && res.status) || 0,\n");
1011            script.push_str("          headers: {},\n");
1012            script.push_str("          body: '',\n");
1013            script.push_str("        },\n");
1014            script.push_str("        _export_error: (e && e.message) ? e.message : String(e),\n");
1015            script.push_str("      }));\n");
1016            script.push_str("    } catch (e2) {\n");
1017            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");
1018            script.push_str("    }\n");
1019            script.push_str("  }\n");
1020            script.push_str("}\n\n");
1021        }
1022
1023        // Default function
1024        script.push_str("export default function () {\n");
1025
1026        if self.config.has_cookie_header() {
1027            script.push_str(
1028                "  // Clear cookie jar to prevent server Set-Cookie from duplicating custom Cookie header\n",
1029            );
1030            script.push_str("  http.cookieJar().clear(BASE_URL);\n\n");
1031        }
1032
1033        // Group operations by category
1034        let mut category_ops: std::collections::BTreeMap<
1035            &'static str,
1036            Vec<(&AnnotatedOperation, &ConformanceFeature)>,
1037        > = std::collections::BTreeMap::new();
1038
1039        for op in &self.operations {
1040            for feature in &op.features {
1041                let category = feature.category();
1042                if self.config.should_include_category(category) {
1043                    category_ops.entry(category).or_default().push((op, feature));
1044                }
1045            }
1046        }
1047
1048        // Emit grouped tests
1049        let mut total_checks = 0usize;
1050        for (category, ops) in &category_ops {
1051            script.push_str(&format!("  group('{}', function () {{\n", category));
1052
1053            if self.config.all_operations {
1054                // All-operations mode: test every operation, using path-qualified check names
1055                let mut emitted_checks: HashSet<String> = HashSet::new();
1056                for (op, feature) in ops {
1057                    let qualified = format!("{}:{}", feature.check_name(), op.path);
1058                    if emitted_checks.insert(qualified.clone()) {
1059                        self.emit_check_named(&mut script, op, feature, &qualified);
1060                        total_checks += 1;
1061                    }
1062                }
1063            } else {
1064                // Default: one representative operation per feature, with path-qualified
1065                // check names so failures identify which endpoint was tested.
1066                let mut emitted_features: HashSet<&str> = HashSet::new();
1067                for (op, feature) in ops {
1068                    if emitted_features.insert(feature.check_name()) {
1069                        let qualified = format!("{}:{}", feature.check_name(), op.path);
1070                        self.emit_check_named(&mut script, op, feature, &qualified);
1071                        total_checks += 1;
1072                    }
1073                }
1074            }
1075
1076            script.push_str("  });\n\n");
1077        }
1078
1079        // Custom checks from YAML file — round 39: init-scope code
1080        // was already emitted above; here we just splice the group
1081        // body inside the default function.
1082        if let Some(emit) = custom_emit {
1083            script.push_str(&emit.group_body);
1084        }
1085
1086        script.push_str("}\n\n");
1087
1088        // handleSummary
1089        self.generate_handle_summary(&mut script);
1090
1091        Ok((script, total_checks))
1092    }
1093
1094    /// Emit a single k6 check for an operation + feature with a custom check name
1095    fn emit_check_named(
1096        &self,
1097        script: &mut String,
1098        op: &AnnotatedOperation,
1099        feature: &ConformanceFeature,
1100        check_name: &str,
1101    ) {
1102        // Escape single quotes in check name since it's embedded in JS single-quoted strings
1103        let check_name = check_name.replace('\'', "\\'");
1104        let check_name = check_name.as_str();
1105
1106        script.push_str("    {\n");
1107
1108        // Build the URL path with parameters substituted
1109        let mut url_path = op.path.clone();
1110        for (name, value) in &op.path_params {
1111            url_path = url_path.replace(&format!("{{{}}}", name), value);
1112        }
1113
1114        // Build query string
1115        if !op.query_params.is_empty() {
1116            let qs: Vec<String> =
1117                op.query_params.iter().map(|(k, v)| format!("{}={}", k, v)).collect();
1118            url_path = format!("{}?{}", url_path, qs.join("&"));
1119        }
1120
1121        let full_url = format!("${{BASE_URL}}{}", url_path);
1122
1123        // Build effective headers: merge spec-derived headers with custom headers.
1124        // Custom headers override spec-derived ones with the same name.
1125        let mut effective_headers = self.effective_headers(&op.header_params);
1126
1127        // For non-default response code checks, add header to tell the mock server
1128        // which status code to return (the server defaults to the first declared status)
1129        if matches!(feature, ConformanceFeature::Response400 | ConformanceFeature::Response404) {
1130            let expected_code = match feature {
1131                ConformanceFeature::Response400 => "400",
1132                ConformanceFeature::Response404 => "404",
1133                _ => unreachable!(),
1134            };
1135            effective_headers
1136                .push(("X-Mockforge-Response-Status".to_string(), expected_code.to_string()));
1137        }
1138
1139        // For security checks AND for all requests on endpoints with security requirements,
1140        // inject auth credentials so the server doesn't reject with 401.
1141        // Only inject if the user hasn't already provided the header via --custom-headers.
1142        let needs_auth = matches!(
1143            feature,
1144            ConformanceFeature::SecurityBearer
1145                | ConformanceFeature::SecurityBasic
1146                | ConformanceFeature::SecurityApiKey
1147        ) || !op.security_schemes.is_empty();
1148
1149        if needs_auth {
1150            self.inject_security_headers(&op.security_schemes, &mut effective_headers);
1151        }
1152
1153        let has_headers = !effective_headers.is_empty();
1154        let headers_obj = if has_headers {
1155            Self::format_headers(&effective_headers)
1156        } else {
1157            String::new()
1158        };
1159
1160        // Determine HTTP method and emit request
1161        match op.method.as_str() {
1162            "GET" => {
1163                if has_headers {
1164                    script.push_str(&format!(
1165                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
1166                        full_url, headers_obj
1167                    ));
1168                } else {
1169                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
1170                }
1171            }
1172            "POST" => {
1173                self.emit_request_with_body(script, "post", &full_url, op, &effective_headers);
1174            }
1175            "PUT" => {
1176                self.emit_request_with_body(script, "put", &full_url, op, &effective_headers);
1177            }
1178            "PATCH" => {
1179                self.emit_request_with_body(script, "patch", &full_url, op, &effective_headers);
1180            }
1181            "DELETE" => {
1182                if has_headers {
1183                    script.push_str(&format!(
1184                        "      let res = http.del(`{}`, null, {{ headers: {} }});\n",
1185                        full_url, headers_obj
1186                    ));
1187                } else {
1188                    script.push_str(&format!("      let res = http.del(`{}`);\n", full_url));
1189                }
1190            }
1191            "HEAD" => {
1192                if has_headers {
1193                    script.push_str(&format!(
1194                        "      let res = http.head(`{}`, {{ headers: {} }});\n",
1195                        full_url, headers_obj
1196                    ));
1197                } else {
1198                    script.push_str(&format!("      let res = http.head(`{}`);\n", full_url));
1199                }
1200            }
1201            "OPTIONS" => {
1202                if has_headers {
1203                    script.push_str(&format!(
1204                        "      let res = http.options(`{}`, null, {{ headers: {} }});\n",
1205                        full_url, headers_obj
1206                    ));
1207                } else {
1208                    script.push_str(&format!("      let res = http.options(`{}`);\n", full_url));
1209                }
1210            }
1211            _ => {
1212                if has_headers {
1213                    script.push_str(&format!(
1214                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
1215                        full_url, headers_obj
1216                    ));
1217                } else {
1218                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
1219                }
1220            }
1221        }
1222
1223        // Capture request/response for --export-requests
1224        // (check_name is already escaped at line 829 above — don't double-escape)
1225        if self.config.export_requests {
1226            script.push_str(&format!(
1227                "      if (typeof __captureExchange === 'function') __captureExchange('{}', res);\n",
1228                check_name
1229            ));
1230        }
1231
1232        // Check: emit assertion based on feature type, with failure detail capture
1233        if matches!(
1234            feature,
1235            ConformanceFeature::Response200
1236                | ConformanceFeature::Response201
1237                | ConformanceFeature::Response204
1238                | ConformanceFeature::Response400
1239                | ConformanceFeature::Response404
1240        ) {
1241            let expected_code = match feature {
1242                ConformanceFeature::Response200 => 200,
1243                ConformanceFeature::Response201 => 201,
1244                ConformanceFeature::Response204 => 204,
1245                ConformanceFeature::Response400 => 400,
1246                ConformanceFeature::Response404 => 404,
1247                _ => 200,
1248            };
1249            script.push_str(&format!(
1250                "      {{ let ok = check(res, {{ '{}': (r) => r.status === {} }}); if (!ok) __captureFailure('{}', res, 'status === {}'); }}\n",
1251                check_name, expected_code, check_name, expected_code
1252            ));
1253        } else if matches!(feature, ConformanceFeature::ResponseValidation) {
1254            // Response schema validation — validate the response body against the schema
1255            // Uses inline field-level error collection so failure details include
1256            // which specific fields violated the schema (field_path, violation_type,
1257            // expected, actual) — matching the Ajv `errors` array structure.
1258            if let Some(schema) = &op.response_schema {
1259                let validation_js = SchemaValidatorGenerator::generate_validation(schema);
1260                let schema_json = serde_json::to_string(schema).unwrap_or_default();
1261                // Escape backticks and backslashes for JS template literal safety
1262                let schema_json_escaped = schema_json.replace('\\', "\\\\").replace('`', "\\`");
1263                script.push_str(&format!(
1264                    concat!(
1265                        "      try {{\n",
1266                        "        let body = res.json();\n",
1267                        "        let ok = check(res, {{ '{check}': (r) => ( {validation} ) }});\n",
1268                        "        if (!ok) {{\n",
1269                        "          let __violations = [];\n",
1270                        "          try {{\n",
1271                        "            let __schema = JSON.parse(`{schema}`);\n",
1272                        "            function __collectErrors(schema, data, path) {{\n",
1273                        "              if (!schema || typeof schema !== 'object') return;\n",
1274                        "              let st = schema.type || (schema.schema_kind && schema.schema_kind.Type && Object.keys(schema.schema_kind.Type)[0]);\n",
1275                        "              if (st) {{ st = st.toLowerCase(); }}\n",
1276                        "              if (st === 'object') {{\n",
1277                        "                if (typeof data !== 'object' || data === null) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'object', actual: typeof data }}); return; }}\n",
1278                        "                let props = schema.properties || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Object && schema.schema_kind.Type.Object.properties) || {{}};\n",
1279                        "                let req = schema.required || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Object && schema.schema_kind.Type.Object.required) || [];\n",
1280                        "                for (let f of req) {{ if (!(f in data)) {{ __violations.push({{ field_path: path + '/' + f, violation_type: 'required', expected: 'present', actual: 'missing' }}); }} }}\n",
1281                        "                for (let [k, v] of Object.entries(props)) {{ if (data[k] !== undefined) {{ let ps = v.Item || v; __collectErrors(ps, data[k], path + '/' + k); }} }}\n",
1282                        "              }} else if (st === 'array') {{\n",
1283                        "                if (!Array.isArray(data)) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'array', actual: typeof data }}); return; }}\n",
1284                        "                let items = schema.items || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Array && schema.schema_kind.Type.Array.items);\n",
1285                        "                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",
1286                        "              }} else if (st === 'string') {{\n",
1287                        "                if (typeof data !== 'string') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'string', actual: typeof data }}); }}\n",
1288                        "              }} else if (st === 'integer') {{\n",
1289                        "                if (typeof data !== 'number' || !Number.isInteger(data)) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'integer', actual: typeof data }}); }}\n",
1290                        "              }} else if (st === 'number') {{\n",
1291                        "                if (typeof data !== 'number') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'number', actual: typeof data }}); }}\n",
1292                        "              }} else if (st === 'boolean') {{\n",
1293                        "                if (typeof data !== 'boolean') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'boolean', actual: typeof data }}); }}\n",
1294                        "              }}\n",
1295                        "            }}\n",
1296                        "            __collectErrors(__schema, body, '');\n",
1297                        "          }} catch(_e) {{}}\n",
1298                        "          __captureFailure('{check}', res, 'schema validation', __violations);\n",
1299                        "        }}\n",
1300                        "      }} catch(e) {{ check(res, {{ '{check}': () => false }}); __captureFailure('{check}', res, 'JSON parse failed: ' + e.message); }}\n",
1301                    ),
1302                    check = check_name,
1303                    validation = validation_js,
1304                    schema = schema_json_escaped,
1305                ));
1306            }
1307        } else if matches!(
1308            feature,
1309            ConformanceFeature::SecurityBearer
1310                | ConformanceFeature::SecurityBasic
1311                | ConformanceFeature::SecurityApiKey
1312        ) {
1313            // Security checks verify the server accepts the auth credentials (not 401/403)
1314            script.push_str(&format!(
1315                "      {{ let ok = check(res, {{ '{}': (r) => r.status >= 200 && r.status < 400 }}); if (!ok) __captureFailure('{}', res, 'status >= 200 && status < 400 (auth accepted)'); }}\n",
1316                check_name, check_name
1317            ));
1318        } else {
1319            script.push_str(&format!(
1320                "      {{ let ok = check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }}); if (!ok) __captureFailure('{}', res, 'status >= 200 && status < 500'); }}\n",
1321                check_name, check_name
1322            ));
1323        }
1324
1325        // Clear cookie jar after each request to prevent Set-Cookie leaking
1326        let has_cookie = self.config.has_cookie_header()
1327            || effective_headers.iter().any(|(h, _)| h.eq_ignore_ascii_case("Cookie"));
1328        if has_cookie {
1329            script.push_str("      http.cookieJar().clear(BASE_URL);\n");
1330        }
1331
1332        script.push_str("    }\n");
1333
1334        // Rate-limit delay between checks (to avoid 429 from target API)
1335        if self.config.request_delay_ms > 0 {
1336            script.push_str(&format!(
1337                "    sleep({:.3});\n",
1338                self.config.request_delay_ms as f64 / 1000.0
1339            ));
1340        }
1341    }
1342
1343    /// Emit an HTTP request with a body
1344    fn emit_request_with_body(
1345        &self,
1346        script: &mut String,
1347        method: &str,
1348        url: &str,
1349        op: &AnnotatedOperation,
1350        effective_headers: &[(String, String)],
1351    ) {
1352        if let Some(body) = &op.sample_body {
1353            let escaped_body = body.replace('\'', "\\'");
1354            let headers = if !effective_headers.is_empty() {
1355                format!(
1356                    "Object.assign({{}}, JSON_HEADERS, {})",
1357                    Self::format_headers(effective_headers)
1358                )
1359            } else {
1360                "JSON_HEADERS".to_string()
1361            };
1362            script.push_str(&format!(
1363                "      let res = http.{}(`{}`, '{}', {{ headers: {} }});\n",
1364                method, url, escaped_body, headers
1365            ));
1366        } else if !effective_headers.is_empty() {
1367            script.push_str(&format!(
1368                "      let res = http.{}(`{}`, null, {{ headers: {} }});\n",
1369                method,
1370                url,
1371                Self::format_headers(effective_headers)
1372            ));
1373        } else {
1374            script.push_str(&format!("      let res = http.{}(`{}`, null);\n", method, url));
1375        }
1376    }
1377
1378    /// Build effective headers by merging spec-derived headers with custom headers.
1379    /// Custom headers override spec-derived ones with the same name (case-insensitive).
1380    /// Custom headers not in the spec are appended.
1381    fn effective_headers(&self, spec_headers: &[(String, String)]) -> Vec<(String, String)> {
1382        let custom = &self.config.custom_headers;
1383        if custom.is_empty() {
1384            return spec_headers.to_vec();
1385        }
1386
1387        let mut result: Vec<(String, String)> = Vec::new();
1388
1389        // Start with spec headers, replacing values if a custom header matches
1390        for (name, value) in spec_headers {
1391            if let Some((_, custom_val)) =
1392                custom.iter().find(|(cn, _)| cn.eq_ignore_ascii_case(name))
1393            {
1394                result.push((name.clone(), custom_val.clone()));
1395            } else {
1396                result.push((name.clone(), value.clone()));
1397            }
1398        }
1399
1400        // Append custom headers that aren't already in spec headers
1401        for (name, value) in custom {
1402            if !spec_headers.iter().any(|(sn, _)| sn.eq_ignore_ascii_case(name)) {
1403                result.push((name.clone(), value.clone()));
1404            }
1405        }
1406
1407        result
1408    }
1409
1410    /// Inject security headers based on resolved security scheme details.
1411    /// Respects user-provided custom headers (won't overwrite if already set).
1412    fn inject_security_headers(
1413        &self,
1414        schemes: &[SecuritySchemeInfo],
1415        headers: &mut Vec<(String, String)>,
1416    ) {
1417        let mut to_add: Vec<(String, String)> = Vec::new();
1418
1419        let has_header = |name: &str, headers: &[(String, String)]| {
1420            headers.iter().any(|(h, _)| h.eq_ignore_ascii_case(name))
1421                || self.config.custom_headers.iter().any(|(h, _)| h.eq_ignore_ascii_case(name))
1422        };
1423
1424        // If user provides Cookie header, they're using session-based auth — skip auto auth
1425        let has_cookie_auth = has_header("Cookie", headers);
1426
1427        for scheme in schemes {
1428            match scheme {
1429                SecuritySchemeInfo::Bearer => {
1430                    if !has_cookie_auth && !has_header("Authorization", headers) {
1431                        // MockForge mock server accepts any Bearer token
1432                        to_add.push((
1433                            "Authorization".to_string(),
1434                            "Bearer mockforge-conformance-test-token".to_string(),
1435                        ));
1436                    }
1437                }
1438                SecuritySchemeInfo::Basic => {
1439                    if !has_cookie_auth && !has_header("Authorization", headers) {
1440                        let creds = self.config.basic_auth.as_deref().unwrap_or("test:test");
1441                        use base64::Engine;
1442                        let encoded =
1443                            base64::engine::general_purpose::STANDARD.encode(creds.as_bytes());
1444                        to_add.push(("Authorization".to_string(), format!("Basic {}", encoded)));
1445                    }
1446                }
1447                SecuritySchemeInfo::ApiKey { location, name } => match location {
1448                    ApiKeyLocation::Header => {
1449                        if !has_header(name, headers) {
1450                            let key = self
1451                                .config
1452                                .api_key
1453                                .as_deref()
1454                                .unwrap_or("mockforge-conformance-test-key");
1455                            to_add.push((name.clone(), key.to_string()));
1456                        }
1457                    }
1458                    ApiKeyLocation::Cookie => {
1459                        if !has_header("Cookie", headers) {
1460                            to_add.push((
1461                                "Cookie".to_string(),
1462                                format!("{}=mockforge-conformance-test-session", name),
1463                            ));
1464                        }
1465                    }
1466                    ApiKeyLocation::Query => {
1467                        // Query params are handled via URL, not headers — skip here
1468                    }
1469                },
1470            }
1471        }
1472
1473        headers.extend(to_add);
1474    }
1475
1476    /// Format header params as a JS object literal
1477    fn format_headers(headers: &[(String, String)]) -> String {
1478        let entries: Vec<String> = headers
1479            .iter()
1480            .map(|(k, v)| format!("'{}': '{}'", k, v.replace('\'', "\\'")))
1481            .collect();
1482        format!("{{ {} }}", entries.join(", "))
1483    }
1484
1485    /// handleSummary — same format as reference mode for report compatibility
1486    fn generate_handle_summary(&self, script: &mut String) {
1487        // Use absolute path for report output so k6 writes where the CLI expects
1488        let report_path = match &self.config.output_dir {
1489            Some(dir) => {
1490                let abs = std::fs::canonicalize(dir)
1491                    .unwrap_or_else(|_| dir.clone())
1492                    .join("conformance-report.json");
1493                abs.to_string_lossy().to_string()
1494            }
1495            None => "conformance-report.json".to_string(),
1496        };
1497
1498        script.push_str("export function handleSummary(data) {\n");
1499        script.push_str("  let checks = {};\n");
1500        script.push_str("  if (data.metrics && data.metrics.checks) {\n");
1501        script.push_str("    checks.overall_pass_rate = data.metrics.checks.values.rate;\n");
1502        script.push_str("  }\n");
1503        script.push_str("  let checkResults = {};\n");
1504        script.push_str("  function walkGroups(group) {\n");
1505        script.push_str("    if (group.checks) {\n");
1506        script.push_str("      for (let checkObj of group.checks) {\n");
1507        script.push_str("        checkResults[checkObj.name] = {\n");
1508        script.push_str("          passes: checkObj.passes,\n");
1509        script.push_str("          fails: checkObj.fails,\n");
1510        script.push_str("        };\n");
1511        script.push_str("      }\n");
1512        script.push_str("    }\n");
1513        script.push_str("    if (group.groups) {\n");
1514        script.push_str("      for (let subGroup of group.groups) {\n");
1515        script.push_str("        walkGroups(subGroup);\n");
1516        script.push_str("      }\n");
1517        script.push_str("    }\n");
1518        script.push_str("  }\n");
1519        script.push_str("  if (data.root_group) {\n");
1520        script.push_str("    walkGroups(data.root_group);\n");
1521        script.push_str("  }\n");
1522        script.push_str("  return {\n");
1523        script.push_str(&format!(
1524            "    '{}': JSON.stringify({{ checks: checkResults, overall: checks }}, null, 2),\n",
1525            report_path
1526        ));
1527        script.push_str("    'summary.json': JSON.stringify(data),\n");
1528        script.push_str("    stdout: textSummary(data, { indent: '  ', enableColors: true }),\n");
1529        script.push_str("  };\n");
1530        script.push_str("}\n\n");
1531        script.push_str("function textSummary(data, opts) {\n");
1532        script.push_str("  return JSON.stringify(data, null, 2);\n");
1533        script.push_str("}\n");
1534    }
1535}
1536
1537#[cfg(test)]
1538mod tests {
1539    use super::*;
1540    use openapiv3::{
1541        Operation, ParameterData, ParameterSchemaOrContent, PathStyle, Response, Schema,
1542        SchemaData, SchemaKind, StringType, Type,
1543    };
1544
1545    fn make_op(method: &str, path: &str, operation: Operation) -> ApiOperation {
1546        ApiOperation {
1547            method: method.to_string(),
1548            path: path.to_string(),
1549            operation,
1550            operation_id: None,
1551        }
1552    }
1553
1554    fn empty_spec() -> OpenAPI {
1555        OpenAPI::default()
1556    }
1557
1558    #[test]
1559    fn test_annotate_get_with_path_param() {
1560        let mut op = Operation::default();
1561        op.parameters.push(ReferenceOr::Item(Parameter::Path {
1562            parameter_data: ParameterData {
1563                name: "id".to_string(),
1564                description: None,
1565                required: true,
1566                deprecated: None,
1567                format: ParameterSchemaOrContent::Schema(ReferenceOr::Item(Schema {
1568                    schema_data: SchemaData::default(),
1569                    schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1570                })),
1571                example: None,
1572                examples: Default::default(),
1573                explode: None,
1574                extensions: Default::default(),
1575            },
1576            style: PathStyle::Simple,
1577        }));
1578
1579        let api_op = make_op("get", "/users/{id}", op);
1580        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1581
1582        assert!(annotated.features.contains(&ConformanceFeature::MethodGet));
1583        assert!(annotated.features.contains(&ConformanceFeature::PathParamString));
1584        assert!(annotated.features.contains(&ConformanceFeature::ConstraintRequired));
1585        assert_eq!(annotated.path_params.len(), 1);
1586        assert_eq!(annotated.path_params[0].0, "id");
1587    }
1588
1589    #[test]
1590    fn test_annotate_post_with_json_body() {
1591        let mut op = Operation::default();
1592        let mut body = RequestBody {
1593            required: true,
1594            ..Default::default()
1595        };
1596        body.content
1597            .insert("application/json".to_string(), openapiv3::MediaType::default());
1598        op.request_body = Some(ReferenceOr::Item(body));
1599
1600        let api_op = make_op("post", "/items", op);
1601        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1602
1603        assert!(annotated.features.contains(&ConformanceFeature::MethodPost));
1604        assert!(annotated.features.contains(&ConformanceFeature::BodyJson));
1605    }
1606
1607    #[test]
1608    fn test_annotate_response_codes() {
1609        let mut op = Operation::default();
1610        op.responses
1611            .responses
1612            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(Response::default()));
1613        op.responses
1614            .responses
1615            .insert(openapiv3::StatusCode::Code(404), ReferenceOr::Item(Response::default()));
1616
1617        let api_op = make_op("get", "/items", op);
1618        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1619
1620        assert!(annotated.features.contains(&ConformanceFeature::Response200));
1621        assert!(annotated.features.contains(&ConformanceFeature::Response404));
1622    }
1623
1624    #[test]
1625    fn test_generate_spec_driven_script() {
1626        let config = ConformanceConfig {
1627            target_url: "http://localhost:3000".to_string(),
1628            api_key: None,
1629            basic_auth: None,
1630            skip_tls_verify: false,
1631            categories: None,
1632            base_path: None,
1633            custom_headers: vec![],
1634            output_dir: None,
1635            all_operations: false,
1636            custom_checks_file: None,
1637            request_delay_ms: 0,
1638            custom_filter: None,
1639            export_requests: false,
1640            validate_requests: false,
1641        };
1642
1643        let operations = vec![AnnotatedOperation {
1644            path: "/users/{id}".to_string(),
1645            method: "GET".to_string(),
1646            features: vec![
1647                ConformanceFeature::MethodGet,
1648                ConformanceFeature::PathParamString,
1649            ],
1650            request_body_content_type: None,
1651            sample_body: None,
1652            query_params: vec![],
1653            header_params: vec![],
1654            path_params: vec![("id".to_string(), "test-value".to_string())],
1655            response_schema: None,
1656            response_schemas: std::collections::BTreeMap::new(),
1657            request_body_schema: None,
1658            security_schemes: vec![],
1659        }];
1660
1661        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1662        let (script, _check_count) = gen.generate().unwrap();
1663
1664        assert!(script.contains("import http from 'k6/http'"));
1665        assert!(script.contains("/users/test-value"));
1666        assert!(script.contains("param:path:string"));
1667        assert!(script.contains("method:GET"));
1668        assert!(script.contains("handleSummary"));
1669    }
1670
1671    #[test]
1672    fn test_generate_with_category_filter() {
1673        let config = ConformanceConfig {
1674            target_url: "http://localhost:3000".to_string(),
1675            api_key: None,
1676            basic_auth: None,
1677            skip_tls_verify: false,
1678            categories: Some(vec!["Parameters".to_string()]),
1679            base_path: None,
1680            custom_headers: vec![],
1681            output_dir: None,
1682            all_operations: false,
1683            custom_checks_file: None,
1684            request_delay_ms: 0,
1685            custom_filter: None,
1686            export_requests: false,
1687            validate_requests: false,
1688        };
1689
1690        let operations = vec![AnnotatedOperation {
1691            path: "/users/{id}".to_string(),
1692            method: "GET".to_string(),
1693            features: vec![
1694                ConformanceFeature::MethodGet,
1695                ConformanceFeature::PathParamString,
1696            ],
1697            request_body_content_type: None,
1698            sample_body: None,
1699            query_params: vec![],
1700            header_params: vec![],
1701            path_params: vec![("id".to_string(), "1".to_string())],
1702            response_schema: None,
1703            response_schemas: std::collections::BTreeMap::new(),
1704            request_body_schema: None,
1705            security_schemes: vec![],
1706        }];
1707
1708        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1709        let (script, _check_count) = gen.generate().unwrap();
1710
1711        assert!(script.contains("group('Parameters'"));
1712        assert!(!script.contains("group('HTTP Methods'"));
1713    }
1714
1715    #[test]
1716    fn test_annotate_response_validation() {
1717        use openapiv3::ObjectType;
1718
1719        // Operation with a 200 response that has a JSON schema
1720        let mut op = Operation::default();
1721        let mut response = Response::default();
1722        let mut media = openapiv3::MediaType::default();
1723        let mut obj_type = ObjectType::default();
1724        obj_type.properties.insert(
1725            "name".to_string(),
1726            ReferenceOr::Item(Box::new(Schema {
1727                schema_data: SchemaData::default(),
1728                schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1729            })),
1730        );
1731        obj_type.required = vec!["name".to_string()];
1732        media.schema = Some(ReferenceOr::Item(Schema {
1733            schema_data: SchemaData::default(),
1734            schema_kind: SchemaKind::Type(Type::Object(obj_type)),
1735        }));
1736        response.content.insert("application/json".to_string(), media);
1737        op.responses
1738            .responses
1739            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1740
1741        let api_op = make_op("get", "/users", op);
1742        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1743
1744        assert!(
1745            annotated.features.contains(&ConformanceFeature::ResponseValidation),
1746            "Should detect ResponseValidation when response has a JSON schema"
1747        );
1748        assert!(annotated.response_schema.is_some(), "Should extract the response schema");
1749
1750        // Verify generated script includes schema validation with try-catch
1751        let config = ConformanceConfig {
1752            target_url: "http://localhost:3000".to_string(),
1753            api_key: None,
1754            basic_auth: None,
1755            skip_tls_verify: false,
1756            categories: None,
1757            base_path: None,
1758            custom_headers: vec![],
1759            output_dir: None,
1760            all_operations: false,
1761            custom_checks_file: None,
1762            request_delay_ms: 0,
1763            custom_filter: None,
1764            export_requests: false,
1765            validate_requests: false,
1766        };
1767        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1768        let (script, _check_count) = gen.generate().unwrap();
1769
1770        assert!(
1771            script.contains("response:schema:validation"),
1772            "Script should contain the validation check name"
1773        );
1774        assert!(script.contains("try {"), "Script should wrap validation in try-catch");
1775        assert!(script.contains("res.json()"), "Script should parse response as JSON");
1776    }
1777
1778    #[test]
1779    fn test_annotate_global_security() {
1780        // Spec with global security requirement, operation without its own security
1781        let op = Operation::default();
1782        let mut spec = OpenAPI::default();
1783        let mut global_req = openapiv3::SecurityRequirement::new();
1784        global_req.insert("bearerAuth".to_string(), vec![]);
1785        spec.security = Some(vec![global_req]);
1786        // Define the security scheme in components
1787        let mut components = openapiv3::Components::default();
1788        components.security_schemes.insert(
1789            "bearerAuth".to_string(),
1790            ReferenceOr::Item(SecurityScheme::HTTP {
1791                scheme: "bearer".to_string(),
1792                bearer_format: Some("JWT".to_string()),
1793                description: None,
1794                extensions: Default::default(),
1795            }),
1796        );
1797        spec.components = Some(components);
1798
1799        let api_op = make_op("get", "/protected", op);
1800        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1801
1802        assert!(
1803            annotated.features.contains(&ConformanceFeature::SecurityBearer),
1804            "Should detect SecurityBearer from global security + components"
1805        );
1806    }
1807
1808    #[test]
1809    fn test_annotate_security_scheme_resolution() {
1810        // Test that security scheme type is resolved from components, not just name heuristic
1811        let mut op = Operation::default();
1812        // Use a generic name that wouldn't match name heuristics
1813        let mut req = openapiv3::SecurityRequirement::new();
1814        req.insert("myAuth".to_string(), vec![]);
1815        op.security = Some(vec![req]);
1816
1817        let mut spec = OpenAPI::default();
1818        let mut components = openapiv3::Components::default();
1819        components.security_schemes.insert(
1820            "myAuth".to_string(),
1821            ReferenceOr::Item(SecurityScheme::APIKey {
1822                location: openapiv3::APIKeyLocation::Header,
1823                name: "X-API-Key".to_string(),
1824                description: None,
1825                extensions: Default::default(),
1826            }),
1827        );
1828        spec.components = Some(components);
1829
1830        let api_op = make_op("get", "/data", op);
1831        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1832
1833        assert!(
1834            annotated.features.contains(&ConformanceFeature::SecurityApiKey),
1835            "Should detect SecurityApiKey from SecurityScheme::APIKey, not name heuristic"
1836        );
1837    }
1838
1839    #[test]
1840    fn test_annotate_content_negotiation() {
1841        let mut op = Operation::default();
1842        let mut response = Response::default();
1843        // Response with multiple content types
1844        response
1845            .content
1846            .insert("application/json".to_string(), openapiv3::MediaType::default());
1847        response
1848            .content
1849            .insert("application/xml".to_string(), openapiv3::MediaType::default());
1850        op.responses
1851            .responses
1852            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1853
1854        let api_op = make_op("get", "/items", op);
1855        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1856
1857        assert!(
1858            annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1859            "Should detect ContentNegotiation when response has multiple content types"
1860        );
1861    }
1862
1863    #[test]
1864    fn test_no_content_negotiation_for_single_type() {
1865        let mut op = Operation::default();
1866        let mut response = Response::default();
1867        response
1868            .content
1869            .insert("application/json".to_string(), openapiv3::MediaType::default());
1870        op.responses
1871            .responses
1872            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1873
1874        let api_op = make_op("get", "/items", op);
1875        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1876
1877        assert!(
1878            !annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1879            "Should NOT detect ContentNegotiation for a single content type"
1880        );
1881    }
1882
1883    #[test]
1884    fn test_spec_driven_with_base_path() {
1885        let annotated = AnnotatedOperation {
1886            path: "/users".to_string(),
1887            method: "GET".to_string(),
1888            features: vec![ConformanceFeature::MethodGet],
1889            path_params: vec![],
1890            query_params: vec![],
1891            header_params: vec![],
1892            request_body_content_type: None,
1893            sample_body: None,
1894            response_schema: None,
1895            response_schemas: std::collections::BTreeMap::new(),
1896            request_body_schema: None,
1897            security_schemes: vec![],
1898        };
1899        let config = ConformanceConfig {
1900            target_url: "https://192.168.2.86/".to_string(),
1901            api_key: None,
1902            basic_auth: None,
1903            skip_tls_verify: true,
1904            categories: None,
1905            base_path: Some("/api".to_string()),
1906            custom_headers: vec![],
1907            output_dir: None,
1908            all_operations: false,
1909            custom_checks_file: None,
1910            request_delay_ms: 0,
1911            custom_filter: None,
1912            export_requests: false,
1913            validate_requests: false,
1914        };
1915        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1916        let (script, _check_count) = gen.generate().unwrap();
1917
1918        assert!(
1919            script.contains("const BASE_URL = 'https://192.168.2.86/api'"),
1920            "BASE_URL should include the base_path. Got: {}",
1921            script.lines().find(|l| l.contains("BASE_URL")).unwrap_or("not found")
1922        );
1923    }
1924
1925    #[test]
1926    fn test_spec_driven_with_custom_headers() {
1927        let annotated = AnnotatedOperation {
1928            path: "/users".to_string(),
1929            method: "GET".to_string(),
1930            features: vec![ConformanceFeature::MethodGet],
1931            path_params: vec![],
1932            query_params: vec![],
1933            header_params: vec![
1934                ("X-Avi-Tenant".to_string(), "test-value".to_string()),
1935                ("X-CSRFToken".to_string(), "test-value".to_string()),
1936            ],
1937            request_body_content_type: None,
1938            sample_body: None,
1939            response_schema: None,
1940            response_schemas: std::collections::BTreeMap::new(),
1941            request_body_schema: None,
1942            security_schemes: vec![],
1943        };
1944        let config = ConformanceConfig {
1945            target_url: "https://192.168.2.86/".to_string(),
1946            api_key: None,
1947            basic_auth: None,
1948            skip_tls_verify: true,
1949            categories: None,
1950            base_path: Some("/api".to_string()),
1951            custom_headers: vec![
1952                ("X-Avi-Tenant".to_string(), "admin".to_string()),
1953                ("X-CSRFToken".to_string(), "real-csrf-token".to_string()),
1954                ("Cookie".to_string(), "sessionid=abc123".to_string()),
1955            ],
1956            output_dir: None,
1957            all_operations: false,
1958            custom_checks_file: None,
1959            request_delay_ms: 0,
1960            custom_filter: None,
1961            export_requests: false,
1962            validate_requests: false,
1963        };
1964        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1965        let (script, _check_count) = gen.generate().unwrap();
1966
1967        // Custom headers should override spec-derived test-value placeholders
1968        assert!(
1969            script.contains("'X-Avi-Tenant': 'admin'"),
1970            "Should use custom value for X-Avi-Tenant, not test-value"
1971        );
1972        assert!(
1973            script.contains("'X-CSRFToken': 'real-csrf-token'"),
1974            "Should use custom value for X-CSRFToken, not test-value"
1975        );
1976        // Custom headers not in spec should be appended
1977        assert!(
1978            script.contains("'Cookie': 'sessionid=abc123'"),
1979            "Should include Cookie header from custom_headers"
1980        );
1981        // test-value should NOT appear
1982        assert!(
1983            !script.contains("'test-value'"),
1984            "test-value placeholders should be replaced by custom values"
1985        );
1986    }
1987
1988    #[test]
1989    fn test_effective_headers_merging() {
1990        let config = ConformanceConfig {
1991            target_url: "http://localhost".to_string(),
1992            api_key: None,
1993            basic_auth: None,
1994            skip_tls_verify: false,
1995            categories: None,
1996            base_path: None,
1997            custom_headers: vec![
1998                ("X-Auth".to_string(), "real-token".to_string()),
1999                ("Cookie".to_string(), "session=abc".to_string()),
2000            ],
2001            output_dir: None,
2002            all_operations: false,
2003            custom_checks_file: None,
2004            request_delay_ms: 0,
2005            custom_filter: None,
2006            export_requests: false,
2007            validate_requests: false,
2008        };
2009        let gen = SpecDrivenConformanceGenerator::new(config, vec![]);
2010
2011        // Spec headers with a matching custom header
2012        let spec_headers = vec![
2013            ("X-Auth".to_string(), "test-value".to_string()),
2014            ("X-Other".to_string(), "keep-this".to_string()),
2015        ];
2016        let effective = gen.effective_headers(&spec_headers);
2017
2018        // X-Auth should be overridden
2019        assert_eq!(effective[0], ("X-Auth".to_string(), "real-token".to_string()));
2020        // X-Other should be kept as-is
2021        assert_eq!(effective[1], ("X-Other".to_string(), "keep-this".to_string()));
2022        // Cookie should be appended
2023        assert_eq!(effective[2], ("Cookie".to_string(), "session=abc".to_string()));
2024    }
2025
2026    // Round 51 (#79) — baseline param filler must be spec-VALID so a probe that
2027    // isn't attacking a given param doesn't trip a spurious violation on it.
2028    #[test]
2029    fn param_sample_value_respects_enum_boolean_and_type() {
2030        use openapiv3::{BooleanType, IntegerType, NumberType, VariantOrUnknownOrEmpty};
2031
2032        // Enum string (like Apigee's `alt`: json|media|proto) -> first member.
2033        let st = StringType {
2034            enumeration: vec![Some("json".into()), Some("media".into())],
2035            ..Default::default()
2036        };
2037        let enum_schema = Schema {
2038            schema_data: SchemaData::default(),
2039            schema_kind: SchemaKind::Type(Type::String(st)),
2040        };
2041        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&enum_schema), "json");
2042
2043        // Boolean (like `prettyPrint`) -> "true", not "test-value".
2044        let bool_schema = Schema {
2045            schema_data: SchemaData::default(),
2046            schema_kind: SchemaKind::Type(Type::Boolean(BooleanType::default())),
2047        };
2048        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&bool_schema), "true");
2049
2050        // Integer -> a number.
2051        let int_schema = Schema {
2052            schema_data: SchemaData::default(),
2053            schema_kind: SchemaKind::Type(Type::Integer(IntegerType::default())),
2054        };
2055        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&int_schema), "42");
2056
2057        // Number -> a number.
2058        let num_schema = Schema {
2059            schema_data: SchemaData::default(),
2060            schema_kind: SchemaKind::Type(Type::Number(NumberType::default())),
2061        };
2062        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&num_schema), "42");
2063
2064        // Plain string with no constraints -> the generic filler.
2065        let plain = Schema {
2066            schema_data: SchemaData::default(),
2067            schema_kind: SchemaKind::Type(Type::String(StringType::default())),
2068        };
2069        assert_eq!(SpecDrivenConformanceGenerator::schema_sample_value(&plain), "test-value");
2070
2071        // String with a known format -> a value valid for that format.
2072        let email = StringType {
2073            format: VariantOrUnknownOrEmpty::Unknown("email".into()),
2074            ..Default::default()
2075        };
2076        let email_schema = Schema {
2077            schema_data: SchemaData::default(),
2078            schema_kind: SchemaKind::Type(Type::String(email)),
2079        };
2080        assert_eq!(
2081            SpecDrivenConformanceGenerator::schema_sample_value(&email_schema),
2082            "user@example.com"
2083        );
2084    }
2085}