gg-cli 0.41.0

GG - Gui for JJ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
use super::{mkid, mkrepo, revs};
use crate::messages::{RevSet, StoreRef, queries::RevsResult};
use crate::worker::{WorkerSession, queries};
use anyhow::Result;
use assert_matches::assert_matches;
use std::collections::HashSet;

#[tokio::test]
async fn log_all() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let all_rows = queries::query_log(&ws, "all()", 100)?;

    assert_eq!(24, all_rows.rows.len());
    assert!(!all_rows.has_more);

    Ok(())
}

#[tokio::test]
async fn log_paged() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let page_rows = queries::query_log(&ws, "all()", 6)?;

    assert_eq!(6, page_rows.rows.len());
    assert!(page_rows.has_more);

    Ok(())
}

#[tokio::test]
async fn log_subset() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let several_rows = queries::query_log(&ws, "bookmarks()", 100)?;

    assert_eq!(4, several_rows.rows.len());

    Ok(())
}

#[tokio::test]
async fn log_mutable() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let single_row = queries::query_log(&ws, "wnpusytq", 100)?
        .rows
        .pop()
        .unwrap();

    assert!(!single_row.revision.is_immutable);

    Ok(())
}

#[tokio::test]
async fn log_immutable() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let single_row = queries::query_log(&ws, "ywknyuol", 100)?
        .rows
        .pop()
        .unwrap();

    assert!(single_row.revision.is_immutable);

    Ok(())
}

#[tokio::test]
async fn revision() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let header = queries::query_revision(&ws, &revs::main_bookmark())?.expect("revision exists");

    assert_matches!(
        header.refs.as_slice(),
        [StoreRef::LocalBookmark { bookmark_name, .. }] if bookmark_name == "main"
    );

    Ok(())
}

/// Test that querying a conflicted revision includes conflict markers in the hunks.
/// The conflict labels from the trees should be passed through to materialize_tree_value().
#[tokio::test]
async fn revision_with_conflict() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let id = revs::conflict_bookmark();
    let result = queries::query_revisions(
        &ws,
        RevSet {
            from: id.clone(),
            to: id,
        },
    )
    .await?;

    let RevsResult::Detail {
        headers, conflicts, ..
    } = result
    else {
        panic!("Expected RevsResult::Detail");
    };

    let header = headers.last().expect("at least one header");

    // The conflict_bookmark commit should be marked as having conflicts
    assert!(
        header.has_conflict,
        "Expected header.has_conflict to be true"
    );

    // The conflicts field should contain the conflict info from the final tree
    assert!(!conflicts.is_empty(), "Expected conflicts to be non-empty");

    // The conflict hunks should contain conflict markers (<<<<<<< and >>>>>>>)
    let conflict_lines: String = conflicts
        .iter()
        .flat_map(|c| &c.hunk.lines.lines)
        .cloned()
        .collect::<Vec<_>>()
        .join("\n");

    assert!(
        conflict_lines.contains("<<<<<<<") && conflict_lines.contains(">>>>>>>"),
        "Expected conflict markers in conflict hunks, got: {conflict_lines}"
    );

    Ok(())
}

#[tokio::test]
async fn conflicted_paths_are_not_duplicated() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let result = queries::query_revisions(
        &ws,
        RevSet {
            from: revs::hunk_source(),
            to: revs::inherited_conflict(),
        },
    )
    .await?;

    let RevsResult::Detail {
        changes, conflicts, ..
    } = result
    else {
        panic!("Expected RevsResult::Detail");
    };

    let conflict_paths: HashSet<String> = conflicts
        .into_iter()
        .map(|conflict| conflict.path.repo_path)
        .collect();
    let duplicated_paths: Vec<String> = changes
        .into_iter()
        .map(|change| change.path.repo_path)
        .filter(|path| conflict_paths.contains(path))
        .collect();

    assert!(
        duplicated_paths.is_empty(),
        "Expected conflicted paths to appear once, duplicates: {:?}",
        duplicated_paths
    );

    Ok(())
}

/// Test that querying a revision without conflicts returns an empty conflicts list.
#[tokio::test]
async fn revision_without_conflict() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let id = revs::main_bookmark();
    let result = queries::query_revisions(
        &ws,
        RevSet {
            from: id.clone(),
            to: id,
        },
    )
    .await?;

    let RevsResult::Detail {
        headers, conflicts, ..
    } = result
    else {
        panic!("Expected RevsResult::Detail");
    };

    let header = headers.last().expect("at least one header");

    assert!(
        !header.has_conflict,
        "Expected header.has_conflict to be false"
    );

    assert!(
        conflicts.is_empty(),
        "Expected conflicts to be empty for non-conflicted revision"
    );

    Ok(())
}

/// Test that resolving a conflict results in no conflicts in the final tree.
/// The diff shows removal of labeled conflict markers from the parent tree.
#[tokio::test]
async fn revision_resolves_conflict() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    // resolve_conflict is a child of conflict_bookmark that resolves the conflict
    let id = revs::resolve_conflict();
    let result = queries::query_revisions(
        &ws,
        RevSet {
            from: id.clone(),
            to: id,
        },
    )
    .await?;

    let RevsResult::Detail {
        headers,
        changes,
        conflicts,
        ..
    } = result
    else {
        panic!("Expected RevsResult::Detail");
    };

    let header = headers.last().expect("at least one header");

    // This commit resolved the conflict, so it should not be conflicted
    assert!(
        !header.has_conflict,
        "Expected header.has_conflict to be false for resolved commit"
    );

    // The conflicts field should be empty since the final tree has no conflicts
    assert!(
        conflicts.is_empty(),
        "Expected conflicts to be empty for resolved commit, got {} conflicts",
        conflicts.len()
    );

    // There should be at least one change (the conflict resolution)
    assert!(!changes.is_empty(), "Expected at least one change");

    // Find the change for the file that had the conflict
    // The diff shows going FROM conflicted parent TO resolved commit
    // So the "before" side (parent tree) had conflict markers
    let all_lines: String = changes
        .iter()
        .flat_map(|c| &c.hunks)
        .flat_map(|h| &h.lines.lines)
        .cloned()
        .collect::<Vec<_>>()
        .join("\n");

    // The diff should show removal of conflict markers (as deleted lines with -)
    // This verifies that format_tree_changes properly materialized the conflicted
    // "before" tree with its labels
    assert!(
        all_lines.contains("-<<<<<<<") || all_lines.contains("->>>>>>>"),
        "Expected removed conflict markers in diff (showing resolution), got:\n{all_lines}"
    );

    Ok(())
}

#[tokio::test]
async fn revisions_nonexistent_range_returns_not_found() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    // use fake change IDs that don't exist in the repo
    let nonexistent_set = RevSet {
        from: mkid("aaaaaaaa", "0000000000000000000000000000000000000000"),
        to: mkid("bbbbbbbb", "1111111111111111111111111111111111111111"),
    };

    let result = queries::query_revisions(&ws, nonexistent_set).await?;
    assert_matches!(
        result,
        RevsResult::NotFound { .. },
        "Querying non-existent range should return NotFound"
    );

    Ok(())
}

/// Test that a merge commit introduces a new conflict where neither parent was conflicted.
/// This verifies the "before" side of the diff (parent trees) has no conflict markers.
#[tokio::test]
async fn merge_introduces_conflict() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    // First verify that a parent of the conflict commit has no conflict
    let parent_id = revs::hunk_source(); // xoooutru - one parent of conflict_bookmark
    let parent_result = queries::query_revisions(
        &ws,
        RevSet {
            from: parent_id.clone(),
            to: parent_id,
        },
    )
    .await?;

    let RevsResult::Detail {
        headers: parent_headers,
        conflicts: parent_conflicts,
        ..
    } = parent_result
    else {
        panic!("Expected RevsResult::Detail for parent");
    };

    let parent_header = parent_headers.last().expect("at least one header");
    assert!(
        !parent_header.has_conflict,
        "Parent commit should not have conflict"
    );
    assert!(
        parent_conflicts.is_empty(),
        "Parent commit should have empty conflicts list"
    );

    // Now query the merge commit that introduces the conflict
    let merge_id = revs::conflict_bookmark();
    let merge_result = queries::query_revisions(
        &ws,
        RevSet {
            from: merge_id.clone(),
            to: merge_id,
        },
    )
    .await?;

    let RevsResult::Detail {
        headers: merge_headers,
        changes,
        conflicts,
        ..
    } = merge_result
    else {
        panic!("Expected RevsResult::Detail for merge");
    };

    let merge_header = merge_headers.last().expect("at least one header");
    assert!(
        merge_header.has_conflict,
        "Merge commit should have conflict"
    );
    assert!(
        !conflicts.is_empty(),
        "Merge commit should have non-empty conflicts"
    );

    // The diff shows changes FROM parent TO merge commit
    // Since the parent wasn't conflicted, the "before" side should NOT have conflict markers
    // Only the "after" side (merge tree) has conflicts, which appear in the `conflicts` field
    let all_change_lines: String = changes
        .iter()
        .flat_map(|c| &c.hunks)
        .flat_map(|h| &h.lines.lines)
        .cloned()
        .collect::<Vec<_>>()
        .join("\n");

    // The changes diff should NOT contain conflict markers in the "before" (deleted) lines
    // because the parent trees weren't conflicted
    let has_conflict_in_before = all_change_lines.contains("-<<<<<<<")
        || all_change_lines.contains("->>>>>>>")
        || all_change_lines.contains("-+++++++")
        || all_change_lines.contains("--------");

    assert!(
        !has_conflict_in_before,
        "Parent trees should not have conflict markers in diff:\n{all_change_lines}"
    );

    Ok(())
}

/// Test that conflicts are inherited through a range when not resolved.
/// A child commit that doesn't resolve a conflict should still show the conflict.
#[tokio::test]
async fn inherited_conflict_persists() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    // Query the child that inherits but doesn't resolve the conflict
    let child_id = revs::inherited_conflict();
    let result = queries::query_revisions(
        &ws,
        RevSet {
            from: child_id.clone(),
            to: child_id,
        },
    )
    .await?;

    let RevsResult::Detail {
        headers,
        changes,
        conflicts,
        ..
    } = result
    else {
        panic!("Expected RevsResult::Detail");
    };

    let header = headers.last().expect("at least one header");

    // The inherited conflict commit should still be marked as conflicted
    assert!(
        header.has_conflict,
        "Expected header.has_conflict to be true for inherited conflict"
    );

    // Conflicts should be non-empty since b.txt is still conflicted
    assert!(
        !conflicts.is_empty(),
        "Expected conflicts to be non-empty for inherited conflict"
    );

    // The conflict markers should still be present
    let conflict_lines: String = conflicts
        .iter()
        .flat_map(|c| &c.hunk.lines.lines)
        .cloned()
        .collect::<Vec<_>>()
        .join("\n");

    assert!(
        conflict_lines.contains("<<<<<<<") && conflict_lines.contains(">>>>>>>"),
        "Expected conflict markers in inherited conflict, got: {conflict_lines}"
    );

    // There should be a change for the unrelated.txt file that was added
    let has_unrelated_change = changes
        .iter()
        .any(|c| c.path.repo_path.ends_with("unrelated.txt"));
    assert!(
        has_unrelated_change,
        "Expected change for unrelated.txt in inherited conflict commit"
    );

    Ok(())
}

/// Test that a range through inherited conflicts shows the final tree's conflict state.
#[tokio::test]
async fn range_through_inherited_conflict() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    // Query range from parent (conflict_bookmark) to child (inherited_conflict)
    let result = queries::query_revisions(
        &ws,
        RevSet {
            from: revs::conflict_bookmark(),
            to: revs::inherited_conflict(),
        },
    )
    .await?;

    let RevsResult::Detail {
        headers, conflicts, ..
    } = result
    else {
        panic!("Expected RevsResult::Detail");
    };

    // Should have 2 commits in range
    assert_eq!(headers.len(), 2, "Expected 2 headers in range");

    // Both commits should be marked as conflicted
    assert!(
        headers.iter().all(|h| h.has_conflict),
        "All commits in range should have conflicts"
    );

    // The conflicts list reflects the final tree (inherited_conflict)
    // which still has the conflict in b.txt
    assert!(
        !conflicts.is_empty(),
        "Range ending in conflicted commit should have conflicts"
    );

    Ok(())
}

/// Test multi-revision ranges where conflicts are introduced and resolved.
/// The chain is: resolve_conflict (no conflict) -> chain_conflict (conflict) -> chain_resolved (no conflict)
mod conflict_chain_ranges {
    use super::*;

    /// Range ending in a conflicted commit should show conflicts
    #[tokio::test]
    async fn range_ends_in_conflict() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        // Range from resolve_conflict to chain_conflict
        // resolve_conflict has no conflicts, chain_conflict does
        let result = queries::query_revisions(
            &ws,
            RevSet {
                from: revs::resolve_conflict(),
                to: revs::chain_conflict(),
            },
        )
        .await?;

        let RevsResult::Detail {
            headers, conflicts, ..
        } = result
        else {
            panic!("Expected RevsResult::Detail");
        };

        // Final commit (chain_conflict) has conflict
        let final_header = headers.first().expect("at least one header");
        assert!(
            final_header.has_conflict,
            "Final commit in range should have conflict"
        );

        // Conflicts field reflects final tree state
        assert!(
            !conflicts.is_empty(),
            "Range ending in conflicted commit should have non-empty conflicts"
        );

        // Verify it's the conflict_chain.txt conflict
        let conflict_paths: Vec<_> = conflicts.iter().map(|c| &c.path.repo_path).collect();
        assert!(
            conflict_paths.iter().any(|p| p.contains("conflict_chain")),
            "Expected conflict in conflict_chain.txt, got: {:?}",
            conflict_paths
        );

        Ok(())
    }

    /// Range starting with conflict, ending with resolution should show no conflicts
    #[tokio::test]
    async fn range_conflict_to_resolved() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        // Range from chain_conflict to chain_resolved
        let result = queries::query_revisions(
            &ws,
            RevSet {
                from: revs::chain_conflict(),
                to: revs::chain_resolved(),
            },
        )
        .await?;

        let RevsResult::Detail {
            headers, conflicts, ..
        } = result
        else {
            panic!("Expected RevsResult::Detail");
        };

        assert_eq!(headers.len(), 2, "Should have 2 headers in range");

        // Oldest commit (chain_conflict) has conflict
        let oldest = headers.last().expect("at least one header");
        assert!(oldest.has_conflict, "Oldest commit should have conflict");

        // Newest commit (chain_resolved) has no conflict
        let newest = headers.first().expect("at least one header");
        assert!(
            !newest.has_conflict,
            "Newest commit should not have conflict"
        );

        // Conflicts field reflects final tree (chain_resolved) which has no conflicts
        assert!(
            conflicts.is_empty(),
            "Range ending in resolved commit should have empty conflicts"
        );

        Ok(())
    }

    /// Long range through multiple conflict states
    /// conflict_bookmark (conflict) -> resolve_conflict (no) -> chain_conflict (conflict) -> chain_resolved (no)
    #[tokio::test]
    async fn range_through_multiple_conflict_states() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        // Range from original conflict through to final resolution
        let result = queries::query_revisions(
            &ws,
            RevSet {
                from: revs::conflict_bookmark(),
                to: revs::chain_resolved(),
            },
        )
        .await?;

        let RevsResult::Detail {
            headers, conflicts, ..
        } = result
        else {
            panic!("Expected RevsResult::Detail");
        };

        // Should have multiple commits in range
        assert!(
            headers.len() >= 4,
            "Expected at least 4 headers, got {}",
            headers.len()
        );

        // Final tree (chain_resolved) has no conflicts
        assert!(
            conflicts.is_empty(),
            "Range ending in fully resolved commit should have empty conflicts"
        );

        // Verify we have mixed conflict states in headers
        let conflict_states: Vec<bool> = headers.iter().map(|h| h.has_conflict).collect();
        let has_conflicted = conflict_states.iter().any(|&c| c);
        let has_resolved = conflict_states.iter().any(|&c| !c);
        assert!(
            has_conflicted && has_resolved,
            "Range should have both conflicted and resolved commits: {:?}",
            conflict_states
        );

        Ok(())
    }

    /// Verify single conflicted commit in chain
    #[tokio::test]
    async fn single_chain_conflict() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        let id = revs::chain_conflict();
        let result = queries::query_revisions(
            &ws,
            RevSet {
                from: id.clone(),
                to: id,
            },
        )
        .await?;

        let RevsResult::Detail {
            headers, conflicts, ..
        } = result
        else {
            panic!("Expected RevsResult::Detail");
        };

        let header = headers.last().expect("at least one header");
        assert!(header.has_conflict, "chain_conflict should have conflict");
        assert!(
            !conflicts.is_empty(),
            "chain_conflict should have non-empty conflicts"
        );

        Ok(())
    }

    /// Verify single resolved commit in chain
    #[tokio::test]
    async fn single_chain_resolved() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        let id = revs::chain_resolved();
        let result = queries::query_revisions(
            &ws,
            RevSet {
                from: id.clone(),
                to: id,
            },
        )
        .await?;

        let RevsResult::Detail {
            headers, conflicts, ..
        } = result
        else {
            panic!("Expected RevsResult::Detail");
        };

        let header = headers.last().expect("at least one header");
        assert!(
            !header.has_conflict,
            "chain_resolved should not have conflict"
        );
        assert!(
            conflicts.is_empty(),
            "chain_resolved should have empty conflicts"
        );

        Ok(())
    }
}

#[tokio::test]
async fn remotes_all() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let remotes = queries::query_remotes(&ws, None)?;

    assert_eq!(2, remotes.len());
    assert!(remotes.contains(&String::from("origin")));
    assert!(remotes.contains(&String::from("second")));

    Ok(())
}

#[tokio::test]
async fn remotes_tracking_bookmark() -> Result<()> {
    let repo = mkrepo();

    let mut session = WorkerSession::default();
    let ws = session.load_workspace(repo.path()).await?;

    let remotes = queries::query_remotes(&ws, Some(String::from("main")))?;

    assert_eq!(1, remotes.len());
    assert!(remotes.contains(&String::from("origin")));

    Ok(())
}

mod revisions_immutability {
    //! Tests for query_revisions immutability checking.
    //!
    //! The test repository has a linear chain: root -> ... -> immutable_grandparent ->
    //! immutable_parent -> immutable_bookmark -> main_bookmark -> working_copy
    //!
    //! immutable_bookmark and its ancestors are immutable; main_bookmark and working_copy are mutable.

    use super::*;
    use crate::messages::{RevSet, queries::RevsResult};

    /// Helper to create a RevSet from two RevIds
    fn mkset(from: crate::messages::RevId, to: crate::messages::RevId) -> RevSet {
        RevSet { from, to }
    }

    /// Helper to extract immutability flags from query result
    fn get_immutability(result: &RevsResult) -> Vec<bool> {
        match result {
            RevsResult::Detail { headers, .. } => headers.iter().map(|h| h.is_immutable).collect(),
            RevsResult::NotFound { .. } => panic!("Expected Detail, got NotFound"),
        }
    }

    #[tokio::test]
    async fn single_revision_immutable() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        let set = mkset(revs::immutable_bookmark(), revs::immutable_bookmark());
        let result = queries::query_revisions(&ws, set).await?;

        let flags = get_immutability(&result);
        assert_eq!(
            flags,
            vec![true],
            "Single immutable revision should be marked immutable"
        );

        Ok(())
    }

    #[tokio::test]
    async fn single_revision_mutable() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        let set = mkset(revs::main_bookmark(), revs::main_bookmark());
        let result = queries::query_revisions(&ws, set).await?;

        let flags = get_immutability(&result);
        assert_eq!(
            flags,
            vec![false],
            "Single mutable revision should be marked mutable"
        );

        Ok(())
    }

    #[tokio::test]
    async fn sequence_all_immutable() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        // immutable_grandparent -> immutable_parent -> immutable_bookmark (all immutable)
        let set = mkset(revs::immutable_grandparent(), revs::immutable_bookmark());
        let result = queries::query_revisions(&ws, set).await?;

        let flags = get_immutability(&result);
        assert_eq!(flags.len(), 3, "Should have 3 revisions in range");
        assert!(
            flags.iter().all(|&f| f),
            "All revisions in immutable range should be immutable: {:?}",
            flags
        );

        Ok(())
    }

    #[tokio::test]
    async fn sequence_all_mutable() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        // main_bookmark -> working_copy (both mutable)
        let set = mkset(revs::main_bookmark(), revs::working_copy());
        let result = queries::query_revisions(&ws, set).await?;

        let flags = get_immutability(&result);
        assert_eq!(flags.len(), 2, "Should have 2 revisions in range");
        assert!(
            flags.iter().all(|&f| !f),
            "All revisions in mutable range should be mutable: {:?}",
            flags
        );

        Ok(())
    }

    #[tokio::test]
    async fn sequence_oldest_immutable_newest_mutable() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        // immutable_bookmark -> main_bookmark (oldest immutable, newest mutable)
        let set = mkset(revs::immutable_bookmark(), revs::main_bookmark());
        let result = queries::query_revisions(&ws, set).await?;

        let flags = get_immutability(&result);
        assert_eq!(flags.len(), 2, "Should have 2 revisions in range");
        assert_eq!(
            flags,
            vec![false, true],
            "Oldest should be immutable, newest should be mutable"
        );

        Ok(())
    }

    #[tokio::test]
    async fn sequence_mixed_immutability_longer() -> Result<()> {
        let repo = mkrepo();
        let mut session = WorkerSession::default();
        let ws = session.load_workspace(repo.path()).await?;

        // immutable_parent -> immutable_bookmark -> main_bookmark -> working_copy
        // First two immutable, last two mutable
        let set = mkset(revs::immutable_parent(), revs::working_copy());
        let result = queries::query_revisions(&ws, set).await?;

        let flags = get_immutability(&result);
        assert_eq!(flags.len(), 4, "Should have 4 revisions in range");
        assert_eq!(
            flags,
            vec![false, false, true, true],
            "First two should be immutable, last two should be mutable"
        );

        Ok(())
    }
}