Add tests for the Conversation object

This commit is contained in:
Stanko K.R.
2025-08-06 17:09:43 +02:00
parent e49c7cb1cb
commit 09739fdba4
5 changed files with 99 additions and 21 deletions
+8
View File
@@ -13,4 +13,12 @@ layout_overflowing_david:
name: body
body: The text is overflowing the container.
kevins_conversation_question:
record: kevin_question (Conversation::Message)
name: content
body: "Who is on a hot-streak?"
kevins_conversation_answer:
record: kevin_answer (Conversation::Message)
name: content
body: "<b>David</b> is on a hot-streak. He has recently added many cards to Writebook."
+13 -8
View File
@@ -1,9 +1,14 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
kevin_question:
conversation: kevin
role: user
client_message_id: djtawmq4vnp9f8es
model_id: "gpt-4.1-2025-04-14"
input_tokens: 3765
output_tokens: 331
input_cost_microcents: 753
output_cost_microcents: 300
one:
conversation: one
role: MyString
two:
conversation: two
role: MyString
kevin_answer:
conversation: kevin
role: assistant
client_message_id: rhwno26j4o
+3 -7
View File
@@ -1,7 +1,3 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
user: one
two:
user: two
kevin:
user: kevin
state: ready
+7 -3
View File
@@ -1,7 +1,11 @@
require "test_helper"
class Conversation::MessageTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "emoji content" do
message = Conversation::Message.new(content: "Hello 😊")
assert_not message.all_emoji?, "Message with mixed content should not be all emoji"
message.content = "😊"
assert message.all_emoji?, "Message with only emoji should be recognized as all emoji"
end
end
+68 -3
View File
@@ -1,7 +1,72 @@
require "test_helper"
class ConversationTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
include VcrTestHelper
test "asking questions" do
conversation = users(:kevin).conversation
assert_raises(ArgumentError) do
conversation.ask("")
end
assert conversation.ready?, "The conversation should be ready before a question is asked"
message = nil
assert_turbo_stream_broadcasts [ conversation, :message_list ], +1 do
assert_turbo_stream_broadcasts [ conversation, :thinking_indicator ], +1 do
message = conversation.ask("What is the meaning of life, the Universe, and everything else?", client_message_id: "deep-thought")
end
end
assert_not_nil message, "A message should be created when a question is asked"
assert message.persisted?, "The message should be saved to the database"
assert_equal "What is the meaning of life, the Universe, and everything else?", message.content.to_plain_text.chomp, "The message content should match the question asked"
assert_equal :user, message.role, "The message role should be 'user' for a question"
assert_equal "deep-thought", message.client_message_id, "Additional attributes should be set correctly"
assert conversation.thinking?, "The conversation should switch to thinking after a question is asked"
end
test "responding to questions" do
conversation = users(:kevin).conversation
conversation.ask("What is the meaning of life, the Universe, and everything else?")
assert_raises(ArgumentError) do
conversation.respond("")
end
assert conversation.thinking?, "The conversation should be thinking before a response is made"
message = nil
assert_turbo_stream_broadcasts [ conversation, :message_list ], +1 do
assert_turbo_stream_broadcasts [ conversation, :thinking_indicator ], +1 do
message = conversation.respond("42", client_message_id: "deep-thought-response")
end
end
assert_not_nil message, "A message should be created when a response is made"
assert message.persisted?, "The message should be saved to the database"
assert_equal "42", message.content.to_plain_text.chomp, "The message content should match the response given"
assert_equal :assistant, message.role, "The message role should be 'assistant' for a response"
assert_equal "deep-thought-response", message.client_message_id, "Asdditional attributes should be set correctly"
assert conversation.ready?, "The conversation should switch back to ready after a response is made"
end
test "clearing conversation messages" do
conversation = conversations(:kevin)
assert conversation.messages.any?, "The conversation should have messages before clearing"
original_updated_at = conversation.updated_at
conversation.clear
assert conversation.updated_at > original_updated_at, "The conversation's updated_at timestamp should change after clearing messages"
assert conversation.messages.empty?, "All messages should be deleted when clearing the conversation"
end
test "cost calculation" do
conversation = conversations(:kevin)
assert_equal "0.001053".to_d, conversation.cost
end
end