BlueGit

timfa bluegit

Files

bluegit / internal / server / commit_list_test.go

commit_list_test.go

feat: geef de commitlijst de GitLab-opbouw
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed b026b59a History
commit_list_test.go 5.3 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "net/http/cookiejar"
7 "net/http/httptest"
8 "net/url"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strings"
13 "testing"
14
15 "gitlab.com/Timfa/bluegit/internal/auth"
16 "gitlab.com/Timfa/bluegit/internal/config"
17 "gitlab.com/Timfa/bluegit/internal/store"
18)
19
20func TestCommitList(t *testing.T) {
21 if _, err := exec.LookPath("git"); err != nil {
22 t.Skip("git ontbreekt")
23 }
24 storage := t.TempDir()
25 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
26 if err != nil {
27 t.Fatal(err)
28 }
29 t.Cleanup(func() { _ = st.Close() })
30 cfg := config.Config{
31 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
32 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
33 Git: config.Git{Binary: "git", DefaultBranch: "master"},
34 }
35 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
36 ts := httptest.NewServer(srv.Handler())
37 t.Cleanup(ts.Close)
38 jar, _ := cookiejar.New(nil)
39 tim := &http.Client{Jar: jar}
40 page := read(t, mustGet(t, tim, ts.URL+"/login"))
41 page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{
42 "csrf": {csrfOf(t, page)}, "name": {"logboek"}, "visibility": {"public"},
43 }))
44 page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{
45 "csrf": {csrfOf(t, page)}, "name": {"laptop"},
46 }))
47 token := between(page, `<p class="code">`, "</p>")
48 remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/logboek.git"
49 work := t.TempDir()
50 gitPrep(t, work)
51 writeFile(t, filepath.Join(work, "note.txt"), "oud\n")
52 git(t, work, "add", "note.txt")
53 gitAs(t, work, "Zoeker", "zoeker@example.com", "2026-09-01T12:00:00Z", "oudste")
54 writeFile(t, filepath.Join(work, "note.txt"), "sept\n")
55 git(t, work, "add", "note.txt")
56 gitBody(t, work, "Zoeker", "zoeker@example.com", "2026-09-02T12:00:00Z", "september-commit", "tweede regel van de body")
57 for i := 2; i <= 20; i++ {
58 writeFile(t, filepath.Join(work, "note.txt"), strings.Repeat("x", i)+"\n")
59 git(t, work, "add", "note.txt")
60 msg := "c02"
61 when := "2026-10-23T12:00:00Z"
62 if i == 20 {
63 msg = "nieuwste"
64 } else {
65 msg = "c" + two(i)
66 }
67 gitAs(t, work, "tim", "tim@users.git.timfalken.com", when, msg)
68 }
69 git(t, work, "remote", "add", "origin", remote)
70 git(t, work, "push", "-u", "origin", "master")
71 head := revHEAD(t, work)
72
73 page = read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits"))
74 for _, want := range []string{
75 "Commits", "logboek", `name="rev"`, `value="master"`,
76 "Search by message or author", "23 October 2026", "2 September 2026",
77 "nieuwste", "september-commit", "authored",
78 `class="avatar ident"`, "Zoeker",
79 `data-copy="` + head + `"`,
80 `/-/commit/` + head,
81 `/-/tree?rev=` + head,
82 `data-commit-toggle`,
83 "tweede regel van de body",
84 "Load more",
85 } {
86 if !strings.Contains(page, want) {
87 t.Fatalf("commitlijst mist %q", want)
88 }
89 }
90 if strings.Contains(page, "Unverified") || strings.Contains(page, "oudste") {
91 t.Fatalf("eerste pagina:\n%s", page)
92 }
93 if !strings.Contains(page, ">Load more</a>") && !strings.Contains(page, "page=2") {
94 t.Fatalf("geen volgende pagina:\n%s", page)
95 }
96
97 filtered := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits?q=september-commit"))
98 if !strings.Contains(filtered, "september-commit") || strings.Contains(filtered, ">nieuwste</a>") || !strings.Contains(filtered, "2 September 2026") {
99 t.Fatalf("filter op bericht:\n%s", filtered)
100 }
101 byAuthor := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits?q=Zoeker"))
102 if !strings.Contains(byAuthor, "september-commit") || !strings.Contains(byAuthor, "oudste") || strings.Contains(byAuthor, ">nieuwste</a>") {
103 t.Fatalf("filter op auteur:\n%s", byAuthor)
104 }
105 none := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits?q=geen-treffer"))
106 if !strings.Contains(none, "No matching commits.") {
107 t.Fatalf("lege filter:\n%s", none)
108 }
109
110 next := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits?page=2"))
111 if !strings.Contains(next, ">oudste</a>") || strings.Contains(next, ">nieuwste</a>") || !strings.Contains(next, "Previous") || strings.Contains(next, ">Load more</a>") {
112 t.Fatalf("pagina 2:\n%s", next)
113 }
114
115 repo, err := st.RepoByPath(context.Background(), "tim", "logboek")
116 if err != nil {
117 t.Fatal(err)
118 }
119 if _, err := st.CreatePipeline(context.Background(), repo.ID, head, "master", "push", "tim", "nieuwste", []store.NewJob{{
120 Name: "build", Stage: "test", Script: "true",
121 }}); err != nil {
122 t.Fatal(err)
123 }
124 withPipe := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits"))
125 if !strings.Contains(withPipe, `class="status pending markonly"`) || !strings.Contains(withPipe, "/-/pipelines/") {
126 t.Fatalf("pipeline-icoon:\n%s", withPipe)
127 }
128}
129
130func two(n int) string {
131 if n < 10 {
132 return "0" + string(rune('0'+n))
133 }
134 return string(rune('0'+n/10)) + string(rune('0'+n%10))
135}
136
137func gitBody(t *testing.T, dir, name, email, date, subject, body string) {
138 t.Helper()
139 cmd := exec.Command("git", "commit", "-m", subject, "-m", body)
140 cmd.Dir = dir
141 cmd.Env = append(os.Environ(),
142 "GIT_AUTHOR_NAME="+name,
143 "GIT_AUTHOR_EMAIL="+email,
144 "GIT_COMMITTER_NAME="+name,
145 "GIT_COMMITTER_EMAIL="+email,
146 "GIT_AUTHOR_DATE="+date,
147 "GIT_COMMITTER_DATE="+date,
148 )
149 out, err := cmd.CombinedOutput()
150 if err != nil {
151 t.Fatalf("commit %s: %v\n%s", subject, err, out)
152 }
153}