|
| 1 | +// Copyright 2017 The Gitea Authors. All rights reserved. |
| 2 | +// Copyright 2014 The Gogs Authors. All rights reserved. |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +package repo |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/asymkey" |
| 12 | + git_model "code.gitea.io/gitea/models/git" |
| 13 | + "code.gitea.io/gitea/models/user" |
| 14 | + "code.gitea.io/gitea/modules/git" |
| 15 | + "code.gitea.io/gitea/modules/timeutil" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +func TestGroupCommitsByDate(t *testing.T) { |
| 21 | + // Create test data |
| 22 | + // These two commits represent the same moment but in different timezones |
| 23 | + // commit1: 2025-04-10T08:00:00+08:00 |
| 24 | + // commit2: 2025-04-09T23:00:00-01:00 |
| 25 | + // Their UTC time is both 2025-04-10T00:00:00Z |
| 26 | + |
| 27 | + // Create the first commit (Asia timezone +8) |
| 28 | + asiaTimezone := time.FixedZone("Asia/Shanghai", 8*60*60) |
| 29 | + commit1Time := time.Date(2025, 4, 10, 8, 0, 0, 0, asiaTimezone) |
| 30 | + commit1 := &git_model.SignCommitWithStatuses{ |
| 31 | + SignCommit: &asymkey.SignCommit{ |
| 32 | + UserCommit: &user.UserCommit{ |
| 33 | + Commit: &git.Commit{ |
| 34 | + Committer: &git.Signature{ |
| 35 | + When: commit1Time, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + // Create the second commit (Western timezone -1) |
| 43 | + westTimezone := time.FixedZone("West", -1*60*60) |
| 44 | + commit2Time := time.Date(2025, 4, 9, 23, 0, 0, 0, westTimezone) |
| 45 | + commit2 := &git_model.SignCommitWithStatuses{ |
| 46 | + SignCommit: &asymkey.SignCommit{ |
| 47 | + UserCommit: &user.UserCommit{ |
| 48 | + Commit: &git.Commit{ |
| 49 | + Committer: &git.Signature{ |
| 50 | + When: commit2Time, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + // Verify that the two timestamps actually represent the same moment |
| 58 | + assert.Equal(t, commit1Time.Unix(), commit2Time.Unix(), "The two commits should have the same Unix timestamp") |
| 59 | + |
| 60 | + // Test the modified grouping behavior |
| 61 | + commits := []*git_model.SignCommitWithStatuses{commit1, commit2} |
| 62 | + grouped := GroupCommitsByDate(commits) |
| 63 | + |
| 64 | + // Output the grouping results for observation |
| 65 | + t.Logf("Number of grouped results: %d", len(grouped)) |
| 66 | + for i, group := range grouped { |
| 67 | + t.Logf("Group %d: Date %s, Number of commits %d", i, time.Unix(int64(group.Date), 0).Format("2006-01-02"), len(group.Commits)) |
| 68 | + for j, c := range group.Commits { |
| 69 | + t.Logf(" Commit %d: Time %s", j, c.SignCommit.UserCommit.Commit.Committer.When.Format(time.RFC3339)) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // After modification, these two commits should be grouped together as they are on the same day in UTC timezone |
| 74 | + assert.Len(t, grouped, 1, "After modification, the two commits should be grouped together") |
| 75 | + |
| 76 | + // Verify the group date (should be 2025-04-10, the date in UTC timezone) |
| 77 | + utcDate := time.Date(2025, 4, 10, 0, 0, 0, 0, time.UTC) |
| 78 | + assert.Equal(t, timeutil.TimeStamp(utcDate.Unix()), grouped[0].Date) |
| 79 | + assert.Len(t, grouped[0].Commits, 2) |
| 80 | + |
| 81 | + // Verify that both commits are in this group |
| 82 | + commitMap := make(map[*git_model.SignCommitWithStatuses]bool) |
| 83 | + for _, c := range grouped[0].Commits { |
| 84 | + commitMap[c] = true |
| 85 | + } |
| 86 | + assert.True(t, commitMap[commit1], "The first commit should be in the group") |
| 87 | + assert.True(t, commitMap[commit2], "The second commit should be in the group") |
| 88 | + |
| 89 | + // Add a commit with a different date for testing |
| 90 | + nextDayTimezone := time.FixedZone("NextDay", 0) |
| 91 | + commit3Time := time.Date(2025, 4, 11, 0, 0, 0, 0, nextDayTimezone) |
| 92 | + commit3 := &git_model.SignCommitWithStatuses{ |
| 93 | + SignCommit: &asymkey.SignCommit{ |
| 94 | + UserCommit: &user.UserCommit{ |
| 95 | + Commit: &git.Commit{ |
| 96 | + Committer: &git.Signature{ |
| 97 | + When: commit3Time, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + // Test with commits from different dates |
| 105 | + commits = append(commits, commit3) |
| 106 | + grouped = GroupCommitsByDate(commits) |
| 107 | + |
| 108 | + // Now there should be two groups |
| 109 | + assert.Len(t, grouped, 2, "There should be two different date groups") |
| 110 | + |
| 111 | + // Verify date sorting (descending, most recent date first) |
| 112 | + assert.True(t, time.Unix(int64(grouped[0].Date), 0).After(time.Unix(int64(grouped[1].Date), 0)), |
| 113 | + "Dates should be sorted in descending order") |
| 114 | +} |
0 commit comments