Skip to main content

alef_e2e/codegen/
java.rs

1//! Java e2e test generator using JUnit 5.
2//!
3//! Generates `e2e/java/pom.xml` and `src/test/java/dev/kreuzberg/e2e/{Category}Test.java`
4//! files from JSON fixtures, driven entirely by `E2eConfig` and `CallConfig`.
5
6use crate::config::E2eConfig;
7use crate::escape::{escape_java, sanitize_filename};
8use crate::field_access::FieldResolver;
9use crate::fixture::{Assertion, CallbackAction, Fixture, FixtureGroup, HttpFixture};
10use alef_core::backend::GeneratedFile;
11use alef_core::config::ResolvedCrateConfig;
12use alef_core::hash::{self, CommentStyle};
13use alef_core::template_versions as tv;
14use anyhow::Result;
15use heck::{ToLowerCamelCase, ToUpperCamelCase};
16use std::path::PathBuf;
17
18use super::E2eCodegen;
19use super::client;
20
21/// Java e2e code generator.
22pub struct JavaCodegen;
23
24impl E2eCodegen for JavaCodegen {
25    fn generate(
26        &self,
27        groups: &[FixtureGroup],
28        e2e_config: &E2eConfig,
29        config: &ResolvedCrateConfig,
30    ) -> Result<Vec<GeneratedFile>> {
31        let lang = self.language_name();
32        let output_base = PathBuf::from(e2e_config.effective_output()).join(lang);
33
34        let mut files = Vec::new();
35
36        // Resolve call config with overrides.
37        let call = &e2e_config.call;
38        let overrides = call.overrides.get(lang);
39        let _module_path = overrides
40            .and_then(|o| o.module.as_ref())
41            .cloned()
42            .unwrap_or_else(|| call.module.clone());
43        let function_name = overrides
44            .and_then(|o| o.function.as_ref())
45            .cloned()
46            .unwrap_or_else(|| call.function.clone());
47        let class_name = overrides
48            .and_then(|o| o.class.as_ref())
49            .cloned()
50            .unwrap_or_else(|| config.name.to_upper_camel_case());
51        let result_is_simple = overrides.is_some_and(|o| o.result_is_simple);
52        let result_var = &call.result_var;
53
54        // Resolve package config.
55        let java_pkg = e2e_config.resolve_package("java");
56        let pkg_name = java_pkg
57            .as_ref()
58            .and_then(|p| p.name.as_ref())
59            .cloned()
60            .unwrap_or_else(|| config.name.clone());
61
62        // Resolve Java package info for the dependency.
63        let java_group_id = config.java_group_id();
64        let binding_pkg = config.java_package();
65        let pkg_version = config.resolved_version().unwrap_or_else(|| "0.1.0".to_string());
66
67        // Generate pom.xml.
68        files.push(GeneratedFile {
69            path: output_base.join("pom.xml"),
70            content: render_pom_xml(&pkg_name, &java_group_id, &pkg_version, e2e_config.dep_mode),
71            generated_header: false,
72        });
73
74        // Generate test files per category. Path mirrors the configured Java
75        // package — `dev.myorg` becomes `dev/myorg`, etc. — so the package
76        // declaration in each test file matches its filesystem location.
77        let mut test_base = output_base.join("src").join("test").join("java");
78        for segment in java_group_id.split('.') {
79            test_base = test_base.join(segment);
80        }
81        let test_base = test_base.join("e2e");
82
83        // Resolve options_type from override.
84        let options_type = overrides.and_then(|o| o.options_type.clone());
85
86        // Get Java-specific enum_fields from override (required for correct enum handling).
87        let empty_enum_fields = std::collections::HashMap::new();
88        let java_enum_fields = overrides.as_ref().map(|o| &o.enum_fields).unwrap_or(&empty_enum_fields);
89
90        // Build effective nested_types by merging defaults with configured overrides.
91        let mut effective_nested_types = default_java_nested_types();
92        if let Some(overrides_map) = overrides.map(|o| &o.nested_types) {
93            effective_nested_types.extend(overrides_map.clone());
94        }
95
96        // Resolve nested_types_optional from override (defaults to true for backward compatibility).
97        let nested_types_optional = overrides.map(|o| o.nested_types_optional).unwrap_or(true);
98
99        let field_resolver = FieldResolver::new(
100            &e2e_config.fields,
101            &e2e_config.fields_optional,
102            &e2e_config.result_fields,
103            &e2e_config.fields_array,
104            &std::collections::HashSet::new(),
105        );
106
107        for group in groups {
108            let active: Vec<&Fixture> = group
109                .fixtures
110                .iter()
111                .filter(|f| super::should_include_fixture(f, lang, e2e_config))
112                .collect();
113
114            if active.is_empty() {
115                continue;
116            }
117
118            let class_file_name = format!("{}Test.java", sanitize_filename(&group.category).to_upper_camel_case());
119            let content = render_test_file(
120                &group.category,
121                &active,
122                &class_name,
123                &function_name,
124                &java_group_id,
125                &binding_pkg,
126                result_var,
127                &e2e_config.call.args,
128                options_type.as_deref(),
129                &field_resolver,
130                result_is_simple,
131                java_enum_fields,
132                e2e_config,
133                &effective_nested_types,
134                nested_types_optional,
135            );
136            files.push(GeneratedFile {
137                path: test_base.join(class_file_name),
138                content,
139                generated_header: true,
140            });
141        }
142
143        Ok(files)
144    }
145
146    fn language_name(&self) -> &'static str {
147        "java"
148    }
149}
150
151// ---------------------------------------------------------------------------
152// Rendering
153// ---------------------------------------------------------------------------
154
155fn render_pom_xml(
156    pkg_name: &str,
157    java_group_id: &str,
158    pkg_version: &str,
159    dep_mode: crate::config::DependencyMode,
160) -> String {
161    // pkg_name may be in "groupId:artifactId" Maven format; split accordingly.
162    let (dep_group_id, dep_artifact_id) = if let Some((g, a)) = pkg_name.split_once(':') {
163        (g, a)
164    } else {
165        (java_group_id, pkg_name)
166    };
167    let artifact_id = format!("{dep_artifact_id}-e2e-java");
168    let dep_block = match dep_mode {
169        crate::config::DependencyMode::Registry => {
170            format!(
171                r#"        <dependency>
172            <groupId>{dep_group_id}</groupId>
173            <artifactId>{dep_artifact_id}</artifactId>
174            <version>{pkg_version}</version>
175        </dependency>"#
176            )
177        }
178        crate::config::DependencyMode::Local => {
179            format!(
180                r#"        <dependency>
181            <groupId>{dep_group_id}</groupId>
182            <artifactId>{dep_artifact_id}</artifactId>
183            <version>{pkg_version}</version>
184            <scope>system</scope>
185            <systemPath>${{project.basedir}}/../../packages/java/target/{dep_artifact_id}-{pkg_version}.jar</systemPath>
186        </dependency>"#
187            )
188        }
189    };
190    crate::template_env::render(
191        "java/pom.xml.jinja",
192        minijinja::context! {
193            artifact_id => artifact_id,
194            java_group_id => java_group_id,
195            dep_block => dep_block,
196            junit_version => tv::maven::JUNIT,
197            jackson_version => tv::maven::JACKSON_E2E,
198            build_helper_version => tv::maven::BUILD_HELPER_MAVEN_PLUGIN,
199            maven_surefire_version => tv::maven::MAVEN_SUREFIRE_PLUGIN_E2E,
200        },
201    )
202}
203
204#[allow(clippy::too_many_arguments)]
205fn render_test_file(
206    category: &str,
207    fixtures: &[&Fixture],
208    class_name: &str,
209    function_name: &str,
210    java_group_id: &str,
211    binding_pkg: &str,
212    result_var: &str,
213    args: &[crate::config::ArgMapping],
214    options_type: Option<&str>,
215    field_resolver: &FieldResolver,
216    result_is_simple: bool,
217    enum_fields: &std::collections::HashMap<String, String>,
218    e2e_config: &E2eConfig,
219    nested_types: &std::collections::HashMap<String, String>,
220    nested_types_optional: bool,
221) -> String {
222    let header = hash::header(CommentStyle::DoubleSlash);
223    let test_class_name = format!("{}Test", sanitize_filename(category).to_upper_camel_case());
224
225    // If the class_name is fully qualified (contains '.'), import it and use
226    // only the simple name for method calls.  Otherwise use it as-is.
227    let (import_path, simple_class) = if class_name.contains('.') {
228        let simple = class_name.rsplit('.').next().unwrap_or(class_name);
229        (class_name, simple)
230    } else {
231        ("", class_name)
232    };
233
234    // Check if any fixture (with its resolved call) will emit MAPPER usage.
235    let lang_for_om = "java";
236    let needs_object_mapper_for_handle = fixtures.iter().any(|f| {
237        args.iter().filter(|a| a.arg_type == "handle").any(|a| {
238            let v = f.input.get(&a.field).unwrap_or(&serde_json::Value::Null);
239            !(v.is_null() || v.is_object() && v.as_object().is_some_and(|o| o.is_empty()))
240        })
241    });
242    // HTTP fixtures always need ObjectMapper for JSON body comparison.
243    let has_http_fixtures = fixtures.iter().any(|f| f.http.is_some());
244    let needs_object_mapper = needs_object_mapper_for_handle || has_http_fixtures;
245
246    // Collect all options_type values used (class-level + per-fixture call overrides).
247    let mut all_options_types: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
248    if let Some(t) = options_type {
249        all_options_types.insert(t.to_string());
250    }
251    for f in fixtures.iter() {
252        let call_cfg = e2e_config.resolve_call(f.call.as_deref());
253        if let Some(ov) = call_cfg.overrides.get(lang_for_om) {
254            if let Some(t) = &ov.options_type {
255                all_options_types.insert(t.clone());
256            }
257        }
258        // Auto-fallback: when the Java override does not declare an options_type
259        // but another non-prefixed binding (csharp/c/go/php/python) does, mirror
260        // that name into the import set so the auto-emitted `Type.fromJson(json)`
261        // expression compiles. The Java POJO class name matches the Rust source
262        // type name for these backends.
263        let java_has_type = call_cfg
264            .overrides
265            .get(lang_for_om)
266            .and_then(|o| o.options_type.as_deref())
267            .is_some();
268        if !java_has_type {
269            for cand in ["csharp", "c", "go", "php", "python"] {
270                if let Some(o) = call_cfg.overrides.get(cand) {
271                    if let Some(t) = &o.options_type {
272                        all_options_types.insert(t.clone());
273                        break;
274                    }
275                }
276            }
277        }
278        // Detect batch item types used in this fixture
279        for arg in &call_cfg.args {
280            if let Some(elem_type) = &arg.element_type {
281                if elem_type == "BatchBytesItem" || elem_type == "BatchFileItem" {
282                    all_options_types.insert(elem_type.clone());
283                }
284            }
285        }
286    }
287
288    // Collect all enum types used in builder expressions across all fixtures.
289    let mut enum_types_used: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
290    // Collect nested config types actually referenced in fixture builder expressions
291    let mut nested_types_used: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
292    for f in fixtures.iter() {
293        let call_cfg = e2e_config.resolve_call(f.call.as_deref());
294        for arg in &call_cfg.args {
295            if arg.arg_type == "json_object" {
296                let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
297                if let Some(val) = f.input.get(field) {
298                    if !val.is_null() && !val.is_array() {
299                        if let Some(obj) = val.as_object() {
300                            collect_enum_and_nested_types(obj, enum_fields, &mut enum_types_used);
301                            collect_nested_type_names(obj, nested_types, &mut nested_types_used);
302                        }
303                    }
304                }
305            }
306        }
307    }
308
309    // Effective binding package for FQN imports of binding types
310    // (ChatCompletionRequest, etc.). Prefer the explicit `[crates.java] package`
311    // wired in via `binding_pkg`; fall back to the package derived from a
312    // fully-qualified `class_name` when present.
313    let binding_pkg_for_imports: String = if !binding_pkg.is_empty() {
314        binding_pkg.to_string()
315    } else if !import_path.is_empty() {
316        import_path
317            .rsplit_once('.')
318            .map(|(p, _)| p.to_string())
319            .unwrap_or_default()
320    } else {
321        String::new()
322    };
323
324    // Build imports list
325    let mut imports: Vec<String> = Vec::new();
326    imports.push("import org.junit.jupiter.api.Test;".to_string());
327    imports.push("import static org.junit.jupiter.api.Assertions.*;".to_string());
328
329    // Import the test entry-point class itself when it is fully-qualified or
330    // when we know the binding package — emit the FQN so javac resolves it.
331    if !import_path.is_empty() {
332        imports.push(format!("import {import_path};"));
333    } else if !binding_pkg_for_imports.is_empty() && !class_name.is_empty() {
334        imports.push(format!("import {binding_pkg_for_imports}.{class_name};"));
335    }
336
337    if needs_object_mapper {
338        imports.push("import com.fasterxml.jackson.databind.ObjectMapper;".to_string());
339        imports.push("import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;".to_string());
340    }
341
342    // Import all options types used across fixtures (for builder expressions and MAPPER).
343    if !all_options_types.is_empty() {
344        for opts_type in &all_options_types {
345            let qualified = if binding_pkg_for_imports.is_empty() {
346                opts_type.clone()
347            } else {
348                format!("{binding_pkg_for_imports}.{opts_type}")
349            };
350            imports.push(format!("import {qualified};"));
351        }
352    }
353
354    // Import all enum types used in builder expressions
355    if !enum_types_used.is_empty() && !binding_pkg_for_imports.is_empty() {
356        for enum_type in &enum_types_used {
357            imports.push(format!("import {binding_pkg_for_imports}.{enum_type};"));
358        }
359    }
360
361    // Import nested options types
362    if !nested_types_used.is_empty() && !binding_pkg_for_imports.is_empty() {
363        for type_name in &nested_types_used {
364            imports.push(format!("import {binding_pkg_for_imports}.{type_name};"));
365        }
366    }
367
368    // Import CrawlConfig when handle args need JSON deserialization.
369    if needs_object_mapper_for_handle && !binding_pkg_for_imports.is_empty() {
370        imports.push(format!("import {binding_pkg_for_imports}.CrawlConfig;"));
371    }
372
373    // Import visitor types when any fixture uses visitor callbacks.
374    let has_visitor_fixtures = fixtures.iter().any(|f| f.visitor.is_some());
375    if has_visitor_fixtures && !binding_pkg_for_imports.is_empty() {
376        imports.push(format!("import {binding_pkg_for_imports}.Visitor;"));
377        imports.push(format!("import {binding_pkg_for_imports}.NodeContext;"));
378        imports.push(format!("import {binding_pkg_for_imports}.VisitResult;"));
379    }
380
381    // Import Optional when using builder expressions with optional fields
382    if !all_options_types.is_empty() {
383        imports.push("import java.util.Optional;".to_string());
384    }
385
386    // Render all test methods
387    let mut fixtures_body = String::new();
388    for (i, fixture) in fixtures.iter().enumerate() {
389        render_test_method(
390            &mut fixtures_body,
391            fixture,
392            simple_class,
393            function_name,
394            result_var,
395            args,
396            options_type,
397            field_resolver,
398            result_is_simple,
399            enum_fields,
400            e2e_config,
401            nested_types,
402            nested_types_optional,
403        );
404        if i + 1 < fixtures.len() {
405            fixtures_body.push('\n');
406        }
407    }
408
409    // Render template
410    crate::template_env::render(
411        "java/test_file.jinja",
412        minijinja::context! {
413            header => header,
414            java_group_id => java_group_id,
415            test_class_name => test_class_name,
416            category => category,
417            imports => imports,
418            needs_object_mapper => needs_object_mapper,
419            fixtures_body => fixtures_body,
420        },
421    )
422}
423
424// ---------------------------------------------------------------------------
425// HTTP test rendering — shared-driver integration
426// ---------------------------------------------------------------------------
427
428/// Thin renderer that emits JUnit 5 test methods targeting a mock server via
429/// `java.net.http.HttpClient`. Satisfies [`client::TestClientRenderer`] so the
430/// shared [`client::http_call::render_http_test`] driver drives the call sequence.
431struct JavaTestClientRenderer;
432
433impl client::TestClientRenderer for JavaTestClientRenderer {
434    fn language_name(&self) -> &'static str {
435        "java"
436    }
437
438    /// Convert a fixture id to the UpperCamelCase suffix appended to `test`.
439    ///
440    /// The emitted method name is `test{fn_name}`, matching the pre-existing shape.
441    fn sanitize_test_name(&self, id: &str) -> String {
442        id.to_upper_camel_case()
443    }
444
445    /// Emit `@Test void test{fn_name}() throws Exception {`.
446    ///
447    /// When `skip_reason` is `Some`, the body is a single
448    /// `Assumptions.assumeTrue(false, ...)` call and `render_test_close` closes
449    /// the brace symmetrically.
450    fn render_test_open(&self, out: &mut String, fn_name: &str, description: &str, skip_reason: Option<&str>) {
451        let escaped_reason = skip_reason.map(escape_java);
452        let rendered = crate::template_env::render(
453            "java/http_test_open.jinja",
454            minijinja::context! {
455                fn_name => fn_name,
456                description => description,
457                skip_reason => escaped_reason,
458            },
459        );
460        out.push_str(&rendered);
461    }
462
463    /// Emit the closing `}` for a test method.
464    fn render_test_close(&self, out: &mut String) {
465        let rendered = crate::template_env::render("java/http_test_close.jinja", minijinja::context! {});
466        out.push_str(&rendered);
467    }
468
469    /// Emit a `java.net.http.HttpClient` request to `baseUrl + path`.
470    ///
471    /// Binds the response to `response` (the `ctx.response_var`). Java's
472    /// `HttpClient` disallows a fixed set of restricted headers; those are
473    /// silently dropped so the test compiles.
474    fn render_call(&self, out: &mut String, ctx: &client::CallCtx<'_>) {
475        // Java's HttpClient throws IllegalArgumentException for these headers.
476        const JAVA_RESTRICTED_HEADERS: &[&str] = &["connection", "content-length", "expect", "host", "upgrade"];
477
478        let method = ctx.method.to_uppercase();
479
480        // Build the path, appending query params when present.
481        let path = if ctx.query_params.is_empty() {
482            ctx.path.to_string()
483        } else {
484            let pairs: Vec<String> = ctx
485                .query_params
486                .iter()
487                .map(|(k, v)| {
488                    let val_str = match v {
489                        serde_json::Value::String(s) => s.clone(),
490                        other => other.to_string(),
491                    };
492                    format!("{}={}", k, escape_java(&val_str))
493                })
494                .collect();
495            format!("{}?{}", ctx.path, pairs.join("&"))
496        };
497
498        let body_publisher = if let Some(body) = ctx.body {
499            let json = serde_json::to_string(body).unwrap_or_default();
500            let escaped = escape_java(&json);
501            format!("java.net.http.HttpRequest.BodyPublishers.ofString(\"{escaped}\")")
502        } else {
503            "java.net.http.HttpRequest.BodyPublishers.noBody()".to_string()
504        };
505
506        // Content-Type header — only when a body is present.
507        let content_type = if ctx.body.is_some() {
508            let ct = ctx.content_type.unwrap_or("application/json");
509            // Only emit when not already in ctx.headers (avoid duplicate Content-Type).
510            if !ctx.headers.keys().any(|k| k.to_lowercase() == "content-type") {
511                Some(ct.to_string())
512            } else {
513                None
514            }
515        } else {
516            None
517        };
518
519        // Build header lines — skip Java-restricted ones.
520        let mut headers_lines: Vec<String> = Vec::new();
521        for (name, value) in ctx.headers {
522            if JAVA_RESTRICTED_HEADERS.contains(&name.to_lowercase().as_str()) {
523                continue;
524            }
525            let escaped_name = escape_java(name);
526            let escaped_value = escape_java(value);
527            headers_lines.push(format!(
528                "builder = builder.header(\"{escaped_name}\", \"{escaped_value}\");"
529            ));
530        }
531
532        // Cookies as a single `Cookie` header.
533        let cookies_line = if !ctx.cookies.is_empty() {
534            let cookie_str: Vec<String> = ctx.cookies.iter().map(|(k, v)| format!("{k}={v}")).collect();
535            let cookie_header = escape_java(&cookie_str.join("; "));
536            Some(format!("builder = builder.header(\"Cookie\", \"{cookie_header}\");"))
537        } else {
538            None
539        };
540
541        let rendered = crate::template_env::render(
542            "java/http_request.jinja",
543            minijinja::context! {
544                method => method,
545                path => path,
546                body_publisher => body_publisher,
547                content_type => content_type,
548                headers_lines => headers_lines,
549                cookies_line => cookies_line,
550                response_var => ctx.response_var,
551            },
552        );
553        out.push_str(&rendered);
554    }
555
556    /// Emit `assertEquals(status, response.statusCode(), ...)`.
557    fn render_assert_status(&self, out: &mut String, response_var: &str, status: u16) {
558        let rendered = crate::template_env::render(
559            "java/http_assertions.jinja",
560            minijinja::context! {
561                response_var => response_var,
562                status_code => status,
563                headers => Vec::<std::collections::HashMap<&str, String>>::new(),
564                body_assertion => String::new(),
565                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
566                validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
567            },
568        );
569        out.push_str(&rendered);
570    }
571
572    /// Emit a header assertion using `response.headers().firstValue(...)`.
573    ///
574    /// Handles special tokens: `<<present>>`, `<<absent>>`, `<<uuid>>`.
575    fn render_assert_header(&self, out: &mut String, response_var: &str, name: &str, expected: &str) {
576        let escaped_name = escape_java(name);
577        let assertion_code = match expected {
578            "<<present>>" => {
579                format!(
580                    "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").isPresent(), \"header {escaped_name} should be present\");"
581                )
582            }
583            "<<absent>>" => {
584                format!(
585                    "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").isEmpty(), \"header {escaped_name} should be absent\");"
586                )
587            }
588            "<<uuid>>" => {
589                format!(
590                    "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").orElse(\"\").matches(\"[0-9a-fA-F]{{8}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{12}}\"), \"header {escaped_name} should be a UUID\");"
591                )
592            }
593            literal => {
594                let escaped_value = escape_java(literal);
595                format!(
596                    "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").orElse(\"\").contains(\"{escaped_value}\"), \"header {escaped_name} mismatch\");"
597                )
598            }
599        };
600
601        let mut headers = vec![std::collections::HashMap::new()];
602        headers[0].insert("assertion_code", assertion_code);
603
604        let rendered = crate::template_env::render(
605            "java/http_assertions.jinja",
606            minijinja::context! {
607                response_var => response_var,
608                status_code => 0u16,
609                headers => headers,
610                body_assertion => String::new(),
611                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
612                validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
613            },
614        );
615        out.push_str(&rendered);
616    }
617
618    /// Emit a JSON body equality assertion using Jackson's `MAPPER.readTree`.
619    fn render_assert_json_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
620        let body_assertion = match expected {
621            serde_json::Value::Object(_) | serde_json::Value::Array(_) => {
622                let json_str = serde_json::to_string(expected).unwrap_or_default();
623                let escaped = escape_java(&json_str);
624                format!(
625                    "var bodyJson = MAPPER.readTree({response_var}.body());\n        var expectedJson = MAPPER.readTree(\"{escaped}\");\n        assertEquals(expectedJson, bodyJson, \"body mismatch\");"
626                )
627            }
628            serde_json::Value::String(s) => {
629                let escaped = escape_java(s);
630                format!("assertEquals(\"{escaped}\", {response_var}.body().trim(), \"body mismatch\");")
631            }
632            other => {
633                let escaped = escape_java(&other.to_string());
634                format!("assertEquals(\"{escaped}\", {response_var}.body().trim(), \"body mismatch\");")
635            }
636        };
637
638        let rendered = crate::template_env::render(
639            "java/http_assertions.jinja",
640            minijinja::context! {
641                response_var => response_var,
642                status_code => 0u16,
643                headers => Vec::<std::collections::HashMap<&str, String>>::new(),
644                body_assertion => body_assertion,
645                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
646                validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
647            },
648        );
649        out.push_str(&rendered);
650    }
651
652    /// Emit partial JSON body assertions: parse once, then assert each expected field.
653    fn render_assert_partial_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
654        if let Some(obj) = expected.as_object() {
655            let mut partial_body: Vec<std::collections::HashMap<&str, String>> = Vec::new();
656            for (key, val) in obj {
657                let escaped_key = escape_java(key);
658                let json_str = serde_json::to_string(val).unwrap_or_default();
659                let escaped_val = escape_java(&json_str);
660                let assertion_code = format!(
661                    "assertEquals(MAPPER.readTree(\"{escaped_val}\"), partialJson.get(\"{escaped_key}\"), \"body field '{escaped_key}' mismatch\");"
662                );
663                let mut entry = std::collections::HashMap::new();
664                entry.insert("assertion_code", assertion_code);
665                partial_body.push(entry);
666            }
667
668            let rendered = crate::template_env::render(
669                "java/http_assertions.jinja",
670                minijinja::context! {
671                    response_var => response_var,
672                    status_code => 0u16,
673                    headers => Vec::<std::collections::HashMap<&str, String>>::new(),
674                    body_assertion => String::new(),
675                    partial_body => partial_body,
676                    validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
677                },
678            );
679            out.push_str(&rendered);
680        }
681    }
682
683    /// Emit validation-error assertions: parse the body and check each expected message.
684    fn render_assert_validation_errors(
685        &self,
686        out: &mut String,
687        response_var: &str,
688        errors: &[crate::fixture::ValidationErrorExpectation],
689    ) {
690        let mut validation_errors: Vec<std::collections::HashMap<&str, String>> = Vec::new();
691        for err in errors {
692            let escaped_msg = escape_java(&err.msg);
693            let assertion_code = format!(
694                "assertTrue(veBody.contains(\"{escaped_msg}\"), \"expected validation error message: {escaped_msg}\");"
695            );
696            let mut entry = std::collections::HashMap::new();
697            entry.insert("assertion_code", assertion_code);
698            validation_errors.push(entry);
699        }
700
701        let rendered = crate::template_env::render(
702            "java/http_assertions.jinja",
703            minijinja::context! {
704                response_var => response_var,
705                status_code => 0u16,
706                headers => Vec::<std::collections::HashMap<&str, String>>::new(),
707                body_assertion => String::new(),
708                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
709                validation_errors => validation_errors,
710            },
711        );
712        out.push_str(&rendered);
713    }
714}
715
716/// Render an HTTP server test method using `java.net.http.HttpClient` against
717/// `MOCK_SERVER_URL`. Delegates to the shared
718/// [`client::http_call::render_http_test`] driver via [`JavaTestClientRenderer`].
719///
720/// The one Java-specific pre-condition — HTTP 101 (WebSocket upgrade) causing an
721/// `EOFException` in `HttpClient` — is handled here before delegating.
722fn render_http_test_method(out: &mut String, fixture: &Fixture, http: &HttpFixture) {
723    // HTTP 101 (WebSocket upgrade) causes Java's HttpClient to throw EOFException.
724    // Emit an assumeTrue(false, ...) stub so the test is skipped rather than failing.
725    if http.expected_response.status_code == 101 {
726        let method_name = fixture.id.to_upper_camel_case();
727        let description = &fixture.description;
728        out.push_str(&crate::template_env::render(
729            "java/http_test_skip_101.jinja",
730            minijinja::context! {
731                method_name => method_name,
732                description => description,
733            },
734        ));
735        return;
736    }
737
738    client::http_call::render_http_test(out, &JavaTestClientRenderer, fixture);
739}
740
741#[allow(clippy::too_many_arguments)]
742fn render_test_method(
743    out: &mut String,
744    fixture: &Fixture,
745    class_name: &str,
746    _function_name: &str,
747    _result_var: &str,
748    _args: &[crate::config::ArgMapping],
749    options_type: Option<&str>,
750    field_resolver: &FieldResolver,
751    result_is_simple: bool,
752    enum_fields: &std::collections::HashMap<String, String>,
753    e2e_config: &E2eConfig,
754    nested_types: &std::collections::HashMap<String, String>,
755    nested_types_optional: bool,
756) {
757    // Delegate HTTP fixtures to the HTTP-specific renderer.
758    if let Some(http) = &fixture.http {
759        render_http_test_method(out, fixture, http);
760        return;
761    }
762
763    // Resolve per-fixture call config (supports named calls via fixture.call field).
764    let call_config = e2e_config.resolve_call(fixture.call.as_deref());
765    let lang = "java";
766    let call_overrides = call_config.overrides.get(lang);
767    let effective_function_name = call_overrides
768        .and_then(|o| o.function.as_ref())
769        .cloned()
770        .unwrap_or_else(|| call_config.function.to_lower_camel_case());
771    let effective_result_var = &call_config.result_var;
772    let effective_args = &call_config.args;
773    let function_name = effective_function_name.as_str();
774    let result_var = effective_result_var.as_str();
775    let args: &[crate::config::ArgMapping] = effective_args.as_slice();
776
777    let method_name = fixture.id.to_upper_camel_case();
778    let description = &fixture.description;
779    let expects_error = fixture.assertions.iter().any(|a| a.assertion_type == "error");
780
781    // Resolve per-fixture options_type: prefer the java call override, fall back to
782    // class-level, then to any other language's options_type for the same call (the
783    // generated Java POJO class name matches the Rust type name across bindings, so
784    // mirroring the C/csharp/go option lets us auto-emit `Type.fromJson(json)` without
785    // requiring an explicit Java override per call).
786    let effective_options_type: Option<String> = call_overrides
787        .and_then(|o| o.options_type.clone())
788        .or_else(|| options_type.map(|s| s.to_string()))
789        .or_else(|| {
790            // Borrow from any other backend's options_type. Prefer non-language-prefixed
791            // names (csharp/c/go/php/python) over wasm or ruby which use prefixed types
792            // like `WasmCreateBatchRequest` or `LiterLlm::CreateBatchRequest`.
793            for cand in ["csharp", "c", "go", "php", "python"] {
794                if let Some(o) = call_config.overrides.get(cand) {
795                    if let Some(t) = &o.options_type {
796                        return Some(t.clone());
797                    }
798                }
799            }
800            None
801        });
802    let effective_options_type = effective_options_type.as_deref();
803    // When options_type is resolvable but no explicit options_via is given for Java,
804    // default to "from_json" so the typed-request arg is emitted as
805    // `Type.fromJson(json)` rather than the raw JSON string. The Java backend exposes
806    // a static `fromJson(String)` factory on every record type (Stage A).
807    let auto_from_json = effective_options_type.is_some()
808        && call_overrides.and_then(|o| o.options_via.as_deref()).is_none()
809        && e2e_config
810            .call
811            .overrides
812            .get(lang)
813            .and_then(|o| o.options_via.as_deref())
814            .is_none();
815
816    // Resolve client_factory: prefer call-level java override, fall back to file-level java override.
817    let client_factory: Option<String> = call_overrides.and_then(|o| o.client_factory.clone()).or_else(|| {
818        e2e_config
819            .call
820            .overrides
821            .get(lang)
822            .and_then(|o| o.client_factory.clone())
823    });
824
825    // Resolve options_via: "kwargs" (default), "from_json", "json", "dict".
826    // Auto-default to "from_json" when an options_type is resolvable and no explicit
827    // options_via is configured — this lets typed-request args emit `Type.fromJson(json)`
828    // even when alef.toml only declares the type in another binding's override block.
829    let options_via: String = call_overrides
830        .and_then(|o| o.options_via.clone())
831        .or_else(|| e2e_config.call.overrides.get(lang).and_then(|o| o.options_via.clone()))
832        .unwrap_or_else(|| {
833            if auto_from_json {
834                "from_json".to_string()
835            } else {
836                "kwargs".to_string()
837            }
838        });
839
840    // Resolve per-fixture result_is_simple and result_is_bytes from the call override.
841    let effective_result_is_simple =
842        call_overrides.is_some_and(|o| o.result_is_simple) || call_config.result_is_simple || result_is_simple;
843    let effective_result_is_bytes = call_overrides.is_some_and(|o| o.result_is_bytes);
844
845    // Check if this test needs ObjectMapper deserialization for json_object args.
846    let needs_deser = effective_options_type.is_some()
847        && args.iter().any(|arg| {
848            if arg.arg_type != "json_object" {
849                return false;
850            }
851            let val = super::resolve_field(&fixture.input, &arg.field);
852            !val.is_null() && !val.is_array()
853        });
854
855    // Emit builder expressions for json_object args.
856    let mut builder_expressions = String::new();
857    if let (true, Some(opts_type)) = (needs_deser, effective_options_type) {
858        for arg in args {
859            if arg.arg_type == "json_object" {
860                let val = super::resolve_field(&fixture.input, &arg.field);
861                if !val.is_null() && !val.is_array() {
862                    if options_via == "from_json" {
863                        // Build the typed POJO via static fromJson(String) method.
864                        let json_str = serde_json::to_string(val).unwrap_or_default();
865                        let escaped = escape_java(&json_str);
866                        let var_name = &arg.name;
867                        builder_expressions.push_str(&format!(
868                            "        var {var_name} = {opts_type}.fromJson(\"{escaped}\");\n",
869                        ));
870                    } else if let Some(obj) = val.as_object() {
871                        // Generate builder expression: TypeName.builder().withFieldName(value)...build()
872                        let empty_path_fields: Vec<String> = Vec::new();
873                        let path_fields = call_overrides.map(|o| &o.path_fields).unwrap_or(&empty_path_fields);
874                        let builder_expr = java_builder_expression(
875                            obj,
876                            opts_type,
877                            enum_fields,
878                            nested_types,
879                            nested_types_optional,
880                            path_fields,
881                        );
882                        let var_name = &arg.name;
883                        builder_expressions.push_str(&format!("        var {} = {};\n", var_name, builder_expr));
884                    }
885                }
886            }
887        }
888    }
889
890    let (mut setup_lines, args_str) =
891        build_args_and_setup(&fixture.input, args, class_name, effective_options_type, &fixture.id);
892
893    // Per-language `extra_args` from call overrides — verbatim trailing
894    // expressions appended after the configured args (e.g. `null` for an
895    // optional trailing parameter the fixture cannot supply). Mirrors the
896    // TypeScript and C# implementations.
897    let extra_args_slice: &[String] = call_overrides.map_or(&[], |o| o.extra_args.as_slice());
898
899    // Build visitor if present and add to setup
900    let mut visitor_var = String::new();
901    let mut has_visitor_fixture = false;
902    if let Some(visitor_spec) = &fixture.visitor {
903        visitor_var = build_java_visitor(&mut setup_lines, visitor_spec, class_name);
904        has_visitor_fixture = true;
905    }
906
907    // When visitor is present, attach it to the options parameter
908    let mut final_args = if has_visitor_fixture {
909        if args_str.is_empty() {
910            format!("new ConversionOptions().withVisitor({})", visitor_var)
911        } else if args_str.contains("new ConversionOptions")
912            || args_str.contains("ConversionOptionsBuilder")
913            || args_str.contains(".builder()")
914        {
915            // Options are being built (either new ConversionOptions(), builder pattern, or .builder().build())
916            // append .withVisitor() call before .build() if present
917            if args_str.contains(".build()") {
918                let idx = args_str.rfind(".build()").unwrap();
919                format!("{}.withVisitor({}){}", &args_str[..idx], visitor_var, &args_str[idx..])
920            } else {
921                format!("{}.withVisitor({})", args_str, visitor_var)
922            }
923        } else if args_str.ends_with(", null") {
924            let base = &args_str[..args_str.len() - 6];
925            format!("{}, new ConversionOptions().withVisitor({})", base, visitor_var)
926        } else {
927            format!("{}, new ConversionOptions().withVisitor({})", args_str, visitor_var)
928        }
929    } else {
930        args_str
931    };
932
933    if !extra_args_slice.is_empty() {
934        let extra_str = extra_args_slice.join(", ");
935        final_args = if final_args.is_empty() {
936            extra_str
937        } else {
938            format!("{final_args}, {extra_str}")
939        };
940    }
941
942    // Render assertions_body
943    let mut assertions_body = String::new();
944
945    // Emit a `source` variable for run_query assertions that need the raw bytes.
946    let needs_source_var = fixture
947        .assertions
948        .iter()
949        .any(|a| a.assertion_type == "method_result" && a.method.as_deref() == Some("run_query"));
950    if needs_source_var {
951        if let Some(source_arg) = args.iter().find(|a| a.field == "source_code") {
952            let field = source_arg.field.strip_prefix("input.").unwrap_or(&source_arg.field);
953            if let Some(val) = fixture.input.get(field) {
954                let java_val = json_to_java(val);
955                assertions_body.push_str(&format!("        var source = {}.getBytes();\n", java_val));
956            }
957        }
958    }
959
960    for assertion in &fixture.assertions {
961        render_assertion(
962            &mut assertions_body,
963            assertion,
964            result_var,
965            class_name,
966            field_resolver,
967            effective_result_is_simple,
968            effective_result_is_bytes,
969            enum_fields,
970        );
971    }
972
973    let throws_clause = " throws Exception";
974
975    // When client_factory is set, instantiate a client and dispatch the call as
976    // a method on the client; otherwise call the static helper on `class_name`.
977    let (client_setup_lines, call_target) = if let Some(factory) = client_factory.as_deref() {
978        let factory_name = factory.to_lower_camel_case();
979        let fixture_id = &fixture.id;
980        let mut setup: Vec<String> = Vec::new();
981        if fixture.mock_response.is_some() || fixture.http.is_some() {
982            setup.push(format!(
983                "String mockUrl = System.getenv(\"MOCK_SERVER_URL\") + \"/fixtures/{fixture_id}\";"
984            ));
985            setup.push(format!(
986                "var client = {class_name}.{factory_name}(\"test-key\", mockUrl, null, null, null);"
987            ));
988        } else if let Some(api_key_var) = fixture.env.as_ref().and_then(|e| e.api_key_var.as_deref()) {
989            setup.push(format!("String apiKey = System.getenv(\"{api_key_var}\");"));
990            setup.push(format!(
991                "org.junit.jupiter.api.Assumptions.assumeTrue(apiKey != null && !apiKey.isEmpty(), \"{api_key_var} not set\");"
992            ));
993            setup.push(format!("var client = {class_name}.{factory_name}(apiKey);"));
994        } else {
995            setup.push(format!("var client = {class_name}.{factory_name}(\"test-key\");"));
996        }
997        (setup, "client".to_string())
998    } else {
999        (Vec::new(), class_name.to_string())
1000    };
1001
1002    // Prepend client setup before any other setup_lines.
1003    let combined_setup: Vec<String> = client_setup_lines.into_iter().chain(setup_lines).collect();
1004
1005    let call_expr = format!("{call_target}.{function_name}({final_args})");
1006
1007    let rendered = crate::template_env::render(
1008        "java/test_method.jinja",
1009        minijinja::context! {
1010            method_name => method_name,
1011            description => description,
1012            builder_expressions => builder_expressions,
1013            setup_lines => combined_setup,
1014            throws_clause => throws_clause,
1015            expects_error => expects_error,
1016            call_expr => call_expr,
1017            result_var => result_var,
1018            assertions_body => assertions_body,
1019        },
1020    );
1021    out.push_str(&rendered);
1022}
1023
1024/// Build setup lines (e.g. handle creation) and the argument list for the function call.
1025///
1026/// Returns `(setup_lines, args_string)`.
1027fn build_args_and_setup(
1028    input: &serde_json::Value,
1029    args: &[crate::config::ArgMapping],
1030    class_name: &str,
1031    options_type: Option<&str>,
1032    fixture_id: &str,
1033) -> (Vec<String>, String) {
1034    if args.is_empty() {
1035        return (Vec::new(), String::new());
1036    }
1037
1038    let mut setup_lines: Vec<String> = Vec::new();
1039    let mut parts: Vec<String> = Vec::new();
1040
1041    for arg in args {
1042        if arg.arg_type == "mock_url" {
1043            setup_lines.push(format!(
1044                "String {} = System.getenv(\"MOCK_SERVER_URL\") + \"/fixtures/{fixture_id}\";",
1045                arg.name,
1046            ));
1047            parts.push(arg.name.clone());
1048            continue;
1049        }
1050
1051        if arg.arg_type == "handle" {
1052            // Generate a createEngine (or equivalent) call and pass the variable.
1053            let constructor_name = format!("create{}", arg.name.to_upper_camel_case());
1054            let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1055            let config_value = input.get(field).unwrap_or(&serde_json::Value::Null);
1056            if config_value.is_null()
1057                || config_value.is_object() && config_value.as_object().is_some_and(|o| o.is_empty())
1058            {
1059                setup_lines.push(format!("var {} = {class_name}.{constructor_name}(null);", arg.name,));
1060            } else {
1061                let json_str = serde_json::to_string(config_value).unwrap_or_default();
1062                let name = &arg.name;
1063                setup_lines.push(format!(
1064                    "var {name}Config = MAPPER.readValue(\"{}\", CrawlConfig.class);",
1065                    escape_java(&json_str),
1066                ));
1067                setup_lines.push(format!(
1068                    "var {} = {class_name}.{constructor_name}({name}Config);",
1069                    arg.name,
1070                    name = name,
1071                ));
1072            }
1073            parts.push(arg.name.clone());
1074            continue;
1075        }
1076
1077        let resolved = super::resolve_field(input, &arg.field);
1078        let val = if resolved.is_null() { None } else { Some(resolved) };
1079        match val {
1080            None | Some(serde_json::Value::Null) if arg.optional => {
1081                // Optional arg with no fixture value: emit positional null/default so the call
1082                // has the right arity. For json_object optional args, build an empty default object
1083                // so we get the right type rather than a raw null.
1084                if arg.arg_type == "json_object" {
1085                    if let Some(opts_type) = options_type {
1086                        parts.push(format!("{opts_type}.builder().build()"));
1087                    } else {
1088                        parts.push("null".to_string());
1089                    }
1090                } else {
1091                    parts.push("null".to_string());
1092                }
1093            }
1094            None | Some(serde_json::Value::Null) => {
1095                // Required arg with no fixture value: pass a language-appropriate default.
1096                let default_val = match arg.arg_type.as_str() {
1097                    "string" | "file_path" => "\"\"".to_string(),
1098                    "int" | "integer" => "0".to_string(),
1099                    "float" | "number" => "0.0d".to_string(),
1100                    "bool" | "boolean" => "false".to_string(),
1101                    _ => "null".to_string(),
1102                };
1103                parts.push(default_val);
1104            }
1105            Some(v) => {
1106                if arg.arg_type == "json_object" {
1107                    // Array json_object args: emit inline Java list expression.
1108                    // Check for batch item arrays first (element_type = BatchBytesItem/BatchFileItem).
1109                    if v.is_array() {
1110                        if let Some(elem_type) = &arg.element_type {
1111                            if elem_type == "BatchBytesItem" || elem_type == "BatchFileItem" {
1112                                parts.push(emit_java_batch_item_array(v, elem_type));
1113                                continue;
1114                            }
1115                        }
1116                        // Otherwise use element_type to emit the correct numeric literal suffix (f vs d).
1117                        let elem_type = arg.element_type.as_deref();
1118                        parts.push(json_to_java_typed(v, elem_type));
1119                        continue;
1120                    }
1121                    // Object json_object args with options_type: use pre-deserialized variable.
1122                    if options_type.is_some() {
1123                        parts.push(arg.name.clone());
1124                        continue;
1125                    }
1126                    parts.push(json_to_java(v));
1127                    continue;
1128                }
1129                // bytes args must be passed as byte[], not String.
1130                if arg.arg_type == "bytes" {
1131                    let val = json_to_java(v);
1132                    parts.push(format!("{val}.getBytes()"));
1133                    continue;
1134                }
1135                // file_path args must be wrapped in java.nio.file.Path.of().
1136                if arg.arg_type == "file_path" {
1137                    let val = json_to_java(v);
1138                    parts.push(format!("java.nio.file.Path.of({val})"));
1139                    continue;
1140                }
1141                parts.push(json_to_java(v));
1142            }
1143        }
1144    }
1145
1146    (setup_lines, parts.join(", "))
1147}
1148
1149#[allow(clippy::too_many_arguments)]
1150fn render_assertion(
1151    out: &mut String,
1152    assertion: &Assertion,
1153    result_var: &str,
1154    class_name: &str,
1155    field_resolver: &FieldResolver,
1156    result_is_simple: bool,
1157    result_is_bytes: bool,
1158    enum_fields: &std::collections::HashMap<String, String>,
1159) {
1160    // Byte-buffer returns: emit length-based assertions instead of struct-field
1161    // accessors. The result is `byte[]`, which has no `isEmpty()`/struct-field methods.
1162    // Field paths on byte-buffer results (e.g. `audio`, `content`) are pseudo-fields
1163    // referencing the buffer itself — treat them the same as no-field assertions.
1164    if result_is_bytes {
1165        match assertion.assertion_type.as_str() {
1166            "not_empty" => {
1167                out.push_str(&format!(
1168                    "        assertTrue({result_var}.length > 0, \"expected non-empty value\");\n"
1169                ));
1170                return;
1171            }
1172            "is_empty" => {
1173                out.push_str(&format!(
1174                    "        assertEquals(0, {result_var}.length, \"expected empty value\");\n"
1175                ));
1176                return;
1177            }
1178            "count_equals" | "length_equals" => {
1179                if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1180                    out.push_str(&format!("        assertEquals({n}, {result_var}.length);\n"));
1181                }
1182                return;
1183            }
1184            "count_min" | "length_min" => {
1185                if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1186                    out.push_str(&format!(
1187                        "        assertTrue({result_var}.length >= {n}, \"expected length >= {n}\");\n"
1188                    ));
1189                }
1190                return;
1191            }
1192            _ => {
1193                out.push_str(&format!(
1194                    "        // skipped: assertion type '{}' not supported on byte[] result\n",
1195                    assertion.assertion_type
1196                ));
1197                return;
1198            }
1199        }
1200    }
1201
1202    // Handle synthetic/virtual fields that are computed rather than direct record accessors.
1203    if let Some(f) = &assertion.field {
1204        match f.as_str() {
1205            // ---- ExtractionResult chunk-level computed predicates ----
1206            "chunks_have_content" => {
1207                let pred = format!(
1208                    "{result_var}.chunks().orElse(java.util.List.of()).stream().allMatch(c -> c.content() != null && !c.content().isBlank())"
1209                );
1210                out.push_str(&crate::template_env::render(
1211                    "java/synthetic_assertion.jinja",
1212                    minijinja::context! {
1213                        assertion_kind => "chunks_content",
1214                        assertion_type => assertion.assertion_type.as_str(),
1215                        pred => pred,
1216                        field_name => f,
1217                    },
1218                ));
1219                return;
1220            }
1221            "chunks_have_heading_context" => {
1222                let pred = format!(
1223                    "{result_var}.chunks().orElse(java.util.List.of()).stream().allMatch(c -> c.metadata().headingContext().isPresent())"
1224                );
1225                out.push_str(&crate::template_env::render(
1226                    "java/synthetic_assertion.jinja",
1227                    minijinja::context! {
1228                        assertion_kind => "chunks_heading_context",
1229                        assertion_type => assertion.assertion_type.as_str(),
1230                        pred => pred,
1231                        field_name => f,
1232                    },
1233                ));
1234                return;
1235            }
1236            "chunks_have_embeddings" => {
1237                let pred = format!(
1238                    "{result_var}.chunks().orElse(java.util.List.of()).stream().allMatch(c -> c.embedding() != null && !c.embedding().isEmpty())"
1239                );
1240                out.push_str(&crate::template_env::render(
1241                    "java/synthetic_assertion.jinja",
1242                    minijinja::context! {
1243                        assertion_kind => "chunks_embeddings",
1244                        assertion_type => assertion.assertion_type.as_str(),
1245                        pred => pred,
1246                        field_name => f,
1247                    },
1248                ));
1249                return;
1250            }
1251            "first_chunk_starts_with_heading" => {
1252                let pred = format!(
1253                    "{result_var}.chunks().orElse(java.util.List.of()).stream().findFirst().map(c -> c.metadata().headingContext().isPresent()).orElse(false)"
1254                );
1255                out.push_str(&crate::template_env::render(
1256                    "java/synthetic_assertion.jinja",
1257                    minijinja::context! {
1258                        assertion_kind => "first_chunk_heading",
1259                        assertion_type => assertion.assertion_type.as_str(),
1260                        pred => pred,
1261                        field_name => f,
1262                    },
1263                ));
1264                return;
1265            }
1266            // ---- EmbedResponse virtual fields ----
1267            // When result_is_simple=true the result IS List<List<Float>> (the raw embeddings list).
1268            // When result_is_simple=false the result has an .embeddings() accessor.
1269            "embedding_dimensions" => {
1270                // Dimension = size of the first embedding vector in the list.
1271                let embed_list = if result_is_simple {
1272                    result_var.to_string()
1273                } else {
1274                    format!("{result_var}.embeddings()")
1275                };
1276                let expr = format!("({embed_list}.isEmpty() ? 0 : {embed_list}.get(0).size())");
1277                let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
1278                out.push_str(&crate::template_env::render(
1279                    "java/synthetic_assertion.jinja",
1280                    minijinja::context! {
1281                        assertion_kind => "embedding_dimensions",
1282                        assertion_type => assertion.assertion_type.as_str(),
1283                        expr => expr,
1284                        java_val => java_val,
1285                        field_name => f,
1286                    },
1287                ));
1288                return;
1289            }
1290            "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
1291                // These are validation predicates that require iterating the embedding matrix.
1292                let embed_list = if result_is_simple {
1293                    result_var.to_string()
1294                } else {
1295                    format!("{result_var}.embeddings()")
1296                };
1297                let pred = match f.as_str() {
1298                    "embeddings_valid" => {
1299                        format!("{embed_list}.stream().allMatch(e -> e != null && !e.isEmpty())")
1300                    }
1301                    "embeddings_finite" => {
1302                        format!("{embed_list}.stream().flatMap(java.util.Collection::stream).allMatch(Float::isFinite)")
1303                    }
1304                    "embeddings_non_zero" => {
1305                        format!("{embed_list}.stream().allMatch(e -> e.stream().anyMatch(v -> v != 0.0f))")
1306                    }
1307                    "embeddings_normalized" => format!(
1308                        "{embed_list}.stream().allMatch(e -> {{ double n = e.stream().mapToDouble(v -> v * v).sum(); return Math.abs(n - 1.0) < 1e-3; }})"
1309                    ),
1310                    _ => unreachable!(),
1311                };
1312                let assertion_kind = format!("embeddings_{}", f.strip_prefix("embeddings_").unwrap_or(f));
1313                out.push_str(&crate::template_env::render(
1314                    "java/synthetic_assertion.jinja",
1315                    minijinja::context! {
1316                        assertion_kind => assertion_kind,
1317                        assertion_type => assertion.assertion_type.as_str(),
1318                        pred => pred,
1319                        field_name => f,
1320                    },
1321                ));
1322                return;
1323            }
1324            // ---- Fields not present on the Java ExtractionResult ----
1325            "keywords" | "keywords_count" => {
1326                out.push_str(&crate::template_env::render(
1327                    "java/synthetic_assertion.jinja",
1328                    minijinja::context! {
1329                        assertion_kind => "keywords",
1330                        field_name => f,
1331                    },
1332                ));
1333                return;
1334            }
1335            // ---- metadata not_empty / is_empty: Metadata is a required record, not Optional ----
1336            // Metadata has no .isEmpty() method; check that at least one optional field is present.
1337            "metadata" => {
1338                match assertion.assertion_type.as_str() {
1339                    "not_empty" | "is_empty" => {
1340                        out.push_str(&crate::template_env::render(
1341                            "java/synthetic_assertion.jinja",
1342                            minijinja::context! {
1343                                assertion_kind => "metadata",
1344                                assertion_type => assertion.assertion_type.as_str(),
1345                                result_var => result_var,
1346                            },
1347                        ));
1348                        return;
1349                    }
1350                    _ => {} // fall through to normal handling
1351                }
1352            }
1353            _ => {}
1354        }
1355    }
1356
1357    // Skip assertions on fields that don't exist on the result type.
1358    if let Some(f) = &assertion.field {
1359        if !f.is_empty() && !field_resolver.is_valid_for_result(f) {
1360            out.push_str(&crate::template_env::render(
1361                "java/synthetic_assertion.jinja",
1362                minijinja::context! {
1363                    assertion_kind => "skipped",
1364                    field_name => f,
1365                },
1366            ));
1367            return;
1368        }
1369    }
1370
1371    // Determine if this field is an enum type (no `.contains()` on enums in Java).
1372    // Check both the raw fixture field path and the resolved (aliased) path so that
1373    // `fields_enum` entries can use either form (e.g., `"assets[].category"` or the
1374    // resolved `"assets[].asset_category"`).
1375    let field_is_enum = assertion
1376        .field
1377        .as_deref()
1378        .is_some_and(|f| enum_fields.contains_key(f) || enum_fields.contains_key(field_resolver.resolve(f)));
1379
1380    // Determine if this field is an array (List<T>) — needed to choose .toString() for
1381    // contains assertions, since List.contains(Object) uses equals() which won't match
1382    // strings against complex record types like StructureItem.
1383    let field_is_array = assertion
1384        .field
1385        .as_deref()
1386        .is_some_and(|f| field_resolver.is_array(field_resolver.resolve(f)));
1387
1388    let field_expr = if result_is_simple {
1389        result_var.to_string()
1390    } else {
1391        match &assertion.field {
1392            Some(f) if !f.is_empty() => {
1393                let accessor = field_resolver.accessor(f, "java", result_var);
1394                let resolved = field_resolver.resolve(f);
1395                // Unwrap Optional fields with a type-appropriate fallback.
1396                // Map.get() returns nullable, not Optional, so skip .orElse() for map access.
1397                // NOTE: is_optional() means the field is in optional_fields, but that doesn't
1398                // guarantee it returns Optional<T> in Java — nested fields like metadata.twitterCard
1399                // return @Nullable String, not Optional<String>. We detect this by checking
1400                // if the field path contains a dot (nested access).
1401                if field_resolver.is_optional(resolved) && !field_resolver.has_map_access(f) {
1402                    // All nullable fields in the Java binding return @Nullable types, not Optional<T>.
1403                    // Wrap them in Optional.ofNullable() so e2e tests can use .orElse() fallbacks.
1404                    let optional_expr = format!("java.util.Optional.ofNullable({accessor})");
1405                    // Enum-typed optional fields need .map(v -> v.getValue()) to coerce to String
1406                    // before the orElse("") fallback can type-check (Optional<Enum>.orElse("") would
1407                    // be a type mismatch — Optional<String>.orElse("") is the only safe form).
1408                    if field_is_enum {
1409                        match assertion.assertion_type.as_str() {
1410                            "not_empty" | "is_empty" => optional_expr,
1411                            _ => format!("{optional_expr}.map(v -> v.getValue()).orElse(\"\")"),
1412                        }
1413                    } else {
1414                        match assertion.assertion_type.as_str() {
1415                            // For not_empty / is_empty on Optional fields, return the raw Optional
1416                            // so the assertion arms can call isPresent()/isEmpty().
1417                            "not_empty" | "is_empty" => optional_expr,
1418                            // For size/count assertions on Optional<List<T>> fields, use List.of() fallback.
1419                            "count_min" | "count_equals" => {
1420                                format!("{optional_expr}.orElse(java.util.List.of())")
1421                            }
1422                            // For numeric comparisons on Optional<Long/Integer> fields, use 0L.
1423                            "greater_than" | "less_than" | "greater_than_or_equal" | "less_than_or_equal" => {
1424                                if field_resolver.is_array(resolved) {
1425                                    format!("{optional_expr}.orElse(java.util.List.of())")
1426                                } else {
1427                                    format!("{optional_expr}.orElse(0L)")
1428                                }
1429                            }
1430                            // For equals on Optional fields, determine fallback based on whether value is numeric.
1431                            // If the fixture value is a number, use 0L; otherwise use "".
1432                            "equals" => {
1433                                if let Some(expected) = &assertion.value {
1434                                    if expected.is_number() {
1435                                        format!("{optional_expr}.orElse(0L)")
1436                                    } else {
1437                                        format!("{optional_expr}.orElse(\"\")")
1438                                    }
1439                                } else {
1440                                    format!("{optional_expr}.orElse(\"\")")
1441                                }
1442                            }
1443                            _ if field_resolver.is_array(resolved) => {
1444                                format!("{optional_expr}.orElse(java.util.List.of())")
1445                            }
1446                            _ => format!("{optional_expr}.orElse(\"\")"),
1447                        }
1448                    }
1449                } else {
1450                    accessor
1451                }
1452            }
1453            _ => result_var.to_string(),
1454        }
1455    };
1456
1457    // For enum fields, string-based assertions need .getValue() to convert the enum to
1458    // its serde-serialized lowercase string value (e.g., AssetCategory.Image -> "image").
1459    // All alef-generated Java enums expose a getValue() method annotated with @JsonValue.
1460    // Optional enum fields are already coerced to String via `.map(v -> v.getValue()).orElse("")`
1461    // upstream in field_expr; in that case the value is already a String and we must not
1462    // call .getValue() again. Detect by looking for `.map(v -> v.getValue())` in the expr.
1463    let string_expr = if field_is_enum && !field_expr.contains(".map(v -> v.getValue())") {
1464        format!("{field_expr}.getValue()")
1465    } else {
1466        field_expr.clone()
1467    };
1468
1469    // Pre-compute context for template
1470    let assertion_type = assertion.assertion_type.as_str();
1471    let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
1472    let is_string_val = assertion.value.as_ref().is_some_and(|v| v.is_string());
1473    let is_numeric_val = assertion.value.as_ref().is_some_and(|v| v.is_number());
1474
1475    let values_java: Vec<String> = assertion
1476        .values
1477        .as_ref()
1478        .map(|values| values.iter().map(json_to_java).collect())
1479        .unwrap_or_default();
1480
1481    let contains_any_expr = if !values_java.is_empty() {
1482        values_java
1483            .iter()
1484            .map(|v| format!("{string_expr}.contains({v})"))
1485            .collect::<Vec<_>>()
1486            .join(" || ")
1487    } else {
1488        String::new()
1489    };
1490
1491    let length_expr = if result_is_bytes {
1492        format!("{field_expr}.length")
1493    } else {
1494        format!("{field_expr}.length()")
1495    };
1496
1497    let n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
1498
1499    let call_expr = if let Some(method_name) = &assertion.method {
1500        build_java_method_call(result_var, method_name, assertion.args.as_ref(), class_name)
1501    } else {
1502        String::new()
1503    };
1504
1505    let check = assertion.check.as_deref().unwrap_or("is_true");
1506
1507    let java_check_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
1508
1509    let check_n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
1510
1511    let is_bool_val = assertion.value.as_ref().is_some_and(|v| v.is_boolean());
1512    let bool_is_true = assertion.value.as_ref().is_some_and(|v| v.as_bool() == Some(true));
1513
1514    let method_returns_collection = assertion
1515        .method
1516        .as_ref()
1517        .is_some_and(|m| matches!(m.as_str(), "find_nodes_by_type" | "findNodesByType"));
1518
1519    let rendered = crate::template_env::render(
1520        "java/assertion.jinja",
1521        minijinja::context! {
1522            assertion_type,
1523            java_val,
1524            string_expr,
1525            field_expr,
1526            field_is_enum,
1527            field_is_array,
1528            is_string_val,
1529            is_numeric_val,
1530            values_java => values_java,
1531            contains_any_expr,
1532            length_expr,
1533            n,
1534            call_expr,
1535            check,
1536            java_check_val,
1537            check_n,
1538            is_bool_val,
1539            bool_is_true,
1540            method_returns_collection,
1541        },
1542    );
1543    out.push_str(&rendered);
1544}
1545
1546/// Build a Java call expression for a `method_result` assertion on a tree-sitter Tree.
1547///
1548/// Maps method names to the appropriate Java static/instance method calls.
1549fn build_java_method_call(
1550    result_var: &str,
1551    method_name: &str,
1552    args: Option<&serde_json::Value>,
1553    class_name: &str,
1554) -> String {
1555    match method_name {
1556        "root_child_count" => format!("{result_var}.rootNode().childCount()"),
1557        "root_node_type" => format!("{result_var}.rootNode().kind()"),
1558        "named_children_count" => format!("{result_var}.rootNode().namedChildCount()"),
1559        "has_error_nodes" => format!("{class_name}.treeHasErrorNodes({result_var})"),
1560        "error_count" | "tree_error_count" => format!("{class_name}.treeErrorCount({result_var})"),
1561        "tree_to_sexp" => format!("{class_name}.treeToSexp({result_var})"),
1562        "contains_node_type" => {
1563            let node_type = args
1564                .and_then(|a| a.get("node_type"))
1565                .and_then(|v| v.as_str())
1566                .unwrap_or("");
1567            format!("{class_name}.treeContainsNodeType({result_var}, \"{node_type}\")")
1568        }
1569        "find_nodes_by_type" => {
1570            let node_type = args
1571                .and_then(|a| a.get("node_type"))
1572                .and_then(|v| v.as_str())
1573                .unwrap_or("");
1574            format!("{class_name}.findNodesByType({result_var}, \"{node_type}\")")
1575        }
1576        "run_query" => {
1577            let query_source = args
1578                .and_then(|a| a.get("query_source"))
1579                .and_then(|v| v.as_str())
1580                .unwrap_or("");
1581            let language = args
1582                .and_then(|a| a.get("language"))
1583                .and_then(|v| v.as_str())
1584                .unwrap_or("");
1585            let escaped_query = escape_java(query_source);
1586            format!("{class_name}.runQuery({result_var}, \"{language}\", \"{escaped_query}\", source)")
1587        }
1588        _ => {
1589            format!("{result_var}.{}()", method_name.to_lower_camel_case())
1590        }
1591    }
1592}
1593
1594/// Convert a `serde_json::Value` to a Java literal string.
1595fn json_to_java(value: &serde_json::Value) -> String {
1596    json_to_java_typed(value, None)
1597}
1598
1599/// Convert a JSON value to a Java literal, optionally overriding number type for array elements.
1600/// `element_type` controls how numeric array elements are emitted: "f32" → `1.0f`, otherwise `1.0d`.
1601/// Emit Java batch item constructors for BatchBytesItem or BatchFileItem arrays.
1602fn emit_java_batch_item_array(arr: &serde_json::Value, elem_type: &str) -> String {
1603    if let Some(items) = arr.as_array() {
1604        let item_strs: Vec<String> = items
1605            .iter()
1606            .filter_map(|item| {
1607                if let Some(obj) = item.as_object() {
1608                    match elem_type {
1609                        "BatchBytesItem" => {
1610                            let content = obj.get("content").and_then(|v| v.as_array());
1611                            let mime_type = obj.get("mime_type").and_then(|v| v.as_str()).unwrap_or("text/plain");
1612                            let content_code = if let Some(arr) = content {
1613                                let bytes: Vec<String> = arr
1614                                    .iter()
1615                                    .filter_map(|v| v.as_u64().map(|n| format!("(byte) {}", n)))
1616                                    .collect();
1617                                format!("new byte[] {{{}}}", bytes.join(", "))
1618                            } else {
1619                                "new byte[] {}".to_string()
1620                            };
1621                            Some(format!("new {}({}, \"{}\", null)", elem_type, content_code, mime_type))
1622                        }
1623                        "BatchFileItem" => {
1624                            let path = obj.get("path").and_then(|v| v.as_str()).unwrap_or("");
1625                            Some(format!(
1626                                "new {}(java.nio.file.Paths.get(\"{}\"), null)",
1627                                elem_type, path
1628                            ))
1629                        }
1630                        _ => None,
1631                    }
1632                } else {
1633                    None
1634                }
1635            })
1636            .collect();
1637        format!("java.util.Arrays.asList({})", item_strs.join(", "))
1638    } else {
1639        "java.util.List.of()".to_string()
1640    }
1641}
1642
1643fn json_to_java_typed(value: &serde_json::Value, element_type: Option<&str>) -> String {
1644    match value {
1645        serde_json::Value::String(s) => format!("\"{}\"", escape_java(s)),
1646        serde_json::Value::Bool(b) => b.to_string(),
1647        serde_json::Value::Number(n) => {
1648            if n.is_f64() {
1649                match element_type {
1650                    Some("f32" | "float" | "Float") => format!("{}f", n),
1651                    _ => format!("{}d", n),
1652                }
1653            } else {
1654                n.to_string()
1655            }
1656        }
1657        serde_json::Value::Null => "null".to_string(),
1658        serde_json::Value::Array(arr) => {
1659            let items: Vec<String> = arr.iter().map(|v| json_to_java_typed(v, element_type)).collect();
1660            format!("java.util.List.of({})", items.join(", "))
1661        }
1662        serde_json::Value::Object(_) => {
1663            let json_str = serde_json::to_string(value).unwrap_or_default();
1664            format!("\"{}\"", escape_java(&json_str))
1665        }
1666    }
1667}
1668
1669/// Generate a Java builder expression for a JSON object.
1670/// E.g., `obj = {"language": "abl", "chunk_max_size": 50}`
1671/// becomes: `TypeName.builder().withLanguage("abl").withChunkMaxSize(50L).build()`
1672///
1673/// For enums: emit `EnumType.VariantName` (detected via camelCase lookup in enum_fields)
1674/// For strings and bools: use the value directly
1675/// For plain numbers: emit the literal with type suffix (long uses L, double uses d)
1676/// For nested objects: recurse with Options suffix
1677/// When `nested_types_optional` is false, nested builders are passed directly without
1678/// Optional.of() wrapping, allowing non-optional nested config types.
1679fn java_builder_expression(
1680    obj: &serde_json::Map<String, serde_json::Value>,
1681    type_name: &str,
1682    enum_fields: &std::collections::HashMap<String, String>,
1683    nested_types: &std::collections::HashMap<String, String>,
1684    nested_types_optional: bool,
1685    path_fields: &[String],
1686) -> String {
1687    let mut expr = format!("{}.builder()", type_name);
1688    for (key, val) in obj {
1689        // Convert snake_case key to camelCase for method name
1690        let camel_key = key.to_lower_camel_case();
1691        let method_name = format!("with{}", camel_key.to_upper_camel_case());
1692
1693        let java_val = match val {
1694            serde_json::Value::String(s) => {
1695                // Check if this field is an enum type by looking up in enum_fields.
1696                // enum_fields is keyed by camelCase names (e.g., "codeBlockStyle"), not snake_case.
1697                if let Some(enum_type_name) = enum_fields.get(&camel_key) {
1698                    // Enum field: use the mapped enum type name from the config
1699                    let variant_name = s.to_upper_camel_case();
1700                    format!("{}.{}", enum_type_name, variant_name)
1701                } else if camel_key == "preset" && type_name == "PreprocessingOptions" {
1702                    // Special case: preset field in PreprocessingOptions maps to PreprocessingPreset
1703                    let variant_name = s.to_upper_camel_case();
1704                    format!("PreprocessingPreset.{}", variant_name)
1705                } else if path_fields.contains(key) {
1706                    // Path field: wrap in Optional.of(java.nio.file.Path.of(...))
1707                    format!("Optional.of(java.nio.file.Path.of(\"{}\"))", escape_java(s))
1708                } else {
1709                    // String field: emit as a quoted literal
1710                    format!("\"{}\"", escape_java(s))
1711                }
1712            }
1713            serde_json::Value::Bool(b) => b.to_string(),
1714            serde_json::Value::Null => "null".to_string(),
1715            serde_json::Value::Number(n) => {
1716                // Number field: emit literal with type suffix.
1717                // Java records/classes use either `long` (primitive, not nullable) or
1718                // `Optional<Long>` (nullable). The codegen wraps in `Optional.of(...)`
1719                // by default since most options builder fields are Optional, but several
1720                // record types (e.g. SecurityLimits) use primitive `long` throughout.
1721                // Skip the wrap for: (a) known-primitive top-level fields and (b) any
1722                // method on a record type whose builder methods take primitives only.
1723                let camel_key = key.to_lower_camel_case();
1724                let is_plain_field = matches!(camel_key.as_str(), "listIndentWidth" | "wrapWidth");
1725                // Builders for typed-record nested config classes use primitives
1726                // throughout — they're not the optional-options pattern.
1727                let is_primitive_builder = matches!(type_name, "SecurityLimits" | "SecurityLimitsBuilder");
1728
1729                if is_plain_field || is_primitive_builder {
1730                    // Plain numeric field: no Optional wrapper
1731                    if n.is_f64() {
1732                        format!("{}d", n)
1733                    } else {
1734                        format!("{}L", n)
1735                    }
1736                } else {
1737                    // Optional numeric field: wrap in Optional.of()
1738                    if n.is_f64() {
1739                        format!("Optional.of({}d)", n)
1740                    } else {
1741                        format!("Optional.of({}L)", n)
1742                    }
1743                }
1744            }
1745            serde_json::Value::Array(arr) => {
1746                let items: Vec<String> = arr.iter().map(|v| json_to_java_typed(v, None)).collect();
1747                format!("java.util.List.of({})", items.join(", "))
1748            }
1749            serde_json::Value::Object(nested) => {
1750                // Recurse with the type from nested_types mapping, or default to snake_case → PascalCase + "Options".
1751                let nested_type = nested_types
1752                    .get(key.as_str())
1753                    .cloned()
1754                    .unwrap_or_else(|| format!("{}Options", key.to_upper_camel_case()));
1755                let inner = java_builder_expression(
1756                    nested,
1757                    &nested_type,
1758                    enum_fields,
1759                    nested_types,
1760                    nested_types_optional,
1761                    &[],
1762                );
1763                // Top-level config builders (e.g. ExtractionConfigBuilder) declare nested
1764                // record fields as `Optional<T>` (since they are nullable). Primitive-fields
1765                // builders (SecurityLimitsBuilder etc.) take the bare type directly.
1766                let is_primitive_builder = matches!(type_name, "SecurityLimits" | "SecurityLimitsBuilder");
1767                if is_primitive_builder || !nested_types_optional {
1768                    inner
1769                } else {
1770                    format!("Optional.of({inner})")
1771                }
1772            }
1773        };
1774        expr.push_str(&format!(".{}({})", method_name, java_val));
1775    }
1776    expr.push_str(".build()");
1777    expr
1778}
1779
1780/// Build default nested type mappings for Java extraction config types.
1781///
1782/// Maps known Kreuzberg/Kreuzcrawl config field names (in snake_case) to their
1783/// Java record type names (in PascalCase). These defaults allow e2e codegen to
1784/// automatically deserialize nested config objects without requiring explicit
1785/// configuration in alef.toml. User-provided overrides take precedence.
1786fn default_java_nested_types() -> std::collections::HashMap<String, String> {
1787    [
1788        ("chunking", "ChunkingConfig"),
1789        ("ocr", "OcrConfig"),
1790        ("images", "ImageExtractionConfig"),
1791        ("html_output", "HtmlOutputConfig"),
1792        ("language_detection", "LanguageDetectionConfig"),
1793        ("postprocessor", "PostProcessorConfig"),
1794        ("acceleration", "AccelerationConfig"),
1795        ("email", "EmailConfig"),
1796        ("pages", "PageConfig"),
1797        ("pdf_options", "PdfConfig"),
1798        ("layout", "LayoutDetectionConfig"),
1799        ("tree_sitter", "TreeSitterConfig"),
1800        ("structured_extraction", "StructuredExtractionConfig"),
1801        ("content_filter", "ContentFilterConfig"),
1802        ("token_reduction", "TokenReductionOptions"),
1803        ("security_limits", "SecurityLimits"),
1804    ]
1805    .iter()
1806    .map(|(k, v)| (k.to_string(), v.to_string()))
1807    .collect()
1808}
1809
1810// ---------------------------------------------------------------------------
1811// Import collection helpers
1812// ---------------------------------------------------------------------------
1813
1814/// Recursively collect enum types and nested option types used in a builder expression.
1815/// Enums are keyed in the enum_fields map by camelCase names (e.g., "codeBlockStyle" → "CodeBlockStyle").
1816fn collect_enum_and_nested_types(
1817    obj: &serde_json::Map<String, serde_json::Value>,
1818    enum_fields: &std::collections::HashMap<String, String>,
1819    types_out: &mut std::collections::BTreeSet<String>,
1820) {
1821    for (key, val) in obj {
1822        // enum_fields is keyed by camelCase, not snake_case.
1823        let camel_key = key.to_lower_camel_case();
1824        if let Some(enum_type) = enum_fields.get(&camel_key) {
1825            // Add the enum type from the mapping (e.g., "CodeBlockStyle").
1826            types_out.insert(enum_type.clone());
1827        } else if camel_key == "preset" {
1828            // Special case: preset field uses PreprocessingPreset enum.
1829            types_out.insert("PreprocessingPreset".to_string());
1830        }
1831        // Recurse into nested objects to find their nested enum types.
1832        if let Some(nested) = val.as_object() {
1833            collect_enum_and_nested_types(nested, enum_fields, types_out);
1834        }
1835    }
1836}
1837
1838fn collect_nested_type_names(
1839    obj: &serde_json::Map<String, serde_json::Value>,
1840    nested_types: &std::collections::HashMap<String, String>,
1841    types_out: &mut std::collections::BTreeSet<String>,
1842) {
1843    for (key, val) in obj {
1844        if let Some(type_name) = nested_types.get(key.as_str()) {
1845            types_out.insert(type_name.clone());
1846        }
1847        if let Some(nested) = val.as_object() {
1848            collect_nested_type_names(nested, nested_types, types_out);
1849        }
1850    }
1851}
1852
1853// ---------------------------------------------------------------------------
1854// Visitor generation
1855// ---------------------------------------------------------------------------
1856
1857/// Build a Java visitor class and add setup lines. Returns the visitor variable name.
1858fn build_java_visitor(
1859    setup_lines: &mut Vec<String>,
1860    visitor_spec: &crate::fixture::VisitorSpec,
1861    class_name: &str,
1862) -> String {
1863    setup_lines.push("class _TestVisitor implements Visitor {".to_string());
1864    for (method_name, action) in &visitor_spec.callbacks {
1865        emit_java_visitor_method(setup_lines, method_name, action, class_name);
1866    }
1867    setup_lines.push("}".to_string());
1868    setup_lines.push("var visitor = new _TestVisitor();".to_string());
1869    "visitor".to_string()
1870}
1871
1872/// Emit a Java visitor method for a callback action.
1873fn emit_java_visitor_method(
1874    setup_lines: &mut Vec<String>,
1875    method_name: &str,
1876    action: &CallbackAction,
1877    _class_name: &str,
1878) {
1879    let camel_method = method_to_camel(method_name);
1880    let params = match method_name {
1881        "visit_link" => "NodeContext ctx, String href, String text, String title",
1882        "visit_image" => "NodeContext ctx, String src, String alt, String title",
1883        "visit_heading" => "NodeContext ctx, int level, String text, String id",
1884        "visit_code_block" => "NodeContext ctx, String lang, String code",
1885        "visit_code_inline"
1886        | "visit_strong"
1887        | "visit_emphasis"
1888        | "visit_strikethrough"
1889        | "visit_underline"
1890        | "visit_subscript"
1891        | "visit_superscript"
1892        | "visit_mark"
1893        | "visit_button"
1894        | "visit_summary"
1895        | "visit_figcaption"
1896        | "visit_definition_term"
1897        | "visit_definition_description" => "NodeContext ctx, String text",
1898        "visit_text" => "NodeContext ctx, String text",
1899        "visit_list_item" => "NodeContext ctx, boolean ordered, String marker, String text",
1900        "visit_blockquote" => "NodeContext ctx, String content, long depth",
1901        "visit_table_row" => "NodeContext ctx, java.util.List<String> cells, boolean isHeader",
1902        "visit_custom_element" => "NodeContext ctx, String tagName, String html",
1903        "visit_form" => "NodeContext ctx, String actionUrl, String method",
1904        "visit_input" => "NodeContext ctx, String inputType, String name, String value",
1905        "visit_audio" | "visit_video" | "visit_iframe" => "NodeContext ctx, String src",
1906        "visit_details" => "NodeContext ctx, boolean isOpen",
1907        "visit_element_end" | "visit_table_end" | "visit_definition_list_end" | "visit_figure_end" => {
1908            "NodeContext ctx, String output"
1909        }
1910        "visit_list_start" => "NodeContext ctx, boolean ordered",
1911        "visit_list_end" => "NodeContext ctx, boolean ordered, String output",
1912        _ => "NodeContext ctx",
1913    };
1914
1915    // Determine action type and values for template
1916    let (action_type, action_value, format_args) = match action {
1917        CallbackAction::Skip => ("skip", String::new(), Vec::new()),
1918        CallbackAction::Continue => ("continue", String::new(), Vec::new()),
1919        CallbackAction::PreserveHtml => ("preserve_html", String::new(), Vec::new()),
1920        CallbackAction::Custom { output } => ("custom_literal", escape_java(output), Vec::new()),
1921        CallbackAction::CustomTemplate { template } => {
1922            // Extract {placeholder} names from the template (in order of appearance).
1923            let mut format_str = String::with_capacity(template.len());
1924            let mut format_args: Vec<String> = Vec::new();
1925            let mut chars = template.chars().peekable();
1926            while let Some(ch) = chars.next() {
1927                if ch == '{' {
1928                    // Collect identifier chars until '}'.
1929                    let mut name = String::new();
1930                    let mut closed = false;
1931                    for inner in chars.by_ref() {
1932                        if inner == '}' {
1933                            closed = true;
1934                            break;
1935                        }
1936                        name.push(inner);
1937                    }
1938                    if closed && !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_') {
1939                        let camel_name = name.as_str().to_lower_camel_case();
1940                        format_args.push(camel_name);
1941                        format_str.push_str("%s");
1942                    } else {
1943                        // Not a simple placeholder — emit literally.
1944                        format_str.push('{');
1945                        format_str.push_str(&name);
1946                        if closed {
1947                            format_str.push('}');
1948                        }
1949                    }
1950                } else {
1951                    format_str.push(ch);
1952                }
1953            }
1954            let escaped = escape_java(&format_str);
1955            if format_args.is_empty() {
1956                ("custom_literal", escaped, Vec::new())
1957            } else {
1958                ("custom_formatted", escaped, format_args)
1959            }
1960        }
1961    };
1962
1963    let params = params.to_string();
1964
1965    let rendered = crate::template_env::render(
1966        "java/visitor_method.jinja",
1967        minijinja::context! {
1968            camel_method,
1969            params,
1970            action_type,
1971            action_value,
1972            format_args => format_args,
1973        },
1974    );
1975    setup_lines.push(rendered);
1976}
1977
1978/// Convert snake_case method names to Java camelCase.
1979fn method_to_camel(snake: &str) -> String {
1980    snake.to_lower_camel_case()
1981}