summaryrefslogtreecommitdiff
path: root/internal/showcase/showcase_test.go
blob: 8bcad888ee1ab3aea6d5dedb8935f013a86dcc83 (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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package showcase

import (
	"os"
	"path/filepath"
	"reflect"
	"strings"
	"testing"

	"codeberg.org/snonux/gitsyncer/internal/config"
)

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

	tests := []struct {
		name string
		repo string
		want bool
	}{
		{name: "exact bak suffix", repo: "foo.bak", want: true},
		{name: "bak dot suffix", repo: "foo.bak.20260222", want: true},
		{name: "bak dot with multiple segments", repo: "foo.bak.tmp.snapshot", want: true},
		{name: "backup word", repo: "foo.backup", want: false},
		{name: "bak as prefix", repo: "bak.foo", want: false},
	}

	for _, tc := range tests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			got := isBackupRepo(tc.repo)
			if got != tc.want {
				t.Fatalf("isBackupRepo(%q) = %v, want %v", tc.repo, got, tc.want)
			}
		})
	}
}

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

	g := &Generator{
		config: &config.Config{
			ExcludeFromShowcase: []string{"manual-exclude"},
		},
	}

	tests := []struct {
		name string
		repo string
		want bool
	}{
		{name: "excluded by config", repo: "manual-exclude", want: true},
		{name: "excluded by backup suffix", repo: "repo.bak", want: true},
		{name: "not excluded", repo: "normal-repo", want: false},
	}

	for _, tc := range tests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			got := g.isExcluded(tc.repo)
			if got != tc.want {
				t.Fatalf("isExcluded(%q) = %v, want %v", tc.repo, got, tc.want)
			}
		})
	}
}

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

	g := &Generator{
		config: &config.Config{
			ExcludeFromShowcase: []string{"manual-exclude"},
		},
	}

	repos := []string{"normal", "manual-exclude", "mirror.bak", "mirror.bak.20260222", "keep"}
	want := []string{"normal", "keep"}

	got := g.filterExcludedRepos(repos)
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("filterExcludedRepos() = %#v, want %#v", got, want)
	}
}

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

	g := &Generator{
		config: &config.Config{},
	}

	repos := []string{"normal", "archive.bak", "archive.bak.old", "keep"}
	want := []string{"normal", "keep"}

	got := g.filterExcludedRepos(repos)
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("filterExcludedRepos() = %#v, want %#v", got, want)
	}
}

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

	g := &Generator{config: &config.Config{}}
	content := g.formatGemtext([]ProjectSummary{
		{
			Name:    "alpha",
			Summary: "alpha summary",
			RankHistory: []RepoRankHistory{
				{Spot: 2, Anchor: "now"},
				{Spot: 3, Anchor: "1w", Arrow: "↖"},
				{Spot: 3, Anchor: "2w", Arrow: "←"},
				{Spot: 0, Anchor: "3w", Arrow: "·"},
				{Spot: 2, Anchor: "4w", Arrow: "↙"},
			},
		},
	})

	if !strings.Contains(content, "### 1. alpha 2↖3←3↙2") {
		t.Fatalf("rank history was not rendered in header: %s", content)
	}
}

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

	g := &Generator{config: &config.Config{}}
	content := g.formatGemtext([]ProjectSummary{
		{
			Name:    "alpha",
			Summary: "# Alpha Project\n\nconf\n====\n\nParagraph body",
		},
	})

	if strings.Contains(content, "\n# Alpha Project\n") {
		t.Fatalf("markdown heading leaked into gemtext summary: %s", content)
	}
	if strings.Contains(content, "\n====\n") {
		t.Fatalf("setext underline leaked into gemtext summary: %s", content)
	}
	if !strings.Contains(content, "\nAlpha Project\n\nconf\n\nParagraph body\n\n") {
		t.Fatalf("sanitized summary not rendered as expected: %s", content)
	}
}

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

	repoPath := filepath.Join(t.TempDir(), "repo")
	if err := os.MkdirAll(repoPath, 0755); err != nil {
		t.Fatalf("failed to create repo dir: %v", err)
	}

	readmePath := filepath.Join(repoPath, "README.md")
	if err := os.WriteFile(readmePath, []byte("repo summary"), 0644); err != nil {
		t.Fatalf("failed to write readme: %v", err)
	}

	content, readmeFile, found := findReadmeContent(repoPath)
	if !found {
		t.Fatal("expected README to be found")
	}
	if readmeFile != "README.md" {
		t.Fatalf("expected README.md, got %q", readmeFile)
	}
	if string(content) != "repo summary" {
		t.Fatalf("unexpected README content: %q", string(content))
	}
}

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

	readme := []byte("first paragraph\n\nsecond paragraph")
	summary := fallbackSummary("repo", readme, true)

	if summary != "first paragraph" {
		t.Fatalf("expected first paragraph summary, got %q", summary)
	}
}

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

	readme := []byte("# repo title\n\n<img src=\"shot.png\" />\n\nactual summary paragraph")
	summary := fallbackSummary("repo", readme, true)

	if summary != "actual summary paragraph" {
		t.Fatalf("expected summary paragraph after heading and image, got %q", summary)
	}
}

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

	input := "<p align=\"center\">\n<img src=\"shot.png\" />\n</p>\n\n* first bullet\n* second bullet\n\nTOC:\n01. Intro\n02. Usage\n\nActual summary paragraph.\n\nSecond useful paragraph."
	got := extractUsefulSummary(input, 2)
	want := "Actual summary paragraph.\n\nSecond useful paragraph."

	if got != want {
		t.Fatalf("extractUsefulSummary() = %q, want %q", got, want)
	}
}

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

	input := "NAME\n    cpuinfo - A small and humble tool to print out CPU data"
	got := extractUsefulSummary(input, 1)
	want := "cpuinfo - A small and humble tool to print out CPU data"

	if got != want {
		t.Fatalf("extractUsefulSummary() = %q, want %q", got, want)
	}
}

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

	input := "```sh\nsudo dnf install wireguard-tools\nbundler install\n```\n\nActual summary paragraph."
	got := extractUsefulSummary(input, 1)
	want := "Actual summary paragraph."

	if got != want {
		t.Fatalf("extractUsefulSummary() = %q, want %q", got, want)
	}
}