|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'mindee' |
| 4 | + |
| 5 | +RSpec.describe Mindee::Parsing::V2::JobWebhook do |
| 6 | + describe '#initialize' do |
| 7 | + context 'when error key is present but value is nil' do |
| 8 | + it 'does not raise an error and sets @error to nil' do |
| 9 | + server_response = { |
| 10 | + 'id' => '12345678-1234-1234-1234-123456789012', |
| 11 | + 'status' => 'Processing', |
| 12 | + 'error' => nil, |
| 13 | + } |
| 14 | + |
| 15 | + webhook = described_class.new(server_response) |
| 16 | + |
| 17 | + expect(webhook.id).to eq('12345678-1234-1234-1234-123456789012') |
| 18 | + expect(webhook.status).to eq('Processing') |
| 19 | + expect(webhook.error).to be_nil |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + context 'when error key is absent' do |
| 24 | + it 'does not raise an error and sets @error to nil' do |
| 25 | + server_response = { |
| 26 | + 'id' => '12345678-1234-1234-1234-123456789012', |
| 27 | + 'status' => 'Processing', |
| 28 | + } |
| 29 | + |
| 30 | + webhook = described_class.new(server_response) |
| 31 | + |
| 32 | + expect(webhook.id).to eq('12345678-1234-1234-1234-123456789012') |
| 33 | + expect(webhook.status).to eq('Processing') |
| 34 | + expect(webhook.error).to be_nil |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'when error key is present with valid error data' do |
| 39 | + it 'creates an ErrorResponse object' do |
| 40 | + server_response = { |
| 41 | + 'id' => '12345678-1234-1234-1234-123456789012', |
| 42 | + 'status' => 'Failed', |
| 43 | + 'error' => { |
| 44 | + 'status' => 500, |
| 45 | + 'detail' => 'Internal server error', |
| 46 | + 'title' => 'Server Error', |
| 47 | + 'code' => 'INTERNAL_ERROR', |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + webhook = described_class.new(server_response) |
| 52 | + |
| 53 | + expect(webhook.id).to eq('12345678-1234-1234-1234-123456789012') |
| 54 | + expect(webhook.status).to eq('Failed') |
| 55 | + expect(webhook.error).to be_a(Mindee::Parsing::V2::ErrorResponse) |
| 56 | + expect(webhook.error.status).to eq(500) |
| 57 | + expect(webhook.error.detail).to eq('Internal server error') |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | +end |
0 commit comments