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