# typed: false
require "rails_helper"

describe Domain::Twitter::Job::UserTimelineTweetsJob do
  GDLClient = Scraper::GalleryDlClient

  let(:http_client_mock) { instance_double("::Scraper::HttpClient") }
  let(:gallery_dl_client_mock) { instance_double("::Scraper::GalleryDlClient") }
  before do
    Scraper::ClientFactory.http_client_mock = http_client_mock
    Scraper::ClientFactory.gallery_dl_client_mock = gallery_dl_client_mock
  end

  context "scanning an unseen user" do
    context "and the user has no tweets" do
      it "creates the user" do
        gallery_dl_user_with_no_tweets_sequence(gallery_dl_client_mock)

        expect do perform_now({ name: "curtus" }) end.to change(
          Domain::Twitter::User,
          :count,
        ).by(1)
        user = Domain::Twitter::User.find_by(name: "curtus")
        expect(user).to_not be_nil
        expect(user.tw_id).to eq(1_234_567)
      end
    end
  end

  context "scanning an exisitng user with no tw_id" do
    it "updates the user" do
      gallery_dl_user_with_no_tweets_sequence(gallery_dl_client_mock)
      user = Domain::Twitter::User.create!(name: "curtus")
      expect do perform_now({ name: "curtus" }) end.not_to change(
        Domain::Twitter::User,
        :count,
      )

      user.reload
      expect(user.tw_id).to eq(1_234_567)
    end
  end
end
