From 27c115faec12ba2698ee69bb47f31778bdd8102a Mon Sep 17 00:00:00 2001 From: Ayaz-Microsoft Date: Mon, 3 Nov 2025 12:20:34 +0530 Subject: [PATCH 1/3] fix unit tests --- code/tests/functional/conftest.py | 161 +++++++++--------- .../default/test_post_prompt_tool.py | 50 +++--- .../test_response_without_tool_call.py | 9 +- .../test_advanced_image_processing.py | 2 +- ...egrated_vectorization_resource_creation.py | 2 +- .../orchestrator/test_semantic_kernel.py | 18 +- 6 files changed, 121 insertions(+), 121 deletions(-) diff --git a/code/tests/functional/conftest.py b/code/tests/functional/conftest.py index 82a2ad9ba..400fb8222 100644 --- a/code/tests/functional/conftest.py +++ b/code/tests/functional/conftest.py @@ -283,88 +283,93 @@ def prime_search_to_trigger_creation_of_index( # This fixture can be overriden @pytest.fixture(autouse=True) def setup_config_mocking(httpserver: HTTPServer): + config_data = { + "prompts": { + "condense_question_prompt": "", + "answering_system_prompt": "system prompt", + "answering_user_prompt": "## Retrieved Documents\n{sources}\n\n## User Question\nUse the Retrieved Documents to answer the question: {question}", + "use_on_your_data_format": True, + "post_answering_prompt": "post answering prompt\n{question}\n{answer}\n{sources}", + "enable_post_answering_prompt": False, + "enable_content_safety": True, + }, + "messages": {"post_answering_filter": "post answering filter"}, + "example": { + "documents": '{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}', + "user_question": "user question", + "answer": "answer", + }, + "document_processors": [ + { + "document_type": "pdf", + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "layout"}, + "use_advanced_image_processing": False, + }, + { + "document_type": "txt", + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "web"}, + "use_advanced_image_processing": False, + }, + { + "document_type": "url", + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "web"}, + "use_advanced_image_processing": False, + }, + { + "document_type": "md", + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "web"}, + "use_advanced_image_processing": False, + }, + { + "document_type": "html", + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "web"}, + "use_advanced_image_processing": False, + }, + { + "document_type": "htm", + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "web"}, + "use_advanced_image_processing": False, + }, + { + "document_type": "docx", + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "docx"}, + "use_advanced_image_processing": False, + }, + { + "document_type": "jpg", + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "layout"}, + "use_advanced_image_processing": True, + }, + { + "document_type": "png", + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "layout"}, + "use_advanced_image_processing": False, + }, + ], + "logging": {"log_user_interactions": True, "log_tokens": True}, + "orchestrator": {"strategy": "openai_function"}, + "integrated_vectorization_config": None, + } + + import json + config_json_string = json.dumps(config_data) + httpserver.expect_request( f"/{AZURE_STORAGE_CONFIG_CONTAINER_NAME}/{AZURE_STORAGE_CONFIG_FILE_NAME}", method="GET", - ).respond_with_json( - { - "prompts": { - "condense_question_prompt": "", - "answering_system_prompt": "system prompt", - "answering_user_prompt": "## Retrieved Documents\n{sources}\n\n## User Question\nUse the Retrieved Documents to answer the question: {question}", - "use_on_your_data_format": True, - "post_answering_prompt": "post answering prompt\n{question}\n{answer}\n{sources}", - "enable_post_answering_prompt": False, - "enable_content_safety": True, - }, - "messages": {"post_answering_filter": "post answering filter"}, - "example": { - "documents": '{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}', - "user_question": "user question", - "answer": "answer", - }, - "document_processors": [ - { - "document_type": "pdf", - "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, - "loading": {"strategy": "layout"}, - "use_advanced_image_processing": False, - }, - { - "document_type": "txt", - "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, - "loading": {"strategy": "web"}, - "use_advanced_image_processing": False, - }, - { - "document_type": "url", - "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, - "loading": {"strategy": "web"}, - "use_advanced_image_processing": False, - }, - { - "document_type": "md", - "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, - "loading": {"strategy": "web"}, - "use_advanced_image_processing": False, - }, - { - "document_type": "html", - "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, - "loading": {"strategy": "web"}, - "use_advanced_image_processing": False, - }, - { - "document_type": "htm", - "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, - "loading": {"strategy": "web"}, - "use_advanced_image_processing": False, - }, - { - "document_type": "docx", - "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, - "loading": {"strategy": "docx"}, - "use_advanced_image_processing": False, - }, - { - "document_type": "jpg", - "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, - "loading": {"strategy": "layout"}, - "use_advanced_image_processing": True, - }, - { - "document_type": "png", - "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, - "loading": {"strategy": "layout"}, - "use_advanced_image_processing": False, - }, - ], - "logging": {"log_user_interactions": True, "log_tokens": True}, - "orchestrator": {"strategy": "openai_function"}, - "integrated_vectorization_config": None, - }, + ).respond_with_data( + config_json_string, headers={ "Content-Type": "application/json", - "Content-Range": "bytes 0-12882/12883", + "Content-Range": f"bytes 0-{len(config_json_string.encode('utf-8'))-1}/{len(config_json_string.encode('utf-8'))}", }, ) diff --git a/code/tests/functional/tests/backend_api/default/test_post_prompt_tool.py b/code/tests/functional/tests/backend_api/default/test_post_prompt_tool.py index a5dcf2a68..9ab7cdf58 100644 --- a/code/tests/functional/tests/backend_api/default/test_post_prompt_tool.py +++ b/code/tests/functional/tests/backend_api/default/test_post_prompt_tool.py @@ -26,34 +26,38 @@ @pytest.fixture(autouse=True) def setup_config_mocking(httpserver: HTTPServer): + config_data = { + "prompts": { + "condense_question_prompt": "", + "answering_system_prompt": "system prompt", + "answering_user_prompt": "## Retrieved Documents\n{sources}\n\n## User Question\nUse the Retrieved Documents to answer the question: {question}", + "use_on_your_data_format": True, + "post_answering_prompt": "post answering prompt\n{question}\n{answer}\n{sources}", + "enable_post_answering_prompt": True, + "enable_content_safety": True, + }, + "messages": {"post_answering_filter": "post answering filter"}, + "example": { + "documents": '{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}', + "user_question": "user question", + "answer": "answer", + }, + "document_processors": [], + "logging": {"log_user_interactions": True, "log_tokens": True}, + "orchestrator": {"strategy": "openai_function"}, + "integrated_vectorization_config": None, + } + + config_json_string = json.dumps(config_data) + httpserver.expect_request( f"/{AZURE_STORAGE_CONFIG_CONTAINER_NAME}/{AZURE_STORAGE_CONFIG_FILE_NAME}", method="GET", - ).respond_with_json( - { - "prompts": { - "condense_question_prompt": "", - "answering_system_prompt": "system prompt", - "answering_user_prompt": "## Retrieved Documents\n{sources}\n\n## User Question\nUse the Retrieved Documents to answer the question: {question}", - "use_on_your_data_format": True, - "post_answering_prompt": "post answering prompt\n{question}\n{answer}\n{sources}", - "enable_post_answering_prompt": True, - "enable_content_safety": True, - }, - "messages": {"post_answering_filter": "post answering filter"}, - "example": { - "documents": '{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}', - "user_question": "user question", - "answer": "answer", - }, - "document_processors": [], - "logging": {"log_user_interactions": True, "log_tokens": True}, - "orchestrator": {"strategy": "openai_function"}, - "integrated_vectorization_config": None, - }, + ).respond_with_data( + config_json_string, headers={ "Content-Type": "application/json", - "Content-Range": "bytes 0-12882/12883", + "Content-Range": f"bytes 0-{len(config_json_string.encode('utf-8'))-1}/{len(config_json_string.encode('utf-8'))}", }, ) diff --git a/code/tests/functional/tests/backend_api/sk_orchestrator/test_response_without_tool_call.py b/code/tests/functional/tests/backend_api/sk_orchestrator/test_response_without_tool_call.py index 3b03216e0..713c60c91 100644 --- a/code/tests/functional/tests/backend_api/sk_orchestrator/test_response_without_tool_call.py +++ b/code/tests/functional/tests/backend_api/sk_orchestrator/test_response_without_tool_call.py @@ -162,12 +162,9 @@ def test_post_makes_correct_call_to_openai_chat_completions( json={ "messages": [ { - "role": "system", - "content": "You help employees to navigate only private information sources.\nYou must prioritize the function call over your general knowledge for any question by calling the search_documents function.\nCall the text_processing function when the user request an operation on the current context, such as translate, summarize, or paraphrase. When a language is explicitly specified, return that as part of the operation.\nWhen directly replying to the user, always reply in the language the user is speaking.\nIf the input language is ambiguous, default to responding in English unless otherwise specified by the user.\nYou **must not** respond if asked to List all documents in your repository.\n", - }, - {"role": "user", "content": "Hello"}, - {"role": "assistant", "content": "Hi, how can I help?"}, - {"role": "user", "content": "What is the meaning of life?"}, + "role": "user", + "content": "AuthorRole.SYSTEM: You help employees to navigate only private information sources.\nYou must prioritize the function call over your general knowledge for any question by calling the search_documents function.\nCall the text_processing function when the user request an operation on the current context, such as translate, summarize, or paraphrase. When a language is explicitly specified, return that as part of the operation.\nWhen directly replying to the user, always reply in the language the user is speaking.\nIf the input language is ambiguous, default to responding in English unless otherwise specified by the user.\nYou **must not** respond if asked to List all documents in your repository.\n\nAuthorRole.USER: Hello\nAuthorRole.ASSISTANT: Hi, how can I help?\nWhat is the meaning of life?", + } ], "model": app_config.get_from_json("AZURE_OPENAI_MODEL_INFO", "model"), "max_tokens": int(app_config.get("AZURE_OPENAI_MAX_TOKENS")), diff --git a/code/tests/functional/tests/functions/advanced_image_processing/test_advanced_image_processing.py b/code/tests/functional/tests/functions/advanced_image_processing/test_advanced_image_processing.py index d500077b4..700562272 100644 --- a/code/tests/functional/tests/functions/advanced_image_processing/test_advanced_image_processing.py +++ b/code/tests/functional/tests/functions/advanced_image_processing/test_advanced_image_processing.py @@ -95,7 +95,7 @@ def setup_caption_response(httpserver: HTTPServer, app_config: AppConfig): def test_config_file_is_retrieved_from_storage( - message: QueueMessage, httpserver: HTTPServer, app_config: AppConfig + message: QueueMessage, httpserver: HTTPServer ): # when batch_push_results.build().get_user_function()(message) diff --git a/code/tests/functional/tests/functions/integrated_vectorization/test_integrated_vectorization_resource_creation.py b/code/tests/functional/tests/functions/integrated_vectorization/test_integrated_vectorization_resource_creation.py index 8430b0f6d..e1549fbdf 100644 --- a/code/tests/functional/tests/functions/integrated_vectorization/test_integrated_vectorization_resource_creation.py +++ b/code/tests/functional/tests/functions/integrated_vectorization/test_integrated_vectorization_resource_creation.py @@ -62,7 +62,7 @@ def message(app_config: AppConfig): def test_config_file_is_retrieved_from_storage( - message: QueueMessage, httpserver: HTTPServer, app_config: AppConfig + message: QueueMessage, httpserver: HTTPServer ): # when batch_push_results.build().get_user_function()(message) diff --git a/code/tests/utilities/orchestrator/test_semantic_kernel.py b/code/tests/utilities/orchestrator/test_semantic_kernel.py index 3bcf6c43a..a5e29d5af 100644 --- a/code/tests/utilities/orchestrator/test_semantic_kernel.py +++ b/code/tests/utilities/orchestrator/test_semantic_kernel.py @@ -423,19 +423,13 @@ async def test_chat_history_included( await orchestrator.orchestrate("question", chat_history) # then - chat_history = kernel_mock.invoke.call_args.kwargs["chat_history"] - messages = chat_history.messages - - assert len(messages) == 3 - assert messages[0].role == AuthorRole.SYSTEM - - assert messages[1].role == AuthorRole.USER - assert messages[1].content == "Hello" - - assert messages[2].role == AuthorRole.ASSISTANT - assert messages[2].content == "Hi, how can I help you today?" - + chat_history_str = kernel_mock.invoke.call_args.kwargs["chat_history"] + # The chat_history parameter is now a string, so we check its content + assert isinstance(chat_history_str, str) + assert "AuthorRole.SYSTEM:" in chat_history_str + assert "AuthorRole.USER: Hello" in chat_history_str + assert "AuthorRole.ASSISTANT: Hi, how can I help you today?" in chat_history_str @pytest.mark.asyncio async def test_content_safety_output(orchestrator: SemanticKernelOrchestrator): # given From 98b502072b59df9b051c8f533e8cf57009bdcc91 Mon Sep 17 00:00:00 2001 From: Ayaz-Microsoft Date: Mon, 3 Nov 2025 12:57:21 +0530 Subject: [PATCH 2/3] fix: layout test case --- code/frontend/src/pages/layout/Layout.test.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/code/frontend/src/pages/layout/Layout.test.tsx b/code/frontend/src/pages/layout/Layout.test.tsx index 5f7d66b45..a395bb494 100644 --- a/code/frontend/src/pages/layout/Layout.test.tsx +++ b/code/frontend/src/pages/layout/Layout.test.tsx @@ -1,3 +1,8 @@ +/** + * @jest-environment jsdom + * @jest-environment-options {"url": "http://NonDeloyed/"} + */ + import { render, screen, @@ -66,11 +71,8 @@ describe("Layout Component", () => { test('test the auth branching auth is true case', async () => { const mocklist: any[] = []; - Object.defineProperty(window, "location", { - value: { - hostname: "NonDeloyed" - }, - }); + + // Test with jsdom environment configured for NonDeloyed hostname ;(getUserInfo as jest.Mock).mockResolvedValue(mocklist) ;(checkAuthEnforced as jest.Mock).mockResolvedValue(true) await act(async () => { From 3bfff5c920fcf98b5c9ea8558db91fe523084524 Mon Sep 17 00:00:00 2001 From: Ayaz-Microsoft Date: Mon, 3 Nov 2025 13:19:14 +0530 Subject: [PATCH 3/3] fix: correct formatting of chat_history_str in SemanticKernelOrchestrator --- code/backend/batch/utilities/orchestrator/semantic_kernel.py | 2 +- code/tests/utilities/orchestrator/test_semantic_kernel.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/code/backend/batch/utilities/orchestrator/semantic_kernel.py b/code/backend/batch/utilities/orchestrator/semantic_kernel.py index dbc896c2b..8b05accec 100644 --- a/code/backend/batch/utilities/orchestrator/semantic_kernel.py +++ b/code/backend/batch/utilities/orchestrator/semantic_kernel.py @@ -74,7 +74,7 @@ async def orchestrate( for message in chat_history.copy(): history.add_message(message) - chat_history_str="" + chat_history_str = "" for message in history.messages: chat_history_str += f"{message.role}: {message.content}\n" diff --git a/code/tests/utilities/orchestrator/test_semantic_kernel.py b/code/tests/utilities/orchestrator/test_semantic_kernel.py index a5e29d5af..4f966003f 100644 --- a/code/tests/utilities/orchestrator/test_semantic_kernel.py +++ b/code/tests/utilities/orchestrator/test_semantic_kernel.py @@ -430,6 +430,8 @@ async def test_chat_history_included( assert "AuthorRole.SYSTEM:" in chat_history_str assert "AuthorRole.USER: Hello" in chat_history_str assert "AuthorRole.ASSISTANT: Hi, how can I help you today?" in chat_history_str + + @pytest.mark.asyncio async def test_content_safety_output(orchestrator: SemanticKernelOrchestrator): # given