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