1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package domain_test
4
5import (
6 "strconv"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11
12 "bnema.dev/ermite/internal/domain"
13)
14
15func TestMergeTreeResultBounded(t *testing.T) {
16 var paths []string
17 for i := 0; i < domain.MaxConflictPaths+2; i++ {
18 paths = append(paths, strconv.Itoa(i))
19 }
20 got := domain.MergeTreeResult{ConflictPaths: paths}.Bounded()
21 assert.Len(t, got.ConflictPaths, domain.MaxConflictPaths)
22 assert.True(t, got.Truncated)
23
24 got = domain.MergeTreeResult{ConflictPaths: paths[:domain.MaxConflictPaths]}.Bounded()
25 assert.Len(t, got.ConflictPaths, domain.MaxConflictPaths)
26 assert.False(t, got.Truncated)
27}
28
29func TestBoundBehind(t *testing.T) {
30 n, capped := domain.BoundBehind(domain.MaxBehindCount)
31 assert.Equal(t, domain.MaxBehindCount, n)
32 assert.False(t, capped)
33 n, capped = domain.BoundBehind(domain.BehindLimit)
34 assert.Equal(t, domain.MaxBehindCount, n)
35 assert.True(t, capped)
36}
37
38func TestMergeTreeResultBoundedCopiesThePaths(t *testing.T) {
39 var paths []string
40 for i := 0; i < domain.MaxConflictPaths+2; i++ {
41 paths = append(paths, strconv.Itoa(i))
42 }
43 got := domain.MergeTreeResult{ConflictPaths: paths}.Bounded()
44 require.Len(t, got.ConflictPaths, domain.MaxConflictPaths)
45 assert.Equal(t, len(got.ConflictPaths), cap(got.ConflictPaths))
46 assert.NotSame(t, &paths[0], &got.ConflictPaths[0], "a new array, not the git output's")
47 assert.Equal(t, paths[:domain.MaxConflictPaths], got.ConflictPaths)
48
49 got = domain.MergeTreeResult{ConflictPaths: paths[:3:10]}.Bounded()
50 assert.Equal(t, 3, cap(got.ConflictPaths))
51 assert.False(t, got.Truncated)
52 assert.Nil(t, domain.MergeTreeResult{}.Bounded().ConflictPaths)
53}