Files
leadchat/spec/enterprise/services/llm/base_ai_service_spec.rb
Aakash Bakhle fd69b4c8f2 fix: captain json parsing (#13708)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
2026-03-05 15:43:21 +05:30

36 lines
1.1 KiB
Ruby

require 'rails_helper'
RSpec.describe Llm::BaseAiService do
subject(:service) { described_class.new }
before do
create(:installation_config, name: 'CAPTAIN_OPEN_AI_API_KEY', value: 'test-key')
end
describe '#sanitize_json_response' do
it 'strips ```json fences' do
input = "```json\n{\"key\": \"value\"}\n```"
expect(service.send(:sanitize_json_response, input)).to eq('{"key": "value"}')
end
it 'strips bare ``` fences' do
input = "```\n{\"key\": \"value\"}\n```"
expect(service.send(:sanitize_json_response, input)).to eq('{"key": "value"}')
end
it 'passes through plain JSON unchanged' do
input = '{"key": "value"}'
expect(service.send(:sanitize_json_response, input)).to eq('{"key": "value"}')
end
it 'returns nil for nil input' do
expect(service.send(:sanitize_json_response, nil)).to be_nil
end
it 'strips surrounding whitespace' do
input = " \n{\"key\": \"value\"}\n "
expect(service.send(:sanitize_json_response, input)).to eq('{"key": "value"}')
end
end
end