Skip to content

Commit

Permalink
fixes most tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlvallelonga committed Nov 5, 2024
1 parent 5e1ae7a commit 09cf14b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/active_storage/service/postgresql_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def generate_url(key, expires_in:, filename:, disposition:, content_type:)
)

generated_url = url_helpers.rails_postgresql_service_url(verified_key_with_expiration,
only_path: true, # This fixes an exception with attachment URL generation from a worker: https://github.com/AllYourBot/hostedgpt/pull/398#issuecomment-2168135853
**url_options,
disposition: content_disposition,
content_type: content_type,
filename: filename,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ActiveStorage::PostgresqlControllerTest < ActionDispatch::IntegrationTest
blob.delete

get blob.send(url_method)
assert_response :not_found
end

test "showing blob with invalid key" do
Expand Down
76 changes: 40 additions & 36 deletions test/jobs/get_next_ai_message_job_openai_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ class GetNextAIMessageJobOpenaiTest < ActiveJob::TestCase
@conversation.messages.create! role: :user, content_text: "Still there?", assistant: @assistant
@assistant.language_model.update!(supports_tools: false) # this will change the TestClient response so we want to be selective about this
@message = @conversation.latest_message_for_version(:latest)

p "#########"
@image_generation = conversations(:image_generation)
p @image_generation
p @image_generation.messages.create! role: :user, content_text: "Generate an image", assistant: @image_generation.assistant
p @image_generation.messages.length
@image_generation_message = @image_generation.latest_message_for_version(:latest)
p @image_generation_message

@user = @conversation.user
@test_client = TestClient::OpenAI.new(access_token: "abc")
end

Expand Down Expand Up @@ -67,49 +57,63 @@ class GetNextAIMessageJobOpenaiTest < ActiveJob::TestCase
end

test "properly handles a tool response call from the assistant when images are included" do
@image_generation = conversations(:image_generation)
@image_generation.messages.create! role: :user, content_text: "Generate an image", assistant: @image_generation.assistant
@image_generation_message = @image_generation.latest_message_for_version(:latest)

@image_generation.assistant.language_model.update!(supports_tools: true)

image_generation_prompt = "Kitten"

response = {
data: [{
url: "https://example.com/image.jpg"
}]
}

images_mock = Minitest::Mock.new
images_mock.expect :generate, response, parameters: {
prompt: image_generation_prompt,
model: "dall-e-3",
size: "1024x1792",
quality: "standard"
}

assert_difference "@image_generation.messages.reload.length", 2 do
TestClient::OpenAI.stub :function, "generate_an_image" do
TestClient::OpenAI.stub :arguments, { image_generation_prompt_s: "Kitten" } do
TestClient::OpenAI.stub :api_response, TestClient::OpenAI.api_function_response do
assert GetNextAIMessageJob.perform_now(@user.id, @message.id, @image_generation.assistant.id)
OpenAI::Client.stub_any_instance :images, images_mock do
TestClient::OpenAI.stub :function, "dalle_generate_an_image" do
TestClient::OpenAI.stub :arguments, { :image_generation_prompt=>image_generation_prompt } do
assert GetNextAIMessageJob.perform_now(@user.id, @image_generation_message.id, @image_generation.assistant.id)
end
end
end
end

p "###### Message:"
p @image_generation_message

@image_generation_message.reload
assert @image_generation_message.content_text.blank?
assert @image_generation_message.tool_call_id.nil?
assert @image_generation_message.content_tool_calls.present?, "Assistant should have decided to call a tool"

@new_messages = @image_generation.messages.where("id > ?", @message.id).order(:created_at)

p "###### Image generation:"
p @image_generation
@new_messages.messages.each_with_index do |message, index|
p "######## #{index}:"
p message
end
@new_messages = @image_generation.messages.where("id > ?", @image_generation_message.id).order(:created_at)

# first
first_new_message = @new_messages.first
assert first_new_message.tool?
# assert_equal "Hello, Keith!".to_json, first_new_message.content_text, "First new message should have the result of calling the tool"
# assert first_new_message.tool_call_id.present?
# assert first_new_message.content_tool_calls.blank?
# assert_equal @message.content_tool_calls.dig(0, :id), first_new_message.tool_call_id, "ID of tool execution should have matched decision to call the tool"
# assert first_new_message.finished?, "This message SHOULD HAVE been considered finished"
content_text = first_new_message.content_text
json_content_text = JSON.parse(content_text)
assert_equal image_generation_prompt, json_content_text["prompt_given"], "First new message should have the result of calling the tool"
assert first_new_message.tool_call_id.present?
assert first_new_message.content_tool_calls.blank?
assert_equal @image_generation_message.content_tool_calls.dig(0, :id), first_new_message.tool_call_id, "ID of tool execution should have matched decision to call the tool"
assert first_new_message.finished?, "This message SHOULD HAVE been considered finished"

# second
# second_new_message = @new_messages.second
# assert second_new_message.assistant?, "Second new message should be queued up for the assistant to reply"
# assert second_new_message.content_text.nil?, "The content should be nil to indicate that it hasn't even started processing"
# assert second_new_message.tool_call_id.nil?
# assert second_new_message.content_tool_calls.blank?
# refute second_new_message.finished?, "This message SHOULD NOT be considered finished yet"
second_new_message = @new_messages.second
assert second_new_message.assistant?, "Second new message should be queued up for the assistant to reply"
assert second_new_message.content_text.nil?, "The content should be nil to indicate that it hasn't even started processing"
assert second_new_message.tool_call_id.nil?
assert second_new_message.content_tool_calls.blank?
refute second_new_message.finished?, "This message SHOULD NOT be considered finished yet"
end

test "returns early if the message id was invalid" do
Expand Down
2 changes: 1 addition & 1 deletion test/lib/active_storage/postgresql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ActiveStorage::PostgresqlTest < ActiveSupport::TestCase
end

test "url generation" do
assert_match(/^\/rails\/active_storage\/postgresql\/.*\/avatar\.png\?content_type=image%2Fpng&disposition=inline/,
assert_match(/^https:\/\/example.com\/rails\/active_storage\/postgresql\/.*\/avatar\.png\?content_type=image%2Fpng&disposition=inline/,
@service.url(FIXTURE_KEY, expires_in: 5.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png"))
end

Expand Down
2 changes: 1 addition & 1 deletion test/lib/active_storage/service/public_postgresql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class ActiveStorage::Service::PublicPostgresqlTest < ActiveSupport::TestCase
test "public URL generation" do
url = @service.url(@key, disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png")

assert_match(/^\/rails\/active_storage\/postgresql\/.*\/avatar\.png/, url)
assert_match(/^https:\/\/example.com\/rails\/active_storage\/postgresql\/.*\/avatar\.png/, url)
end
end
4 changes: 3 additions & 1 deletion test/models/api_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class APIServiceTest < ActiveSupport::TestCase
end

test "can create record" do
APIService.create!(create_params)
assert_nothing_raised do
APIService.create!(create_params)
end
end

test "soft delete also soft deletes language_models" do
Expand Down
6 changes: 4 additions & 2 deletions test/models/message/version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ class Message::VersionTest < ActiveSupport::TestCase
end

test "creating a message with branched true AND branched_from_version specified SUCCEEDS" do
Current.user = users(:keith)
conversations(:versioned).messages.create!(assistant: assistants(:samantha), content_text: "What is your name?", index: 2, version: 3, branched: true, branched_from_version: 2)
assert_nothing_raised do
Current.user = users(:keith)
conversations(:versioned).messages.create!(assistant: assistants(:samantha), content_text: "What is your name?", index: 2, version: 3, branched: true, branched_from_version: 2)
end
end

test "creating a new messages for a SPECIFIC INDEX and SPECIFIC VERSION fails if the VERSION is SKIPPING a number" do
Expand Down
12 changes: 6 additions & 6 deletions test/services/ai_backend/anthropic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class AIBackend::AnthropicTest < ActiveSupport::TestCase
end
end

test "preceding_conversation_messages only considers messages on the intended conversation version and includes the correct names" do
# TODO
end
# test "preceding_conversation_messages only considers messages on the intended conversation version and includes the correct names" do
# # TODO
# end

test "preceding_conversation_messages includes the appropriate tool details" do
# TODO
end
# test "preceding_conversation_messages includes the appropriate tool details" do
# # TODO
# end
end

0 comments on commit 09cf14b

Please sign in to comment.