cp-ast-core 0.1.3

Core AST types for competitive programming problem specification DSL
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
use cp_ast_core::constraint::*;
use cp_ast_core::operation::*;
use cp_ast_core::structure::*;

#[test]
fn engine_construction() {
    let engine = AstEngine::new();
    assert!(engine.structure.contains(engine.structure.root()));
    assert!(engine.constraints.is_empty());
}

#[test]
fn engine_default() {
    let engine = AstEngine::default();
    assert_eq!(engine.structure.len(), 1); // root node
}

#[test]
fn action_fill_hole_construction() {
    let action = Action::FillHole {
        target: NodeId::from_raw(1),
        fill: FillContent::Scalar {
            name: "N".to_owned(),
            typ: VarType::Int,
        },
    };
    assert!(matches!(action, Action::FillHole { .. }));
}

#[test]
fn action_add_constraint_construction() {
    let action = Action::AddConstraint {
        target: NodeId::from_raw(1),
        constraint: ConstraintDef {
            kind: ConstraintDefKind::Range {
                lower: "1".to_owned(),
                upper: "100".to_owned(),
            },
        },
    };
    assert!(matches!(action, Action::AddConstraint { .. }));
}

#[test]
fn action_remove_constraint_construction() {
    let action = Action::RemoveConstraint {
        constraint_id: ConstraintId::from_raw(0),
    };
    assert!(matches!(action, Action::RemoveConstraint { .. }));
}

#[test]
fn operation_error_node_not_found() {
    let err = OperationError::NodeNotFound {
        node: NodeId::from_raw(99),
    };
    assert!(matches!(err, OperationError::NodeNotFound { .. }));
}

#[test]
fn operation_error_constraint_violation() {
    let err = OperationError::ConstraintViolation {
        violated_constraints: vec![ViolationDetail {
            constraint_id: ConstraintId::from_raw(0),
            description: "out of range".to_owned(),
            suggestion: Some("use value within 1..100".to_owned()),
        }],
    };
    assert!(matches!(err, OperationError::ConstraintViolation { .. }));
}

#[test]
fn apply_result_construction() {
    let result = ApplyResult {
        created_nodes: vec![NodeId::from_raw(1)],
        removed_nodes: vec![],
        created_constraints: vec![ConstraintId::from_raw(0)],
        affected_constraints: vec![],
    };
    assert_eq!(result.created_nodes.len(), 1);
}

#[test]
fn fill_content_all_variants() {
    let scalar = FillContent::Scalar {
        name: "N".to_owned(),
        typ: VarType::Int,
    };
    let array = FillContent::Array {
        name: "A".to_owned(),
        element_type: VarType::Int,
        length: LengthSpec::RefVar(NodeId::from_raw(1)),
    };
    let grid = FillContent::Grid {
        name: "G".to_owned(),
        rows: LengthSpec::Fixed(3),
        cols: LengthSpec::Fixed(3),
        cell_type: VarType::Int,
    };
    let section = FillContent::Section {
        label: "Input".to_owned(),
    };
    let output_val = FillContent::OutputSingleValue { typ: VarType::Int };
    let output_yn = FillContent::OutputYesNo;

    // Verify all variants can be created
    assert!(matches!(scalar, FillContent::Scalar { .. }));
    assert!(matches!(array, FillContent::Array { .. }));
    assert!(matches!(grid, FillContent::Grid { .. }));
    assert!(matches!(section, FillContent::Section { .. }));
    assert!(matches!(output_val, FillContent::OutputSingleValue { .. }));
    assert!(matches!(output_yn, FillContent::OutputYesNo));
}

#[test]
fn constraint_def_all_kinds() {
    let range = ConstraintDefKind::Range {
        lower: "1".to_owned(),
        upper: "100".to_owned(),
    };
    let type_decl = ConstraintDefKind::TypeDecl { typ: VarType::Int };
    let relation = ConstraintDefKind::Relation {
        op: RelationOp::Le,
        rhs: "N".to_owned(),
    };
    let distinct = ConstraintDefKind::Distinct;
    let sorted = ConstraintDefKind::Sorted {
        order: SortOrder::Ascending,
    };
    let property = ConstraintDefKind::Property {
        tag: "simple".to_owned(),
    };
    let sum_bound = ConstraintDefKind::SumBound {
        over_var: "N".to_owned(),
        upper: "2*10^5".to_owned(),
    };
    let guarantee = ConstraintDefKind::Guarantee {
        description: "valid".to_owned(),
    };

    // Verify all constraint kinds can be created
    assert!(matches!(range, ConstraintDefKind::Range { .. }));
    assert!(matches!(type_decl, ConstraintDefKind::TypeDecl { .. }));
    assert!(matches!(relation, ConstraintDefKind::Relation { .. }));
    assert!(matches!(distinct, ConstraintDefKind::Distinct));
    assert!(matches!(sorted, ConstraintDefKind::Sorted { .. }));
    assert!(matches!(property, ConstraintDefKind::Property { .. }));
    assert!(matches!(sum_bound, ConstraintDefKind::SumBound { .. }));
    assert!(matches!(guarantee, ConstraintDefKind::Guarantee { .. }));
}

// New tests for the implemented operations

#[test]
fn fill_hole_scalar_success() {
    let mut engine = AstEngine::new();
    // Add a hole to the structure
    let hole_id = engine.structure.add_node(NodeKind::Hole {
        expected_kind: None,
    });
    if let Some(root) = engine.structure.get_mut(engine.structure.root()) {
        root.set_kind(NodeKind::Sequence {
            children: vec![hole_id],
        });
    }

    let result = engine.apply(&Action::FillHole {
        target: hole_id,
        fill: FillContent::Scalar {
            name: "N".to_owned(),
            typ: VarType::Int,
        },
    });

    let result = result.unwrap();
    // The hole itself is replaced (not a new node)
    assert!(matches!(
        engine.structure.get(hole_id).unwrap().kind(),
        NodeKind::Scalar { .. }
    ));
    // TypeDecl constraint auto-added
    assert!(!result.created_constraints.is_empty());
}

#[test]
fn fill_hole_nonexistent_node_fails() {
    let mut engine = AstEngine::new();
    let result = engine.apply(&Action::FillHole {
        target: NodeId::from_raw(999),
        fill: FillContent::Scalar {
            name: "N".to_owned(),
            typ: VarType::Int,
        },
    });
    assert!(matches!(result, Err(OperationError::NodeNotFound { .. })));
}

#[test]
fn fill_hole_non_hole_fails() {
    let mut engine = AstEngine::new();
    let scalar_id = engine.structure.add_node(NodeKind::Scalar {
        name: Ident::new("N"),
    });
    let result = engine.apply(&Action::FillHole {
        target: scalar_id,
        fill: FillContent::Scalar {
            name: "M".to_owned(),
            typ: VarType::Int,
        },
    });
    assert!(matches!(
        result,
        Err(OperationError::InvalidOperation { .. })
    ));
}

#[test]
fn fill_hole_array_creates_structure() {
    let mut engine = AstEngine::new();
    let n_id = engine.structure.add_node(NodeKind::Scalar {
        name: Ident::new("N"),
    });
    let hole_id = engine.structure.add_node(NodeKind::Hole {
        expected_kind: None,
    });
    if let Some(root) = engine.structure.get_mut(engine.structure.root()) {
        root.set_kind(NodeKind::Sequence {
            children: vec![n_id, hole_id],
        });
    }

    let _result = engine
        .apply(&Action::FillHole {
            target: hole_id,
            fill: FillContent::Array {
                name: "A".to_owned(),
                element_type: VarType::Int,
                length: LengthSpec::RefVar(n_id),
            },
        })
        .unwrap();

    // The hole is now an Array
    assert!(matches!(
        engine.structure.get(hole_id).unwrap().kind(),
        NodeKind::Array { .. }
    ));
}

#[test]
fn add_constraint_range_success() {
    let mut engine = AstEngine::new();
    let n_id = engine.structure.add_node(NodeKind::Scalar {
        name: Ident::new("N"),
    });

    let result = engine
        .apply(&Action::AddConstraint {
            target: n_id,
            constraint: ConstraintDef {
                kind: ConstraintDefKind::Range {
                    lower: "1".to_owned(),
                    upper: "100".to_owned(),
                },
            },
        })
        .unwrap();

    assert_eq!(result.created_constraints.len(), 1);
    assert!(
        engine
            .constraints
            .get(result.created_constraints[0])
            .is_some()
    );
}

#[test]
fn add_constraint_to_hole_allowed() {
    // Rev.1 L-4: constraints can be pre-attached to holes
    let mut engine = AstEngine::new();
    let hole_id = engine.structure.add_node(NodeKind::Hole {
        expected_kind: None,
    });

    let result = engine
        .apply(&Action::AddConstraint {
            target: hole_id,
            constraint: ConstraintDef {
                kind: ConstraintDefKind::TypeDecl { typ: VarType::Int },
            },
        })
        .unwrap();

    assert_eq!(result.created_constraints.len(), 1);
}

#[test]
fn add_constraint_node_not_found_fails() {
    let mut engine = AstEngine::new();
    let result = engine.apply(&Action::AddConstraint {
        target: NodeId::from_raw(999),
        constraint: ConstraintDef {
            kind: ConstraintDefKind::Distinct,
        },
    });
    assert!(matches!(result, Err(OperationError::NodeNotFound { .. })));
}

#[test]
fn remove_constraint_success() {
    let mut engine = AstEngine::new();
    let n_id = engine.structure.add_node(NodeKind::Scalar {
        name: Ident::new("N"),
    });

    // First add a constraint
    let add_result = engine
        .apply(&Action::AddConstraint {
            target: n_id,
            constraint: ConstraintDef {
                kind: ConstraintDefKind::TypeDecl { typ: VarType::Int },
            },
        })
        .unwrap();

    let cid = add_result.created_constraints[0];

    // Now remove it
    let remove_result = engine
        .apply(&Action::RemoveConstraint { constraint_id: cid })
        .unwrap();
    assert!(remove_result.affected_constraints.contains(&cid));
    assert!(engine.constraints.get(cid).is_none());
}

#[test]
fn remove_constraint_not_found_fails() {
    let mut engine = AstEngine::new();
    let result = engine.apply(&Action::RemoveConstraint {
        constraint_id: ConstraintId::from_raw(999),
    });
    assert!(matches!(
        result,
        Err(OperationError::InvalidOperation { .. })
    ));
}

#[test]
fn replace_node_success() {
    let mut engine = AstEngine::new();
    let scalar_id = engine.structure.add_node(NodeKind::Scalar {
        name: Ident::new("N"),
    });

    let result = engine
        .apply(&Action::ReplaceNode {
            target: scalar_id,
            replacement: FillContent::Array {
                name: "A".to_owned(),
                element_type: VarType::Int,
                length: LengthSpec::Expr("N".to_owned()),
            },
        })
        .unwrap();

    assert!(matches!(
        engine.structure.get(scalar_id).unwrap().kind(),
        NodeKind::Array { .. }
    ));
    assert!(result.removed_nodes.is_empty()); // replace is in-place
}

#[test]
fn replace_node_keeps_compatible_range() {
    let mut engine = AstEngine::new();
    let scalar_id = engine.structure.add_node(NodeKind::Scalar {
        name: Ident::new("N"),
    });
    // Add a constraint to create a dependent
    engine
        .apply(&Action::AddConstraint {
            target: scalar_id,
            constraint: ConstraintDef {
                kind: ConstraintDefKind::Range {
                    lower: "1".to_owned(),
                    upper: "100".to_owned(),
                },
            },
        })
        .unwrap();

    let result = engine
        .apply(&Action::ReplaceNode {
            target: scalar_id,
            replacement: FillContent::Scalar {
                name: "M".to_owned(),
                typ: VarType::Int,
            },
        })
        .unwrap();

    assert!(result.affected_constraints.is_empty());
    assert_eq!(engine.constraints.for_node(scalar_id).len(), 2);
}

#[test]
fn replace_node_removes_incompatible_range() {
    let mut engine = AstEngine::new();
    let scalar_id = engine.structure.add_node(NodeKind::Scalar {
        name: Ident::new("N"),
    });
    engine
        .apply(&Action::AddConstraint {
            target: scalar_id,
            constraint: ConstraintDef {
                kind: ConstraintDefKind::Range {
                    lower: "1".to_owned(),
                    upper: "100".to_owned(),
                },
            },
        })
        .unwrap();

    let result = engine
        .apply(&Action::ReplaceNode {
            target: scalar_id,
            replacement: FillContent::Scalar {
                name: "c".to_owned(),
                typ: VarType::Char,
            },
        })
        .unwrap();

    assert_eq!(result.affected_constraints.len(), 1);
    let constraints = engine.constraints.for_node(scalar_id);
    assert_eq!(constraints.len(), 1);
    assert!(matches!(
        engine.constraints.get(constraints[0]),
        Some(Constraint::TypeDecl {
            expected: ExpectedType::Char,
            ..
        })
    ));
}

#[test]
fn add_slot_element_to_sequence() {
    let mut engine = AstEngine::new();
    let root = engine.structure.root();

    let result = engine
        .apply(&Action::AddSlotElement {
            parent: root,
            slot_name: "children".to_owned(),
            element: FillContent::Scalar {
                name: "N".to_owned(),
                typ: VarType::Int,
            },
        })
        .unwrap();

    assert_eq!(result.created_nodes.len(), 1);
    // Verify the new node is in root's children
    if let NodeKind::Sequence { children } = engine.structure.get(root).unwrap().kind() {
        assert!(children.contains(&result.created_nodes[0]));
    } else {
        panic!("Root should be Sequence");
    }
}

#[test]
fn remove_slot_element_from_sequence() {
    let mut engine = AstEngine::new();
    let root = engine.structure.root();

    // First add an element
    let add_result = engine
        .apply(&Action::AddSlotElement {
            parent: root,
            slot_name: "children".to_owned(),
            element: FillContent::Scalar {
                name: "N".to_owned(),
                typ: VarType::Int,
            },
        })
        .unwrap();

    let child_id = add_result.created_nodes[0];

    // Now remove it
    let remove_result = engine
        .apply(&Action::RemoveSlotElement {
            parent: root,
            slot_name: "children".to_owned(),
            child: child_id,
        })
        .unwrap();

    assert!(remove_result.removed_nodes.contains(&child_id));
    assert!(!engine.structure.contains(child_id));
}

#[test]
fn introduce_multi_test_case_success() {
    let mut engine = AstEngine::new();
    // Add some structure first
    let n_id = engine.structure.add_node(NodeKind::Scalar {
        name: Ident::new("N"),
    });
    if let Some(root) = engine.structure.get_mut(engine.structure.root()) {
        root.set_kind(NodeKind::Sequence {
            children: vec![n_id],
        });
    }

    let result = engine
        .apply(&Action::IntroduceMultiTestCase {
            count_var_name: "T".to_owned(),
            sum_bound: Some(SumBoundDef {
                bound_var: "N".to_owned(),
                upper: "200000".to_owned(),
            }),
        })
        .unwrap();

    // Should have created count var + repeat node
    assert!(result.created_nodes.len() >= 2);
    // Should have created SumBound constraint
    assert!(!result.created_constraints.is_empty());
}

#[test]
fn introduce_multi_test_case_already_exists_fails() {
    let mut engine = AstEngine::new();
    // Add a Repeat node manually (simulating existing multi-test-case)
    let repeat_id = engine.structure.add_node(NodeKind::Repeat {
        count: Expression::Var(Reference::Unresolved(Ident::new("T"))),
        index_var: None,
        body: vec![],
    });
    if let Some(root) = engine.structure.get_mut(engine.structure.root()) {
        root.set_kind(NodeKind::Sequence {
            children: vec![repeat_id],
        });
    }

    let result = engine.apply(&Action::IntroduceMultiTestCase {
        count_var_name: "T".to_owned(),
        sum_bound: None,
    });

    assert!(matches!(
        result,
        Err(OperationError::InvalidOperation { .. })
    ));
}

// ── Preview (dry-run) tests ──────────────────────────────────────────

#[test]
fn preview_fill_hole_shows_new_holes() {
    let mut engine = AstEngine::new();
    let root = engine.structure.root();

    // Root is a Sequence; add a Hole child so we can fill it
    let hole_id = engine.structure.add_node(NodeKind::Hole {
        expected_kind: None,
    });
    if let Some(root_node) = engine.structure.get_mut(root) {
        root_node.set_kind(NodeKind::Sequence {
            children: vec![hole_id],
        });
    }

    // Preview filling the hole with a Section (which creates a body hole)
    let action = Action::FillHole {
        target: hole_id,
        fill: FillContent::Section {
            label: "input".to_owned(),
        },
    };
    let preview = engine.preview(&action).unwrap();

    // Section fill creates exactly one body hole
    assert_eq!(preview.new_holes_created.len(), 1);

    // Original AST is still untouched — hole_id is still a Hole
    assert!(matches!(
        engine.structure.get(hole_id).unwrap().kind(),
        NodeKind::Hole { .. }
    ));
}

#[test]
fn preview_fill_hole_scalar_no_new_holes() {
    let mut engine = AstEngine::new();
    let root = engine.structure.root();

    let hole_id = engine.structure.add_node(NodeKind::Hole {
        expected_kind: None,
    });
    if let Some(root_node) = engine.structure.get_mut(root) {
        root_node.set_kind(NodeKind::Sequence {
            children: vec![hole_id],
        });
    }

    let action = Action::FillHole {
        target: hole_id,
        fill: FillContent::Scalar {
            name: "N".to_owned(),
            typ: VarType::Int,
        },
    };
    let preview = engine.preview(&action).unwrap();

    // Scalar fill creates no holes but does create a TypeDecl constraint
    assert!(preview.new_holes_created.is_empty());
    assert!(!preview.constraints_affected.is_empty());
}

#[test]
fn preview_invalid_action_returns_error() {
    let engine = AstEngine::new();

    // Trying to fill a non-existent node should fail just like apply
    let action = Action::FillHole {
        target: NodeId::from_raw(999),
        fill: FillContent::Scalar {
            name: "X".to_owned(),
            typ: VarType::Int,
        },
    };
    let result = engine.preview(&action);
    assert!(matches!(result, Err(OperationError::NodeNotFound { .. })));
}

#[test]
fn preview_does_not_mutate_ast() {
    let mut engine = AstEngine::new();
    let root = engine.structure.root();

    let hole_id = engine.structure.add_node(NodeKind::Hole {
        expected_kind: None,
    });
    if let Some(root_node) = engine.structure.get_mut(root) {
        root_node.set_kind(NodeKind::Sequence {
            children: vec![hole_id],
        });
    }

    // Snapshot state before preview
    let node_count_before = engine.structure.len();
    let constraint_count_before = engine.constraints.len();

    let action = Action::FillHole {
        target: hole_id,
        fill: FillContent::Section {
            label: "test".to_owned(),
        },
    };
    let _preview = engine.preview(&action).unwrap();

    // After preview: AST must be completely unchanged
    assert_eq!(engine.structure.len(), node_count_before);
    assert_eq!(engine.constraints.len(), constraint_count_before);
    assert!(matches!(
        engine.structure.get(hole_id).unwrap().kind(),
        NodeKind::Hole { .. }
    ));
}

#[test]
fn preview_add_constraint_shows_affected() {
    let engine = AstEngine::new();
    let root = engine.structure.root();

    let action = Action::AddConstraint {
        target: root,
        constraint: ConstraintDef {
            kind: ConstraintDefKind::Range {
                lower: "1".to_owned(),
                upper: "100".to_owned(),
            },
        },
    };
    let preview = engine.preview(&action).unwrap();

    assert!(preview.new_holes_created.is_empty());
    assert_eq!(preview.constraints_affected.len(), 1);

    // Engine is unchanged
    assert!(engine.constraints.is_empty());
}

#[test]
fn preview_remove_constraint_shows_affected() {
    let mut engine = AstEngine::new();
    let root = engine.structure.root();

    // First actually add a constraint
    let add_result = engine
        .apply(&Action::AddConstraint {
            target: root,
            constraint: ConstraintDef {
                kind: ConstraintDefKind::Range {
                    lower: "1".to_owned(),
                    upper: "100".to_owned(),
                },
            },
        })
        .unwrap();
    let cid = add_result.created_constraints[0];

    // Preview removing it
    let preview = engine
        .preview(&Action::RemoveConstraint { constraint_id: cid })
        .unwrap();

    assert!(preview.new_holes_created.is_empty());
    assert!(preview.constraints_affected.contains(&cid));

    // Constraint still exists in the real engine
    assert!(engine.constraints.get(cid).is_some());
}

#[test]
fn preview_introduce_multi_test_case() {
    let mut engine = AstEngine::new();
    let root = engine.structure.root();

    // Add some structure first
    let n_id = engine.structure.add_node(NodeKind::Scalar {
        name: Ident::new("N"),
    });
    if let Some(root_node) = engine.structure.get_mut(root) {
        root_node.set_kind(NodeKind::Sequence {
            children: vec![n_id],
        });
    }
    let node_count_before = engine.structure.len();

    let action = Action::IntroduceMultiTestCase {
        count_var_name: "T".to_owned(),
        sum_bound: Some(SumBoundDef {
            bound_var: "N".to_owned(),
            upper: "200000".to_owned(),
        }),
    };
    let preview = engine.preview(&action).unwrap();

    // No holes created by this action
    assert!(preview.new_holes_created.is_empty());
    // SumBound constraint would be created
    assert!(!preview.constraints_affected.is_empty());
    // Original engine unchanged
    assert_eq!(engine.structure.len(), node_count_before);
    assert!(engine.constraints.is_empty());
}