# frozen_string_literal: true require "spec_helpers" describe Wayfarer::Middleware::Stage do let(:task) { build(:task) } subject { described_class.new } describe "#call" do it "assigns an empty set" do subject.call(task) expect(task[:staged_urls]).to eq(SortedSet.new) end it "yields" do expect { |spy| subject.call(task, &spy) }.to yield_control end it "enqueues tasks" do urls = [test_app_path("/alpha"), test_app_path("/beta")] spy.tap do |job| expect(job).to receive(:crawl).with(urls.first, batch: task.batch).ordered expect(job).to receive(:crawl).with(urls.second, batch: task.batch).ordered task[:job] = double(class: job) end subject.call(task) do task[:staged_urls] = SortedSet.new(urls) end end it "resets staged URLs" do task[:staged_urls] = SortedSet.new([test_app_path("/foo")]) expect { subject.call(task) }.to change { task[:staged_urls].count }.to(0) end end describe described_class::API do subject(:controller) do Struct.new(:task).include(described_class).new(task) end describe "#stage" do before { task[:staged_urls] = SortedSet.new } it "stages URLs" do expect { controller.stage(test_app_path("/foo")) }.to change { task[:staged_urls].count }.by(1) end it "converts to strings" do controller.stage(Addressable::URI.parse(test_app_path("/foo"))) expect(task[:staged_urls].to_a.first).to be_a(String) end end end end