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