# typed: false
require "rails_helper"

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

  it "works" do
    file = create(:http_log_entry)

    log_entries =
      HttpClientMockHelpers.init_http_client_mock(
        http_client_mock,
        [
          {
            uri: "https://e621.net/posts.json",
            status_code: 200,
            content_type: "application/json; charset=utf-8",
            contents:
              SpecUtil.read_fixture_file("domain/e621/job/posts_index_1.json"),
            caused_by_entry: file,
          },
        ],
      )

    perform_now({ caused_by_entry: file })

    expect(Domain::Post::E621Post.count).to eq(5)
    post = Domain::Post::E621Post.find_by(e621_id: 4_247_443)
    expect(post.file.url_str).to eq(
      "https://static1.e621.net/data/1c/61/1c6169aa51668681e9697a48144d7c78.jpg",
    )
    expect(post.md5).to eq("1c6169aa51668681e9697a48144d7c78")
    expect(post.tags_array).to match(
      hash_including(
        "general" => array_including("alcohol", "beach_ball", "wide_hips"),
        "species" => array_including("mammal", "procyonid", "raccoon"),
      ),
    )

    # expect jobs to be enqueued
    expect(
      SpecUtil.enqueued_job_args(Domain::E621::Job::StaticFileJob).count,
    ).to eq(5)
    expect(
      SpecUtil.enqueued_job_args(Domain::E621::Job::StaticFileJob)[0],
    ).to eq(
      {
        post_file: Domain::Post::E621Post.find_by(e621_id: 4_247_444).file,
        caused_by_entry: log_entries[0],
      },
    )
  end
end
