cadmpeg-ir 0.5.2

CAD data model, validation, diffing, and codec API for cadmpeg.
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
// SPDX-License-Identifier: Apache-2.0
//! Atomic staging for neutral entity transfer.

use std::collections::{BTreeMap, HashMap};
use std::hash::{Hash, Hasher};

use crate::annotations::{Annotations, ExactnessNote};
use crate::appearance::{Appearance, AppearanceBinding};
use crate::attributes::SourceAttribute;
use crate::document::{CadIr, Model};
use crate::drawings::Drawing;
use crate::features::{
    DesignConfiguration, DesignParameter, Feature, FeatureInputTopology, FeatureResultTopology,
};
use crate::geometry::{Curve, Pcurve, ProceduralCurve, ProceduralSurface, Surface};
use crate::index::ModelIndex;
use crate::presentation::{PresentationDocument, ViewPresentation};
use crate::products::{AssemblyJoint, Occurrence, ProductDefinition};
use crate::provenance::Exactness;
use crate::report::{LossNote, TransferLedger};
use crate::schema::{EntityKind, EntitySchema};
use crate::semantic_annotations::SemanticAnnotation;
use crate::sketches::{
    Sketch, SketchConstraint, SketchEntity, SpatialSketch, SpatialSketchConstraint,
    SpatialSketchEntity,
};
use crate::spreadsheets::Spreadsheet;
use crate::subd::SubdSurface;
use crate::tessellation::Tessellation;
use crate::topology::{Body, Coedge, Edge, Face, Loop, Point, Region, Shell, Vertex};

mod private {
    pub trait Sealed {}
}

/// Entity type owned by one registry-declared model arena.
pub trait ArenaEntity: private::Sealed + EntitySchema + Sized {
    /// Returns this entity type's arena.
    fn arena(model: &Model) -> &Vec<Self>;

    /// Returns this entity type's mutable arena.
    fn arena_mut(model: &mut Model) -> &mut Vec<Self>;
}

/// Registry-complete arena lengths captured at a model transaction boundary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelCheckpoint {
    lengths: [usize; EntityKind::ALL.len()],
}

impl ModelCheckpoint {
    /// Captures every neutral arena length.
    pub fn capture(model: &Model) -> Self {
        let mut lengths = Vec::with_capacity(EntityKind::ALL.len());
        macro_rules! capture_lengths {
            ($($field:ident: $ty:ty, $doc:literal, [$($attribute:meta),*];)*) => {
                $(lengths.push(model.$field.len());)*
            };
        }
        crate::document::arena_registry!(capture_lengths);
        Self {
            lengths: lengths
                .try_into()
                .expect("arena registry and EntityKind::ALL have equal length"),
        }
    }

    fn length<T: ArenaEntity>(&self) -> usize {
        let index = EntityKind::ALL
            .iter()
            .position(|kind| *kind == T::KIND)
            .expect("arena entity kind is registered");
        self.lengths[index]
    }

    /// Returns the captured length of one typed arena.
    pub fn arena_len<T: ArenaEntity>(&self) -> usize {
        self.length::<T>()
    }

    /// Returns entities of `T` added since this checkpoint.
    pub fn added<'a, T: ArenaEntity>(&self, model: &'a Model) -> Option<&'a [T]> {
        T::arena(model).get(self.length::<T>()..)
    }

    /// Returns mutable entities of `T` added since this checkpoint.
    pub fn added_mut<'a, T: ArenaEntity>(&self, model: &'a mut Model) -> Option<&'a mut [T]> {
        T::arena_mut(model).get_mut(self.length::<T>()..)
    }

    /// Counts all entities added since this checkpoint, rejecting arena shrinkage.
    pub fn added_count(&self, model: &Model) -> Option<usize> {
        let after = Self::capture(model);
        after
            .lengths
            .into_iter()
            .zip(self.lengths)
            .try_fold(0_usize, |total, (after, before)| {
                total.checked_add(after.checked_sub(before)?)
            })
    }
}

macro_rules! impl_arena_entities {
    ($($field:ident: $ty:ty, $doc:literal, [$($attribute:meta),*];)*) => {
        $(
            impl private::Sealed for $ty {}

            impl ArenaEntity for $ty {
                fn arena(model: &Model) -> &Vec<Self> {
                    &model.$field
                }

                fn arena_mut(model: &mut Model) -> &mut Vec<Self> {
                    &mut model.$field
                }
            }
        )*
    };
}

crate::document::arena_registry!(impl_arena_entities);

/// One neutral identity location. The arena owns the identity string; the
/// index stores only its stable slot, so transaction checks do not allocate a
/// second copy of every model identity.
#[derive(Debug, Clone, Copy)]
struct IdentitySlot {
    kind: EntityKind,
    index: usize,
}

type IdentityIndex = HashMap<u64, Vec<IdentitySlot>>;

fn identity_hash(identity: &str) -> u64 {
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    identity.hash(&mut hasher);
    hasher.finish()
}

fn identity_index_contains(model: &Model, index: &IdentityIndex, identity: &str) -> bool {
    index
        .get(&identity_hash(identity))
        .into_iter()
        .flatten()
        .any(|slot| model.identity_at(slot.kind, slot.index) == Some(identity))
}

fn index_model_identities(model: &Model) -> Result<IdentityIndex, DraftError> {
    let mut identity_index = IdentityIndex::new();
    macro_rules! index_arenas {
        ($($field:ident: $ty:ty, $doc:literal, [$($attribute:meta),*];)*) => {
            $(for (slot_index, entity) in model.$field.iter().enumerate() {
                if identity_index_contains(model, &identity_index, entity.identity()) {
                    return Err(DraftError::IdentityCollision(entity.identity().to_owned()));
                }
                identity_index
                    .entry(identity_hash(entity.identity()))
                    .or_default()
                    .push(IdentitySlot {
                        kind: <$ty as EntitySchema>::KIND,
                        index: slot_index,
                    });
            })*
        };
    }
    crate::document::arena_registry!(index_arenas);
    Ok(identity_index)
}

/// Error returned before an atomic draft commit mutates its destination.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum DraftError {
    /// An identity already exists in the base model or this draft.
    #[error("entity identity collision: {0}")]
    IdentityCollision(String),
    /// A staged typed reference cannot resolve after commit.
    #[error("staged entity {owner} has unresolved reference {target}")]
    UnresolvedReference {
        /// Staged entity holding the reference.
        owner: String,
        /// Missing target identity.
        target: String,
    },
    /// A B-rep assembly does not contain exactly one closed body graph.
    #[error("invalid B-rep assembly: {0}")]
    InvalidBrep(String),
}

/// Transactional collection of staged model entities and decode accounting.
///
/// Plan prose calls this stage `DocumentDraft`. Prefer that name in docs; this
/// type remains the runtime draft that commits into [`CadIr`].
#[derive(Debug, Default)]
pub struct ModelDraft {
    model: Model,
    identity_index: IdentityIndex,
    identities_synced: bool,
    exactness: BTreeMap<String, ExactnessNote>,
    notes: Vec<LossNote>,
    ledger: TransferLedger,
}

/// Plan name for [`ModelDraft`] in the
/// `DocumentDraft → CadIr → ValidationReport` state machine.
pub type DocumentDraft = ModelDraft;

impl ModelDraft {
    /// Creates an empty draft.
    pub fn new() -> Self {
        Self {
            identities_synced: true,
            ..Self::default()
        }
    }

    /// Inserts one entity, rejecting draft-local identity collisions immediately.
    pub fn insert<T: ArenaEntity>(&mut self, entity: T) -> Result<(), DraftError> {
        self.synchronize_identities()?;
        let identity = entity.identity();
        if identity_index_contains(&self.model, &self.identity_index, identity) {
            return Err(DraftError::IdentityCollision(identity.to_owned()));
        }
        let index = T::arena(&self.model).len();
        self.identity_index
            .entry(identity_hash(identity))
            .or_default()
            .push(IdentitySlot {
                kind: T::KIND,
                index,
            });
        T::arena_mut(&mut self.model).push(entity);
        Ok(())
    }

    /// Returns the staged arena for one entity type.
    pub fn arena<T: ArenaEntity>(&self) -> &Vec<T> {
        T::arena(&self.model)
    }

    /// Returns the mutable staged arena for one entity type.
    pub fn arena_mut<T: ArenaEntity>(&mut self) -> &mut Vec<T> {
        self.identities_synced = false;
        T::arena_mut(&mut self.model)
    }

    /// Returns the staged model for coordinated multi-arena construction.
    pub fn model(&self) -> &Model {
        &self.model
    }

    /// Returns the staged model for coordinated multi-arena construction.
    ///
    /// Commit still checks all identities and references, including entities inserted
    /// through this lower-level surface.
    pub fn model_mut(&mut self) -> &mut Model {
        self.identities_synced = false;
        &mut self.model
    }

    /// Records sparse exactness for a staged entity.
    pub fn exactness(&mut self, identity: impl Into<String>, exactness: Exactness) {
        let identity = identity.into();
        if exactness == Exactness::ByteExact {
            self.exactness.remove(&identity);
        } else {
            self.exactness.insert(
                identity,
                ExactnessNote {
                    entity: exactness,
                    fields: BTreeMap::new(),
                },
            );
        }
    }

    /// Retains exactness notes selected by identity.
    pub fn retain_exactness(&mut self, mut keep: impl FnMut(&str) -> bool) {
        self.exactness.retain(|identity, _| keep(identity));
    }

    /// Adds a staged loss note.
    pub fn note(&mut self, note: LossNote) {
        self.notes.push(note);
    }

    /// Returns the mutable staged transfer ledger.
    pub fn ledger_mut(&mut self) -> &mut TransferLedger {
        &mut self.ledger
    }

    /// Number of staged entities.
    pub fn entity_count(&self) -> usize {
        self.model.entity_count()
    }

    /// Commits only staged model entities, discarding empty ancillary staging surfaces.
    pub fn commit_model(self, base: &mut CadIr) -> Result<(), DraftError> {
        self.commit(
            base,
            &mut Annotations::default(),
            &mut Vec::new(),
            &mut TransferLedger::default(),
        )
    }

    fn validate_against(&mut self, base: &CadIr) -> Result<(), DraftError> {
        let index = ModelIndex::new(base);
        self.validate_with_contains(|identity| index.contains(identity))
    }

    fn synchronize_identities(&mut self) -> Result<(), DraftError> {
        if self.identities_synced {
            return Ok(());
        }
        self.identity_index = index_model_identities(&self.model)?;
        self.identities_synced = true;
        Ok(())
    }

    /// Validates staged identities and references against one identity universe.
    ///
    /// The caller supplies the identities already committed outside this draft;
    /// references may also target another entity staged in the same draft. The
    /// index populated by `insert` is reused. Direct mutable staging invalidates
    /// that index and causes one complete rebuild before validation.
    fn validate_with_contains(
        &mut self,
        contains: impl Fn(&str) -> bool,
    ) -> Result<(), DraftError> {
        self.synchronize_identities()?;
        macro_rules! check_external_identities {
            ($($field:ident: $ty:ty, $doc:literal, [$($attribute:meta),*];)*) => {
                $(for entity in &self.model.$field {
                    if contains(entity.identity()) {
                        return Err(DraftError::IdentityCollision(entity.identity().to_owned()));
                    }
                })*
            };
        }
        crate::document::arena_registry!(check_external_identities);
        macro_rules! validate_arenas {
            ($($field:ident: $ty:ty, $doc:literal, [$($attribute:meta),*];)*) => {
                $(for entity in &self.model.$field {
                    let owner = entity.identity();
                    let mut missing = None;
                    entity.visit_references(&mut |reference| {
                        if missing.is_none()
                            && !contains(&reference.target)
                            && !identity_index_contains(
                                &self.model,
                                &self.identity_index,
                                &reference.target,
                            )
                        {
                            missing = Some(reference.target);
                        }
                    });
                    if let Some(target) = missing {
                        return Err(DraftError::UnresolvedReference {
                            owner: owner.to_owned(),
                            target,
                        });
                    }
                })*
            };
        }
        crate::document::arena_registry!(validate_arenas);
        Ok(())
    }

    /// Validates and atomically extends a document, annotations, notes, and ledger.
    pub fn commit(
        mut self,
        base: &mut CadIr,
        annotations: &mut Annotations,
        notes: &mut Vec<LossNote>,
        ledger: &mut TransferLedger,
    ) -> Result<(), DraftError> {
        self.validate_against(base)?;
        self.commit_validated(base, annotations, notes, ledger);
        Ok(())
    }

    /// Keeps selected staged entities, then validates and commits the resulting salvage graph.
    pub fn commit_incomplete(
        mut self,
        base: &mut CadIr,
        annotations: &mut Annotations,
        notes: &mut Vec<LossNote>,
        ledger: &mut TransferLedger,
        mut keep: impl FnMut(EntityKind, &str) -> bool,
    ) -> Result<(), DraftError> {
        macro_rules! retain_arenas {
            ($($field:ident: $ty:ty, $doc:literal, [$($attribute:meta),*];)*) => {
                $(self.model.$field.retain(|entity| keep(<$ty>::KIND, entity.identity()));)*
            };
        }
        crate::document::arena_registry!(retain_arenas);
        let identity_index = index_model_identities(&self.model)?;
        self.exactness
            .retain(|identity, _| identity_index_contains(&self.model, &identity_index, identity));
        self.identity_index = identity_index;
        self.identities_synced = true;
        self.commit(base, annotations, notes, ledger)
    }

    fn commit_validated(
        self,
        base: &mut CadIr,
        annotations: &mut Annotations,
        notes: &mut Vec<LossNote>,
        ledger: &mut TransferLedger,
    ) {
        let Self {
            mut model,
            identity_index: _,
            identities_synced: _,
            exactness,
            notes: staged_notes,
            ledger: staged_ledger,
        } = self;
        macro_rules! extend_arenas {
            ($($field:ident: $ty:ty, $doc:literal, [$($attribute:meta),*];)*) => {
                $(base.model.$field.append(&mut model.$field);)*
            };
        }
        crate::document::arena_registry!(extend_arenas);
        annotations.exactness.extend(exactness);
        notes.extend(staged_notes);
        ledger.entries.extend(staged_ledger.entries);
    }
}

#[derive(Debug)]
enum CommittedIdentity {
    Neutral(IdentitySlot),
    Native(String),
}

type CommittedIdentityIndex = HashMap<u64, Vec<CommittedIdentity>>;

/// Identity universe for one decode-scoped sequence of draft commits.
///
/// A session is valid only while the associated [`CadIr`] is mutated through
/// this session. Inserting a neutral or native record directly into `base`
/// after construction makes the session stale; topology decoding observes this
/// invariant by routing every topology draft through one session and performing
/// no other record insertion during that phase.
#[derive(Debug)]
pub struct CommitSession {
    identities: CommittedIdentityIndex,
}

impl CommitSession {
    /// Builds an identity index over every neutral and native arena.
    pub fn new(base: &CadIr) -> Self {
        let mut identities = CommittedIdentityIndex::new();
        macro_rules! collect_model_identities {
            ($($field:ident: $ty:ty, $doc:literal, [$($attribute:meta),*];)*) => {
                $(for (index, entity) in base.model.$field.iter().enumerate() {
                    Self::insert_neutral(
                        &mut identities,
                        entity.identity(),
                        IdentitySlot {
                            kind: <$ty as EntitySchema>::KIND,
                            index,
                        },
                    );
                })*
            };
        }
        crate::document::arena_registry!(collect_model_identities);
        for record in base
            .native
            .0
            .values()
            .flat_map(|namespace| namespace.arenas.values().flatten())
        {
            identities
                .entry(identity_hash(record.id()))
                .or_default()
                .push(CommittedIdentity::Native(record.id().to_owned()));
        }
        Self { identities }
    }

    fn insert_neutral(identities: &mut CommittedIdentityIndex, identity: &str, slot: IdentitySlot) {
        identities
            .entry(identity_hash(identity))
            .or_default()
            .push(CommittedIdentity::Neutral(slot));
    }

    /// Reports whether `identity` is already owned by `base` or a
    /// prior successful commit in this session.
    ///
    /// Identity ownership is kind-blind: this checks all neutral and native
    /// arenas, not whether a record exists in one particular arena.
    pub fn contains(&self, base: &CadIr, identity: &str) -> bool {
        self.identities
            .get(&identity_hash(identity))
            .into_iter()
            .flatten()
            .any(|owner| match owner {
                CommittedIdentity::Neutral(slot) => {
                    base.model.identity_at(slot.kind, slot.index) == Some(identity)
                }
                CommittedIdentity::Native(candidate) => candidate == identity,
            })
    }

    fn register_added(&mut self, model: &Model, checkpoint: &ModelCheckpoint) {
        macro_rules! register_arenas {
            ($($field:ident: $ty:ty, $doc:literal, [$($attribute:meta),*];)*) => {
                $(if let Some(added) = checkpoint.added::<$ty>(model) {
                    for (offset, entity) in added.iter().enumerate() {
                        Self::insert_neutral(
                            &mut self.identities,
                            entity.identity(),
                            IdentitySlot {
                                kind: <$ty as EntitySchema>::KIND,
                                index: checkpoint.arena_len::<$ty>() + offset,
                            },
                        );
                    }
                })*
            };
        }
        crate::document::arena_registry!(register_arenas);
    }

    /// Validates and commits one model draft into `base`.
    ///
    /// Validation is completed before either destination is changed. On
    /// success, the draft's owned identities are moved into this session. A
    /// rejected draft therefore leaves both the document and the session
    /// unchanged and does not poison a later commit.
    pub fn commit_model(
        &mut self,
        mut draft: ModelDraft,
        base: &mut CadIr,
    ) -> Result<(), DraftError> {
        draft.validate_with_contains(|identity| self.contains(base, identity))?;
        let checkpoint = ModelCheckpoint::capture(&base.model);
        draft.commit_validated(
            base,
            &mut Annotations::default(),
            &mut Vec::new(),
            &mut TransferLedger::default(),
        );
        self.register_added(&base.model, &checkpoint);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::{CommitSession, DraftError, ModelDraft};
    use crate::annotations::Annotations;
    use crate::document::CadIr;
    use crate::ids::PointId;
    use crate::math::Point3;
    use crate::native::NativeRecord;
    use crate::report::TransferLedger;
    use crate::topology::{Point, Vertex};
    use crate::units::Units;

    fn point(id: &str) -> Point {
        Point {
            id: PointId(id.into()),
            position: Point3::new(0.0, 0.0, 0.0),
            source_object: None,
        }
    }

    fn point_draft(id: &str) -> ModelDraft {
        let mut draft = ModelDraft::new();
        draft.insert(point(id)).expect("insert point into draft");
        draft
    }

    fn vertex_draft(id: &str, point: &str) -> ModelDraft {
        let mut draft = ModelDraft::new();
        draft
            .insert(Vertex {
                id: id.into(),
                point: point.into(),
                tolerance: None,
            })
            .expect("insert vertex into draft");
        draft
    }

    #[test]
    fn collision_refuses_without_mutating_any_destination() {
        let mut ir = CadIr::empty(Units::default());
        ir.model.points.push(point("test:model:point#1"));
        let mut draft = ModelDraft::new();
        draft
            .insert(point("test:model:point#1"))
            .expect("insert point into empty draft");
        let mut annotations = Annotations::default();
        let mut notes = Vec::new();
        let mut ledger = TransferLedger::default();

        assert!(matches!(
            draft.commit(&mut ir, &mut annotations, &mut notes, &mut ledger),
            Err(DraftError::IdentityCollision(_))
        ));
        assert_eq!(ir.model.points.len(), 1);
        assert!(annotations.exactness.is_empty());
        assert!(notes.is_empty());
        assert!(ledger.is_empty());
    }

    #[test]
    fn direct_model_staging_rebuilds_identity_cache_before_commit() {
        let identity = "test:model:point#direct-duplicate";
        let mut draft = ModelDraft::new();
        draft.model_mut().points.push(point(identity));
        draft.model_mut().points.push(point(identity));
        let mut ir = CadIr::empty(Units::default());

        assert_eq!(
            draft.commit_model(&mut ir),
            Err(DraftError::IdentityCollision(identity.into()))
        );
        assert!(ir.model.points.is_empty());
    }

    #[test]
    fn direct_model_staging_revalidates_references_before_commit() {
        let owner = "test:model:vertex#direct-missing";
        let target = "test:model:point#direct-missing";
        let mut draft = ModelDraft::new();
        draft.model_mut().vertices.push(Vertex {
            id: owner.into(),
            point: target.into(),
            tolerance: None,
        });
        let mut ir = CadIr::empty(Units::default());

        assert_eq!(
            draft.commit_model(&mut ir),
            Err(DraftError::UnresolvedReference {
                owner: owner.into(),
                target: target.into(),
            })
        );
        assert!(ir.model.vertices.is_empty());
    }

    #[test]
    fn incomplete_commit_rechecks_duplicate_identities() {
        let identity = "test:model:point#incomplete-duplicate";
        let mut draft = ModelDraft::new();
        draft.model_mut().points.push(point(identity));
        draft.model_mut().points.push(point(identity));
        let mut ir = CadIr::empty(Units::default());

        assert_eq!(
            draft.commit_incomplete(
                &mut ir,
                &mut Annotations::default(),
                &mut Vec::new(),
                &mut TransferLedger::default(),
                |_, _| true,
            ),
            Err(DraftError::IdentityCollision(identity.into()))
        );
        assert!(ir.model.points.is_empty());
    }

    #[test]
    fn commit_session_matches_sequential_model_commits() {
        let mut session_ir = CadIr::empty(Units::default());
        let mut session = CommitSession::new(&session_ir);
        session
            .commit_model(point_draft("test:model:point#1"), &mut session_ir)
            .expect("first session commit");
        session
            .commit_model(point_draft("test:model:point#2"), &mut session_ir)
            .expect("second session commit");

        let mut sequential_ir = CadIr::empty(Units::default());
        point_draft("test:model:point#1")
            .commit_model(&mut sequential_ir)
            .expect("first sequential commit");
        point_draft("test:model:point#2")
            .commit_model(&mut sequential_ir)
            .expect("second sequential commit");

        assert_eq!(session_ir, sequential_ir);
    }

    #[test]
    fn commit_session_rejects_cross_draft_identity_collision() {
        let mut ir = CadIr::empty(Units::default());
        let mut session = CommitSession::new(&ir);
        let identity = "test:model:point#cross-draft";
        session
            .commit_model(point_draft(identity), &mut ir)
            .expect("first session commit");

        assert_eq!(
            session.commit_model(point_draft(identity), &mut ir),
            Err(DraftError::IdentityCollision(identity.into()))
        );
        assert_eq!(ir.model.points.len(), 1);
    }

    #[test]
    fn commit_session_rejects_pre_existing_neutral_identity() {
        let identity = "test:model:point#existing";
        let mut ir = CadIr::empty(Units::default());
        ir.model.points.push(point(identity));
        let mut session = CommitSession::new(&ir);

        assert_eq!(
            session.commit_model(point_draft(identity), &mut ir),
            Err(DraftError::IdentityCollision(identity.into()))
        );
        assert_eq!(ir.model.points.len(), 1);
    }

    #[test]
    fn commit_session_rejects_native_identity() {
        let identity = "test:native:record#1";
        let mut ir = CadIr::empty(Units::default());
        ir.native.namespace_mut("test").arenas.insert(
            "records".into(),
            vec![NativeRecord::new(identity, serde_json::Map::new())],
        );
        let mut session = CommitSession::new(&ir);

        assert_eq!(
            session.commit_model(point_draft(identity), &mut ir),
            Err(DraftError::IdentityCollision(identity.into()))
        );
        assert!(ir.model.points.is_empty());
    }

    #[test]
    fn commit_session_rejects_unresolved_reference() {
        let owner = "test:model:vertex#missing";
        let target = "test:model:point#missing";
        let mut ir = CadIr::empty(Units::default());
        let mut session = CommitSession::new(&ir);

        assert_eq!(
            session.commit_model(vertex_draft(owner, target), &mut ir),
            Err(DraftError::UnresolvedReference {
                owner: owner.into(),
                target: target.into(),
            })
        );
        assert!(ir.model.vertices.is_empty());
    }

    #[test]
    fn commit_session_resolves_reference_into_earlier_draft() {
        let point_id = "test:model:point#earlier";
        let mut ir = CadIr::empty(Units::default());
        let mut session = CommitSession::new(&ir);
        session
            .commit_model(point_draft(point_id), &mut ir)
            .expect("point commit");
        session
            .commit_model(vertex_draft("test:model:vertex#later", point_id), &mut ir)
            .expect("reference into committed draft resolves");

        assert_eq!(ir.model.points.len(), 1);
        assert_eq!(ir.model.vertices.len(), 1);
    }

    #[test]
    fn rejected_session_commit_leaves_session_and_base_usable() {
        let rejected_identity = "test:model:vertex#rejected";
        let mut ir = CadIr::empty(Units::default());
        let before = ir.clone();
        let mut session = CommitSession::new(&ir);

        assert!(session
            .commit_model(
                vertex_draft(rejected_identity, "test:model:point#never-committed"),
                &mut ir,
            )
            .is_err());
        assert_eq!(ir, before);

        session
            .commit_model(point_draft(rejected_identity), &mut ir)
            .expect("rejected identity was not absorbed into the session");
        assert_eq!(ir.model.points.len(), 1);
    }

    #[test]
    fn commit_session_contains_tracks_only_successful_commits() {
        let committed_identity = "test:model:point#committed";
        let rejected_identity = "test:model:point#rejected";
        let mut ir = CadIr::empty(Units::default());
        let mut session = CommitSession::new(&ir);

        assert!(!session.contains(&ir, committed_identity));
        session
            .commit_model(point_draft(committed_identity), &mut ir)
            .expect("point commit");
        assert!(session.contains(&ir, committed_identity));

        let mut rejected = ModelDraft::new();
        rejected
            .insert(Vertex {
                id: rejected_identity.into(),
                point: "test:model:point#missing".into(),
                tolerance: None,
            })
            .expect("insert rejected vertex");
        assert!(!session.contains(&ir, rejected_identity));
        assert!(session.commit_model(rejected, &mut ir).is_err());
        assert!(!session.contains(&ir, rejected_identity));
    }
}