# typed: false
require "rails_helper"

RSpec.describe Scraper::FaHttpClientConfig do
  describe "#cookies" do
    let(:config) { described_class.new }

    context "when all cookies are valid" do
      before do
        allow(GlobalState).to receive(:get).with(
          "furaffinity-cookie-a",
        ).and_return("12345678-1234-1234-1234-123456789abc")
        allow(GlobalState).to receive(:get).with(
          "furaffinity-cookie-b",
        ).and_return("abcdef12-3456-7890-1234-567890abcdef")
        allow(GlobalState).to receive(:get).with(
          "furaffinity-cookie-oaid",
        ).and_return("0123456789abcdef0123456789abcdef")
      end

      it "returns the correct cookie structure" do
        cookies = config.cookies
        expect(cookies).to match(
          [
            {
              domain: ".furaffinity.net",
              cookies: [
                {
                  name: "a",
                  value: "12345678-1234-1234-1234-123456789abc",
                  path: "/",
                },
                {
                  name: "b",
                  value: "abcdef12-3456-7890-1234-567890abcdef",
                  path: "/",
                },
              ],
            },
            {
              domain: "rv.furaffinity.net",
              cookies: [
                {
                  name: "OAID",
                  value: "0123456789abcdef0123456789abcdef",
                  path: "/",
                },
              ],
            },
          ],
        )
      end
    end

    context "when cookies are missing" do
      before do
        allow(GlobalState).to receive(:get).with(
          "furaffinity-cookie-a",
        ).and_return(nil)
        allow(GlobalState).to receive(:get).with(
          "furaffinity-cookie-b",
        ).and_return("abcdef12-3456-7890-1234-567890abcdef")
        allow(GlobalState).to receive(:get).with(
          "furaffinity-cookie-oaid",
        ).and_return("0123456789abcdef0123456789abcdef")
      end

      it "raises an error" do
        expect { config.cookies }.to raise_error("a cookie is not set")
      end
    end

    context "when cookies have invalid format" do
      before do
        allow(GlobalState).to receive(:get).with(
          "furaffinity-cookie-a",
        ).and_return("12345678-1234-1234-1234-123456789abc")
        allow(GlobalState).to receive(:get).with(
          "furaffinity-cookie-b",
        ).and_return("invalid-uuid-format")
        allow(GlobalState).to receive(:get).with(
          "furaffinity-cookie-oaid",
        ).and_return("0123456789abcdef0123456789abcdef")
      end

      it "raises an error" do
        expect { config.cookies }.to raise_error("b cookie has invalid format")
      end
    end
  end

  describe "#allowed_domains" do
    it "returns the default allowed domains" do
      config = described_class.new
      expect(config.allowed_domains).to eq(
        %w[*.furaffinity.net *.facdn.net ipinfo.io],
      )
    end
  end

  describe "#redirect_limit" do
    it "returns the configured redirect limit" do
      config = described_class.new
      expect(config.redirect_limit).to eq(4)
    end
  end
end
