triblespace-core 0.47.1

The triblespace core implementation.
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
//! Native storage for collection-calculus records.
//!
//! A collection store is a grow-only set of canonical records: a record's
//! exact bytes are its identity, and inserting one the store already holds
//! adds nothing. It deliberately exposes no mutable head, deletion, compare-and-swap, or
//! read-through-writer path. A [`CollectionRead`] implementation belongs to an
//! immutable store snapshot, while [`CollectionStore`] only admits new records.

use std::collections::BTreeSet;
use std::error::Error;
use std::fmt::Debug;

use super::coverage::{Coverage, CoverageIndex};
use super::{CollectionData, CollectionHandle, CollectionRecord};

/// One raw selection route into the grow-only collection-record set.
///
/// A batch of selectors is interpreted as set union. Exact operation lookup
/// deliberately names only the inputs: every distinct asserted result remains
/// visible to callers as conflicting evidence. Selection does not establish
/// authority, output residency, or the validity of an input witness closure.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum CollectionRecordSelector {
    /// Select every record whose intrinsic collection is exactly `C`.
    ///
    /// This is the canonical construction route for one collection's sparse
    /// overlay inventory. It includes signed `COMMIT`, `MERGE`, `DERIVE`
    /// and `MAP` records without exposing or enumerating unrelated
    /// collections.
    Collection(CollectionHandle),
    /// Select every signed membership claim for one exact collection element.
    ///
    /// Several authors or metadata archives may attest the same data member;
    /// all of those claims remain provenance over one payload identity.
    CommitMember(CollectionHandle, CollectionData),
    /// Select every raw record producing one exact collection member.
    ///
    /// This matches `COMMIT.data`, `MERGE.result`, `DERIVE.output` and
    /// `MAP.attachment` at exactly `(C, H)`. Distinct producers and input
    /// witnesses remain distinct records; neither authority nor witness
    /// closure is evaluated here.
    ProducedMember(CollectionHandle, CollectionData),
    /// Select every `MERGE` asserted for one collection descriptor.
    MergeCollection(CollectionHandle),
    /// Select every `DERIVE` into one exact target descriptor.
    ///
    /// The source is not part of the selector: a target has one source, named
    /// by its descriptor, so selecting the target selects the mapping.
    DeriveTarget(CollectionHandle),
    /// Select the records that replicate for `C`: its `COMMIT`s and
    /// `DERIVE`s.
    ///
    /// A collection is a set of foundations. A `MERGE` is the signing host's
    /// own lattice node, never replicated, so it never matches. This is the
    /// selection sync exports and hydrates.
    Foundations(CollectionHandle),
}

pub(crate) fn selectors_match_record(
    selectors: &BTreeSet<CollectionRecordSelector>,
    record: CollectionRecord,
) -> bool {
    let collection = record.collection();
    if selectors.contains(&CollectionRecordSelector::Collection(collection)) {
        return true;
    }
    // Named positively: a record kind added later replicates only once it
    // is listed here.
    if matches!(
        record,
        CollectionRecord::Commit(_) | CollectionRecord::Derive(_)
    ) && selectors.contains(&CollectionRecordSelector::Foundations(collection))
    {
        return true;
    }
    let output = match record {
        CollectionRecord::Commit(commit) => commit.data(),
        CollectionRecord::Merge(merge) => merge.result(),
        CollectionRecord::Derive(derive) => derive.output(),
        CollectionRecord::Map(map) => map.attachment(),
    };
    if selectors.contains(&CollectionRecordSelector::ProducedMember(
        collection, output,
    )) {
        return true;
    }
    let matches_fields = match record {
        CollectionRecord::Commit(commit) => selectors.contains(
            &CollectionRecordSelector::CommitMember(commit.collection(), commit.data()),
        ),
        CollectionRecord::Merge(merge) => selectors.contains(
            &CollectionRecordSelector::MergeCollection(merge.collection()),
        ),
        CollectionRecord::Derive(derive) => {
            selectors.contains(&CollectionRecordSelector::DeriveTarget(derive.collection()))
        }
        // A MAP is selected by its collection or by its attachment only.
        CollectionRecord::Map(_) => false,
    };
    matches_fields
}

/// Immutable read surface for canonical collection-calculus records.
///
/// Implementations enumerate one coherent store snapshot in a deterministic
/// order of their own. Mutation lives on [`CollectionStore`], so admission and
/// physical-cover resolution cannot accidentally observe different prefixes.
/// Signatures are trusted here: foreign dense records are checked at ingress,
/// and local constructors sign their own records. Opening or concatenating a
/// raw pile is an explicit trust decision; audit unfamiliar piles before using
/// them as trusted storage. This contract does not grant WRITE authority,
/// which is evaluated against each snapshot's policy and proofs. Equation
/// witnesses are references to other records, not ingress prerequisites:
/// a record may be persisted before its witnesses or WRITE proofs arrive.
pub trait CollectionRead {
    /// Failure while enumerating stored records.
    type RecordsError: Error + Debug + Send + Sync + 'static;
    /// Borrowing iterator over one deterministic view of known records.
    type RecordIter<'a>: Iterator<Item = Result<CollectionRecord, Self::RecordsError>>
    where
        Self: 'a;

    /// Enumerate currently known records, each once, in a deterministic
    /// order.
    fn records<'a>(&'a self) -> Result<Self::RecordIter<'a>, Self::RecordsError>;

    /// Every collection at least one known record names, each once, in
    /// ascending handle order.
    ///
    /// A store that indexes its records by collection answers from that
    /// index's first segment; nothing enumerates records to find out. There
    /// is no default: a reader that could only walk should say so in its own
    /// implementation, where the cost is visible.
    fn collections(&self) -> Result<Vec<CollectionHandle>, Self::RecordsError>;

    /// Select one deterministic union of raw record routes.
    ///
    /// The default implementation performs exactly one ordinary enumeration
    /// and filters it. Backends with primary or secondary indexes may override
    /// this method without changing the grow-only set contract. Each selected
    /// record is returned once, in the backend's deterministic order. An empty
    /// union returns immediately without asking the backend for a view.
    fn select_records(
        &self,
        selectors: &BTreeSet<CollectionRecordSelector>,
    ) -> Result<Vec<CollectionRecord>, Self::RecordsError> {
        if selectors.is_empty() {
            return Ok(Vec::new());
        }
        let records = self.records()?;
        let mut selected = Vec::new();
        for record in records {
            let record = record?;
            if selectors_match_record(selectors, record) {
                selected.push(record);
            }
        }
        Ok(selected)
    }

    /// Canonical records added and removed between two immutable observations
    /// of exactly these routes, returned as `(added, removed)`.
    ///
    /// This is a semantic set difference, not a difference of physical frame
    /// locations. Removing one duplicate occurrence must not remove a record
    /// still present in the current view. The snapshots need not be ordered,
    /// related, or grow-only relative to each other. Indexed backends override
    /// this selected-read boundary with persistent native index differences;
    /// the fallback explicitly selects both sides and compares their records.
    fn select_record_changes(
        &self,
        previous: &Self,
        selectors: &BTreeSet<CollectionRecordSelector>,
    ) -> Result<(Vec<CollectionRecord>, Vec<CollectionRecord>), Self::RecordsError> {
        selected_record_changes(self, previous, selectors)
    }
}

pub(crate) fn selected_record_changes<R: CollectionRead + ?Sized>(
    current: &R,
    previous: &R,
    selectors: &BTreeSet<CollectionRecordSelector>,
) -> Result<(Vec<CollectionRecord>, Vec<CollectionRecord>), R::RecordsError> {
    if selectors.is_empty() {
        return Ok((Vec::new(), Vec::new()));
    }
    let current: BTreeSet<_> = current.select_records(selectors)?.into_iter().collect();
    let previous: BTreeSet<_> = previous.select_records(selectors)?.into_iter().collect();
    Ok((
        current.difference(&previous).copied().collect(),
        previous.difference(&current).copied().collect(),
    ))
}

/// Downward coverage for every lattice node this store has admitted.
///
/// Deliberately not a method of [`CollectionRead`], and deliberately without
/// a default. A store that maintains the index -- a pile during replay, a
/// [`crate::repo::memoryrepo::MemoryRepo`] across inserts -- hands out its
/// published root, a constant-time clone. A wrapper delegates. A reader that
/// can do neither says so by not implementing this, and then cannot be passed
/// where coverage is needed: a compile error, where the alternative was a
/// default that refolded every record with an admission query for each one
/// the store did not admit -- found doing exactly that, silently, in four
/// wrappers that had simply never overridden it.
///
/// `lineage` names every collection whose rows the caller may consult -- the
/// target and each of its sources down to the foundation. The index itself
/// is snapshot-wide and no implementor needs to narrow what it hands back,
/// but a reader that TRACKS what was read does need it: the fold only
/// propagates along a lineage, so a row under one can change only when a
/// record arrives in one of these collections. Without the parameter the
/// honest read set of a coverage read is every record in the store, which
/// makes every observation wake on every arrival.
pub trait CoverageRead: CollectionRead {
    /// The settled index itself, a constant-time clone.
    ///
    /// This is what a reader that composes on top of another needs: an
    /// operation frontier clones its control's index, parks the records it
    /// authored, and settles -- the same fold the control ran, continued for
    /// a few records, instead of restarted for all of them.
    fn index(
        &self,
        lineage: &BTreeSet<CollectionHandle>,
    ) -> Result<CoverageIndex, Self::RecordsError>;

    /// The published half of [`Self::index`]: what a reader that only asks
    /// questions needs. Exact and constant-time by construction, so it may be
    /// provided; the trait's refusal is of defaults that would refold.
    fn coverage(
        &self,
        lineage: &BTreeSet<CollectionHandle>,
    ) -> Result<Coverage, Self::RecordsError> {
        Ok(self.index(lineage)?.published().clone())
    }
}

impl<R: CoverageRead> CoverageRead for &R {
    fn index(
        &self,
        lineage: &BTreeSet<CollectionHandle>,
    ) -> Result<CoverageIndex, Self::RecordsError> {
        (**self).index(lineage)
    }

    fn coverage(
        &self,
        lineage: &BTreeSet<CollectionHandle>,
    ) -> Result<Coverage, Self::RecordsError> {
        (**self).coverage(lineage)
    }
}

impl<R> CollectionRead for &R
where
    R: CollectionRead + ?Sized,
{
    type RecordsError = R::RecordsError;
    type RecordIter<'a>
        = R::RecordIter<'a>
    where
        Self: 'a;

    fn records<'a>(&'a self) -> Result<Self::RecordIter<'a>, Self::RecordsError> {
        (**self).records()
    }

    fn collections(&self) -> Result<Vec<CollectionHandle>, Self::RecordsError> {
        (**self).collections()
    }

    fn select_records(
        &self,
        selectors: &BTreeSet<CollectionRecordSelector>,
    ) -> Result<Vec<CollectionRecord>, Self::RecordsError> {
        (**self).select_records(selectors)
    }

    fn select_record_changes(
        &self,
        previous: &Self,
        selectors: &BTreeSet<CollectionRecordSelector>,
    ) -> Result<(Vec<CollectionRecord>, Vec<CollectionRecord>), Self::RecordsError> {
        (**self).select_record_changes(*previous, selectors)
    }
}

/// The distinct collections a set of records names, in ascending handle
/// order: what [`CollectionRead::collections`] answers for a reader that holds
/// its records in memory rather than under an index.
pub fn distinct_collections(
    records: impl IntoIterator<Item = CollectionRecord>,
) -> Vec<CollectionHandle> {
    records
        .into_iter()
        .map(|record| record.collection())
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect()
}

/// Grow-only write surface for canonical collection-calculus records.
///
/// Inserting the same canonical record more than once is an idempotent
/// success. Records are never replaced through this interface. Read access is
/// deliberately obtained from the store's immutable snapshot instead.
pub trait CollectionStore {
    /// Failure while admitting one canonical record.
    type InsertError: Error + Debug + Send + Sync + 'static;

    /// Insert one canonical record.
    ///
    /// Re-inserting a record with the same canonical bytes is success and does not
    /// add another logical set member.
    ///
    /// This is the trusted typed boundary, not a foreign-byte decoder. The
    /// public dense record decoder verifies signatures before returning a
    /// value; local signing constructs one directly. Backends and transparent
    /// adapters must not repeat that cryptographic work. No capability proof
    /// is required for insertion: a signed record can arrive before the proof
    /// that later authorizes its producer.
    fn insert(&mut self, record: CollectionRecord) -> Result<(), Self::InsertError>;
}

impl<S> CollectionStore for &mut S
where
    S: CollectionStore + ?Sized,
{
    type InsertError = S::InsertError;

    fn insert(&mut self, record: CollectionRecord) -> Result<(), Self::InsertError> {
        (**self).insert(record)
    }
}

#[cfg(test)]
mod tests {
    // Canonical but deliberately uninserted COMMIT witnesses keep these
    // physical-storage fixtures independent of ancestor arrival order.
    /// A merge or derive names its input PAYLOAD. This used to wrap it
    /// beside a fingerprint citing a record that produced it; nothing
    /// cites anything now, so it is just the payload.
    use std::cell::Cell;
    use std::convert::Infallible;

    use ed25519_dalek::SigningKey;

    use super::*;
    use crate::collection::{
        empty_metadata_handle, CollectionCommit, CollectionData, CollectionDerive, CollectionMerge,
    };
    use crate::inline::Inline;

    fn collection(byte: u8) -> CollectionHandle {
        Inline::new([byte; 32])
    }

    fn data(byte: u8) -> CollectionData {
        Inline::new([byte; 32])
    }

    fn fixture() -> Vec<CollectionRecord> {
        let source = collection(1);
        let target = collection(2);
        let other = collection(3);
        let input = data(10);
        let mut records = vec![
            CollectionRecord::Commit(CollectionCommit::sign(
                &SigningKey::from_bytes(&[7; 32]),
                source,
                data(4),
                empty_metadata_handle(),
            )),
            CollectionRecord::Merge(
                CollectionMerge::sign(
                    &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                    source,
                    [data(4), data(5)],
                    data(6),
                )
                .unwrap(),
            ),
            CollectionRecord::Merge(
                CollectionMerge::sign(
                    &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                    other,
                    [data(4), data(5)],
                    data(7),
                )
                .unwrap(),
            ),
            CollectionRecord::Derive(CollectionDerive::sign(
                &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                target,
                crate::collection::SourceLocator::of(input.raw),
                data(11),
            )),
            CollectionRecord::Derive(CollectionDerive::sign(
                &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                target,
                crate::collection::SourceLocator::of(input.raw),
                data(12),
            )),
            CollectionRecord::Derive(CollectionDerive::sign(
                &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                target,
                crate::collection::SourceLocator::of(data(13).raw),
                data(14),
            )),
            CollectionRecord::Derive(CollectionDerive::sign(
                &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                other,
                crate::collection::SourceLocator::of(input.raw),
                data(15),
            )),
        ];
        records.sort_unstable_by_key(CollectionRecord::fingerprint);
        records
    }

    #[derive(Default)]
    struct FallbackStore {
        records: Vec<CollectionRecord>,
        enumerations: Cell<usize>,
    }

    impl CollectionRead for FallbackStore {
        type RecordsError = Infallible;
        type RecordIter<'a> = std::vec::IntoIter<Result<CollectionRecord, Infallible>>;

        fn records<'a>(&'a self) -> Result<Self::RecordIter<'a>, Self::RecordsError> {
            self.enumerations.set(self.enumerations.get() + 1);
            Ok(self
                .records
                .iter()
                .copied()
                .map(Ok)
                .collect::<Vec<_>>()
                .into_iter())
        }

        fn collections(&self) -> Result<Vec<CollectionHandle>, Self::RecordsError> {
            Ok(distinct_collections(self.records.iter().copied()))
        }
    }

    impl CollectionStore for FallbackStore {
        type InsertError = Infallible;

        fn insert(&mut self, record: CollectionRecord) -> Result<(), Self::InsertError> {
            self.records.push(record);
            self.records
                .sort_unstable_by_key(CollectionRecord::fingerprint);
            self.records.dedup_by_key(|record| record.fingerprint());
            Ok(())
        }
    }

    #[test]
    fn field_selectors_do_not_recompute_record_fingerprints() {
        use crate::collection::records::FINGERPRINT_CALLS;

        let records = fixture();
        let selectors = [
            CollectionRecordSelector::Collection(collection(1)),
            CollectionRecordSelector::Collection(collection(99)),
            CollectionRecordSelector::CommitMember(collection(1), data(4)),
            CollectionRecordSelector::ProducedMember(collection(2), data(11)),
            CollectionRecordSelector::MergeCollection(collection(1)),
            CollectionRecordSelector::DeriveTarget(collection(2)),
        ];
        let before = FINGERPRINT_CALLS.get();
        for selector in selectors {
            for &record in &records {
                selectors_match_record(&BTreeSet::from([selector]), record);
            }
        }
        for &record in &records {
            assert!(!selectors_match_record(&BTreeSet::new(), record));
        }
        assert_eq!(FINGERPRINT_CALLS.get() - before, 0);
    }

    #[test]
    fn default_relationship_selection_does_not_require_present_witnesses() {
        let records = fixture();
        let store = FallbackStore {
            records: records.clone(),
            ..FallbackStore::default()
        };
        let selectors = BTreeSet::from([
            CollectionRecordSelector::ProducedMember(collection(1), data(4)),
            CollectionRecordSelector::ProducedMember(collection(1), data(6)),
            CollectionRecordSelector::ProducedMember(collection(2), data(11)),
        ]);
        let selected = store.select_records(&selectors).unwrap();
        assert_eq!(store.enumerations.get(), 1);
        let expected: Vec<_> = records
            .into_iter()
            .filter(|record| match record {
                CollectionRecord::Commit(commit) => {
                    commit.collection() == collection(1) && commit.data() == data(4)
                }
                CollectionRecord::Merge(merge) => {
                    merge.collection() == collection(1) && merge.result() == data(6)
                }
                // Selected as the producer of (collection(2), data(11)),
                // which is what the removed citation selector was standing in
                // for here.
                CollectionRecord::Derive(derive) => {
                    derive.collection() == collection(2) && derive.output() == data(11)
                }
                CollectionRecord::Map(_) => false,
            })
            .collect();
        assert_eq!(selected, expected);
        assert!(selected
            .windows(2)
            .all(|pair| pair[0].fingerprint() < pair[1].fingerprint()));
    }

    #[test]
    fn default_pair_selection_includes_all_inputs_and_excludes_other_pairs() {
        let records = fixture();
        let target = collection(2);
        let pair = [CollectionRecordSelector::DeriveTarget(target)]
            .into_iter()
            .collect();
        let store = FallbackStore {
            records,
            ..FallbackStore::default()
        };

        let selected = store.select_records(&pair).unwrap();

        assert_eq!(selected.len(), 3);
        assert!(selected.iter().all(|record| match record {
            CollectionRecord::Derive(derive) => derive.collection() == target,
            _ => false,
        }));
    }

    #[test]
    fn exact_collection_selection_includes_every_record_kind_and_no_other_collection() {
        let expected = collection(1);
        let other = collection(2);
        let author = SigningKey::from_bytes(&[9; 32]);
        let mut records = vec![
            CollectionRecord::Commit(CollectionCommit::sign(
                &author,
                expected,
                data(1),
                empty_metadata_handle(),
            )),
            CollectionRecord::Merge(
                CollectionMerge::sign(
                    &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                    expected,
                    [data(1), data(2)],
                    data(3),
                )
                .unwrap(),
            ),
            CollectionRecord::Derive(CollectionDerive::sign(
                &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                expected,
                crate::collection::SourceLocator::of(data(3).raw),
                data(4),
            )),
            CollectionRecord::Commit(CollectionCommit::sign(
                &author,
                other,
                data(1),
                empty_metadata_handle(),
            )),
            CollectionRecord::Merge(
                CollectionMerge::sign(
                    &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                    other,
                    [data(1), data(2)],
                    data(3),
                )
                .unwrap(),
            ),
            CollectionRecord::Derive(CollectionDerive::sign(
                &ed25519_dalek::SigningKey::from_bytes(&[7; 32]),
                other,
                crate::collection::SourceLocator::of(data(3).raw),
                data(4),
            )),
        ];
        records.sort_unstable_by_key(CollectionRecord::fingerprint);
        let store = FallbackStore {
            records,
            ..FallbackStore::default()
        };
        let selectors = BTreeSet::from([CollectionRecordSelector::Collection(expected)]);

        let selected = store.select_records(&selectors).unwrap();

        assert_eq!(selected.len(), 3);
        assert!(selected
            .iter()
            .any(|record| matches!(record, CollectionRecord::Commit(_))));
        assert!(selected
            .iter()
            .any(|record| matches!(record, CollectionRecord::Merge(_))));
        assert!(selected
            .iter()
            .any(|record| matches!(record, CollectionRecord::Derive(_))));
        assert!(selected
            .iter()
            .all(|record| record.collection() == expected));
        assert_eq!(store.enumerations.get(), 1);
    }

    #[test]
    fn empty_selection_returns_without_enumeration() {
        let store = FallbackStore {
            records: fixture(),
            ..FallbackStore::default()
        };

        assert!(store.select_records(&BTreeSet::new()).unwrap().is_empty());
        assert_eq!(store.enumerations.get(), 0);
    }

    #[derive(Default)]
    struct OverrideStore {
        records_calls: Cell<usize>,
        selection_calls: Cell<usize>,
    }

    impl CollectionRead for OverrideStore {
        type RecordsError = Infallible;
        type RecordIter<'a> = std::vec::IntoIter<Result<CollectionRecord, Infallible>>;

        fn records<'a>(&'a self) -> Result<Self::RecordIter<'a>, Self::RecordsError> {
            self.records_calls.set(self.records_calls.get() + 1);
            Ok(Vec::new().into_iter())
        }

        fn collections(&self) -> Result<Vec<CollectionHandle>, Self::RecordsError> {
            Ok(Vec::new())
        }

        fn select_records(
            &self,
            _selectors: &BTreeSet<CollectionRecordSelector>,
        ) -> Result<Vec<CollectionRecord>, Self::RecordsError> {
            self.selection_calls.set(self.selection_calls.get() + 1);
            Ok(Vec::new())
        }
    }

    impl CollectionStore for OverrideStore {
        type InsertError = Infallible;

        fn insert(&mut self, _record: CollectionRecord) -> Result<(), Self::InsertError> {
            Ok(())
        }
    }

    #[test]
    fn shared_reference_forwards_selection_override() {
        let store = OverrideStore::default();
        let borrowed = &store;
        let selectors = [CollectionRecordSelector::Collection(collection(1))]
            .into_iter()
            .collect();
        CollectionRead::select_records(&borrowed, &selectors).unwrap();
        assert_eq!(store.selection_calls.get(), 1);
        assert_eq!(store.records_calls.get(), 0);
    }
}