From d7eaba677e03486043a843b4e430bacbeace7559 Mon Sep 17 00:00:00 2001 From: Kev Date: Sat, 14 Sep 2024 18:27:56 +0200 Subject: [PATCH] Refactor feed tests --- feed/feed_test.go | 673 +++++++++++++++++----------------------------- 1 file changed, 245 insertions(+), 428 deletions(-) diff --git a/feed/feed_test.go b/feed/feed_test.go index 9c21380e..23b90b6e 100644 --- a/feed/feed_test.go +++ b/feed/feed_test.go @@ -14,7 +14,7 @@ import ( "github.com/strideynet/bsky-furry-feed/tristate" ) -func TestChronologicalGenerator(t *testing.T) { +func TestGenerator(t *testing.T) { t.Parallel() ctx, cancel := context.WithCancel(context.Background()) @@ -48,506 +48,323 @@ func TestChronologicalGenerator(t *testing.T) { for _, opts := range []store.CreatePostOpts{ { - URI: fursuitPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"fursuit"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, + URI: fursuitPost, + Hashtags: []string{"fursuit"}, + HasMedia: true, }, { - URI: murrsuitPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"fursuit", "murrsuit"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, + URI: murrsuitPost, + Hashtags: []string{"fursuit", "murrsuit"}, + HasMedia: true, }, { - URI: artPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"art"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, + URI: artPost, + Hashtags: []string{"art"}, + HasMedia: true, }, { - URI: nsfwArtPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"furryart", "nsfw"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, + URI: nsfwArtPost, + Hashtags: []string{"furryart", "nsfw"}, + HasMedia: true, }, { - URI: poastPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{}, - HasMedia: true, - Raw: &bsky.FeedPost{}, + URI: poastPost, + HasMedia: true, }, { URI: nsfwLabelledPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, Hashtags: []string{"art"}, HasMedia: true, - Raw: &bsky.FeedPost{}, SelfLabels: []string{"sexual"}, }, { URI: nsfwVideoPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, HasVideo: true, Hashtags: []string{}, - Raw: &bsky.FeedPost{}, SelfLabels: []string{"sexual"}, }, { URI: nsfwArtVideoPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, HasVideo: true, Hashtags: []string{"furryart"}, - Raw: &bsky.FeedPost{}, SelfLabels: []string{"sexual"}, }, { - URI: videoPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - HasVideo: true, - Hashtags: []string{}, - Raw: &bsky.FeedPost{}, + URI: videoPost, + HasVideo: true, }, { - URI: artVideoPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - HasVideo: true, - Hashtags: []string{"art"}, - Raw: &bsky.FeedPost{}, + URI: artVideoPost, + HasVideo: true, + Hashtags: []string{"art"}, }, { - URI: aiArtPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"art", "aiart", "aiartist"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, + URI: aiArtPost, + Hashtags: []string{"art", "aiart", "aiartist"}, + HasMedia: true, }, { - URI: pinnedPost, - ActorDID: pinnedFurry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{}, - HasMedia: true, - Raw: &bsky.FeedPost{}, - SelfLabels: []string{}, + URI: pinnedPost, + ActorDID: pinnedFurry.DID(), + HasMedia: true, }, } { + if opts.ActorDID == "" { + opts.ActorDID = furry.DID() + } + if opts.Raw == nil { + opts.Raw = &bsky.FeedPost{} + } + if opts.Hashtags == nil { + opts.Hashtags = []string{} + } require.NoError(t, harness.Store.CreatePost(ctx, opts)) } - for _, test := range []struct { - name string - opts chronologicalGeneratorOpts - expectedPosts []string - }{ - { - name: "all", - opts: chronologicalGeneratorOpts{ - generatorOpts: generatorOpts{ - Hashtags: []string{}, - IsNSFW: tristate.Maybe, - HasMedia: tristate.Maybe, - HasVideo: tristate.Maybe, + t.Run("chronological", func(t *testing.T) { + t.Parallel() + + for _, test := range []struct { + name string + opts chronologicalGeneratorOpts + expectedPosts []string + }{ + { + name: "all", + opts: chronologicalGeneratorOpts{ + generatorOpts: generatorOpts{ + Hashtags: []string{}, + IsNSFW: tristate.Maybe, + HasMedia: tristate.Maybe, + HasVideo: tristate.Maybe, + }, + }, + expectedPosts: []string{ + fursuitPost, + murrsuitPost, + artPost, + nsfwArtPost, + poastPost, + nsfwLabelledPost, + pinnedPost, + aiArtPost, + videoPost, + nsfwVideoPost, + artVideoPost, + nsfwArtVideoPost, }, }, - expectedPosts: []string{ - fursuitPost, - murrsuitPost, - artPost, - nsfwArtPost, - poastPost, - nsfwLabelledPost, - pinnedPost, - aiArtPost, - videoPost, - nsfwVideoPost, - artVideoPost, - nsfwArtVideoPost, - }, - }, - { - name: "all fursuits", - opts: chronologicalGeneratorOpts{ - generatorOpts: generatorOpts{ - Hashtags: []string{"fursuit"}, - IsNSFW: tristate.Maybe, - HasMedia: tristate.True, + { + name: "all fursuits", + opts: chronologicalGeneratorOpts{ + generatorOpts: generatorOpts{ + Hashtags: []string{"fursuit"}, + IsNSFW: tristate.Maybe, + HasMedia: tristate.True, + }, }, + expectedPosts: []string{fursuitPost, murrsuitPost}, }, - expectedPosts: []string{fursuitPost, murrsuitPost}, - }, - { - name: "sfw only fursuits", - opts: chronologicalGeneratorOpts{ - generatorOpts: generatorOpts{ - Hashtags: []string{"fursuit"}, - IsNSFW: tristate.False, - HasMedia: tristate.True, + { + name: "sfw only fursuits", + opts: chronologicalGeneratorOpts{ + generatorOpts: generatorOpts{ + Hashtags: []string{"fursuit"}, + IsNSFW: tristate.False, + HasMedia: tristate.True, + }, }, + expectedPosts: []string{fursuitPost}, }, - expectedPosts: []string{fursuitPost}, - }, - { - name: "art", - opts: chronologicalGeneratorOpts{ - generatorOpts: generatorOpts{ - Hashtags: []string{"art", "furryart"}, - DisallowedHashtags: []string{"aiart"}, - HasMedia: tristate.True, + { + name: "art", + opts: chronologicalGeneratorOpts{ + generatorOpts: generatorOpts{ + Hashtags: []string{"art", "furryart"}, + DisallowedHashtags: []string{"aiart"}, + HasMedia: tristate.True, + }, }, + expectedPosts: []string{artPost, nsfwArtPost, nsfwLabelledPost}, }, - expectedPosts: []string{artPost, nsfwArtPost, nsfwLabelledPost}, - }, - { - name: "nsfw only art", - opts: chronologicalGeneratorOpts{ - generatorOpts: generatorOpts{ - Hashtags: []string{"art", "furryart"}, - IsNSFW: tristate.True, - HasMedia: tristate.True, + { + name: "nsfw only art", + opts: chronologicalGeneratorOpts{ + generatorOpts: generatorOpts{ + Hashtags: []string{"art", "furryart"}, + IsNSFW: tristate.True, + HasMedia: tristate.True, + }, }, + expectedPosts: []string{nsfwArtPost, nsfwLabelledPost}, }, - expectedPosts: []string{nsfwArtPost, nsfwLabelledPost}, - }, - { - name: "pinned post", - opts: chronologicalGeneratorOpts{ - generatorOpts: generatorOpts{ - Hashtags: []string{"placeholder"}, + { + name: "pinned post", + opts: chronologicalGeneratorOpts{ + generatorOpts: generatorOpts{ + Hashtags: []string{"placeholder"}, + }, + PinnedDIDs: []string{pinnedFurry.DID()}, }, - PinnedDIDs: []string{pinnedFurry.DID()}, + expectedPosts: []string{pinnedPost}, }, - expectedPosts: []string{pinnedPost}, - }, - { - name: "all videos", - opts: chronologicalGeneratorOpts{ - generatorOpts: generatorOpts{ - Hashtags: []string{}, - HasVideo: tristate.True, + { + name: "all videos", + opts: chronologicalGeneratorOpts{ + generatorOpts: generatorOpts{ + Hashtags: []string{}, + HasVideo: tristate.True, + }, }, + expectedPosts: []string{videoPost, nsfwVideoPost, artVideoPost, nsfwArtVideoPost}, }, - expectedPosts: []string{videoPost, nsfwVideoPost, artVideoPost, nsfwArtVideoPost}, - }, - { - name: "videos nsfw", - opts: chronologicalGeneratorOpts{ - generatorOpts: generatorOpts{ - IsNSFW: tristate.True, - HasVideo: tristate.True, + { + name: "videos nsfw", + opts: chronologicalGeneratorOpts{ + generatorOpts: generatorOpts{ + IsNSFW: tristate.True, + HasVideo: tristate.True, + }, }, + expectedPosts: []string{nsfwVideoPost, nsfwArtVideoPost}, }, - expectedPosts: []string{nsfwVideoPost, nsfwArtVideoPost}, - }, - } { - test := test + } { + test := test - t.Run(test.name, func(t *testing.T) { - t.Parallel() - posts, err := chronologicalGenerator(test.opts)(ctx, harness.Store, "", 1000) - require.NoError(t, err) - postURIs := make([]string, len(posts)) - for i, post := range posts { - postURIs[i] = post.URI - } - require.ElementsMatch(t, test.expectedPosts, postURIs) - }) - } -} - -func TestPreScoredGenerator(t *testing.T) { - t.Parallel() - - ctx, cancel := context.WithCancel(context.Background()) - t.Cleanup(cancel) - harness := testenv.StartHarness(ctx, t) - furry := harness.PDS.MustNewUser(t, "furry.tpds") - pinnedFurry := harness.PDS.MustNewUser(t, "pinnedFurry.tpds") - _, err := harness.Store.CreateActor(ctx, store.CreateActorOpts{ - Status: bffv1pb.ActorStatus_ACTOR_STATUS_APPROVED, - DID: furry.DID(), + t.Run(test.name, func(t *testing.T) { + t.Parallel() + posts, err := chronologicalGenerator(test.opts)(ctx, harness.Store, "", 1000) + require.NoError(t, err) + postURIs := make([]string, len(posts)) + for i, post := range posts { + postURIs[i] = post.URI + } + require.ElementsMatch(t, test.expectedPosts, postURIs) + }) + } }) - require.NoError(t, err) - _, err = harness.Store.CreateActor(ctx, store.CreateActorOpts{ - Status: bffv1pb.ActorStatus_ACTOR_STATUS_APPROVED, - DID: pinnedFurry.DID(), - }) - require.NoError(t, err) - fursuitPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "fursuit") - murrsuitPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "murrsuit") - artPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "art") - nsfwArtPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "nsfwArt") - poastPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "poast") - nsfwLabelledPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "nsfw-labelled") - aiArtPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "ai") - pinnedPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "pinned-post") - videoPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "video") - nsfwVideoPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "nsfw-video") - nsfwArtVideoPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "nsfw-art-video") - artVideoPost := indigoTest.RandFakeAtUri("app.bsky.feed.post", "art-video") + t.Run("prescored", func(t *testing.T) { + t.Parallel() - for _, opts := range []store.CreatePostOpts{ - { - URI: fursuitPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"fursuit"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, - }, - { - URI: murrsuitPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"fursuit", "murrsuit"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, - }, - { - URI: artPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"art"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, - }, - { - URI: nsfwArtPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"furryart", "nsfw"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, - }, - { - URI: poastPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{}, - HasMedia: true, - Raw: &bsky.FeedPost{}, - }, - { - URI: nsfwLabelledPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"art"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, - SelfLabels: []string{"sexual"}, - }, - { - URI: aiArtPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{"art", "aiart", "aiartist"}, - HasMedia: true, - Raw: &bsky.FeedPost{}, - }, - { - URI: pinnedPost, - ActorDID: pinnedFurry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - Hashtags: []string{}, - HasMedia: true, - Raw: &bsky.FeedPost{}, - SelfLabels: []string{}, - }, - { - URI: nsfwVideoPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - HasVideo: true, - Hashtags: []string{}, - Raw: &bsky.FeedPost{}, - SelfLabels: []string{"sexual"}, - }, - { - URI: nsfwArtVideoPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - HasVideo: true, - Hashtags: []string{"furryart"}, - Raw: &bsky.FeedPost{}, - SelfLabels: []string{"sexual"}, - }, - { - URI: videoPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - HasVideo: true, - Hashtags: []string{}, - Raw: &bsky.FeedPost{}, - }, - { - URI: artVideoPost, - ActorDID: furry.DID(), - CreatedAt: time.Time{}, - IndexedAt: time.Time{}, - HasVideo: true, - Hashtags: []string{"art"}, - Raw: &bsky.FeedPost{}, - }, - } { - require.NoError(t, harness.Store.CreatePost(ctx, opts)) - } - _, err = harness.Store.MaterializeClassicPostScores(ctx, time.Time{}) - require.NoError(t, err) + _, err = harness.Store.MaterializeClassicPostScores(ctx, time.Time{}) + require.NoError(t, err) - for _, test := range []struct { - name string - opts preScoredGeneratorOpts - expectedPosts []string - }{ - { - name: "all", - opts: preScoredGeneratorOpts{ - Alg: "classic", - generatorOpts: generatorOpts{ - Hashtags: []string{}, - IsNSFW: tristate.Maybe, - HasMedia: tristate.Maybe, - HasVideo: tristate.Maybe, + for _, test := range []struct { + name string + opts preScoredGeneratorOpts + expectedPosts []string + }{ + { + name: "all", + opts: preScoredGeneratorOpts{ + Alg: "classic", + generatorOpts: generatorOpts{ + Hashtags: []string{}, + IsNSFW: tristate.Maybe, + HasMedia: tristate.Maybe, + HasVideo: tristate.Maybe, + }, + }, + expectedPosts: []string{ + fursuitPost, + murrsuitPost, + artPost, + nsfwArtPost, + poastPost, + nsfwLabelledPost, + aiArtPost, + pinnedPost, + videoPost, + nsfwVideoPost, + nsfwArtVideoPost, + artVideoPost, }, }, - expectedPosts: []string{ - fursuitPost, - murrsuitPost, - artPost, - nsfwArtPost, - poastPost, - nsfwLabelledPost, - aiArtPost, - pinnedPost, - videoPost, - nsfwVideoPost, - nsfwArtVideoPost, - artVideoPost, - }, - }, - { - name: "all fursuits", - opts: preScoredGeneratorOpts{ - Alg: "classic", - generatorOpts: generatorOpts{ - Hashtags: []string{"fursuit"}, - IsNSFW: tristate.Maybe, - HasMedia: tristate.True, + { + name: "all fursuits", + opts: preScoredGeneratorOpts{ + Alg: "classic", + generatorOpts: generatorOpts{ + Hashtags: []string{"fursuit"}, + IsNSFW: tristate.Maybe, + HasMedia: tristate.True, + }, }, + expectedPosts: []string{fursuitPost, murrsuitPost}, }, - expectedPosts: []string{fursuitPost, murrsuitPost}, - }, - { - name: "sfw only fursuits", - opts: preScoredGeneratorOpts{ - Alg: "classic", - generatorOpts: generatorOpts{ - Hashtags: []string{"fursuit"}, - IsNSFW: tristate.False, - HasMedia: tristate.True, + { + name: "sfw only fursuits", + opts: preScoredGeneratorOpts{ + Alg: "classic", + generatorOpts: generatorOpts{ + Hashtags: []string{"fursuit"}, + IsNSFW: tristate.False, + HasMedia: tristate.True, + }, }, + expectedPosts: []string{fursuitPost}, }, - expectedPosts: []string{fursuitPost}, - }, - { - name: "art", - opts: preScoredGeneratorOpts{ - Alg: "classic", - generatorOpts: generatorOpts{ - Hashtags: []string{"art", "furryart"}, - DisallowedHashtags: []string{"aiart"}, - HasMedia: tristate.True, + { + name: "art", + opts: preScoredGeneratorOpts{ + Alg: "classic", + generatorOpts: generatorOpts{ + Hashtags: []string{"art", "furryart"}, + DisallowedHashtags: []string{"aiart"}, + HasMedia: tristate.True, + }, }, + expectedPosts: []string{artPost, nsfwArtPost, nsfwLabelledPost}, }, - expectedPosts: []string{artPost, nsfwArtPost, nsfwLabelledPost}, - }, - { - name: "nsfw only art", - opts: preScoredGeneratorOpts{ - Alg: "classic", - generatorOpts: generatorOpts{ - Hashtags: []string{"art", "furryart"}, - IsNSFW: tristate.True, - HasMedia: tristate.True, + { + name: "nsfw only art", + opts: preScoredGeneratorOpts{ + Alg: "classic", + generatorOpts: generatorOpts{ + Hashtags: []string{"art", "furryart"}, + IsNSFW: tristate.True, + HasMedia: tristate.True, + }, }, + expectedPosts: []string{nsfwArtPost, nsfwLabelledPost}, }, - expectedPosts: []string{nsfwArtPost, nsfwLabelledPost}, - }, - { - name: "all videos", - opts: preScoredGeneratorOpts{ - Alg: "classic", - generatorOpts: generatorOpts{ - Hashtags: []string{}, - HasVideo: tristate.True, + { + name: "all videos", + opts: preScoredGeneratorOpts{ + Alg: "classic", + generatorOpts: generatorOpts{ + Hashtags: []string{}, + HasVideo: tristate.True, + }, }, + expectedPosts: []string{videoPost, nsfwVideoPost, artVideoPost, nsfwArtVideoPost}, }, - expectedPosts: []string{videoPost, nsfwVideoPost, artVideoPost, nsfwArtVideoPost}, - }, - { - name: "nsfw videos", - opts: preScoredGeneratorOpts{ - Alg: "classic", - generatorOpts: generatorOpts{ - Hashtags: []string{}, - IsNSFW: tristate.True, - HasVideo: tristate.True, + { + name: "nsfw videos", + opts: preScoredGeneratorOpts{ + Alg: "classic", + generatorOpts: generatorOpts{ + Hashtags: []string{}, + IsNSFW: tristate.True, + HasVideo: tristate.True, + }, }, + expectedPosts: []string{nsfwVideoPost, nsfwArtVideoPost}, }, - expectedPosts: []string{nsfwVideoPost, nsfwArtVideoPost}, - }, - } { - test := test + } { + test := test - t.Run(test.name, func(t *testing.T) { - t.Parallel() - posts, err := preScoredGenerator(test.opts)(ctx, harness.Store, "", 1000) - require.NoError(t, err) - postURIs := make([]string, len(posts)) - for i, post := range posts { - postURIs[i] = post.URI - } - require.ElementsMatch(t, test.expectedPosts, postURIs) - }) - } + t.Run(test.name, func(t *testing.T) { + t.Parallel() + posts, err := preScoredGenerator(test.opts)(ctx, harness.Store, "", 1000) + require.NoError(t, err) + postURIs := make([]string, len(posts)) + for i, post := range posts { + postURIs[i] = post.URI + } + require.ElementsMatch(t, test.expectedPosts, postURIs) + }) + } + }) }