# frozen_string_literal: true require "spec_helpers" describe Wayfarer::CLI::Job, cli: true, redis: true do include Wayfarer::Redis let(:url) { test_app_path("/hello_world") } let(:batch) { "my-batch" } subject(:cli) { Wayfarer::CLI::Runner } before do write_file "app/jobs/dummy_job.rb", <<~FILE class DummyJob < Wayfarer::Base end FILE end before { Wayfarer::CLI::Base.new.send(:load_environment) } describe "job perform" do it "performs the worker" do expect_any_instance_of(DummyJob).to receive(:perform).with(kind_of(Wayfarer::Task)) do |job| task = job.arguments.first task.metadata.job = job end cli.start(["job", "perform", "DummyJob", url]) end it "collects garbage" do expect_any_instance_of(Wayfarer::GC).to receive(:run).exactly(:once) cli.start(["job", "perform", "DummyJob", url]) end context "using MockRedis" do it "performs the worker using MockRedis" do cli.start(["job", "perform", "--mock-redis", "DummyJob", url]) expect(Wayfarer.config.redis.factory.call(nil)).to be_a(MockRedis) end end end describe "job enqueue" do it "enqueues the job" do expect(DummyJob).to receive(:crawl).with(Addressable::URI.parse(url), batch: kind_of(String)) cli.start(["job", "enqueue", "DummyJob", url]) end context "with batch provided" do it "enqueues the job" do expect(DummyJob).to receive(:crawl).with(Addressable::URI.parse(url), batch: batch) cli.start(["job", "enqueue", "--batch", batch, "DummyJob", url]) end end end describe "job execute" do it "executes the job" do expect(DummyJob).to receive(:crawl).with(Addressable::URI.parse(url), batch: kind_of(String)) cli.start(["job", "execute", "DummyJob", url]) end context "with batch provided" do it "enqueues the job" do expect(DummyJob).to receive(:crawl).with(Addressable::URI.parse(url), batch: batch) cli.start(["job", "execute", "--batch", batch, "DummyJob", url]) end end context "using MockRedis" do it "performs the worker using MockRedis" do cli.start(["job", "execute", "--mock-redis", "DummyJob", url]) expect(Wayfarer.config.redis.factory.call(nil)).to be_a(MockRedis) end end end end