azure_data_cosmos 0.33.0

Rust wrappers around Microsoft Azure REST APIs - Azure Cosmos DB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

//! Helpers for merging and managing session tokens across feed ranges.

use crate::feed_range::FeedRange;
use azure_core::error::ErrorKind;
use azure_data_cosmos_driver::models::{SessionToken, SessionTokenSegment};

/// Returns `true` if the session token string contains multiple comma-separated segments.
fn is_compound(token: &str) -> bool {
    token.contains(',')
}

/// Merges two non-compound session token strings that cover the same feed range.
///
/// When the tokens have different partition key range IDs, keeps the ID from
/// the token with the higher global LSN (the more recent topology).
fn merge_tokens_same_range(token1: &str, token2: &str) -> azure_core::Result<String> {
    let mut seg1: SessionTokenSegment = token1.parse()?;
    let seg2: SessionTokenSegment = token2.parse()?;

    if seg1.pk_range_id() != seg2.pk_range_id()
        && seg2.is_as_recent_as(&seg1)
        && !seg1.is_as_recent_as(&seg2)
    {
        seg1.set_pk_range_id(seg2.pk_range_id());
    }

    seg1.merge_value(&seg2);
    Ok(seg1.to_string())
}

/// Phase 1: merge session tokens that share the exact same feed range (non-compound only).
fn merge_same_ranges(overlapping: &mut Vec<(FeedRange, String)>) -> azure_core::Result<()> {
    let mut i = 0;
    while i < overlapping.len() {
        let mut j = i + 1;
        while j < overlapping.len() {
            if !is_compound(&overlapping[i].1)
                && !is_compound(&overlapping[j].1)
                && overlapping[i].0 == overlapping[j].0
            {
                let token1 = overlapping[i].1.clone();
                let token2 = overlapping[j].1.clone();
                let merged = merge_tokens_same_range(&token1, &token2)?;
                let feed_range = overlapping[i].0.clone();
                overlapping.remove(j);
                overlapping.remove(i);
                overlapping.push((feed_range, merged));
                i = 0;
                j = 1;
            } else {
                j += 1;
                if j >= overlapping.len() {
                    i += 1;
                    j = i + 1;
                }
            }
        }
        if j >= overlapping.len() {
            i += 1;
        }
    }
    Ok(())
}

/// Describes the action to take after analyzing parent/child subset relationships.
enum MergeAction {
    /// No parent/child match found; do nothing.
    None,
    /// Parent is newer — remove children, keep parent at index 0.
    KeepParent { children_indices: Vec<usize> },
    /// Children are newer (split) — remove children and parent, add compound.
    ReplaceParent {
        children_indices: Vec<usize>,
        compound: (FeedRange, String),
    },
    /// Mixed — remove children, keep parent, add compound of all tokens.
    AddCompound {
        children_indices: Vec<usize>,
        compound: (FeedRange, String),
    },
}

/// Phase 2: detect partition split/merge scenarios by analyzing parent/child feed range
/// relationships.
///
/// - **Split**: children's combined range equals parent and children have higher LSN →
///   parent is replaced by compound of children.
/// - **Merge**: parent has higher LSN than children → children are removed.
/// - **Mixed**: some children are newer, some aren't → all tokens are kept as compound.
///
/// The input is sorted by range size descending so that parents are always processed
/// before their children, regardless of the caller's input order.
fn merge_ranges_with_subsets(
    mut overlapping: Vec<(FeedRange, String)>,
) -> azure_core::Result<Vec<(FeedRange, String)>> {
    // Sort by range size descending: larger ranges (parents) first.
    // Primary: max_exclusive descending, secondary: min_inclusive ascending.
    overlapping.sort_by(|(a, _), (b, _)| {
        b.max_exclusive
            .cmp(&a.max_exclusive)
            .then(a.min_inclusive.cmp(&b.min_inclusive))
    });

    let mut processed = Vec::new();

    while !overlapping.is_empty() {
        let range_cmp = overlapping[0].0.clone();
        let token_cmp = overlapping[0].1.clone();

        if !is_compound(&token_cmp) {
            let seg_cmp: SessionTokenSegment = token_cmp.parse()?;

            // Find non-compound subsets of the current range
            let subsets: Vec<(usize, FeedRange, String)> = overlapping
                .iter()
                .enumerate()
                .skip(1)
                .filter(|(_, (fr, tok))| !is_compound(tok) && fr.is_subset_of(&range_cmp))
                .map(|(i, (fr, tok))| (i, fr.clone(), tok.clone()))
                .collect();

            if subsets.len() == 1 {
                // Single subset: only remove child if parent has strictly higher LSN
                let child_seg: SessionTokenSegment = subsets[0].2.parse()?;
                if seg_cmp.is_as_recent_as(&child_seg) && !child_seg.is_as_recent_as(&seg_cmp) {
                    overlapping.remove(subsets[0].0);
                }
            } else if subsets.len() > 1 {
                let action = analyze_subsets(&range_cmp, &seg_cmp, &token_cmp, &subsets)?;

                // Apply the action — caller manages all mutations consistently
                match action {
                    MergeAction::None => {}
                    MergeAction::KeepParent { children_indices } => {
                        remove_indices(&mut overlapping, &children_indices);
                    }
                    MergeAction::ReplaceParent {
                        children_indices,
                        compound,
                    } => {
                        remove_indices(&mut overlapping, &children_indices);
                        overlapping.remove(0); // remove parent
                        overlapping.push(compound);
                        // Restart: the compound is now at the end and will be processed later
                        continue;
                    }
                    MergeAction::AddCompound {
                        children_indices,
                        compound,
                    } => {
                        remove_indices(&mut overlapping, &children_indices);
                        overlapping.push(compound);
                        // Parent stays at index 0, falls through to processed.push below
                    }
                }
            }
        }

        processed.push(overlapping.remove(0));
    }

    Ok(processed)
}

/// Removes elements at the given indices from a vec, handling index shifts correctly.
fn remove_indices<T>(vec: &mut Vec<T>, indices: &[usize]) {
    let mut sorted = indices.to_vec();
    sorted.sort_unstable();
    sorted.reverse();
    for idx in sorted {
        vec.remove(idx);
    }
}

/// Analyzes multiple child subsets to determine if they combine to equal the parent range,
/// and returns the appropriate merge action.
///
/// Subsets are sorted by `min_inclusive` ascending before the merge loop to ensure
/// adjacent children are processed in order, regardless of the caller's input ordering.
fn analyze_subsets(
    parent_range: &FeedRange,
    parent_seg: &SessionTokenSegment,
    parent_token: &str,
    subsets: &[(usize, FeedRange, String)],
) -> azure_core::Result<MergeAction> {
    // Sort subsets by min_inclusive so adjacent children are always in order
    let mut sorted_subsets = subsets.to_vec();
    sorted_subsets.sort_by(|a, b| a.1.min_inclusive.cmp(&b.1.min_inclusive));

    for start_idx in 0..sorted_subsets.len() {
        let mut merged_range = sorted_subsets[start_idx].1.clone();
        let mut tokens = vec![sorted_subsets[start_idx].2.clone()];
        let mut indices = vec![sorted_subsets[start_idx].0];

        for (k, (idx, fr, tok)) in sorted_subsets.iter().enumerate() {
            if k == start_idx {
                continue;
            }
            if merged_range.can_merge(fr) {
                merged_range = merged_range.merge_with(fr);
                tokens.push(tok.clone());
                indices.push(*idx);
            }
            if *parent_range == merged_range {
                // Children combine to equal the parent — determine action
                let mut children_more_updated = true;
                let mut parent_more_updated = true;
                for t in &tokens {
                    let child_seg: SessionTokenSegment = t.parse()?;
                    if parent_seg.is_as_recent_as(&child_seg)
                        && !child_seg.is_as_recent_as(parent_seg)
                    {
                        children_more_updated = false;
                    } else {
                        parent_more_updated = false;
                    }
                }

                if children_more_updated {
                    // Split: children are newer
                    let compound = tokens.join(",");
                    return Ok(MergeAction::ReplaceParent {
                        children_indices: indices,
                        compound: (merged_range, compound),
                    });
                } else if parent_more_updated {
                    // Merge: parent is newer
                    return Ok(MergeAction::KeepParent {
                        children_indices: indices,
                    });
                } else {
                    // Mixed: keep all as compound
                    tokens.push(parent_token.to_owned());
                    let compound = tokens.join(",");
                    return Ok(MergeAction::AddCompound {
                        children_indices: indices,
                        compound: (merged_range, compound),
                    });
                }
            }
        }
    }

    Ok(MergeAction::None)
}

/// Phase 3: split compound session tokens into individual segments.
///
/// Feed range context is intentionally dropped here — from this point on,
/// merging is done purely by partition key range ID (Phase 4).
fn split_compound_tokens(ranges_and_tokens: &[(FeedRange, String)]) -> Vec<String> {
    let mut result = Vec::new();
    for (_, token) in ranges_and_tokens {
        for segment in token.split(',') {
            let trimmed = segment.trim();
            if !trimmed.is_empty() {
                result.push(trimmed.to_owned());
            }
        }
    }
    result
}

/// Phase 4: merge session token segments that share the same partition key range ID.
///
/// Delegates to `SessionToken::merge()` on the driver side so that token format
/// details stay encapsulated.
fn merge_tokens_by_partition(tokens: Vec<String>) -> azure_core::Result<SessionToken> {
    let mut result = SessionToken::new(tokens[0].clone());
    for t in &tokens[1..] {
        result = result.merge(&SessionToken::new(t.clone()))?;
    }
    Ok(result)
}

/// Gets the most up-to-date session token from a list of feed range and session token pairs
/// for a specific target feed range.
///
/// This function merges session tokens from feed ranges that overlap with the target,
/// handling partition split and merge scenarios automatically. It is useful when
/// maintaining your own session token cache across multiple clients.
///
/// Session tokens and feed ranges are scoped to a single container. Only pass session
/// tokens and feed ranges obtained from the same container.
///
/// # Arguments
///
/// * `feed_ranges_to_session_tokens` - Pairs of feed ranges and their associated session tokens.
/// * `target_feed_range` - The feed range to get the most up-to-date session token for.
///
/// # Errors
///
/// Returns an error if no input feed ranges overlap with the target feed range,
/// or if any session token string is malformed.
///
/// # Examples
///
/// ```rust,no_run
/// # use azure_data_cosmos::{clients::ContainerClient, FeedRange, SessionToken};
/// # async fn example(container: ContainerClient) -> azure_core::Result<()> {
/// // After read/write operations, capture session tokens from response headers.
/// // When using multiple clients against the same container, merge their tokens
/// // to get the most up-to-date session state.
/// let feed_range = FeedRange::full();
/// let token_a: SessionToken = "0:1#100#3=50".into();
/// let token_b: SessionToken = "0:1#200#3=60".into();
///
/// let latest = container.get_latest_session_token(
///     &[(feed_range.clone(), token_a), (feed_range, token_b)],
///     &FeedRange::full(),
/// )?;
/// // latest == "0:1#200#3=60" (higher LSN values win)
/// # Ok(())
/// # }
/// ```
pub(crate) fn get_latest_session_token(
    feed_ranges_to_session_tokens: &[(FeedRange, SessionToken)],
    target_feed_range: &FeedRange,
) -> azure_core::Result<SessionToken> {
    // Step 1: Filter to overlapping feed ranges
    let mut overlapping: Vec<(FeedRange, String)> = feed_ranges_to_session_tokens
        .iter()
        .filter(|(fr, _)| target_feed_range.overlaps(fr))
        .map(|(fr, st)| (fr.clone(), st.as_str().to_owned()))
        .collect();

    if overlapping.is_empty() {
        return Err(azure_core::Error::with_message(
            ErrorKind::Other,
            "no overlapping feed ranges with the target feed range",
        ));
    }

    // Step 2: Merge session tokens for identical feed ranges
    merge_same_ranges(&mut overlapping)?;

    // Step 3: Handle partition split/merge via subset detection
    let processed = merge_ranges_with_subsets(overlapping)?;

    // Step 4: Split compound tokens into individual segments
    let remaining = split_compound_tokens(&processed);

    if remaining.len() == 1 {
        return Ok(SessionToken::new(remaining.into_iter().next().unwrap()));
    }

    // Step 5: Merge segments with same partition key range ID
    merge_tokens_by_partition(remaining)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hash::EffectivePartitionKey;

    fn fr(min: &str, max: &str) -> FeedRange {
        FeedRange {
            min_inclusive: EffectivePartitionKey::from(min),
            max_exclusive: EffectivePartitionKey::from(max),
        }
    }

    fn st(s: &str) -> SessionToken {
        SessionToken::new(s.to_owned())
    }

    // === Normal merge scenarios ===

    #[test]
    fn normal_case_same_range_merge() {
        let result = get_latest_session_token(
            &[
                (fr("AA", "BB"), st("0:1#54#3=50")),
                (fr("AA", "BB"), st("0:1#51#3=52")),
            ],
            &fr("AA", "BB"),
        )
        .unwrap();
        assert_eq!(result.as_str(), "0:1#54#3=52");
    }

    #[test]
    fn split_with_both_children() {
        let result = get_latest_session_token(
            &[
                (fr("AA", "DD"), st("0:1#51#3=52")),
                (fr("AA", "BB"), st("1:1#55#3=52")),
                (fr("BB", "DD"), st("2:1#54#3=52")),
            ],
            &fr("AA", "DD"),
        )
        .unwrap();
        // Children are newer → split detected
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 2);
        assert!(parts.contains(&"1:1#55#3=52"));
        assert!(parts.contains(&"2:1#54#3=52"));
    }

    #[test]
    fn split_with_one_child() {
        let result = get_latest_session_token(
            &[
                (fr("AA", "DD"), st("0:1#51#3=52")),
                (fr("AA", "BB"), st("1:1#55#3=52")),
            ],
            &fr("AA", "DD"),
        )
        .unwrap();
        // Single child can't cover parent → both survive
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 2);
        assert!(parts.contains(&"0:1#51#3=52"));
        assert!(parts.contains(&"1:1#55#3=52"));
    }

    #[test]
    fn merge_parent_newer() {
        let result = get_latest_session_token(
            &[
                (fr("AA", "DD"), st("0:1#55#3=52")),
                (fr("AA", "BB"), st("1:1#51#3=52")),
            ],
            &fr("AA", "DD"),
        )
        .unwrap();
        // Parent is newer → child removed (merge scenario)
        assert_eq!(result.as_str(), "0:1#55#3=52");
    }

    #[test]
    fn compound_token_passthrough() {
        let result = get_latest_session_token(
            &[
                (fr("AA", "DD"), st("2:1#54#3=52,1:1#55#3=52")),
                (fr("AA", "BB"), st("0:1#51#3=52")),
            ],
            &fr("AA", "BB"),
        )
        .unwrap();
        // Compound passes through, all tokens preserved
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 3);
        assert!(parts.contains(&"2:1#54#3=52"));
        assert!(parts.contains(&"1:1#55#3=52"));
        assert!(parts.contains(&"0:1#51#3=52"));
    }

    #[test]
    fn several_compound_tokens() {
        let result = get_latest_session_token(
            &[
                (fr("AA", "DD"), st("2:1#57#3=52,1:1#57#3=52")),
                (fr("AA", "DD"), st("2:1#56#3=52,1:1#58#3=52")),
            ],
            &fr("AA", "DD"),
        )
        .unwrap();
        // Compound tokens split and merged by pk range id
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 2);
        assert!(parts.contains(&"2:1#57#3=52"));
        assert!(parts.contains(&"1:1#58#3=52"));
    }

    #[test]
    fn overlapping_ranges() {
        let result = get_latest_session_token(
            &[
                (fr("AA", "CC"), st("0:1#54#3=52")),
                (fr("BB", "FF"), st("2:1#51#3=52")),
            ],
            &fr("AA", "EE"),
        )
        .unwrap();
        // Both overlap with target, different pk range ids
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 2);
        assert!(parts.contains(&"0:1#54#3=52"));
        assert!(parts.contains(&"2:1#51#3=52"));
    }

    #[test]
    fn no_relevant_feed_ranges() {
        let result = get_latest_session_token(
            &[
                (fr("CC", "DD"), st("0:1#54#3=52")),
                (fr("EE", "FF"), st("0:1#51")),
            ],
            &fr("AA", "BB"),
        );
        assert!(result.is_err());
    }

    // === Additional edge cases ===

    #[test]
    fn same_range_different_pk_range_ids() {
        // When same feed range has different pk range ids, keep the one with higher LSN
        let result = get_latest_session_token(
            &[
                (fr("AA", "BB"), st("0:1#100#3=50")),
                (fr("AA", "BB"), st("1:1#200#3=60")),
            ],
            &fr("AA", "BB"),
        )
        .unwrap();
        // pk range id 1 has higher global LSN (200 > 100)
        assert!(result.as_str().starts_with("1:"));
        assert!(result.as_str().contains("#200#"));
    }

    #[test]
    fn v1_tokens_merge() {
        let result = get_latest_session_token(
            &[(fr("AA", "BB"), st("0:100")), (fr("AA", "BB"), st("0:200"))],
            &fr("AA", "BB"),
        )
        .unwrap();
        assert_eq!(result.as_str(), "0:200");
    }

    #[test]
    fn single_input() {
        let result =
            get_latest_session_token(&[(fr("AA", "FF"), st("0:1#100#1=10"))], &fr("AA", "FF"))
                .unwrap();
        assert_eq!(result.as_str(), "0:1#100#1=10");
    }

    #[test]
    fn full_range_target() {
        let result = get_latest_session_token(
            &[(FeedRange::full(), st("0:1#100#1=10"))],
            &FeedRange::full(),
        )
        .unwrap();
        assert_eq!(result.as_str(), "0:1#100#1=10");
    }

    #[test]
    fn mixed_split_scenario() {
        // Parent LSN is between the two children
        let result = get_latest_session_token(
            &[
                (fr("AA", "DD"), st("0:1#53#3=52")),
                (fr("AA", "BB"), st("1:1#55#3=52")),
                (fr("BB", "DD"), st("2:1#51#3=52")),
            ],
            &fr("AA", "DD"),
        )
        .unwrap();
        // Mixed: child 1 newer (55 > 53), child 2 older (51 < 53) → keep all
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 3);
        assert!(parts.contains(&"0:1#53#3=52"));
        assert!(parts.contains(&"1:1#55#3=52"));
        assert!(parts.contains(&"2:1#51#3=52"));
    }

    // === Input ordering tests (Findings 2, 3, 4) ===

    #[test]
    fn three_way_split_sorted() {
        // Parent with 3 children in sorted order, all children newer
        let result = get_latest_session_token(
            &[
                (fr("AA", "FF"), st("0:1#50#3=50")),
                (fr("AA", "BB"), st("1:1#55#3=52")),
                (fr("BB", "DD"), st("2:1#56#3=52")),
                (fr("DD", "FF"), st("3:1#57#3=52")),
            ],
            &fr("AA", "FF"),
        )
        .unwrap();
        // All children newer → split detected, parent replaced
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 3);
        assert!(parts.contains(&"1:1#55#3=52"));
        assert!(parts.contains(&"2:1#56#3=52"));
        assert!(parts.contains(&"3:1#57#3=52"));
        // Parent token should NOT be present
        assert!(!parts.contains(&"0:1#50#3=50"));
    }

    #[test]
    fn three_way_split_shuffled() {
        // Same as above but children in non-sorted order (Finding 3)
        let result = get_latest_session_token(
            &[
                (fr("AA", "FF"), st("0:1#50#3=50")),
                (fr("DD", "FF"), st("3:1#57#3=52")),
                (fr("AA", "BB"), st("1:1#55#3=52")),
                (fr("BB", "DD"), st("2:1#56#3=52")),
            ],
            &fr("AA", "FF"),
        )
        .unwrap();
        // Should produce same result regardless of child order
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 3);
        assert!(parts.contains(&"1:1#55#3=52"));
        assert!(parts.contains(&"2:1#56#3=52"));
        assert!(parts.contains(&"3:1#57#3=52"));
        assert!(!parts.contains(&"0:1#50#3=50"));
    }

    #[test]
    fn children_before_parent_in_input() {
        // Children appear before parent in the input array (Finding 4)
        let result = get_latest_session_token(
            &[
                (fr("AA", "BB"), st("1:1#55#3=52")),
                (fr("BB", "DD"), st("2:1#54#3=52")),
                (fr("AA", "DD"), st("0:1#51#3=52")),
            ],
            &fr("AA", "DD"),
        )
        .unwrap();
        // Should still detect the split — same as split_with_both_children
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 2);
        assert!(parts.contains(&"1:1#55#3=52"));
        assert!(parts.contains(&"2:1#54#3=52"));
    }

    #[test]
    fn unrelated_between_parent_and_children() {
        // Unrelated feed range sits between parent and children in input (Finding 2)
        let result = get_latest_session_token(
            &[
                (fr("AA", "DD"), st("0:1#51#3=52")),
                (fr("EE", "FF"), st("9:1#99#3=99")),
                (fr("AA", "BB"), st("1:1#55#3=52")),
                (fr("BB", "DD"), st("2:1#54#3=52")),
            ],
            &fr("AA", "FF"),
        )
        .unwrap();
        // Split should still be detected for [AA,DD), and unrelated [EE,FF) preserved
        let parts: Vec<&str> = result.as_str().split(',').collect();
        assert_eq!(parts.len(), 3);
        assert!(parts.contains(&"1:1#55#3=52"));
        assert!(parts.contains(&"2:1#54#3=52"));
        assert!(parts.contains(&"9:1#99#3=99"));
        // Parent should have been replaced by children
        assert!(!parts.contains(&"0:1#51#3=52"));
    }

    // === FeedRange helper tests ===

    #[test]
    fn can_merge_adjacent() {
        let a = fr("AA", "BB");
        let b = fr("BB", "DD");
        assert!(a.can_merge(&b));
        assert!(b.can_merge(&a));
    }

    #[test]
    fn can_merge_overlapping() {
        let a = fr("AA", "CC");
        let b = fr("BB", "DD");
        assert!(a.can_merge(&b));
    }

    #[test]
    fn cannot_merge_disjoint() {
        let a = fr("AA", "BB");
        let b = fr("CC", "DD");
        assert!(!a.can_merge(&b));
    }

    #[test]
    fn merge_with_produces_bounding_range() {
        let a = fr("AA", "BB");
        let b = fr("BB", "DD");
        let merged = a.merge_with(&b);
        assert_eq!(merged, fr("AA", "DD"));
    }
}