1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package domain_test
4
5import (
6 "strings"
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 TestRevisionCandidates(t *testing.T) {
16 full := strings.Repeat("a", 40)
17 ref := func(s string) domain.RevisionCandidate {
18 return domain.RevisionCandidate{Kind: domain.RevisionCandidateRef, Name: s}
19 }
20 branches := func(s string) []domain.RevisionCandidate {
21 return []domain.RevisionCandidate{ref("refs/heads/" + s), ref("refs/tags/" + s)}
22 }
23 for _, tt := range []struct {
24 name, text string
25 want []domain.RevisionCandidate
26 }{
27 {"zero HEAD", "", []domain.RevisionCandidate{{Kind: domain.RevisionCandidateHead, Name: "HEAD"}}},
28 {"explicit HEAD", "HEAD", []domain.RevisionCandidate{{Kind: domain.RevisionCandidateHead, Name: "HEAD"}}},
29 {"full object first", full, append([]domain.RevisionCandidate{{Kind: domain.RevisionCandidateObject, Name: full}}, branches(full)...)},
30 {"full branch ref last", "refs/heads/main", append(branches("refs/heads/main"), ref("refs/heads/main"))},
31 {"full tag ref last", "refs/tags/v1", append(branches("refs/tags/v1"), ref("refs/tags/v1"))},
32 {"other full ref last", "refs/custom/x", append(branches("refs/custom/x"), ref("refs/custom/x"))},
33 {"minimum abbreviation last", "abc1234", append(branches("abc1234"), domain.RevisionCandidate{Kind: domain.RevisionCandidateAbbrevID, Name: "abc1234"})},
34 {"maximum abbreviation last", full[:39], append(branches(full[:39]), domain.RevisionCandidate{Kind: domain.RevisionCandidateAbbrevID, Name: full[:39]})},
35 {"short hex is ref", "abc123", branches("abc123")},
36 {"uppercase hex is ref", "ABC1234", branches("ABC1234")},
37 {"ordinary name", "main", branches("main")},
38 {"slash name", "feature/x", branches("feature/x")},
39 } {
40 t.Run(tt.name, func(t *testing.T) {
41 r, err := domain.ParseRevision(tt.text)
42 require.NoError(t, err)
43 assert.Equal(t, tt.want, r.Candidates())
44 })
45 }
46}
47
48func TestRevisionCandidateResolvedRef(t *testing.T) {
49 for _, tc := range []struct {
50 kind domain.RevisionCandidateKind
51 want string
52 }{
53 {domain.RevisionCandidateHead, ""}, {domain.RevisionCandidateObject, ""},
54 {domain.RevisionCandidateRef, "refs/heads/main"}, {domain.RevisionCandidateAbbrevID, ""},
55 } {
56 assert.Equal(t, tc.want, (domain.RevisionCandidate{Kind: tc.kind, Name: "refs/heads/main"}).ResolvedRef())
57 }
58}