fix: captain json parsing (#13708)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
35
spec/enterprise/services/llm/base_ai_service_spec.rb
Normal file
35
spec/enterprise/services/llm/base_ai_service_spec.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
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
|
||||
Reference in New Issue
Block a user