75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package production
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"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"
|
|
)
|
|
|
|
type failingCursor struct {
|
|
failure error
|
|
}
|
|
|
|
// Encode injects a signing failure at the service dependency boundary.
|
|
func (codec failingCursor) Encode(_ string, _ cursor.Position) (string, error) {
|
|
return "", codec.failure
|
|
}
|
|
|
|
// Decode injects a verification failure at the service dependency boundary.
|
|
func (codec failingCursor) Decode(_, _ string) (cursor.Position, error) {
|
|
return cursor.Position{}, codec.failure
|
|
}
|
|
|
|
// TestService_parsePage translates invalid tokens while retaining unexpected dependency failures.
|
|
func TestService_parsePage(t *testing.T) {
|
|
failure := errors.New("codec unavailable")
|
|
cases := []struct {
|
|
name string
|
|
cause error
|
|
want error
|
|
}{
|
|
{"Negative: invalid token", cursor.ErrInvalid, service.ErrInvalid},
|
|
{"Negative: unavailable codec", failure, failure},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
// Arrange.
|
|
svc := &Service{cursors: failingCursor{failure: tc.cause}}
|
|
input := dto.PageRequestDTO{Cursor: "supplied-token"}
|
|
|
|
// Act.
|
|
page, err := svc.parsePage(input, "messages:chat")
|
|
|
|
// Assert.
|
|
if !errors.Is(err, tc.want) {
|
|
t.Fatalf("error=%v, want=%v", err, tc.want)
|
|
}
|
|
if page != (pagination{}) {
|
|
t.Fatalf("failed cursor produced a page: %+v", page)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestService_messagePage_EncodeFailure returns no partial page when signing fails.
|
|
func TestService_messagePage_EncodeFailure(t *testing.T) {
|
|
// Arrange.
|
|
failure := errors.New("signing unavailable")
|
|
svc := &Service{cursors: failingCursor{failure: failure}}
|
|
items := []dto.MessageDTO{{Text: "first"}, {Text: "lookahead"}}
|
|
|
|
// Act.
|
|
page, err := svc.messagePage(items, pagination{limit: 1, resource: "messages:chat"})
|
|
|
|
// Assert.
|
|
if !errors.Is(err, failure) {
|
|
t.Fatalf("lost signing failure: %v", err)
|
|
}
|
|
if page.Items != nil || page.NextCursor != nil {
|
|
t.Fatalf("partial page escaped: %+v", page)
|
|
}
|
|
}
|