summaryrefslogtreecommitdiff
path: root/internal/showcase/metadata_test.go
blob: abc466413098de421f0b7b187723277d84a60608 (plain)
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
package showcase

import (
	"os"
	"os/exec"
	"path/filepath"
	"testing"
)

func TestCalculateRepoScore_IncreasesWithTagCount(t *testing.T) {
	t.Parallel()

	withoutTags := calculateRepoScore(5000, 14, 0, true)
	withTags := calculateRepoScore(5000, 14, 10, true)

	if withTags <= withoutTags {
		t.Fatalf("expected tags to increase score, got without=%f with=%f", withoutTags, withTags)
	}
}

func TestCalculateRepoScore_DecreasesWithAge(t *testing.T) {
	t.Parallel()

	recent := calculateRepoScore(5000, 7, 3, true)
	old := calculateRepoScore(5000, 70, 3, true)

	if recent <= old {
		t.Fatalf("expected newer activity to score higher, got recent=%f old=%f", recent, old)
	}
}

func TestCalculateRepoScore_DecreasesWithoutRelease(t *testing.T) {
	t.Parallel()

	released := calculateRepoScore(5000, 14, 3, true)
	unreleased := calculateRepoScore(5000, 14, 3, false)

	if unreleased >= released {
		t.Fatalf("expected unreleased repo to score lower, got released=%f unreleased=%f", released, unreleased)
	}
}

func TestGetLatestTag_ReturnsTotalTagCount(t *testing.T) {
	t.Parallel()

	repoPath := t.TempDir()
	runGit(t, repoPath, "init")
	runGit(t, repoPath, "config", "user.name", "Test User")
	runGit(t, repoPath, "config", "user.email", "test@example.com")

	writeAndCommit := func(name, content, message string) {
		path := filepath.Join(repoPath, name)
		if err := os.WriteFile(path, []byte(content), 0644); err != nil {
			t.Fatalf("write file %s: %v", name, err)
		}
		runGit(t, repoPath, "add", name)
		runGit(t, repoPath, "commit", "-m", message)
	}

	writeAndCommit("README.md", "first", "first")
	runGit(t, repoPath, "tag", "notes")
	runGit(t, repoPath, "tag", "v1.0.0")

	writeAndCommit("README.md", "second", "second")
	runGit(t, repoPath, "tag", "v1.1.0")

	latestTag, _, hasReleases, tagCount, err := getLatestTag(repoPath)
	if err != nil {
		t.Fatalf("getLatestTag() error = %v", err)
	}
	if latestTag != "v1.1.0" {
		t.Fatalf("latestTag = %q, want %q", latestTag, "v1.1.0")
	}
	if !hasReleases {
		t.Fatal("expected hasReleases to be true")
	}
	if tagCount != 3 {
		t.Fatalf("tagCount = %d, want %d", tagCount, 3)
	}
}

func TestExtractRepoMetadata_UsesCurrentBranchState(t *testing.T) {
	t.Parallel()

	repoPath := t.TempDir()
	runGit(t, repoPath, "init", "--initial-branch=main")
	runGit(t, repoPath, "config", "user.name", "Test User")
	runGit(t, repoPath, "config", "user.email", "test@example.com")

	writeAndCommit := func(name, content, message string) {
		path := filepath.Join(repoPath, name)
		if err := os.WriteFile(path, []byte(content), 0644); err != nil {
			t.Fatalf("write file %s: %v", name, err)
		}
		runGit(t, repoPath, "add", name)
		runGit(t, repoPath, "commit", "-m", message)
	}

	writeAndCommit("main.go", "package main\n\nfunc main() {\n}\n", "main branch")
	runGit(t, repoPath, "tag", "v1.0.0")

	runGit(t, repoPath, "checkout", "-b", "content-gemtext")
	writeAndCommit("content.go", "package main\n\nfunc render() string {\n\treturn \"gemtext\"\n}\n", "content branch")
	runGit(t, repoPath, "tag", "v2.0.0")

	runGit(t, repoPath, "checkout", "main")

	mainMetadata, err := extractRepoMetadata(repoPath)
	if err != nil {
		t.Fatalf("extractRepoMetadata(main) error = %v", err)
	}
	if mainMetadata.CommitCount != 1 {
		t.Fatalf("main branch CommitCount = %d, want %d", mainMetadata.CommitCount, 1)
	}
	if mainMetadata.LinesOfCode != 4 {
		t.Fatalf("main branch LinesOfCode = %d, want %d", mainMetadata.LinesOfCode, 4)
	}
	if mainMetadata.LatestTag != "v1.0.0" {
		t.Fatalf("main branch LatestTag = %q, want %q", mainMetadata.LatestTag, "v1.0.0")
	}
	if mainMetadata.TagCount != 1 {
		t.Fatalf("main branch TagCount = %d, want %d", mainMetadata.TagCount, 1)
	}

	runGit(t, repoPath, "checkout", "content-gemtext")

	contentMetadata, err := extractRepoMetadata(repoPath)
	if err != nil {
		t.Fatalf("extractRepoMetadata(content-gemtext) error = %v", err)
	}
	if contentMetadata.CommitCount != 2 {
		t.Fatalf("content-gemtext CommitCount = %d, want %d", contentMetadata.CommitCount, 2)
	}
	if contentMetadata.LinesOfCode != 9 {
		t.Fatalf("content-gemtext LinesOfCode = %d, want %d", contentMetadata.LinesOfCode, 9)
	}
	if contentMetadata.LatestTag != "v2.0.0" {
		t.Fatalf("content-gemtext LatestTag = %q, want %q", contentMetadata.LatestTag, "v2.0.0")
	}
	if contentMetadata.TagCount != 2 {
		t.Fatalf("content-gemtext TagCount = %d, want %d", contentMetadata.TagCount, 2)
	}
}

func runGit(t *testing.T, repoPath string, args ...string) string {
	t.Helper()

	cmd := exec.Command("git", append([]string{"-C", repoPath}, args...)...)
	output, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("git %v failed: %v\n%s", args, err, string(output))
	}

	return string(output)
}