# frozen_string_literal: true require "spec_helpers" describe Wayfarer::Middleware::Worker do let(:task) { build(:task) } let(:job) do Class.new(Wayfarer::Base) do def index :foobar end end end before do task.metadata.action = :index allow_any_instance_of(job).to receive(:arguments).and_return([task]) end describe "::router" do it "returns a router" do expect(job.router).to be_a(Wayfarer::Routing::Router) end end describe "#call" do it "calls the action method" do worker = job.new expect(worker).to receive(:index) worker.call(task) end it "yields" do expect { |spy| job.new.call(task, &spy) }.to yield_control end end describe "#chain" do it "returns its middleware chain" do worker = job.new expect(worker.chain.middlewares).to eq([*Wayfarer.middleware, worker]) end end describe "#redis" do it "yields a Redis connection" do job.new.redis do |conn| expect(conn).to be_a(Redis) end end end describe "#params" do it "delegates to task metadata" do worker = job.new expect(worker.task.metadata).to receive(:params) worker.params end end describe "#agent" do it "delegates to task metadata" do worker = job.new expect(worker.task.metadata).to receive(:agent) worker.agent end end describe "#page" do let(:worker) { job.new } let(:page) { Object.new } it "returns the page" do task.metadata.page = page expect(worker.page).to be(page) end context "with live keyword" do let(:agent) { spy } let(:result) { Wayfarer::Networking::Result::Success.new(page) } before { task.metadata.agent = agent } context "when agent returns Result" do before { allow(agent).to receive(:live).and_return(result) } it "returns the page" do expect(worker.page(live: true)).to be(page) end it "replaces the existing page" do expect { worker.page(live: true) }.to change { task.metadata.page }.to(page) end end context "when agent returns nil" do before do task.metadata.page = page allow(agent).to receive(:live).and_return(nil) end it "returns the existing page" do expect(worker.page(live: true)).to be(page) end it "does not replace the existing page" do expect { worker.page(live: true) }.not_to(change { task.metadata.page }) end end end end end