# typed: false
require "rails_helper"

RSpec.describe Job::FaPostFurArchiverPostFileJob do
  include PerformJobHelpers

  let(:http_client_mock) { instance_double("::Scraper::HttpClient") }

  let(:post_file) do
    post_file =
      create(
        :domain_post_file,
        url_str: file_url_str,
        state: "terminal_error",
        last_status_code: 404,
        log_entry:
          create(
            :http_log_entry,
            :text_entry,
            status_code: 404,
            uri_str: file_url_str,
          ),
        post:
          create(
            :domain_post_fa_post,
            creator: create(:domain_user_fa_user, url_name: user_url_name),
          ),
      )
    post_file.save!
    post_file
  end
  let(:post) { post_file.post }
  let(:old_log_entry) { post_file.log_entry }

  let(:image_data) do
    File.binread(
      Rails.root.join(
        "test/fixtures/files/1496842943.wolfsparta_caught_pt2.png",
      ),
    )
  end

  let(:not_found_data) do
    File.binread(
      Rails.root.join(
        "test/fixtures/files/1165272531.akirameerkat_meerkat_feet_1.jpg",
      ),
    )
  end

  before do
    Scraper::ClientFactory.http_client_mock = http_client_mock
    @log_entries =
      HttpClientMockHelpers.init_http_client_mock(
        http_client_mock,
        client_mock_config,
      )
  end

  describe "#perform" do
    let(:fur_archiver_url_str) do
      "https://furarchiver.net/File/View?artist=wolfsparta&filename=1496842943.wolfsparta_caught_pt2.png"
    end

    let(:user_url_name) { "wolfsparta" }

    shared_examples "correct behavior" do
      let(:client_mock_config) do
        [
          {
            uri: fur_archiver_url_str,
            status_code: 200,
            content_type: "image/png",
            contents: image_data,
          },
        ]
      end

      it "downloads the file from fur archiver" do
        expect do
          perform_now({ post_file: post_file })
          post.reload
        end.to change { post.file&.log_entry }.from(old_log_entry).to(
          have_attributes(uri: have_attributes(to_s: fur_archiver_url_str)),
        )
      end

      it "updates the post_file blob" do
        expect do
          perform_now({ post_file: post_file })
          post.reload
        end.to change { post.file&.blob }.from(old_log_entry.response).to(
          @log_entries[0].response,
        )
      end

      it "sets the last status code" do
        expect do
          perform_now({ post_file: post_file })
          post.reload
        end.to change { post.file&.last_status_code }.from(404).to(200)
      end

      it "sets the post_file state to ok" do
        expect do
          perform_now({ post_file: post_file })
          post.reload
        end.to change { post.file&.state }.from("terminal_error").to("ok")
      end
    end

    context "with a d.facdn.net url" do
      let(:file_url_str) do
        "https://d.facdn.net/art/wolfsparta/1496842943/1496842943.wolfsparta_caught_pt2.png"
      end
      include_examples "correct behavior"
    end

    context "with a d.furaffinity.net url" do
      let(:file_url_str) do
        "https://d.furaffinity.net/art/wolfsparta/1496842943/1496842943.wolfsparta_caught_pt2.png"
      end
      include_examples "correct behavior"
    end
  end
end
