# frozen_string_literal: true

require 'rails_helper'
require 'mastodon/cli/media'

RSpec.describe Mastodon::CLI::Media do
  subject { cli.invoke(action, arguments, options) }

  let(:cli) { described_class.new }
  let(:arguments) { [] }
  let(:options) { {} }

  it_behaves_like 'CLI Command'

  describe '#remove' do
    let(:action) { :remove }

    context 'with --prune-profiles and --remove-headers' do
      let(:options) { { prune_profiles: true, remove_headers: true } }

      it 'warns about usage and exits' do
        expect { subject }
          .to raise_error(Thor::Error, '--prune-profiles and --remove-headers should not be specified simultaneously')
      end
    end

    context 'with --include-follows but not including --prune-profiles and --remove-headers' do
      let(:options) { { include_follows: true } }

      it 'warns about usage and exits' do
        expect { subject }
          .to raise_error(Thor::Error, '--include-follows can only be used with --prune-profiles or --remove-headers')
      end
    end

    context 'with a relevant account' do
      let!(:account) do
        Fabricate(:account, domain: 'example.com', updated_at: 1.month.ago, last_webfingered_at: 1.month.ago, avatar: attachment_fixture('attachment.jpg'), header: attachment_fixture('attachment.jpg'))
      end

      context 'with --prune-profiles' do
        let(:options) { { prune_profiles: true } }

        it 'removes account avatars' do
          expect { subject }
            .to output_results('Visited 1')

          expect(account.reload.avatar).to be_blank
        end
      end

      context 'with --remove-headers' do
        let(:options) { { remove_headers: true } }

        it 'removes account header' do
          expect { subject }
            .to output_results('Visited 1')

          expect(account.reload.header).to be_blank
        end
      end
    end

    context 'with a relevant media attachment' do
      let!(:media_attachment) { Fabricate(:media_attachment, remote_url: 'https://example.com/image.jpg', created_at: 1.month.ago) }

      context 'without options' do
        it 'removes account avatars' do
          expect { subject }
            .to output_results('Removed 1')

          expect(media_attachment.reload.file).to be_blank
          expect(media_attachment.reload.thumbnail).to be_blank
        end
      end

      context 'with --keep-interacted' do
        let(:options) { { keep_interacted: true } }

        let!(:favourited_media) { Fabricate(:media_attachment, created_at: 1.month.ago, remote_url: 'https://example.com/image.jpg') }
        let!(:bookmarked_media) { Fabricate(:media_attachment, created_at: 1.month.ago, remote_url: 'https://example.com/image.jpg') }
        let!(:replied_to_media) { Fabricate(:media_attachment, created_at: 1.month.ago, remote_url: 'https://example.com/image.jpg') }
        let!(:reblogged_media) { Fabricate(:media_attachment, created_at: 1.month.ago, remote_url: 'https://example.com/image.jpg') }
        let!(:remote_quoted_media) { Fabricate(:media_attachment, created_at: 1.month.ago, remote_url: 'https://example.com/image.jpg') }
        let!(:remote_quoting_media) { Fabricate(:media_attachment, created_at: 1.month.ago, remote_url: 'https://example.com/image.jpg') }

        before do
          local_account = Fabricate(:account, username: 'alice')
          remote_account = Fabricate(:account, username: 'bob', domain: 'example.com')

          favourited_status = Fabricate(:status, account: remote_account)
          bookmarked_status = Fabricate(:status, account: remote_account)
          replied_to_status = Fabricate(:status, account: remote_account)
          reblogged_status = Fabricate(:status, account: remote_account)

          favourited_media.update!(status: favourited_status)
          bookmarked_media.update!(status: bookmarked_status)
          replied_to_media.update!(status: replied_to_status)
          reblogged_media.update!(status: reblogged_status)

          local_quoting_status = Fabricate(:status, account: local_account)
          remote_quoted_status = Fabricate(:status, account: remote_account)
          local_status_being_quoted = Fabricate(:status, account: local_account)
          remote_quoting_status = Fabricate(:status, account: remote_account)

          remote_quoted_media.update!(status: remote_quoted_status)
          remote_quoting_media.update!(status: remote_quoting_status)

          non_interacted_status = Fabricate(:status, account: remote_account)

          media_attachment.update(status: non_interacted_status)

          Fabricate(:favourite, account: local_account, status: favourited_status)
          Fabricate(:bookmark, account: local_account, status: bookmarked_status)
          Fabricate(:status, account: local_account, in_reply_to_id: replied_to_status.id)
          Fabricate(:status, account: local_account, reblog: reblogged_status)
          Fabricate(:quote, account: local_account, status: local_quoting_status, quoted_status: remote_quoted_status)
          Fabricate(:quote, account: remote_account, status: remote_quoting_status, quoted_status: local_status_being_quoted)
        end

        it 'keeps media associated with statuses that have been favourited, bookmarked, replied to, or reblogged by a local account' do
          expect { subject }
            .to output_results('Removed 1')

          expect(favourited_media.reload.file).to be_present
          expect(bookmarked_media.reload.file).to be_present
          expect(replied_to_media.reload.file).to be_present
          expect(reblogged_media.reload.file).to be_present
          expect(remote_quoted_media.reload.file).to be_present
          expect(remote_quoting_media.reload.file).to be_present

          expect(media_attachment.reload.file).to be_blank
          expect(media_attachment.reload.thumbnail).to be_blank
        end
      end
    end
  end

  describe '#usage' do
    let(:action) { :usage }

    context 'without options' do
      it 'reports about storage size' do
        expect { subject }
          .to output_results('0 Bytes')
      end
    end
  end

  describe '#lookup' do
    let(:action) { :lookup }
    let(:arguments) { [url] }

    context 'with valid url not connected to a record' do
      let(:url) { 'https://example.host/assets/1' }

      it 'warns about url and exits' do
        expect { subject }
          .to raise_error(Thor::Error, 'Not a media URL')
      end
    end

    context 'with a valid media url' do
      let(:status) { Fabricate(:status) }
      let(:media_attachment) { Fabricate(:media_attachment, status: status) }
      let(:url) { media_attachment.file.url(:original) }

      it 'displays the url of a connected status' do
        expect { subject }
          .to output_results(status.id.to_s)
      end
    end
  end

  describe '#refresh' do
    let(:action) { :refresh }

    context 'without any options' do
      it 'warns about usage and exits' do
        expect { subject }
          .to raise_error(Thor::Error, /Specify the source/)
      end
    end

    context 'with --status option' do
      before do
        media_attachment.update(file_file_name: nil)
      end

      let(:media_attachment) { Fabricate(:media_attachment, status: status, remote_url: 'https://host.example/asset.jpg') }
      let(:options) { { status: status.id } }
      let(:status) { Fabricate(:status) }

      it 'redownloads the attachment file' do
        expect { subject }
          .to output_results('Downloaded 1 media')
      end
    end

    context 'with --account option' do
      context 'when the account does not exist' do
        let(:options) { { account: 'not-real-user@example.host' } }

        it 'warns about usage and exits' do
          expect { subject }
            .to raise_error(Thor::Error, 'No such account')
        end
      end

      context 'when the account exists' do
        before do
          media_attachment.update(file_file_name: nil)
        end

        let(:media_attachment) { Fabricate(:media_attachment, account: account) }
        let(:options) { { account: account.acct } }
        let(:account) { Fabricate(:account) }

        it 'redownloads the attachment file' do
          expect { subject }
            .to output_results('Downloaded 1 media')
        end
      end
    end

    context 'with --domain option' do
      before do
        media_attachment.update(file_file_name: nil)
      end

      let(:domain) { 'example.host' }
      let(:media_attachment) { Fabricate(:media_attachment, account: account) }
      let(:options) { { domain: domain } }
      let(:account) { Fabricate(:account, domain: domain) }

      it 'redownloads the attachment file' do
        expect { subject }
          .to output_results('Downloaded 1 media')
      end
    end

    context 'with --days option' do
      before do
        Fabricate(:media_attachment, remote_url: 'https://example.com/image.jpg', id: Mastodon::Snowflake.id_at(50.days.ago))
        Fabricate(:media_attachment, remote_url: 'https://example.com/image.jpg', id: Mastodon::Snowflake.id_at(5.days.ago))
        Fabricate(:media_attachment, remote_url: '', id: Mastodon::Snowflake.id_at(5.days.ago))
      end

      let(:options) { { days: 10 } }

      it 'redownloads the attachment file for the remote records more recent than the option' do
        expect { subject }
          .to output_results('Downloaded 1 media')
      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 raise_error(Thor::Error, /azure storage driver is not supported/)
      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 raise_error(Thor::Error, /fog storage driver is not supported/)
      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
