package production import ( "context" "errors" "reflect" "testing" "time" "github.com/google/uuid" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/cursor" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service" ) // TestService_ChatsGetPage validates access and pagination before issuing a typed store query. func TestService_ChatsGetPage(t *testing.T) { t.Run("Positive: continuation uses scope, position and lookahead", func(t *testing.T) { // Arrange. repo := newStoreStub(t) actor := uuid.New() allowIdentity(repo, actor) previous := cursor.Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()} records := []dto.ChatDTO{ {ID: uuid.New(), CreatedAt: time.Unix(1700000001, 0).UTC()}, {ID: uuid.New(), CreatedAt: time.Unix(1700000002, 0).UTC()}, {ID: uuid.New(), CreatedAt: time.Unix(1700000003, 0).UTC()}, } calls := 0 repo.chats = func(_ context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) { calls++ expected := dto.PositionDTO{ID: previous.ID, CreatedAt: previous.CreatedAt} if id != actor || position != expected || limit != 3 { t.Fatalf("wrong page query: id=%s, position=%v, limit=%d", id, position, limit) } return records, nil } svc := newStubService(t, repo) resource := "chats:" + actor.String() token, err := svc.cursors.Encode(resource, previous) if err != nil { t.Fatal(err) } input := dto.PageRequestDTO{UserID: actor.String(), Limit: "2", Cursor: token} // Act. got, err := svc.ChatsGetPage(t.Context(), input) // Assert. if err != nil { t.Fatal(err) } if calls != 1 || !reflect.DeepEqual(got.Items, records[:2]) || got.NextCursor == nil { t.Fatalf("calls=%d, page=%+v", calls, got) } position, err := svc.cursors.Decode(*got.NextCursor, resource) expected := cursor.Position{ID: records[1].ID, CreatedAt: records[1].CreatedAt} if err != nil || position != expected { t.Fatalf("next position=%v, want=%v, error=%v", position, expected, err) } }) t.Run("Positive: empty history uses default limit and no continuation", func(t *testing.T) { // Arrange. repo := newStoreStub(t) actor := uuid.New() allowIdentity(repo, actor) calls := 0 repo.chats = func(_ context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) { calls++ if id != actor || position != (dto.PositionDTO{}) || limit != 51 { t.Fatalf("wrong initial page query: id=%s, position=%v, limit=%d", id, position, limit) } return []dto.ChatDTO{}, nil } svc := newStubService(t, repo) input := dto.PageRequestDTO{UserID: actor.String()} // Act. got, err := svc.ChatsGetPage(t.Context(), input) // Assert. if err != nil { t.Fatal(err) } if calls != 1 || got.Items == nil || len(got.Items) != 0 || got.NextCursor != nil { t.Fatalf("calls=%d, page=%+v", calls, got) } }) t.Run("Negative: cursor for another resource skips page lookup", func(t *testing.T) { // Arrange. repo := newStoreStub(t) actor := uuid.New() allowIdentity(repo, actor) svc := newStubService(t, repo) token, err := svc.cursors.Encode("another-resource", cursor.Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()}) if err != nil { t.Fatal(err) } input := dto.PageRequestDTO{UserID: actor.String(), Cursor: token} // Act. got, err := svc.ChatsGetPage(t.Context(), input) // Assert. if !errors.Is(err, service.ErrInvalid) || len(got.Items) != 0 || got.NextCursor != nil { t.Fatalf("page=%+v, error=%v", got, err) } }) t.Run("Negative: codec failure skips page lookup", func(t *testing.T) { // Arrange. repo := newStoreStub(t) actor := uuid.New() allowIdentity(repo, actor) failure := errors.New("cursor unavailable") svc := newStubService(t, repo) svc.cursors = failingCursor{failure: failure} input := dto.PageRequestDTO{UserID: actor.String(), Cursor: "token"} // Act. got, err := svc.ChatsGetPage(t.Context(), input) // Assert. if !errors.Is(err, failure) || len(got.Items) != 0 || got.NextCursor != nil { t.Fatalf("page=%+v, error=%v", got, err) } }) t.Run("Negative: page storage fails", func(t *testing.T) { // Arrange. repo := newStoreStub(t) actor := uuid.New() allowIdentity(repo, actor) failure := errors.New("page unavailable") repo.chats = func(_ context.Context, _ uuid.UUID, _ dto.PositionDTO, _ int) ([]dto.ChatDTO, error) { return nil, failure } svc := newStubService(t, repo) input := dto.PageRequestDTO{UserID: actor.String()} // Act. got, err := svc.ChatsGetPage(t.Context(), input) // Assert. if !errors.Is(err, failure) || len(got.Items) != 0 || got.NextCursor != nil { t.Fatalf("page=%+v, error=%v", got, err) } }) t.Run("Negative: malformed identity skips all storage", func(t *testing.T) { // Arrange. svc := newStubService(t, newStoreStub(t)) input := dto.PageRequestDTO{UserID: "invalid", ChatID: uuid.NewString()} // Act. got, err := svc.ChatsGetPage(t.Context(), input) // Assert. if !errors.Is(err, service.ErrUnauthorized) || len(got.Items) != 0 || got.NextCursor != nil { t.Fatalf("page=%+v, error=%v", got, err) } }) } // TestService_ChatsGetPage_InvalidLimit rejects invalid limits before reading stored pages. func TestService_ChatsGetPage_InvalidLimit(t *testing.T) { cases := []struct { name string limit string }{ {"Negative: zero limit", "0"}, {"Negative: negative limit", "-1"}, {"Negative: limit above maximum", "101"}, {"Negative: nonnumeric limit", "text"}, {"Negative: fractional limit", "1.5"}, {"Negative: integer overflow", "99999999999999999999999999"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { // Arrange. repo := newStoreStub(t) actor := uuid.New() allowIdentity(repo, actor) svc := newStubService(t, repo) input := dto.PageRequestDTO{UserID: actor.String(), Limit: tc.limit} // Act. got, err := svc.ChatsGetPage(t.Context(), input) // Assert. if !errors.Is(err, service.ErrInvalid) || len(got.Items) != 0 || got.NextCursor != nil { t.Fatalf("page=%+v, error=%v", got, err) } }) } }