package store import ( "context" "testing" "time" "gitlab.com/Timfa/bluegit/internal/access" ) func TestReposSortedByLastPush(t *testing.T) { s := openStore(t) ctx := context.Background() tim, err := s.UpsertUser(ctx, 1, "tim", "", false) if err != nil { t.Fatal(err) } mk := func(kind string, owner int64, name string) Repo { r, err := s.CreateRepo(ctx, kind, owner, name, "", "master", "public") if err != nil { t.Fatal(err) } return r } a, b, c, d := mk("user", tim.ID, "alpha"), mk("user", tim.ID, "beta"), mk("user", tim.ID, "Gamma"), mk("user", tim.ID, "delta") base := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC) _ = s.TouchPushed(ctx, a.ID, base) _ = s.TouchPushed(ctx, c.ID, base.Add(2*time.Hour)) _ = s.TouchPushed(ctx, d.ID, base.Add(2*time.Hour)) // Een oudere tijd overschrijft een nieuwere niet. _ = s.TouchPushed(ctx, c.ID, base.Add(-time.Hour)) _ = b list, err := s.ReposByOwner(ctx, tim.ID) if err != nil { t.Fatal(err) } var got []string for _, r := range list { got = append(got, r.Name) } want := []string{"delta", "Gamma", "alpha", "beta"} for i := range want { if got[i] != want[i] { t.Fatalf("volgorde %v, wil %v", got, want) } } if !list[0].PushedAt.Equal(base.Add(2*time.Hour)) || !list[3].PushedAt.IsZero() { t.Fatalf("pushed_at: %v %v", list[0].PushedAt, list[3].PushedAt) } SortReposByPush(list[:0]) rev := []Repo{list[3], list[2], list[1], list[0]} SortReposByPush(rev) for i := range want { if rev[i].Name != want[i] { t.Fatalf("SortReposByPush %v", rev) } } g, err := s.CreateGroup(ctx, tim.ID, "Crew", "crew", "", access.RoleDeveloper) if err != nil { t.Fatal(err) } x, y := mk("group", g.ID, "x"), mk("group", g.ID, "y") _ = s.TouchPushed(ctx, y.ID, base) gl, _ := s.ReposByGroup(ctx, g.ID) if len(gl) != 2 || gl[0].ID != y.ID || gl[1].ID != x.ID { t.Fatalf("groep %+v", gl) } } func TestGroupsSortedByLastPush(t *testing.T) { s := openStore(t) ctx := context.Background() tim, _ := s.UpsertUser(ctx, 1, "tim", "", false) a, _ := s.CreateGroup(ctx, tim.ID, "Alpha", "alpha", "", access.RoleDeveloper) b, _ := s.CreateGroup(ctx, tim.ID, "Beta", "beta", "", access.RoleDeveloper) c, _ := s.CreateGroup(ctx, tim.ID, "Gamma", "gamma", "", access.RoleDeveloper) ra, _ := s.CreateRepo(ctx, "group", a.ID, "x", "", "master", "public") rc, _ := s.CreateRepo(ctx, "group", c.ID, "y", "", "master", "public") rc2, _ := s.CreateRepo(ctx, "group", c.ID, "z", "", "master", "public") base := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC) _ = s.TouchPushed(ctx, ra.ID, base) _ = s.TouchPushed(ctx, rc.ID, base.Add(-time.Hour)) _ = s.TouchPushed(ctx, rc2.ID, base.Add(time.Hour)) at, err := s.GroupPushTimes(ctx) if err != nil { t.Fatal(err) } list := []Group{a, b, c} SortGroupsByPush(list, at) if list[0].ID != c.ID || list[1].ID != a.ID || list[2].ID != b.ID { t.Fatalf("volgorde %s %s %s", list[0].Name, list[1].Name, list[2].Name) } }