Skip to main content

alef_e2e/codegen/rust/
assertions.rs

1//! Assertion rendering for Rust e2e tests.
2
3use std::fmt::Write as FmtWrite;
4
5use crate::escape::escape_rust;
6use crate::field_access::FieldResolver;
7use crate::fixture::Assertion;
8
9use super::assertion_helpers::{
10    render_count_equals_assertion, render_count_min_assertion, render_equals_assertion, render_gte_assertion,
11    render_is_empty_assertion, render_method_result_assertion, render_not_empty_assertion,
12};
13use super::assertion_synthetic::{
14    numeric_literal, render_chunks_have_content, render_chunks_have_embeddings, render_embedding_dimensions,
15    render_embedding_quality, render_embeddings_assertion, render_keywords_assertion, render_keywords_count_assertion,
16    tree_field_access_expr, value_to_rust_string,
17};
18
19/// Render a single assertion into the test function body.
20#[allow(clippy::too_many_arguments)]
21pub fn render_assertion(
22    out: &mut String,
23    assertion: &Assertion,
24    result_var: &str,
25    module: &str,
26    dep_name: &str,
27    is_error_context: bool,
28    unwrapped_fields: &[(String, String)], // (fixture_field, local_var)
29    field_resolver: &FieldResolver,
30    result_is_tree: bool,
31    result_is_simple: bool,
32    result_is_vec: bool,
33    result_is_option: bool,
34    returns_result: bool,
35) {
36    // Vec<T> result: iterate per-element so each assertion checks every element.
37    // Field-path assertions become `for r in &{result} { <assert using r> }`.
38    // Length-style assertions on the Vec itself (no field path) operate on the
39    // Vec directly.
40    let has_field = assertion.field.as_ref().is_some_and(|f| !f.is_empty());
41    if result_is_vec && has_field && !is_error_context {
42        let _ = writeln!(out, "    for r in &{result_var} {{");
43        render_assertion(
44            out,
45            assertion,
46            "r",
47            module,
48            dep_name,
49            is_error_context,
50            unwrapped_fields,
51            field_resolver,
52            result_is_tree,
53            result_is_simple,
54            false, // already inside loop
55            result_is_option,
56            returns_result,
57        );
58        let _ = writeln!(out, "    }}");
59        return;
60    }
61    // Option<T> result: map `is_empty`/`not_empty` to `is_none()`/`is_some()`,
62    // and unwrap the inner value before any other assertion runs.
63    if result_is_option && !is_error_context {
64        let assertion_type = assertion.assertion_type.as_str();
65        if !has_field && (assertion_type == "is_empty" || assertion_type == "not_empty") {
66            let check = if assertion_type == "is_empty" {
67                "is_none"
68            } else {
69                "is_some"
70            };
71            let _ = writeln!(
72                out,
73                "    assert!({result_var}.{check}(), \"expected Option to be {check}\");"
74            );
75            return;
76        }
77        // For any other assertion shape, unwrap the Option and recurse with a
78        // bare reference variable so the rest of the renderer treats the inner
79        // value as the result.
80        let _ = writeln!(
81            out,
82            "    let r = {result_var}.as_ref().expect(\"Option<T> should be Some\");"
83        );
84        render_assertion(
85            out,
86            assertion,
87            "r",
88            module,
89            dep_name,
90            is_error_context,
91            unwrapped_fields,
92            field_resolver,
93            result_is_tree,
94            result_is_simple,
95            result_is_vec,
96            false, // already unwrapped
97            returns_result,
98        );
99        return;
100    }
101    let _ = dep_name;
102    // Handle synthetic fields like chunks_have_content (derived assertions).
103    // These are computed expressions, not real struct fields — intercept before
104    // the is_valid_for_result check so they are never treated as field accesses.
105    if let Some(f) = &assertion.field {
106        match f.as_str() {
107            "chunks_have_content" => {
108                render_chunks_have_content(out, result_var, assertion.assertion_type.as_str());
109                return;
110            }
111            "chunks_have_embeddings" => {
112                render_chunks_have_embeddings(out, result_var, assertion.assertion_type.as_str());
113                return;
114            }
115            "embeddings" => {
116                render_embeddings_assertion(out, result_var, assertion);
117                return;
118            }
119            "embedding_dimensions" => {
120                render_embedding_dimensions(out, result_var, assertion);
121                return;
122            }
123            "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
124                render_embedding_quality(out, result_var, f, assertion.assertion_type.as_str());
125                return;
126            }
127            "keywords" => {
128                render_keywords_assertion(out, result_var, assertion);
129                return;
130            }
131            "keywords_count" => {
132                render_keywords_count_assertion(out, result_var, assertion);
133                return;
134            }
135            _ => {}
136        }
137    }
138
139    // Streaming virtual fields: intercept before is_valid_for_result so they are
140    // never skipped.  These fields resolve against the `chunks` collected-list variable.
141    if let Some(f) = &assertion.field {
142        if !f.is_empty() && crate::codegen::streaming_assertions::is_streaming_virtual_field(f) {
143            if let Some(expr) =
144                crate::codegen::streaming_assertions::StreamingFieldResolver::accessor(f, "rust", "chunks")
145            {
146                match assertion.assertion_type.as_str() {
147                    "count_min" => {
148                        if let Some(val) = &assertion.value {
149                            if let Some(n) = val.as_u64() {
150                                let _ = writeln!(
151                                    out,
152                                    "    assert!({expr}.len() >= {n} as usize, \"expected >= {n} chunks\");"
153                                );
154                            }
155                        }
156                    }
157                    "count_equals" => {
158                        if let Some(val) = &assertion.value {
159                            if let Some(n) = val.as_u64() {
160                                let _ = writeln!(
161                                    out,
162                                    "    assert_eq!({expr}.len(), {n} as usize, \"expected exactly {n} chunks\");"
163                                );
164                            }
165                        }
166                    }
167                    "equals" => {
168                        if let Some(serde_json::Value::String(s)) = &assertion.value {
169                            let escaped = crate::escape::escape_rust(s);
170                            let _ = writeln!(out, "    assert_eq!({expr}, \"{escaped}\");");
171                        } else if let Some(val) = &assertion.value {
172                            let lit = super::assertion_synthetic::numeric_literal(val);
173                            let _ = writeln!(out, "    assert_eq!({expr}, {lit});");
174                        }
175                    }
176                    "not_empty" => {
177                        let _ = writeln!(out, "    assert!(!{expr}.is_empty(), \"expected non-empty\");");
178                    }
179                    "is_empty" => {
180                        let _ = writeln!(out, "    assert!({expr}.is_empty(), \"expected empty\");");
181                    }
182                    "is_true" => {
183                        let _ = writeln!(out, "    assert!({expr}, \"expected true\");");
184                    }
185                    "is_false" => {
186                        let _ = writeln!(out, "    assert!(!{expr}, \"expected false\");");
187                    }
188                    "greater_than" => {
189                        if let Some(val) = &assertion.value {
190                            let lit = super::assertion_synthetic::numeric_literal(val);
191                            let _ = writeln!(out, "    assert!({expr} > {lit}, \"expected > {lit}\");");
192                        }
193                    }
194                    "greater_than_or_equal" => {
195                        if let Some(val) = &assertion.value {
196                            let lit = super::assertion_synthetic::numeric_literal(val);
197                            let _ = writeln!(out, "    assert!({expr} >= {lit}, \"expected >= {lit}\");");
198                        }
199                    }
200                    "contains" => {
201                        if let Some(serde_json::Value::String(s)) = &assertion.value {
202                            let escaped = crate::escape::escape_rust(s);
203                            let _ = writeln!(
204                                out,
205                                "    assert!({expr}.contains(\"{escaped}\"), \"expected to contain: {escaped}\");"
206                            );
207                        }
208                    }
209                    _ => {
210                        let _ = writeln!(
211                            out,
212                            "    // streaming field '{f}': assertion type '{}' not rendered",
213                            assertion.assertion_type
214                        );
215                    }
216                }
217            }
218            return;
219        }
220    }
221
222    // Skip assertions on fields that don't exist on the result type.
223    // Exception: fields prefixed with "error." target the error value in error-context
224    // assertions — they are resolved against the error type via accessor_for_error,
225    // not against the success result type, so they must not be skipped here.
226    if let Some(f) = &assertion.field {
227        if !f.is_empty() && !f.starts_with("error.") && !field_resolver.is_valid_for_result(f) {
228            let _ = writeln!(out, "    // skipped: field '{f}' not available on result type");
229            return;
230        }
231    }
232
233    // Check if this field was unwrapped (i.e., it is optional and was bound to a local).
234    let is_unwrapped = assertion
235        .field
236        .as_ref()
237        .is_some_and(|f| unwrapped_fields.iter().any(|(ff, _)| ff == f));
238
239    // When in error context with returns_result=true and accessing a field (not an error check),
240    // we need to unwrap the Result first. The test generator creates a binding like
241    // `let result_ok = result.as_ref().ok();` which we can dereference here.
242    // Exception: fields prefixed with "error." access the Err value, not the Ok value.
243    let has_field = assertion.field.as_ref().is_some_and(|f| !f.is_empty());
244    let is_field_assertion = !matches!(assertion.assertion_type.as_str(), "error" | "not_error");
245    let is_error_field = assertion.field.as_ref().is_some_and(|f| f.starts_with("error."));
246    let effective_result_var =
247        if has_field && is_error_context && returns_result && is_field_assertion && !is_error_field {
248            // Dereference the Option<&T> bound as {result_var}_ok
249            format!("{result_var}_ok.as_ref().unwrap()")
250        } else {
251            result_var.to_string()
252        };
253
254    // Determine field access expression:
255    // 1. If the field was unwrapped to a local var, use that local var name.
256    // 2. When result_is_simple, the function returns a plain type (String etc.) — use result_var.
257    // 3. When the field path is exactly the result var name (sentinel: `field: "result"`),
258    //    refer to the result variable directly to avoid emitting `result.result`.
259    // 4. When the result is a Tree, map pseudo-field names to correct Rust expressions.
260    // 5. When the field starts with "error.", resolve against the error type.
261    // 6. Otherwise, use the field resolver to generate the accessor.
262    let field_access = match &assertion.field {
263        Some(f) if !f.is_empty() => {
264            if let Some((_, local_var)) = unwrapped_fields.iter().find(|(ff, _)| ff == f) {
265                local_var.clone()
266            } else if result_is_simple {
267                // Plain return type (String, Vec<T>, etc.) has no struct fields.
268                // Use the result variable directly so assertions operate on the value itself.
269                effective_result_var.clone()
270            } else if f == result_var {
271                // Sentinel: fixture uses `field: "result"` (or matches the result variable name)
272                // to refer to the whole return value, not a struct field named "result".
273                effective_result_var.clone()
274            } else if result_is_tree {
275                // Tree is an opaque type — its "fields" are accessed via root_node() or
276                // free functions. Map known pseudo-field names to correct Rust expressions.
277                tree_field_access_expr(f, &effective_result_var, module)
278            } else if let Some(sub) = f.strip_prefix("error.") {
279                // Error-path field: access a field on the Err value rather than the Ok value.
280                // Inline-bind the error so the expression is self-contained.
281                let err_accessor = field_resolver.accessor_for_error(sub, "rust", "__err");
282                format!("{{ let __err = {result_var}.as_ref().err().unwrap(); {err_accessor} }}")
283            } else {
284                field_resolver.accessor(f, "rust", &effective_result_var)
285            }
286        }
287        _ => effective_result_var,
288    };
289
290    match assertion.assertion_type.as_str() {
291        "error" => {
292            let _ = writeln!(out, "    assert!({result_var}.is_err(), \"expected call to fail\");");
293            if let Some(serde_json::Value::String(msg)) = &assertion.value {
294                let escaped = escape_rust(msg);
295                // Match against the Debug format (variant-name-style) and the Display format
296                // (human-readable text). Fixtures often name the error variant ("BadRequest"),
297                // but Display impls typically lowercase with a colon ("bad request: ..."), so
298                // checking both lets either kind of fixture value match.
299                let _ = writeln!(
300                    out,
301                    "    {{ let __e = {result_var}.as_ref().err().unwrap(); assert!(format!(\"{{:?}}\", __e).contains(\"{escaped}\") || __e.to_string().contains(\"{escaped}\"), \"error message mismatch\"); }}"
302                );
303            }
304        }
305        "not_error" => {
306            // Handled at call site; nothing extra needed here.
307        }
308        "equals" => {
309            render_equals_assertion(out, assertion, &field_access, is_unwrapped, field_resolver);
310        }
311        "contains" => {
312            if let Some(val) = &assertion.value {
313                let expected = value_to_rust_string(val);
314                let line = format!(
315                    "    assert!(format!(\"{{:?}}\", {field_access}).contains({expected}), \"expected to contain: {{}}\", {expected});"
316                );
317                let _ = writeln!(out, "{line}");
318            }
319        }
320        "contains_all" => {
321            if let Some(values) = &assertion.values {
322                for val in values {
323                    let expected = value_to_rust_string(val);
324                    let line = format!(
325                        "    assert!(format!(\"{{:?}}\", {field_access}).contains({expected}), \"expected to contain: {{}}\", {expected});"
326                    );
327                    let _ = writeln!(out, "{line}");
328                }
329            }
330        }
331        "not_contains" => {
332            if let Some(val) = &assertion.value {
333                let expected = value_to_rust_string(val);
334                let line = format!(
335                    "    assert!(!format!(\"{{:?}}\", {field_access}).contains({expected}), \"expected NOT to contain: {{}}\", {expected});"
336                );
337                let _ = writeln!(out, "{line}");
338            }
339        }
340        "not_empty" => {
341            render_not_empty_assertion(
342                out,
343                assertion,
344                &field_access,
345                result_var,
346                result_is_option,
347                is_unwrapped,
348                field_resolver,
349            );
350        }
351        "is_empty" => {
352            render_is_empty_assertion(out, assertion, &field_access, is_unwrapped, field_resolver);
353        }
354        "contains_any" => {
355            if let Some(values) = &assertion.values {
356                let checks: Vec<String> = values
357                    .iter()
358                    .map(|v| {
359                        let expected = value_to_rust_string(v);
360                        format!("{field_access}.contains({expected})")
361                    })
362                    .collect();
363                let joined = checks.join(" || ");
364                let _ = writeln!(
365                    out,
366                    "    assert!({joined}, \"expected to contain at least one of the specified values\");"
367                );
368            }
369        }
370        "greater_than" => {
371            if let Some(val) = &assertion.value {
372                // Skip comparisons with negative values against unsigned types (.len() etc.)
373                if val.as_f64().is_some_and(|n| n < 0.0) {
374                    let _ = writeln!(
375                        out,
376                        "    // skipped: greater_than with negative value is always true for unsigned types"
377                    );
378                } else if val.as_u64() == Some(0) {
379                    if field_access.ends_with(".len()") {
380                        // Clippy prefers !is_empty() over len() > 0 for collections.
381                        let base = field_access.strip_suffix(".len()").unwrap();
382                        let _ = writeln!(out, "    assert!(!{base}.is_empty(), \"expected > 0\");");
383                    } else {
384                        // Scalar types (usize, u64, etc.) — use direct comparison.
385                        let _ = writeln!(out, "    assert!({field_access} > 0, \"expected > 0\");");
386                    }
387                } else {
388                    let lit = numeric_literal(val);
389                    let _ = writeln!(out, "    assert!({field_access} > {lit}, \"expected > {lit}\");");
390                }
391            }
392        }
393        "less_than" => {
394            if let Some(val) = &assertion.value {
395                let lit = numeric_literal(val);
396                let _ = writeln!(out, "    assert!({field_access} < {lit}, \"expected < {lit}\");");
397            }
398        }
399        "greater_than_or_equal" => {
400            render_gte_assertion(out, assertion, &field_access, is_unwrapped, field_resolver);
401        }
402        "less_than_or_equal" => {
403            if let Some(val) = &assertion.value {
404                let lit = numeric_literal(val);
405                let _ = writeln!(out, "    assert!({field_access} <= {lit}, \"expected <= {lit}\");");
406            }
407        }
408        "starts_with" => {
409            if let Some(val) = &assertion.value {
410                let expected = value_to_rust_string(val);
411                let _ = writeln!(
412                    out,
413                    "    assert!({field_access}.starts_with({expected}), \"expected to start with: {{}}\", {expected});"
414                );
415            }
416        }
417        "ends_with" => {
418            if let Some(val) = &assertion.value {
419                let expected = value_to_rust_string(val);
420                let _ = writeln!(
421                    out,
422                    "    assert!({field_access}.ends_with({expected}), \"expected to end with: {{}}\", {expected});"
423                );
424            }
425        }
426        "min_length" => {
427            if let Some(val) = &assertion.value {
428                if let Some(n) = val.as_u64() {
429                    let _ = writeln!(
430                        out,
431                        "    assert!({field_access}.len() >= {n}, \"expected length >= {n}, got {{}}\", {field_access}.len());"
432                    );
433                }
434            }
435        }
436        "max_length" => {
437            if let Some(val) = &assertion.value {
438                if let Some(n) = val.as_u64() {
439                    let _ = writeln!(
440                        out,
441                        "    assert!({field_access}.len() <= {n}, \"expected length <= {n}, got {{}}\", {field_access}.len());"
442                    );
443                }
444            }
445        }
446        "count_min" => {
447            render_count_min_assertion(out, assertion, &field_access, is_unwrapped, field_resolver);
448        }
449        "count_equals" => {
450            render_count_equals_assertion(out, assertion, &field_access, is_unwrapped, field_resolver);
451        }
452        "is_true" => {
453            let _ = writeln!(out, "    assert!({field_access}, \"expected true\");");
454        }
455        "is_false" => {
456            let _ = writeln!(out, "    assert!(!{field_access}, \"expected false\");");
457        }
458        "method_result" => {
459            render_method_result_assertion(out, assertion, &field_access, result_is_tree, module);
460        }
461        other => {
462            panic!("Rust e2e generator: unsupported assertion type: {other}");
463        }
464    }
465}
466
467#[cfg(test)]
468mod tests {
469    use std::collections::{HashMap, HashSet};
470
471    use super::*;
472    use crate::field_access::FieldResolver;
473    use crate::fixture::Assertion;
474
475    fn empty_resolver() -> FieldResolver {
476        FieldResolver::new(
477            &HashMap::new(),
478            &HashSet::new(),
479            &HashSet::new(),
480            &HashSet::new(),
481            &HashSet::new(),
482        )
483    }
484
485    fn make_assertion(assertion_type: &str, field: Option<&str>, value: Option<serde_json::Value>) -> Assertion {
486        Assertion {
487            assertion_type: assertion_type.to_string(),
488            field: field.map(|s| s.to_string()),
489            value,
490            ..Default::default()
491        }
492    }
493
494    #[test]
495    fn render_assertion_error_type_emits_is_err_check() {
496        let resolver = empty_resolver();
497        let assertion = make_assertion("error", None, None);
498        let mut out = String::new();
499        render_assertion(
500            &mut out,
501            &assertion,
502            "result",
503            "my_mod",
504            "dep",
505            true,
506            &[],
507            &resolver,
508            false,
509            false,
510            false,
511            false,
512            false,
513        );
514        assert!(out.contains("is_err()"), "got: {out}");
515    }
516
517    #[test]
518    fn render_assertion_vec_result_wraps_in_for_loop() {
519        let resolver = empty_resolver();
520        let assertion = make_assertion("not_empty", Some("content"), None);
521        let mut out = String::new();
522        render_assertion(
523            &mut out,
524            &assertion,
525            "result",
526            "my_mod",
527            "dep",
528            false,
529            &[],
530            &resolver,
531            false,
532            false,
533            true,
534            false,
535            false,
536        );
537        assert!(out.contains("for r in"), "got: {out}");
538    }
539
540    #[test]
541    fn render_assertion_not_empty_bare_result_uses_is_empty() {
542        let resolver = empty_resolver();
543        let assertion = make_assertion("not_empty", None, None);
544        let mut out = String::new();
545        render_assertion(
546            &mut out,
547            &assertion,
548            "result",
549            "my_mod",
550            "dep",
551            false,
552            &[],
553            &resolver,
554            false,
555            false,
556            false,
557            false,
558            false,
559        );
560        assert!(out.contains("is_empty()"), "got: {out}");
561    }
562}