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