# typed: false
# frozen_string_literal: true

require "rails_helper"

describe Domain::Sofurry::Job::ScanPostJob do
  let(:http_client_mock) { instance_double("::Scraper::HttpClient") }
  before { Scraper::ClientFactory.http_client_mock = http_client_mock }

  context "an art post" do
    let!(:log_entries) do
      HttpClientMockHelpers.init_http_client_mock(
        http_client_mock,
        [
          {
            uri: "https://api2.sofurry.com/std/getSubmissionDetails?id=2265979",
            status_code: 200,
            content_type: "application/json",
            contents:
              SpecUtil.read_fixture_file(
                "domain/sofurry/getSubmissionDetails/2265979.art.json",
              ),
          },
        ],
      )
    end

    it "creates the post model" do
      expect do perform_now({ sofurry_id: 2_265_979 }) end.to change {
        Domain::Post::SofurryPost.count
      }.by(1)
    end

    it "sets post attributes" do
      perform_now({ sofurry_id: 2_265_979 })
      post = Domain::Post::SofurryPost.find_by(sofurry_id: 2_265_979)
      expect(post.media_type).to eq("art")
      expect(post.title).to eq("Trying Some Spanking with Clara")
      expect(post.description).to include(
        "I had forgotten that I sketched out a third idea",
      )
      expect(post.file_name).to eq(
        "gewitter-Trying-Some-Spanking-with-Clara.png",
      )
    end

    it "sets the creator on the post" do
      perform_now({ sofurry_id: 2_265_979 })
      post = Domain::Post::SofurryPost.find_by(sofurry_id: 2_265_979)
      expect(post.creator.sofurry_id).to eq(101_763)
      expect(post.creator.username).to eq("gewitter")
      expect(post.creator.useralias).to eq("gewitter")
    end

    it "enqueues a static file job" do
      perform_now({ sofurry_id: 2_265_979 })
      post = Domain::Post::SofurryPost.find_by(sofurry_id: 2_265_979)
      file = post.file
      expect(file).to be_present
      expect(file.state).to eq("pending")
      expect(
        SpecUtil.enqueued_job_args(Domain::Sofurry::Job::StaticFileJob),
      ).to match([{ file:, caused_by_entry: anything }])
    end
  end
end
