lemma-engine 0.11.4

A pure, declarative language for business rules.
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
//! End-to-end tests for strict veto semantics under normalization.
//!
//! Normalization may only delete a subexpression whose removal cannot change the
//! result: a total operand, a literal `false` operand of `and`, or a literal `true`
//! operand of `or`. Those absorbing literals decide the connective whatever the
//! other operands are. These tests pin the contract that the folded and unfolded
//! forms of the same expression produce identical evaluation results for all of:
//! value present, value missing, and operand vetoes.

use indexmap::IndexMap;
use lemma::{DateTimeValue, Engine, EvaluateDataValue};
use std::time::Instant;

fn load(code: &str) -> Engine {
    let mut engine = Engine::new();
    engine
        .load([(lemma::SourceType::Volatile, code.to_string())])
        .expect("spec must load");
    engine
}

fn run(engine: &Engine, spec: &str, inputs: &[(&str, &str)]) -> lemma::Response {
    let now = DateTimeValue::now();
    let data: IndexMap<String, EvaluateDataValue> = inputs
        .iter()
        .map(|(name, value)| (name.to_string(), (*value).into()))
        .collect();
    engine
        .evaluate(None, spec, Some(&now), data, None, false)
        .expect("evaluation must succeed")
}

fn rule_result<'response>(
    response: &'response lemma::Response,
    rule_name: &str,
) -> &'response lemma::RuleResult {
    response
        .results
        .values()
        .find(|result| result.rule.name == rule_name)
        .unwrap_or_else(|| panic!("rule '{rule_name}' must be in the response"))
}

#[test]
fn multiply_by_zero_with_missing_data_vetoes() {
    let engine = load(
        r#"
spec strict_multiply
data x: number
rule r: x * 0
"#,
    );

    let missing = run(&engine, "strict_multiply", &[]);
    let result = rule_result(&missing, "r");
    assert!(
        result.vetoed,
        "x is missing: the multiply must veto, not fold to 0"
    );

    let supplied = run(&engine, "strict_multiply", &[("x", "7")]);
    let result = rule_result(&supplied, "r");
    assert!(!result.vetoed);
    assert_eq!(result.result(), Some("0"));
}

#[test]
fn multiply_measure_data_by_zero_keeps_unit() {
    let engine = load(
        r#"
spec strict_measure_multiply
data money: measure -> unit eur: 1
data price: money
rule r: price * 0
"#,
    );

    let response = run(&engine, "strict_measure_multiply", &[("price", "5 eur")]);
    let result = rule_result(&response, "r");
    assert!(!result.vetoed);
    assert_eq!(
        result.result(),
        Some("0 eur"),
        "the runtime multiply must keep the measure's unit"
    );

    let missing = run(&engine, "strict_measure_multiply", &[]);
    let result = rule_result(&missing, "r");
    assert!(result.vetoed, "price is missing: the multiply must veto");
}

#[test]
fn multiply_measure_literal_by_zero_folds_to_typed_zero() {
    let engine = load(
        r#"
spec folded_measure_multiply
data money: measure -> unit eur: 1
data rate: 5 eur
rule r: rate * 0
"#,
    );

    // `rate` is bound in the spec, so the inlined literal product folds at
    // planning time; the folded zero must keep the unit signature.
    let response = run(&engine, "folded_measure_multiply", &[]);
    let result = rule_result(&response, "r");
    assert!(!result.vetoed);
    assert_eq!(result.result(), Some("0 eur"));
}

#[test]
fn multiply_vetoing_rule_by_zero_propagates_the_veto() {
    let engine = load(
        r#"
spec strict_veto_multiply
rule blocked: veto "credit check failed"
rule r: blocked * 0
"#,
    );

    let response = run(&engine, "strict_veto_multiply", &[]);
    let result = rule_result(&response, "r");
    assert!(
        result.vetoed,
        "the inlined veto must propagate through the multiply"
    );
    assert_eq!(result.veto_reason.as_deref(), Some("credit check failed"));
}

#[test]
fn and_with_literal_false_is_false_without_missing_data() {
    let engine = load(
        r#"
spec strict_and
data flag: boolean
rule r: flag and (1 > 2)
"#,
    );

    // `1 > 2` constant-folds to `false`, which decides the conjunction whatever
    // `flag` is, so a missing `flag` does not veto.
    let missing = run(&engine, "strict_and", &[]);
    let result = rule_result(&missing, "r");
    assert!(!result.vetoed, "flag is not needed: false decides the and");
    assert_eq!(result.result(), Some("false"));

    let supplied = run(&engine, "strict_and", &[("flag", "yes")]);
    let result = rule_result(&supplied, "r");
    assert!(!result.vetoed);
    assert_eq!(result.result(), Some("false"));
}

#[test]
fn exp_of_log_keeps_domain_veto() {
    let engine = load(
        r#"
spec strict_exp_log
data offset: number
rule r: exp (log offset)
"#,
    );

    let negative = run(&engine, "strict_exp_log", &[("offset", "-5")]);
    let result = rule_result(&negative, "r");
    assert!(
        result.vetoed,
        "log of a negative operand must veto; the exp/log collapse would erase it"
    );

    let positive = run(&engine, "strict_exp_log", &[("offset", "5")]);
    let result = rule_result(&positive, "r");
    assert!(!result.vetoed);
}

#[test]
fn power_zero_with_missing_data_vetoes() {
    let engine = load(
        r#"
spec strict_power_zero
data x: number
rule r: x ^ 0
"#,
    );

    let missing = run(&engine, "strict_power_zero", &[]);
    let result = rule_result(&missing, "r");
    assert!(
        result.vetoed,
        "x is missing: the power must veto, not fold to 1"
    );

    let supplied = run(&engine, "strict_power_zero", &[("x", "9")]);
    let result = rule_result(&supplied, "r");
    assert!(!result.vetoed);
    assert_eq!(result.result(), Some("1"));
}

#[test]
fn self_doubling_rule_chain_shares_graph_cells() {
    // Each rule references its predecessor twice. Shared NormalForm graph
    // growth is linear in chain length — not Expression tree blowup.
    // Cell-count band is asserted in transitive_normalization_plan_shape::
    // doubling_chain_graph_stays_linear (crate-private normal_forms table).
    let n = 40;
    let mut code = String::from("\nspec doubling\ndata input: number\nrule r0: input > 0\n");
    for index in 1..=n {
        code.push_str(&format!(
            "rule r{index}: r{previous} and not r{previous}\n",
            previous = index - 1
        ));
    }

    let started = Instant::now();
    let mut engine = Engine::new();
    engine
        .load([(lemma::SourceType::Volatile, code.to_string())])
        .expect("shared-graph self-doubling chain must plan");
    let response = run(&engine, "doubling", &[("input", "1")]);
    let result = rule_result(&response, &format!("r{n}"));
    assert!(!result.vetoed);
    assert_eq!(result.result(), Some("false"));
    // Regression guard only (not a microbench). Pre-DAG-collect walks blew past
    // this at much smaller n (~18s at n=12).
    assert!(
        started.elapsed().as_secs_f64() < 2.0,
        "doubling n={n} load+run took {:?}; expected shared-graph path",
        started.elapsed()
    );
}

#[derive(Debug)]
enum Outcome {
    Result(&'static str),
    Missing(Vec<&'static str>),
    Veto(&'static str),
}

fn assert_outcome(result: &lemma::RuleResult, expected: &Outcome, context: &str) {
    match expected {
        Outcome::Result(value) => {
            assert!(
                !result.vetoed,
                "{context}: expected {value}, got {result:?}"
            );
            assert_eq!(result.result(), Some(*value), "{context}");
        }
        Outcome::Missing(names) => {
            assert!(
                result.vetoed,
                "{context}: expected missing data, got {result:?}"
            );
            let mut actual = result.missing_data().to_vec();
            actual.sort();
            let mut wanted: Vec<String> = names.iter().map(|name| name.to_string()).collect();
            wanted.sort();
            assert_eq!(actual, wanted, "{context}");
        }
        Outcome::Veto(reason) => {
            assert!(result.vetoed, "{context}: expected veto, got {result:?}");
            assert_eq!(result.veto_reason.as_deref(), Some(*reason), "{context}");
            assert!(result.missing_data().is_empty(), "{context}");
        }
    }
}

const RESIDUAL_FIRST_FOLDED: &str = r#"
spec order
data model: text
data extra: boolean
data flag_c: boolean
data amount: number
rule r: 0
  unless model is "b" and extra then amount
  unless flag_c and model is "c" then 3
"#;

const RESIDUAL_FIRST_UNFOLDED: &str = r#"
spec order
data model: text
data extra: boolean
data flag_c: boolean
data amount: number
rule r: 0
  unless model is "b" and extra then amount
  unless flag_c and model is "c" then 3
  unless model is "z" then 9
"#;

#[test]
fn residual_first_conjunction_folded_and_unfolded_agree() {
    let cases: [(&[(&str, &str)], Outcome); 6] = [
        (
            &[("model", "b"), ("extra", "true"), ("amount", "5")],
            Outcome::Result("5"),
        ),
        (
            &[("model", "b"), ("extra", "true")],
            Outcome::Missing(vec!["amount"]),
        ),
        (&[("model", "b"), ("extra", "false")], Outcome::Result("0")),
        (&[("model", "c")], Outcome::Missing(vec!["flag_c"])),
        (&[("model", "c"), ("flag_c", "false")], Outcome::Result("0")),
        (&[("model", "c"), ("flag_c", "true")], Outcome::Result("3")),
    ];
    for (form, source) in [
        ("folded", RESIDUAL_FIRST_FOLDED),
        ("unfolded", RESIDUAL_FIRST_UNFOLDED),
    ] {
        let engine = load(source);
        for (inputs, expected) in &cases {
            let response = run(&engine, "order", inputs);
            let result = rule_result(&response, "r");
            let context = format!("{form} {inputs:?}");
            assert_outcome(result, expected, &context);
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum OperandKind {
    True,
    False,
    Missing,
    Veto,
}

/// Operand text for the matrix spec. `Veto` operands are the rule `blocked`.
fn operand_expression(kind: OperandKind, name: &'static str) -> &'static str {
    match kind {
        OperandKind::Veto => "blocked",
        OperandKind::True | OperandKind::False | OperandKind::Missing => name,
    }
}

/// Expected outcome of `first and second`, in evaluation order. A false operand
/// decides regardless of position. Otherwise the first missing or vetoed operand in
/// source order propagates.
fn expected_and(
    first: (OperandKind, &'static str),
    second: (OperandKind, &'static str),
) -> Outcome {
    let operands = [first, second];
    if operands.iter().any(|(kind, _)| *kind == OperandKind::False) {
        return Outcome::Result("false");
    }
    for (kind, _) in operands {
        match kind {
            OperandKind::True => {}
            OperandKind::False => unreachable!("BUG: false operand decided above"),
            OperandKind::Missing => {
                let names = operands
                    .iter()
                    .filter(|(kind, _)| *kind == OperandKind::Missing)
                    .map(|(_, name)| *name)
                    .collect();
                return Outcome::Missing(names);
            }
            OperandKind::Veto => return Outcome::Veto("blocked"),
        }
    }
    Outcome::Result("true")
}

#[test]
fn and_is_order_independent_for_every_operand_kind_pair() {
    let kinds = [
        OperandKind::True,
        OperandKind::False,
        OperandKind::Missing,
        OperandKind::Veto,
    ];
    for first_kind in kinds {
        for second_kind in kinds {
            let first = (first_kind, "a");
            let second = (second_kind, "b");
            let engine = load(&format!(
                r#"
spec matrix
data a: boolean
data b: boolean
rule blocked: veto "blocked"
rule forward: {} and {}
rule backward: {} and {}
"#,
                operand_expression(first_kind, "a"),
                operand_expression(second_kind, "b"),
                operand_expression(second_kind, "b"),
                operand_expression(first_kind, "a"),
            ));
            let mut inputs: Vec<(&str, &str)> = Vec::new();
            match first_kind {
                OperandKind::True => inputs.push(("a", "true")),
                OperandKind::False => inputs.push(("a", "false")),
                OperandKind::Missing | OperandKind::Veto => {}
            }
            match second_kind {
                OperandKind::True => inputs.push(("b", "true")),
                OperandKind::False => inputs.push(("b", "false")),
                OperandKind::Missing | OperandKind::Veto => {}
            }
            let response = run(&engine, "matrix", &inputs);
            assert_outcome(
                rule_result(&response, "forward"),
                &expected_and(first, second),
                &format!("forward {first_kind:?} and {second_kind:?}"),
            );
            assert_outcome(
                rule_result(&response, "backward"),
                &expected_and(second, first),
                &format!("backward {second_kind:?} and {first_kind:?}"),
            );
        }
    }
}

/// Expected outcome of `first or second`. A true operand decides regardless of
/// position. Otherwise the first missing or vetoed operand in source order
/// propagates.
fn expected_or(first: (OperandKind, &'static str), second: (OperandKind, &'static str)) -> Outcome {
    let operands = [first, second];
    if operands.iter().any(|(kind, _)| *kind == OperandKind::True) {
        return Outcome::Result("true");
    }
    for (kind, _) in operands {
        match kind {
            OperandKind::False => {}
            OperandKind::True => unreachable!("BUG: true operand decided above"),
            OperandKind::Missing => {
                let names = operands
                    .iter()
                    .filter(|(kind, _)| *kind == OperandKind::Missing)
                    .map(|(_, name)| *name)
                    .collect();
                return Outcome::Missing(names);
            }
            OperandKind::Veto => return Outcome::Veto("blocked"),
        }
    }
    Outcome::Result("false")
}

#[test]
fn or_is_order_independent_for_every_operand_kind_pair() {
    let kinds = [
        OperandKind::True,
        OperandKind::False,
        OperandKind::Missing,
        OperandKind::Veto,
    ];
    for first_kind in kinds {
        for second_kind in kinds {
            let first = (first_kind, "a");
            let second = (second_kind, "b");
            let engine = load(&format!(
                r#"
spec matrix
data a: boolean
data b: boolean
rule blocked: veto "blocked"
rule forward: {} or {}
rule backward: {} or {}
"#,
                operand_expression(first_kind, "a"),
                operand_expression(second_kind, "b"),
                operand_expression(second_kind, "b"),
                operand_expression(first_kind, "a"),
            ));
            let mut inputs: Vec<(&str, &str)> = Vec::new();
            match first_kind {
                OperandKind::True => inputs.push(("a", "true")),
                OperandKind::False => inputs.push(("a", "false")),
                OperandKind::Missing | OperandKind::Veto => {}
            }
            match second_kind {
                OperandKind::True => inputs.push(("b", "true")),
                OperandKind::False => inputs.push(("b", "false")),
                OperandKind::Missing | OperandKind::Veto => {}
            }
            let response = run(&engine, "matrix", &inputs);
            assert_outcome(
                rule_result(&response, "forward"),
                &expected_or(first, second),
                &format!("forward {first_kind:?} or {second_kind:?}"),
            );
            assert_outcome(
                rule_result(&response, "backward"),
                &expected_or(second, first),
                &format!("backward {second_kind:?} or {first_kind:?}"),
            );
        }
    }
}

// =============================================================================
// An `unless` decided in planning that leaves only a veto
// =============================================================================

/// `r` of spec `decided` with declarations `body` is the veto `closed` for `inputs`, and
/// needs no data.
fn assert_always_closed(body: &str, inputs: &[(&str, &str)]) {
    let engine = load(&format!("spec decided\n{body}\n"));
    for given in [inputs, &[]] {
        let response = run(&engine, "decided", given);
        let result = rule_result(&response, "r");
        assert!(result.vetoed, "{body}\nmust veto, given {given:?}");
        assert_eq!(result.veto_reason.as_deref(), Some("closed"), "{body}");
        assert!(
            result.missing_data().is_empty(),
            "{body}\nthe decided branch reads no data, got {:?}",
            result.missing_data()
        );
    }
}

#[test]
fn always_true_unless_with_a_veto_over_data_is_the_veto() {
    assert_always_closed(
        "data x: number\nrule r: x\n  unless 2 > 1 then veto \"closed\"",
        &[("x", "3")],
    );
}

#[test]
fn literal_true_unless_with_a_veto_is_the_veto() {
    assert_always_closed(
        "data x: number\nrule r: x\n  unless true then veto \"closed\"",
        &[("x", "3")],
    );
}

#[test]
fn always_true_unless_with_a_veto_over_a_literal_is_the_veto() {
    assert_always_closed("rule r: 5\n  unless 2 > 1 then veto \"closed\"", &[]);
}

#[test]
fn always_true_unless_with_a_veto_over_text_is_the_veto() {
    assert_always_closed(
        "data name: text\nrule r: name\n  unless 2 > 1 then veto \"closed\"",
        &[("name", "Ada")],
    );
}

#[test]
fn always_true_unless_with_a_veto_after_a_live_unless_is_the_veto() {
    assert_always_closed(
        "data x: number\ndata y: number\nrule r: x\n  unless y > 1 then 4\n  unless 2 > 1 then veto \"closed\"",
        &[("x", "3"), ("y", "2")],
    );
}

#[test]
fn default_veto_under_an_always_false_unless_is_the_veto() {
    assert_always_closed(
        "data x: number\nrule r: veto \"closed\"\n  unless 1 > 2 then x",
        &[("x", "3")],
    );
}

#[test]
fn always_true_veto_before_a_live_veto_is_a_veto() {
    let engine = load(
        r#"
spec decided
data x: number
data y: number
rule r: x
  unless 2 > 1 then veto "closed"
  unless y > 1 then veto "full"
"#,
    );
    for (y, reason) in [("0", "closed"), ("2", "full")] {
        let response = run(&engine, "decided", &[("x", "3"), ("y", y)]);
        let result = rule_result(&response, "r");
        assert_eq!(result.veto_reason.as_deref(), Some(reason), "y = {y}");
    }
    let missing = run(&engine, "decided", &[("x", "3")]);
    assert_eq!(rule_result(&missing, "r").missing_data(), ["y"]);
}

#[test]
fn default_veto_before_a_live_veto_is_a_veto() {
    let engine = load(
        r#"
spec decided
data x: number
data y: number
rule r: veto "closed"
  unless 1 > 2 then x
  unless y > 1 then veto "full"
"#,
    );
    for (y, reason) in [("0", "closed"), ("2", "full")] {
        let response = run(&engine, "decided", &[("y", y)]);
        let result = rule_result(&response, "r");
        assert_eq!(result.veto_reason.as_deref(), Some(reason), "y = {y}");
    }
}

#[test]
fn the_last_of_two_always_true_vetoes_is_the_veto() {
    assert_always_closed(
        "data x: number\nrule r: x\n  unless 2 > 1 then veto \"full\"\n  unless 3 > 2 then veto \"closed\"",
        &[("x", "3")],
    );
}

#[test]
fn always_true_unless_with_a_vetoing_rule_is_the_veto() {
    assert_always_closed(
        "data x: number\nrule blocked: veto \"closed\"\nrule r: x\n  unless 2 > 1 then blocked",
        &[("x", "3")],
    );
}

#[test]
fn default_vetoing_rule_under_an_always_false_unless_is_the_veto() {
    assert_always_closed(
        "data x: number\nrule blocked: veto \"closed\"\nrule r: blocked\n  unless 1 > 2 then x",
        &[("x", "3")],
    );
}

#[test]
fn always_true_unless_with_a_veto_over_a_measure_is_the_veto() {
    assert_always_closed(
        "data distance: measure\n  -> unit meter: 1\ndata x: distance\nrule r: x\n  unless 2 > 1 then veto \"closed\"",
        &[("x", "3 meter")],
    );
}

#[test]
fn always_true_unless_with_a_veto_over_a_list_is_the_veto() {
    assert_always_closed(
        "data xs: number list\nrule r: xs\n  unless 2 > 1 then veto \"closed\"",
        &[],
    );
}

#[test]
fn arithmetic_on_a_rule_decided_to_a_veto_is_the_veto() {
    assert_always_closed(
        "data x: number\nrule decided_veto: x\n  unless 2 > 1 then veto \"closed\"\nrule r: decided_veto + 1",
        &[("x", "3")],
    );
}

#[test]
fn comparing_a_rule_decided_to_a_veto_is_the_veto() {
    assert_always_closed(
        "data x: number\nrule decided_veto: x\n  unless 2 > 1 then veto \"closed\"\nrule r: decided_veto > 1",
        &[("x", "3")],
    );
}

#[test]
fn explaining_a_rule_decided_to_a_veto_gives_the_veto() {
    let engine = load(
        r#"
spec decided
data x: number
data y: number
rule r: x
  unless 2 > 1 then veto "closed"
rule s: r + 1
rule t: x
  unless 2 > 1 then veto "closed"
  unless y > 1 then veto "full"
"#,
    );
    let now = DateTimeValue::now();
    for inputs in [&[("x", "3"), ("y", "0")][..], &[("y", "2")][..], &[][..]] {
        let data: IndexMap<String, EvaluateDataValue> = inputs
            .iter()
            .map(|(name, value)| (name.to_string(), (*value).into()))
            .collect();
        let response = engine
            .evaluate(None, "decided", Some(&now), data, None, true)
            .expect("evaluation must succeed");
        for name in ["r", "s"] {
            let result = rule_result(&response, name);
            assert_eq!(result.veto_reason.as_deref(), Some("closed"), "{name}");
        }
    }
}

/// The same decision through a rule: the outcome the folded forms must have.
#[test]
fn always_true_unless_with_a_veto_through_a_rule_is_the_veto() {
    assert_always_closed(
        "data x: number\nrule always: 2 > 1\nrule r: x\n  unless always then veto \"closed\"",
        &[("x", "3")],
    );
}

/// A live `unless` after the decided veto still wins when it matches.
#[test]
fn live_unless_after_an_always_true_veto_still_decides() {
    let engine = load(
        r#"
spec decided
data x: number
data y: number
rule r: x
  unless 2 > 1 then veto "closed"
  unless y > 1 then 4
"#,
    );
    let matched = run(&engine, "decided", &[("x", "3"), ("y", "2")]);
    assert_eq!(rule_result(&matched, "r").result(), Some("4"));
    let unmatched = run(&engine, "decided", &[("x", "3"), ("y", "0")]);
    assert_eq!(
        rule_result(&unmatched, "r").veto_reason.as_deref(),
        Some("closed")
    );
}

// =============================================================================
// Arithmetic that folds away over an `unless`
// =============================================================================

/// `r`, declared as `folded` over `base`, has the outcome of `base` whatever part of
/// `inputs` is given: the same value, or the same missing data.
fn assert_reads_as_base(base: &str, folded: &str, inputs: &[(&str, &str)]) {
    let engine = load(&format!(
        "spec folding\ndata x: number\ndata y: number\ndata name: text\nrule base: x\n{base}\n{folded}\n"
    ));
    for subset in 0..(1usize << inputs.len()) {
        let given: Vec<(&str, &str)> = inputs
            .iter()
            .enumerate()
            .filter(|(index, _)| subset & (1 << index) != 0)
            .map(|(_, input)| *input)
            .collect();
        let response = run(&engine, "folding", &given);
        let plain = rule_result(&response, "base");
        let result = rule_result(&response, "r");
        let context = format!("{folded}, given {given:?}");
        assert_eq!(result.result(), plain.result(), "{context}");
        assert_eq!(result.missing_data(), plain.missing_data(), "{context}");
        assert_eq!(result.veto_reason, plain.veto_reason, "{context}");
        assert_eq!(
            result.missing_data().is_empty(),
            result.result().is_some(),
            "{context}"
        );
    }
}

const THRESHOLD: &str = "  unless y > 1 then 1";
/// `y` picks the default, so `x` is needed.
const BELOW: &[(&str, &str)] = &[("x", "5"), ("y", "0")];

#[test]
fn adding_zero_to_an_unless_misses_the_same_data() {
    assert_reads_as_base(THRESHOLD, "rule r: base + 0", BELOW);
}

#[test]
fn subtracting_zero_from_an_unless_misses_the_same_data() {
    assert_reads_as_base(THRESHOLD, "rule r: base - 0", BELOW);
}

#[test]
fn multiplying_an_unless_by_one_misses_the_same_data() {
    assert_reads_as_base(THRESHOLD, "rule r: base * 1", BELOW);
}

#[test]
fn adding_zero_twice_to_an_unless_misses_the_same_data() {
    assert_reads_as_base(THRESHOLD, "rule once: base + 0\nrule r: once + 0", BELOW);
}

#[test]
fn adding_zero_to_a_three_way_unless_misses_the_same_data() {
    assert_reads_as_base(
        "  unless y > 1 then 1\n  unless y > 5 then 2",
        "rule r: base + 0",
        BELOW,
    );
}

#[test]
fn adding_zero_to_an_unless_on_text_misses_the_same_data() {
    assert_reads_as_base(
        "  unless name is \"Ada\" then 1",
        "rule r: base + 0",
        &[("x", "5"), ("name", "Grace")],
    );
}

#[test]
fn negating_an_unless_twice_misses_the_same_data() {
    assert_reads_as_base(THRESHOLD, "rule r: -(-base)", BELOW);
}

#[test]
fn raising_an_unless_to_the_first_power_misses_the_same_data() {
    assert_reads_as_base(THRESHOLD, "rule r: base ^ 1", BELOW);
}

#[test]
fn dividing_an_unless_by_one_misses_the_same_data() {
    assert_reads_as_base(THRESHOLD, "rule r: base / 1", BELOW);
}

#[test]
fn adding_zero_to_an_alias_of_an_unless_misses_the_same_data() {
    assert_reads_as_base(THRESHOLD, "rule alias: base\nrule r: alias + 0", BELOW);
}

#[test]
fn a_larger_sum_over_a_folded_unless_names_what_both_miss() {
    let engine = load(
        r#"
spec folding
data x: number
data y: number
data z: number
rule base: x
  unless y > 1 then 1
rule r: (base + 0) + z
"#,
    );
    let response = run(&engine, "folding", &[("y", "0")]);
    assert_eq!(rule_result(&response, "r").missing_data(), ["x", "z"]);
}

/// `z` sits in a branch planning drops: it is never read, so never missing.
#[test]
fn an_always_false_unless_over_an_unless_misses_the_same_data() {
    assert_reads_as_base(
        THRESHOLD,
        "data z: number\nrule r: base\n  unless 1 > 2 then z",
        BELOW,
    );
}

#[test]
fn an_always_true_unless_to_an_unless_misses_the_same_data() {
    assert_reads_as_base(
        THRESHOLD,
        "data z: number\nrule r: z\n  unless 2 > 1 then base",
        BELOW,
    );
}

#[test]
fn an_always_false_unless_over_a_folded_unless_misses_the_same_data() {
    assert_reads_as_base(
        THRESHOLD,
        "data z: number\nrule r: (base + 0)\n  unless 1 > 2 then z",
        BELOW,
    );
}

/// A matching `unless` needs no default: `x` is not missing.
#[test]
fn adding_zero_to_a_matching_unless_needs_no_default() {
    let engine = load(
        r#"
spec folding
data x: number
data y: number
rule base: x
  unless y > 1 then 1
rule r: base + 0
"#,
    );
    let response = run(&engine, "folding", &[("y", "2")]);
    assert_eq!(rule_result(&response, "r").result(), Some("1"));
}

/// A reference folds nothing: the outcome the folded forms must have.
#[test]
fn a_reference_to_an_unless_misses_the_same_data() {
    assert_reads_as_base(THRESHOLD, "rule r: base", BELOW);
}