|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package service |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + |
| 21 | + pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestReferrerPaginationOptsFromProto(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + in *pb.CursorPaginationRequest |
| 32 | + wantLimit int |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "nil request applies default page size", |
| 36 | + in: nil, |
| 37 | + wantLimit: defaultReferrerPageSize, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "empty request applies default page size", |
| 41 | + in: &pb.CursorPaginationRequest{}, |
| 42 | + wantLimit: defaultReferrerPageSize, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "zero limit applies default page size", |
| 46 | + in: &pb.CursorPaginationRequest{Limit: 0}, |
| 47 | + wantLimit: defaultReferrerPageSize, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "explicit limit is honored", |
| 51 | + in: &pb.CursorPaginationRequest{Limit: 50}, |
| 52 | + wantLimit: 50, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "limit of 1 is honored", |
| 56 | + in: &pb.CursorPaginationRequest{Limit: 1}, |
| 57 | + wantLimit: 1, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "negative limit falls through to default page size", |
| 61 | + in: &pb.CursorPaginationRequest{Limit: -5}, |
| 62 | + wantLimit: defaultReferrerPageSize, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "proto max limit of 100 is honored", |
| 66 | + in: &pb.CursorPaginationRequest{Limit: 100}, |
| 67 | + wantLimit: 100, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tc := range tests { |
| 72 | + t.Run(tc.name, func(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + |
| 75 | + opts, err := referrerPaginationOptsFromProto(tc.in) |
| 76 | + require.NoError(t, err) |
| 77 | + require.NotNil(t, opts, "pagination options must always be returned so the response is bounded") |
| 78 | + assert.Equal(t, tc.wantLimit, opts.Limit) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
0 commit comments