From bc2e889d72edcd26c7bf2cb9c872c3494e242a80 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 11 Sep 2024 16:45:44 -0700 Subject: [PATCH] Update chat director, client tests to call chat API using new POST method --- tests/test_client.py | 5 +- tests/test_offline_chat_director.py | 148 +++++++++++++++++----------- tests/test_openai_chat_director.py | 118 ++++++++++++---------- 3 files changed, 160 insertions(+), 111 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index d5bc2ba1..b8284e4b 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -459,11 +459,12 @@ def test_user_no_data_returns_empty(client, sample_org_data, api_user3: KhojApiU @pytest.mark.django_db(transaction=True) async def test_chat_with_unauthenticated_user(chat_client_with_auth, api_user2: KhojApiUser): # Arrange + query = "Hello!" headers = {"Authorization": f"Bearer {api_user2.token}"} # Act - auth_response = chat_client_with_auth.post(f'/api/chat?q="Hello!"', headers=headers) - no_auth_response = chat_client_with_auth.post(f'/api/chat?q="Hello!"') + auth_response = chat_client_with_auth.post(f"/api/chat", json={"q": query}, headers=headers) + no_auth_response = chat_client_with_auth.post(f"/api/chat", json={"q": query}) # Assert assert auth_response.status_code == 200 diff --git a/tests/test_offline_chat_director.py b/tests/test_offline_chat_director.py index 82f53b69..afb5d4ce 100644 --- a/tests/test_offline_chat_director.py +++ b/tests/test_offline_chat_director.py @@ -1,6 +1,4 @@ -import os import urllib.parse -from urllib.parse import quote import pytest from faker import Faker @@ -49,7 +47,8 @@ def create_conversation(message_list, user, agent=None): @pytest.mark.django_db(transaction=True) def test_offline_chat_with_no_chat_history_or_retrieved_content(client_offline_chat): # Act - response = client_offline_chat.post(f'/api/chat?q="Hello, my name is Testatron. Who are you?"&stream=true') + query = "Hello, my name is Testatron. Who are you?" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -66,8 +65,7 @@ def test_offline_chat_with_no_chat_history_or_retrieved_content(client_offline_c def test_chat_with_online_content(client_offline_chat): # Act q = "/online give me the link to paul graham's essay how to do great work" - encoded_q = quote(q, safe="") - response = client_offline_chat.post(f"/api/chat?q={encoded_q}") + response = client_offline_chat.post(f"/api/chat", json={"q": q}) response_message = response.json()["response"] # Assert @@ -88,8 +86,7 @@ def test_chat_with_online_content(client_offline_chat): def test_chat_with_online_webpage_content(client_offline_chat): # Act q = "/online how many firefighters were involved in the great chicago fire and which year did it take place?" - encoded_q = quote(q, safe="") - response = client_offline_chat.post(f"/api/chat?q={encoded_q}") + response = client_offline_chat.post(f"/api/chat", json={"q": q}) response_message = response.json()["response"] # Assert @@ -112,7 +109,8 @@ def test_answer_from_chat_history(client_offline_chat, default_user2): create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f'/api/chat?q="What is my name?"&stream=true') + q = "What is my name?" + response = client_offline_chat.post(f"/api/chat", json={"q": q, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -139,7 +137,8 @@ def test_answer_from_currently_retrieved_content(client_offline_chat, default_us create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f'/api/chat?q="Where was Xi Li born?"') + q = "Where was Xi Li born?" + response = client_offline_chat.post(f"/api/chat", json={"q": q}) response_message = response.content.decode("utf-8") # Assert @@ -163,7 +162,8 @@ def test_answer_from_chat_history_and_previously_retrieved_content(client_offlin create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f'/api/chat?q="Where was I born?"') + q = "Where was I born?" + response = client_offline_chat.post(f"/api/chat", json={"q": q}) response_message = response.content.decode("utf-8") # Assert @@ -185,7 +185,8 @@ def test_answer_from_chat_history_and_currently_retrieved_content(client_offline create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f'/api/chat?q="Where was I born?"') + q = "Where was I born?" + response = client_offline_chat.post(f"/api/chat", json={"q": q}) response_message = response.content.decode("utf-8") # Assert @@ -211,7 +212,8 @@ def test_no_answer_in_chat_history_or_retrieved_content(client_offline_chat, def create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f'/api/chat?q="Where was I born?"&stream=true') + q = "Where was I born?" + response = client_offline_chat.post(f"/api/chat", json={"q": q, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -227,12 +229,12 @@ def test_no_answer_in_chat_history_or_retrieved_content(client_offline_chat, def @pytest.mark.django_db(transaction=True) def test_answer_using_general_command(client_offline_chat, default_user2): # Arrange - query = urllib.parse.quote("/general Where was Xi Li born?") message_list = [] create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f"/api/chat?q={query}&stream=true") + query = "/general Where was Xi Li born?" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -245,12 +247,12 @@ def test_answer_using_general_command(client_offline_chat, default_user2): @pytest.mark.django_db(transaction=True) def test_answer_from_retrieved_content_using_notes_command(client_offline_chat, default_user2): # Arrange - query = urllib.parse.quote("/notes Where was Xi Li born?") message_list = [] create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f"/api/chat?q={query}&stream=true") + query = "/notes Where was Xi Li born?" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -269,8 +271,12 @@ def test_answer_using_file_filter(client_offline_chat, default_user2): create_conversation(message_list, default_user2) # Act - no_answer_response = client_offline_chat.post(f"/api/chat?q={no_answer_query}&stream=true").content.decode("utf-8") - answer_response = client_offline_chat.post(f"/api/chat?q={answer_query}&stream=true").content.decode("utf-8") + no_answer_response = client_offline_chat.post( + f"/api/chat", json={"q": no_answer_query, "stream": True} + ).content.decode("utf-8") + answer_response = client_offline_chat.post(f"/api/chat", json={"q": answer_query, "stream": True}).content.decode( + "utf-8" + ) # Assert assert "Fujiang" not in no_answer_response @@ -282,12 +288,12 @@ def test_answer_using_file_filter(client_offline_chat, default_user2): @pytest.mark.django_db(transaction=True) def test_answer_not_known_using_notes_command(client_offline_chat, default_user2): # Arrange - query = urllib.parse.quote("/notes Where was Testatron born?") message_list = [] create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f"/api/chat?q={query}&stream=true") + query = urllib.parse.quote("/notes Where was Testatron born?") + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -299,6 +305,7 @@ def test_answer_not_known_using_notes_command(client_offline_chat, default_user2 @pytest.mark.django_db(transaction=True) @pytest.mark.chatquality def test_summarize_one_file(client_offline_chat, default_user2: KhojUser): + # Arrange message_list = [] conversation = create_conversation(message_list, default_user2) # post "Xi Li.markdown" file to the file filters @@ -319,9 +326,14 @@ def test_summarize_one_file(client_offline_chat, default_user2: KhojUser): "api/chat/conversation/file-filters", json={"filename": summarization_file, "conversation_id": str(conversation.id)}, ) - query = urllib.parse.quote("/summarize") - response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") + + # Act + query = "/summarize" + response = client_offline_chat.post( + f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True} + ) response_message = response.content.decode("utf-8") + # Assert assert response_message != "" assert response_message != "No files selected for summarization. Please add files using the section on the left." @@ -331,6 +343,7 @@ def test_summarize_one_file(client_offline_chat, default_user2: KhojUser): @pytest.mark.django_db(transaction=True) @pytest.mark.chatquality def test_summarize_extra_text(client_offline_chat, default_user2: KhojUser): + # Arrange message_list = [] conversation = create_conversation(message_list, default_user2) # post "Xi Li.markdown" file to the file filters @@ -351,9 +364,14 @@ def test_summarize_extra_text(client_offline_chat, default_user2: KhojUser): "api/chat/conversation/file-filters", json={"filename": summarization_file, "conversation_id": str(conversation.id)}, ) - query = urllib.parse.quote("/summarize tell me about Xiu") - response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") + + # Act + query = "/summarize tell me about Xiu" + response = client_offline_chat.post( + f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True} + ) response_message = response.content.decode("utf-8") + # Assert assert response_message != "" assert response_message != "No files selected for summarization. Please add files using the section on the left." @@ -363,6 +381,7 @@ def test_summarize_extra_text(client_offline_chat, default_user2: KhojUser): @pytest.mark.django_db(transaction=True) @pytest.mark.chatquality def test_summarize_multiple_files(client_offline_chat, default_user2: KhojUser): + # Arrange message_list = [] conversation = create_conversation(message_list, default_user2) # post "Xi Li.markdown" file to the file filters @@ -379,9 +398,10 @@ def test_summarize_multiple_files(client_offline_chat, default_user2: KhojUser): "api/chat/conversation/file-filters", json={"filename": file_list[1], "conversation_id": str(conversation.id)} ) - query = urllib.parse.quote("/summarize") - response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") - response_message = response.content.decode("utf-8") + # Act + query = "/summarize" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) + response_message = response.json()["response"] # Assert assert response_message == "Only one file can be selected for summarization." @@ -390,11 +410,13 @@ def test_summarize_multiple_files(client_offline_chat, default_user2: KhojUser): @pytest.mark.django_db(transaction=True) @pytest.mark.chatquality def test_summarize_no_files(client_offline_chat, default_user2: KhojUser): + # Arrange message_list = [] conversation = create_conversation(message_list, default_user2) - query = urllib.parse.quote("/summarize") - response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") - response_message = response.content.decode("utf-8") + # Act + query = "/summarize" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) + response_message = response.json()["response"] # Assert assert response_message == "No files selected for summarization. Please add files using the section on the left." @@ -424,16 +446,16 @@ def test_summarize_different_conversation(client_offline_chat, default_user2: Kh json={"filename": summarization_file, "conversation_id": str(conversation1.id)}, ) - query = urllib.parse.quote("/summarize") - response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation2.id}&stream=true") - response_message = response.content.decode("utf-8") + query = "/summarize" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation2.id}) + response_message = response.json()["response"] # Assert assert response_message == "No files selected for summarization. Please add files using the section on the left." # now make sure that the file filter is still in conversation 1 - response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation1.id}&stream=true") - response_message = response.content.decode("utf-8") + response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation1.id}) + response_message = response.json()["response"] # Assert assert response_message != "" @@ -444,6 +466,7 @@ def test_summarize_different_conversation(client_offline_chat, default_user2: Kh @pytest.mark.django_db(transaction=True) @pytest.mark.chatquality def test_summarize_nonexistant_file(client_offline_chat, default_user2: KhojUser): + # Arrange message_list = [] conversation = create_conversation(message_list, default_user2) # post "imaginary.markdown" file to the file filters @@ -451,9 +474,10 @@ def test_summarize_nonexistant_file(client_offline_chat, default_user2: KhojUser "api/chat/conversation/file-filters", json={"filename": "imaginary.markdown", "conversation_id": str(conversation.id)}, ) - query = urllib.parse.quote("/summarize") - response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") - response_message = response.content.decode("utf-8") + # Act + query = "/summarize" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) + response_message = response.json()["response"] # Assert assert response_message == "No files selected for summarization. Please add files using the section on the left." @@ -463,6 +487,7 @@ def test_summarize_nonexistant_file(client_offline_chat, default_user2: KhojUser def test_summarize_diff_user_file( client_offline_chat, default_user: KhojUser, pdf_configured_user1, default_user2: KhojUser ): + # Arrange message_list = [] conversation = create_conversation(message_list, default_user2) # Get the pdf file called singlepage.pdf @@ -482,9 +507,12 @@ def test_summarize_diff_user_file( "api/chat/conversation/file-filters", json={"filename": summarization_file, "conversation_id": str(conversation.id)}, ) - query = urllib.parse.quote("/summarize") - response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") - response_message = response.content.decode("utf-8") + + # Act + query = "/summarize" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) + response_message = response.json()["response"] + # Assert assert response_message == "No files selected for summarization. Please add files using the section on the left." @@ -495,11 +523,9 @@ def test_summarize_diff_user_file( @freeze_time("2023-04-01", ignore=["transformers"]) def test_answer_requires_current_date_awareness(client_offline_chat): "Chat actor should be able to answer questions relative to current date using provided notes" - # Arrange - query = urllib.parse.quote("Where did I have lunch today?") - # Act - response = client_offline_chat.post(f"/api/chat?q={query}&stream=true") + query = "Where did I have lunch today?" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -518,7 +544,8 @@ def test_answer_requires_current_date_awareness(client_offline_chat): def test_answer_requires_date_aware_aggregation_across_provided_notes(client_offline_chat): "Chat director should be able to answer questions that require date aware aggregation across multiple notes" # Act - response = client_offline_chat.post(f'/api/chat?q="How much did I spend on dining this year?"&stream=true') + query = "How much did I spend on dining this year?" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -539,9 +566,8 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(client create_conversation(message_list, default_user2) # Act - response = client_offline_chat.get( - f'/api/chat?q=""Write a haiku about unit testing. Do not say anything else."&stream=true' - ) + query = "Write a haiku about unit testing. Do not say anything else." + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -559,7 +585,8 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(client @pytest.mark.django_db(transaction=True) def test_ask_for_clarification_if_not_enough_context_in_question(client_offline_chat, default_user2): # Act - response = client_offline_chat.post(f'/api/chat?q="What is the name of Namitas older son"&stream=true') + query = "What is the name of Namitas older son" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -589,7 +616,8 @@ def test_answer_in_chat_history_beyond_lookback_window(client_offline_chat, defa create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f'/api/chat?q="What is my name?"&stream=true') + query = "What is my name?" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -621,8 +649,9 @@ def test_answer_in_chat_history_by_conversation_id(client_offline_chat, default_ create_conversation(message_list2, default_user2) # Act - response = client_offline_chat.get( - f'/api/chat?q="What is my favorite color?"&conversation_id={conversation.id}&stream=true' + query = "What is my favorite color?" + response = client_offline_chat.post( + f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True} ) response_message = response.content.decode("utf-8") @@ -652,8 +681,10 @@ def test_answer_in_chat_history_by_conversation_id_with_agent( conversation = create_conversation(message_list, default_user2, offline_agent) # Act - query = urllib.parse.quote("/general What did I eat for breakfast?") - response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") + query = "/general What did I eat for breakfast?" + response = client_offline_chat.post( + f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True} + ) response_message = response.content.decode("utf-8") # Assert that agent only responds with the summary of spending @@ -669,11 +700,11 @@ def test_answer_in_chat_history_by_conversation_id_with_agent( def test_answer_chat_history_very_long(client_offline_chat, default_user2): # Arrange message_list = [(" ".join([fake.paragraph() for _ in range(50)]), fake.sentence(), []) for _ in range(10)] - create_conversation(message_list, default_user2) # Act - response = client_offline_chat.post(f'/api/chat?q="What is my name?"&stream=true') + query = "What is my name?" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -687,7 +718,8 @@ def test_answer_chat_history_very_long(client_offline_chat, default_user2): def test_answer_requires_multiple_independent_searches(client_offline_chat): "Chat director should be able to answer by doing multiple independent searches for required information" # Act - response = client_offline_chat.post(f'/api/chat?q="Is Xi older than Namita?"&stream=true') + query = "Is Xi older than Namita?" + response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert diff --git a/tests/test_openai_chat_director.py b/tests/test_openai_chat_director.py index 371ff5a4..279d6e37 100644 --- a/tests/test_openai_chat_director.py +++ b/tests/test_openai_chat_director.py @@ -49,7 +49,7 @@ def create_conversation(message_list, user, agent=None): @pytest.mark.django_db(transaction=True) def test_chat_with_no_chat_history_or_retrieved_content(chat_client): # Act - response = chat_client.post(f'/api/chat?q="Hello, my name is Testatron. Who are you?"') + response = chat_client.post(f"/api/chat", json={"q": "Hello, my name is Testatron. Who are you?"}) response_message = response.json()["response"] # Assert @@ -66,8 +66,7 @@ def test_chat_with_no_chat_history_or_retrieved_content(chat_client): def test_chat_with_online_content(chat_client): # Act q = "/online give me the link to paul graham's essay how to do great work" - encoded_q = quote(q, safe="") - response = chat_client.post(f"/api/chat?q={encoded_q}") + response = chat_client.post(f"/api/chat?", json={"q": q}) response_message = response.json()["response"] # Assert @@ -87,8 +86,7 @@ def test_chat_with_online_content(chat_client): def test_chat_with_online_webpage_content(chat_client): # Act q = "/online how many firefighters were involved in the great chicago fire and which year did it take place?" - encoded_q = quote(q, safe="") - response = chat_client.post(f"/api/chat?q={encoded_q}") + response = chat_client.post(f"/api/chat", json={"q": q}) response_message = response.json()["response"] # Assert @@ -111,7 +109,7 @@ def test_answer_from_chat_history(chat_client, default_user2: KhojUser): create_conversation(message_list, default_user2) # Act - response = chat_client.post(f'/api/chat?q="What is my name?"') + response = chat_client.post(f"/api/chat", json={"q": "What is my name?"}) response_message = response.content.decode("utf-8") # Assert @@ -138,7 +136,7 @@ def test_answer_from_currently_retrieved_content(chat_client, default_user2: Kho create_conversation(message_list, default_user2) # Act - response = chat_client.post(f'/api/chat?q="Where was Xi Li born?"') + response = chat_client.post(f"/api/chat", json={"q": "Where was Xi Li born?"}) response_message = response.json()["response"] # Assert @@ -162,7 +160,7 @@ def test_answer_from_chat_history_and_previously_retrieved_content(chat_client_n create_conversation(message_list, default_user2) # Act - response = chat_client_no_background.post(f'/api/chat?q="Where was I born?"') + response = chat_client_no_background.post(f"/api/chat", json={"q": "Where was I born?"}) response_message = response.json()["response"] # Assert @@ -185,7 +183,7 @@ def test_answer_from_chat_history_and_currently_retrieved_content(chat_client, d create_conversation(message_list, default_user2) # Act - response = chat_client.post(f'/api/chat?q="Where was I born?"') + response = chat_client.post(f"/api/chat", json={"q": "Where was I born?"}) response_message = response.json()["response"] # Assert @@ -210,7 +208,7 @@ def test_no_answer_in_chat_history_or_retrieved_content(chat_client, default_use create_conversation(message_list, default_user2) # Act - response = chat_client.post(f'/api/chat?q="Where was I born?"') + response = chat_client.post(f"/api/chat", json={"q": "Where was I born?"}) response_message = response.json()["response"] # Assert @@ -235,12 +233,12 @@ def test_no_answer_in_chat_history_or_retrieved_content(chat_client, default_use @pytest.mark.chatquality def test_answer_using_general_command(chat_client, default_user2: KhojUser): # Arrange - query = urllib.parse.quote("/general Where was Xi Li born?") + query = "/general Where was Xi Li born?" message_list = [] create_conversation(message_list, default_user2) # Act - response = chat_client.post(f"/api/chat?q={query}&stream=true") + response = chat_client.post(f"/api/chat", json={"q": query, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -253,12 +251,12 @@ def test_answer_using_general_command(chat_client, default_user2: KhojUser): @pytest.mark.chatquality def test_answer_from_retrieved_content_using_notes_command(chat_client, default_user2: KhojUser): # Arrange - query = urllib.parse.quote("/notes Where was Xi Li born?") + query = "/notes Where was Xi Li born?" message_list = [] create_conversation(message_list, default_user2) # Act - response = chat_client.post(f"/api/chat?q={query}") + response = chat_client.post(f"/api/chat", json={"q": query}) response_message = response.json()["response"] # Assert @@ -271,12 +269,12 @@ def test_answer_from_retrieved_content_using_notes_command(chat_client, default_ @pytest.mark.chatquality def test_answer_not_known_using_notes_command(chat_client_no_background, default_user2: KhojUser): # Arrange - query = urllib.parse.quote("/notes Where was Testatron born?") + query = "/notes Where was Testatron born?" message_list = [] create_conversation(message_list, default_user2) # Act - response = chat_client_no_background.post(f"/api/chat?q={query}") + response = chat_client_no_background.post(f"/api/chat", json={"q": query}) response_message = response.json()["response"] # Assert @@ -308,8 +306,8 @@ def test_summarize_one_file(chat_client, default_user2: KhojUser): "api/chat/conversation/file-filters", json={"filename": summarization_file, "conversation_id": str(conversation.id)}, ) - query = urllib.parse.quote("/summarize") - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") + query = "/summarize" + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) response_message = response.json()["response"] # Assert assert response_message != "" @@ -340,8 +338,8 @@ def test_summarize_extra_text(chat_client, default_user2: KhojUser): "api/chat/conversation/file-filters", json={"filename": summarization_file, "conversation_id": str(conversation.id)}, ) - query = urllib.parse.quote("/summarize tell me about Xiu") - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") + query = "/summarize tell me about Xiu" + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) response_message = response.json()["response"] # Assert assert response_message != "" @@ -368,8 +366,8 @@ def test_summarize_multiple_files(chat_client, default_user2: KhojUser): "api/chat/conversation/file-filters", json={"filename": file_list[1], "conversation_id": str(conversation.id)} ) - query = urllib.parse.quote("/summarize") - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") + query = "/summarize" + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) response_message = response.json()["response"] # Assert @@ -379,11 +377,15 @@ def test_summarize_multiple_files(chat_client, default_user2: KhojUser): @pytest.mark.django_db(transaction=True) @pytest.mark.chatquality def test_summarize_no_files(chat_client, default_user2: KhojUser): + # Arrange message_list = [] conversation = create_conversation(message_list, default_user2) - query = urllib.parse.quote("/summarize") - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") + + # Act + query = "/summarize" + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) response_message = response.json()["response"] + # Assert assert response_message == "No files selected for summarization. Please add files using the section on the left." @@ -391,6 +393,7 @@ def test_summarize_no_files(chat_client, default_user2: KhojUser): @pytest.mark.django_db(transaction=True) @pytest.mark.chatquality def test_summarize_different_conversation(chat_client, default_user2: KhojUser): + # Arrange message_list = [] conversation1 = create_conversation(message_list, default_user2) conversation2 = create_conversation(message_list, default_user2) @@ -413,21 +416,26 @@ def test_summarize_different_conversation(chat_client, default_user2: KhojUser): json={"filename": summarization_file, "conversation_id": str(conversation1.id)}, ) - query = urllib.parse.quote("/summarize") - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation2.id}") - response_message = response.json()["response"] - - # Assert - assert response_message == "No files selected for summarization. Please add files using the section on the left." + # Act + query = "/summarize" + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation2.id}) + response_message_conv2 = response.json()["response"] # now make sure that the file filter is still in conversation 1 - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation1.id}") - response_message = response.json()["response"] + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation1.id}) + response_message_conv1 = response.json()["response"] # Assert - assert response_message != "" - assert response_message != "No files selected for summarization. Please add files using the section on the left." - assert response_message != "Only one file can be selected for summarization." + assert ( + response_message_conv2 == "No files selected for summarization. Please add files using the section on the left." + ) + + # now check the file filter is still in conversation 1 + assert response_message_conv1 != "" + assert ( + response_message_conv1 != "No files selected for summarization. Please add files using the section on the left." + ) + assert response_message_conv1 != "Only one file can be selected for summarization." @pytest.mark.django_db(transaction=True) @@ -441,7 +449,7 @@ def test_summarize_nonexistant_file(chat_client, default_user2: KhojUser): json={"filename": "imaginary.markdown", "conversation_id": str(conversation.id)}, ) query = urllib.parse.quote("/summarize") - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) response_message = response.json()["response"] # Assert assert response_message == "No files selected for summarization. Please add files using the section on the left." @@ -450,6 +458,7 @@ def test_summarize_nonexistant_file(chat_client, default_user2: KhojUser): @pytest.mark.django_db(transaction=True) @pytest.mark.chatquality def test_summarize_diff_user_file(chat_client, default_user: KhojUser, pdf_configured_user1, default_user2: KhojUser): + # Arrange message_list = [] conversation = create_conversation(message_list, default_user2) # Get the pdf file called singlepage.pdf @@ -469,9 +478,12 @@ def test_summarize_diff_user_file(chat_client, default_user: KhojUser, pdf_confi "api/chat/conversation/file-filters", json={"filename": summarization_file, "conversation_id": str(conversation.id)}, ) - query = urllib.parse.quote("/summarize") - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") + + # Act + query = "/summarize" + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) response_message = response.json()["response"] + # Assert assert response_message == "No files selected for summarization. Please add files using the section on the left." @@ -484,7 +496,7 @@ def test_summarize_diff_user_file(chat_client, default_user: KhojUser, pdf_confi def test_answer_requires_current_date_awareness(chat_client): "Chat actor should be able to answer questions relative to current date using provided notes" # Act - response = chat_client.post(f'/api/chat?q="Where did I have lunch today?"&stream=true') + response = chat_client.post(f"/api/chat", json={"q": "Where did I have lunch today?", "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -502,7 +514,8 @@ def test_answer_requires_current_date_awareness(chat_client): def test_answer_requires_date_aware_aggregation_across_provided_notes(chat_client): "Chat director should be able to answer questions that require date aware aggregation across multiple notes" # Act - response = chat_client.post(f'/api/chat?q="How much did I spend on dining this year?"') + query = "How much did I spend on dining this year?" + response = chat_client.post(f"/api/chat", json={"q": query}) response_message = response.json()["response"] # Assert @@ -523,7 +536,8 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(chat_c create_conversation(message_list, default_user2) # Act - response = chat_client.post(f'/api/chat?q="Write a haiku about unit testing. Do not say anything else.') + query = "Write a haiku about unit testing. Do not say anything else." + response = chat_client.post(f"/api/chat", json={"q": query}) response_message = response.json()["response"] # Assert @@ -540,7 +554,8 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(chat_c @pytest.mark.chatquality def test_ask_for_clarification_if_not_enough_context_in_question(chat_client_no_background): # Act - response = chat_client_no_background.post(f'/api/chat?q="What is the name of Namitas older son?"') + query = "What is the name of Namitas older son?" + response = chat_client_no_background.post(f"/api/chat", json={"q": query}) response_message = response.json()["response"].lower() # Assert @@ -574,7 +589,8 @@ def test_answer_in_chat_history_beyond_lookback_window(chat_client, default_user create_conversation(message_list, default_user2) # Act - response = chat_client.post(f'/api/chat?q="What is my name?"') + query = "What is my name?" + response = chat_client.post(f"/api/chat", json={"q": query}) response_message = response.json()["response"] # Assert @@ -606,8 +622,8 @@ def test_answer_in_chat_history_by_conversation_id(chat_client, default_user2: K create_conversation(message_list2, default_user2) # Act - query = urllib.parse.quote("/general What is my favorite color?") - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") + query = "/general What is my favorite color?" + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True}) response_message = response.content.decode("utf-8") # Assert @@ -639,8 +655,8 @@ def test_answer_in_chat_history_by_conversation_id_with_agent( conversation = create_conversation(message_list, default_user2, openai_agent) # Act - query = urllib.parse.quote("/general What did I buy for breakfast?") - response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") + query = "/general What did I buy for breakfast?" + response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id}) response_message = response.json()["response"] # Assert that agent only responds with the summary of spending @@ -657,7 +673,8 @@ def test_answer_in_chat_history_by_conversation_id_with_agent( def test_answer_requires_multiple_independent_searches(chat_client): "Chat director should be able to answer by doing multiple independent searches for required information" # Act - response = chat_client.post(f'/api/chat?q="Is Xi Li older than Namita? Just say the older persons full name"') + query = "Is Xi Li older than Namita? Just say the older persons full name" + response = chat_client.post(f"/api/chat", json={"q": query}) response_message = response.json()["response"].lower() # Assert @@ -678,11 +695,10 @@ def test_answer_requires_multiple_independent_searches(chat_client): def test_answer_using_file_filter(chat_client): "Chat should be able to use search filters in the query" # Act - query = urllib.parse.quote( + query = ( 'Is Xi Li older than Namita? Just say the older persons full name. file:"Namita.markdown" file:"Xi Li.markdown"' ) - - response = chat_client.post(f"/api/chat?q={query}") + response = chat_client.post(f"/api/chat", json={"q": query}) response_message = response.json()["response"].lower() # Assert