From 09739fdba4bbcf3c7d95abc6d88615e05fe7fa7a Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Wed, 6 Aug 2025 17:09:43 +0200 Subject: [PATCH] Add tests for the Conversation object --- test/fixtures/action_text/rich_texts.yml | 8 +++ test/fixtures/conversation/messages.yml | 21 ++++--- test/fixtures/conversations.yml | 10 +--- test/models/conversation/message_test.rb | 10 +++- test/models/conversation_test.rb | 71 +++++++++++++++++++++++- 5 files changed, 99 insertions(+), 21 deletions(-) diff --git a/test/fixtures/action_text/rich_texts.yml b/test/fixtures/action_text/rich_texts.yml index 9a86c3b30..987a7b17d 100644 --- a/test/fixtures/action_text/rich_texts.yml +++ b/test/fixtures/action_text/rich_texts.yml @@ -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: "David is on a hot-streak. He has recently added many cards to Writebook." diff --git a/test/fixtures/conversation/messages.yml b/test/fixtures/conversation/messages.yml index 7cdb27dd0..afdfbb3ea 100644 --- a/test/fixtures/conversation/messages.yml +++ b/test/fixtures/conversation/messages.yml @@ -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 diff --git a/test/fixtures/conversations.yml b/test/fixtures/conversations.yml index 6dc1ab362..b82ab29a6 100644 --- a/test/fixtures/conversations.yml +++ b/test/fixtures/conversations.yml @@ -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 diff --git a/test/models/conversation/message_test.rb b/test/models/conversation/message_test.rb index f233d579b..1320db819 100644 --- a/test/models/conversation/message_test.rb +++ b/test/models/conversation/message_test.rb @@ -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 diff --git a/test/models/conversation_test.rb b/test/models/conversation_test.rb index 0c0d00ac7..0ca1c6790 100644 --- a/test/models/conversation_test.rb +++ b/test/models/conversation_test.rb @@ -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