54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package production
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/cursor"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto"
|
|
)
|
|
|
|
// TestService_messagePage checks boundaries and signs the last returned item, not lookahead.
|
|
func TestService_messagePage(t *testing.T) {
|
|
for _, count := range []int{0, 2, 3} {
|
|
names := map[int]string{0: "Positive: empty page", 2: "Positive: exact limit is final", 3: "Positive: lookahead creates continuation"}
|
|
t.Run(names[count], func(t *testing.T) {
|
|
// Arrange.
|
|
svc := newStubService(t, newStoreStub(t))
|
|
items := make([]dto.MessageDTO, count)
|
|
for i := range items {
|
|
items[i] = dto.MessageDTO{ID: uuid.New(), Text: "message", CreatedAt: time.Unix(int64(i+1), 0).UTC()}
|
|
}
|
|
expected := items[:min(count, 2)]
|
|
|
|
// Act.
|
|
page, err := svc.messagePage(items, pagination{limit: 2, resource: "messages:chat"})
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(page.Items, expected) {
|
|
t.Fatalf("page=%v, want=%v", page.Items, expected)
|
|
}
|
|
if count <= 2 {
|
|
if page.NextCursor != nil {
|
|
t.Fatal("final page has a continuation")
|
|
}
|
|
return
|
|
}
|
|
if page.NextCursor == nil {
|
|
t.Fatal("lookahead did not produce continuation")
|
|
}
|
|
decoded, err := svc.cursors.Decode(*page.NextCursor, "messages:chat")
|
|
want := cursor.Position{ID: items[1].ID, CreatedAt: items[1].CreatedAt}
|
|
if err != nil || decoded != want {
|
|
t.Fatalf("cursor=%v, want=%v, error=%v", decoded, want, err)
|
|
}
|
|
})
|
|
}
|
|
}
|