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    ) -> Result<Vec<GeneratedFile>> {
31        let lang = self.language_name();
32        let output_base = PathBuf::from(e2e_config.effective_output()).join(lang);
33
34        let mut files = Vec::new();
35
36        // Resolve call config with overrides.
37        let call = &e2e_config.call;
38        let overrides = call.overrides.get(lang);
39        let raw_module = overrides
40            .and_then(|o| o.module.as_ref())
41            .cloned()
42            .unwrap_or_else(|| call.module.clone());
43        // Convert module path to Elixir PascalCase if it looks like snake_case
44        // (e.g., "html_to_markdown" -> "HtmlToMarkdown").
45        // If the override already contains "." (e.g., "Elixir.HtmlToMarkdown"), use as-is.
46        let module_path = if raw_module.contains('.') || raw_module.chars().next().is_some_and(|c| c.is_uppercase()) {
47            raw_module.clone()
48        } else {
49            elixir_module_name(&raw_module)
50        };
51        let base_function_name = overrides
52            .and_then(|o| o.function.as_ref())
53            .cloned()
54            .unwrap_or_else(|| call.function.clone());
55        // Elixir facade exports async variants with `_async` suffix when the call is async.
56        // Append the suffix only if not already present and the function isn't a streaming
57        // entry-point — streaming wrappers (e.g. `defaultclient_chat_stream`) drive the
58        // FFI iterator handle and aren't async-callable in the OpenAI sense.
59        let function_name =
60            if call.r#async && !base_function_name.ends_with("_async") && !base_function_name.ends_with("_stream") {
61                format!("{base_function_name}_async")
62            } else {
63                base_function_name
64            };
65        let options_type = overrides.and_then(|o| o.options_type.clone());
66        let options_default_fn = overrides.and_then(|o| o.options_via.clone());
67        let empty_enum_fields = HashMap::new();
68        let enum_fields = overrides.map(|o| &o.enum_fields).unwrap_or(&empty_enum_fields);
69        let handle_struct_type = overrides.and_then(|o| o.handle_struct_type.clone());
70        let empty_atom_fields = std::collections::HashSet::new();
71        let handle_atom_list_fields = overrides
72            .map(|o| &o.handle_atom_list_fields)
73            .unwrap_or(&empty_atom_fields);
74        let result_var = &call.result_var;
75
76        // Check if any fixture in any group is an HTTP test.
77        let has_http_tests = groups.iter().any(|g| g.fixtures.iter().any(|f| f.is_http_test()));
78        let has_nif_tests = groups.iter().any(|g| g.fixtures.iter().any(|f| !f.is_http_test()));
79        // Check if any fixture needs the mock server (either via http or mock_response or client_factory).
80        let has_mock_server_tests = groups.iter().any(|g| {
81            g.fixtures.iter().any(|f| {
82                if f.needs_mock_server() {
83                    return true;
84                }
85                let cc = e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.input);
86                let elixir_override = cc
87                    .overrides
88                    .get("elixir")
89                    .or_else(|| e2e_config.call.overrides.get("elixir"));
90                elixir_override.and_then(|o| o.client_factory.as_deref()).is_some()
91            })
92        });
93
94        // Resolve package reference (path or version) for the NIF dependency.
95        let pkg_ref = e2e_config.resolve_package(lang);
96        let pkg_path = if has_nif_tests {
97            pkg_ref
98                .as_ref()
99                .and_then(|p| p.path.as_deref())
100                .unwrap_or("../../packages/elixir")
101        } else {
102            ""
103        };
104
105        // Generate mix.exs. The dep atom must match the binding package's
106        // mix `app:` value, not the crate name. Use the configured
107        // `[elixir].app_name` (the same source the package's own mix.exs
108        // uses); fall back to the crate name only when unset. Without this,
109        // mix's path-dep resolution silently misroutes — the path-dep's
110        // own deps (notably `:rustler_precompiled`) never load during its
111        // compilation and the parent build fails with `RustlerPrecompiled
112        // is not loaded`.
113        let pkg_atom = config.elixir_app_name();
114        files.push(GeneratedFile {
115            path: output_base.join("mix.exs"),
116            content: render_mix_exs(&pkg_atom, pkg_path, e2e_config.dep_mode, has_http_tests, has_nif_tests),
117            generated_header: false,
118        });
119
120        // Generate lib/e2e_elixir.ex — required so the mix project compiles.
121        files.push(GeneratedFile {
122            path: output_base.join("lib").join("e2e_elixir.ex"),
123            content: "defmodule E2eElixir do\n  @moduledoc false\nend\n".to_string(),
124            generated_header: false,
125        });
126
127        // Generate test_helper.exs.
128        files.push(GeneratedFile {
129            path: output_base.join("test").join("test_helper.exs"),
130            content: render_test_helper(has_http_tests || has_mock_server_tests),
131            generated_header: false,
132        });
133
134        // Generate test files per category.
135        for group in groups {
136            let active: Vec<&Fixture> = group
137                .fixtures
138                .iter()
139                .filter(|f| super::should_include_fixture(f, lang, e2e_config))
140                .collect();
141
142            if active.is_empty() {
143                continue;
144            }
145
146            let filename = format!("{}_test.exs", sanitize_filename(&group.category));
147            let field_resolver = FieldResolver::new(
148                &e2e_config.fields,
149                &e2e_config.fields_optional,
150                &e2e_config.result_fields,
151                &e2e_config.fields_array,
152                &std::collections::HashSet::new(),
153            );
154            let content = render_test_file(
155                &group.category,
156                &active,
157                e2e_config,
158                &module_path,
159                &function_name,
160                result_var,
161                &e2e_config.call.args,
162                &field_resolver,
163                options_type.as_deref(),
164                options_default_fn.as_deref(),
165                enum_fields,
166                handle_struct_type.as_deref(),
167                handle_atom_list_fields,
168            );
169            files.push(GeneratedFile {
170                path: output_base.join("test").join(filename),
171                content,
172                generated_header: true,
173            });
174        }
175
176        Ok(files)
177    }
178
179    fn language_name(&self) -> &'static str {
180        "elixir"
181    }
182}
183
184fn render_test_helper(has_http_tests: bool) -> String {
185    if has_http_tests {
186        r#"ExUnit.start()
187
188# Spawn mock-server binary and set MOCK_SERVER_URL for all tests.
189mock_server_bin = Path.expand("../../rust/target/release/mock-server", __DIR__)
190fixtures_dir = Path.expand("../../../fixtures", __DIR__)
191
192if File.exists?(mock_server_bin) do
193  port = Port.open({:spawn_executable, mock_server_bin}, [
194    :binary,
195    :line,
196    args: [fixtures_dir]
197  ])
198  receive do
199    {^port, {:data, {:eol, "MOCK_SERVER_URL=" <> url}}} ->
200      System.put_env("MOCK_SERVER_URL", url)
201  after
202    30_000 ->
203      raise "mock-server startup timeout"
204  end
205end
206"#
207        .to_string()
208    } else {
209        "ExUnit.start()\n".to_string()
210    }
211}
212
213fn render_mix_exs(
214    pkg_name: &str,
215    pkg_path: &str,
216    dep_mode: crate::config::DependencyMode,
217    has_http_tests: bool,
218    has_nif_tests: bool,
219) -> String {
220    let mut out = String::new();
221    let _ = writeln!(out, "defmodule E2eElixir.MixProject do");
222    let _ = writeln!(out, "  use Mix.Project");
223    let _ = writeln!(out);
224    let _ = writeln!(out, "  def project do");
225    let _ = writeln!(out, "    [");
226    let _ = writeln!(out, "      app: :e2e_elixir,");
227    let _ = writeln!(out, "      version: \"0.1.0\",");
228    let _ = writeln!(out, "      elixir: \"~> 1.14\",");
229    let _ = writeln!(out, "      deps: deps()");
230    let _ = writeln!(out, "    ]");
231    let _ = writeln!(out, "  end");
232    let _ = writeln!(out);
233    let _ = writeln!(out, "  defp deps do");
234    let _ = writeln!(out, "    [");
235
236    // Build the list of deps, then join with commas to avoid double-commas.
237    let mut deps: Vec<String> = Vec::new();
238
239    // Add the binding NIF dependency when there are non-HTTP tests.
240    if has_nif_tests && !pkg_path.is_empty() {
241        let pkg_atom = pkg_name;
242        let nif_dep = match dep_mode {
243            crate::config::DependencyMode::Local => {
244                format!("      {{:{pkg_atom}, path: \"{pkg_path}\"}}")
245            }
246            crate::config::DependencyMode::Registry => {
247                // Registry mode: pkg_path is repurposed as the version string.
248                format!("      {{:{pkg_atom}, \"{pkg_path}\"}}")
249            }
250        };
251        deps.push(nif_dep);
252        // rustler_precompiled provides the precompiled NIF loader.
253        deps.push(format!(
254            "      {{:rustler_precompiled, \"{rp}\"}}",
255            rp = tv::hex::RUSTLER_PRECOMPILED
256        ));
257        // rustler must be a direct, non-optional dep in the consumer project for
258        // `force_build: Mix.env() in [:test, :dev]` to actually fetch the rustler hex
259        // package. With `optional: true` mix omits it when no other dep declares it as
260        // required, breaking the build-from-source path used by the e2e suite.
261        deps.push(format!(
262            "      {{:rustler, \"{rustler}\", runtime: false}}",
263            rustler = tv::hex::RUSTLER
264        ));
265    }
266
267    // Add Req + Jason for HTTP testing.
268    if has_http_tests {
269        deps.push(format!("      {{:req, \"{req}\"}}", req = tv::hex::REQ));
270        deps.push(format!("      {{:jason, \"{jason}\"}}", jason = tv::hex::JASON));
271    }
272
273    let _ = writeln!(out, "{}", deps.join(",\n"));
274    let _ = writeln!(out, "    ]");
275    let _ = writeln!(out, "  end");
276    let _ = writeln!(out, "end");
277    out
278}
279
280#[allow(clippy::too_many_arguments)]
281fn render_test_file(
282    category: &str,
283    fixtures: &[&Fixture],
284    e2e_config: &E2eConfig,
285    module_path: &str,
286    function_name: &str,
287    result_var: &str,
288    args: &[crate::config::ArgMapping],
289    field_resolver: &FieldResolver,
290    options_type: Option<&str>,
291    options_default_fn: Option<&str>,
292    enum_fields: &HashMap<String, String>,
293    handle_struct_type: Option<&str>,
294    handle_atom_list_fields: &std::collections::HashSet<String>,
295) -> String {
296    let mut out = String::new();
297    out.push_str(&hash::header(CommentStyle::Hash));
298    let _ = writeln!(out, "# E2e tests for category: {category}");
299    let _ = writeln!(out, "defmodule E2e.{}Test do", elixir_module_name(category));
300
301    // Add client helper when there are HTTP fixtures in this group.
302    let has_http = fixtures.iter().any(|f| f.is_http_test());
303
304    // Use async: false for NIF tests — concurrent Tokio runtimes created by DirtyCpu NIFs
305    // on ARM64 macOS cause SIGBUS when tests run in parallel. HTTP-only tests can stay async.
306    let async_flag = if has_http { "true" } else { "false" };
307    let _ = writeln!(out, "  use ExUnit.Case, async: {async_flag}");
308
309    if has_http {
310        let _ = writeln!(out);
311        let _ = writeln!(out, "  defp mock_server_url do");
312        let _ = writeln!(
313            out,
314            "    System.get_env(\"MOCK_SERVER_URL\") || \"http://localhost:8080\""
315        );
316        let _ = writeln!(out, "  end");
317    }
318
319    // Emit a shared helper for array field contains assertions — extracts string
320    // representations from each item's attributes so String.contains? works on struct lists.
321    let has_array_contains = fixtures.iter().any(|fixture| {
322        fixture.assertions.iter().any(|a| {
323            matches!(a.assertion_type.as_str(), "contains" | "contains_all" | "not_contains")
324                && a.field
325                    .as_deref()
326                    .is_some_and(|f| !f.is_empty() && field_resolver.is_array(field_resolver.resolve(f)))
327        })
328    });
329    if has_array_contains {
330        let _ = writeln!(out);
331        let _ = writeln!(out, "  defp alef_e2e_item_texts(item) when is_binary(item), do: [item]");
332        let _ = writeln!(out, "  defp alef_e2e_item_texts(item) do");
333        let _ = writeln!(out, "    [:kind, :name, :signature, :path, :alias, :text, :source]");
334        let _ = writeln!(out, "    |> Enum.filter(&Map.has_key?(item, &1))");
335        let _ = writeln!(out, "    |> Enum.flat_map(fn attr ->");
336        let _ = writeln!(out, "      case Map.get(item, attr) do");
337        let _ = writeln!(out, "        nil -> []");
338        let _ = writeln!(
339            out,
340            "        atom when is_atom(atom) -> [atom |> to_string() |> String.capitalize()]"
341        );
342        let _ = writeln!(out, "        str -> [to_string(str)]");
343        let _ = writeln!(out, "      end");
344        let _ = writeln!(out, "    end)");
345        let _ = writeln!(out, "  end");
346    }
347
348    let _ = writeln!(out);
349
350    for (i, fixture) in fixtures.iter().enumerate() {
351        if let Some(http) = &fixture.http {
352            render_http_test_case(&mut out, fixture, http);
353        } else {
354            render_test_case(
355                &mut out,
356                fixture,
357                e2e_config,
358                module_path,
359                function_name,
360                result_var,
361                args,
362                field_resolver,
363                options_type,
364                options_default_fn,
365                enum_fields,
366                handle_struct_type,
367                handle_atom_list_fields,
368            );
369        }
370        if i + 1 < fixtures.len() {
371            let _ = writeln!(out);
372        }
373    }
374
375    let _ = writeln!(out, "end");
376    out
377}
378
379// ---------------------------------------------------------------------------
380// HTTP test rendering
381// ---------------------------------------------------------------------------
382
383/// HTTP methods that Finch (Req's underlying HTTP client) does not support.
384/// Tests using these methods are emitted with `@tag :skip` so they don't fail.
385const FINCH_UNSUPPORTED_METHODS: &[&str] = &["TRACE", "CONNECT"];
386
387/// HTTP methods that Req exposes as convenience functions.
388/// All others must be called via `Req.request(method: :METHOD, ...)`.
389const REQ_CONVENIENCE_METHODS: &[&str] = &["get", "post", "put", "patch", "delete", "head"];
390
391/// Thin renderer that emits ExUnit `describe` + `test` blocks targeting a mock
392/// server via `Req`. Satisfies [`client::TestClientRenderer`] so the shared
393/// [`client::http_call::render_http_test`] driver drives the call sequence.
394struct ElixirTestClientRenderer<'a> {
395    /// The fixture id is needed in [`render_call`] to build the mock server URL
396    /// (`mock_server_url()/fixtures/<id>`).
397    fixture_id: &'a str,
398    /// Expected response status, needed to disable Req's redirect-following for 3xx.
399    expected_status: u16,
400}
401
402impl<'a> client::TestClientRenderer for ElixirTestClientRenderer<'a> {
403    fn language_name(&self) -> &'static str {
404        "elixir"
405    }
406
407    /// Emit `describe "{fn_name}" do` + inner `test "METHOD PATH - description" do`.
408    ///
409    /// When `skip_reason` is `Some`, emit `@tag :skip` before the test block so
410    /// ExUnit skips it; the shared driver short-circuits before emitting any
411    /// assertions and then calls `render_test_close` for symmetry.
412    fn render_test_open(&self, out: &mut String, fn_name: &str, description: &str, skip_reason: Option<&str>) {
413        let escaped_description = description.replace('"', "\\\"");
414        let _ = writeln!(out, "  describe \"{fn_name}\" do");
415        if skip_reason.is_some() {
416            let _ = writeln!(out, "    @tag :skip");
417        }
418        let _ = writeln!(out, "    test \"{escaped_description}\" do");
419    }
420
421    /// Close the inner `test` block and the outer `describe` block.
422    fn render_test_close(&self, out: &mut String) {
423        let _ = writeln!(out, "    end");
424        let _ = writeln!(out, "  end");
425    }
426
427    /// Emit a `Req` request to the mock server using `mock_server_url()/fixtures/<id>`.
428    fn render_call(&self, out: &mut String, ctx: &client::CallCtx<'_>) {
429        let method = ctx.method.to_lowercase();
430        let mut opts: Vec<String> = Vec::new();
431
432        if let Some(body) = ctx.body {
433            let elixir_val = json_to_elixir(body);
434            opts.push(format!("json: {elixir_val}"));
435        }
436
437        if !ctx.headers.is_empty() {
438            let header_pairs: Vec<String> = ctx
439                .headers
440                .iter()
441                .map(|(k, v)| format!("{{\"{}\", \"{}\"}}", escape_elixir(k), escape_elixir(v)))
442                .collect();
443            opts.push(format!("headers: [{}]", header_pairs.join(", ")));
444        }
445
446        if !ctx.cookies.is_empty() {
447            let cookie_str = ctx
448                .cookies
449                .iter()
450                .map(|(k, v)| format!("{k}={v}"))
451                .collect::<Vec<_>>()
452                .join("; ");
453            opts.push(format!("headers: [{{\"cookie\", \"{}\"}}]", escape_elixir(&cookie_str)));
454        }
455
456        if !ctx.query_params.is_empty() {
457            let pairs: Vec<String> = ctx
458                .query_params
459                .iter()
460                .map(|(k, v)| {
461                    let val_str = match v {
462                        serde_json::Value::String(s) => s.clone(),
463                        other => other.to_string(),
464                    };
465                    format!("{{\"{}\", \"{}\"}}", escape_elixir(k), escape_elixir(&val_str))
466                })
467                .collect();
468            opts.push(format!("params: [{}]", pairs.join(", ")));
469        }
470
471        // When the expected response is a redirect (3xx), disable automatic redirect
472        // following so the test can assert the redirect status and Location header.
473        if (300..400).contains(&self.expected_status) {
474            opts.push("redirect: false".to_string());
475        }
476
477        let fixture_id = escape_elixir(self.fixture_id);
478        let url_expr = format!("\"#{{mock_server_url()}}/fixtures/{fixture_id}\"");
479
480        if REQ_CONVENIENCE_METHODS.contains(&method.as_str()) {
481            if opts.is_empty() {
482                let _ = writeln!(out, "      {{:ok, response}} = Req.{method}(url: {url_expr})");
483            } else {
484                let opts_str = opts.join(", ");
485                let _ = writeln!(
486                    out,
487                    "      {{:ok, response}} = Req.{method}(url: {url_expr}, {opts_str})"
488                );
489            }
490        } else {
491            opts.insert(0, format!("method: :{method}"));
492            opts.insert(1, format!("url: {url_expr}"));
493            let opts_str = opts.join(", ");
494            let _ = writeln!(out, "      {{:ok, response}} = Req.request({opts_str})");
495        }
496    }
497
498    fn render_assert_status(&self, out: &mut String, response_var: &str, status: u16) {
499        let _ = writeln!(out, "      assert {response_var}.status == {status}");
500    }
501
502    /// Emit a header assertion.
503    ///
504    /// Handles the special tokens `<<present>>`, `<<absent>>`, `<<uuid>>`.
505    /// Skips the `connection` header (hop-by-hop, stripped by Req/Mint).
506    fn render_assert_header(&self, out: &mut String, response_var: &str, name: &str, expected: &str) {
507        let header_key = name.to_lowercase();
508        // Req (via Mint) strips hop-by-hop headers; asserting on them is meaningless.
509        if header_key == "connection" {
510            return;
511        }
512        let key_lit = format!("\"{}\"", escape_elixir(&header_key));
513        let get_header_expr = format!(
514            "Enum.find_value({response_var}.headers, fn {{k, v}} -> if String.downcase(k) == {key_lit}, do: List.first(List.wrap(v)) end)"
515        );
516        match expected {
517            "<<present>>" => {
518                let _ = writeln!(out, "      assert {get_header_expr} != nil");
519            }
520            "<<absent>>" => {
521                let _ = writeln!(out, "      assert {get_header_expr} == nil");
522            }
523            "<<uuid>>" => {
524                let var = sanitize_ident(&header_key);
525                let _ = writeln!(out, "      header_val_{var} = {get_header_expr}");
526                let _ = writeln!(
527                    out,
528                    "      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}))"
529                );
530            }
531            literal => {
532                let val_lit = format!("\"{}\"", escape_elixir(literal));
533                let _ = writeln!(out, "      assert {get_header_expr} == {val_lit}");
534            }
535        }
536    }
537
538    /// Emit a full JSON body equality assertion.
539    ///
540    /// Req auto-decodes `application/json` bodies; when the response body is a
541    /// binary (non-JSON content type), decode it with `Jason.decode!` first.
542    fn render_assert_json_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
543        let elixir_val = json_to_elixir(expected);
544        match expected {
545            serde_json::Value::Object(_) | serde_json::Value::Array(_) => {
546                let _ = writeln!(
547                    out,
548                    "      body_decoded = if is_binary({response_var}.body), do: Jason.decode!({response_var}.body), else: {response_var}.body"
549                );
550                let _ = writeln!(out, "      assert body_decoded == {elixir_val}");
551            }
552            _ => {
553                let _ = writeln!(out, "      assert {response_var}.body == {elixir_val}");
554            }
555        }
556    }
557
558    /// Emit partial body assertions: one assertion per key in `expected`.
559    fn render_assert_partial_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
560        if let Some(obj) = expected.as_object() {
561            let _ = writeln!(
562                out,
563                "      decoded_body = if is_binary({response_var}.body), do: Jason.decode!({response_var}.body), else: {response_var}.body"
564            );
565            for (key, val) in obj {
566                let key_lit = format!("\"{}\"", escape_elixir(key));
567                let elixir_val = json_to_elixir(val);
568                let _ = writeln!(out, "      assert decoded_body[{key_lit}] == {elixir_val}");
569            }
570        }
571    }
572
573    /// Emit validation-error assertions, checking each expected `msg` appears in
574    /// the encoded response body.
575    fn render_assert_validation_errors(
576        &self,
577        out: &mut String,
578        response_var: &str,
579        errors: &[ValidationErrorExpectation],
580    ) {
581        for err in errors {
582            let msg_lit = format!("\"{}\"", escape_elixir(&err.msg));
583            let _ = writeln!(
584                out,
585                "      assert String.contains?(Jason.encode!({response_var}.body), {msg_lit})"
586            );
587        }
588    }
589}
590
591/// Render an ExUnit `describe` + `test` block for an HTTP server test fixture.
592///
593/// Delegates to [`client::http_call::render_http_test`] after the one
594/// Elixir-specific pre-condition: HTTP methods unsupported by Finch (the
595/// underlying Req adapter) are emitted with `@tag :skip` directly.
596fn render_http_test_case(out: &mut String, fixture: &Fixture, http: &HttpFixture) {
597    let method = http.request.method.to_uppercase();
598
599    // Finch does not support TRACE/CONNECT — emit a skipped test stub directly
600    // rather than routing through the shared driver, which would assert on the
601    // response and fail.
602    if FINCH_UNSUPPORTED_METHODS.contains(&method.as_str()) {
603        let test_name = sanitize_ident(&fixture.id);
604        let test_label = fixture.id.replace('"', "\\\"");
605        let path = &http.request.path;
606        let _ = writeln!(out, "  describe \"{test_name}\" do");
607        let _ = writeln!(out, "    @tag :skip");
608        let _ = writeln!(out, "    test \"{method} {path} - {test_label}\" do");
609        let _ = writeln!(out, "    end");
610        let _ = writeln!(out, "  end");
611        return;
612    }
613
614    let renderer = ElixirTestClientRenderer {
615        fixture_id: &fixture.id,
616        expected_status: http.expected_response.status_code,
617    };
618    client::http_call::render_http_test(out, &renderer, fixture);
619}
620
621// ---------------------------------------------------------------------------
622// Function-call test rendering
623// ---------------------------------------------------------------------------
624
625#[allow(clippy::too_many_arguments)]
626fn render_test_case(
627    out: &mut String,
628    fixture: &Fixture,
629    e2e_config: &E2eConfig,
630    default_module_path: &str,
631    default_function_name: &str,
632    default_result_var: &str,
633    args: &[crate::config::ArgMapping],
634    field_resolver: &FieldResolver,
635    options_type: Option<&str>,
636    options_default_fn: Option<&str>,
637    enum_fields: &HashMap<String, String>,
638    handle_struct_type: Option<&str>,
639    handle_atom_list_fields: &std::collections::HashSet<String>,
640) {
641    let test_name = sanitize_ident(&fixture.id);
642    let test_label = fixture.id.replace('"', "\\\"");
643
644    // Non-HTTP non-mock_response fixtures (e.g. AsyncAPI, WebSocket, OpenRPC
645    // protocol-only fixtures) cannot be tested via the configured `[e2e.call]`
646    // function when the binding does not expose it. Emit a documented `@tag :skip`
647    // test so the suite stays compilable. HTTP fixtures dispatch via render_http_test_case
648    // and never reach here.
649    if fixture.mock_response.is_none() && !fixture_has_elixir_callable(fixture, e2e_config) {
650        let _ = writeln!(out, "  describe \"{test_name}\" do");
651        let _ = writeln!(out, "    @tag :skip");
652        let _ = writeln!(out, "    test \"{test_label}\" do");
653        let _ = writeln!(
654            out,
655            "      # non-HTTP fixture: Elixir binding does not expose a callable for the configured `[e2e.call]` function"
656        );
657        let _ = writeln!(out, "      :ok");
658        let _ = writeln!(out, "    end");
659        let _ = writeln!(out, "  end");
660        return;
661    }
662
663    // Resolve per-fixture call config (falls back to default if fixture.call is None).
664    let call_config = e2e_config.resolve_call_for_fixture(fixture.call.as_deref(), &fixture.input);
665    let lang = "elixir";
666    let call_overrides = call_config.overrides.get(lang);
667
668    // Check if the function is excluded from the Elixir binding (e.g., batch functions
669    // that require unsafe NIF tuple marshalling). Emit a skipped test with rationale.
670    let base_fn = call_overrides
671        .and_then(|o| o.function.as_ref())
672        .cloned()
673        .unwrap_or_else(|| call_config.function.clone());
674    if base_fn.starts_with("batch_extract_") {
675        let _ = writeln!(
676            out,
677            "  describe \"{test_name}\" do",
678            test_name = sanitize_ident(&fixture.id)
679        );
680        let _ = writeln!(out, "    @tag :skip");
681        let _ = writeln!(
682            out,
683            "    test \"{test_label}\" do",
684            test_label = fixture.id.replace('"', "\\\"")
685        );
686        let _ = writeln!(
687            out,
688            "      # batch functions excluded from Elixir binding: unsafe NIF tuple marshalling"
689        );
690        let _ = writeln!(out, "      :ok");
691        let _ = writeln!(out, "    end");
692        let _ = writeln!(out, "  end");
693        return;
694    }
695
696    // Compute module_path and function_name from the resolved call config,
697    // applying Elixir-specific PascalCase conversion.
698    let (module_path, function_name, result_var) = if fixture.call.is_some() {
699        let raw_module = call_overrides
700            .and_then(|o| o.module.as_ref())
701            .cloned()
702            .unwrap_or_else(|| call_config.module.clone());
703        let resolved_module = if raw_module.contains('.') || raw_module.chars().next().is_some_and(|c| c.is_uppercase())
704        {
705            raw_module.clone()
706        } else {
707            elixir_module_name(&raw_module)
708        };
709        let resolved_fn = if call_config.r#async && !base_fn.ends_with("_async") && !base_fn.ends_with("_stream") {
710            format!("{base_fn}_async")
711        } else {
712            base_fn
713        };
714        (resolved_module, resolved_fn, call_config.result_var.clone())
715    } else {
716        (
717            default_module_path.to_string(),
718            default_function_name.to_string(),
719            default_result_var.to_string(),
720        )
721    };
722
723    let expects_error = fixture.assertions.iter().any(|a| a.assertion_type == "error");
724
725    // When the fixture uses a named call, use the args and options from that call's config.
726    let (
727        effective_args,
728        effective_options_type,
729        effective_options_default_fn,
730        effective_enum_fields,
731        effective_handle_struct_type,
732        effective_handle_atom_list_fields,
733    );
734    let empty_enum_fields_local: HashMap<String, String>;
735    let empty_atom_fields_local: std::collections::HashSet<String>;
736    let (
737        resolved_args,
738        resolved_options_type,
739        resolved_options_default_fn,
740        resolved_enum_fields_ref,
741        resolved_handle_struct_type,
742        resolved_handle_atom_list_fields_ref,
743    ) = if fixture.call.is_some() {
744        let co = call_config.overrides.get(lang);
745        effective_args = call_config.args.as_slice();
746        effective_options_type = co.and_then(|o| o.options_type.as_deref());
747        effective_options_default_fn = co.and_then(|o| o.options_via.as_deref());
748        empty_enum_fields_local = HashMap::new();
749        effective_enum_fields = co.map(|o| &o.enum_fields).unwrap_or(&empty_enum_fields_local);
750        effective_handle_struct_type = co.and_then(|o| o.handle_struct_type.as_deref());
751        empty_atom_fields_local = std::collections::HashSet::new();
752        effective_handle_atom_list_fields = co
753            .map(|o| &o.handle_atom_list_fields)
754            .unwrap_or(&empty_atom_fields_local);
755        (
756            effective_args,
757            effective_options_type,
758            effective_options_default_fn,
759            effective_enum_fields,
760            effective_handle_struct_type,
761            effective_handle_atom_list_fields,
762        )
763    } else {
764        (
765            args as &[_],
766            options_type,
767            options_default_fn,
768            enum_fields,
769            handle_struct_type,
770            handle_atom_list_fields,
771        )
772    };
773
774    let (mut setup_lines, args_str) = build_args_and_setup(
775        &fixture.input,
776        resolved_args,
777        &module_path,
778        resolved_options_type,
779        resolved_options_default_fn,
780        resolved_enum_fields_ref,
781        &fixture.id,
782        resolved_handle_struct_type,
783        resolved_handle_atom_list_fields_ref,
784    );
785
786    // Build visitor if present — it will be injected into the options map.
787    let visitor_var = fixture
788        .visitor
789        .as_ref()
790        .map(|visitor_spec| build_elixir_visitor(&mut setup_lines, visitor_spec));
791
792    // If we have a visitor and the args contain a nil for options, replace it with a map
793    // containing the visitor. The fixture.visitor is already set above.
794    let final_args = if let Some(ref visitor_var) = visitor_var {
795        // Parse args_str to handle injection properly.
796        // Since we're dealing with a 2-arg function (html, options), and options might be nil,
797        // we need to inject visitor into the options.
798        let parts: Vec<&str> = args_str.split(", ").collect();
799        if parts.len() == 2 && parts[1] == "nil" {
800            // Replace nil with %{visitor: visitor}
801            format!("{}, %{{visitor: {}}}", parts[0], visitor_var)
802        } else if parts.len() == 2 {
803            // Options is a variable (e.g., "options") — add visitor to it
804            setup_lines.push(format!(
805                "{} = Map.put({}, :visitor, {})",
806                parts[1], parts[1], visitor_var
807            ));
808            args_str
809        } else if parts.len() == 1 {
810            // Only HTML provided — create options map with just visitor
811            format!("{}, %{{visitor: {}}}", parts[0], visitor_var)
812        } else {
813            args_str
814        }
815    } else {
816        args_str
817    };
818
819    // Client factory: when configured, create a client and pass it as the first argument.
820    let client_factory = call_overrides.and_then(|o| o.client_factory.as_deref()).or_else(|| {
821        e2e_config
822            .call
823            .overrides
824            .get("elixir")
825            .and_then(|o| o.client_factory.as_deref())
826    });
827
828    // Append per-call extra_args (e.g. trailing `nil` for `list_files(client, query)`)
829    // so Elixir matches the binding's positional arity. Mirrors the same override the
830    // Ruby/Go/Node codegens already honor.
831    let extra_args: Vec<String> = call_overrides.map(|o| o.extra_args.clone()).unwrap_or_default();
832    let final_args_with_extras = if extra_args.is_empty() {
833        final_args
834    } else if final_args.is_empty() {
835        extra_args.join(", ")
836    } else {
837        format!("{final_args}, {}", extra_args.join(", "))
838    };
839
840    // Prefix the client variable to the args when client_factory is set.
841    let effective_args = if client_factory.is_some() {
842        if final_args_with_extras.is_empty() {
843            "client".to_string()
844        } else {
845            format!("client, {final_args_with_extras}")
846        }
847    } else {
848        final_args_with_extras
849    };
850
851    // Real-API smoke fixtures (no mock_response, no http) must be env-gated on the
852    // configured `env.api_key_var` so absent keys yield a deterministic skip rather
853    // than a spurious "no mock route" failure. Mirrors the Python conftest skip.
854    let needs_api_key_skip = fixture.mock_response.is_none()
855        && fixture.http.is_none()
856        && fixture.env.as_ref().and_then(|e| e.api_key_var.as_deref()).is_some();
857
858    let _ = writeln!(out, "  describe \"{test_name}\" do");
859    let _ = writeln!(out, "    test \"{test_label}\" do");
860
861    if needs_api_key_skip {
862        let api_key_var = fixture
863            .env
864            .as_ref()
865            .and_then(|e| e.api_key_var.as_deref())
866            .unwrap_or("");
867        let _ = writeln!(out, "      if System.get_env(\"{api_key_var}\") in [nil, \"\"] do");
868        let _ = writeln!(out, "        # {api_key_var} not set — skipping live smoke test");
869        let _ = writeln!(out, "        :ok");
870        let _ = writeln!(out, "      else");
871    }
872
873    for line in &setup_lines {
874        let _ = writeln!(out, "      {line}");
875    }
876
877    // Emit client creation when client_factory is configured.
878    if let Some(factory) = client_factory {
879        let fixture_id = &fixture.id;
880        let _ = writeln!(
881            out,
882            "      {{:ok, client}} = {module_path}.{factory}(\"test-key\", (System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\")"
883        );
884    }
885
886    // Use returns_result from the Elixir override if present, otherwise from base config
887    let returns_result = call_overrides
888        .and_then(|o| o.returns_result)
889        .unwrap_or(call_config.returns_result || client_factory.is_some());
890
891    // Some calls (e.g. speech, file_content) return raw bytes rather than a struct.
892    // When the call is marked `result_is_simple`, treat the bound `result` variable as
893    // the value itself so assertions on a logical "audio"/"content" field map to the
894    // bare binary instead of a struct accessor that doesn't exist.
895    let result_is_simple = call_config.result_is_simple || call_overrides.is_some_and(|o| o.result_is_simple);
896
897    if expects_error {
898        if returns_result {
899            let _ = writeln!(
900                out,
901                "      assert {{:error, _}} = {module_path}.{function_name}({effective_args})"
902            );
903        } else {
904            // Non-Result function — just call and discard; error detection not meaningful.
905            let _ = writeln!(out, "      _result = {module_path}.{function_name}({effective_args})");
906        }
907        if needs_api_key_skip {
908            let _ = writeln!(out, "      end");
909        }
910        let _ = writeln!(out, "    end");
911        let _ = writeln!(out, "  end");
912        return;
913    }
914
915    if returns_result {
916        let _ = writeln!(
917            out,
918            "      {{:ok, {result_var}}} = {module_path}.{function_name}({effective_args})"
919        );
920    } else {
921        // Non-Result function returns value directly (e.g., bool, String).
922        let _ = writeln!(
923            out,
924            "      {result_var} = {module_path}.{function_name}({effective_args})"
925        );
926    }
927
928    for assertion in &fixture.assertions {
929        render_assertion(
930            out,
931            assertion,
932            &result_var,
933            field_resolver,
934            &module_path,
935            &e2e_config.fields_enum,
936            result_is_simple,
937        );
938    }
939
940    if needs_api_key_skip {
941        let _ = writeln!(out, "      end");
942    }
943    let _ = writeln!(out, "    end");
944    let _ = writeln!(out, "  end");
945}
946
947/// Build setup lines (e.g. handle creation) and the argument list for the function call.
948///
949/// Returns `(setup_lines, args_string)`.
950#[allow(clippy::too_many_arguments)]
951/// Emit Elixir batch item map constructors for BatchBytesItem or BatchFileItem arrays.
952fn emit_elixir_batch_item_array(arr: &serde_json::Value, elem_type: &str) -> String {
953    if let Some(items) = arr.as_array() {
954        let item_strs: Vec<String> = items
955            .iter()
956            .filter_map(|item| {
957                if let Some(obj) = item.as_object() {
958                    match elem_type {
959                        "BatchBytesItem" => {
960                            let content = obj.get("content").and_then(|v| v.as_array());
961                            let mime_type = obj.get("mime_type").and_then(|v| v.as_str()).unwrap_or("text/plain");
962                            let content_code = if let Some(arr) = content {
963                                let bytes: Vec<String> =
964                                    arr.iter().filter_map(|v| v.as_u64().map(|n| n.to_string())).collect();
965                                format!("<<{}>>", bytes.join(", "))
966                            } else {
967                                "<<>>".to_string()
968                            };
969                            Some(format!(
970                                "%BatchBytesItem{{content: {}, mime_type: \"{}\"}}",
971                                content_code, mime_type
972                            ))
973                        }
974                        "BatchFileItem" => {
975                            let path = obj.get("path").and_then(|v| v.as_str()).unwrap_or("");
976                            Some(format!("%BatchFileItem{{path: \"{}\"}}", path))
977                        }
978                        _ => None,
979                    }
980                } else {
981                    None
982                }
983            })
984            .collect();
985        format!("[{}]", item_strs.join(", "))
986    } else {
987        "[]".to_string()
988    }
989}
990
991#[allow(clippy::too_many_arguments)]
992fn build_args_and_setup(
993    input: &serde_json::Value,
994    args: &[crate::config::ArgMapping],
995    module_path: &str,
996    options_type: Option<&str>,
997    options_default_fn: Option<&str>,
998    enum_fields: &HashMap<String, String>,
999    fixture_id: &str,
1000    _handle_struct_type: Option<&str>,
1001    _handle_atom_list_fields: &std::collections::HashSet<String>,
1002) -> (Vec<String>, String) {
1003    if args.is_empty() {
1004        // No args config: pass the whole input only when it's non-empty.
1005        // Functions with no parameters (e.g. language_count) have empty input
1006        // and must be called with no arguments — not with `%{}`.
1007        let is_empty_input = match input {
1008            serde_json::Value::Null => true,
1009            serde_json::Value::Object(m) => m.is_empty(),
1010            _ => false,
1011        };
1012        if is_empty_input {
1013            return (Vec::new(), String::new());
1014        }
1015        return (Vec::new(), json_to_elixir(input));
1016    }
1017
1018    let mut setup_lines: Vec<String> = Vec::new();
1019    let mut parts: Vec<String> = Vec::new();
1020
1021    for arg in args {
1022        if arg.arg_type == "mock_url" {
1023            setup_lines.push(format!(
1024                "{} = (System.get_env(\"MOCK_SERVER_URL\") || \"\") <> \"/fixtures/{fixture_id}\"",
1025                arg.name,
1026            ));
1027            parts.push(arg.name.clone());
1028            continue;
1029        }
1030
1031        if arg.arg_type == "handle" {
1032            // Generate a create_{name} call using {:ok, name} = ... pattern.
1033            // The NIF now accepts config as an optional JSON string (not a NifStruct/NifMap)
1034            // so that partial maps work: serde_json::from_str respects #[serde(default)].
1035            let constructor_name = format!("create_{}", arg.name.to_snake_case());
1036            let config_value = if arg.field == "input" {
1037                input
1038            } else {
1039                let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1040                input.get(field).unwrap_or(&serde_json::Value::Null)
1041            };
1042            let name = &arg.name;
1043            if config_value.is_null()
1044                || config_value.is_object() && config_value.as_object().is_some_and(|o| o.is_empty())
1045            {
1046                setup_lines.push(format!("{{:ok, {name}}} = {module_path}.{constructor_name}(nil)"));
1047            } else {
1048                // Serialize the config map to a JSON string with Jason so that Rust can
1049                // deserialize it with serde_json and apply field defaults for missing keys.
1050                let json_str = serde_json::to_string(config_value).unwrap_or_else(|_| "{}".to_string());
1051                let escaped = escape_elixir(&json_str);
1052                setup_lines.push(format!("{name}_config = \"{escaped}\""));
1053                setup_lines.push(format!(
1054                    "{{:ok, {name}}} = {module_path}.{constructor_name}({name}_config)",
1055                ));
1056            }
1057            parts.push(arg.name.clone());
1058            continue;
1059        }
1060
1061        let val = if arg.field == "input" {
1062            Some(input)
1063        } else {
1064            let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1065            input.get(field)
1066        };
1067        match val {
1068            None | Some(serde_json::Value::Null) if arg.optional => {
1069                // Elixir functions have fixed positional arity — pass nil for optional args
1070                // rather than skipping them, so the call site has the correct arity.
1071                parts.push("nil".to_string());
1072                continue;
1073            }
1074            None | Some(serde_json::Value::Null) => {
1075                // Required arg with no fixture value: pass a language-appropriate default.
1076                let default_val = match arg.arg_type.as_str() {
1077                    "string" => "\"\"".to_string(),
1078                    "int" | "integer" => "0".to_string(),
1079                    "float" | "number" => "0.0".to_string(),
1080                    "bool" | "boolean" => "false".to_string(),
1081                    _ => "nil".to_string(),
1082                };
1083                parts.push(default_val);
1084            }
1085            Some(v) => {
1086                // For file_path args, prepend the path to the test_documents directory
1087                // relative to the e2e/elixir/ directory where `mix test` runs.
1088                if arg.arg_type == "file_path" {
1089                    if let Some(path_str) = v.as_str() {
1090                        let full_path = format!("../../test_documents/{path_str}");
1091                        parts.push(format!("\"{}\"", escape_elixir(&full_path)));
1092                        continue;
1093                    }
1094                }
1095                // For bytes args, use File.read! for file paths and Base.decode64! for base64.
1096                // Inline text (starts with '<', '{', '[' or contains spaces) is used as-is (UTF-8 binary).
1097                if arg.arg_type == "bytes" {
1098                    if let Some(raw) = v.as_str() {
1099                        let var_name = &arg.name;
1100                        if raw.starts_with('<') || raw.starts_with('{') || raw.starts_with('[') || raw.contains(' ') {
1101                            // Inline text — use as a binary string.
1102                            parts.push(format!("\"{}\"", escape_elixir(raw)));
1103                        } else {
1104                            let first = raw.chars().next().unwrap_or('\0');
1105                            let is_file_path = (first.is_ascii_alphanumeric() || first == '_')
1106                                && raw
1107                                    .find('/')
1108                                    .is_some_and(|slash_pos| slash_pos > 0 && raw[slash_pos + 1..].contains('.'));
1109                            if is_file_path {
1110                                // Looks like "dir/file.ext" — read from test_documents.
1111                                let full_path = format!("../../test_documents/{raw}");
1112                                let escaped = escape_elixir(&full_path);
1113                                setup_lines.push(format!("{var_name} = File.read!(\"{escaped}\")"));
1114                                parts.push(var_name.to_string());
1115                            } else {
1116                                // Treat as base64-encoded binary.
1117                                setup_lines.push(format!(
1118                                    "{var_name} = Base.decode64!(\"{}\", padding: false)",
1119                                    escape_elixir(raw)
1120                                ));
1121                                parts.push(var_name.to_string());
1122                            }
1123                        }
1124                        continue;
1125                    }
1126                }
1127                // For json_object args with options_type+options_via, build a proper struct.
1128                if arg.arg_type == "json_object" && !v.is_null() {
1129                    if let (Some(_opts_type), Some(options_fn), Some(obj)) =
1130                        (options_type, options_default_fn, v.as_object())
1131                    {
1132                        // Add setup line to initialize options from default function.
1133                        let options_var = "options";
1134                        setup_lines.push(format!("{options_var} = {module_path}.{options_fn}()"));
1135
1136                        // For each field in the options object, add a struct update line.
1137                        for (k, vv) in obj.iter() {
1138                            let snake_key = k.to_snake_case();
1139                            let elixir_val = if let Some(_enum_type) = enum_fields.get(k) {
1140                                if let Some(s) = vv.as_str() {
1141                                    let snake_val = s.to_snake_case();
1142                                    // Use atom for enum values, not string
1143                                    format!(":{snake_val}")
1144                                } else {
1145                                    json_to_elixir(vv)
1146                                }
1147                            } else {
1148                                json_to_elixir(vv)
1149                            };
1150                            setup_lines.push(format!(
1151                                "{options_var} = %{{{options_var} | {snake_key}: {elixir_val}}}"
1152                            ));
1153                        }
1154
1155                        // Push the variable name as the argument.
1156                        parts.push(options_var.to_string());
1157                        continue;
1158                    }
1159                    // When element_type is set to a batch item type, wrap items with constructors.
1160                    if let Some(elem_type) = &arg.element_type {
1161                        if (elem_type == "BatchBytesItem" || elem_type == "BatchFileItem") && v.is_array() {
1162                            parts.push(emit_elixir_batch_item_array(v, elem_type));
1163                            continue;
1164                        }
1165                        // When element_type is set to a simple type (e.g. Vec<String>).
1166                        // The NIF accepts an Elixir list directly — emit one.
1167                        if v.is_array() {
1168                            parts.push(json_to_elixir(v));
1169                            continue;
1170                        }
1171                    }
1172                    // When there's no options_type+options_via, the Elixir NIF expects a JSON
1173                    // string (Option<String> decoded by serde_json) rather than an Elixir map.
1174                    // Serialize the JSON value to a string literal here.
1175                    if !v.is_null() {
1176                        let json_str = serde_json::to_string(v).unwrap_or_else(|_| "{}".to_string());
1177                        let escaped = escape_elixir(&json_str);
1178                        parts.push(format!("\"{escaped}\""));
1179                        continue;
1180                    }
1181                }
1182                parts.push(json_to_elixir(v));
1183            }
1184        }
1185    }
1186
1187    (setup_lines, parts.join(", "))
1188}
1189
1190/// Returns true if the field expression is a numeric/integer expression
1191/// (e.g., a `length(...)` call) rather than a string.
1192fn is_numeric_expr(field_expr: &str) -> bool {
1193    field_expr.starts_with("length(")
1194}
1195
1196fn render_assertion(
1197    out: &mut String,
1198    assertion: &Assertion,
1199    result_var: &str,
1200    field_resolver: &FieldResolver,
1201    module_path: &str,
1202    fields_enum: &std::collections::HashSet<String>,
1203    result_is_simple: bool,
1204) {
1205    // Handle synthetic / derived fields before the is_valid_for_result check
1206    // so they are never treated as struct field accesses on the result.
1207    if let Some(f) = &assertion.field {
1208        match f.as_str() {
1209            "chunks_have_content" => {
1210                let pred =
1211                    format!("Enum.all?({result_var}.chunks || [], fn c -> c.content != nil and c.content != \"\" end)");
1212                match assertion.assertion_type.as_str() {
1213                    "is_true" => {
1214                        let _ = writeln!(out, "      assert {pred}");
1215                    }
1216                    "is_false" => {
1217                        let _ = writeln!(out, "      refute {pred}");
1218                    }
1219                    _ => {
1220                        let _ = writeln!(
1221                            out,
1222                            "      # skipped: unsupported assertion type on synthetic field '{f}'"
1223                        );
1224                    }
1225                }
1226                return;
1227            }
1228            "chunks_have_embeddings" => {
1229                let pred = format!(
1230                    "Enum.all?({result_var}.chunks || [], fn c -> c.embedding != nil and c.embedding != [] end)"
1231                );
1232                match assertion.assertion_type.as_str() {
1233                    "is_true" => {
1234                        let _ = writeln!(out, "      assert {pred}");
1235                    }
1236                    "is_false" => {
1237                        let _ = writeln!(out, "      refute {pred}");
1238                    }
1239                    _ => {
1240                        let _ = writeln!(
1241                            out,
1242                            "      # skipped: unsupported assertion type on synthetic field '{f}'"
1243                        );
1244                    }
1245                }
1246                return;
1247            }
1248            // ---- EmbedResponse virtual fields ----
1249            // embed_texts returns [[float]] in Elixir — no wrapper struct.
1250            // result_var is the embedding matrix; use it directly.
1251            "embeddings" => {
1252                match assertion.assertion_type.as_str() {
1253                    "count_equals" => {
1254                        if let Some(val) = &assertion.value {
1255                            let ex_val = json_to_elixir(val);
1256                            let _ = writeln!(out, "      assert length({result_var}) == {ex_val}");
1257                        }
1258                    }
1259                    "count_min" => {
1260                        if let Some(val) = &assertion.value {
1261                            let ex_val = json_to_elixir(val);
1262                            let _ = writeln!(out, "      assert length({result_var}) >= {ex_val}");
1263                        }
1264                    }
1265                    "not_empty" => {
1266                        let _ = writeln!(out, "      assert {result_var} != []");
1267                    }
1268                    "is_empty" => {
1269                        let _ = writeln!(out, "      assert {result_var} == []");
1270                    }
1271                    _ => {
1272                        let _ = writeln!(
1273                            out,
1274                            "      # skipped: unsupported assertion type on synthetic field 'embeddings'"
1275                        );
1276                    }
1277                }
1278                return;
1279            }
1280            "embedding_dimensions" => {
1281                let expr = format!("(if {result_var} == [], do: 0, else: length(hd({result_var})))");
1282                match assertion.assertion_type.as_str() {
1283                    "equals" => {
1284                        if let Some(val) = &assertion.value {
1285                            let ex_val = json_to_elixir(val);
1286                            let _ = writeln!(out, "      assert {expr} == {ex_val}");
1287                        }
1288                    }
1289                    "greater_than" => {
1290                        if let Some(val) = &assertion.value {
1291                            let ex_val = json_to_elixir(val);
1292                            let _ = writeln!(out, "      assert {expr} > {ex_val}");
1293                        }
1294                    }
1295                    _ => {
1296                        let _ = writeln!(
1297                            out,
1298                            "      # skipped: unsupported assertion type on synthetic field 'embedding_dimensions'"
1299                        );
1300                    }
1301                }
1302                return;
1303            }
1304            "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
1305                let pred = match f.as_str() {
1306                    "embeddings_valid" => {
1307                        format!("Enum.all?({result_var}, fn e -> e != [] end)")
1308                    }
1309                    "embeddings_finite" => {
1310                        format!("Enum.all?({result_var}, fn e -> Enum.all?(e, fn v -> is_float(v) and v == v end) end)")
1311                    }
1312                    "embeddings_non_zero" => {
1313                        format!("Enum.all?({result_var}, fn e -> Enum.any?(e, fn v -> v != 0.0 end) end)")
1314                    }
1315                    "embeddings_normalized" => {
1316                        format!(
1317                            "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)"
1318                        )
1319                    }
1320                    _ => unreachable!(),
1321                };
1322                match assertion.assertion_type.as_str() {
1323                    "is_true" => {
1324                        let _ = writeln!(out, "      assert {pred}");
1325                    }
1326                    "is_false" => {
1327                        let _ = writeln!(out, "      refute {pred}");
1328                    }
1329                    _ => {
1330                        let _ = writeln!(
1331                            out,
1332                            "      # skipped: unsupported assertion type on synthetic field '{f}'"
1333                        );
1334                    }
1335                }
1336                return;
1337            }
1338            // ---- keywords / keywords_count ----
1339            // Elixir ExtractionResult does not expose extracted_keywords; skip.
1340            "keywords" | "keywords_count" => {
1341                let _ = writeln!(
1342                    out,
1343                    "      # skipped: field '{f}' not available on Elixir ExtractionResult"
1344                );
1345                return;
1346            }
1347            _ => {}
1348        }
1349    }
1350
1351    // Skip assertions on fields that don't exist on the result type.
1352    // When `result_is_simple`, the bound result is the value itself (e.g. a binary)
1353    // so `is_valid_for_result` is meaningless — fall through and emit the assertion
1354    // against the bare result_var below.
1355    if !result_is_simple {
1356        if let Some(f) = &assertion.field {
1357            if !f.is_empty() && !field_resolver.is_valid_for_result(f) {
1358                let _ = writeln!(out, "      # skipped: field '{f}' not available on result type");
1359                return;
1360            }
1361        }
1362    }
1363
1364    // result_is_simple: when the call returns the value itself (e.g. a binary for
1365    // `speech` / `file_content`), bypass the field accessor and assert against the
1366    // bound `result` variable directly.
1367    let field_expr = if result_is_simple {
1368        result_var.to_string()
1369    } else {
1370        match &assertion.field {
1371            Some(f) if !f.is_empty() => field_resolver.accessor(f, "elixir", result_var),
1372            _ => result_var.to_string(),
1373        }
1374    };
1375
1376    // Only wrap in String.trim/0 when the expression is actually a string.
1377    // Numeric expressions (e.g., length(...)) must not be wrapped.
1378    let is_numeric = is_numeric_expr(&field_expr);
1379    // Detect whether the field resolves to an enum type. Rustler binds Rust
1380    // enums as atoms (e.g. `:stop`), so calling `String.trim/1` on them raises
1381    // FunctionClauseError. Coerce via `to_string/1` before string ops.
1382    let field_is_enum = assertion.field.as_deref().filter(|f| !f.is_empty()).is_some_and(|f| {
1383        let resolved = field_resolver.resolve(f);
1384        fields_enum.contains(f) || fields_enum.contains(resolved)
1385    });
1386    let coerced_field_expr = if field_is_enum {
1387        format!("to_string({field_expr})")
1388    } else {
1389        field_expr.clone()
1390    };
1391    let trimmed_field_expr = if is_numeric {
1392        field_expr.clone()
1393    } else {
1394        format!("String.trim({coerced_field_expr})")
1395    };
1396
1397    // Detect whether the assertion field resolves to an array type so that
1398    // contains assertions can iterate items instead of calling to_string on the list.
1399    let field_is_array = assertion
1400        .field
1401        .as_deref()
1402        .filter(|f| !f.is_empty())
1403        .is_some_and(|f| field_resolver.is_array(field_resolver.resolve(f)));
1404
1405    match assertion.assertion_type.as_str() {
1406        "equals" => {
1407            if let Some(expected) = &assertion.value {
1408                let elixir_val = json_to_elixir(expected);
1409                // Apply String.trim only for string comparisons, not numeric ones.
1410                let is_string_expected = expected.is_string();
1411                if is_string_expected && !is_numeric {
1412                    let _ = writeln!(out, "      assert {trimmed_field_expr} == {elixir_val}");
1413                } else if field_is_enum {
1414                    let _ = writeln!(out, "      assert {coerced_field_expr} == {elixir_val}");
1415                } else {
1416                    let _ = writeln!(out, "      assert {field_expr} == {elixir_val}");
1417                }
1418            }
1419        }
1420        "contains" => {
1421            if let Some(expected) = &assertion.value {
1422                let elixir_val = json_to_elixir(expected);
1423                if field_is_array && expected.is_string() {
1424                    // List of structs: check if any item's text representation contains the value.
1425                    let _ = writeln!(
1426                        out,
1427                        "      assert Enum.any?({field_expr}, fn item -> Enum.any?(alef_e2e_item_texts(item), &String.contains?(&1, {elixir_val})) end)"
1428                    );
1429                } else {
1430                    // Use to_string() to handle atoms (enums) as well as strings
1431                    let _ = writeln!(
1432                        out,
1433                        "      assert String.contains?(to_string({field_expr}), {elixir_val})"
1434                    );
1435                }
1436            }
1437        }
1438        "contains_all" => {
1439            if let Some(values) = &assertion.values {
1440                for val in values {
1441                    let elixir_val = json_to_elixir(val);
1442                    if field_is_array && val.is_string() {
1443                        let _ = writeln!(
1444                            out,
1445                            "      assert Enum.any?({field_expr}, fn item -> Enum.any?(alef_e2e_item_texts(item), &String.contains?(&1, {elixir_val})) end)"
1446                        );
1447                    } else {
1448                        let _ = writeln!(
1449                            out,
1450                            "      assert String.contains?(to_string({field_expr}), {elixir_val})"
1451                        );
1452                    }
1453                }
1454            }
1455        }
1456        "not_contains" => {
1457            if let Some(expected) = &assertion.value {
1458                let elixir_val = json_to_elixir(expected);
1459                if field_is_array && expected.is_string() {
1460                    let _ = writeln!(
1461                        out,
1462                        "      refute Enum.any?({field_expr}, fn item -> Enum.any?(alef_e2e_item_texts(item), &String.contains?(&1, {elixir_val})) end)"
1463                    );
1464                } else {
1465                    let _ = writeln!(
1466                        out,
1467                        "      refute String.contains?(to_string({field_expr}), {elixir_val})"
1468                    );
1469                }
1470            }
1471        }
1472        "not_empty" => {
1473            let _ = writeln!(out, "      assert {field_expr} != \"\"");
1474        }
1475        "is_empty" => {
1476            if is_numeric {
1477                // length(...) == 0
1478                let _ = writeln!(out, "      assert {field_expr} == 0");
1479            } else {
1480                // Handle nil (None) as empty
1481                let _ = writeln!(out, "      assert is_nil({field_expr}) or {trimmed_field_expr} == \"\"");
1482            }
1483        }
1484        "contains_any" => {
1485            if let Some(values) = &assertion.values {
1486                let items: Vec<String> = values.iter().map(json_to_elixir).collect();
1487                let list_str = items.join(", ");
1488                let _ = writeln!(
1489                    out,
1490                    "      assert Enum.any?([{list_str}], fn v -> String.contains?(to_string({field_expr}), v) end)"
1491                );
1492            }
1493        }
1494        "greater_than" => {
1495            if let Some(val) = &assertion.value {
1496                let elixir_val = json_to_elixir(val);
1497                let _ = writeln!(out, "      assert {field_expr} > {elixir_val}");
1498            }
1499        }
1500        "less_than" => {
1501            if let Some(val) = &assertion.value {
1502                let elixir_val = json_to_elixir(val);
1503                let _ = writeln!(out, "      assert {field_expr} < {elixir_val}");
1504            }
1505        }
1506        "greater_than_or_equal" => {
1507            if let Some(val) = &assertion.value {
1508                let elixir_val = json_to_elixir(val);
1509                let _ = writeln!(out, "      assert {field_expr} >= {elixir_val}");
1510            }
1511        }
1512        "less_than_or_equal" => {
1513            if let Some(val) = &assertion.value {
1514                let elixir_val = json_to_elixir(val);
1515                let _ = writeln!(out, "      assert {field_expr} <= {elixir_val}");
1516            }
1517        }
1518        "starts_with" => {
1519            if let Some(expected) = &assertion.value {
1520                let elixir_val = json_to_elixir(expected);
1521                let _ = writeln!(out, "      assert String.starts_with?({field_expr}, {elixir_val})");
1522            }
1523        }
1524        "ends_with" => {
1525            if let Some(expected) = &assertion.value {
1526                let elixir_val = json_to_elixir(expected);
1527                let _ = writeln!(out, "      assert String.ends_with?({field_expr}, {elixir_val})");
1528            }
1529        }
1530        "min_length" => {
1531            if let Some(val) = &assertion.value {
1532                if let Some(n) = val.as_u64() {
1533                    let _ = writeln!(out, "      assert String.length({field_expr}) >= {n}");
1534                }
1535            }
1536        }
1537        "max_length" => {
1538            if let Some(val) = &assertion.value {
1539                if let Some(n) = val.as_u64() {
1540                    let _ = writeln!(out, "      assert String.length({field_expr}) <= {n}");
1541                }
1542            }
1543        }
1544        "count_min" => {
1545            if let Some(val) = &assertion.value {
1546                if let Some(n) = val.as_u64() {
1547                    let _ = writeln!(out, "      assert length({field_expr}) >= {n}");
1548                }
1549            }
1550        }
1551        "count_equals" => {
1552            if let Some(val) = &assertion.value {
1553                if let Some(n) = val.as_u64() {
1554                    let _ = writeln!(out, "      assert length({field_expr}) == {n}");
1555                }
1556            }
1557        }
1558        "is_true" => {
1559            let _ = writeln!(out, "      assert {field_expr} == true");
1560        }
1561        "is_false" => {
1562            let _ = writeln!(out, "      assert {field_expr} == false");
1563        }
1564        "method_result" => {
1565            if let Some(method_name) = &assertion.method {
1566                let call_expr = build_elixir_method_call(result_var, method_name, assertion.args.as_ref(), module_path);
1567                let check = assertion.check.as_deref().unwrap_or("is_true");
1568                match check {
1569                    "equals" => {
1570                        if let Some(val) = &assertion.value {
1571                            let elixir_val = json_to_elixir(val);
1572                            let _ = writeln!(out, "      assert {call_expr} == {elixir_val}");
1573                        }
1574                    }
1575                    "is_true" => {
1576                        let _ = writeln!(out, "      assert {call_expr} == true");
1577                    }
1578                    "is_false" => {
1579                        let _ = writeln!(out, "      assert {call_expr} == false");
1580                    }
1581                    "greater_than_or_equal" => {
1582                        if let Some(val) = &assertion.value {
1583                            let n = val.as_u64().unwrap_or(0);
1584                            let _ = writeln!(out, "      assert {call_expr} >= {n}");
1585                        }
1586                    }
1587                    "count_min" => {
1588                        if let Some(val) = &assertion.value {
1589                            let n = val.as_u64().unwrap_or(0);
1590                            let _ = writeln!(out, "      assert length({call_expr}) >= {n}");
1591                        }
1592                    }
1593                    "contains" => {
1594                        if let Some(val) = &assertion.value {
1595                            let elixir_val = json_to_elixir(val);
1596                            let _ = writeln!(out, "      assert String.contains?({call_expr}, {elixir_val})");
1597                        }
1598                    }
1599                    "is_error" => {
1600                        let _ = writeln!(out, "      assert_raise RuntimeError, fn -> {call_expr} end");
1601                    }
1602                    other_check => {
1603                        panic!("Elixir e2e generator: unsupported method_result check type: {other_check}");
1604                    }
1605                }
1606            } else {
1607                panic!("Elixir e2e generator: method_result assertion missing 'method' field");
1608            }
1609        }
1610        "matches_regex" => {
1611            if let Some(expected) = &assertion.value {
1612                let elixir_val = json_to_elixir(expected);
1613                let _ = writeln!(out, "      assert Regex.match?(~r/{elixir_val}/, {field_expr})");
1614            }
1615        }
1616        "not_error" => {
1617            // Already handled — the call would fail if it returned {:error, _}.
1618        }
1619        "error" => {
1620            // Handled at the test level.
1621        }
1622        other => {
1623            panic!("Elixir e2e generator: unsupported assertion type: {other}");
1624        }
1625    }
1626}
1627
1628/// Build an Elixir call expression for a `method_result` assertion on a tree-sitter result.
1629/// Maps method names to the appropriate `module_path` function calls.
1630fn build_elixir_method_call(
1631    result_var: &str,
1632    method_name: &str,
1633    args: Option<&serde_json::Value>,
1634    module_path: &str,
1635) -> String {
1636    match method_name {
1637        "root_child_count" => format!("{module_path}.root_child_count({result_var})"),
1638        "has_error_nodes" => format!("{module_path}.tree_has_error_nodes({result_var})"),
1639        "error_count" | "tree_error_count" => format!("{module_path}.tree_error_count({result_var})"),
1640        "tree_to_sexp" => format!("{module_path}.tree_to_sexp({result_var})"),
1641        "contains_node_type" => {
1642            let node_type = args
1643                .and_then(|a| a.get("node_type"))
1644                .and_then(|v| v.as_str())
1645                .unwrap_or("");
1646            format!("{module_path}.tree_contains_node_type({result_var}, \"{node_type}\")")
1647        }
1648        "find_nodes_by_type" => {
1649            let node_type = args
1650                .and_then(|a| a.get("node_type"))
1651                .and_then(|v| v.as_str())
1652                .unwrap_or("");
1653            format!("{module_path}.find_nodes_by_type({result_var}, \"{node_type}\")")
1654        }
1655        "run_query" => {
1656            let query_source = args
1657                .and_then(|a| a.get("query_source"))
1658                .and_then(|v| v.as_str())
1659                .unwrap_or("");
1660            let language = args
1661                .and_then(|a| a.get("language"))
1662                .and_then(|v| v.as_str())
1663                .unwrap_or("");
1664            format!("{module_path}.run_query({result_var}, \"{language}\", \"{query_source}\", source)")
1665        }
1666        _ => format!("{module_path}.{method_name}({result_var})"),
1667    }
1668}
1669
1670/// Convert a category name to an Elixir module-safe PascalCase name.
1671fn elixir_module_name(category: &str) -> String {
1672    use heck::ToUpperCamelCase;
1673    category.to_upper_camel_case()
1674}
1675
1676/// Convert a `serde_json::Value` to an Elixir literal string.
1677fn json_to_elixir(value: &serde_json::Value) -> String {
1678    match value {
1679        serde_json::Value::String(s) => format!("\"{}\"", escape_elixir(s)),
1680        serde_json::Value::Bool(true) => "true".to_string(),
1681        serde_json::Value::Bool(false) => "false".to_string(),
1682        serde_json::Value::Number(n) => {
1683            // Elixir requires floats to have a decimal point and does not accept
1684            // `e+N` exponent notation. Strip the `+` and ensure there is a decimal
1685            // point before any `e` exponent marker (e.g. `1e-10` → `1.0e-10`).
1686            let s = n.to_string().replace("e+", "e");
1687            if s.contains('e') && !s.contains('.') {
1688                // Insert `.0` before the `e` so Elixir treats this as a float.
1689                s.replacen('e', ".0e", 1)
1690            } else {
1691                s
1692            }
1693        }
1694        serde_json::Value::Null => "nil".to_string(),
1695        serde_json::Value::Array(arr) => {
1696            let items: Vec<String> = arr.iter().map(json_to_elixir).collect();
1697            format!("[{}]", items.join(", "))
1698        }
1699        serde_json::Value::Object(map) => {
1700            let entries: Vec<String> = map
1701                .iter()
1702                .map(|(k, v)| format!("\"{}\" => {}", escape_elixir(k), json_to_elixir(v)))
1703                .collect();
1704            format!("%{{{}}}", entries.join(", "))
1705        }
1706    }
1707}
1708
1709/// Build an Elixir visitor map and add setup line. Returns the visitor variable name.
1710fn build_elixir_visitor(setup_lines: &mut Vec<String>, visitor_spec: &crate::fixture::VisitorSpec) -> String {
1711    use std::fmt::Write as FmtWrite;
1712    let mut visitor_obj = String::new();
1713    let _ = writeln!(visitor_obj, "%{{");
1714    for (method_name, action) in &visitor_spec.callbacks {
1715        emit_elixir_visitor_method(&mut visitor_obj, method_name, action);
1716    }
1717    let _ = writeln!(visitor_obj, "    }}");
1718
1719    setup_lines.push(format!("visitor = {visitor_obj}"));
1720    "visitor".to_string()
1721}
1722
1723/// Emit an Elixir visitor method for a callback action.
1724fn emit_elixir_visitor_method(out: &mut String, method_name: &str, action: &CallbackAction) {
1725    use std::fmt::Write as FmtWrite;
1726
1727    // Elixir uses atom keys and handle_ prefix
1728    let handle_method = format!("handle_{}", &method_name[6..]); // strip "visit_" prefix
1729    // The Rust NIF bridge packages every visitor argument (`_ctx`, `_text`, …) into a
1730    // single map and invokes the user's anonymous function with that map. Generating
1731    // multi-arity functions like `fn(_ctx, _text) ->` therefore raised BadArityError
1732    // ("arity 2 called with 1 argument") at runtime. Generate arity-1 functions that
1733    // accept the args map (and ignore it) to match the bridge's calling convention.
1734
1735    // CustomTemplate needs to read from args; other actions can ignore it.
1736    let arg_binding = match action {
1737        CallbackAction::CustomTemplate { .. } => "args",
1738        _ => "_args",
1739    };
1740    let _ = writeln!(out, "      :{handle_method} => fn({arg_binding}) ->");
1741    match action {
1742        CallbackAction::Skip => {
1743            let _ = writeln!(out, "        :skip");
1744        }
1745        CallbackAction::Continue => {
1746            let _ = writeln!(out, "        :continue");
1747        }
1748        CallbackAction::PreserveHtml => {
1749            let _ = writeln!(out, "        :preserve_html");
1750        }
1751        CallbackAction::Custom { output } => {
1752            let escaped = escape_elixir(output);
1753            let _ = writeln!(out, "        {{:custom, \"{escaped}\"}}");
1754        }
1755        CallbackAction::CustomTemplate { template } => {
1756            // Build a <> concatenation expression so {key} placeholders are substituted
1757            // from the args map at runtime without embedding double-quoted strings inside
1758            // a double-quoted string literal.
1759            let expr = template_to_elixir_concat(template);
1760            let _ = writeln!(out, "        {{:custom, {expr}}}");
1761        }
1762    }
1763    let _ = writeln!(out, "      end,");
1764}
1765
1766/// Convert a template like `"_{text}_"` into an Elixir `<>` concat expression:
1767/// `"_" <> Map.get(args, "text", "") <> "_"`.
1768/// Static parts are escaped via `escape_elixir`; `{key}` placeholders become
1769/// `Map.get(args, "key", "")` lookups into the JSON-decoded args map.
1770fn template_to_elixir_concat(template: &str) -> String {
1771    let mut parts: Vec<String> = Vec::new();
1772    let mut static_buf = String::new();
1773    let mut chars = template.chars().peekable();
1774
1775    while let Some(ch) = chars.next() {
1776        if ch == '{' {
1777            let mut key = String::new();
1778            let mut closed = false;
1779            for kc in chars.by_ref() {
1780                if kc == '}' {
1781                    closed = true;
1782                    break;
1783                }
1784                key.push(kc);
1785            }
1786            if closed && !key.is_empty() {
1787                if !static_buf.is_empty() {
1788                    let escaped = escape_elixir(&static_buf);
1789                    parts.push(format!("\"{escaped}\""));
1790                    static_buf.clear();
1791                }
1792                let escaped_key = escape_elixir(&key);
1793                parts.push(format!("Map.get(args, \"{escaped_key}\", \"\")"));
1794            } else {
1795                static_buf.push('{');
1796                static_buf.push_str(&key);
1797                if !closed {
1798                    // unclosed brace — treat remaining as literal
1799                }
1800            }
1801        } else {
1802            static_buf.push(ch);
1803        }
1804    }
1805
1806    if !static_buf.is_empty() {
1807        let escaped = escape_elixir(&static_buf);
1808        parts.push(format!("\"{escaped}\""));
1809    }
1810
1811    if parts.is_empty() {
1812        return "\"\"".to_string();
1813    }
1814    parts.join(" <> ")
1815}
1816
1817fn fixture_has_elixir_callable(fixture: &Fixture, e2e_config: &E2eConfig) -> bool {
1818    // HTTP fixtures are handled separately — not our concern here.
1819    if fixture.is_http_test() {
1820        return false;
1821    }
1822    let call_config = e2e_config.resolve_call_for_fixture(fixture.call.as_deref(), &fixture.input);
1823    let elixir_override = call_config
1824        .overrides
1825        .get("elixir")
1826        .or_else(|| e2e_config.call.overrides.get("elixir"));
1827    // When a client_factory is configured the fixture is callable via the client pattern.
1828    if elixir_override.and_then(|o| o.client_factory.as_deref()).is_some() {
1829        return true;
1830    }
1831    // Elixir bindings expose functions via module-level callables.
1832    // Like Python and Node, Elixir can call the base function directly without requiring
1833    // a language-specific override. The function can come from either the override or
1834    // the default [e2e.call] configuration.
1835    let function_from_override = elixir_override.and_then(|o| o.function.as_deref());
1836
1837    // If there's an override function, use it. Otherwise, Elixir can use the base function.
1838    function_from_override.is_some() || !call_config.function.is_empty()
1839}