package server import ( "context" "net/http" "net/http/cookiejar" "net/http/httptest" "net/url" "os" "os/exec" "path/filepath" "strings" "testing" "gitlab.com/Timfa/bluegit/internal/auth" "gitlab.com/Timfa/bluegit/internal/config" "gitlab.com/Timfa/bluegit/internal/store" ) func TestCommitList(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git ontbreekt") } storage := t.TempDir() st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = st.Close() }) cfg := config.Config{ BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, Git: config.Git{Binary: "git", DefaultBranch: "master"}, } srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} ts := httptest.NewServer(srv.Handler()) t.Cleanup(ts.Close) jar, _ := cookiejar.New(nil) tim := &http.Client{Jar: jar} page := read(t, mustGet(t, tim, ts.URL+"/login")) page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{ "csrf": {csrfOf(t, page)}, "name": {"logboek"}, "visibility": {"public"}, })) page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{ "csrf": {csrfOf(t, page)}, "name": {"laptop"}, })) token := between(page, `

`, "

") remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/logboek.git" work := t.TempDir() gitPrep(t, work) writeFile(t, filepath.Join(work, "note.txt"), "oud\n") git(t, work, "add", "note.txt") gitAs(t, work, "Zoeker", "zoeker@example.com", "2026-09-01T12:00:00Z", "oudste") writeFile(t, filepath.Join(work, "note.txt"), "sept\n") git(t, work, "add", "note.txt") gitBody(t, work, "Zoeker", "zoeker@example.com", "2026-09-02T12:00:00Z", "september-commit", "tweede regel van de body") for i := 2; i <= 20; i++ { writeFile(t, filepath.Join(work, "note.txt"), strings.Repeat("x", i)+"\n") git(t, work, "add", "note.txt") msg := "c02" when := "2026-10-23T12:00:00Z" if i == 20 { msg = "nieuwste" } else { msg = "c" + two(i) } gitAs(t, work, "tim", "tim@users.git.timfalken.com", when, msg) } git(t, work, "remote", "add", "origin", remote) git(t, work, "push", "-u", "origin", "master") head := revHEAD(t, work) page = read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits")) for _, want := range []string{ "Commits", "logboek", `name="rev"`, `value="master"`, "Search by message or author", "23 October 2026", "2 September 2026", "nieuwste", "september-commit", "authored", `class="avatar ident"`, "Zoeker", `data-copy="` + head + `"`, `/-/commit/` + head, `/-/tree?rev=` + head, `data-commit-toggle`, "tweede regel van de body", "Load more", } { if !strings.Contains(page, want) { t.Fatalf("commitlijst mist %q", want) } } if strings.Contains(page, "Unverified") || strings.Contains(page, "oudste") { t.Fatalf("eerste pagina:\n%s", page) } if !strings.Contains(page, ">Load more") && !strings.Contains(page, "page=2") { t.Fatalf("geen volgende pagina:\n%s", page) } filtered := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits?q=september-commit")) if !strings.Contains(filtered, "september-commit") || strings.Contains(filtered, ">nieuwste") || !strings.Contains(filtered, "2 September 2026") { t.Fatalf("filter op bericht:\n%s", filtered) } byAuthor := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits?q=Zoeker")) if !strings.Contains(byAuthor, "september-commit") || !strings.Contains(byAuthor, "oudste") || strings.Contains(byAuthor, ">nieuwste") { t.Fatalf("filter op auteur:\n%s", byAuthor) } none := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits?q=geen-treffer")) if !strings.Contains(none, "No matching commits.") { t.Fatalf("lege filter:\n%s", none) } next := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits?page=2")) if !strings.Contains(next, ">oudste") || strings.Contains(next, ">nieuwste") || !strings.Contains(next, "Previous") || strings.Contains(next, ">Load more") { t.Fatalf("pagina 2:\n%s", next) } repo, err := st.RepoByPath(context.Background(), "tim", "logboek") if err != nil { t.Fatal(err) } if _, err := st.CreatePipeline(context.Background(), repo.ID, head, "master", "push", "tim", "nieuwste", []store.NewJob{{ Name: "build", Stage: "test", Script: "true", }}); err != nil { t.Fatal(err) } withPipe := read(t, mustGet(t, tim, ts.URL+"/tim/logboek/-/commits")) if !strings.Contains(withPipe, `class="status pending markonly"`) || !strings.Contains(withPipe, "/-/pipelines/") { t.Fatalf("pipeline-icoon:\n%s", withPipe) } } func two(n int) string { if n < 10 { return "0" + string(rune('0'+n)) } return string(rune('0'+n/10)) + string(rune('0'+n%10)) } func gitBody(t *testing.T, dir, name, email, date, subject, body string) { t.Helper() cmd := exec.Command("git", "commit", "-m", subject, "-m", body) cmd.Dir = dir cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME="+name, "GIT_AUTHOR_EMAIL="+email, "GIT_COMMITTER_NAME="+name, "GIT_COMMITTER_EMAIL="+email, "GIT_AUTHOR_DATE="+date, "GIT_COMMITTER_DATE="+date, ) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("commit %s: %v\n%s", subject, err, out) } }