1package merge_test
2
3import (
4 "strings"
5 "testing"
6
7 "arche/internal/merge"
8)
9
10func TestMergeText_Equal(t *testing.T) {
11 r := merge.MergeText("base", "same", "same")
12 if !r.Clean {
13 t.Error("equal ours/theirs should be clean")
14 }
15 if r.Content != "same" {
16 t.Errorf("got %q, want %q", r.Content, "same")
17 }
18}
19
20func TestMergeText_FastForwardOurs(t *testing.T) {
21 r := merge.MergeText("base", "base", "theirs-edit")
22 if !r.Clean {
23 t.Error("base==ours should be clean fast-forward")
24 }
25 if r.Content != "theirs-edit" {
26 t.Errorf("got %q, want %q", r.Content, "theirs-edit")
27 }
28}
29
30func TestMergeText_FastForwardTheirs(t *testing.T) {
31 r := merge.MergeText("base", "ours-edit", "base")
32 if !r.Clean {
33 t.Error("base==theirs should be clean fast-forward")
34 }
35 if r.Content != "ours-edit" {
36 t.Errorf("got %q, want %q", r.Content, "ours-edit")
37 }
38}
39
40func TestMergeText_NonConflictingEdits(t *testing.T) {
41 base := "line1\nline2\nline3\n"
42 ours := "line1\nLINE2-OURS\nline3\n"
43 theirs := "line1\nline2\nLINE3-THEIRS\n"
44 r := merge.MergeText(base, ours, theirs)
45 if !r.Clean {
46 t.Logf("merge produced conflict (patch may not have applied cleanly): %q", r.Content)
47 return
48 }
49 if !strings.Contains(r.Content, "LINE2-OURS") {
50 t.Error("merged content missing LINE2-OURS")
51 }
52 if !strings.Contains(r.Content, "LINE3-THEIRS") {
53 t.Error("merged content missing LINE3-THEIRS")
54 }
55}
56
57func TestMergeText_Conflict(t *testing.T) {
58 base := "shared line\n"
59 ours := "ours changed this\n"
60 theirs := "theirs changed this\n"
61 r := merge.MergeText(base, ours, theirs)
62 if r.Clean {
63 t.Logf("no conflict detected; merged content: %q", r.Content)
64 return
65 }
66 if !strings.Contains(r.Content, "<<<<<<<") {
67 t.Errorf("expected conflict markers in: %q", r.Content)
68 }
69 if r.Ours != ours {
70 t.Errorf("Ours: got %q, want %q", r.Ours, ours)
71 }
72 if r.Theirs != theirs {
73 t.Errorf("Theirs: got %q, want %q", r.Theirs, theirs)
74 }
75}
76
77func TestMergeText_AllSameAsBase(t *testing.T) {
78 r := merge.MergeText("content", "content", "content")
79 if !r.Clean {
80 t.Error("identical base/ours/theirs should be clean")
81 }
82 if r.Content != "content" {
83 t.Errorf("got %q, want %q", r.Content, "content")
84 }
85}
86
87func TestMergeText_EmptyBase(t *testing.T) {
88 r := merge.MergeText("", "add-ours\n", "add-theirs\n")
89 if r.Content == "" && r.Clean {
90 t.Error("merging two non-empty strings into empty base should produce non-empty result")
91 }
92}