From 5c769de096ce7205308deaf5d62ad516b1e39259 Mon Sep 17 00:00:00 2001
From: Matt Jankowski <matt@jankowski.online>
Date: Wed, 3 Jan 2024 09:12:56 -0500
Subject: [PATCH] Add spec coverage for `CLI::Media#remove_orphans` command
 (#28267)

---
 lib/mastodon/cli/base.rb            |  1 +
 spec/lib/mastodon/cli/media_spec.rb | 54 +++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/lib/mastodon/cli/base.rb b/lib/mastodon/cli/base.rb
index 32aff2fcc..8c222bbb2 100644
--- a/lib/mastodon/cli/base.rb
+++ b/lib/mastodon/cli/base.rb
@@ -4,6 +4,7 @@ require_relative '../../../config/boot'
 require_relative '../../../config/environment'
 
 require 'thor'
+require 'pastel'
 require_relative 'progress_helper'
 
 module Mastodon
diff --git a/spec/lib/mastodon/cli/media_spec.rb b/spec/lib/mastodon/cli/media_spec.rb
index 24e1467a3..071bcd9e3 100644
--- a/spec/lib/mastodon/cli/media_spec.rb
+++ b/spec/lib/mastodon/cli/media_spec.rb
@@ -184,4 +184,58 @@ describe Mastodon::CLI::Media do
       end
     end
   end
+
+  describe '#remove_orphans' do
+    let(:action) { :remove_orphans }
+
+    before do
+      FileUtils.mkdir_p Rails.public_path.join('system')
+    end
+
+    context 'without any options' do
+      it 'runs without error' do
+        expect { subject }
+          .to output_results('Removed', 'orphans (approx')
+      end
+    end
+
+    context 'when in azure mode' do
+      before do
+        allow(Paperclip::Attachment).to receive(:default_options).and_return(storage: :azure)
+      end
+
+      it 'warns about usage and exits' do
+        expect { subject }
+          .to output_results('azure storage driver is not supported')
+          .and raise_error(SystemExit)
+      end
+    end
+
+    context 'when in fog mode' do
+      before do
+        allow(Paperclip::Attachment).to receive(:default_options).and_return(storage: :fog)
+      end
+
+      it 'warns about usage and exits' do
+        expect { subject }
+          .to output_results('fog storage driver is not supported')
+          .and raise_error(SystemExit)
+      end
+    end
+
+    context 'when in filesystem mode' do
+      before do
+        allow(File).to receive(:delete).and_return(true)
+        media_attachment.delete
+      end
+
+      let(:media_attachment) { Fabricate(:media_attachment) }
+
+      it 'removes the unlinked files' do
+        expect { subject }
+          .to output_results('Removed', 'orphans (approx')
+        expect(File).to have_received(:delete).with(media_attachment.file.path)
+      end
+    end
+  end
 end