add proper tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
class Captain::Documents::PdfExtractionJob < ApplicationJob
|
||||
retry_on ActiveStorage::FileNotFoundError, attempts: 3, wait: 2.seconds, on: :perform
|
||||
retry_on Faraday::BadRequestError, attempts: 3, wait: 2.seconds, on: :perform
|
||||
retry_on ActiveStorage::FileNotFoundError, attempts: 3, wait: 2.seconds
|
||||
retry_on Faraday::BadRequestError, attempts: 3, wait: 2.seconds
|
||||
|
||||
queue_as :low
|
||||
|
||||
|
||||
@@ -41,10 +41,27 @@ class Captain::Tools::PdfExtractionService
|
||||
log_extraction_success(chunked_content)
|
||||
|
||||
success_response(chunked_content)
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "PDF extraction failed: #{e.message}"
|
||||
failure_response([e.message])
|
||||
end
|
||||
|
||||
def log_extraction_success(chunked_content)
|
||||
total_chars = chunked_content.sum { |chunk| chunk[:content].length }
|
||||
Rails.logger.info "PDF extraction completed: #{chunked_content.length} chunks, #{total_chars} characters"
|
||||
end
|
||||
|
||||
def success_response(content)
|
||||
{
|
||||
success: true,
|
||||
content: content
|
||||
}
|
||||
end
|
||||
|
||||
def failure_response(errors)
|
||||
{
|
||||
success: false,
|
||||
errors: errors
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -27,9 +27,9 @@ module Captain::Tools::PdfExtractionService::SourceHandler
|
||||
|
||||
temp_file = Down.download(
|
||||
pdf_source,
|
||||
max_size: MAX_PDF_SIZE,
|
||||
open_timeout: DOWNLOAD_TIMEOUT,
|
||||
read_timeout: DOWNLOAD_TIMEOUT
|
||||
max_size: self.class::MAX_PDF_SIZE,
|
||||
open_timeout: self.class::DOWNLOAD_TIMEOUT,
|
||||
read_timeout: self.class::DOWNLOAD_TIMEOUT
|
||||
)
|
||||
|
||||
begin
|
||||
|
||||
@@ -12,6 +12,12 @@ module Captain::Tools::PdfExtractionService::TextProcessor
|
||||
end
|
||||
|
||||
text_content
|
||||
rescue PDF::Reader::MalformedPDFError => e
|
||||
Rails.logger.error "Malformed PDF error: #{e.message}"
|
||||
raise StandardError, 'Invalid or corrupted PDF file'
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "PDF extraction error: #{e.message}"
|
||||
raise StandardError, "Failed to extract text from PDF: #{e.message}"
|
||||
end
|
||||
|
||||
def extract_page_content(page, index)
|
||||
|
||||
@@ -2,7 +2,7 @@ require 'rails_helper'
|
||||
|
||||
RSpec.describe Captain::Tools::PdfExtractionService do
|
||||
let(:service) { described_class.new(pdf_source) }
|
||||
let(:sample_pdf_path) { Rails.root.join('spec/fixtures/files/sample.pdf') }
|
||||
let(:sample_pdf_path) { Rails.root.join('spec/fixtures/files/valid_test.pdf') }
|
||||
|
||||
describe '#initialize' do
|
||||
context 'with valid PDF path' do
|
||||
@@ -69,12 +69,16 @@ RSpec.describe Captain::Tools::PdfExtractionService do
|
||||
end
|
||||
|
||||
context 'with malformed PDF' do
|
||||
let(:pdf_source) { Rails.root.join('spec/fixtures/files/sample.txt').to_s }
|
||||
let(:pdf_source) { sample_pdf_path.to_s }
|
||||
|
||||
it 'handles malformed PDF gracefully' do
|
||||
# Mock PDF::Reader to raise a MalformedPDFError
|
||||
allow(PDF::Reader).to receive(:open).and_raise(PDF::Reader::MalformedPDFError, 'PDF does not contain EOF marker')
|
||||
|
||||
result = service.perform
|
||||
expect(result[:success]).to be false
|
||||
expect(result[:errors]).to be_an(Array)
|
||||
expect(result[:errors]).to include('Invalid or corrupted PDF file')
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user