Skip to main content

alef_e2e/codegen/
elixir.rs

1//! Elixir e2e test generator using ExUnit.
2
3use crate::config::E2eConfig;
4use crate::escape::{escape_elixir, sanitize_filename, sanitize_ident};
5use crate::field_access::FieldResolver;
6use crate::fixture::{Assertion, CallbackAction, Fixture, FixtureGroup, HttpFixture, ValidationErrorExpectation};
7use alef_core::backend::GeneratedFile;
8use alef_core::config::ResolvedCrateConfig;
9use alef_core::hash::{self, CommentStyle};
10use alef_core::template_versions as tv;
11use anyhow::Result;
12use heck::ToSnakeCase;
13use std::collections::HashMap;
14use std::fmt::Write as FmtWrite;
15use std::path::PathBuf;
16
17use super::E2eCodegen;
18use super::client;
19
20/// Elixir e2e code generator.
21pub struct ElixirCodegen;
22
23impl E2eCodegen for ElixirCodegen {
24    fn generate(
25        &self,
26        groups: &[FixtureGroup],
27        e2e_config: &E2eConfig,
28        config: &ResolvedCrateConfig,
29        _type_defs: &[alef_core::ir::TypeDef],
30        _enums: &[alef_core::ir::EnumDef],
31    ) -> Result<Vec<GeneratedFile>> {
32        let lang = self.language_name();
33        let output_base = PathBuf::from(e2e_config.effective_output()).join(lang);
34
35        let mut files = Vec::new();
36
37        // Resolve call config with overrides.
38        let call = &e2e_config.call;
39        let overrides = call.overrides.get(lang);
40        let raw_module = overrides
41            .and_then(|o| o.module.as_ref())
42            .cloned()
43            .unwrap_or_else(|| call.module.clone());
44        // Convert module path to Elixir PascalCase if it looks like snake_case
45        // (e.g., "html_to_markdown" -> "HtmlToMarkdown").
46        // If the override already contains "." (e.g., "Elixir.HtmlToMarkdown"), use as-is.
47        let module_path = if raw_module.contains('.') || raw_module.chars().next().is_some_and(|c| c.is_uppercase()) {
48            raw_module.clone()
49        } else {
50            elixir_module_name(&raw_module)
51        };
52        let base_function_name = overrides
53            .and_then(|o| o.function.as_ref())
54            .cloned()
55            .unwrap_or_else(|| call.function.clone());
56        // Elixir facade exports async variants with `_async` suffix when the call is async.
57        // Append the suffix only if not already present and the function isn't a streaming
58        // entry-point — streaming wrappers (e.g. `defaultclient_chat_stream`) drive the
59        // FFI iterator handle and aren't async-callable in the OpenAI sense.
60        let function_name =
61            if call.r#async && !base_function_name.ends_with("_async") && !base_function_name.ends_with("_stream") {
62                format!("{base_function_name}_async")
63            } else {
64                base_function_name
65            };
66        let options_type = overrides.and_then(|o| o.options_type.clone());
67        let options_default_fn = overrides.and_then(|o| o.options_via.clone());
68        let empty_enum_fields = HashMap::new();
69        let enum_fields = overrides.map(|o| &o.enum_fields).unwrap_or(&empty_enum_fields);
70        let handle_struct_type = overrides.and_then(|o| o.handle_struct_type.clone());
71        let empty_atom_fields = std::collections::HashSet::new();
72        let handle_atom_list_fields = overrides
73            .map(|o| &o.handle_atom_list_fields)
74            .unwrap_or(&empty_atom_fields);
75        let result_var = &call.result_var;
76
77        // Check if any fixture in any group is an HTTP test.
78        let has_http_tests = groups.iter().any(|g| g.fixtures.iter().any(|f| f.is_http_test()));
79        let has_nif_tests = groups.iter().any(|g| g.fixtures.iter().any(|f| !f.is_http_test()));
80        // Check if any fixture needs the mock server (either via http or mock_response or client_factory).
81        let has_mock_server_tests = groups.iter().any(|g| {
82            g.fixtures.iter().any(|f| {
83                if f.needs_mock_server() {
84                    return true;
85                }
86                let cc = e2e_config.resolve_call_for_fixture(
87                    f.call.as_deref(),
88                    &f.id,
89                    &f.resolved_category(),
90                    &f.tags,
91                    &f.input,
92                );
93                let elixir_override = cc
94                    .overrides
95                    .get("elixir")
96                    .or_else(|| e2e_config.call.overrides.get("elixir"));
97                elixir_override.and_then(|o| o.client_factory.as_deref()).is_some()
98            })
99        });
100
101        // Resolve package reference (path or version) for the NIF dependency.
102        let pkg_ref = e2e_config.resolve_package(lang);
103        let pkg_dep_ref = if has_nif_tests {
104            match e2e_config.dep_mode {
105                crate::config::DependencyMode::Local => pkg_ref
106                    .as_ref()
107                    .and_then(|p| p.path.as_deref())
108                    .unwrap_or("../../packages/elixir")
109                    .to_string(),
110                crate::config::DependencyMode::Registry => pkg_ref
111                    .as_ref()
112                    .and_then(|p| p.version.clone())
113                    .or_else(|| config.resolved_version())
114                    .unwrap_or_else(|| "0.1.0".to_string()),
115            }
116        } else {
117            String::new()
118        };
119
120        // Generate mix.exs. The dep atom must match the binding package's
121        // mix `app:` value, not the crate name. Use the configured
122        // `[elixir].app_name` (the same source the package's own mix.exs
123        // uses); fall back to the crate name only when unset. Without this,
124        // mix's path-dep resolution silently misroutes — the path-dep's
125        // own deps (notably `:rustler_precompiled`) never load during its
126        // compilation and the parent build fails with `RustlerPrecompiled
127        // is not loaded`.
128        let pkg_atom = config.elixir_app_name();
129        files.push(GeneratedFile {
130            path: output_base.join("mix.exs"),
131            content: render_mix_exs(
132                &pkg_atom,
133                &pkg_dep_ref,
134                e2e_config.dep_mode,
135                has_http_tests,
136                has_nif_tests,
137            ),
138            generated_header: false,
139        });
140
141        // Generate lib/e2e_elixir.ex — required so the mix project compiles.
142        files.push(GeneratedFile {
143            path: output_base.join("lib").join("e2e_elixir.ex"),
144            content: "defmodule E2eElixir do\n  @moduledoc false\nend\n".to_string(),
145            generated_header: false,
146        });
147
148        // Generate test_helper.exs.
149        files.push(GeneratedFile {
150            path: output_base.join("test").join("test_helper.exs"),
151            content: render_test_helper(has_http_tests || has_mock_server_tests),
152            generated_header: false,
153        });
154
155        // Generate test files per category.
156        for group in groups {
157            let active: Vec<&Fixture> = group
158                .fixtures
159                .iter()
160                .filter(|f| super::should_include_fixture(f, lang, e2e_config))
161                .collect();
162
163            if active.is_empty() {
164                continue;
165            }
166
167            let filename = format!("{}_test.exs", sanitize_filename(&group.category));
168            let content = render_test_file(
169                &group.category,
170                &active,
171                e2e_config,
172                &module_path,
173                &function_name,
174                result_var,
175                &e2e_config.call.args,
176                options_type.as_deref(),
177                options_default_fn.as_deref(),
178                enum_fields,
179                handle_struct_type.as_deref(),
180                handle_atom_list_fields,
181                &config.adapters,
182            );
183            files.push(GeneratedFile {
184                path: output_base.join("test").join(filename),
185                content,
186                generated_header: true,
187            });
188        }
189
190        Ok(files)
191    }
192
193    fn language_name(&self) -> &'static str {
194        "elixir"
195    }
196}
197
198fn render_test_helper(has_http_tests: bool) -> String {
199    if has_http_tests {
200        r#"ExUnit.start()
201
202# Spawn mock-server binary and set MOCK_SERVER_URL for all tests.
203mock_server_bin = Path.expand("../../rust/target/release/mock-server", __DIR__)
204fixtures_dir = Path.expand("../../../fixtures", __DIR__)
205
206if File.exists?(mock_server_bin) do
207  port = Port.open({:spawn_executable, mock_server_bin}, [
208    :binary,
209    # Use a large line buffer (default 1024 truncates `MOCK_SERVERS={...}` lines for
210    # fixture sets with many host-root routes, splitting them into `:noeol` chunks
211    # that the prefix-match clauses below would never see).
212    {:line, 65_536},
213    args: [fixtures_dir]
214  ])
215  # Read startup lines: MOCK_SERVER_URL= then MOCK_SERVERS= (always emitted, possibly `{}`).
216  # The standalone mock-server prints noisy stderr lines BEFORE the stdout sentinels;
217  # selective receive ignores anything that doesn't match the two prefix patterns.
218  # Each iteration only halts after the MOCK_SERVERS= line is processed.
219  {url, _} =
220    Enum.reduce_while(1..16, {nil, port}, fn _, {url_acc, p} ->
221      receive do
222        {^p, {:data, {:eol, "MOCK_SERVER_URL=" <> u}}} ->
223          {:cont, {u, p}}
224
225        {^p, {:data, {:eol, "MOCK_SERVERS=" <> json_val}}} ->
226          System.put_env("MOCK_SERVERS", json_val)
227          case Jason.decode(json_val) do
228            {:ok, servers} ->
229              Enum.each(servers, fn {fid, furl} ->
230                System.put_env("MOCK_SERVER_#{String.upcase(fid)}", furl)
231              end)
232
233            _ ->
234              :ok
235          end
236
237          {:halt, {url_acc, p}}
238      after
239        30_000 ->
240          raise "mock-server startup timeout"
241      end
242    end)
243
244  if url != nil do
245    System.put_env("MOCK_SERVER_URL", url)
246  end
247end
248"#
249        .to_string()
250    } else {
251        "ExUnit.start()\n".to_string()
252    }
253}
254
255fn render_mix_exs(
256    pkg_name: &str,
257    pkg_path: &str,
258    dep_mode: crate::config::DependencyMode,
259    has_http_tests: bool,
260    has_nif_tests: bool,
261) -> String {
262    let mut out = String::new();
263    let _ = writeln!(out, "defmodule E2eElixir.MixProject do");
264    let _ = writeln!(out, "  use Mix.Project");
265    let _ = writeln!(out);
266    let _ = writeln!(out, "  def project do");
267    let _ = writeln!(out, "    [");
268    let _ = writeln!(out, "      app: :e2e_elixir,");
269    let _ = writeln!(out, "      version: \"0.1.0\",");
270    let _ = writeln!(out, "      elixir: \"~> 1.14\",");
271    let _ = writeln!(out, "      deps: deps()");
272    let _ = writeln!(out, "    ]");
273    let _ = writeln!(out, "  end");
274    let _ = writeln!(out);
275    let _ = writeln!(out, "  defp deps do");
276    let _ = writeln!(out, "    [");
277
278    // Build the list of deps, then join with commas to avoid double-commas.
279    let mut deps: Vec<String> = Vec::new();
280
281    // Add the binding NIF dependency when there are non-HTTP tests.
282    if has_nif_tests && !pkg_path.is_empty() {
283        let pkg_atom = pkg_name;
284        let nif_dep = match dep_mode {
285            crate::config::DependencyMode::Local => {
286                format!("      {{:{pkg_atom}, path: \"{pkg_path}\"}}")
287            }
288            crate::config::DependencyMode::Registry => {
289                // Registry mode: pkg_path is repurposed as the version string.
290                format!("      {{:{pkg_atom}, \"{pkg_path}\"}}")
291            }
292        };
293        deps.push(nif_dep);
294        // rustler_precompiled provides the precompiled NIF loader.
295        deps.push(format!(
296            "      {{:rustler_precompiled, \"{rp}\"}}",
297            rp = tv::hex::RUSTLER_PRECOMPILED
298        ));
299        // rustler must be a direct, non-optional dep in the consumer project for
300        // `force_build: Mix.env() in [:test, :dev]` to actually fetch the rustler hex
301        // package. With `optional: true` mix omits it when no other dep declares it as
302        // required, breaking the build-from-source path used by the e2e suite.
303        deps.push(format!(
304            "      {{:rustler, \"{rustler}\", runtime: false}}",
305            rustler = tv::hex::RUSTLER
306        ));
307    }
308
309    // Add Req + Jason for HTTP testing.
310    if has_http_tests {
311        deps.push(format!("      {{:req, \"{req}\"}}", req = tv::hex::REQ));
312        deps.push(format!("      {{:jason, \"{jason}\"}}", jason = tv::hex::JASON));
313    }
314
315    let _ = writeln!(out, "{}", deps.join(",\n"));
316    let _ = writeln!(out, "    ]");
317    let _ = writeln!(out, "  end");
318    let _ = writeln!(out, "end");
319    out
320}
321
322#[allow(clippy::too_many_arguments)]
323fn render_test_file(
324    category: &str,
325    fixtures: &[&Fixture],
326    e2e_config: &E2eConfig,
327    module_path: &str,
328    function_name: &str,
329    result_var: &str,
330    args: &[crate::config::ArgMapping],
331    options_type: Option<&str>,
332    options_default_fn: Option<&str>,
333    enum_fields: &HashMap<String, String>,
334    handle_struct_type: Option<&str>,
335    handle_atom_list_fields: &std::collections::HashSet<String>,
336    adapters: &[alef_core::config::extras::AdapterConfig],
337) -> String {
338    let mut out = String::new();
339    out.push_str(&hash::header(CommentStyle::Hash));
340    let _ = writeln!(out, "# E2e tests for category: {category}");
341    let _ = writeln!(out, "defmodule E2e.{}Test do", elixir_module_name(category));
342
343    // Add client helper when there are HTTP fixtures in this group.
344    let has_http = fixtures.iter().any(|f| f.is_http_test());
345
346    // Use async: false for NIF tests — concurrent Tokio runtimes created by DirtyCpu NIFs
347    // on ARM64 macOS cause SIGBUS when tests run in parallel. HTTP-only tests can stay async.
348    let async_flag = if has_http { "true" } else { "false" };
349    let _ = writeln!(out, "  use ExUnit.Case, async: {async_flag}");
350
351    if has_http {
352        let _ = writeln!(out);
353        let _ = writeln!(out, "  defp mock_server_url do");
354        let _ = writeln!(
355            out,
356            "    System.get_env(\"MOCK_SERVER_URL\") || \"http://localhost:8080\""
357        );
358        let _ = writeln!(out, "  end");
359    }
360
361    // Emit a shared helper for array field contains assertions — extracts string
362    // representations from each item's attributes so String.contains? works on struct lists.
363    let has_array_contains = fixtures.iter().any(|fixture| {
364        let cc = e2e_config.resolve_call_for_fixture(
365            fixture.call.as_deref(),
366            &fixture.id,
367            &fixture.resolved_category(),
368            &fixture.tags,
369            &fixture.input,
370        );
371        let fr = FieldResolver::new(
372            e2e_config.effective_fields(cc),
373            e2e_config.effective_fields_optional(cc),
374            e2e_config.effective_result_fields(cc),
375            e2e_config.effective_fields_array(cc),
376            &std::collections::HashSet::new(),
377        );
378        fixture.assertions.iter().any(|a| {
379            matches!(a.assertion_type.as_str(), "contains" | "contains_all" | "not_contains")
380                && a.field
381                    .as_deref()
382                    .is_some_and(|f| !f.is_empty() && fr.is_array(fr.resolve(f)))
383        })
384    });
385    if has_array_contains {
386        let _ = writeln!(out);
387        let _ = writeln!(out, "  defp alef_e2e_item_texts(item) when is_binary(item), do: [item]");
388        let _ = writeln!(out, "  defp alef_e2e_item_texts(item) do");
389        let _ = writeln!(out, "    [:kind, :name, :signature, :path, :alias, :text, :source]");
390        let _ = writeln!(out, "    |> Enum.filter(&Map.has_key?(item, &1))");
391        let _ = writeln!(out, "    |> Enum.flat_map(fn attr ->");
392        let _ = writeln!(out, "      case Map.get(item, attr) do");
393        let _ = writeln!(out, "        nil -> []");
394        let _ = writeln!(
395            out,
396            "        atom when is_atom(atom) -> [atom |> to_string() |> String.capitalize()]"
397        );
398        let _ = writeln!(out, "        str -> [inspect(str)]");
399        let _ = writeln!(out, "      end");
400        let _ = writeln!(out, "    end)");
401        let _ = writeln!(out, "  end");
402    }
403
404    // Emit a helper to convert FormatMetadata struct to a string representation
405    // (pattern-match on the image field and extract the format string).
406    let has_format_metadata = fixtures.iter().any(|fixture| {
407        fixture.assertions.iter().any(|a| {
408            a.field
409                .as_deref()
410                .is_some_and(|f| f.contains("format") && f.contains("metadata"))
411        })
412    });
413    if has_format_metadata {
414        let _ = writeln!(out);
415        let _ = writeln!(
416            out,
417            "  defp alef_e2e_format_to_string(value) when is_binary(value), do: value"
418        );
419        let _ = writeln!(out, "  defp alef_e2e_format_to_string(metadata) do");
420        let _ = writeln!(out, "    case metadata.image do");
421        let _ = writeln!(out, "      %{{format: fmt}} when is_binary(fmt) -> fmt");
422        let _ = writeln!(out, "      _ ->");
423        let _ = writeln!(out, "        case metadata.pdf do");
424        let _ = writeln!(out, "          %{{}} -> \"PDF\"");
425        let _ = writeln!(out, "          _ ->");
426        let _ = writeln!(out, "            case metadata.html do");
427        let _ = writeln!(out, "              %{{}} -> \"HTML\"");
428        let _ = writeln!(out, "              _ -> inspect(metadata)");
429        let _ = writeln!(out, "            end");
430        let _ = writeln!(out, "        end");
431        let _ = writeln!(out, "    end");
432        let _ = writeln!(out, "  end");
433    }
434
435    let _ = writeln!(out);
436
437    for (i, fixture) in fixtures.iter().enumerate() {
438        if let Some(http) = &fixture.http {
439            render_http_test_case(&mut out, fixture, http);
440        } else {
441            render_test_case(
442                &mut out,
443                fixture,
444                e2e_config,
445                module_path,
446                function_name,
447                result_var,
448                args,
449                options_type,
450                options_default_fn,
451                enum_fields,
452                handle_struct_type,
453                handle_atom_list_fields,
454                adapters,
455            );
456        }
457        if i + 1 < fixtures.len() {
458            let _ = writeln!(out);
459        }
460    }
461
462    let _ = writeln!(out, "end");
463    out
464}
465
466// ---------------------------------------------------------------------------
467// HTTP test rendering
468// ---------------------------------------------------------------------------
469
470/// HTTP methods that Finch (Req's underlying HTTP client) does not support.
471/// Tests using these methods are emitted with `@tag :skip` so they don't fail.
472const FINCH_UNSUPPORTED_METHODS: &[&str] = &["TRACE", "CONNECT"];
473
474/// HTTP methods that Req exposes as convenience functions.
475/// All others must be called via `Req.request(method: :METHOD, ...)`.
476const REQ_CONVENIENCE_METHODS: &[&str] = &["get", "post", "put", "patch", "delete", "head"];
477
478/// Thin renderer that emits ExUnit `describe` + `test` blocks targeting a mock
479/// server via `Req`. Satisfies [`client::TestClientRenderer`] so the shared
480/// [`client::http_call::render_http_test`] driver drives the call sequence.
481struct ElixirTestClientRenderer<'a> {
482    /// The fixture id is needed in [`render_call`] to build the mock server URL
483    /// (`mock_server_url()/fixtures/<id>`).
484    fixture_id: &'a str,
485    /// Expected response status, needed to disable Req's redirect-following for 3xx.
486    expected_status: u16,
487}
488
489impl<'a> client::TestClientRenderer for ElixirTestClientRenderer<'a> {
490    fn language_name(&self) -> &'static str {
491        "elixir"
492    }
493
494    /// Emit `describe "{fn_name}" do` + inner `test "METHOD PATH - description" do`.
495    ///
496    /// When `skip_reason` is `Some`, emit `@tag :skip` before the test block so
497    /// ExUnit skips it; the shared driver short-circuits before emitting any
498    /// assertions and then calls `render_test_close` for symmetry.
499    fn render_test_open(&self, out: &mut String, fn_name: &str, description: &str, skip_reason: Option<&str>) {
500        let escaped_description = description.replace('"', "\\\"");
501        let _ = writeln!(out, "  describe \"{fn_name}\" do");
502        if skip_reason.is_some() {
503            let _ = writeln!(out, "    @tag :skip");
504        }
505        let _ = writeln!(out, "    test \"{escaped_description}\" do");
506    }
507
508    /// Close the inner `test` block and the outer `describe` block.
509    fn render_test_close(&self, out: &mut String) {
510        let _ = writeln!(out, "    end");
511        let _ = writeln!(out, "  end");
512    }
513
514    /// Emit a `Req` request to the mock server using `mock_server_url()/fixtures/<id>`.
515    fn render_call(&self, out: &mut String, ctx: &client::CallCtx<'_>) {
516        let method = ctx.method.to_lowercase();
517        let mut opts: Vec<String> = Vec::new();
518
519        if let Some(body) = ctx.body {
520            let elixir_val = json_to_elixir(body);
521            opts.push(format!("json: {elixir_val}"));
522        }
523
524        if !ctx.headers.is_empty() {
525            let header_pairs: Vec<String> = ctx
526                .headers
527                .iter()
528                .map(|(k, v)| format!("{{\"{}\", \"{}\"}}", escape_elixir(k), escape_elixir(v)))
529                .collect();
530            opts.push(format!("headers: [{}]", header_pairs.join(", ")));
531        }
532
533        if !ctx.cookies.is_empty() {
534            let cookie_str = ctx
535                .cookies
536                .iter()
537                .map(|(k, v)| format!("{k}={v}"))
538                .collect::<Vec<_>>()
539                .join("; ");
540            opts.push(format!("headers: [{{\"cookie\", \"{}\"}}]", escape_elixir(&cookie_str)));
541        }
542
543        if !ctx.query_params.is_empty() {
544            let pairs: Vec<String> = ctx
545                .query_params
546                .iter()
547                .map(|(k, v)| {
548                    let val_str = match v {
549                        serde_json::Value::String(s) => s.clone(),
550                        other => other.to_string(),
551                    };
552                    format!("{{\"{}\", \"{}\"}}", escape_elixir(k), escape_elixir(&val_str))
553                })
554                .collect();
555            opts.push(format!("params: [{}]", pairs.join(", ")));
556        }
557
558        // When the expected response is a redirect (3xx), disable automatic redirect
559        // following so the test can assert the redirect status and Location header.
560        if (300..400).contains(&self.expected_status) {
561            opts.push("redirect: false".to_string());
562        }
563
564        let fixture_id = escape_elixir(self.fixture_id);
565        let url_expr = format!("\"#{{mock_server_url()}}/fixtures/{fixture_id}\"");
566
567        if REQ_CONVENIENCE_METHODS.contains(&method.as_str()) {
568            if opts.is_empty() {
569                let _ = writeln!(out, "      {{:ok, response}} = Req.{method}(url: {url_expr})");
570            } else {
571                let opts_str = opts.join(", ");
572                let _ = writeln!(
573                    out,
574                    "      {{:ok, response}} = Req.{method}(url: {url_expr}, {opts_str})"
575                );
576            }
577        } else {
578            opts.insert(0, format!("method: :{method}"));
579            opts.insert(1, format!("url: {url_expr}"));
580            let opts_str = opts.join(", ");
581            let _ = writeln!(out, "      {{:ok, response}} = Req.request({opts_str})");
582        }
583    }
584
585    fn render_assert_status(&self, out: &mut String, response_var: &str, status: u16) {
586        let _ = writeln!(out, "      assert {response_var}.status == {status}");
587    }
588
589    /// Emit a header assertion.
590    ///
591    /// Handles the special tokens `<<present>>`, `<<absent>>`, `<<uuid>>`.
592    /// Skips the `connection` header (hop-by-hop, stripped by Req/Mint).
593    fn render_assert_header(&self, out: &mut String, response_var: &str, name: &str, expected: &str) {
594        let header_key = name.to_lowercase();
595        // Req (via Mint) strips hop-by-hop headers; asserting on them is meaningless.
596        if header_key == "connection" {
597            return;
598        }
599        let key_lit = format!("\"{}\"", escape_elixir(&header_key));
600        let get_header_expr = format!(
601            "Enum.find_value({response_var}.headers, fn {{k, v}} -> if String.downcase(k) == {key_lit}, do: List.first(List.wrap(v)) end)"
602        );
603        match expected {
604            "<<present>>" => {
605                let _ = writeln!(out, "      assert {get_header_expr} != nil");
606            }
607            "<<absent>>" => {
608                let _ = writeln!(out, "      assert {get_header_expr} == nil");
609            }
610            "<<uuid>>" => {
611                let var = sanitize_ident(&header_key);
612                let _ = writeln!(out, "      header_val_{var} = {get_header_expr}");
613                let _ = writeln!(
614                    out,
615                    "      assert Regex.match?(~r/^[0-9a-f]{{8}}-[0-9a-f]{{4}}-[0-9a-f]{{4}}-[0-9a-f]{{4}}-[0-9a-f]{{12}}$/i, to_string(header_val_{var}))"
616                );
617            }
618            literal => {
619                let val_lit = format!("\"{}\"", escape_elixir(literal));
620                let _ = writeln!(out, "      assert {get_header_expr} == {val_lit}");
621            }
622        }
623    }
624
625    /// Emit a full JSON body equality assertion.
626    ///
627    /// Req auto-decodes `application/json` bodies; when the response body is a
628    /// binary (non-JSON content type), decode it with `Jason.decode!` first.
629    fn render_assert_json_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
630        let elixir_val = json_to_elixir(expected);
631        match expected {
632            serde_json::Value::Object(_) | serde_json::Value::Array(_) => {
633                let _ = writeln!(
634                    out,
635                    "      body_decoded = if is_binary({response_var}.body), do: Jason.decode!({response_var}.body), else: {response_var}.body"
636                );
637                let _ = writeln!(out, "      assert body_decoded == {elixir_val}");
638            }
639            _ => {
640                let _ = writeln!(out, "      assert {response_var}.body == {elixir_val}");
641            }
642        }
643    }
644
645    /// Emit partial body assertions: one assertion per key in `expected`.
646    fn render_assert_partial_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
647        if let Some(obj) = expected.as_object() {
648            let _ = writeln!(
649                out,
650                "      decoded_body = if is_binary({response_var}.body), do: Jason.decode!({response_var}.body), else: {response_var}.body"
651            );
652            for (key, val) in obj {
653                let key_lit = format!("\"{}\"", escape_elixir(key));
654                let elixir_val = json_to_elixir(val);
655                let _ = writeln!(out, "      assert decoded_body[{key_lit}] == {elixir_val}");
656            }
657        }
658    }
659
660    /// Emit validation-error assertions, checking each expected `msg` appears in
661    /// the encoded response body.
662    fn render_assert_validation_errors(
663        &self,
664        out: &mut String,
665        response_var: &str,
666        errors: &[ValidationErrorExpectation],
667    ) {
668        for err in errors {
669            let msg_lit = format!("\"{}\"", escape_elixir(&err.msg));
670            let _ = writeln!(
671                out,
672                "      assert String.contains?(Jason.encode!({response_var}.body), {msg_lit})"
673            );
674        }
675    }
676}
677
678/// Render an ExUnit `describe` + `test` block for an HTTP server test fixture.
679///
680/// Delegates to [`client::http_call::render_http_test`] after the one
681/// Elixir-specific pre-condition: HTTP methods unsupported by Finch (the
682/// underlying Req adapter) are emitted with `@tag :skip` directly.
683fn render_http_test_case(out: &mut String, fixture: &Fixture, http: &HttpFixture) {
684    let method = http.request.method.to_uppercase();
685
686    // Finch does not support TRACE/CONNECT — emit a skipped test stub directly
687    // rather than routing through the shared driver, which would assert on the
688    // response and fail.
689    if FINCH_UNSUPPORTED_METHODS.contains(&method.as_str()) {
690        let test_name = sanitize_ident(&fixture.id);
691        let test_label = fixture.id.replace('"', "\\\"");
692        let path = &http.request.path;
693        let _ = writeln!(out, "  describe \"{test_name}\" do");
694        let _ = writeln!(out, "    @tag :skip");
695        let _ = writeln!(out, "    test \"{method} {path} - {test_label}\" do");
696        let _ = writeln!(out, "    end");
697        let _ = writeln!(out, "  end");
698        return;
699    }
700
701    let renderer = ElixirTestClientRenderer {
702        fixture_id: &fixture.id,
703        expected_status: http.expected_response.status_code,
704    };
705    client::http_call::render_http_test(out, &renderer, fixture);
706}
707
708// ---------------------------------------------------------------------------
709// Function-call test rendering
710// ---------------------------------------------------------------------------
711
712#[allow(clippy::too_many_arguments)]
713fn render_test_case(
714    out: &mut String,
715    fixture: &Fixture,
716    e2e_config: &E2eConfig,
717    default_module_path: &str,
718    default_function_name: &str,
719    default_result_var: &str,
720    args: &[crate::config::ArgMapping],
721    options_type: Option<&str>,
722    options_default_fn: Option<&str>,
723    enum_fields: &HashMap<String, String>,
724    handle_struct_type: Option<&str>,
725    handle_atom_list_fields: &std::collections::HashSet<String>,
726    adapters: &[alef_core::config::extras::AdapterConfig],
727) {
728    let test_name = sanitize_ident(&fixture.id);
729    let test_label = fixture.id.replace('"', "\\\"");
730
731    // Non-HTTP non-mock_response fixtures (e.g. AsyncAPI, WebSocket, OpenRPC
732    // protocol-only fixtures) cannot be tested via the configured `[e2e.call]`
733    // function when the binding does not expose it. Emit a documented `@tag :skip`
734    // test so the suite stays compilable. HTTP fixtures dispatch via render_http_test_case
735    // and never reach here.
736    if fixture.mock_response.is_none() && !fixture_has_elixir_callable(fixture, e2e_config) {
737        let _ = writeln!(out, "  describe \"{test_name}\" do");
738        let _ = writeln!(out, "    @tag :skip");
739        let _ = writeln!(out, "    test \"{test_label}\" do");
740        let _ = writeln!(
741            out,
742            "      # non-HTTP fixture: Elixir binding does not expose a callable for the configured `[e2e.call]` function"
743        );
744        let _ = writeln!(out, "      :ok");
745        let _ = writeln!(out, "    end");
746        let _ = writeln!(out, "  end");
747        return;
748    }
749
750    // Resolve per-fixture call config (falls back to default if fixture.call is None).
751    let call_config = e2e_config.resolve_call_for_fixture(
752        fixture.call.as_deref(),
753        &fixture.id,
754        &fixture.resolved_category(),
755        &fixture.tags,
756        &fixture.input,
757    );
758    // Build per-call field resolver using the effective field sets for this call.
759    let call_field_resolver = FieldResolver::new(
760        e2e_config.effective_fields(call_config),
761        e2e_config.effective_fields_optional(call_config),
762        e2e_config.effective_result_fields(call_config),
763        e2e_config.effective_fields_array(call_config),
764        &std::collections::HashSet::new(),
765    );
766    let field_resolver = &call_field_resolver;
767    let lang = "elixir";
768    let call_overrides = call_config.overrides.get(lang);
769
770    // Check if the function is excluded from the Elixir binding (e.g., batch functions
771    // that require unsafe NIF tuple marshalling). Emit a skipped test with rationale.
772    let base_fn = call_overrides
773        .and_then(|o| o.function.as_ref())
774        .cloned()
775        .unwrap_or_else(|| call_config.function.clone());
776    if base_fn.starts_with("batch_extract_") {
777        let _ = writeln!(
778            out,
779            "  describe \"{test_name}\" do",
780            test_name = sanitize_ident(&fixture.id)
781        );
782        let _ = writeln!(out, "    @tag :skip");
783        let _ = writeln!(
784            out,
785            "    test \"{test_label}\" do",
786            test_label = fixture.id.replace('"', "\\\"")
787        );
788        let _ = writeln!(
789            out,
790            "      # batch functions excluded from Elixir binding: unsafe NIF tuple marshalling"
791        );
792        let _ = writeln!(out, "      :ok");
793        let _ = writeln!(out, "    end");
794        let _ = writeln!(out, "  end");
795        return;
796    }
797
798    // Compute module_path and function_name from the resolved call config,
799    // applying Elixir-specific PascalCase conversion.
800    let (module_path, function_name, result_var) = if fixture.call.is_some() {
801        let raw_module = call_overrides
802            .and_then(|o| o.module.as_ref())
803            .cloned()
804            .unwrap_or_else(|| call_config.module.clone());
805        let resolved_module = if raw_module.contains('.') || raw_module.chars().next().is_some_and(|c| c.is_uppercase())
806        {
807            raw_module.clone()
808        } else {
809            elixir_module_name(&raw_module)
810        };
811        let resolved_fn = if call_config.r#async && !base_fn.ends_with("_async") && !base_fn.ends_with("_stream") {
812            format!("{base_fn}_async")
813        } else {
814            base_fn
815        };
816        (resolved_module, resolved_fn, call_config.result_var.clone())
817    } else {
818        (
819            default_module_path.to_string(),
820            default_function_name.to_string(),
821            default_result_var.to_string(),
822        )
823    };
824
825    let expects_error = fixture.assertions.iter().any(|a| a.assertion_type == "error");
826    // Validation-category fixtures expect engine creation itself to fail (bad config).
827    // Other expects_error fixtures (e.g. error_*) construct a valid engine and expect the
828    // *operation under test* to fail. We need different shapes for these two cases.
829    let validation_creation_failure = expects_error && fixture.resolved_category() == "validation";
830
831    // When the fixture uses a named call, use the args and options from that call's config.
832    let (
833        effective_args,
834        effective_options_type,
835        effective_options_default_fn,
836        effective_enum_fields,
837        effective_handle_struct_type,
838        effective_handle_atom_list_fields,
839    );
840    let empty_enum_fields_local: HashMap<String, String>;
841    let empty_atom_fields_local: std::collections::HashSet<String>;
842    let (
843        resolved_args,
844        resolved_options_type,
845        resolved_options_default_fn,
846        resolved_enum_fields_ref,
847        resolved_handle_struct_type,
848        resolved_handle_atom_list_fields_ref,
849    ) = if fixture.call.is_some() {
850        let co = call_config.overrides.get(lang);
851        effective_args = call_config.args.as_slice();
852        effective_options_type = co.and_then(|o| o.options_type.as_deref());
853        effective_options_default_fn = co.and_then(|o| o.options_via.as_deref());
854        empty_enum_fields_local = HashMap::new();
855        effective_enum_fields = co.map(|o| &o.enum_fields).unwrap_or(&empty_enum_fields_local);
856        effective_handle_struct_type = co.and_then(|o| o.handle_struct_type.as_deref());
857        empty_atom_fields_local = std::collections::HashSet::new();
858        effective_handle_atom_list_fields = co
859            .map(|o| &o.handle_atom_list_fields)
860            .unwrap_or(&empty_atom_fields_local);
861        (
862            effective_args,
863            effective_options_type,
864            effective_options_default_fn,
865            effective_enum_fields,
866            effective_handle_struct_type,
867            effective_handle_atom_list_fields,
868        )
869    } else {
870        (
871            args as &[_],
872            options_type,
873            options_default_fn,
874            enum_fields,
875            handle_struct_type,
876            handle_atom_list_fields,
877        )
878    };
879
880    let test_documents_path = e2e_config.test_documents_relative_from(0);
881    let adapter_request_type: Option<String> = adapters
882        .iter()
883        .find(|a| a.name == call_config.function.as_str())
884        .and_then(|a| a.request_type.as_deref())
885        .map(|rt| rt.rsplit("::").next().unwrap_or(rt).to_string());
886    let (mut setup_lines, args_str) = build_args_and_setup(
887        &fixture.input,
888        resolved_args,
889        &module_path,
890        resolved_options_type,
891        resolved_options_default_fn,
892        resolved_enum_fields_ref,
893        fixture,
894        resolved_handle_struct_type,
895        resolved_handle_atom_list_fields_ref,
896        &test_documents_path,
897        adapter_request_type.as_deref(),
898    );
899
900    // Build visitor if present — it will be injected into the options map.
901    let visitor_var = fixture
902        .visitor
903        .as_ref()
904        .map(|visitor_spec| build_elixir_visitor(&mut setup_lines, visitor_spec));
905
906    // If we have a visitor and the args contain a nil for options, replace it with a map
907    // containing the visitor. The fixture.visitor is already set above.
908    let final_args = if let Some(ref visitor_var) = visitor_var {
909        // Parse args_str to handle injection properly.
910        // Since we're dealing with a 2-arg function (html, options), and options might be nil,
911        // we need to inject visitor into the options.
912        let parts: Vec<&str> = args_str.split(", ").collect();
913        if parts.len() == 2 && parts[1] == "nil" {
914            // Replace nil with %{visitor: visitor}
915            format!("{}, %{{visitor: {}}}", parts[0], visitor_var)
916        } else if parts.len() == 2 {
917            // Options is a variable (e.g., "options") — add visitor to it
918            setup_lines.push(format!(
919                "{} = Map.put({}, :visitor, {})",
920                parts[1], parts[1], visitor_var
921            ));
922            args_str
923        } else if parts.len() == 1 {
924            // Only HTML provided — create options map with just visitor
925            format!("{}, %{{visitor: {}}}", parts[0], visitor_var)
926        } else {
927            args_str
928        }
929    } else {
930        args_str
931    };
932
933    // Client factory: when configured, create a client and pass it as the first argument.
934    let client_factory = call_overrides.and_then(|o| o.client_factory.as_deref()).or_else(|| {
935        e2e_config
936            .call
937            .overrides
938            .get("elixir")
939            .and_then(|o| o.client_factory.as_deref())
940    });
941
942    // Append per-call extra_args (e.g. trailing `nil` for `list_files(client, query)`)
943    // so Elixir matches the binding's positional arity. Mirrors the same override the
944    // Ruby/Go/Node codegens already honor.
945    let extra_args: Vec<String> = call_overrides.map(|o| o.extra_args.clone()).unwrap_or_default();
946    let final_args_with_extras = if extra_args.is_empty() {
947        final_args
948    } else if final_args.is_empty() {
949        extra_args.join(", ")
950    } else {
951        format!("{final_args}, {}", extra_args.join(", "))
952    };
953
954    // Prefix the client variable to the args when client_factory is set.
955    let effective_args = if client_factory.is_some() {
956        if final_args_with_extras.is_empty() {
957            "client".to_string()
958        } else {
959            format!("client, {final_args_with_extras}")
960        }
961    } else {
962        final_args_with_extras
963    };
964
965    // Real-API smoke fixtures (no mock_response, no http) must be env-gated on the
966    // configured `env.api_key_var` so absent keys yield a deterministic skip rather
967    // than a spurious "no mock route" failure. Mirrors the Python conftest skip.
968    let has_mock = fixture.mock_response.is_some() || fixture.http.is_some();
969    let api_key_var_opt = fixture.env.as_ref().and_then(|e| e.api_key_var.as_deref());
970    let needs_api_key_skip = !has_mock && api_key_var_opt.is_some();
971    // When the fixture has both a mock and an api_key_var, generate env-fallback code:
972    // use the real API when the key is set, otherwise fall back to the mock server.
973    let needs_env_fallback = has_mock && api_key_var_opt.is_some();
974
975    let _ = writeln!(out, "  describe \"{test_name}\" do");
976    let _ = writeln!(out, "    test \"{test_label}\" do");
977
978    if needs_api_key_skip {
979        let api_key_var = api_key_var_opt.unwrap_or("");
980        let _ = writeln!(out, "      if System.get_env(\"{api_key_var}\") in [nil, \"\"] do");
981        let _ = writeln!(out, "        # {api_key_var} not set — skipping live smoke test");
982        let _ = writeln!(out, "        :ok");
983        let _ = writeln!(out, "      else");
984    }
985
986    // Validation-category fixtures: engine/handle creation itself is expected to fail.
987    // Transform the first `{:ok, _} = ...` setup line into `assert {:error, _} = ...`
988    // and stop emission there, since the rest of the test body would be unreachable.
989    if validation_creation_failure {
990        let mut emitted_error_assertion = false;
991        for line in &setup_lines {
992            if !emitted_error_assertion && line.starts_with("{:ok,") {
993                if let Some(rhs) = line.split_once('=').map(|x| x.1) {
994                    let rhs = rhs.trim();
995                    let _ = writeln!(out, "      assert {{:error, _}} = {rhs}");
996                    emitted_error_assertion = true;
997                } else {
998                    let _ = writeln!(out, "      {line}");
999                }
1000            } else {
1001                let _ = writeln!(out, "      {line}");
1002            }
1003        }
1004        if !emitted_error_assertion {
1005            let _ = writeln!(
1006                out,
1007                "      assert {{:error, _}} = {module_path}.{function_name}({effective_args})"
1008            );
1009        }
1010        if needs_api_key_skip {
1011            let _ = writeln!(out, "      end");
1012        }
1013        let _ = writeln!(out, "    end");
1014        let _ = writeln!(out, "  end");
1015        return;
1016    }
1017
1018    // Non-validation expects_error fixtures (error_*, etc.): engine creation succeeds.
1019    // Emit setup as-is and assert that the *operation under test* fails. The
1020    // call body still references `client` (e.g. `defaultclient_chat_async(client, …)`),
1021    // so we must emit the same `{:ok, client} = create_client(...)` line that the
1022    // non-error path below uses — without it the generated test fails to compile
1023    // with `undefined variable "client"`.
1024    if expects_error {
1025        for line in &setup_lines {
1026            let _ = writeln!(out, "      {line}");
1027        }
1028        if let Some(factory) = client_factory {
1029            let fixture_id = &fixture.id;
1030            let base_url_expr = if fixture.has_host_root_route() {
1031                let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1032                format!(
1033                    "(System.get_env(\"{env_key}\") || (System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\")"
1034                )
1035            } else {
1036                format!("(System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\"")
1037            };
1038            let _ = writeln!(
1039                out,
1040                "      {{:ok, client}} = {module_path}.{factory}(\"test-key\", base_url: {base_url_expr})"
1041            );
1042        }
1043        let _ = writeln!(
1044            out,
1045            "      assert {{:error, _}} = {module_path}.{function_name}({effective_args})"
1046        );
1047        if needs_api_key_skip {
1048            let _ = writeln!(out, "      end");
1049        }
1050        let _ = writeln!(out, "    end");
1051        let _ = writeln!(out, "  end");
1052        return;
1053    }
1054
1055    for line in &setup_lines {
1056        let _ = writeln!(out, "      {line}");
1057    }
1058
1059    // Emit client creation when client_factory is configured.
1060    if let Some(factory) = client_factory {
1061        let fixture_id = &fixture.id;
1062        if needs_env_fallback {
1063            // Fixture has both a mock and an api_key_var: use the real API when the key is
1064            // set, otherwise fall back to the mock server.
1065            let api_key_var = api_key_var_opt.unwrap_or("");
1066            let mock_url_expr = if fixture.has_host_root_route() {
1067                let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1068                format!(
1069                    "System.get_env(\"{env_key}\") || (System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\""
1070                )
1071            } else {
1072                format!("(System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\"")
1073            };
1074            let _ = writeln!(out, "      api_key_val = System.get_env(\"{api_key_var}\")");
1075            let _ = writeln!(
1076                out,
1077                "      {{api_key_val, client_opts}} = if api_key_val && api_key_val != \"\" do"
1078            );
1079            let _ = writeln!(
1080                out,
1081                "        IO.puts(\"{fixture_id}: using real API ({api_key_var} is set)\")"
1082            );
1083            let _ = writeln!(out, "        {{api_key_val, []}}");
1084            let _ = writeln!(out, "      else");
1085            let _ = writeln!(
1086                out,
1087                "        IO.puts(\"{fixture_id}: using mock server ({api_key_var} not set)\")"
1088            );
1089            let _ = writeln!(out, "        {{\"test-key\", [base_url: {mock_url_expr}]}}");
1090            let _ = writeln!(out, "      end");
1091            let _ = writeln!(
1092                out,
1093                "      {{:ok, client}} = {module_path}.{factory}(api_key_val, client_opts)"
1094            );
1095        } else {
1096            let base_url_expr = if fixture.has_host_root_route() {
1097                let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1098                format!(
1099                    "(System.get_env(\"{env_key}\") || (System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\")"
1100                )
1101            } else {
1102                format!("(System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\"")
1103            };
1104            let _ = writeln!(
1105                out,
1106                "      {{:ok, client}} = {module_path}.{factory}(\"test-key\", base_url: {base_url_expr})"
1107            );
1108        }
1109    }
1110
1111    // Use returns_result from the Elixir override if present, otherwise from base config
1112    let returns_result = call_overrides
1113        .and_then(|o| o.returns_result)
1114        .unwrap_or(call_config.returns_result || client_factory.is_some());
1115
1116    // Some calls (e.g. speech, file_content) return raw bytes rather than a struct.
1117    // When the call is marked `result_is_simple`, treat the bound `result` variable as
1118    // the value itself so assertions on a logical "audio"/"content" field map to the
1119    // bare binary instead of a struct accessor that doesn't exist.
1120    let result_is_simple = call_config.result_is_simple || call_overrides.is_some_and(|o| o.result_is_simple);
1121
1122    // Streaming detection (call-level `streaming` opt-out is honored).
1123    let is_streaming = crate::codegen::streaming_assertions::resolve_is_streaming(fixture, call_config.streaming);
1124    // For streaming fixtures the stream is bound to `result_var` first, then drained into `chunks`.
1125    let chunks_var = "chunks";
1126
1127    // If the result variable is never referenced in assertions or streaming operations,
1128    // prefix it with _ to avoid "unused variable" warnings in mix compile --warnings-as-errors.
1129    let actual_result_var = if fixture.assertions.is_empty() && !is_streaming {
1130        format!("_{result_var}")
1131    } else {
1132        result_var.to_string()
1133    };
1134
1135    if returns_result {
1136        let _ = writeln!(
1137            out,
1138            "      {{:ok, {actual_result_var}}} = {module_path}.{function_name}({effective_args})"
1139        );
1140    } else {
1141        // Non-Result function returns value directly (e.g., bool, String).
1142        let _ = writeln!(
1143            out,
1144            "      {actual_result_var} = {module_path}.{function_name}({effective_args})"
1145        );
1146    }
1147
1148    // For streaming fixtures, drain the stream into a list before asserting.
1149    if is_streaming {
1150        if let Some(collect) = crate::codegen::streaming_assertions::StreamingFieldResolver::collect_snippet(
1151            "elixir",
1152            &result_var,
1153            chunks_var,
1154        ) {
1155            let _ = writeln!(out, "      {collect}");
1156        }
1157    }
1158
1159    for assertion in &fixture.assertions {
1160        render_assertion(
1161            out,
1162            assertion,
1163            if is_streaming { chunks_var } else { &result_var },
1164            field_resolver,
1165            &module_path,
1166            e2e_config.effective_fields_enum(call_config),
1167            resolved_enum_fields_ref,
1168            result_is_simple,
1169            is_streaming,
1170        );
1171    }
1172
1173    if needs_api_key_skip {
1174        let _ = writeln!(out, "      end");
1175    }
1176    let _ = writeln!(out, "    end");
1177    let _ = writeln!(out, "  end");
1178}
1179
1180/// Build setup lines (e.g. handle creation) and the argument list for the function call.
1181///
1182/// Returns `(setup_lines, args_string)`.
1183#[allow(clippy::too_many_arguments)]
1184/// Emit Elixir batch item map constructors for BatchBytesItem or BatchFileItem arrays.
1185fn emit_elixir_batch_item_array(arr: &serde_json::Value, elem_type: &str) -> String {
1186    if let Some(items) = arr.as_array() {
1187        let item_strs: Vec<String> = items
1188            .iter()
1189            .filter_map(|item| {
1190                if let Some(obj) = item.as_object() {
1191                    match elem_type {
1192                        "BatchBytesItem" => {
1193                            let content = obj.get("content").and_then(|v| v.as_array());
1194                            let mime_type = obj.get("mime_type").and_then(|v| v.as_str()).unwrap_or("text/plain");
1195                            let content_code = if let Some(arr) = content {
1196                                let bytes: Vec<String> =
1197                                    arr.iter().filter_map(|v| v.as_u64().map(|n| n.to_string())).collect();
1198                                format!("<<{}>>", bytes.join(", "))
1199                            } else {
1200                                "<<>>".to_string()
1201                            };
1202                            Some(format!(
1203                                "%BatchBytesItem{{content: {}, mime_type: \"{}\"}}",
1204                                content_code, mime_type
1205                            ))
1206                        }
1207                        "BatchFileItem" => {
1208                            let path = obj.get("path").and_then(|v| v.as_str()).unwrap_or("");
1209                            Some(format!("%BatchFileItem{{path: \"{}\"}}", path))
1210                        }
1211                        _ => None,
1212                    }
1213                } else {
1214                    None
1215                }
1216            })
1217            .collect();
1218        format!("[{}]", item_strs.join(", "))
1219    } else {
1220        "[]".to_string()
1221    }
1222}
1223
1224#[allow(clippy::too_many_arguments)]
1225fn build_args_and_setup(
1226    input: &serde_json::Value,
1227    args: &[crate::config::ArgMapping],
1228    module_path: &str,
1229    options_type: Option<&str>,
1230    options_default_fn: Option<&str>,
1231    enum_fields: &HashMap<String, String>,
1232    fixture: &crate::fixture::Fixture,
1233    _handle_struct_type: Option<&str>,
1234    _handle_atom_list_fields: &std::collections::HashSet<String>,
1235    test_documents_path: &str,
1236    adapter_request_type: Option<&str>,
1237) -> (Vec<String>, String) {
1238    let fixture_id = &fixture.id;
1239    if args.is_empty() {
1240        // No args config: pass the whole input only when it's non-empty AND not just the harness setup dict.
1241        // Functions with no parameters (e.g. language_count) have empty input
1242        // and must be called with no arguments — not with `%{}`.
1243        // Filter out the harness' internal "setup" field — it's not part of the fixture's actual input.
1244        let cleaned_input = match input {
1245            serde_json::Value::Object(m) => {
1246                let mut cleaned = m.clone();
1247                cleaned.remove("setup");
1248                if cleaned.is_empty() {
1249                    serde_json::Value::Null
1250                } else {
1251                    serde_json::Value::Object(cleaned)
1252                }
1253            }
1254            other => other.clone(),
1255        };
1256        let is_empty_input = matches!(cleaned_input, serde_json::Value::Null);
1257        if is_empty_input {
1258            return (Vec::new(), String::new());
1259        }
1260        return (Vec::new(), json_to_elixir(&cleaned_input));
1261    }
1262
1263    let mut setup_lines: Vec<String> = Vec::new();
1264    let mut parts: Vec<String> = Vec::new();
1265
1266    for arg in args {
1267        if arg.arg_type == "mock_url" {
1268            if fixture.has_host_root_route() {
1269                let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1270                setup_lines.push(format!(
1271                    "{} = System.get_env(\"{env_key}\") || (System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\"",
1272                    arg.name,
1273                ));
1274            } else {
1275                setup_lines.push(format!(
1276                    "{} = (System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\"",
1277                    arg.name,
1278                ));
1279            }
1280            if let Some(req_type) = adapter_request_type {
1281                let req_var = format!("{}_req", arg.name);
1282                setup_lines.push(format!("{req_var} = %{module_path}.{req_type}{{url: {}}}", arg.name,));
1283                parts.push(req_var);
1284            } else {
1285                parts.push(arg.name.clone());
1286            }
1287            continue;
1288        }
1289
1290        if arg.arg_type == "mock_url_list" {
1291            // list of URLs: each element is either a bare path (`/seed1`) — prefixed
1292            // with the per-fixture mock-server URL at runtime — or an absolute URL
1293            // kept as-is. Mirrors `mock_url` resolution: `MOCK_SERVER_<FIXTURE_ID>`
1294            // first, then `MOCK_SERVER_URL/fixtures/<id>`. Without this branch the
1295            // codegen falls back to a JSON-array literal of bare relative paths and
1296            // the Rust HTTP client rejects them.
1297            let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1298            let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1299            let val = input.get(field).unwrap_or(&serde_json::Value::Null);
1300            let paths: Vec<String> = if let Some(arr) = val.as_array() {
1301                arr.iter()
1302                    .filter_map(|v| v.as_str().map(|s| format!("\"{}\"", escape_elixir(s))))
1303                    .collect()
1304            } else {
1305                Vec::new()
1306            };
1307            let paths_literal = paths.join(", ");
1308            let name = &arg.name;
1309            setup_lines.push(format!(
1310                "{name}_base = System.get_env(\"{env_key}\") || ((System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\")"
1311            ));
1312            setup_lines.push(format!(
1313                "{name} = Enum.map([{paths_literal}], fn p -> if String.starts_with?(p, \"http\"), do: p, else: {name}_base <> p end)"
1314            ));
1315            parts.push(name.clone());
1316            continue;
1317        }
1318
1319        if arg.arg_type == "handle" {
1320            // Generate a create_{name} call using {:ok, name} = ... pattern.
1321            // The NIF now accepts config as an optional JSON string (not a NifStruct/NifMap)
1322            // so that partial maps work: serde_json::from_str respects #[serde(default)].
1323            let constructor_name = format!("create_{}", arg.name.to_snake_case());
1324            let config_value = if arg.field == "input" {
1325                input
1326            } else {
1327                let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1328                input.get(field).unwrap_or(&serde_json::Value::Null)
1329            };
1330            let name = &arg.name;
1331            if config_value.is_null()
1332                || config_value.is_object() && config_value.as_object().is_some_and(|o| o.is_empty())
1333            {
1334                setup_lines.push(format!("{{:ok, {name}}} = {module_path}.{constructor_name}(nil)"));
1335            } else {
1336                // Serialize the config map to a JSON string with Jason so that Rust can
1337                // deserialize it with serde_json and apply field defaults for missing keys.
1338                let json_str = serde_json::to_string(config_value).unwrap_or_else(|_| "{}".to_string());
1339                let escaped = escape_elixir(&json_str);
1340                setup_lines.push(format!("{name}_config = \"{escaped}\""));
1341                setup_lines.push(format!(
1342                    "{{:ok, {name}}} = {module_path}.{constructor_name}({name}_config)",
1343                ));
1344            }
1345            parts.push(arg.name.clone());
1346            continue;
1347        }
1348
1349        let val = if arg.field == "input" {
1350            Some(input)
1351        } else {
1352            let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1353            input.get(field)
1354        };
1355        match val {
1356            None | Some(serde_json::Value::Null) if arg.optional => {
1357                // Optional params map to the keyword-opts `opts \\ []` argument.
1358                // When the value is absent, omit the keyword entirely — the default `[]` applies.
1359                continue;
1360            }
1361            None | Some(serde_json::Value::Null) => {
1362                // Required arg with no fixture value: pass a language-appropriate default.
1363                let default_val = match arg.arg_type.as_str() {
1364                    "string" => "\"\"".to_string(),
1365                    "int" | "integer" => "0".to_string(),
1366                    "float" | "number" => "0.0".to_string(),
1367                    "bool" | "boolean" => "false".to_string(),
1368                    _ => "nil".to_string(),
1369                };
1370                parts.push(default_val);
1371            }
1372            Some(v) => {
1373                // For file_path args, prepend the path to the test_documents directory
1374                // relative to the e2e/elixir/ directory where `mix test` runs.
1375                if arg.arg_type == "file_path" {
1376                    if let Some(path_str) = v.as_str() {
1377                        let full_path = format!("{test_documents_path}/{path_str}");
1378                        let formatted = format!("\"{}\"", escape_elixir(&full_path));
1379                        if arg.optional {
1380                            parts.push(format!("{}: {formatted}", arg.name));
1381                        } else {
1382                            parts.push(formatted);
1383                        }
1384                        continue;
1385                    }
1386                }
1387                // For bytes args, use File.read! for file paths and Base.decode64! for base64.
1388                // Inline text (starts with '<', '{', '[' or contains spaces) is used as-is (UTF-8 binary).
1389                if arg.arg_type == "bytes" {
1390                    if let Some(raw) = v.as_str() {
1391                        let var_name = &arg.name;
1392                        if raw.starts_with('<') || raw.starts_with('{') || raw.starts_with('[') || raw.contains(' ') {
1393                            // Inline text — use as a binary string.
1394                            let formatted = format!("\"{}\"", escape_elixir(raw));
1395                            if arg.optional {
1396                                parts.push(format!("{}: {formatted}", arg.name));
1397                            } else {
1398                                parts.push(formatted);
1399                            }
1400                        } else {
1401                            let first = raw.chars().next().unwrap_or('\0');
1402                            let is_file_path = (first.is_ascii_alphanumeric() || first == '_')
1403                                && raw
1404                                    .find('/')
1405                                    .is_some_and(|slash_pos| slash_pos > 0 && raw[slash_pos + 1..].contains('.'));
1406                            if is_file_path {
1407                                // Looks like "dir/file.ext" — read from the
1408                                // configured test-documents directory.
1409                                let full_path = format!("{test_documents_path}/{raw}");
1410                                let escaped = escape_elixir(&full_path);
1411                                setup_lines.push(format!("{var_name} = File.read!(\"{escaped}\")"));
1412                                if arg.optional {
1413                                    parts.push(format!("{}: {var_name}", arg.name));
1414                                } else {
1415                                    parts.push(var_name.to_string());
1416                                }
1417                            } else {
1418                                // Treat as base64-encoded binary.
1419                                setup_lines.push(format!(
1420                                    "{var_name} = Base.decode64!(\"{}\", padding: false)",
1421                                    escape_elixir(raw)
1422                                ));
1423                                if arg.optional {
1424                                    parts.push(format!("{}: {var_name}", arg.name));
1425                                } else {
1426                                    parts.push(var_name.to_string());
1427                                }
1428                            }
1429                        }
1430                        continue;
1431                    }
1432                }
1433                // For json_object args with options_type+options_via, build a proper struct.
1434                if arg.arg_type == "json_object" && !v.is_null() {
1435                    if let (Some(_opts_type), Some(options_fn), Some(obj)) =
1436                        (options_type, options_default_fn, v.as_object())
1437                    {
1438                        // Add setup line to initialize options from default function.
1439                        let options_var = "options";
1440                        setup_lines.push(format!("{options_var} = {module_path}.{options_fn}()"));
1441
1442                        // For each field in the options object, add a struct update line.
1443                        for (k, vv) in obj.iter() {
1444                            let snake_key = k.to_snake_case();
1445                            let elixir_val = if let Some(_enum_type) = enum_fields.get(k) {
1446                                if let Some(s) = vv.as_str() {
1447                                    let snake_val = s.to_snake_case();
1448                                    // Use atom for enum values, not string
1449                                    format!(":{snake_val}")
1450                                } else {
1451                                    json_to_elixir(vv)
1452                                }
1453                            } else {
1454                                json_to_elixir(vv)
1455                            };
1456                            setup_lines.push(format!(
1457                                "{options_var} = %{{{options_var} | {snake_key}: {elixir_val}}}"
1458                            ));
1459                        }
1460
1461                        // Push the variable name as the argument.
1462                        // Optional args (with `\\ []` or `\\ nil`) always use keyword form
1463                        // so that the facade can handle them via Keyword.get() or defaults.
1464                        parts.push(format!("{}: {options_var}", arg.name));
1465                        continue;
1466                    }
1467                    // When options_type is set but options_via is NOT, emit struct-literal form.
1468                    if let (Some(opts_type), None, Some(obj)) = (options_type, options_default_fn, v.as_object()) {
1469                        let options_var = "options";
1470                        let mut field_strs = Vec::new();
1471                        for (k, vv) in obj.iter() {
1472                            let snake_key = k.to_snake_case();
1473                            let elixir_val = if let Some(_enum_type) = enum_fields.get(k) {
1474                                if let Some(s) = vv.as_str() {
1475                                    let snake_val = s.to_snake_case();
1476                                    format!(":{snake_val}")
1477                                } else {
1478                                    json_to_elixir(vv)
1479                                }
1480                            } else {
1481                                json_to_elixir(vv)
1482                            };
1483                            field_strs.push(format!("{snake_key}: {elixir_val}"));
1484                        }
1485                        let fields = field_strs.join(", ");
1486                        setup_lines.push(format!("{options_var} = %{module_path}.{opts_type}{{{fields}}}"));
1487                        // Optional args always use keyword form so the facade can handle defaults.
1488                        parts.push(format!("{}: {options_var}", arg.name));
1489                        continue;
1490                    }
1491                    // When element_type is set to a batch item type, wrap items with constructors.
1492                    if let Some(elem_type) = &arg.element_type {
1493                        if (elem_type == "BatchBytesItem" || elem_type == "BatchFileItem") && v.is_array() {
1494                            let formatted = emit_elixir_batch_item_array(v, elem_type);
1495                            if arg.optional {
1496                                parts.push(format!("{}: {formatted}", arg.name));
1497                            } else {
1498                                parts.push(formatted);
1499                            }
1500                            continue;
1501                        }
1502                        // When element_type is set to a simple type (e.g. Vec<String>).
1503                        // The NIF accepts an Elixir list directly — emit one.
1504                        if v.is_array() {
1505                            let formatted = json_to_elixir(v);
1506                            if arg.optional {
1507                                parts.push(format!("{}: {formatted}", arg.name));
1508                            } else {
1509                                parts.push(formatted);
1510                            }
1511                            continue;
1512                        }
1513                    }
1514                    // When there's no options_type+options_via, the Elixir NIF expects a JSON
1515                    // string (Option<String> decoded by serde_json) rather than an Elixir map.
1516                    // Serialize the JSON value to a string literal here.
1517                    if !v.is_null() {
1518                        let json_str = serde_json::to_string(v).unwrap_or_else(|_| "{}".to_string());
1519                        let escaped = escape_elixir(&json_str);
1520                        let formatted = format!("\"{escaped}\"");
1521                        if arg.optional {
1522                            parts.push(format!("{}: {formatted}", arg.name));
1523                        } else {
1524                            parts.push(formatted);
1525                        }
1526                        continue;
1527                    }
1528                }
1529                // Optional args use keyword-opts form: `name: value`.
1530                let elixir_val = json_to_elixir(v);
1531                if arg.optional {
1532                    parts.push(format!("{}: {elixir_val}", arg.name));
1533                } else {
1534                    parts.push(elixir_val);
1535                }
1536            }
1537        }
1538    }
1539
1540    // Elixir requires all positional args before keyword args.
1541    // Track the index of the first keyword arg to ensure no positional args come after it.
1542    let mut positional_args = Vec::new();
1543    let mut keyword_args = Vec::new();
1544    let mut seen_keyword = false;
1545
1546    for (idx, part) in parts.into_iter().enumerate() {
1547        let is_keyword = part.contains(": ") && !part.starts_with('"');
1548        if is_keyword {
1549            seen_keyword = true;
1550            keyword_args.push((idx, part));
1551        } else if seen_keyword {
1552            // We've already seen a keyword arg, so any positional arg from here on
1553            // breaks Elixir's syntax rules. This shouldn't happen if the arg ordering
1554            // respects the function signature, but we'll keep it as a positional
1555            // and let Elixir fail with a syntax error (the real fix is in alef.toml config).
1556            keyword_args.push((idx, part));
1557        } else {
1558            positional_args.push(part);
1559        }
1560    }
1561
1562    let mut final_args = positional_args;
1563    final_args.extend(keyword_args.into_iter().map(|(_, arg)| arg));
1564
1565    (setup_lines, final_args.join(", "))
1566}
1567
1568/// Returns true if the field expression is a numeric/integer expression
1569/// (e.g., a `length(...)` call) rather than a string.
1570fn is_numeric_expr(field_expr: &str) -> bool {
1571    field_expr.starts_with("length(")
1572}
1573
1574#[allow(clippy::too_many_arguments)]
1575fn render_assertion(
1576    out: &mut String,
1577    assertion: &Assertion,
1578    result_var: &str,
1579    field_resolver: &FieldResolver,
1580    module_path: &str,
1581    fields_enum: &std::collections::HashSet<String>,
1582    per_call_enum_fields: &HashMap<String, String>,
1583    result_is_simple: bool,
1584    is_streaming: bool,
1585) {
1586    // Handle synthetic / derived fields before the is_valid_for_result check
1587    // so they are never treated as struct field accesses on the result.
1588    if let Some(f) = &assertion.field {
1589        match f.as_str() {
1590            "chunks_have_content" => {
1591                let pred =
1592                    format!("Enum.all?({result_var}.chunks || [], fn c -> c.content != nil and c.content != \"\" end)");
1593                match assertion.assertion_type.as_str() {
1594                    "is_true" => {
1595                        let _ = writeln!(out, "      assert {pred}");
1596                    }
1597                    "is_false" => {
1598                        let _ = writeln!(out, "      refute {pred}");
1599                    }
1600                    _ => {
1601                        let _ = writeln!(
1602                            out,
1603                            "      # skipped: unsupported assertion type on synthetic field '{f}'"
1604                        );
1605                    }
1606                }
1607                return;
1608            }
1609            "chunks_have_embeddings" => {
1610                let pred = format!(
1611                    "Enum.all?({result_var}.chunks || [], fn c -> c.embedding != nil and c.embedding != [] end)"
1612                );
1613                match assertion.assertion_type.as_str() {
1614                    "is_true" => {
1615                        let _ = writeln!(out, "      assert {pred}");
1616                    }
1617                    "is_false" => {
1618                        let _ = writeln!(out, "      refute {pred}");
1619                    }
1620                    _ => {
1621                        let _ = writeln!(
1622                            out,
1623                            "      # skipped: unsupported assertion type on synthetic field '{f}'"
1624                        );
1625                    }
1626                }
1627                return;
1628            }
1629            "chunks_have_heading_context" => {
1630                let pred = format!(
1631                    "Enum.all?({result_var}.chunks || [], fn c -> c.metadata != nil and c.metadata.heading_context != nil end)"
1632                );
1633                match assertion.assertion_type.as_str() {
1634                    "is_true" => {
1635                        let _ = writeln!(out, "      assert {pred}");
1636                    }
1637                    "is_false" => {
1638                        let _ = writeln!(out, "      refute {pred}");
1639                    }
1640                    _ => {
1641                        let _ = writeln!(
1642                            out,
1643                            "      # skipped: unsupported assertion type on synthetic field '{f}'"
1644                        );
1645                    }
1646                }
1647                return;
1648            }
1649            "first_chunk_starts_with_heading" => {
1650                let expr = format!(
1651                    "case List.first({result_var}.chunks || []) do
1652        c when is_map(c) -> String.trim_leading(c.content || \"\") |> String.starts_with?(\"#\")
1653        _ -> false
1654      end"
1655                );
1656                match assertion.assertion_type.as_str() {
1657                    "is_true" => {
1658                        let _ = writeln!(out, "      assert ({expr})");
1659                    }
1660                    "is_false" => {
1661                        let _ = writeln!(out, "      refute ({expr})");
1662                    }
1663                    _ => {
1664                        let _ = writeln!(
1665                            out,
1666                            "      # skipped: unsupported assertion type on synthetic field '{f}'"
1667                        );
1668                    }
1669                }
1670                return;
1671            }
1672            // ---- EmbedResponse virtual fields ----
1673            // embed_texts returns [[float]] in Elixir — no wrapper struct.
1674            // result_var is the embedding matrix; use it directly.
1675            "embeddings" => {
1676                match assertion.assertion_type.as_str() {
1677                    "count_equals" => {
1678                        if let Some(val) = &assertion.value {
1679                            let ex_val = json_to_elixir(val);
1680                            let _ = writeln!(out, "      assert length({result_var}) == {ex_val}");
1681                        }
1682                    }
1683                    "count_min" => {
1684                        if let Some(val) = &assertion.value {
1685                            let ex_val = json_to_elixir(val);
1686                            let _ = writeln!(out, "      assert length({result_var}) >= {ex_val}");
1687                        }
1688                    }
1689                    "not_empty" => {
1690                        let _ = writeln!(out, "      assert {result_var} != []");
1691                    }
1692                    "is_empty" => {
1693                        let _ = writeln!(out, "      assert {result_var} == []");
1694                    }
1695                    _ => {
1696                        let _ = writeln!(
1697                            out,
1698                            "      # skipped: unsupported assertion type on synthetic field 'embeddings'"
1699                        );
1700                    }
1701                }
1702                return;
1703            }
1704            "embedding_dimensions" => {
1705                let expr = format!("(if {result_var} == [], do: 0, else: length(hd({result_var})))");
1706                match assertion.assertion_type.as_str() {
1707                    "equals" => {
1708                        if let Some(val) = &assertion.value {
1709                            let ex_val = json_to_elixir(val);
1710                            let _ = writeln!(out, "      assert {expr} == {ex_val}");
1711                        }
1712                    }
1713                    "greater_than" => {
1714                        if let Some(val) = &assertion.value {
1715                            let ex_val = json_to_elixir(val);
1716                            let _ = writeln!(out, "      assert {expr} > {ex_val}");
1717                        }
1718                    }
1719                    _ => {
1720                        let _ = writeln!(
1721                            out,
1722                            "      # skipped: unsupported assertion type on synthetic field 'embedding_dimensions'"
1723                        );
1724                    }
1725                }
1726                return;
1727            }
1728            "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
1729                let pred = match f.as_str() {
1730                    "embeddings_valid" => {
1731                        format!("Enum.all?({result_var}, fn e -> e != [] end)")
1732                    }
1733                    "embeddings_finite" => {
1734                        format!("Enum.all?({result_var}, fn e -> Enum.all?(e, fn v -> is_float(v) and v == v end) end)")
1735                    }
1736                    "embeddings_non_zero" => {
1737                        format!("Enum.all?({result_var}, fn e -> Enum.any?(e, fn v -> v != 0.0 end) end)")
1738                    }
1739                    "embeddings_normalized" => {
1740                        format!(
1741                            "Enum.all?({result_var}, fn e -> n = Enum.reduce(e, 0.0, fn v, acc -> acc + v * v end); abs(n - 1.0) < 1.0e-3 end)"
1742                        )
1743                    }
1744                    _ => unreachable!(),
1745                };
1746                match assertion.assertion_type.as_str() {
1747                    "is_true" => {
1748                        let _ = writeln!(out, "      assert {pred}");
1749                    }
1750                    "is_false" => {
1751                        let _ = writeln!(out, "      refute {pred}");
1752                    }
1753                    _ => {
1754                        let _ = writeln!(
1755                            out,
1756                            "      # skipped: unsupported assertion type on synthetic field '{f}'"
1757                        );
1758                    }
1759                }
1760                return;
1761            }
1762            // ---- keywords / keywords_count ----
1763            // Elixir ExtractionResult does not expose extracted_keywords; skip.
1764            "keywords" | "keywords_count" => {
1765                let _ = writeln!(
1766                    out,
1767                    "      # skipped: field '{f}' not available on Elixir ExtractionResult"
1768                );
1769                return;
1770            }
1771            _ => {}
1772        }
1773    }
1774
1775    // Streaming virtual fields: intercept before is_valid_for_result so they are
1776    // never skipped.  These fields resolve against the `chunks` collected-list variable.
1777    if is_streaming {
1778        if let Some(f) = &assertion.field {
1779            if !f.is_empty() && crate::codegen::streaming_assertions::is_streaming_virtual_field(f) {
1780                if let Some(expr) =
1781                    crate::codegen::streaming_assertions::StreamingFieldResolver::accessor(f, "elixir", result_var)
1782                {
1783                    match assertion.assertion_type.as_str() {
1784                        "count_min" => {
1785                            if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1786                                let _ = writeln!(out, "      assert length({expr}) >= {n}");
1787                            }
1788                        }
1789                        "count_equals" => {
1790                            if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1791                                let _ = writeln!(out, "      assert length({expr}) == {n}");
1792                            }
1793                        }
1794                        "equals" => {
1795                            if let Some(serde_json::Value::String(s)) = &assertion.value {
1796                                let escaped = escape_elixir(s);
1797                                let _ = writeln!(out, "      assert {expr} == \"{escaped}\"");
1798                            } else if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1799                                let _ = writeln!(out, "      assert {expr} == {n}");
1800                            }
1801                        }
1802                        "not_empty" => {
1803                            let _ = writeln!(out, "      assert {expr} != []");
1804                        }
1805                        "is_empty" => {
1806                            let _ = writeln!(out, "      assert {expr} == []");
1807                        }
1808                        "is_true" => {
1809                            let _ = writeln!(out, "      assert {expr}");
1810                        }
1811                        "is_false" => {
1812                            let _ = writeln!(out, "      refute {expr}");
1813                        }
1814                        "greater_than" => {
1815                            if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1816                                let _ = writeln!(out, "      assert {expr} > {n}");
1817                            }
1818                        }
1819                        "greater_than_or_equal" => {
1820                            if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1821                                let _ = writeln!(out, "      assert {expr} >= {n}");
1822                            }
1823                        }
1824                        "contains" => {
1825                            if let Some(serde_json::Value::String(s)) = &assertion.value {
1826                                let escaped = escape_elixir(s);
1827                                let _ = writeln!(out, "      assert String.contains?({expr}, \"{escaped}\")");
1828                            }
1829                        }
1830                        _ => {
1831                            let _ = writeln!(
1832                                out,
1833                                "      # streaming field '{f}': assertion type '{}' not rendered",
1834                                assertion.assertion_type
1835                            );
1836                        }
1837                    }
1838                }
1839                return;
1840            }
1841        }
1842    }
1843
1844    // Skip assertions on fields that don't exist on the result type.
1845    // When `result_is_simple`, the bound result is the value itself (e.g. a binary)
1846    // so `is_valid_for_result` is meaningless — fall through and emit the assertion
1847    // against the bare result_var below.
1848    if !result_is_simple {
1849        if let Some(f) = &assertion.field {
1850            if !f.is_empty() && !field_resolver.is_valid_for_result(f) {
1851                let _ = writeln!(out, "      # skipped: field '{f}' not available on result type");
1852                return;
1853            }
1854        }
1855    }
1856
1857    // result_is_simple: when the call returns the value itself (e.g. a binary for
1858    // `speech` / `file_content`), bypass the field accessor and assert against the
1859    // bound `result` variable directly.
1860    let field_expr = if result_is_simple {
1861        result_var.to_string()
1862    } else {
1863        match &assertion.field {
1864            Some(f) if !f.is_empty() => field_resolver.accessor(f, "elixir", result_var),
1865            _ => result_var.to_string(),
1866        }
1867    };
1868
1869    // Only wrap in String.trim/0 when the expression is actually a string.
1870    // Numeric expressions (e.g., length(...)) must not be wrapped.
1871    let is_numeric = is_numeric_expr(&field_expr);
1872    // Detect whether the field resolves to an enum type. Rustler binds Rust
1873    // enums as atoms (e.g. `:stop`), so calling `String.trim/1` on them raises
1874    // FunctionClauseError. Coerce via `to_string/1` before string ops. Look up
1875    // both the global `[crates.e2e] fields_enum` set AND the per-call override
1876    // `[crates.e2e.calls.<x>.overrides.elixir] enum_fields = { ... }` so a single
1877    // config entry already populated for the C#/Java/Python sides applies here.
1878    let field_is_enum = assertion.field.as_deref().filter(|f| !f.is_empty()).is_some_and(|f| {
1879        let resolved = field_resolver.resolve(f);
1880        fields_enum.contains(f)
1881            || fields_enum.contains(resolved)
1882            || per_call_enum_fields.contains_key(f)
1883            || per_call_enum_fields.contains_key(resolved)
1884    });
1885    // Check if the field is exactly metadata.format (FormatMetadata struct; needs special display conversion)
1886    // Don't match on other fields like metadata.output_format (which is a plain string)
1887    let field_is_format_metadata = assertion
1888        .field
1889        .as_deref()
1890        .filter(|f| !f.is_empty())
1891        .is_some_and(|f| f == "metadata.format" || f.ends_with(".metadata.format"));
1892    let coerced_field_expr = if field_is_format_metadata {
1893        format!("alef_e2e_format_to_string({field_expr})")
1894    } else if field_is_enum {
1895        format!("to_string({field_expr})")
1896    } else {
1897        field_expr.clone()
1898    };
1899    let trimmed_field_expr = if is_numeric {
1900        field_expr.clone()
1901    } else {
1902        format!("String.trim({coerced_field_expr})")
1903    };
1904
1905    // Detect whether the assertion field resolves to an array type so that
1906    // contains assertions can iterate items instead of calling to_string on the list.
1907    let field_is_array = assertion
1908        .field
1909        .as_deref()
1910        .filter(|f| !f.is_empty())
1911        .is_some_and(|f| field_resolver.is_array(field_resolver.resolve(f)));
1912
1913    match assertion.assertion_type.as_str() {
1914        "equals" => {
1915            if let Some(expected) = &assertion.value {
1916                let elixir_val = json_to_elixir(expected);
1917                // Apply String.trim only for string comparisons, not numeric ones.
1918                let is_string_expected = expected.is_string();
1919                if is_string_expected && !is_numeric {
1920                    let _ = writeln!(out, "      assert {trimmed_field_expr} == {elixir_val}");
1921                } else if field_is_enum {
1922                    let _ = writeln!(out, "      assert {coerced_field_expr} == {elixir_val}");
1923                } else {
1924                    let _ = writeln!(out, "      assert {field_expr} == {elixir_val}");
1925                }
1926            }
1927        }
1928        "contains" => {
1929            if let Some(expected) = &assertion.value {
1930                let elixir_val = json_to_elixir(expected);
1931                if field_is_array && expected.is_string() {
1932                    // List of structs: check if any item's text representation contains the value.
1933                    let _ = writeln!(
1934                        out,
1935                        "      assert Enum.any?({field_expr}, fn item -> Enum.any?(alef_e2e_item_texts(item), &String.contains?(&1, {elixir_val})) end)"
1936                    );
1937                } else {
1938                    // Use to_string() to handle atoms (enums) as well as strings
1939                    let _ = writeln!(
1940                        out,
1941                        "      assert String.contains?(to_string({field_expr}), {elixir_val})"
1942                    );
1943                }
1944            }
1945        }
1946        "contains_all" => {
1947            if let Some(values) = &assertion.values {
1948                for val in values {
1949                    let elixir_val = json_to_elixir(val);
1950                    if field_is_array && val.is_string() {
1951                        let _ = writeln!(
1952                            out,
1953                            "      assert Enum.any?({field_expr}, fn item -> Enum.any?(alef_e2e_item_texts(item), &String.contains?(&1, {elixir_val})) end)"
1954                        );
1955                    } else {
1956                        let _ = writeln!(
1957                            out,
1958                            "      assert String.contains?(to_string({field_expr}), {elixir_val})"
1959                        );
1960                    }
1961                }
1962            }
1963        }
1964        "not_contains" => {
1965            if let Some(expected) = &assertion.value {
1966                let elixir_val = json_to_elixir(expected);
1967                if field_is_array && expected.is_string() {
1968                    let _ = writeln!(
1969                        out,
1970                        "      refute Enum.any?({field_expr}, fn item -> Enum.any?(alef_e2e_item_texts(item), &String.contains?(&1, {elixir_val})) end)"
1971                    );
1972                } else {
1973                    let _ = writeln!(
1974                        out,
1975                        "      refute String.contains?(to_string({field_expr}), {elixir_val})"
1976                    );
1977                }
1978            }
1979        }
1980        "not_empty" => {
1981            let _ = writeln!(out, "      assert {field_expr} != \"\"");
1982        }
1983        "is_empty" => {
1984            if is_numeric {
1985                // length(...) == 0
1986                let _ = writeln!(out, "      assert {field_expr} == 0");
1987            } else {
1988                // Handle nil (None) as empty
1989                let _ = writeln!(out, "      assert is_nil({field_expr}) or {trimmed_field_expr} == \"\"");
1990            }
1991        }
1992        "contains_any" => {
1993            if let Some(values) = &assertion.values {
1994                let items: Vec<String> = values.iter().map(json_to_elixir).collect();
1995                let list_str = items.join(", ");
1996                let _ = writeln!(
1997                    out,
1998                    "      assert Enum.any?([{list_str}], fn v -> String.contains?(to_string({field_expr}), v) end)"
1999                );
2000            }
2001        }
2002        "greater_than" => {
2003            if let Some(val) = &assertion.value {
2004                let elixir_val = json_to_elixir(val);
2005                let _ = writeln!(out, "      assert {field_expr} > {elixir_val}");
2006            }
2007        }
2008        "less_than" => {
2009            if let Some(val) = &assertion.value {
2010                let elixir_val = json_to_elixir(val);
2011                let _ = writeln!(out, "      assert {field_expr} < {elixir_val}");
2012            }
2013        }
2014        "greater_than_or_equal" => {
2015            if let Some(val) = &assertion.value {
2016                let elixir_val = json_to_elixir(val);
2017                let _ = writeln!(out, "      assert {field_expr} >= {elixir_val}");
2018            }
2019        }
2020        "less_than_or_equal" => {
2021            if let Some(val) = &assertion.value {
2022                let elixir_val = json_to_elixir(val);
2023                let _ = writeln!(out, "      assert {field_expr} <= {elixir_val}");
2024            }
2025        }
2026        "starts_with" => {
2027            if let Some(expected) = &assertion.value {
2028                let elixir_val = json_to_elixir(expected);
2029                let _ = writeln!(out, "      assert String.starts_with?({field_expr}, {elixir_val})");
2030            }
2031        }
2032        "ends_with" => {
2033            if let Some(expected) = &assertion.value {
2034                let elixir_val = json_to_elixir(expected);
2035                let _ = writeln!(out, "      assert String.ends_with?({field_expr}, {elixir_val})");
2036            }
2037        }
2038        "min_length" => {
2039            if let Some(val) = &assertion.value {
2040                if let Some(n) = val.as_u64() {
2041                    // Binary uses byte_size, list uses length, string uses String.length
2042                    let _ = writeln!(
2043                        out,
2044                        "      assert (is_binary({field_expr}) && byte_size({field_expr}) >= {n}) || (is_list({field_expr}) && length({field_expr}) >= {n}) || (is_binary({field_expr}) == false && is_list({field_expr}) == false && String.length({field_expr}) >= {n})"
2045                    );
2046                }
2047            }
2048        }
2049        "max_length" => {
2050            if let Some(val) = &assertion.value {
2051                if let Some(n) = val.as_u64() {
2052                    let _ = writeln!(
2053                        out,
2054                        "      assert (is_binary({field_expr}) && byte_size({field_expr}) <= {n}) || (is_list({field_expr}) && length({field_expr}) <= {n}) || (is_binary({field_expr}) == false && is_list({field_expr}) == false && String.length({field_expr}) <= {n})"
2055                    );
2056                }
2057            }
2058        }
2059        "count_min" => {
2060            if let Some(val) = &assertion.value {
2061                if let Some(n) = val.as_u64() {
2062                    let _ = writeln!(out, "      assert length({field_expr}) >= {n}");
2063                }
2064            }
2065        }
2066        "count_equals" => {
2067            if let Some(val) = &assertion.value {
2068                if let Some(n) = val.as_u64() {
2069                    let _ = writeln!(out, "      assert length({field_expr}) == {n}");
2070                }
2071            }
2072        }
2073        "is_true" => {
2074            let _ = writeln!(out, "      assert {field_expr} == true");
2075        }
2076        "is_false" => {
2077            let _ = writeln!(out, "      assert {field_expr} == false");
2078        }
2079        "method_result" => {
2080            if let Some(method_name) = &assertion.method {
2081                let call_expr = build_elixir_method_call(result_var, method_name, assertion.args.as_ref(), module_path);
2082                let check = assertion.check.as_deref().unwrap_or("is_true");
2083                match check {
2084                    "equals" => {
2085                        if let Some(val) = &assertion.value {
2086                            let elixir_val = json_to_elixir(val);
2087                            let _ = writeln!(out, "      assert {call_expr} == {elixir_val}");
2088                        }
2089                    }
2090                    "is_true" => {
2091                        let _ = writeln!(out, "      assert {call_expr} == true");
2092                    }
2093                    "is_false" => {
2094                        let _ = writeln!(out, "      assert {call_expr} == false");
2095                    }
2096                    "greater_than_or_equal" => {
2097                        if let Some(val) = &assertion.value {
2098                            let n = val.as_u64().unwrap_or(0);
2099                            let _ = writeln!(out, "      assert {call_expr} >= {n}");
2100                        }
2101                    }
2102                    "count_min" => {
2103                        if let Some(val) = &assertion.value {
2104                            let n = val.as_u64().unwrap_or(0);
2105                            let _ = writeln!(out, "      assert length({call_expr}) >= {n}");
2106                        }
2107                    }
2108                    "contains" => {
2109                        if let Some(val) = &assertion.value {
2110                            let elixir_val = json_to_elixir(val);
2111                            let _ = writeln!(out, "      assert String.contains?({call_expr}, {elixir_val})");
2112                        }
2113                    }
2114                    "is_error" => {
2115                        let _ = writeln!(out, "      assert_raise RuntimeError, fn -> {call_expr} end");
2116                    }
2117                    other_check => {
2118                        panic!("Elixir e2e generator: unsupported method_result check type: {other_check}");
2119                    }
2120                }
2121            } else {
2122                panic!("Elixir e2e generator: method_result assertion missing 'method' field");
2123            }
2124        }
2125        "matches_regex" => {
2126            if let Some(expected) = &assertion.value {
2127                let elixir_val = json_to_elixir(expected);
2128                let _ = writeln!(out, "      assert Regex.match?(~r/{elixir_val}/, {field_expr})");
2129            }
2130        }
2131        "not_error" => {
2132            // Already handled — the call would fail if it returned {:error, _}.
2133        }
2134        "error" => {
2135            // Handled at the test level.
2136        }
2137        other => {
2138            panic!("Elixir e2e generator: unsupported assertion type: {other}");
2139        }
2140    }
2141}
2142
2143/// Build an Elixir call expression for a `method_result` assertion on a tree-sitter result.
2144/// Maps method names to the appropriate `module_path` function calls.
2145fn build_elixir_method_call(
2146    result_var: &str,
2147    method_name: &str,
2148    args: Option<&serde_json::Value>,
2149    module_path: &str,
2150) -> String {
2151    match method_name {
2152        "root_child_count" => format!("{module_path}.root_child_count({result_var})"),
2153        "has_error_nodes" => format!("{module_path}.tree_has_error_nodes({result_var})"),
2154        "error_count" | "tree_error_count" => format!("{module_path}.tree_error_count({result_var})"),
2155        "tree_to_sexp" => format!("{module_path}.tree_to_sexp({result_var})"),
2156        "contains_node_type" => {
2157            let node_type = args
2158                .and_then(|a| a.get("node_type"))
2159                .and_then(|v| v.as_str())
2160                .unwrap_or("");
2161            format!("{module_path}.tree_contains_node_type({result_var}, \"{node_type}\")")
2162        }
2163        "find_nodes_by_type" => {
2164            let node_type = args
2165                .and_then(|a| a.get("node_type"))
2166                .and_then(|v| v.as_str())
2167                .unwrap_or("");
2168            format!("{module_path}.find_nodes_by_type({result_var}, \"{node_type}\")")
2169        }
2170        "run_query" => {
2171            let query_source = args
2172                .and_then(|a| a.get("query_source"))
2173                .and_then(|v| v.as_str())
2174                .unwrap_or("");
2175            let language = args
2176                .and_then(|a| a.get("language"))
2177                .and_then(|v| v.as_str())
2178                .unwrap_or("");
2179            format!("{module_path}.run_query({result_var}, \"{language}\", \"{query_source}\", source)")
2180        }
2181        _ => format!("{module_path}.{method_name}({result_var})"),
2182    }
2183}
2184
2185/// Convert a category name to an Elixir module-safe PascalCase name.
2186fn elixir_module_name(category: &str) -> String {
2187    use heck::ToUpperCamelCase;
2188    category.to_upper_camel_case()
2189}
2190
2191/// Convert a `serde_json::Value` to an Elixir literal string.
2192fn json_to_elixir(value: &serde_json::Value) -> String {
2193    match value {
2194        serde_json::Value::String(s) => format!("\"{}\"", escape_elixir(s)),
2195        serde_json::Value::Bool(true) => "true".to_string(),
2196        serde_json::Value::Bool(false) => "false".to_string(),
2197        serde_json::Value::Number(n) => {
2198            // Elixir requires floats to have a decimal point and does not accept
2199            // `e+N` exponent notation. Strip the `+` and ensure there is a decimal
2200            // point before any `e` exponent marker (e.g. `1e-10` → `1.0e-10`).
2201            let s = n.to_string().replace("e+", "e");
2202            if s.contains('e') && !s.contains('.') {
2203                // Insert `.0` before the `e` so Elixir treats this as a float.
2204                s.replacen('e', ".0e", 1)
2205            } else {
2206                s
2207            }
2208        }
2209        serde_json::Value::Null => "nil".to_string(),
2210        serde_json::Value::Array(arr) => {
2211            let items: Vec<String> = arr.iter().map(json_to_elixir).collect();
2212            format!("[{}]", items.join(", "))
2213        }
2214        serde_json::Value::Object(map) => {
2215            let entries: Vec<String> = map
2216                .iter()
2217                .map(|(k, v)| format!("\"{}\" => {}", escape_elixir(k), json_to_elixir(v)))
2218                .collect();
2219            format!("%{{{}}}", entries.join(", "))
2220        }
2221    }
2222}
2223
2224/// Build an Elixir visitor map and add setup line. Returns the visitor variable name.
2225fn build_elixir_visitor(setup_lines: &mut Vec<String>, visitor_spec: &crate::fixture::VisitorSpec) -> String {
2226    use std::fmt::Write as FmtWrite;
2227    let mut visitor_obj = String::new();
2228    let _ = writeln!(visitor_obj, "%{{");
2229    for (method_name, action) in &visitor_spec.callbacks {
2230        emit_elixir_visitor_method(&mut visitor_obj, method_name, action);
2231    }
2232    let _ = writeln!(visitor_obj, "    }}");
2233
2234    setup_lines.push(format!("visitor = {visitor_obj}"));
2235    "visitor".to_string()
2236}
2237
2238/// Emit an Elixir visitor method for a callback action.
2239fn emit_elixir_visitor_method(out: &mut String, method_name: &str, action: &CallbackAction) {
2240    use std::fmt::Write as FmtWrite;
2241
2242    // Elixir uses atom keys and handle_ prefix
2243    let handle_method = format!("handle_{}", &method_name[6..]); // strip "visit_" prefix
2244    // The Rust NIF bridge packages every visitor argument (`_ctx`, `_text`, …) into a
2245    // single map and invokes the user's anonymous function with that map. Generating
2246    // multi-arity functions like `fn(_ctx, _text) ->` therefore raised BadArityError
2247    // ("arity 2 called with 1 argument") at runtime. Generate arity-1 functions that
2248    // accept the args map (and ignore it) to match the bridge's calling convention.
2249
2250    // CustomTemplate needs to read from args; other actions can ignore it.
2251    let arg_binding = match action {
2252        CallbackAction::CustomTemplate { .. } => "args",
2253        _ => "_args",
2254    };
2255    let _ = writeln!(out, "      :{handle_method} => fn({arg_binding}) ->");
2256    match action {
2257        CallbackAction::Skip => {
2258            let _ = writeln!(out, "        :skip");
2259        }
2260        CallbackAction::Continue => {
2261            let _ = writeln!(out, "        :continue");
2262        }
2263        CallbackAction::PreserveHtml => {
2264            let _ = writeln!(out, "        :preserve_html");
2265        }
2266        CallbackAction::Custom { output } => {
2267            let escaped = escape_elixir(output);
2268            let _ = writeln!(out, "        {{:custom, \"{escaped}\"}}");
2269        }
2270        CallbackAction::CustomTemplate { template, .. } => {
2271            // Build a <> concatenation expression so {key} placeholders are substituted
2272            // from the args map at runtime without embedding double-quoted strings inside
2273            // a double-quoted string literal.
2274            let expr = template_to_elixir_concat(template);
2275            let _ = writeln!(out, "        {{:custom, {expr}}}");
2276        }
2277    }
2278    let _ = writeln!(out, "      end,");
2279}
2280
2281/// Convert a template like `"_{text}_"` into an Elixir `<>` concat expression:
2282/// `"_" <> Map.get(args, "text", "") <> "_"`.
2283/// Static parts are escaped via `escape_elixir`; `{key}` placeholders become
2284/// `Map.get(args, "key", "")` lookups into the JSON-decoded args map.
2285fn template_to_elixir_concat(template: &str) -> String {
2286    let mut parts: Vec<String> = Vec::new();
2287    let mut static_buf = String::new();
2288    let mut chars = template.chars().peekable();
2289
2290    while let Some(ch) = chars.next() {
2291        if ch == '{' {
2292            let mut key = String::new();
2293            let mut closed = false;
2294            for kc in chars.by_ref() {
2295                if kc == '}' {
2296                    closed = true;
2297                    break;
2298                }
2299                key.push(kc);
2300            }
2301            if closed && !key.is_empty() {
2302                if !static_buf.is_empty() {
2303                    let escaped = escape_elixir(&static_buf);
2304                    parts.push(format!("\"{escaped}\""));
2305                    static_buf.clear();
2306                }
2307                let escaped_key = escape_elixir(&key);
2308                parts.push(format!("Map.get(args, \"{escaped_key}\", \"\")"));
2309            } else {
2310                static_buf.push('{');
2311                static_buf.push_str(&key);
2312                if !closed {
2313                    // unclosed brace — treat remaining as literal
2314                }
2315            }
2316        } else {
2317            static_buf.push(ch);
2318        }
2319    }
2320
2321    if !static_buf.is_empty() {
2322        let escaped = escape_elixir(&static_buf);
2323        parts.push(format!("\"{escaped}\""));
2324    }
2325
2326    if parts.is_empty() {
2327        return "\"\"".to_string();
2328    }
2329    parts.join(" <> ")
2330}
2331
2332fn fixture_has_elixir_callable(fixture: &Fixture, e2e_config: &E2eConfig) -> bool {
2333    // HTTP fixtures are handled separately — not our concern here.
2334    if fixture.is_http_test() {
2335        return false;
2336    }
2337    let call_config = e2e_config.resolve_call_for_fixture(
2338        fixture.call.as_deref(),
2339        &fixture.id,
2340        &fixture.resolved_category(),
2341        &fixture.tags,
2342        &fixture.input,
2343    );
2344    let elixir_override = call_config
2345        .overrides
2346        .get("elixir")
2347        .or_else(|| e2e_config.call.overrides.get("elixir"));
2348    // When a client_factory is configured the fixture is callable via the client pattern.
2349    if elixir_override.and_then(|o| o.client_factory.as_deref()).is_some() {
2350        return true;
2351    }
2352    // Elixir bindings expose functions via module-level callables.
2353    // Like Python and Node, Elixir can call the base function directly without requiring
2354    // a language-specific override. The function can come from either the override or
2355    // the default [e2e.call] configuration.
2356    let function_from_override = elixir_override.and_then(|o| o.function.as_deref());
2357
2358    // If there's an override function, use it. Otherwise, Elixir can use the base function.
2359    function_from_override.is_some() || !call_config.function.is_empty()
2360}