# typed: false
require "rails_helper"

describe Domain::Sofurry::GalleryPageParser do
  Folder = Domain::Sofurry::GalleryPageParser::Folder

  it "parses_folders" do
    parser =
      get_parser(
        "browse_user_gallery/user_556949_stories_page_1.html",
        user_id: 556_949,
      )
    expect(parser.folders).to include(
      Folder.new(name: "Writing Training and Story Hooks", sofurry_id: 77_526),
      Folder.new(name: "Furry Fantasy Stories", sofurry_id: 77_671),
      Folder.new(name: "The Saint's Companion", sofurry_id: 95_425),
    )
    expect(parser.folders.length).to eq(15)
  end

  it "parses posts" do
    parser =
      get_parser(
        "browse_user_gallery/user_556949_stories_page_1.html",
        user_id: 556_949,
      )
    expect(parser.posts.length).to eq(30)
    expect(parser.posts.first.sofurry_id).to eq(2_250_263)
    expect(parser.posts.first.title).to eq("The Rite of Love")

    expect(parser.posts.last.sofurry_id).to eq(1_715_021)
    expect(parser.posts.last.title).to eq("In the Strong Arms of Love")
  end

  it "extracts username from the page" do
    parser =
      get_parser(
        "browse_user_gallery/user_556949_stories_page_1.html",
        user_id: 806_079,
      )
    expect(parser.username).to eq("Matkaja")
  end

  it "extracts posts from art page" do
    parser =
      get_parser("browse_user_gallery/101763.art_page_1.html", user_id: 101_763)
    expect(parser.username).to eq("gewitter")
    expect(parser.posts.length).to eq(30)
    expect(parser.posts.first.sofurry_id).to eq(2_265_979)
    expect(parser.posts.first.title).to eq("Trying Some Spanking with Clara")
    expect(parser.posts.last.sofurry_id).to eq(2_220_943)
    expect(parser.posts.last.title).to eq("Patreon Sketch YCH 68")
  end

  def get_parser(file, user_id:)
    Domain::Sofurry::GalleryPageParser.new(
      File.read(Rails.root.join("test/fixtures/files/domain/sofurry/#{file}")),
      user_id:,
    )
  end
end
