wallfacer-core 0.4.0

Runtime fuzzing and invariant-testing harness for MCP servers — catch crashes, hangs, schema drift, and state leaks before they ship.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//! Property invariant evaluator.
//!
//! Phase D rewrites the runner to:
//!
//! * use [`Operand::resolve`] instead of the legacy `starts_with('$')`
//!   heuristic, while preserving full v1 backwards compatibility;
//! * support boolean combinators (`all_of`, `any_of`, `not`),
//!   element-wise iteration (`for_each`), and inline schema validation
//!   (`matches_schema`);
//! * forbid `unwrap` / `expect` at the module level so any future
//!   regression is caught by the workspace clippy gate.

#![deny(clippy::expect_used, clippy::unwrap_used, clippy::panic)]

use jsonschema::validator_for;
use rand::RngCore;
use regex::Regex;
use serde_json::{json, Map, Number, Value};
use thiserror::Error;
use tracing::warn;

use super::{
    dsl::{
        Assertion, FixtureExpect, Invariant, JsonType, Operand, TestFixture, ValueKind, ValueSpec,
    },
    jsonpath,
};

/// Errors raised while evaluating a single invariant case.
#[derive(Debug, Error)]
pub enum RunnerError {
    /// An assertion produced a human-readable failure.
    #[error("{0}")]
    Assertion(String),
    /// A JSONPath used by the assertion did not resolve cleanly.
    #[error(transparent)]
    JsonPath(#[from] jsonpath::JsonPathError),
    /// A regex inside a `matches_regex` assertion failed to compile.
    #[error("invalid regex `{pattern}`: {source}")]
    Regex {
        pattern: String,
        #[source]
        source: regex::Error,
    },
    /// A JSON Schema inside a `matches_schema` assertion failed to compile.
    #[error("invalid inline schema in `matches_schema`: {0}")]
    Schema(String),
}

pub type Result<T> = std::result::Result<T, RunnerError>;

pub fn input_for_case(invariant: &Invariant, case_index: u32, rng: &mut impl RngCore) -> Value {
    if let Some(fixed) = &invariant.fixed {
        return Value::Object(
            fixed
                .iter()
                .map(|(key, value)| (key.clone(), value.clone()))
                .collect(),
        );
    }

    let mut input = Map::new();
    if let Some(generate) = &invariant.generate {
        for (key, spec) in generate {
            let value = if case_index == 0 {
                boundary_value(spec)
            } else {
                generated_value(spec, rng)
            };
            input.insert(key.clone(), value);
        }
    }
    Value::Object(input)
}

/// Top-level entry point: evaluates every assertion of an invariant against
/// a freshly built `{input, response}` context.
pub fn evaluate(invariant: &Invariant, input: Value, response: Value) -> Result<()> {
    evaluate_step_assertions(&invariant.assertions, input, response)
}

/// Evaluates a free-form list of assertions against a fresh
/// `{input, response}` context. Used by the sequence runner where
/// the assertions live on a [`super::dsl::SequenceStep`] rather than
/// an [`Invariant`]. Behaviour is otherwise identical to
/// [`evaluate`].
pub fn evaluate_step_assertions(
    assertions: &[Assertion],
    input: Value,
    response: Value,
) -> Result<()> {
    let context = json!({
        "input": input,
        "response": response,
    });
    for assertion in assertions {
        evaluate_assertion(assertion, &context)?;
    }
    Ok(())
}

/// Outcome of [`evaluate_fixture`]: the comparison between what the
/// fixture asked for and what the runner actually produced.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FixtureOutcome {
    /// Observed matches `expected`; this fixture passed.
    Match,
    /// Observed differs from `expected`; this fixture failed.
    Mismatch {
        /// What the fixture said should happen.
        expected: FixtureExpect,
        /// What the runner actually observed.
        observed: FixtureExpect,
        /// Human-readable detail (assertion message when the runner
        /// returned `Err`, empty string on `Ok`).
        detail: String,
    },
    /// The runner returned a structural error (bad path, malformed
    /// regex / schema). The invariant itself is broken — this fixture
    /// can neither pass nor fail meaningfully.
    Structural {
        /// Pass-through of the structural error.
        error: String,
    },
}

/// Phase H — evaluates an invariant against a synthetic
/// `(input, response)` pair scripted by a [`TestFixture`] and reports
/// whether the observed outcome matches the fixture's `expect`.
pub fn evaluate_fixture(invariant: &Invariant, fixture: &TestFixture) -> FixtureOutcome {
    let input = fixture.input.clone().unwrap_or_else(|| {
        let map = invariant
            .fixed
            .clone()
            .unwrap_or_default()
            .into_iter()
            .collect::<serde_json::Map<_, _>>();
        Value::Object(map)
    });
    match evaluate(invariant, input, fixture.response.clone()) {
        Ok(()) => match fixture.expect {
            FixtureExpect::Pass => FixtureOutcome::Match,
            FixtureExpect::Fail => FixtureOutcome::Mismatch {
                expected: FixtureExpect::Fail,
                observed: FixtureExpect::Pass,
                detail: String::new(),
            },
        },
        Err(RunnerError::Assertion(message)) => match fixture.expect {
            FixtureExpect::Fail => FixtureOutcome::Match,
            FixtureExpect::Pass => FixtureOutcome::Mismatch {
                expected: FixtureExpect::Pass,
                observed: FixtureExpect::Fail,
                detail: message,
            },
        },
        Err(other) => FixtureOutcome::Structural {
            error: other.to_string(),
        },
    }
}

fn evaluate_assertion(assertion: &Assertion, context: &Value) -> Result<()> {
    match assertion {
        Assertion::Equals { lhs, rhs } => {
            let left = lhs.resolve(context)?;
            let right = rhs.resolve(context)?;
            if left == right {
                Ok(())
            } else {
                Err(RunnerError::Assertion(format!(
                    "expected {left} to equal {right}"
                )))
            }
        }
        Assertion::NotEquals { lhs, rhs } => {
            let left = lhs.resolve(context)?;
            let right = rhs.resolve(context)?;
            if left != right {
                Ok(())
            } else {
                Err(RunnerError::Assertion(format!(
                    "expected {left} to differ from {right}"
                )))
            }
        }
        Assertion::AtMost { path, value } => compare_number(path, value, context, |o| {
            matches!(o, std::cmp::Ordering::Less | std::cmp::Ordering::Equal)
        }),
        Assertion::AtLeast { path, value } => compare_number(path, value, context, |o| {
            matches!(o, std::cmp::Ordering::Greater | std::cmp::Ordering::Equal)
        }),
        Assertion::LengthEq { path, value } => compare_length(path, value, context, |a, b| a == b),
        Assertion::LengthAtMost { path, value } => {
            compare_length(path, value, context, |a, b| a <= b)
        }
        Assertion::LengthAtLeast { path, value } => {
            compare_length(path, value, context, |a, b| a >= b)
        }
        Assertion::IsType { path, expected } => {
            let value = jsonpath::resolve_one(context, path)?;
            let actual = json_type(&value);
            if actual == *expected {
                Ok(())
            } else {
                Err(RunnerError::Assertion(format!(
                    "expected {path} to be {expected:?}, got {actual:?}"
                )))
            }
        }
        Assertion::MatchesRegex { path, pattern } => {
            let value = jsonpath::resolve_one(context, path)?;
            let Some(text) = value.as_str() else {
                return Err(RunnerError::Assertion(format!(
                    "expected {path} to resolve to a string"
                )));
            };
            let regex = Regex::new(pattern).map_err(|source| RunnerError::Regex {
                pattern: pattern.clone(),
                source,
            })?;
            if regex.is_match(text) {
                Ok(())
            } else {
                Err(RunnerError::Assertion(format!(
                    "expected {path} to match {pattern}"
                )))
            }
        }
        Assertion::AllOf { assertions } => {
            for child in assertions {
                evaluate_assertion(child, context)?;
            }
            Ok(())
        }
        Assertion::AnyOf { assertions } => evaluate_any_of(assertions, context),
        Assertion::Not { assertion } => match evaluate_assertion(assertion, context) {
            Ok(()) => Err(RunnerError::Assertion(
                "expected child assertion to fail under `not`".to_string(),
            )),
            // A failing child *is* OK semantically when:
            //  - it produced an `Assertion` failure;
            //  - it referenced a path that did not resolve. Missing
            //    paths are equivalent to "child assertion does not
            //    hold" rather than a malformed invariant — promoting
            //    them to hard errors would make `not(matches_regex
            //    path=...)` surprising whenever the path is absent.
            // Other errors (regex compile, schema compile, malformed
            // YAML) are genuinely structural and propagate.
            Err(RunnerError::Assertion(_)) => Ok(()),
            Err(RunnerError::JsonPath(jsonpath::JsonPathError::Missing(_))) => Ok(()),
            Err(other) => Err(other),
        },
        Assertion::ForEach { path, assertions } => evaluate_for_each(path, assertions, context),
        Assertion::MatchesSchema { path, schema } => {
            let target = jsonpath::resolve_one(context, path)?;
            let validator =
                validator_for(schema).map_err(|err| RunnerError::Schema(err.to_string()))?;
            if validator.is_valid(&target) {
                Ok(())
            } else {
                let errors = validator
                    .iter_errors(&target)
                    .map(|err| format!("{err} at {}", err.instance_path()))
                    .collect::<Vec<_>>()
                    .join("; ");
                Err(RunnerError::Assertion(format!(
                    "value at {path} does not validate against inline schema: {errors}"
                )))
            }
        }
    }
}

fn evaluate_any_of(assertions: &[Assertion], context: &Value) -> Result<()> {
    if assertions.is_empty() {
        return Err(RunnerError::Assertion(
            "`any_of` requires at least one child assertion".to_string(),
        ));
    }
    let mut last_assertion_error: Option<String> = None;
    for child in assertions {
        match evaluate_assertion(child, context) {
            Ok(()) => return Ok(()),
            Err(RunnerError::Assertion(message)) => {
                last_assertion_error = Some(message);
            }
            // A missing path inside one branch of `any_of` simply means
            // "this branch does not apply" — try the next one. Other
            // structural errors (regex compile, schema compile) signal
            // a broken invariant and propagate.
            Err(RunnerError::JsonPath(jsonpath::JsonPathError::Missing(path))) => {
                last_assertion_error = Some(format!("path `{path}` did not resolve"));
            }
            Err(other) => return Err(other),
        }
    }
    Err(RunnerError::Assertion(format!(
        "no `any_of` branch matched (last failure: {})",
        last_assertion_error.unwrap_or_else(|| "unknown".to_string())
    )))
}

fn evaluate_for_each(path: &str, assertions: &[Assertion], context: &Value) -> Result<()> {
    let nodes = jsonpath::resolve(context, path)?;
    if nodes.is_empty() {
        // `for_each` over an empty set is vacuously true. Emit a warning
        // so an author who fat-fingered a JSONPath does not ship an
        // invariant that silently always passes. Run `wallfacer property
        // -v` to see this.
        warn!(
            jsonpath = path,
            "for_each path matched zero nodes; the assertion is vacuously true. \
             Double-check the path or wrap intentional empty-set cases in `any_of` / `not`."
        );
        return Ok(());
    }
    for (index, node) in nodes.into_iter().enumerate() {
        // Build a child context with the iterated node exposed under
        // `$.item`. Original `$.input` / `$.response` remain accessible so
        // assertions can correlate the current element with global state.
        let Some(base) = context.as_object() else {
            return Err(RunnerError::Assertion(
                "internal: evaluation context must be an object".to_string(),
            ));
        };
        let mut child = base.clone();
        child.insert("item".to_string(), node);
        child.insert("index".to_string(), json!(index));
        let child_context = Value::Object(child);
        for assertion in assertions {
            evaluate_assertion(assertion, &child_context).map_err(|err| match err {
                RunnerError::Assertion(message) => {
                    RunnerError::Assertion(format!("for_each at {path}[{index}]: {message}"))
                }
                other => other,
            })?;
        }
    }
    Ok(())
}

fn compare_number(
    path: &str,
    value: &Operand,
    context: &Value,
    compare: impl Fn(std::cmp::Ordering) -> bool,
) -> Result<()> {
    let left = jsonpath::resolve_one(context, path)?;
    let right = value.resolve(context)?;

    // Fast path: when both operands fit in `i128` (covers any JSON
    // integer, signed or unsigned), compare without going through f64.
    // f64 only has 53 bits of mantissa, so values like
    // `9_007_199_254_740_993` lose precision and equal their
    // `…992` neighbour — silently corrupting `at_most`/`at_least`.
    if let (Some(l), Some(r)) = (as_i128(&left), as_i128(&right)) {
        if compare(l.cmp(&r)) {
            return Ok(());
        }
        return Err(RunnerError::Assertion(format!(
            "numeric comparison failed: {l} vs {r}"
        )));
    }

    let Some(left_f) = left.as_f64() else {
        return Err(RunnerError::Assertion(format!(
            "expected {path} to resolve to a number"
        )));
    };
    let Some(right_f) = right.as_f64() else {
        return Err(RunnerError::Assertion(
            "expected comparison value to be a number".to_string(),
        ));
    };
    let ordering = left_f
        .partial_cmp(&right_f)
        .ok_or_else(|| RunnerError::Assertion("comparison against NaN".to_string()))?;
    if compare(ordering) {
        Ok(())
    } else {
        Err(RunnerError::Assertion(format!(
            "numeric comparison failed: {left_f} vs {right_f}"
        )))
    }
}

fn as_i128(value: &Value) -> Option<i128> {
    let Value::Number(n) = value else {
        return None;
    };
    if let Some(i) = n.as_i64() {
        Some(i as i128)
    } else {
        n.as_u64().map(|u| u as i128)
    }
}

fn compare_length(
    path: &str,
    value: &Operand,
    context: &Value,
    compare: impl FnOnce(usize, usize) -> bool,
) -> Result<()> {
    let left = jsonpath::resolve_one(context, path)?;
    let right = value.resolve(context)?;
    let Some(right) = right.as_u64().map(|value| value as usize) else {
        return Err(RunnerError::Assertion(
            "expected comparison value to be an integer".to_string(),
        ));
    };
    let Some(left) = length(&left) else {
        return Err(RunnerError::Assertion(format!(
            "expected {path} to resolve to an array or string"
        )));
    };
    if compare(left, right) {
        Ok(())
    } else {
        Err(RunnerError::Assertion(format!(
            "length comparison failed: {left} vs {right}"
        )))
    }
}

fn length(value: &Value) -> Option<usize> {
    match value {
        Value::Array(items) => Some(items.len()),
        Value::String(text) => Some(text.chars().count()),
        _ => None,
    }
}

fn json_type(value: &Value) -> JsonType {
    match value {
        Value::Null => JsonType::Null,
        Value::Bool(_) => JsonType::Boolean,
        Value::Number(number) if number.is_i64() || number.is_u64() => JsonType::Integer,
        Value::Number(_) => JsonType::Number,
        Value::String(_) => JsonType::String,
        Value::Array(_) => JsonType::Array,
        Value::Object(_) => JsonType::Object,
    }
}

impl Operand {
    /// Resolves an operand against the `{input, response}` context.
    ///
    /// * [`Operand::Path`] → JSONPath lookup (errors if missing).
    /// * [`Operand::Literal`] → returned verbatim.
    /// * [`Operand::Direct`] strings starting with `$` → JSONPath lookup
    ///   (legacy v1 contract); anything else → returned verbatim.
    pub fn resolve(&self, context: &Value) -> Result<Value> {
        match self {
            Operand::Path { path } => Ok(jsonpath::resolve_one(context, path)?),
            Operand::Literal { value } => Ok(value.clone()),
            Operand::Direct(Value::String(s)) if s.starts_with('$') => {
                Ok(jsonpath::resolve_one(context, s)?)
            }
            Operand::Direct(value) => Ok(value.clone()),
        }
    }
}

fn boundary_value(spec: &ValueSpec) -> Value {
    match spec.kind {
        ValueKind::String => {
            let len = spec.max_length.or(spec.min_length).unwrap_or(8).min(1024);
            Value::String("x".repeat(len))
        }
        ValueKind::Integer => json!(spec.max.or(spec.min).unwrap_or(1)),
        ValueKind::Number => Number::from_f64(spec.max.or(spec.min).unwrap_or(1) as f64)
            .map(Value::Number)
            .unwrap_or(Value::Null),
        ValueKind::Boolean => Value::Bool(true),
        ValueKind::Array => {
            let len = spec.max_items.or(spec.min_items).unwrap_or(1).min(64);
            let item_spec = spec.items.as_deref();
            Value::Array(
                (0..len)
                    .map(|_| item_spec.map(boundary_value).unwrap_or(Value::Null))
                    .collect(),
            )
        }
    }
}

fn generated_value(spec: &ValueSpec, rng: &mut impl RngCore) -> Value {
    match spec.kind {
        ValueKind::String => {
            let min = spec.min_length.unwrap_or(0);
            let max = spec.max_length.unwrap_or(32).max(min).min(1024);
            let len = min + (rng.next_u64() as usize % (max - min + 1));
            Value::String("a".repeat(len))
        }
        ValueKind::Integer => {
            let min = spec.min.unwrap_or(-100);
            let max = spec.max.unwrap_or(100).max(min);
            let span = (max as i128 - min as i128 + 1) as u64;
            json!(min + (rng.next_u64() % span) as i64)
        }
        ValueKind::Number => {
            let min = spec.min.unwrap_or(-100) as f64;
            let max = (spec.max.unwrap_or(100) as f64).max(min);
            let unit = rng.next_u64() as f64 / u64::MAX as f64;
            Number::from_f64(min + (max - min) * unit)
                .map(Value::Number)
                .unwrap_or(Value::Null)
        }
        ValueKind::Boolean => Value::Bool((rng.next_u64() & 1) == 0),
        ValueKind::Array => {
            let min = spec.min_items.unwrap_or(0);
            let max = spec.max_items.unwrap_or(8).max(min).min(64);
            let len = min + (rng.next_u64() as usize % (max - min + 1));
            let item_spec = spec.items.as_deref();
            Value::Array(
                (0..len)
                    .map(|_| {
                        item_spec
                            .map(|item| generated_value(item, rng))
                            .unwrap_or(Value::Null)
                    })
                    .collect(),
            )
        }
    }
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    clippy::unwrap_used,
    clippy::panic,
    clippy::unwrap_in_result
)]
mod tests {
    use super::*;
    use crate::property::dsl::parse;

    fn evaluate_yaml(source: &str, input: Value, response: Value) -> Result<()> {
        let file = parse(source).unwrap();
        evaluate(&file.invariants[0], input, response)
    }

    #[test]
    fn explicit_path_operand_works() {
        let source = r#"
version: 2
invariants:
  - name: t
    tool: x
    fixed: {}
    assert:
      - kind: equals
        lhs: { path: "$.response.x" }
        rhs: { value: 42 }
"#;
        evaluate_yaml(source, json!({}), json!({"x": 42})).unwrap();
        assert!(evaluate_yaml(source, json!({}), json!({"x": 41})).is_err());
    }

    #[test]
    fn at_least_uses_integer_comparison_beyond_f64_mantissa() {
        // 9_007_199_254_740_993 == 2^53 + 1, the first integer that loses
        // precision when round-tripped through f64. With the previous
        // f64-only path, `>= 9007199254740993` accepted 9007199254740992,
        // which is silently wrong.
        let source = r#"
version: 2
invariants:
  - name: precision
    tool: x
    fixed: {}
    assert:
      - kind: at_least
        path: "$.response.n"
        value: { value: 9007199254740993 }
"#;
        // Equal: must pass.
        evaluate_yaml(source, json!({}), json!({"n": 9_007_199_254_740_993_i64})).unwrap();
        // One below: must fail (used to silently pass via f64 rounding).
        let err =
            evaluate_yaml(source, json!({}), json!({"n": 9_007_199_254_740_992_i64})).unwrap_err();
        assert!(matches!(err, RunnerError::Assertion(_)));
    }

    #[test]
    fn legacy_string_operand_still_works() {
        let source = r#"
version: 1
invariants:
  - name: t
    tool: x
    fixed: {}
    assert:
      - kind: equals
        lhs: "$.response.x"
        rhs: "$.input.expected"
"#;
        evaluate_yaml(source, json!({"expected": 7}), json!({"x": 7})).unwrap();
    }

    #[test]
    fn all_of_combines_assertions() {
        let source = r#"
version: 2
invariants:
  - name: t
    tool: x
    fixed: {}
    assert:
      - kind: all_of
        assert:
          - kind: equals
            lhs: { path: "$.response.a" }
            rhs: { value: 1 }
          - kind: at_most
            path: "$.response.b"
            value: { value: 5 }
"#;
        evaluate_yaml(source, json!({}), json!({"a": 1, "b": 4})).unwrap();
        let err = evaluate_yaml(source, json!({}), json!({"a": 1, "b": 99})).unwrap_err();
        assert!(matches!(err, RunnerError::Assertion(_)));
    }

    #[test]
    fn any_of_succeeds_when_one_branch_passes() {
        let source = r#"
version: 2
invariants:
  - name: t
    tool: x
    fixed: {}
    assert:
      - kind: any_of
        assert:
          - kind: equals
            lhs: { path: "$.response.a" }
            rhs: { value: 1 }
          - kind: equals
            lhs: { path: "$.response.a" }
            rhs: { value: 2 }
"#;
        evaluate_yaml(source, json!({}), json!({"a": 2})).unwrap();
        let err = evaluate_yaml(source, json!({}), json!({"a": 9})).unwrap_err();
        assert!(matches!(err, RunnerError::Assertion(message) if message.contains("any_of")));
    }

    #[test]
    fn not_inverts_assertion_outcome() {
        let source = r#"
version: 2
invariants:
  - name: t
    tool: x
    fixed: {}
    assert:
      - kind: not
        assertion:
          kind: equals
          lhs: { path: "$.response.a" }
          rhs: { value: 0 }
"#;
        evaluate_yaml(source, json!({}), json!({"a": 5})).unwrap();
        let err = evaluate_yaml(source, json!({}), json!({"a": 0})).unwrap_err();
        assert!(matches!(err, RunnerError::Assertion(_)));
    }

    #[test]
    fn for_each_visits_every_node() {
        let source = r#"
version: 2
invariants:
  - name: t
    tool: x
    fixed: {}
    assert:
      - kind: for_each
        path: "$.response.items[*]"
        assert:
          - kind: at_least
            path: "$.item.score"
            value: { value: 0 }
"#;
        evaluate_yaml(
            source,
            json!({}),
            json!({"items": [{"score": 1}, {"score": 5}]}),
        )
        .unwrap();
        let err = evaluate_yaml(
            source,
            json!({}),
            json!({"items": [{"score": 1}, {"score": -3}]}),
        )
        .unwrap_err();
        assert!(matches!(err, RunnerError::Assertion(message) if message.contains("for_each at")));
    }

    #[test]
    fn matches_schema_validates_inline_schema() {
        let source = r#"
version: 2
invariants:
  - name: t
    tool: x
    fixed: {}
    assert:
      - kind: matches_schema
        path: "$.response.user"
        schema:
          type: object
          required: [name]
          properties:
            name: { type: string }
            age: { type: integer, minimum: 0 }
"#;
        evaluate_yaml(
            source,
            json!({}),
            json!({"user": {"name": "alice", "age": 30}}),
        )
        .unwrap();
        let err = evaluate_yaml(source, json!({}), json!({"user": {"age": -1}})).unwrap_err();
        assert!(matches!(err, RunnerError::Assertion(message) if message.contains("schema")));
    }
}