diff --git a/spec/lib/answer_composition/pipeline/claude/structured_answer_composer_spec.rb b/spec/lib/answer_composition/pipeline/claude/structured_answer_composer_spec.rb index 7246179d..690c9343 100644 --- a/spec/lib/answer_composition/pipeline/claude/structured_answer_composer_spec.rb +++ b/spec/lib/answer_composition/pipeline/claude/structured_answer_composer_spec.rb @@ -3,33 +3,7 @@ let(:question) { build :question } let(:context) { build(:answer_pipeline_context, question:) } let(:response) do - [ - { - output: { - message: { - role: "assistant", - content: [ - { - tool_use: { - input: { "answer" => "VAT (Value Added Tax) is a tax applied to most goods and services in the UK." }, - tool_use_id: "tool_id", - name: "tool_name", - }, - }, - ], - }, - }, - stop_reason: "end_turn", - usage: { - input_tokens: 10, - output_tokens: 20, - total_tokens: 30, - }, - metrics: { - latency_ms: 999, - }, - }, - ] + stub_claude_structured_answer_reponse("VAT (Value Added Tax) is a tax applied to most goods and services in the UK.") end it "uses Bedrock converse endpoint to assign the correct values to the context's answer" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bd67ef71..96988b81 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -36,6 +36,7 @@ config.include MailerExamples config.include StubOpenAIChat config.include StubBedrockRequest + config.include StubClaudeConverse config.include PasswordlessRequestHelpers, type: :request config.include StubOpenAIEmbedding config.include SidekiqHelpers diff --git a/spec/support/stub_claude_converse.rb b/spec/support/stub_claude_converse.rb new file mode 100644 index 00000000..42d48395 --- /dev/null +++ b/spec/support/stub_claude_converse.rb @@ -0,0 +1,31 @@ +module StubClaudeConverse + def stub_claude_structured_answer_reponse(answer) + [ + { + output: { + message: { + role: "assistant", + content: [ + { + tool_use: { + input: { "answer" => answer }, + tool_use_id: "tool_id", + name: "tool_name", + }, + }, + ], + }, + }, + stop_reason: "end_turn", + usage: { + input_tokens: 10, + output_tokens: 20, + total_tokens: 30, + }, + metrics: { + latency_ms: 999, + }, + }, + ] + end +end diff --git a/spec/system/conversation_with_claude_structured_answer_spec.rb b/spec/system/conversation_with_claude_structured_answer_spec.rb new file mode 100644 index 00000000..bd612bde --- /dev/null +++ b/spec/system/conversation_with_claude_structured_answer_spec.rb @@ -0,0 +1,82 @@ +RSpec.describe "Conversation with Claude with a structured answer" do + scenario do + given_i_am_a_signed_in_early_access_user + and_i_have_confirmed_i_understand_chat_risks + when_i_visit_the_conversation_page + and_i_enter_a_question + then_i_see_the_answer_is_pending + + when_the_first_answer_is_generated + and_i_click_on_the_check_answer_button + then_i_see_my_question_on_the_page + and_i_can_see_the_first_answer + + when_i_enter_a_second_question + then_i_see_the_answer_is_pending + + when_the_second_answer_is_generated + and_i_click_on_the_check_answer_button + then_i_see_my_second_question_on_the_page + and_i_can_see_the_second_answer + end + + def when_i_visit_the_conversation_page + visit show_conversation_path + end + + def and_i_enter_a_question + @first_question = "How much tax should I be paying?" + fill_in "Message", with: @first_question + click_on "Send" + end + + def then_i_see_the_answer_is_pending + expect(page).to have_content("GOV.UK Chat is generating an answer") + end + + def when_the_first_answer_is_generated + @first_answer = "Lots of tax." + stub_bedrock_request( + :converse, + stub_claude_structured_answer_reponse(@first_answer), + ) + + execute_queued_sidekiq_jobs + end + + def when_the_second_answer_is_generated + @second_answer = "Even more tax." + stub_bedrock_request( + :converse, + stub_claude_structured_answer_reponse(@second_answer), + ) + + execute_queued_sidekiq_jobs + end + + def and_i_click_on_the_check_answer_button + click_on "Check if an answer has been generated" + end + + def then_i_see_my_question_on_the_page + expect(page).to have_content(@first_question) + end + + def then_i_see_my_second_question_on_the_page + expect(page).to have_content(@second_question) + end + + def and_i_can_see_the_first_answer + expect(page).to have_content(@first_answer) + end + + def and_i_can_see_the_second_answer + expect(page).to have_content(@second_answer) + end + + def when_i_enter_a_second_question + @second_question = "Are you sure?" + fill_in "Message", with: @second_question + click_on "Send" + end +end