From b1c10969ac2cfad52a6ee6dbcc34087f86cd87c6 Mon Sep 17 00:00:00 2001
From: Matt Jankowski <matt@jankowski.online>
Date: Tue, 15 Oct 2024 09:10:03 -0400
Subject: [PATCH] Reduce factory creation (48 -> 8) in `AP::Note` serializer
 spec (#32492)

---
 .../activitypub/note_serializer_spec.rb       | 31 +++++++++----------
 1 file changed, 14 insertions(+), 17 deletions(-)

diff --git a/spec/serializers/activitypub/note_serializer_spec.rb b/spec/serializers/activitypub/note_serializer_spec.rb
index 285b241ee..a6976193b 100644
--- a/spec/serializers/activitypub/note_serializer_spec.rb
+++ b/spec/serializers/activitypub/note_serializer_spec.rb
@@ -14,7 +14,7 @@ RSpec.describe ActivityPub::NoteSerializer do
   let!(:reply_by_account_third) { Fabricate(:status, account: account, thread: parent, visibility: :public) }
   let!(:reply_by_account_visibility_direct) { Fabricate(:status, account: account, thread: parent, visibility: :direct) }
 
-  it 'has the expected shape' do
+  it 'has the expected shape and replies collection' do
     expect(subject).to include({
       '@context' => include('https://www.w3.org/ns/activitystreams'),
       'type' => 'Note',
@@ -22,26 +22,23 @@ RSpec.describe ActivityPub::NoteSerializer do
       'contentMap' => include({
         'zh-TW' => a_kind_of(String),
       }),
+      'replies' => replies_collection_values,
     })
   end
 
-  it 'has a replies collection' do
-    expect(subject['replies']['type']).to eql('Collection')
+  def replies_collection_values
+    include(
+      'type' => eql('Collection'),
+      'first' => include(
+        'type' => eql('CollectionPage'),
+        'items' => reply_items
+      )
+    )
   end
 
-  it 'has a replies collection with a first Page' do
-    expect(subject['replies']['first']['type']).to eql('CollectionPage')
-  end
-
-  it 'includes public self-replies in its replies collection' do
-    expect(subject['replies']['first']['items']).to include(reply_by_account_first.uri, reply_by_account_next.uri, reply_by_account_third.uri)
-  end
-
-  it 'does not include replies from others in its replies collection' do
-    expect(subject['replies']['first']['items']).to_not include(reply_by_other_first.uri)
-  end
-
-  it 'does not include replies with direct visibility in its replies collection' do
-    expect(subject['replies']['first']['items']).to_not include(reply_by_account_visibility_direct.uri)
+  def reply_items
+    include(reply_by_account_first.uri, reply_by_account_next.uri, reply_by_account_third.uri) # Public self replies
+      .and(not_include(reply_by_other_first.uri)) # Replies from others
+      .and(not_include(reply_by_account_visibility_direct.uri)) # Replies with direct visibility
   end
 end