Something went wrong. Try again.
a bluesky feed generator framework
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199package store
import ( "context" "math" "path/filepath" "slices" "testing"
"tangled.org/edavis.dev/updraft/internal/ranking")
func openTestStore(t *testing.T) *Store { t.Helper() s, err := Open(filepath.Join(t.TempDir(), "test.db")) if err != nil { t.Fatalf("Open: %v", err) } t.Cleanup(func() { s.Close() }) return s}
func mustTx(t *testing.T, s *Store, fn func(*Tx) error) { t.Helper() if err := s.WithTx(context.Background(), fn); err != nil { t.Fatalf("WithTx: %v", err) }}
func insert(t *testing.T, s *Store, feed, uri string, indexedAtUS int64) { t.Helper() mustTx(t, s, func(tx *Tx) error { return tx.InsertPost(context.Background(), feed, uri, "cid", indexedAtUS) })}
func uris(posts []Post) []string { out := make([]string, len(posts)) for i, p := range posts { out[i] = p.URI } return out}
func TestInsertPostIsIdempotent(t *testing.T) { s := openTestStore(t) insert(t, s, "f", "at://a", 100) insert(t, s, "f", "at://a", 200)
posts, err := s.FeedPosts(context.Background(), "f") if err != nil { t.Fatal(err) } if len(posts) != 1 || posts[0].IndexedAtUS != 100 || posts[0].Heat != ranking.InitialHeat { t.Fatalf("posts = %+v, want single original row with initial heat", posts) }}
func TestApplyLikeOnlyUpdatesTrackedFeeds(t *testing.T) { s := openTestStore(t) const hourUS = int64(3_600_000_000) insert(t, s, "hot", "at://a", 0) insert(t, s, "plain", "at://a", 0)
gravity := math.Ln2 // one-hour half-life mustTx(t, s, func(tx *Tx) error { return tx.ApplyLike(context.Background(), "at://a", hourUS, map[string]float64{"hot": gravity}) })
hot, _ := s.FeedPosts(context.Background(), "hot") if hot[0].Score != 1 || math.Abs(hot[0].Heat-1.5) > 1e-9 || hot[0].HeatUpdatedAtUS != hourUS { t.Errorf("hot row = %+v, want score 1, heat 1.5, updated at %d", hot[0], hourUS) } plain, _ := s.FeedPosts(context.Background(), "plain") if plain[0].Score != 0 || plain[0].Heat != ranking.InitialHeat { t.Errorf("plain row = %+v, want untouched", plain[0]) }}
func TestApplyLikeUnknownURIIsNoOp(t *testing.T) { s := openTestStore(t) mustTx(t, s, func(tx *Tx) error { return tx.ApplyLike(context.Background(), "at://missing", 1, map[string]float64{"hot": 1}) })}
func TestDeletePostRemovesFromAllFeeds(t *testing.T) { s := openTestStore(t) insert(t, s, "f1", "at://a", 1) insert(t, s, "f2", "at://a", 1) insert(t, s, "f1", "at://b", 1) mustTx(t, s, func(tx *Tx) error { return tx.DeletePost(context.Background(), "at://a") })
got, err := s.PostURIs(context.Background(), []string{"f1", "f2"}) if err != nil { t.Fatal(err) } if !slices.Equal(got, []string{"at://b"}) { t.Errorf("remaining = %v, want [at://b]", got) }}
func TestPurgeDeletesOnlyOldPostsInFeed(t *testing.T) { s := openTestStore(t) insert(t, s, "f", "at://old", 10) insert(t, s, "f", "at://new", 30) insert(t, s, "other", "at://old", 10)
n, err := s.Purge(context.Background(), "f", 20) if err != nil { t.Fatal(err) } if n != 1 { t.Errorf("purged %d, want 1", n) } f, _ := s.FeedPosts(context.Background(), "f") other, _ := s.FeedPosts(context.Background(), "other") if !slices.Equal(uris(f), []string{"at://new"}) || len(other) != 1 { t.Errorf("f = %v, other = %v; want only at://old purged from f", uris(f), uris(other)) }}
func TestPostURIsFiltersByFeed(t *testing.T) { s := openTestStore(t) insert(t, s, "a", "at://1", 1) insert(t, s, "b", "at://1", 1) insert(t, s, "b", "at://2", 1) insert(t, s, "c", "at://3", 1)
got, err := s.PostURIs(context.Background(), []string{"a", "b"}) if err != nil { t.Fatal(err) } slices.Sort(got) if !slices.Equal(got, []string{"at://1", "at://2"}) { t.Errorf("PostURIs = %v, want [at://1 at://2]", got) } if got, _ := s.PostURIs(context.Background(), nil); got != nil { t.Errorf("PostURIs(nil) = %v, want nil", got) }}
func TestCursorRoundTrip(t *testing.T) { s := openTestStore(t) ctx := context.Background() if seq, err := s.Cursor(ctx); err != nil || seq != 0 { t.Fatalf("initial Cursor = %d, %v; want 0, nil", seq, err) } mustTx(t, s, func(tx *Tx) error { return tx.SaveCursor(ctx, 42) }) mustTx(t, s, func(tx *Tx) error { return tx.SaveCursor(ctx, 99) }) if seq, err := s.Cursor(ctx); err != nil || seq != 99 { t.Fatalf("Cursor = %d, %v; want 99, nil", seq, err) }}
func TestWithTxRollsBackOnError(t *testing.T) { s := openTestStore(t) ctx := context.Background() err := s.WithTx(ctx, func(tx *Tx) error { if err := tx.InsertPost(ctx, "f", "at://a", "cid", 1); err != nil { return err } return context.Canceled }) if err == nil { t.Fatal("WithTx returned nil, want error") } if posts, _ := s.FeedPosts(ctx, "f"); len(posts) != 0 { t.Errorf("posts = %v, want rollback", posts) }}
func TestChronologicalPagePaginates(t *testing.T) { s := openTestStore(t) ctx := context.Background() // Two posts share indexed_at 20 to exercise the URI tiebreak. insert(t, s, "f", "at://a", 10) insert(t, s, "f", "at://b", 20) insert(t, s, "f", "at://c", 20) insert(t, s, "f", "at://d", 30) insert(t, s, "other", "at://z", 40)
page1, err := s.ChronologicalPage(ctx, "f", nil, 2) if err != nil { t.Fatal(err) } if !slices.Equal(uris(page1), []string{"at://d", "at://c"}) { t.Fatalf("page1 = %v", uris(page1)) } last := page1[len(page1)-1] page2, err := s.ChronologicalPage(ctx, "f", &ChronologicalCursor{IndexedAtUS: last.IndexedAtUS, URI: last.URI}, 2) if err != nil { t.Fatal(err) } if !slices.Equal(uris(page2), []string{"at://b", "at://a"}) { t.Fatalf("page2 = %v", uris(page2)) }}