From db57fe80c8a4fcac2b0fb857c7eb959cbf82a2e0 Mon Sep 17 00:00:00 2001
From: Matt Jankowski <matt@jankowski.online>
Date: Thu, 26 Sep 2024 04:54:01 -0400
Subject: [PATCH] Remove `page_json` var from ap/replies spec (#32000)

---
 .../activitypub/replies_controller_spec.rb    | 82 ++++++++++++++-----
 1 file changed, 61 insertions(+), 21 deletions(-)

diff --git a/spec/controllers/activitypub/replies_controller_spec.rb b/spec/controllers/activitypub/replies_controller_spec.rb
index 27821b0d4..d7c2c2d3b 100644
--- a/spec/controllers/activitypub/replies_controller_spec.rb
+++ b/spec/controllers/activitypub/replies_controller_spec.rb
@@ -66,7 +66,6 @@ RSpec.describe ActivityPub::RepliesController do
 
     context 'when status is public' do
       let(:parent_visibility) { :public }
-      let(:page_json) { response.parsed_body[:first] }
 
       it 'returns http success and correct media type' do
         expect(response)
@@ -78,16 +77,28 @@ RSpec.describe ActivityPub::RepliesController do
 
       context 'without only_other_accounts' do
         it "returns items with thread author's replies" do
-          expect(page_json).to be_a Hash
-          expect(page_json[:items]).to be_an Array
-          expect(page_json[:items].size).to eq 1
-          expect(page_json[:items].all? { |item| targets_public_collection?(item) }).to be true
+          expect(response.parsed_body)
+            .to include(
+              first: be_a(Hash).and(
+                include(
+                  items: be_an(Array)
+                  .and(have_attributes(size: 1))
+                  .and(all(satisfy { |item| targets_public_collection?(item) }))
+                )
+              )
+            )
         end
 
         context 'when there are few self-replies' do
           it 'points next to replies from other people' do
-            expect(page_json).to be_a Hash
-            expect(parsed_uri_query_values(page_json[:next])).to include('only_other_accounts=true', 'page=true')
+            expect(response.parsed_body)
+              .to include(
+                first: be_a(Hash).and(
+                  include(
+                    next: satisfy { |value| (parsed_uri_query_values(value) & %w(only_other_accounts=true page=true)).any? }
+                  )
+                )
+              )
           end
         end
 
@@ -97,8 +108,14 @@ RSpec.describe ActivityPub::RepliesController do
           end
 
           it 'points next to other self-replies' do
-            expect(page_json).to be_a Hash
-            expect(parsed_uri_query_values(page_json[:next])).to include('only_other_accounts=false', 'page=true')
+            expect(response.parsed_body)
+              .to include(
+                first: be_a(Hash).and(
+                  include(
+                    next: satisfy { |value| (parsed_uri_query_values(value) & %w(only_other_accounts=false page=true)).any? }
+                  )
+                )
+              )
           end
         end
       end
@@ -107,26 +124,31 @@ RSpec.describe ActivityPub::RepliesController do
         let(:only_other_accounts) { 'true' }
 
         it 'returns items with other public or unlisted replies' do
-          expect(page_json).to be_a Hash
-          expect(page_json[:items]).to be_an Array
-          expect(page_json[:items].size).to eq 3
+          expect(response.parsed_body)
+            .to include(
+              first: be_a(Hash).and(
+                include(items: be_an(Array).and(have_attributes(size: 3)))
+              )
+            )
         end
 
         it 'only inlines items that are local and public or unlisted replies' do
-          inlined_replies = page_json[:items].select { |x| x.is_a?(Hash) }
-          expect(inlined_replies.all? { |item| targets_public_collection?(item) }).to be true
-          expect(inlined_replies.all? { |item| ActivityPub::TagManager.instance.local_uri?(item[:id]) }).to be true
+          expect(inlined_replies)
+            .to all(satisfy { |item| targets_public_collection?(item) })
+            .and all(satisfy { |item| ActivityPub::TagManager.instance.local_uri?(item[:id]) })
         end
 
         it 'uses ids for remote toots' do
-          remote_replies = page_json[:items].reject { |x| x.is_a?(Hash) }
-          expect(remote_replies.all? { |item| item.is_a?(String) && !ActivityPub::TagManager.instance.local_uri?(item) }).to be true
+          expect(remote_replies)
+            .to all(satisfy { |item| item.is_a?(String) && !ActivityPub::TagManager.instance.local_uri?(item) })
         end
 
         context 'when there are few replies' do
           it 'does not have a next page' do
-            expect(page_json).to be_a Hash
-            expect(page_json[:next]).to be_nil
+            expect(response.parsed_body)
+              .to include(
+                first: be_a(Hash).and(not_include(next: be_present))
+              )
           end
         end
 
@@ -136,8 +158,14 @@ RSpec.describe ActivityPub::RepliesController do
           end
 
           it 'points next to other replies' do
-            expect(page_json).to be_a Hash
-            expect(parsed_uri_query_values(page_json[:next])).to include('only_other_accounts=true', 'page=true')
+            expect(response.parsed_body)
+              .to include(
+                first: be_a(Hash).and(
+                  include(
+                    next: satisfy { |value| (parsed_uri_query_values(value) & %w(only_other_accounts=true page=true)).any? }
+                  )
+                )
+              )
           end
         end
       end
@@ -193,6 +221,18 @@ RSpec.describe ActivityPub::RepliesController do
 
   private
 
+  def inlined_replies
+    response
+      .parsed_body[:first][:items]
+      .select { |x| x.is_a?(Hash) }
+  end
+
+  def remote_replies
+    response
+      .parsed_body[:first][:items]
+      .reject { |x| x.is_a?(Hash) }
+  end
+
   def parsed_uri_query_values(uri)
     Addressable::URI
       .parse(uri)