| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/access" |
| 9 | ) |
| 10 | |
| 11 | func TestReposSortedByLastPush(t *testing.T) { |
| 12 | s := openStore(t) |
| 13 | ctx := context.Background() |
| 14 | tim, err := s.UpsertUser(ctx, 1, "tim", "", false) |
| 15 | if err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | mk := func(kind string, owner int64, name string) Repo { |
| 19 | r, err := s.CreateRepo(ctx, kind, owner, name, "", "master", "public") |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | return r |
| 24 | } |
| 25 | a, b, c, d := mk("user", tim.ID, "alpha"), mk("user", tim.ID, "beta"), mk("user", tim.ID, "Gamma"), mk("user", tim.ID, "delta") |
| 26 | base := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC) |
| 27 | _ = s.TouchPushed(ctx, a.ID, base) |
| 28 | _ = s.TouchPushed(ctx, c.ID, base.Add(2*time.Hour)) |
| 29 | _ = s.TouchPushed(ctx, d.ID, base.Add(2*time.Hour)) |
| 30 | // Een oudere tijd overschrijft een nieuwere niet. |
| 31 | _ = s.TouchPushed(ctx, c.ID, base.Add(-time.Hour)) |
| 32 | _ = b |
| 33 | list, err := s.ReposByOwner(ctx, tim.ID) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | var got []string |
| 38 | for _, r := range list { |
| 39 | got = append(got, r.Name) |
| 40 | } |
| 41 | want := []string{"delta", "Gamma", "alpha", "beta"} |
| 42 | for i := range want { |
| 43 | if got[i] != want[i] { |
| 44 | t.Fatalf("volgorde %v, wil %v", got, want) |
| 45 | } |
| 46 | } |
| 47 | if !list[0].PushedAt.Equal(base.Add(2*time.Hour)) || !list[3].PushedAt.IsZero() { |
| 48 | t.Fatalf("pushed_at: %v %v", list[0].PushedAt, list[3].PushedAt) |
| 49 | } |
| 50 | SortReposByPush(list[:0]) |
| 51 | rev := []Repo{list[3], list[2], list[1], list[0]} |
| 52 | SortReposByPush(rev) |
| 53 | for i := range want { |
| 54 | if rev[i].Name != want[i] { |
| 55 | t.Fatalf("SortReposByPush %v", rev) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | g, err := s.CreateGroup(ctx, tim.ID, "Crew", "crew", "", access.RoleDeveloper) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | x, y := mk("group", g.ID, "x"), mk("group", g.ID, "y") |
| 64 | _ = s.TouchPushed(ctx, y.ID, base) |
| 65 | gl, _ := s.ReposByGroup(ctx, g.ID) |
| 66 | if len(gl) != 2 || gl[0].ID != y.ID || gl[1].ID != x.ID { |
| 67 | t.Fatalf("groep %+v", gl) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestGroupsSortedByLastPush(t *testing.T) { |
| 72 | s := openStore(t) |
| 73 | ctx := context.Background() |
| 74 | tim, _ := s.UpsertUser(ctx, 1, "tim", "", false) |
| 75 | a, _ := s.CreateGroup(ctx, tim.ID, "Alpha", "alpha", "", access.RoleDeveloper) |
| 76 | b, _ := s.CreateGroup(ctx, tim.ID, "Beta", "beta", "", access.RoleDeveloper) |
| 77 | c, _ := s.CreateGroup(ctx, tim.ID, "Gamma", "gamma", "", access.RoleDeveloper) |
| 78 | ra, _ := s.CreateRepo(ctx, "group", a.ID, "x", "", "master", "public") |
| 79 | rc, _ := s.CreateRepo(ctx, "group", c.ID, "y", "", "master", "public") |
| 80 | rc2, _ := s.CreateRepo(ctx, "group", c.ID, "z", "", "master", "public") |
| 81 | base := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC) |
| 82 | _ = s.TouchPushed(ctx, ra.ID, base) |
| 83 | _ = s.TouchPushed(ctx, rc.ID, base.Add(-time.Hour)) |
| 84 | _ = s.TouchPushed(ctx, rc2.ID, base.Add(time.Hour)) |
| 85 | at, err := s.GroupPushTimes(ctx) |
| 86 | if err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | list := []Group{a, b, c} |
| 90 | SortGroupsByPush(list, at) |
| 91 | if list[0].ID != c.ID || list[1].ID != a.ID || list[2].ID != b.ID { |
| 92 | t.Fatalf("volgorde %s %s %s", list[0].Name, list[1].Name, list[2].Name) |
| 93 | } |
| 94 | } |