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