Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarKhater-sws committed Oct 15, 2024
2 parents 6b26997 + 38965ad commit 662b934
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
10 changes: 6 additions & 4 deletions app/controllers/chat_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
class ChatController < ApplicationController
#def index
#end

def chat
user_input = params[:user_input]

chat_service = ChatService.new
response = chat_service.call(user_input)

render json: { response: response }
if response.nil? || response.empty?
render json: { error: 'No response from OpenAI' }, status: :unprocessable_entity
else
render json: { response: response }, status: :ok
end
end
end
50 changes: 50 additions & 0 deletions spec/controllers/chat_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,56 @@

RSpec.describe ChatController, type: :controller do
describe "POST#chat" do
let(:valid_response) { "\n\nHello there, how may I assist you today?" }
let(:invalid_response) { nil }

context "with a valid API key" do
before do
# Mocking the ChatService to return a valid response
chat_service = instance_double("ChatService")
allow(ChatService).to receive(:new).and_return(chat_service)
allow(chat_service).to receive(:call).and_return(valid_response)
end

it 'sends a new message to the AI chat bot and validates the response body' do
# Trigger the controller action with a valid user_input
post :chat, params: { user_input: "Hello" }

# Parse the response body
parsed_response = JSON.parse(response.body)

# Validate that the response has the expected structure
expect(parsed_response).to have_key("response")
expect(parsed_response["response"]).to eq(valid_response)

# Additionally, check that the HTTP status is successful (200)
expect(response).to have_http_status(:success)
end
end

context "with an invalid API key or failed response" do
before do
# Mocking the ChatService to return an invalid response (nil)
chat_service = instance_double("ChatService")
allow(ChatService).to receive(:new).and_return(chat_service)
allow(chat_service).to receive(:call).and_return(invalid_response)
end

it 'handles the failed response gracefully' do
# Trigger the controller action with a valid user_input
post :chat, params: { user_input: "Hello" }

# Parse the response body
parsed_response = JSON.parse(response.body)

# Validate that the response contains an error message
expect(parsed_response).to have_key("error")
expect(parsed_response["error"]).to eq("No response from OpenAI")

# Additionally, check that the HTTP status reflects unprocessable entity (422)
expect(response).to have_http_status(:unprocessable_entity)
end
=======
it 'sends a new message to the AI chat bot and validates the response body' do
# Mock the ChatService to avoid making actual API requests
chat_service = instance_double("ChatService")
Expand Down
5 changes: 1 addition & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

require 'simplecov'
require 'simplecov_json_formatter'
SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::JSONFormatter
])
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
SimpleCov.start 'rails'

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
Expand Down

0 comments on commit 662b934

Please sign in to comment.