diff --git a/coderd/users_test.go b/coderd/users_test.go index 0e08462f7537b..25315ec1ec64b 100644 --- a/coderd/users_test.go +++ b/coderd/users_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "sort" "testing" "github.com/google/uuid" @@ -597,6 +598,13 @@ func TestPaginatedUsers(t *testing.T) { } } + // Sorting the users will sort by (created_at, uuid). This is to handle + // the off case that created_at is identical for 2 users. + // This is a really rare case in production, but does happen in unit tests + // due to the fake database being in memory and exceptionally quick. + sortUsers(allUsers) + sortUsers(specialUsers) + assertPagination(ctx, t, client, 10, allUsers, nil) assertPagination(ctx, t, client, 5, allUsers, nil) assertPagination(ctx, t, client, 3, allUsers, nil) @@ -679,3 +687,13 @@ func assertPagination(ctx context.Context, t *testing.T, client *codersdk.Client count += len(page) } } + +// sortUsers sorts by (created_at, id) +func sortUsers(users []codersdk.User) { + sort.Slice(users, func(i, j int) bool { + if users[i].CreatedAt.Equal(users[j].CreatedAt) { + return users[i].ID.String() < users[j].ID.String() + } + return users[i].CreatedAt.Before(users[j].CreatedAt) + }) +}