|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe "the wait_for_action_attempt option" do |
| 4 | + let(:api_key) { "seam_some_api_key" } |
| 5 | + |
| 6 | + def invalid_options_error(message) |
| 7 | + raise_error(Seam::Http::Options::SeamInvalidOptionsError, "Seam received invalid options: #{message}") |
| 8 | + end |
| 9 | + |
| 10 | + describe "on the client" do |
| 11 | + it "rejects a value that is neither a boolean nor a hash" do |
| 12 | + expect { Seam.new(api_key: api_key, wait_for_action_attempt: 1) }.to invalid_options_error( |
| 13 | + 'The wait_for_action_attempt option must be true, false, or a hash with "timeout" and "polling_interval" keys, got Integer' |
| 14 | + ) |
| 15 | + end |
| 16 | + |
| 17 | + it "rejects a value that is neither a boolean nor a hash on the without-workspace client" do |
| 18 | + expect do |
| 19 | + Seam::Http::WithoutWorkspace.new(personal_access_token: "seam_at_token", wait_for_action_attempt: "true") |
| 20 | + end.to invalid_options_error( |
| 21 | + 'The wait_for_action_attempt option must be true, false, or a hash with "timeout" and "polling_interval" keys, got String' |
| 22 | + ) |
| 23 | + end |
| 24 | + |
| 25 | + it "rejects an unknown option key" do |
| 26 | + expect { Seam.new(api_key: api_key, wait_for_action_attempt: {poll_interval: 1}) }.to invalid_options_error( |
| 27 | + 'The wait_for_action_attempt option got an unknown key :poll_interval, expected "timeout" or "polling_interval"' |
| 28 | + ) |
| 29 | + end |
| 30 | + |
| 31 | + it "rejects a non-numeric option value" do |
| 32 | + expect { Seam.new(api_key: api_key, wait_for_action_attempt: {timeout: "5"}) }.to invalid_options_error( |
| 33 | + "The wait_for_action_attempt option :timeout must be a number, got String" |
| 34 | + ) |
| 35 | + end |
| 36 | + |
| 37 | + it "rejects a boolean option value" do |
| 38 | + expect { Seam.new(api_key: api_key, wait_for_action_attempt: {timeout: true}) }.to invalid_options_error( |
| 39 | + "The wait_for_action_attempt option :timeout must be a number, got TrueClass" |
| 40 | + ) |
| 41 | + end |
| 42 | + |
| 43 | + it "treats nil as the default" do |
| 44 | + seam = Seam.new(api_key: api_key, wait_for_action_attempt: nil) |
| 45 | + |
| 46 | + expect(seam.defaults.wait_for_action_attempt).to be true |
| 47 | + end |
| 48 | + |
| 49 | + it "accepts string keys" do |
| 50 | + seam = Seam.new(api_key: api_key, wait_for_action_attempt: {"timeout" => 1, "polling_interval" => 0.1}) |
| 51 | + |
| 52 | + expect(seam.defaults.wait_for_action_attempt.to_h).to eq("timeout" => 1, "polling_interval" => 0.1) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + describe "on a route call", recorder: true do |
| 57 | + def pending_action_attempt |
| 58 | + {action_attempt: {action_attempt_id: "attempt-1", action_type: "UNLOCK_DOOR", status: "pending"}}.to_json |
| 59 | + end |
| 60 | + |
| 61 | + def unlock_door(wait_for_action_attempt) |
| 62 | + seam.locks.unlock_door(device_id: "device-1", wait_for_action_attempt: wait_for_action_attempt) |
| 63 | + end |
| 64 | + |
| 65 | + before { recorder.respond_with(pending_action_attempt) } |
| 66 | + |
| 67 | + it "rejects a truthy value that is not true before polling" do |
| 68 | + expect { unlock_door(1) }.to invalid_options_error( |
| 69 | + 'The wait_for_action_attempt option must be true, false, or a hash with "timeout" and "polling_interval" keys, got Integer' |
| 70 | + ) |
| 71 | + |
| 72 | + expect(recorder.requests.map(&:path)).to eq(["/locks/unlock_door"]) |
| 73 | + end |
| 74 | + |
| 75 | + it "rejects an unknown option key before polling" do |
| 76 | + expect { unlock_door({poll_interval: 1}) }.to invalid_options_error( |
| 77 | + 'The wait_for_action_attempt option got an unknown key :poll_interval, expected "timeout" or "polling_interval"' |
| 78 | + ) |
| 79 | + |
| 80 | + expect(recorder.requests.map(&:path)).to eq(["/locks/unlock_door"]) |
| 81 | + end |
| 82 | + |
| 83 | + it "honors string option keys" do |
| 84 | + expect { unlock_door({"timeout" => 0.1, "polling_interval" => 3}) }.to raise_error(Seam::ActionAttemptTimeoutError) |
| 85 | + |
| 86 | + expect(recorder.requests.map(&:path)).to eq(["/locks/unlock_door", "/action_attempts/get"]) |
| 87 | + end |
| 88 | + |
| 89 | + it "honors string option keys set on the client" do |
| 90 | + seam = Seam.new(api_key: "seam_some_api_key", endpoint: recorder.endpoint, |
| 91 | + wait_for_action_attempt: {"timeout" => 0.1, "polling_interval" => 3}) |
| 92 | + |
| 93 | + expect { seam.locks.unlock_door(device_id: "device-1") }.to raise_error(Seam::ActionAttemptTimeoutError) |
| 94 | + |
| 95 | + expect(recorder.requests.map(&:path)).to eq(["/locks/unlock_door", "/action_attempts/get"]) |
| 96 | + end |
| 97 | + end |
| 98 | +end |
0 commit comments