47 lines
1.5 KiB
Go
47 lines
1.5 KiB
Go
package creator
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type pageCollector struct {
|
|
works map[string]WorkPage
|
|
comments map[string]CommentPage
|
|
}
|
|
|
|
func (c pageCollector) ListWorks(_ context.Context, _ string, cursor string) (WorkPage, error) {
|
|
return c.works[cursor], nil
|
|
}
|
|
func (c pageCollector) ListTopLevelComments(_ context.Context, workKey, cursor string) (CommentPage, error) {
|
|
return c.comments[workKey+":"+cursor], nil
|
|
}
|
|
|
|
func TestCollectPagesRequiresAdvancingCursor(t *testing.T) {
|
|
collector := pageCollector{works: map[string]WorkPage{
|
|
"": {Items: []WorkInput{{WorkKey: "one"}}, NextCursor: "next", HasMore: true},
|
|
"next": {Items: []WorkInput{{WorkKey: "two"}}, HasMore: false},
|
|
}}
|
|
items, err := CollectWorkPages(context.Background(), collector)
|
|
if err != nil || len(items) != 2 {
|
|
t.Fatalf("collect pages: %#v err=%v", items, err)
|
|
}
|
|
bad := pageCollector{works: map[string]WorkPage{"": {HasMore: true}}}
|
|
if _, err := CollectWorkPages(context.Background(), bad); !errors.Is(err, ErrInvalid) {
|
|
t.Fatalf("missing cursor error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCollectionWindowIsFixedInUTC(t *testing.T) {
|
|
now := time.Date(2026, 3, 2, 8, 0, 0, 0, time.FixedZone("CST", 8*60*60))
|
|
start, end, err := NewCollectionWindow(now, 30)
|
|
if err != nil || !end.Equal(now.UTC()) || !start.Equal(now.UTC().Add(-30*24*time.Hour)) {
|
|
t.Fatalf("window = %s..%s err=%v", start, end, err)
|
|
}
|
|
if InWindow(end.Add(time.Nanosecond), start, end) || !InWindow(start, start, end) {
|
|
t.Fatal("window must include both exact boundaries only")
|
|
}
|
|
}
|