Something went wrong. Try again.
Monorepo for Tangled
Something went wrong. Try again.
2.2 kB · 65 lines
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566package knotstream
import ( "testing"
"github.com/samber/lo" "tangled.org/core/knotfeed")
func TestSubscriptionTracksOnePositionPerFeed(t *testing.T) { const legacySeq = 1788245839553422000 atproto := func(seq int64) knotfeed.Cursor { return knotfeed.NewCursor(knotfeed.FeedAtproto, seq) } legacy := func(seq int64) knotfeed.Cursor { return knotfeed.NewCursor(knotfeed.FeedLegacy, seq) }
for _, tt := range []struct { name string resume knotfeed.Cursor seen []knotfeed.Cursor applied []knotfeed.Cursor wantLast knotfeed.Cursor wantApplied knotfeed.Cursor }{ { name: "applied rises to the high water mark", resume: atproto(10), applied: []knotfeed.Cursor{atproto(40), atproto(20)}, wantLast: atproto(10), wantApplied: atproto(40), }, { name: "straggler from the feed we left is dropped", resume: legacy(legacySeq), seen: []knotfeed.Cursor{atproto(500)}, applied: []knotfeed.Cursor{legacy(legacySeq + 1)}, wantLast: atproto(500), wantApplied: atproto(0), }, { name: "changing feeds clears the applied position", resume: legacy(legacySeq), seen: []knotfeed.Cursor{atproto(7)}, applied: []knotfeed.Cursor{atproto(7)}, wantLast: atproto(7), wantApplied: atproto(7), }, { name: "cursor from another feed resets the subscription", resume: atproto(500), seen: []knotfeed.Cursor{legacy(legacySeq)}, wantLast: legacy(legacySeq), wantApplied: legacy(0), }, } { t.Run(tt.name, func(t *testing.T) { sub := subscription{hostname: "oyster.cafe"} sub.Resume(tt.resume) lo.ForEach(tt.seen, func(c knotfeed.Cursor, _ int) { sub.Seen(c) }) lo.ForEach(tt.applied, func(c knotfeed.Cursor, _ int) { sub.MarkApplied(c) })
if got := sub.Last(); got != tt.wantLast { t.Errorf("last = %v/%d, want %v/%d", got.Feed(), got.Seq(), tt.wantLast.Feed(), tt.wantLast.Seq()) } if got := sub.HostCursor(); got.Hostname != "oyster.cafe" || got.Cursor != tt.wantApplied { t.Errorf("host cursor = %s at %v/%d, want oyster.cafe at %v/%d", got.Hostname, got.Cursor.Feed(), got.Cursor.Seq(), tt.wantApplied.Feed(), tt.wantApplied.Seq()) } }) }}