Go · 3484 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package repo
4
5 import (
6 "testing"
7
8 "github.com/jackc/pgx/v5/pgtype"
9
10 "github.com/tenseleyFlow/shithub/internal/repos/git"
11 reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc"
12 )
13
14 func TestRepoAboutResources_GitHubResourceOrder(t *testing.T) {
15 t.Parallel()
16 row := reposdb.Repo{LicenseKey: pgtype.Text{String: "AGPL-3.0", Valid: true}}
17 entries := []git.TreeEntry{
18 {Kind: git.EntryBlob, Name: "README.md"},
19 {Kind: git.EntryBlob, Name: "LICENSE"},
20 {Kind: git.EntryBlob, Name: "CODE_OF_CONDUCT.md"},
21 {Kind: git.EntryBlob, Name: "CONTRIBUTING.md"},
22 {Kind: git.EntryBlob, Name: "SECURITY.md"},
23 }
24
25 got := repoAboutResources("tenseleyFlow", "shithub", "trunk", row, entries)
26 labels := make([]string, 0, len(got))
27 for _, r := range got {
28 labels = append(labels, r.Label)
29 }
30 want := []string{
31 "Readme",
32 "AGPL-3.0 license",
33 "Code of conduct",
34 "Contributing",
35 "Security policy",
36 "Activity",
37 "Custom properties",
38 }
39 if len(labels) != len(want) {
40 t.Fatalf("labels = %#v, want %#v", labels, want)
41 }
42 for i := range want {
43 if labels[i] != want[i] {
44 t.Fatalf("labels = %#v, want %#v", labels, want)
45 }
46 }
47 if got[1].Href != "/tenseleyFlow/shithub/blob/trunk/LICENSE" {
48 t.Fatalf("license href = %q", got[1].Href)
49 }
50 }
51
52 func TestRepoReadmeTabs_FiltersDocumentTabs(t *testing.T) {
53 t.Parallel()
54 resources := []repoAboutResource{
55 {Icon: "book", Label: "Readme", Href: "#readme"},
56 {Icon: "law", Label: "AGPL-3.0 license", Href: "/LICENSE"},
57 {Icon: "people", Label: "Code of conduct", Href: "/CODE_OF_CONDUCT.md"},
58 {Icon: "people", Label: "Contributing", Href: "/CONTRIBUTING.md"},
59 {Icon: "law", Label: "Security policy", Href: "/SECURITY.md"},
60 {Icon: "pulse", Label: "Activity", Href: "/activity"},
61 {Icon: "note", Label: "Custom properties", Href: "/settings/custom-properties"},
62 }
63 got := repoReadmeTabs(resources)
64 want := []string{"README", "AGPL-3.0 license", "Code of conduct", "Contributing", "Security"}
65 if len(got) != len(want) {
66 t.Fatalf("tabs = %#v, want labels %#v", got, want)
67 }
68 for i := range want {
69 if got[i].Label != want[i] {
70 t.Fatalf("tabs = %#v, want labels %#v", got, want)
71 }
72 }
73 if !got[0].Active {
74 t.Fatalf("README tab should be active: %#v", got[0])
75 }
76 }
77
78 func TestRepoLanguageForPath_ApproximatesGitHubLinguist(t *testing.T) {
79 t.Parallel()
80 cases := []struct {
81 path string
82 want string
83 ok bool
84 }{
85 {"internal/web/server.go", "Go", true},
86 {"templates/repo/tree.html", "HTML", true},
87 {"static/css/shithub.css", "CSS", true},
88 {"scripts/dev.sh", "Shell", true},
89 {"deploy/schema.sql", "PLpgSQL", true},
90 {"README.md", "", false},
91 {"LICENSE", "", false},
92 }
93 for _, tc := range cases {
94 got, _, ok := repoLanguageForPath(tc.path)
95 if got != tc.want || ok != tc.ok {
96 t.Fatalf("%s: got (%q, %v), want (%q, %v)", tc.path, got, ok, tc.want, tc.ok)
97 }
98 }
99 }
100
101 func TestMergeSmallLanguages_CapsSidebarList(t *testing.T) {
102 t.Parallel()
103 ordered := []repoLanguageAggregate{
104 {name: "Go", size: 100},
105 {name: "HTML", size: 90},
106 {name: "CSS", size: 80},
107 {name: "Shell", size: 70},
108 {name: "PLpgSQL", size: 60},
109 {name: "Jinja", size: 50},
110 {name: "JavaScript", size: 40},
111 {name: "Python", size: 30},
112 }
113 got := mergeSmallLanguages(ordered)
114 if len(got) != 7 {
115 t.Fatalf("len = %d, want 7: %#v", len(got), got)
116 }
117 if got[6].name != "Other" || got[6].size != 70 {
118 t.Fatalf("merged tail = %#v, want Other size 70", got[6])
119 }
120 }
121