aboutsummaryrefslogtreecommitdiffstats
path: root/entity/dag/entity_actions_test.go
blob: d7717056bacf61ed313f3e72e47a9e922e8eb435 (plain) (blame)
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
package dag

import (
	"sort"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/repository"
)

func allEntities(t testing.TB, bugs <-chan StreamedEntity) []*Entity {
	t.Helper()

	var result []*Entity
	for streamed := range bugs {
		require.NoError(t, streamed.Err)

		result = append(result, streamed.Entity)
	}
	return result
}

func TestPushPull(t *testing.T) {
	repoA, repoB, remote, id1, id2, def := makeTestContextRemote(t)
	defer repository.CleanupTestRepos(repoA, repoB, remote)

	// A --> remote --> B
	e := New(def)
	e.Append(newOp1(id1, "foo"))

	err := e.Commit(repoA)
	require.NoError(t, err)

	_, err = Push(def, repoA, "remote")
	require.NoError(t, err)

	err = Pull(def, repoB, "remote")
	require.NoError(t, err)

	entities := allEntities(t, ReadAll(def, repoB))
	require.Len(t, entities, 1)

	// B --> remote --> A
	e = New(def)
	e.Append(newOp2(id2, "bar"))

	err = e.Commit(repoB)
	require.NoError(t, err)

	_, err = Push(def, repoB, "remote")
	require.NoError(t, err)

	err = Pull(def, repoA, "remote")
	require.NoError(t, err)

	entities = allEntities(t, ReadAll(def, repoB))
	require.Len(t, entities, 2)
}

func TestListLocalIds(t *testing.T) {
	repoA, repoB, remote, id1, id2, def := makeTestContextRemote(t)
	defer repository.CleanupTestRepos(repoA, repoB, remote)

	// A --> remote --> B
	e := New(def)
	e.Append(newOp1(id1, "foo"))
	err := e.Commit(repoA)
	require.NoError(t, err)

	e = New(def)
	e.Append(newOp2(id2, "bar"))
	err = e.Commit(repoA)
	require.NoError(t, err)

	listLocalIds(t, def, repoA, 2)
	listLocalIds(t, def, repoB, 0)

	_, err = Push(def, repoA, "remote")
	require.NoError(t, err)

	_, err = Fetch(def, repoB, "remote")
	require.NoError(t, err)

	listLocalIds(t, def, repoA, 2)
	listLocalIds(t, def, repoB, 0)

	err = Pull(def, repoB, "remote")
	require.NoError(t, err)

	listLocalIds(t, def, repoA, 2)
	listLocalIds(t, def, repoB, 2)
}

func listLocalIds(t *testing.T, def Definition, repo repository.RepoData, expectedCount int) {
	ids, err := ListLocalIds(def, repo)
	require.NoError(t, err)
	require.Len(t, ids, expectedCount)
}

func assertMergeResults(t *testing.T, expected []entity.MergeResult, results <-chan entity.MergeResult) {
	t.Helper()

	var allResults []entity.MergeResult
	for result := range results {
		allResults = append(allResults, result)
	}

	require.Equal(t, len(expected), len(allResults))

	sort.Slice(allResults, func(i, j int) bool {
		return allResults[i].Id < allResults[j].Id
	})
	sort.Slice(expected, func(i, j int) bool {
		return expected[i].Id < expected[j].Id
	})

	for i, result := range allResults {
		require.NoError(t, result.Err)

		require.Equal(t, expected[i].Id, result.Id)
		require.Equal(t, expected[i].Status, result.Status)

		switch result.Status {
		case entity.MergeStatusNew, entity.MergeStatusUpdated:
			require.NotNil(t, result.Entity)
			require.Equal(t, expected[i].Id, result.Entity.Id())
		}

		i++
	}
}

func TestMerge(t *testing.T) {
	repoA, repoB, remote, id1, id2, def := makeTestContextRemote(t)
	defer repository.CleanupTestRepos(repoA, repoB, remote)

	// SCENARIO 1
	// if the remote Entity doesn't exist locally, it's created

	// 2 entities in repoA + push to remote
	e1 := New(def)
	e1.Append(newOp1(id1, "foo"))
	err := e1.Commit(repoA)
	require.NoError(t, err)

	e2 := New(def)
	e2.Append(newOp2(id2, "bar"))
	err = e2.Commit(repoA)
	require.NoError(t, err)

	_, err = Push(def, repoA, "remote")
	require.NoError(t, err)

	// repoB: fetch + merge from remote

	_, err = Fetch(def, repoB, "remote")
	require.NoError(t, err)

	results := MergeAll(def, repoB, "remote")

	assertMergeResults(t, []entity.MergeResult{
		{
			Id:     e1.Id(),
			Status: entity.MergeStatusNew,
		},
		{
			Id:     e2.Id(),
			Status: entity.MergeStatusNew,
		},
	}, results)

	// SCENARIO 2
	// if the remote and local Entity have the same state, nothing is changed

	results = MergeAll(def, repoB, "remote")

	assertMergeResults(t, []entity.MergeResult{
		{
			Id:     e1.Id(),
			Status: entity.MergeStatusNothing,
		},
		{
			Id:     e2.Id(),
			Status: entity.MergeStatusNothing,
		},
	}, results)

	// SCENARIO 3
	// if the local Entity has new commits but the remote don't, nothing is changed

	e1.Append(newOp1(id1, "barbar"))
	err = e1.Commit(repoA)
	require.NoError(t, err)

	e2.Append(newOp2(id2, "barbarbar"))
	err = e2.Commit(repoA)
	require.NoError(t, err)

	results = MergeAll(def, repoA, "remote")

	assertMergeResults(t, []entity.MergeResult{
		{
			Id:     e1.Id(),
			Status: entity.MergeStatusNothing,
		},
		{
			Id:     e2.Id(),
			Status: entity.MergeStatusNothing,
		},
	}, results)

	// SCENARIO 4
	// if the remote has new commit, the local bug is updated to match the same history
	// (fast-forward update)
}