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/// An API operation annotated with the conformance features it exercises
145#[derive(Debug, Clone)]
146pub struct AnnotatedOperation {
147    pub path: String,
148    pub method: String,
149    pub features: Vec<ConformanceFeature>,
150    pub request_body_content_type: Option<String>,
151    pub sample_body: Option<String>,
152    pub query_params: Vec<(String, String)>,
153    pub header_params: Vec<(String, String)>,
154    pub path_params: Vec<(String, String)>,
155    /// Response schema for validation (JSON string of the schema)
156    pub response_schema: Option<Schema>,
157}
158
159/// Generates spec-driven conformance k6 scripts
160pub struct SpecDrivenConformanceGenerator {
161    config: ConformanceConfig,
162    operations: Vec<AnnotatedOperation>,
163}
164
165impl SpecDrivenConformanceGenerator {
166    pub fn new(config: ConformanceConfig, operations: Vec<AnnotatedOperation>) -> Self {
167        Self { config, operations }
168    }
169
170    /// Annotate a list of API operations with conformance features
171    pub fn annotate_operations(
172        operations: &[ApiOperation],
173        spec: &OpenAPI,
174    ) -> Vec<AnnotatedOperation> {
175        operations.iter().map(|op| Self::annotate_operation(op, spec)).collect()
176    }
177
178    /// Analyze an operation and determine which conformance features it exercises
179    fn annotate_operation(op: &ApiOperation, spec: &OpenAPI) -> AnnotatedOperation {
180        let mut features = Vec::new();
181        let mut query_params = Vec::new();
182        let mut header_params = Vec::new();
183        let mut path_params = Vec::new();
184
185        // Detect HTTP method feature
186        match op.method.to_uppercase().as_str() {
187            "GET" => features.push(ConformanceFeature::MethodGet),
188            "POST" => features.push(ConformanceFeature::MethodPost),
189            "PUT" => features.push(ConformanceFeature::MethodPut),
190            "PATCH" => features.push(ConformanceFeature::MethodPatch),
191            "DELETE" => features.push(ConformanceFeature::MethodDelete),
192            "HEAD" => features.push(ConformanceFeature::MethodHead),
193            "OPTIONS" => features.push(ConformanceFeature::MethodOptions),
194            _ => {}
195        }
196
197        // Detect parameter features (resolves $ref)
198        for param_ref in &op.operation.parameters {
199            if let Some(param) = ref_resolver::resolve_parameter(param_ref, spec) {
200                Self::annotate_parameter(
201                    param,
202                    spec,
203                    &mut features,
204                    &mut query_params,
205                    &mut header_params,
206                    &mut path_params,
207                );
208            }
209        }
210
211        // Detect path parameters from the path template itself
212        for segment in op.path.split('/') {
213            if segment.starts_with('{') && segment.ends_with('}') {
214                let name = &segment[1..segment.len() - 1];
215                // Only add if not already found from parameters
216                if !path_params.iter().any(|(n, _)| n == name) {
217                    path_params.push((name.to_string(), "test-value".to_string()));
218                    // Determine type from path params we didn't already handle
219                    if !features.contains(&ConformanceFeature::PathParamString)
220                        && !features.contains(&ConformanceFeature::PathParamInteger)
221                    {
222                        features.push(ConformanceFeature::PathParamString);
223                    }
224                }
225            }
226        }
227
228        // Detect request body features (resolves $ref)
229        let mut request_body_content_type = None;
230        let mut sample_body = None;
231
232        let resolved_body = op
233            .operation
234            .request_body
235            .as_ref()
236            .and_then(|b| ref_resolver::resolve_request_body(b, spec));
237
238        if let Some(body) = resolved_body {
239            for (content_type, _media) in &body.content {
240                match content_type.as_str() {
241                    "application/json" => {
242                        features.push(ConformanceFeature::BodyJson);
243                        request_body_content_type = Some("application/json".to_string());
244                        // Generate sample body from schema
245                        if let Ok(template) = RequestGenerator::generate_template(op) {
246                            if let Some(body_val) = &template.body {
247                                sample_body = Some(body_val.to_string());
248                            }
249                        }
250                    }
251                    "application/x-www-form-urlencoded" => {
252                        features.push(ConformanceFeature::BodyFormUrlencoded);
253                        request_body_content_type =
254                            Some("application/x-www-form-urlencoded".to_string());
255                    }
256                    "multipart/form-data" => {
257                        features.push(ConformanceFeature::BodyMultipart);
258                        request_body_content_type = Some("multipart/form-data".to_string());
259                    }
260                    _ => {}
261                }
262            }
263
264            // Detect schema features in request body (resolves $ref in schema)
265            if let Some(media) = body.content.get("application/json") {
266                if let Some(schema_ref) = &media.schema {
267                    if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
268                        Self::annotate_schema(schema, spec, &mut features);
269                    }
270                }
271            }
272        }
273
274        // Detect response code features
275        Self::annotate_responses(&op.operation, spec, &mut features);
276
277        // Extract response schema for validation (resolves $ref)
278        let response_schema = Self::extract_response_schema(&op.operation, spec);
279        if response_schema.is_some() {
280            features.push(ConformanceFeature::ResponseValidation);
281        }
282
283        // Detect content negotiation (response with multiple content types)
284        Self::annotate_content_negotiation(&op.operation, spec, &mut features);
285
286        // Detect security features
287        Self::annotate_security(&op.operation, spec, &mut features);
288
289        // Deduplicate features
290        features.sort_by_key(|f| f.check_name());
291        features.dedup_by_key(|f| f.check_name());
292
293        AnnotatedOperation {
294            path: op.path.clone(),
295            method: op.method.to_uppercase(),
296            features,
297            request_body_content_type,
298            sample_body,
299            query_params,
300            header_params,
301            path_params,
302            response_schema,
303        }
304    }
305
306    /// Annotate parameter features
307    fn annotate_parameter(
308        param: &Parameter,
309        spec: &OpenAPI,
310        features: &mut Vec<ConformanceFeature>,
311        query_params: &mut Vec<(String, String)>,
312        header_params: &mut Vec<(String, String)>,
313        path_params: &mut Vec<(String, String)>,
314    ) {
315        let (location, data) = match param {
316            Parameter::Query { parameter_data, .. } => ("query", parameter_data),
317            Parameter::Path { parameter_data, .. } => ("path", parameter_data),
318            Parameter::Header { parameter_data, .. } => ("header", parameter_data),
319            Parameter::Cookie { .. } => {
320                features.push(ConformanceFeature::CookieParam);
321                return;
322            }
323        };
324
325        // Detect type from schema
326        let is_integer = Self::param_schema_is_integer(data, spec);
327        let is_array = Self::param_schema_is_array(data, spec);
328
329        // Generate sample value
330        let sample = if is_integer {
331            "42".to_string()
332        } else if is_array {
333            "a,b".to_string()
334        } else {
335            "test-value".to_string()
336        };
337
338        match location {
339            "path" => {
340                if is_integer {
341                    features.push(ConformanceFeature::PathParamInteger);
342                } else {
343                    features.push(ConformanceFeature::PathParamString);
344                }
345                path_params.push((data.name.clone(), sample));
346            }
347            "query" => {
348                if is_array {
349                    features.push(ConformanceFeature::QueryParamArray);
350                } else if is_integer {
351                    features.push(ConformanceFeature::QueryParamInteger);
352                } else {
353                    features.push(ConformanceFeature::QueryParamString);
354                }
355                query_params.push((data.name.clone(), sample));
356            }
357            "header" => {
358                features.push(ConformanceFeature::HeaderParam);
359                header_params.push((data.name.clone(), sample));
360            }
361            _ => {}
362        }
363
364        // Check for constraint features on the parameter (resolves $ref)
365        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
366            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
367                Self::annotate_schema(schema, spec, features);
368            }
369        }
370
371        // Required/optional
372        if data.required {
373            features.push(ConformanceFeature::ConstraintRequired);
374        } else {
375            features.push(ConformanceFeature::ConstraintOptional);
376        }
377    }
378
379    fn param_schema_is_integer(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
380        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
381            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
382                return matches!(&schema.schema_kind, SchemaKind::Type(Type::Integer(_)));
383            }
384        }
385        false
386    }
387
388    fn param_schema_is_array(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
389        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
390            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
391                return matches!(&schema.schema_kind, SchemaKind::Type(Type::Array(_)));
392            }
393        }
394        false
395    }
396
397    /// Annotate schema-level features (types, composition, formats, constraints)
398    fn annotate_schema(schema: &Schema, spec: &OpenAPI, features: &mut Vec<ConformanceFeature>) {
399        match &schema.schema_kind {
400            SchemaKind::Type(Type::String(s)) => {
401                features.push(ConformanceFeature::SchemaString);
402                // Check format
403                match &s.format {
404                    VariantOrUnknownOrEmpty::Item(StringFormat::Date) => {
405                        features.push(ConformanceFeature::FormatDate);
406                    }
407                    VariantOrUnknownOrEmpty::Item(StringFormat::DateTime) => {
408                        features.push(ConformanceFeature::FormatDateTime);
409                    }
410                    VariantOrUnknownOrEmpty::Unknown(fmt) => match fmt.as_str() {
411                        "email" => features.push(ConformanceFeature::FormatEmail),
412                        "uuid" => features.push(ConformanceFeature::FormatUuid),
413                        "uri" | "url" => features.push(ConformanceFeature::FormatUri),
414                        "ipv4" => features.push(ConformanceFeature::FormatIpv4),
415                        "ipv6" => features.push(ConformanceFeature::FormatIpv6),
416                        _ => {}
417                    },
418                    _ => {}
419                }
420                // Check constraints
421                if s.pattern.is_some() {
422                    features.push(ConformanceFeature::ConstraintPattern);
423                }
424                if !s.enumeration.is_empty() {
425                    features.push(ConformanceFeature::ConstraintEnum);
426                }
427                if s.min_length.is_some() || s.max_length.is_some() {
428                    features.push(ConformanceFeature::ConstraintMinMax);
429                }
430            }
431            SchemaKind::Type(Type::Integer(i)) => {
432                features.push(ConformanceFeature::SchemaInteger);
433                if i.minimum.is_some() || i.maximum.is_some() {
434                    features.push(ConformanceFeature::ConstraintMinMax);
435                }
436                if !i.enumeration.is_empty() {
437                    features.push(ConformanceFeature::ConstraintEnum);
438                }
439            }
440            SchemaKind::Type(Type::Number(n)) => {
441                features.push(ConformanceFeature::SchemaNumber);
442                if n.minimum.is_some() || n.maximum.is_some() {
443                    features.push(ConformanceFeature::ConstraintMinMax);
444                }
445            }
446            SchemaKind::Type(Type::Boolean(_)) => {
447                features.push(ConformanceFeature::SchemaBoolean);
448            }
449            SchemaKind::Type(Type::Array(arr)) => {
450                features.push(ConformanceFeature::SchemaArray);
451                if let Some(item_ref) = &arr.items {
452                    if let Some(item_schema) = ref_resolver::resolve_boxed_schema(item_ref, spec) {
453                        Self::annotate_schema(item_schema, spec, features);
454                    }
455                }
456            }
457            SchemaKind::Type(Type::Object(obj)) => {
458                features.push(ConformanceFeature::SchemaObject);
459                // Check required fields
460                if !obj.required.is_empty() {
461                    features.push(ConformanceFeature::ConstraintRequired);
462                }
463                // Walk properties (resolves $ref)
464                for (_name, prop_ref) in &obj.properties {
465                    if let Some(prop_schema) = ref_resolver::resolve_boxed_schema(prop_ref, spec) {
466                        Self::annotate_schema(prop_schema, spec, features);
467                    }
468                }
469            }
470            SchemaKind::OneOf { .. } => {
471                features.push(ConformanceFeature::CompositionOneOf);
472            }
473            SchemaKind::AnyOf { .. } => {
474                features.push(ConformanceFeature::CompositionAnyOf);
475            }
476            SchemaKind::AllOf { .. } => {
477                features.push(ConformanceFeature::CompositionAllOf);
478            }
479            _ => {}
480        }
481    }
482
483    /// Detect response code features (resolves $ref in responses)
484    fn annotate_responses(
485        operation: &Operation,
486        spec: &OpenAPI,
487        features: &mut Vec<ConformanceFeature>,
488    ) {
489        for (status_code, resp_ref) in &operation.responses.responses {
490            // Only count features for responses that actually resolve
491            if ref_resolver::resolve_response(resp_ref, spec).is_some() {
492                match status_code {
493                    openapiv3::StatusCode::Code(200) => {
494                        features.push(ConformanceFeature::Response200)
495                    }
496                    openapiv3::StatusCode::Code(201) => {
497                        features.push(ConformanceFeature::Response201)
498                    }
499                    openapiv3::StatusCode::Code(204) => {
500                        features.push(ConformanceFeature::Response204)
501                    }
502                    openapiv3::StatusCode::Code(400) => {
503                        features.push(ConformanceFeature::Response400)
504                    }
505                    openapiv3::StatusCode::Code(404) => {
506                        features.push(ConformanceFeature::Response404)
507                    }
508                    _ => {}
509                }
510            }
511        }
512    }
513
514    /// Extract the response schema for the primary success response (200 or 201)
515    /// Resolves $ref for both the response and the schema within it.
516    fn extract_response_schema(operation: &Operation, spec: &OpenAPI) -> Option<Schema> {
517        // Try 200 first, then 201
518        for code in [200u16, 201] {
519            if let Some(resp_ref) =
520                operation.responses.responses.get(&openapiv3::StatusCode::Code(code))
521            {
522                if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
523                    if let Some(media) = response.content.get("application/json") {
524                        if let Some(schema_ref) = &media.schema {
525                            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
526                                return Some(schema.clone());
527                            }
528                        }
529                    }
530                }
531            }
532        }
533        None
534    }
535
536    /// Detect content negotiation: response supports multiple content types
537    fn annotate_content_negotiation(
538        operation: &Operation,
539        spec: &OpenAPI,
540        features: &mut Vec<ConformanceFeature>,
541    ) {
542        for (_status_code, resp_ref) in &operation.responses.responses {
543            if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
544                if response.content.len() > 1 {
545                    features.push(ConformanceFeature::ContentNegotiation);
546                    return; // Only need to detect once per operation
547                }
548            }
549        }
550    }
551
552    /// Detect security scheme features.
553    /// Checks operation-level security first, falling back to global security requirements.
554    /// Resolves scheme names against SecurityScheme definitions in components.
555    fn annotate_security(
556        operation: &Operation,
557        spec: &OpenAPI,
558        features: &mut Vec<ConformanceFeature>,
559    ) {
560        // Use operation-level security if present, otherwise fall back to global
561        let security_reqs = operation.security.as_ref().or(spec.security.as_ref());
562
563        if let Some(security) = security_reqs {
564            for security_req in security {
565                for scheme_name in security_req.keys() {
566                    // Try to resolve the scheme from components for accurate type detection
567                    if let Some(resolved) = Self::resolve_security_scheme(scheme_name, spec) {
568                        match resolved {
569                            SecurityScheme::HTTP { ref scheme, .. } => {
570                                if scheme.eq_ignore_ascii_case("bearer") {
571                                    features.push(ConformanceFeature::SecurityBearer);
572                                } else if scheme.eq_ignore_ascii_case("basic") {
573                                    features.push(ConformanceFeature::SecurityBasic);
574                                }
575                            }
576                            SecurityScheme::APIKey { .. } => {
577                                features.push(ConformanceFeature::SecurityApiKey);
578                            }
579                            // OAuth2 and OpenIDConnect don't map to our current feature set
580                            _ => {}
581                        }
582                    } else {
583                        // Fallback: heuristic name matching for unresolvable schemes
584                        let name_lower = scheme_name.to_lowercase();
585                        if name_lower.contains("bearer") || name_lower.contains("jwt") {
586                            features.push(ConformanceFeature::SecurityBearer);
587                        } else if name_lower.contains("api") && name_lower.contains("key") {
588                            features.push(ConformanceFeature::SecurityApiKey);
589                        } else if name_lower.contains("basic") {
590                            features.push(ConformanceFeature::SecurityBasic);
591                        }
592                    }
593                }
594            }
595        }
596    }
597
598    /// Resolve a security scheme name to its SecurityScheme definition
599    fn resolve_security_scheme<'a>(name: &str, spec: &'a OpenAPI) -> Option<&'a SecurityScheme> {
600        let components = spec.components.as_ref()?;
601        match components.security_schemes.get(name)? {
602            ReferenceOr::Item(scheme) => Some(scheme),
603            ReferenceOr::Reference { .. } => None,
604        }
605    }
606
607    /// Returns the number of operations being tested
608    pub fn operation_count(&self) -> usize {
609        self.operations.len()
610    }
611
612    /// Generate the k6 conformance script.
613    /// Returns (script, check_count) where check_count is the number of unique checks emitted.
614    pub fn generate(&self) -> Result<(String, usize)> {
615        let mut script = String::with_capacity(16384);
616
617        // Imports
618        script.push_str("import http from 'k6/http';\n");
619        script.push_str("import { check, group } from 'k6';\n\n");
620
621        // Options
622        script.push_str("export const options = {\n");
623        script.push_str("  vus: 1,\n");
624        script.push_str("  iterations: 1,\n");
625        if self.config.skip_tls_verify {
626            script.push_str("  insecureSkipTLSVerify: true,\n");
627        }
628        script.push_str("  thresholds: {\n");
629        script.push_str("    checks: ['rate>0'],\n");
630        script.push_str("  },\n");
631        script.push_str("};\n\n");
632
633        // Base URL (includes base_path if configured)
634        script.push_str(&format!("const BASE_URL = '{}';\n\n", self.config.effective_base_url()));
635        script.push_str("const JSON_HEADERS = { 'Content-Type': 'application/json' };\n\n");
636
637        // Default function
638        script.push_str("export default function () {\n");
639
640        if self.config.has_cookie_header() {
641            script.push_str(
642                "  // Clear cookie jar to prevent server Set-Cookie from duplicating custom Cookie header\n",
643            );
644            script.push_str("  http.cookieJar().clear(BASE_URL);\n\n");
645        }
646
647        // Group operations by category
648        let mut category_ops: std::collections::BTreeMap<
649            &'static str,
650            Vec<(&AnnotatedOperation, &ConformanceFeature)>,
651        > = std::collections::BTreeMap::new();
652
653        for op in &self.operations {
654            for feature in &op.features {
655                let category = feature.category();
656                if self.config.should_include_category(category) {
657                    category_ops.entry(category).or_default().push((op, feature));
658                }
659            }
660        }
661
662        // Emit grouped tests
663        let mut total_checks = 0usize;
664        for (category, ops) in &category_ops {
665            script.push_str(&format!("  group('{}', function () {{\n", category));
666
667            if self.config.all_operations {
668                // All-operations mode: test every operation, using path-qualified check names
669                let mut emitted_checks: HashSet<String> = HashSet::new();
670                for (op, feature) in ops {
671                    let qualified = format!("{}:{}", feature.check_name(), op.path);
672                    if emitted_checks.insert(qualified.clone()) {
673                        self.emit_check_named(&mut script, op, feature, &qualified);
674                        total_checks += 1;
675                    }
676                }
677            } else {
678                // Default: one representative operation per feature, with path-qualified
679                // check names so failures identify which endpoint was tested.
680                let mut emitted_features: HashSet<&str> = HashSet::new();
681                for (op, feature) in ops {
682                    if emitted_features.insert(feature.check_name()) {
683                        let qualified = format!("{}:{}", feature.check_name(), op.path);
684                        self.emit_check_named(&mut script, op, feature, &qualified);
685                        total_checks += 1;
686                    }
687                }
688            }
689
690            script.push_str("  });\n\n");
691        }
692
693        script.push_str("}\n\n");
694
695        // handleSummary
696        self.generate_handle_summary(&mut script);
697
698        Ok((script, total_checks))
699    }
700
701    /// Emit a single k6 check for an operation + feature with a custom check name
702    fn emit_check_named(
703        &self,
704        script: &mut String,
705        op: &AnnotatedOperation,
706        feature: &ConformanceFeature,
707        check_name: &str,
708    ) {
709        // Escape single quotes in check name since it's embedded in JS single-quoted strings
710        let check_name = check_name.replace('\'', "\\'");
711        let check_name = check_name.as_str();
712
713        script.push_str("    {\n");
714
715        // Build the URL path with parameters substituted
716        let mut url_path = op.path.clone();
717        for (name, value) in &op.path_params {
718            url_path = url_path.replace(&format!("{{{}}}", name), value);
719        }
720
721        // Build query string
722        if !op.query_params.is_empty() {
723            let qs: Vec<String> =
724                op.query_params.iter().map(|(k, v)| format!("{}={}", k, v)).collect();
725            url_path = format!("{}?{}", url_path, qs.join("&"));
726        }
727
728        let full_url = format!("${{BASE_URL}}{}", url_path);
729
730        // Build effective headers: merge spec-derived headers with custom headers.
731        // Custom headers override spec-derived ones with the same name.
732        let mut effective_headers = self.effective_headers(&op.header_params);
733
734        // For non-default response code checks, add header to tell the mock server
735        // which status code to return (the server defaults to the first declared status)
736        if matches!(feature, ConformanceFeature::Response400 | ConformanceFeature::Response404) {
737            let expected_code = match feature {
738                ConformanceFeature::Response400 => "400",
739                ConformanceFeature::Response404 => "404",
740                _ => unreachable!(),
741            };
742            effective_headers
743                .push(("X-Mockforge-Response-Status".to_string(), expected_code.to_string()));
744        }
745
746        let has_headers = !effective_headers.is_empty();
747        let headers_obj = if has_headers {
748            Self::format_headers(&effective_headers)
749        } else {
750            String::new()
751        };
752
753        // Determine HTTP method and emit request
754        match op.method.as_str() {
755            "GET" => {
756                if has_headers {
757                    script.push_str(&format!(
758                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
759                        full_url, headers_obj
760                    ));
761                } else {
762                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
763                }
764            }
765            "POST" => {
766                self.emit_request_with_body(script, "post", &full_url, op, &effective_headers);
767            }
768            "PUT" => {
769                self.emit_request_with_body(script, "put", &full_url, op, &effective_headers);
770            }
771            "PATCH" => {
772                self.emit_request_with_body(script, "patch", &full_url, op, &effective_headers);
773            }
774            "DELETE" => {
775                if has_headers {
776                    script.push_str(&format!(
777                        "      let res = http.del(`{}`, null, {{ headers: {} }});\n",
778                        full_url, headers_obj
779                    ));
780                } else {
781                    script.push_str(&format!("      let res = http.del(`{}`);\n", full_url));
782                }
783            }
784            "HEAD" => {
785                if has_headers {
786                    script.push_str(&format!(
787                        "      let res = http.head(`{}`, {{ headers: {} }});\n",
788                        full_url, headers_obj
789                    ));
790                } else {
791                    script.push_str(&format!("      let res = http.head(`{}`);\n", full_url));
792                }
793            }
794            "OPTIONS" => {
795                if has_headers {
796                    script.push_str(&format!(
797                        "      let res = http.options(`{}`, null, {{ headers: {} }});\n",
798                        full_url, headers_obj
799                    ));
800                } else {
801                    script.push_str(&format!("      let res = http.options(`{}`);\n", full_url));
802                }
803            }
804            _ => {
805                if has_headers {
806                    script.push_str(&format!(
807                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
808                        full_url, headers_obj
809                    ));
810                } else {
811                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
812                }
813            }
814        }
815
816        // Check: emit assertion based on feature type
817        if matches!(
818            feature,
819            ConformanceFeature::Response200
820                | ConformanceFeature::Response201
821                | ConformanceFeature::Response204
822                | ConformanceFeature::Response400
823                | ConformanceFeature::Response404
824        ) {
825            let expected_code = match feature {
826                ConformanceFeature::Response200 => 200,
827                ConformanceFeature::Response201 => 201,
828                ConformanceFeature::Response204 => 204,
829                ConformanceFeature::Response400 => 400,
830                ConformanceFeature::Response404 => 404,
831                _ => 200,
832            };
833            script.push_str(&format!(
834                "      check(res, {{ '{}': (r) => r.status === {} }});\n",
835                check_name, expected_code
836            ));
837        } else if matches!(feature, ConformanceFeature::ResponseValidation) {
838            // Response schema validation — validate the response body against the schema
839            if let Some(schema) = &op.response_schema {
840                let validation_js = SchemaValidatorGenerator::generate_validation(schema);
841                script.push_str(&format!(
842                    "      try {{ let body = res.json(); check(res, {{ '{}': (r) => {{ {} }} }}); }} catch(e) {{ check(res, {{ '{}': () => false }}); }}\n",
843                    check_name, validation_js, check_name
844                ));
845            }
846        } else {
847            script.push_str(&format!(
848                "      check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }});\n",
849                check_name
850            ));
851        }
852
853        // Clear cookie jar after each request to prevent Set-Cookie leaking
854        if self.config.has_cookie_header() {
855            script.push_str("      http.cookieJar().clear(BASE_URL);\n");
856        }
857
858        script.push_str("    }\n");
859    }
860
861    /// Emit an HTTP request with a body
862    fn emit_request_with_body(
863        &self,
864        script: &mut String,
865        method: &str,
866        url: &str,
867        op: &AnnotatedOperation,
868        effective_headers: &[(String, String)],
869    ) {
870        if let Some(body) = &op.sample_body {
871            let escaped_body = body.replace('\'', "\\'");
872            let headers = if !effective_headers.is_empty() {
873                format!(
874                    "Object.assign({{}}, JSON_HEADERS, {})",
875                    Self::format_headers(effective_headers)
876                )
877            } else {
878                "JSON_HEADERS".to_string()
879            };
880            script.push_str(&format!(
881                "      let res = http.{}(`{}`, '{}', {{ headers: {} }});\n",
882                method, url, escaped_body, headers
883            ));
884        } else if !effective_headers.is_empty() {
885            script.push_str(&format!(
886                "      let res = http.{}(`{}`, null, {{ headers: {} }});\n",
887                method,
888                url,
889                Self::format_headers(effective_headers)
890            ));
891        } else {
892            script.push_str(&format!("      let res = http.{}(`{}`, null);\n", method, url));
893        }
894    }
895
896    /// Build effective headers by merging spec-derived headers with custom headers.
897    /// Custom headers override spec-derived ones with the same name (case-insensitive).
898    /// Custom headers not in the spec are appended.
899    fn effective_headers(&self, spec_headers: &[(String, String)]) -> Vec<(String, String)> {
900        let custom = &self.config.custom_headers;
901        if custom.is_empty() {
902            return spec_headers.to_vec();
903        }
904
905        let mut result: Vec<(String, String)> = Vec::new();
906
907        // Start with spec headers, replacing values if a custom header matches
908        for (name, value) in spec_headers {
909            if let Some((_, custom_val)) =
910                custom.iter().find(|(cn, _)| cn.eq_ignore_ascii_case(name))
911            {
912                result.push((name.clone(), custom_val.clone()));
913            } else {
914                result.push((name.clone(), value.clone()));
915            }
916        }
917
918        // Append custom headers that aren't already in spec headers
919        for (name, value) in custom {
920            if !spec_headers.iter().any(|(sn, _)| sn.eq_ignore_ascii_case(name)) {
921                result.push((name.clone(), value.clone()));
922            }
923        }
924
925        result
926    }
927
928    /// Format header params as a JS object literal
929    fn format_headers(headers: &[(String, String)]) -> String {
930        let entries: Vec<String> = headers
931            .iter()
932            .map(|(k, v)| format!("'{}': '{}'", k, v.replace('\'', "\\'")))
933            .collect();
934        format!("{{ {} }}", entries.join(", "))
935    }
936
937    /// handleSummary — same format as reference mode for report compatibility
938    fn generate_handle_summary(&self, script: &mut String) {
939        // Use absolute path for report output so k6 writes where the CLI expects
940        let report_path = match &self.config.output_dir {
941            Some(dir) => {
942                let abs = std::fs::canonicalize(dir)
943                    .unwrap_or_else(|_| dir.clone())
944                    .join("conformance-report.json");
945                abs.to_string_lossy().to_string()
946            }
947            None => "conformance-report.json".to_string(),
948        };
949
950        script.push_str("export function handleSummary(data) {\n");
951        script.push_str("  let checks = {};\n");
952        script.push_str("  if (data.metrics && data.metrics.checks) {\n");
953        script.push_str("    checks.overall_pass_rate = data.metrics.checks.values.rate;\n");
954        script.push_str("  }\n");
955        script.push_str("  let checkResults = {};\n");
956        script.push_str("  function walkGroups(group) {\n");
957        script.push_str("    if (group.checks) {\n");
958        script.push_str("      for (let checkObj of group.checks) {\n");
959        script.push_str("        checkResults[checkObj.name] = {\n");
960        script.push_str("          passes: checkObj.passes,\n");
961        script.push_str("          fails: checkObj.fails,\n");
962        script.push_str("        };\n");
963        script.push_str("      }\n");
964        script.push_str("    }\n");
965        script.push_str("    if (group.groups) {\n");
966        script.push_str("      for (let subGroup of group.groups) {\n");
967        script.push_str("        walkGroups(subGroup);\n");
968        script.push_str("      }\n");
969        script.push_str("    }\n");
970        script.push_str("  }\n");
971        script.push_str("  if (data.root_group) {\n");
972        script.push_str("    walkGroups(data.root_group);\n");
973        script.push_str("  }\n");
974        script.push_str("  return {\n");
975        script.push_str(&format!(
976            "    '{}': JSON.stringify({{ checks: checkResults, overall: checks }}, null, 2),\n",
977            report_path
978        ));
979        script.push_str("    stdout: textSummary(data, { indent: '  ', enableColors: true }),\n");
980        script.push_str("  };\n");
981        script.push_str("}\n\n");
982        script.push_str("function textSummary(data, opts) {\n");
983        script.push_str("  return JSON.stringify(data, null, 2);\n");
984        script.push_str("}\n");
985    }
986}
987
988#[cfg(test)]
989mod tests {
990    use super::*;
991    use openapiv3::{
992        Operation, ParameterData, ParameterSchemaOrContent, PathStyle, Response, Schema,
993        SchemaData, SchemaKind, StringType, Type,
994    };
995
996    fn make_op(method: &str, path: &str, operation: Operation) -> ApiOperation {
997        ApiOperation {
998            method: method.to_string(),
999            path: path.to_string(),
1000            operation,
1001            operation_id: None,
1002        }
1003    }
1004
1005    fn empty_spec() -> OpenAPI {
1006        OpenAPI::default()
1007    }
1008
1009    #[test]
1010    fn test_annotate_get_with_path_param() {
1011        let mut op = Operation::default();
1012        op.parameters.push(ReferenceOr::Item(Parameter::Path {
1013            parameter_data: ParameterData {
1014                name: "id".to_string(),
1015                description: None,
1016                required: true,
1017                deprecated: None,
1018                format: ParameterSchemaOrContent::Schema(ReferenceOr::Item(Schema {
1019                    schema_data: SchemaData::default(),
1020                    schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1021                })),
1022                example: None,
1023                examples: Default::default(),
1024                explode: None,
1025                extensions: Default::default(),
1026            },
1027            style: PathStyle::Simple,
1028        }));
1029
1030        let api_op = make_op("get", "/users/{id}", op);
1031        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1032
1033        assert!(annotated.features.contains(&ConformanceFeature::MethodGet));
1034        assert!(annotated.features.contains(&ConformanceFeature::PathParamString));
1035        assert!(annotated.features.contains(&ConformanceFeature::ConstraintRequired));
1036        assert_eq!(annotated.path_params.len(), 1);
1037        assert_eq!(annotated.path_params[0].0, "id");
1038    }
1039
1040    #[test]
1041    fn test_annotate_post_with_json_body() {
1042        let mut op = Operation::default();
1043        let mut body = openapiv3::RequestBody {
1044            required: true,
1045            ..Default::default()
1046        };
1047        body.content
1048            .insert("application/json".to_string(), openapiv3::MediaType::default());
1049        op.request_body = Some(ReferenceOr::Item(body));
1050
1051        let api_op = make_op("post", "/items", op);
1052        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1053
1054        assert!(annotated.features.contains(&ConformanceFeature::MethodPost));
1055        assert!(annotated.features.contains(&ConformanceFeature::BodyJson));
1056    }
1057
1058    #[test]
1059    fn test_annotate_response_codes() {
1060        let mut op = Operation::default();
1061        op.responses
1062            .responses
1063            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(Response::default()));
1064        op.responses
1065            .responses
1066            .insert(openapiv3::StatusCode::Code(404), ReferenceOr::Item(Response::default()));
1067
1068        let api_op = make_op("get", "/items", op);
1069        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1070
1071        assert!(annotated.features.contains(&ConformanceFeature::Response200));
1072        assert!(annotated.features.contains(&ConformanceFeature::Response404));
1073    }
1074
1075    #[test]
1076    fn test_generate_spec_driven_script() {
1077        let config = ConformanceConfig {
1078            target_url: "http://localhost:3000".to_string(),
1079            api_key: None,
1080            basic_auth: None,
1081            skip_tls_verify: false,
1082            categories: None,
1083            base_path: None,
1084            custom_headers: vec![],
1085            output_dir: None,
1086            all_operations: false,
1087        };
1088
1089        let operations = vec![AnnotatedOperation {
1090            path: "/users/{id}".to_string(),
1091            method: "GET".to_string(),
1092            features: vec![
1093                ConformanceFeature::MethodGet,
1094                ConformanceFeature::PathParamString,
1095            ],
1096            request_body_content_type: None,
1097            sample_body: None,
1098            query_params: vec![],
1099            header_params: vec![],
1100            path_params: vec![("id".to_string(), "test-value".to_string())],
1101            response_schema: None,
1102        }];
1103
1104        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1105        let (script, _check_count) = gen.generate().unwrap();
1106
1107        assert!(script.contains("import http from 'k6/http'"));
1108        assert!(script.contains("/users/test-value"));
1109        assert!(script.contains("param:path:string"));
1110        assert!(script.contains("method:GET"));
1111        assert!(script.contains("handleSummary"));
1112    }
1113
1114    #[test]
1115    fn test_generate_with_category_filter() {
1116        let config = ConformanceConfig {
1117            target_url: "http://localhost:3000".to_string(),
1118            api_key: None,
1119            basic_auth: None,
1120            skip_tls_verify: false,
1121            categories: Some(vec!["Parameters".to_string()]),
1122            base_path: None,
1123            custom_headers: vec![],
1124            output_dir: None,
1125            all_operations: false,
1126        };
1127
1128        let operations = vec![AnnotatedOperation {
1129            path: "/users/{id}".to_string(),
1130            method: "GET".to_string(),
1131            features: vec![
1132                ConformanceFeature::MethodGet,
1133                ConformanceFeature::PathParamString,
1134            ],
1135            request_body_content_type: None,
1136            sample_body: None,
1137            query_params: vec![],
1138            header_params: vec![],
1139            path_params: vec![("id".to_string(), "1".to_string())],
1140            response_schema: None,
1141        }];
1142
1143        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1144        let (script, _check_count) = gen.generate().unwrap();
1145
1146        assert!(script.contains("group('Parameters'"));
1147        assert!(!script.contains("group('HTTP Methods'"));
1148    }
1149
1150    #[test]
1151    fn test_annotate_response_validation() {
1152        use openapiv3::ObjectType;
1153
1154        // Operation with a 200 response that has a JSON schema
1155        let mut op = Operation::default();
1156        let mut response = Response::default();
1157        let mut media = openapiv3::MediaType::default();
1158        let mut obj_type = ObjectType::default();
1159        obj_type.properties.insert(
1160            "name".to_string(),
1161            ReferenceOr::Item(Box::new(Schema {
1162                schema_data: SchemaData::default(),
1163                schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1164            })),
1165        );
1166        obj_type.required = vec!["name".to_string()];
1167        media.schema = Some(ReferenceOr::Item(Schema {
1168            schema_data: SchemaData::default(),
1169            schema_kind: SchemaKind::Type(Type::Object(obj_type)),
1170        }));
1171        response.content.insert("application/json".to_string(), media);
1172        op.responses
1173            .responses
1174            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1175
1176        let api_op = make_op("get", "/users", op);
1177        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1178
1179        assert!(
1180            annotated.features.contains(&ConformanceFeature::ResponseValidation),
1181            "Should detect ResponseValidation when response has a JSON schema"
1182        );
1183        assert!(annotated.response_schema.is_some(), "Should extract the response schema");
1184
1185        // Verify generated script includes schema validation with try-catch
1186        let config = ConformanceConfig {
1187            target_url: "http://localhost:3000".to_string(),
1188            api_key: None,
1189            basic_auth: None,
1190            skip_tls_verify: false,
1191            categories: None,
1192            base_path: None,
1193            custom_headers: vec![],
1194            output_dir: None,
1195            all_operations: false,
1196        };
1197        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1198        let (script, _check_count) = gen.generate().unwrap();
1199
1200        assert!(
1201            script.contains("response:schema:validation"),
1202            "Script should contain the validation check name"
1203        );
1204        assert!(script.contains("try {"), "Script should wrap validation in try-catch");
1205        assert!(script.contains("res.json()"), "Script should parse response as JSON");
1206    }
1207
1208    #[test]
1209    fn test_annotate_global_security() {
1210        // Spec with global security requirement, operation without its own security
1211        let op = Operation::default();
1212        let mut spec = OpenAPI::default();
1213        let mut global_req = openapiv3::SecurityRequirement::new();
1214        global_req.insert("bearerAuth".to_string(), vec![]);
1215        spec.security = Some(vec![global_req]);
1216        // Define the security scheme in components
1217        let mut components = openapiv3::Components::default();
1218        components.security_schemes.insert(
1219            "bearerAuth".to_string(),
1220            ReferenceOr::Item(SecurityScheme::HTTP {
1221                scheme: "bearer".to_string(),
1222                bearer_format: Some("JWT".to_string()),
1223                description: None,
1224                extensions: Default::default(),
1225            }),
1226        );
1227        spec.components = Some(components);
1228
1229        let api_op = make_op("get", "/protected", op);
1230        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1231
1232        assert!(
1233            annotated.features.contains(&ConformanceFeature::SecurityBearer),
1234            "Should detect SecurityBearer from global security + components"
1235        );
1236    }
1237
1238    #[test]
1239    fn test_annotate_security_scheme_resolution() {
1240        // Test that security scheme type is resolved from components, not just name heuristic
1241        let mut op = Operation::default();
1242        // Use a generic name that wouldn't match name heuristics
1243        let mut req = openapiv3::SecurityRequirement::new();
1244        req.insert("myAuth".to_string(), vec![]);
1245        op.security = Some(vec![req]);
1246
1247        let mut spec = OpenAPI::default();
1248        let mut components = openapiv3::Components::default();
1249        components.security_schemes.insert(
1250            "myAuth".to_string(),
1251            ReferenceOr::Item(SecurityScheme::APIKey {
1252                location: openapiv3::APIKeyLocation::Header,
1253                name: "X-API-Key".to_string(),
1254                description: None,
1255                extensions: Default::default(),
1256            }),
1257        );
1258        spec.components = Some(components);
1259
1260        let api_op = make_op("get", "/data", op);
1261        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1262
1263        assert!(
1264            annotated.features.contains(&ConformanceFeature::SecurityApiKey),
1265            "Should detect SecurityApiKey from SecurityScheme::APIKey, not name heuristic"
1266        );
1267    }
1268
1269    #[test]
1270    fn test_annotate_content_negotiation() {
1271        let mut op = Operation::default();
1272        let mut response = Response::default();
1273        // Response with multiple content types
1274        response
1275            .content
1276            .insert("application/json".to_string(), openapiv3::MediaType::default());
1277        response
1278            .content
1279            .insert("application/xml".to_string(), openapiv3::MediaType::default());
1280        op.responses
1281            .responses
1282            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1283
1284        let api_op = make_op("get", "/items", op);
1285        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1286
1287        assert!(
1288            annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1289            "Should detect ContentNegotiation when response has multiple content types"
1290        );
1291    }
1292
1293    #[test]
1294    fn test_no_content_negotiation_for_single_type() {
1295        let mut op = Operation::default();
1296        let mut response = Response::default();
1297        response
1298            .content
1299            .insert("application/json".to_string(), openapiv3::MediaType::default());
1300        op.responses
1301            .responses
1302            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1303
1304        let api_op = make_op("get", "/items", op);
1305        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1306
1307        assert!(
1308            !annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1309            "Should NOT detect ContentNegotiation for a single content type"
1310        );
1311    }
1312
1313    #[test]
1314    fn test_spec_driven_with_base_path() {
1315        let annotated = AnnotatedOperation {
1316            path: "/users".to_string(),
1317            method: "GET".to_string(),
1318            features: vec![ConformanceFeature::MethodGet],
1319            path_params: vec![],
1320            query_params: vec![],
1321            header_params: vec![],
1322            request_body_content_type: None,
1323            sample_body: None,
1324            response_schema: None,
1325        };
1326        let config = ConformanceConfig {
1327            target_url: "https://192.168.2.86/".to_string(),
1328            api_key: None,
1329            basic_auth: None,
1330            skip_tls_verify: true,
1331            categories: None,
1332            base_path: Some("/api".to_string()),
1333            custom_headers: vec![],
1334            output_dir: None,
1335            all_operations: false,
1336        };
1337        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1338        let (script, _check_count) = gen.generate().unwrap();
1339
1340        assert!(
1341            script.contains("const BASE_URL = 'https://192.168.2.86/api'"),
1342            "BASE_URL should include the base_path. Got: {}",
1343            script.lines().find(|l| l.contains("BASE_URL")).unwrap_or("not found")
1344        );
1345    }
1346
1347    #[test]
1348    fn test_spec_driven_with_custom_headers() {
1349        let annotated = AnnotatedOperation {
1350            path: "/users".to_string(),
1351            method: "GET".to_string(),
1352            features: vec![ConformanceFeature::MethodGet],
1353            path_params: vec![],
1354            query_params: vec![],
1355            header_params: vec![
1356                ("X-Avi-Tenant".to_string(), "test-value".to_string()),
1357                ("X-CSRFToken".to_string(), "test-value".to_string()),
1358            ],
1359            request_body_content_type: None,
1360            sample_body: None,
1361            response_schema: None,
1362        };
1363        let config = ConformanceConfig {
1364            target_url: "https://192.168.2.86/".to_string(),
1365            api_key: None,
1366            basic_auth: None,
1367            skip_tls_verify: true,
1368            categories: None,
1369            base_path: Some("/api".to_string()),
1370            custom_headers: vec![
1371                ("X-Avi-Tenant".to_string(), "admin".to_string()),
1372                ("X-CSRFToken".to_string(), "real-csrf-token".to_string()),
1373                ("Cookie".to_string(), "sessionid=abc123".to_string()),
1374            ],
1375            output_dir: None,
1376            all_operations: false,
1377        };
1378        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1379        let (script, _check_count) = gen.generate().unwrap();
1380
1381        // Custom headers should override spec-derived test-value placeholders
1382        assert!(
1383            script.contains("'X-Avi-Tenant': 'admin'"),
1384            "Should use custom value for X-Avi-Tenant, not test-value"
1385        );
1386        assert!(
1387            script.contains("'X-CSRFToken': 'real-csrf-token'"),
1388            "Should use custom value for X-CSRFToken, not test-value"
1389        );
1390        // Custom headers not in spec should be appended
1391        assert!(
1392            script.contains("'Cookie': 'sessionid=abc123'"),
1393            "Should include Cookie header from custom_headers"
1394        );
1395        // test-value should NOT appear
1396        assert!(
1397            !script.contains("'test-value'"),
1398            "test-value placeholders should be replaced by custom values"
1399        );
1400    }
1401
1402    #[test]
1403    fn test_effective_headers_merging() {
1404        let config = ConformanceConfig {
1405            target_url: "http://localhost".to_string(),
1406            api_key: None,
1407            basic_auth: None,
1408            skip_tls_verify: false,
1409            categories: None,
1410            base_path: None,
1411            custom_headers: vec![
1412                ("X-Auth".to_string(), "real-token".to_string()),
1413                ("Cookie".to_string(), "session=abc".to_string()),
1414            ],
1415            output_dir: None,
1416            all_operations: false,
1417        };
1418        let gen = SpecDrivenConformanceGenerator::new(config, vec![]);
1419
1420        // Spec headers with a matching custom header
1421        let spec_headers = vec![
1422            ("X-Auth".to_string(), "test-value".to_string()),
1423            ("X-Other".to_string(), "keep-this".to_string()),
1424        ];
1425        let effective = gen.effective_headers(&spec_headers);
1426
1427        // X-Auth should be overridden
1428        assert_eq!(effective[0], ("X-Auth".to_string(), "real-token".to_string()));
1429        // X-Other should be kept as-is
1430        assert_eq!(effective[1], ("X-Other".to_string(), "keep-this".to_string()));
1431        // Cookie should be appended
1432        assert_eq!(effective[2], ("Cookie".to_string(), "session=abc".to_string()));
1433    }
1434}