mnem-cli 0.1.7

Command-line interface for mnem - Git for AI Agent Knowledge.
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
//! Integration tests for `mnem merge` (B4.3).
//!
//! These tests drive the built `mnem` binary against a temp-dir repo.
//! Every test covers one externally-visible behaviour: the LCA
//! fast-forward path, the clean 3-way path, conflict persistence, the
//! --dry-run side-effect-free preview, and the --abort cleanup flow.

use std::path::Path;
use std::process::Command;

use assert_cmd::prelude::*;
use tempfile::TempDir;

fn mnem(repo: &Path, args: &[&str]) -> Command {
    let mut cmd = Command::cargo_bin("mnem").expect("built mnem binary");
    cmd.current_dir(repo);
    cmd.arg("-R").arg(repo);
    for a in args {
        cmd.arg(a);
    }
    cmd
}

/// Init a repo in `dir` (asserts success).
fn init(dir: &Path) {
    mnem(dir, &["init", dir.to_str().unwrap()])
        .assert()
        .success();
}

/// Add a single node with `--no-embed` (avoids provider calls in CI).
fn add_node(dir: &Path, summary: &str) {
    mnem(dir, &["add", "node", "--summary", summary, "--no-embed"])
        .assert()
        .success();
}

/// Return the HEAD commit CID as shown by `mnem status` (`commit     <cid>`).
fn head_cid(dir: &Path) -> String {
    let out = mnem(dir, &["status"]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    for line in stdout.lines() {
        if let Some(rest) = line.strip_prefix("commit     ") {
            let cid = rest.trim().to_string();
            if !cid.is_empty() && !cid.starts_with('<') {
                return cid;
            }
        }
    }
    panic!("could not extract HEAD CID from `mnem status` output:\n{stdout}");
}

/// Build two diverged branches from a common base and return (main_tip, feature_tip).
/// After this helper, the active branch is `main`.
fn setup_diverged_branches(dir: &Path) -> (String, String) {
    // Common ancestor on main.
    add_node(dir, "shared-base");

    // Create feature branch at this point.
    mnem(dir, &["branch", "create", "feature"])
        .assert()
        .success();

    // Advance main with a unique commit.
    add_node(dir, "main-only");
    let main_tip = head_cid(dir);

    // Switch to feature and add its own commit.
    mnem(dir, &["switch", "feature"]).assert().success();
    add_node(dir, "feature-only");
    let feature_tip = head_cid(dir);

    // Switch back to main so we can merge feature into it.
    mnem(dir, &["switch", "main"]).assert().success();

    assert_ne!(main_tip, feature_tip, "branches must have diverged");
    (main_tip, feature_tip)
}

#[test]
fn merge_missing_branch_errors_actionably() {
    let dir = TempDir::new().unwrap();
    mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
        .assert()
        .success();
    // No branch arg, no --continue / --abort.
    let out = mnem(dir.path(), &["merge"]).assert().failure();
    let stderr = String::from_utf8_lossy(&out.get_output().stderr).to_string();
    assert!(
        stderr.contains("missing <branch>") || stderr.contains("required"),
        "missing-branch diagnostic should mention the missing argument: {stderr}"
    );
}

#[test]
fn merge_already_up_to_date_when_same_commit() {
    let dir = TempDir::new().unwrap();
    mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
        .assert()
        .success();
    mnem(
        dir.path(),
        &["add", "node", "--summary", "seed", "--prop", "k=1"],
    )
    .assert()
    .success();
    // Create a branch at HEAD, then merge it - should be up-to-date.
    mnem(dir.path(), &["branch", "create", "feat"])
        .assert()
        .success();
    let out = mnem(dir.path(), &["merge", "feat"]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("already up to date") || stdout.contains("fast-forward"),
        "expected up-to-date or FF, got: {stdout}"
    );
}

#[test]
fn merge_abort_without_in_progress_errors() {
    let dir = TempDir::new().unwrap();
    mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
        .assert()
        .success();
    let out = mnem(dir.path(), &["merge", "--abort"]).assert().failure();
    let stderr = String::from_utf8_lossy(&out.get_output().stderr).to_string();
    assert!(
        stderr.contains("no merge in progress"),
        "abort without in-progress should complain: {stderr}"
    );
}

#[test]
fn merge_help_lists_strategy_and_flags() {
    let out = Command::cargo_bin("mnem")
        .unwrap()
        .args(["merge", "--help"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    for needle in [
        "--strategy",
        "--dry-run",
        "--continue",
        "--abort",
        "ours",
        "theirs",
        "manual",
    ] {
        assert!(
            stdout.contains(needle),
            "merge --help must surface `{needle}`, got: {stdout}"
        );
    }
}

// ---------------------------------------------------------------------------
// Test 5: clean merge of two branches with non-overlapping commits
// ---------------------------------------------------------------------------

#[test]
fn merge_clean_diverged_branches_succeeds() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    let (_main_tip, _feature_tip) = setup_diverged_branches(p);

    // Merge feature into main - non-overlapping commits, no conflicts expected.
    let out = mnem(p, &["merge", "feature"]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("fast-forward")
            || stdout.contains("merge:")
            || stdout.contains("already up to date"),
        "merge of non-overlapping branches should succeed with a merge/ff message, got: {stdout}"
    );

    // After a clean merge, no MERGE_HEAD sentinel should exist.
    let merge_head = p.join(".mnem").join("MERGE_HEAD");
    assert!(
        !merge_head.exists(),
        "MERGE_HEAD must not exist after a clean merge"
    );
}

// ---------------------------------------------------------------------------
// Test 6: --dry-run does not persist any state
// ---------------------------------------------------------------------------

#[test]
fn merge_dry_run_persists_no_state() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    let (main_tip, _feature_tip) = setup_diverged_branches(p);

    // Capture HEAD CID before the dry-run.
    let head_before = head_cid(p);
    assert_eq!(
        head_before, main_tip,
        "we should be back on main after setup"
    );

    // Run with --dry-run; it should exit 0 and describe what would happen.
    let out = mnem(p, &["merge", "--dry-run", "feature"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("[dry-run]"),
        "--dry-run output should contain '[dry-run]' prefix, got: {stdout}"
    );

    // MERGE_HEAD must NOT exist (dry-run writes nothing).
    let mnem_dir = p.join(".mnem");
    assert!(
        !mnem_dir.join("MERGE_HEAD").exists(),
        "--dry-run must not write MERGE_HEAD"
    );
    assert!(
        !mnem_dir.join("ORIG_HEAD").exists(),
        "--dry-run must not write ORIG_HEAD"
    );
    assert!(
        !mnem_dir.join("MERGE_CONFLICTS.json").exists(),
        "--dry-run must not write MERGE_CONFLICTS.json"
    );

    // HEAD CID must be unchanged.
    let head_after = head_cid(p);
    assert_eq!(head_before, head_after, "--dry-run must not advance HEAD");
}

// ---------------------------------------------------------------------------
// Test 7: --strategy=ours is accepted and merge succeeds
// ---------------------------------------------------------------------------

#[test]
fn merge_strategy_ours_accepted() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    setup_diverged_branches(p);

    // --strategy=ours should be accepted and produce a successful merge.
    let out = mnem(p, &["merge", "--strategy=ours", "feature"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("fast-forward")
            || stdout.contains("merge:")
            || stdout.contains("already up to date"),
        "--strategy=ours merge should succeed, got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// Test 8: --strategy=theirs is accepted and merge succeeds
// ---------------------------------------------------------------------------

#[test]
fn merge_strategy_theirs_accepted() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    setup_diverged_branches(p);

    // --strategy=theirs should be accepted and produce a successful merge.
    let out = mnem(p, &["merge", "--strategy=theirs", "feature"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("fast-forward")
            || stdout.contains("merge:")
            || stdout.contains("already up to date"),
        "--strategy=theirs merge should succeed, got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// Test 9: --abort with in-progress merge removes sentinel files
// ---------------------------------------------------------------------------

#[test]
fn merge_abort_with_in_progress_removes_sentinels() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    // Commit something so we have a real CID to use.
    add_node(p, "base-commit");
    let cid = head_cid(p);

    // Manually write the sentinel files to simulate a merge in progress.
    let mnem_dir = p.join(".mnem");
    std::fs::write(mnem_dir.join("MERGE_HEAD"), &cid).expect("could not write MERGE_HEAD");
    std::fs::write(mnem_dir.join("ORIG_HEAD"), &cid).expect("could not write ORIG_HEAD");
    std::fs::write(
        mnem_dir.join("MERGE_CONFLICTS.json"),
        r#"{"schema":"mnem.v1.merge_conflicts","left_head":"fake","right_head":"fake","conflicts":[]}"#,
    )
    .expect("could not write MERGE_CONFLICTS.json");

    assert!(
        mnem_dir.join("MERGE_HEAD").exists(),
        "sanity: MERGE_HEAD must exist before abort"
    );

    // --abort should succeed and clean up.
    let out = mnem(p, &["merge", "--abort"]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("aborted") || stdout.contains("abort"),
        "--abort should confirm cancellation, got: {stdout}"
    );

    assert!(
        !mnem_dir.join("MERGE_HEAD").exists(),
        "--abort must remove MERGE_HEAD"
    );
    assert!(
        !mnem_dir.join("ORIG_HEAD").exists(),
        "--abort must remove ORIG_HEAD"
    );
    assert!(
        !mnem_dir.join("MERGE_CONFLICTS.json").exists(),
        "--abort must remove MERGE_CONFLICTS.json"
    );
}

// ---------------------------------------------------------------------------
// Test 10: --continue with no merge in progress errors helpfully
// ---------------------------------------------------------------------------

#[test]
fn merge_continue_without_in_progress_errors() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    let out = mnem(p, &["merge", "--continue"]).assert().failure();
    let stderr = String::from_utf8_lossy(&out.get_output().stderr).to_string();
    assert!(
        stderr.contains("no merge in progress"),
        "--continue without MERGE_HEAD must say 'no merge in progress', got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// Test 11: --continue with an unresolved conflict file errors helpfully
// ---------------------------------------------------------------------------

#[test]
fn merge_continue_with_unresolved_conflicts_errors() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    // Commit something to get a valid CID.
    add_node(p, "anchor");
    let cid = head_cid(p);

    let mnem_dir = p.join(".mnem");

    // Write sentinel files with a conflict entry that has no resolution field.
    // MERGE_HEAD and ORIG_HEAD contain the real CID so parse_cid_file succeeds
    // if the code ever gets that far (it should not, since unresolved check
    // comes first).
    std::fs::write(mnem_dir.join("MERGE_HEAD"), &cid).expect("write MERGE_HEAD");
    std::fs::write(mnem_dir.join("ORIG_HEAD"), &cid).expect("write ORIG_HEAD");

    // Build a MERGE_CONFLICTS.json with one conflict that has no resolution.
    // The `category` field is required (snake_case); `node_id` is optional but
    // we include it so the conflict is realistic. No `resolution` field means
    // `--continue` must refuse with an "unresolved" error.
    let conflicts_json = format!(
        r#"{{
  "schema": "mnem.v1.merge_conflicts",
  "left_head": "{cid}",
  "right_head": "{cid}",
  "conflicts": [
    {{
      "node_id": "00000000-0000-0000-0000-000000000001",
      "category": "node_cid_divergence",
      "left": {{"cid": "bafy000left"}},
      "right": {{"cid": "bafy000right"}}
    }}
  ]
}}"#
    );
    std::fs::write(mnem_dir.join("MERGE_CONFLICTS.json"), &conflicts_json)
        .expect("write MERGE_CONFLICTS.json");

    let out = mnem(p, &["merge", "--continue"]).assert().failure();
    let stderr = String::from_utf8_lossy(&out.get_output().stderr).to_string();
    assert!(
        stderr.contains("resolution")
            || stderr.contains("unresolved")
            || stderr.contains("conflict"),
        "--continue with unresolved conflicts must mention resolution/conflict, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// Helpers for conflict-producing tests
// ---------------------------------------------------------------------------

/// Return the CID that `refs/heads/<branch>` points to, as shown by
/// `mnem ref list`. Used to verify the ref advances after a merge.
fn branch_tip(dir: &Path, branch: &str) -> String {
    let out = mnem(dir, &["ref", "list"]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    let prefix = format!("refs/heads/{branch}");
    for line in stdout.lines() {
        let stripped = line.trim();
        if stripped.starts_with(&prefix) {
            if let Some(arrow_pos) = stripped.find("->") {
                let cid = stripped[arrow_pos + 2..].trim().to_string();
                if !cid.is_empty() {
                    return cid;
                }
            }
        }
    }
    panic!("could not find ref for branch '{branch}' in `mnem ref list` output:\n{stdout}");
}

/// Set up two diverged branches that have a real conflict: both branches
/// modify the same node UUID (NODE_CONFLICT_ID) to different content.
///
/// After this helper:
/// - Active branch is `main`
/// - `main` has: shared-base node + conflict node with summary "conflict-node-on-main"
/// - `feature` has: conflict node with summary "conflict-node-on-feature"
/// - The two branches have a real `NodeCidDivergence` conflict on the shared UUID
///
/// Returns `(main_tip, feature_tip)` before the merge.
const NODE_CONFLICT_ID: &str = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";

fn setup_conflicting_branches(dir: &Path) -> (String, String) {
    // Common base commit.
    add_node(dir, "shared-base");

    // Create feature at this point.
    mnem(dir, &["branch", "create", "feature"])
        .assert()
        .success();

    // Advance main: add the conflict node with "main" content.
    mnem(
        dir,
        &[
            "add",
            "node",
            "--id",
            NODE_CONFLICT_ID,
            "--summary",
            "conflict-node-on-main",
            "--no-embed",
        ],
    )
    .assert()
    .success();
    let main_tip = branch_tip(dir, "main");

    // Switch to feature and add the conflict node with "feature" content.
    mnem(dir, &["switch", "feature"]).assert().success();
    mnem(
        dir,
        &[
            "add",
            "node",
            "--id",
            NODE_CONFLICT_ID,
            "--summary",
            "conflict-node-on-feature",
            "--no-embed",
        ],
    )
    .assert()
    .success();
    let feature_tip = branch_tip(dir, "feature");

    // Switch back to main so the caller can run `mnem merge feature`.
    mnem(dir, &["switch", "main"]).assert().success();

    assert_ne!(main_tip, feature_tip, "branches must have diverged");
    (main_tip, feature_tip)
}

/// Read `.mnem/MERGE_CONFLICTS.json`, add `"resolution": <value>` to every
/// conflict entry, and write it back. Used by the `--continue` tests.
fn resolve_all_conflicts(dir: &Path, resolution: &str) {
    let mc_path = dir.join(".mnem").join("MERGE_CONFLICTS.json");
    let raw = std::fs::read_to_string(&mc_path)
        .expect("MERGE_CONFLICTS.json must exist before resolving");
    let mut data: serde_json::Value =
        serde_json::from_str(&raw).expect("MERGE_CONFLICTS.json must be valid JSON");
    if let Some(conflicts) = data.get_mut("conflicts").and_then(|v| v.as_array_mut()) {
        for entry in conflicts.iter_mut() {
            if let Some(obj) = entry.as_object_mut() {
                obj.insert(
                    "resolution".to_string(),
                    serde_json::Value::String(resolution.to_string()),
                );
            }
        }
    }
    std::fs::write(
        &mc_path,
        serde_json::to_string_pretty(&data).expect("re-serialise"),
    )
    .expect("write resolved MERGE_CONFLICTS.json");
}

// ---------------------------------------------------------------------------
// Test 12 (Gap 1): --continue success path end-to-end
// ---------------------------------------------------------------------------

#[test]
fn merge_continue_resolves_to_completion() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    let (main_tip_before, _feature_tip) = setup_conflicting_branches(p);

    let mnem_dir = p.join(".mnem");

    // Trigger a conflict - exits 0 but writes sentinel files.
    let out = mnem(p, &["merge", "feature"]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("conflict"),
        "merge of conflicting branches must report conflicts, got: {stdout}"
    );

    // All three sentinel files must exist.
    assert!(
        mnem_dir.join("MERGE_HEAD").exists(),
        "MERGE_HEAD must exist after a conflicting merge"
    );
    assert!(
        mnem_dir.join("ORIG_HEAD").exists(),
        "ORIG_HEAD must exist after a conflicting merge"
    );
    assert!(
        mnem_dir.join("MERGE_CONFLICTS.json").exists(),
        "MERGE_CONFLICTS.json must exist after a conflicting merge"
    );

    // Resolve every conflict entry with "ours" so --continue can proceed.
    resolve_all_conflicts(p, "ours");

    // Run --continue - must exit 0.
    let out = mnem(p, &["merge", "--continue"]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("merge continued") || stdout.contains("advanced") || stdout.contains("->"),
        "--continue must report that the merge was finalised, got: {stdout}"
    );

    // All three sentinel files must be gone.
    assert!(
        !mnem_dir.join("MERGE_HEAD").exists(),
        "MERGE_HEAD must be removed after --continue"
    );
    assert!(
        !mnem_dir.join("ORIG_HEAD").exists(),
        "ORIG_HEAD must be removed after --continue"
    );
    assert!(
        !mnem_dir.join("MERGE_CONFLICTS.json").exists(),
        "MERGE_CONFLICTS.json must be removed after --continue"
    );

    // The branch ref must have advanced past the pre-merge tip.
    let main_tip_after = branch_tip(p, "main");
    assert_ne!(
        main_tip_before, main_tip_after,
        "refs/heads/main must advance after --continue"
    );
}

// ---------------------------------------------------------------------------
// Test 13 (Gap 2a): --strategy=ours auto-resolves a real conflict
// ---------------------------------------------------------------------------

#[test]
fn merge_strategy_ours_picks_left_on_conflict() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    let (main_tip_before, _feature_tip) = setup_conflicting_branches(p);

    let mnem_dir = p.join(".mnem");

    // --strategy=ours must auto-resolve without writing sentinel files.
    let out = mnem(p, &["merge", "--strategy=ours", "feature"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("merge:") || stdout.contains("advanced") || stdout.contains("->"),
        "--strategy=ours on a conflicting merge must produce a merge commit, got: {stdout}"
    );

    // No sentinel files must exist: auto-strategy resolves without pausing.
    assert!(
        !mnem_dir.join("MERGE_HEAD").exists(),
        "--strategy=ours must not leave MERGE_HEAD"
    );
    assert!(
        !mnem_dir.join("ORIG_HEAD").exists(),
        "--strategy=ours must not leave ORIG_HEAD"
    );
    assert!(
        !mnem_dir.join("MERGE_CONFLICTS.json").exists(),
        "--strategy=ours must not leave MERGE_CONFLICTS.json"
    );

    // The branch ref must have advanced.
    let main_tip_after = branch_tip(p, "main");
    assert_ne!(
        main_tip_before, main_tip_after,
        "refs/heads/main must advance after --strategy=ours merge"
    );

    // With --strategy=ours, the left/main side must win the conflict.
    // "ours" = left = the branch we are merging INTO (main), so the merged
    // node must carry the main-side summary and NOT the feature-side one.
    let out = mnem(p, &["get", NODE_CONFLICT_ID]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("conflict-node-on-main"),
        "--strategy=ours must pick the left (main) side on conflict, got: {stdout}"
    );
    assert!(
        !stdout.contains("conflict-node-on-feature"),
        "--strategy=ours must NOT pick the right (feature) side on conflict, got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// Test 14 (Gap 2b): --strategy=theirs auto-resolves a real conflict
// ---------------------------------------------------------------------------

#[test]
fn merge_strategy_theirs_picks_right_on_conflict() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    let (main_tip_before, _feature_tip) = setup_conflicting_branches(p);

    let mnem_dir = p.join(".mnem");

    // --strategy=theirs must auto-resolve without writing sentinel files.
    let out = mnem(p, &["merge", "--strategy=theirs", "feature"])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("merge:") || stdout.contains("advanced") || stdout.contains("->"),
        "--strategy=theirs on a conflicting merge must produce a merge commit, got: {stdout}"
    );

    // No sentinel files must exist.
    assert!(
        !mnem_dir.join("MERGE_HEAD").exists(),
        "--strategy=theirs must not leave MERGE_HEAD"
    );
    assert!(
        !mnem_dir.join("ORIG_HEAD").exists(),
        "--strategy=theirs must not leave ORIG_HEAD"
    );
    assert!(
        !mnem_dir.join("MERGE_CONFLICTS.json").exists(),
        "--strategy=theirs must not leave MERGE_CONFLICTS.json"
    );

    // The branch ref must have advanced.
    let main_tip_after = branch_tip(p, "main");
    assert_ne!(
        main_tip_before, main_tip_after,
        "refs/heads/main must advance after --strategy=theirs merge"
    );

    // With --strategy=theirs, the right/feature side must win the conflict.
    // "theirs" = right = the branch being merged IN (feature), so the merged
    // node must carry the feature-side summary and NOT the main-side one.
    let out = mnem(p, &["get", NODE_CONFLICT_ID]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("conflict-node-on-feature"),
        "--strategy=theirs must pick the right (feature) side on conflict, got: {stdout}"
    );
    assert!(
        !stdout.contains("conflict-node-on-main"),
        "--strategy=theirs must NOT pick the left (main) side on conflict, got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// Test 15 (Gap 3): --continue with MERGE_HEAD but no MERGE_CONFLICTS.json
// ---------------------------------------------------------------------------

#[test]
fn merge_continue_missing_conflicts_json_errors() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    // We need a real CID in MERGE_HEAD so parse_cid_file succeeds if the
    // code reaches that far (it should not - the missing-file check comes first).
    add_node(p, "anchor");
    let cid = head_cid(p);

    let mnem_dir = p.join(".mnem");
    std::fs::write(mnem_dir.join("MERGE_HEAD"), &cid).expect("write MERGE_HEAD");
    std::fs::write(mnem_dir.join("ORIG_HEAD"), &cid).expect("write ORIG_HEAD");
    // Deliberately do NOT write MERGE_CONFLICTS.json.

    let out = mnem(p, &["merge", "--continue"]).assert().failure();
    let stderr = String::from_utf8_lossy(&out.get_output().stderr).to_string();
    assert!(
        stderr.contains("MERGE_CONFLICTS")
            || stderr.contains("not found")
            || stderr.contains("conflict"),
        "--continue with missing MERGE_CONFLICTS.json must complain about it, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// Test 16 (Gap 4 from audit): --continue with mixed resolutions errors
// ---------------------------------------------------------------------------

/// Writing a MERGE_CONFLICTS.json where one conflict is resolved as "ours"
/// and another as "theirs" must cause `--continue` to fail with a
/// message about mixed picks. The current engine applies one strategy
/// globally; per-conflict resolution is not yet supported.
#[test]
fn merge_continue_mixed_resolutions_errors() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    add_node(p, "anchor");
    let cid = head_cid(p);

    let mnem_dir = p.join(".mnem");
    std::fs::write(mnem_dir.join("MERGE_HEAD"), &cid).expect("write MERGE_HEAD");
    std::fs::write(mnem_dir.join("ORIG_HEAD"), &cid).expect("write ORIG_HEAD");

    // Two conflict entries: one resolved "ours", one resolved "theirs".
    let conflicts_json = format!(
        r#"{{
  "schema": "mnem.v1.merge_conflicts",
  "left_head": "{cid}",
  "right_head": "{cid}",
  "conflicts": [
    {{
      "node_id": "00000000-0000-0000-0000-000000000001",
      "category": "node_cid_divergence",
      "left": {{"cid": "bafy000left1"}},
      "right": {{"cid": "bafy000right1"}},
      "resolution": "ours"
    }},
    {{
      "node_id": "00000000-0000-0000-0000-000000000002",
      "category": "node_cid_divergence",
      "left": {{"cid": "bafy000left2"}},
      "right": {{"cid": "bafy000right2"}},
      "resolution": "theirs"
    }}
  ]
}}"#
    );
    std::fs::write(mnem_dir.join("MERGE_CONFLICTS.json"), &conflicts_json)
        .expect("write MERGE_CONFLICTS.json");

    let out = mnem(p, &["merge", "--continue"]).assert().failure();
    let stderr = String::from_utf8_lossy(&out.get_output().stderr).to_string();
    assert!(
        stderr.contains("mixed") || stderr.contains("ours") || stderr.contains("theirs"),
        "--continue with mixed resolutions must mention the mixed-picks problem, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// Test 17 (Gap 5 from audit): --continue with empty conflicts list succeeds
// ---------------------------------------------------------------------------

/// When MERGE_CONFLICTS.json exists but its `"conflicts"` array is empty
/// (i.e. the merge was actually clean or the user deleted every entry),
/// `--continue` must treat it as a clean continuation and advance HEAD.
#[test]
fn merge_continue_empty_conflicts_list_succeeds() {
    let dir = TempDir::new().unwrap();
    let p = dir.path();
    init(p);

    // We need a REAL repo state so --continue can actually advance the ref.
    // Set up two diverged branches and trigger a merge that produces sentinel
    // files, then manually replace MERGE_CONFLICTS.json with an empty list.
    let (main_tip_before, _feature_tip) = setup_conflicting_branches(p);
    let mnem_dir = p.join(".mnem");

    // Trigger a conflict to write sentinel files.
    let out = mnem(p, &["merge", "feature"]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("conflict"),
        "setup: merge should have produced conflicts, got: {stdout}"
    );

    // Overwrite MERGE_CONFLICTS.json with an empty conflicts list.
    // Keep the real left_head and right_head so the file parses correctly.
    let mc_path = mnem_dir.join("MERGE_CONFLICTS.json");
    let raw = std::fs::read_to_string(&mc_path).expect("MERGE_CONFLICTS.json must exist");
    let mut data: serde_json::Value =
        serde_json::from_str(&raw).expect("MERGE_CONFLICTS.json must be valid JSON");
    *data.get_mut("conflicts").unwrap() = serde_json::Value::Array(vec![]);
    std::fs::write(&mc_path, serde_json::to_string_pretty(&data).unwrap())
        .expect("overwrite MERGE_CONFLICTS.json");

    // --continue with an empty conflicts list must succeed and advance HEAD.
    let out = mnem(p, &["merge", "--continue"]).assert().success();
    let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
    assert!(
        stdout.contains("merge continued") || stdout.contains("advanced") || stdout.contains("->"),
        "--continue with empty conflicts must finalise the merge, got: {stdout}"
    );

    // Sentinel files must be gone.
    assert!(
        !mnem_dir.join("MERGE_HEAD").exists(),
        "MERGE_HEAD must be removed after --continue"
    );
    assert!(
        !mnem_dir.join("ORIG_HEAD").exists(),
        "ORIG_HEAD must be removed after --continue"
    );
    assert!(
        !mnem_dir.join("MERGE_CONFLICTS.json").exists(),
        "MERGE_CONFLICTS.json must be removed after --continue"
    );

    // The branch ref must have advanced.
    let main_tip_after = branch_tip(p, "main");
    assert_ne!(
        main_tip_before, main_tip_after,
        "refs/heads/main must advance after --continue with empty conflicts list"
    );
}