Files

70 lines
2.3 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")
}
}
func TestPublishedAtCollectionWindowRejectsFutureItems(t *testing.T) {
start := time.Date(2026, 3, 1, 0, 0, 0, 0, time.UTC)
end := start.Add(24 * time.Hour)
before, atStart, atEnd, after := start.Add(-time.Nanosecond), start, end, end.Add(time.Nanosecond)
for _, test := range []struct {
name string
at *time.Time
want bool
}{
{name: "unknown", at: nil, want: true},
{name: "before", at: &before, want: false},
{name: "start", at: &atStart, want: true},
{name: "end", at: &atEnd, want: true},
{name: "future", at: &after, want: false},
} {
t.Run(test.name, func(t *testing.T) {
if got := publishedAtInCollectionWindow(test.at, start, end); got != test.want {
t.Fatalf("publishedAtInCollectionWindow(%v) = %v, want %v", test.at, got, test.want)
}
})
}
}