// SPDX-License-Identifier: AGPL-3.0-or-later package web import ( "bytes" "context" "errors" "fmt" "net/http" "net/http/httptest" "strings" "sync/atomic" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "bnema.dev/ermite/internal/domain" "bnema.dev/ermite/internal/ports" "bnema.dev/ermite/internal/ports/mocks" ) // pushedRepo is a public repository that was pushed to. func pushedRepo(t testing.TB, name string) ports.RepoView { t.Helper() v := publicRepo(t, name) v.PushedAt = time.Now().Add(-time.Hour) return v } // homeCommits are n commits, newest first, with distinct ids and subjects. func homeCommits(t testing.TB, n int) []domain.Commit { t.Helper() var out []domain.Commit for i := n; i >= 1; i-- { out = append(out, domain.Commit{ ID: objectID(t, fmt.Sprintf("%040x", i)), Summary: fmt.Sprintf("subject number %d", i), Committer: domain.Signature{Time: time.Now().Add(-time.Duration(i) * time.Minute)}, }) } return out } // homeBrowse is a BrowseService whose figures are always there: 1234 // commits, 7 of them recent, no tag. Tests that care set their own. func homeBrowse(t *testing.T) *mocks.BrowseService { t.Helper() b := mocks.NewBrowseService(t) b.EXPECT().RepoStats(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). Return(ports.RepoStats{Commits: 1234, Recent: 7}, nil).Maybe() return b } // Each row shows the figures of its repository: commits, recent commits, // latest tag and last activity; no branch name. func TestHomeRowFigures(t *testing.T) { t.Parallel() repo := repoName(t, "proj") head := objectID(t, fmt.Sprintf("%040x", 2)) now := time.Now() browse := mocks.NewBrowseService(t) browse.EXPECT().ListCommits(mock.Anything, mock.Anything, repo, mock.Anything).Return(ports.HistoryPage{Commits: homeCommits(t, 2)}, nil) browse.EXPECT().RepoStats(mock.Anything, mock.Anything, repo, head, now).Return(ports.RepoStats{ Commits: 12345, Recent: 42, LatestTag: &domain.TagInfo{Name: "v1.2.0", At: now.Add(-3 * time.Hour), CommitID: head}, Activity: make([]int, ports.ActivityDays), }, nil).Once() srv := newTestServer(t, Config{Repos: reposServing(t, pushedRepo(t, "proj")), Browse: browse}) view := pushedRepo(t, "proj") view.Topics = []string{"go", "git"} row := srv.homeRowOf(context.Background(), view, now, false) require.NotNil(t, row.Stats) out := renderRow(t, row) assert.Contains(t, out, "