1313# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
1414
1515import os
16- from unittest .mock import MagicMock , patch
16+ from unittest .mock import AsyncMock , MagicMock , patch
1717
1818import pytest
1919from fastapi import Response
@@ -50,13 +50,14 @@ async def test_post_chat_endpoint_success(
5050
5151 with (
5252 patch (
53- "app.controller.chat_controller.create_task_lock " ,
53+ "app.controller.chat_controller.get_or_create_task_lock " ,
5454 return_value = mock_task_lock ,
5555 ),
5656 patch (
5757 "app.controller.chat_controller.step_solve"
5858 ) as mock_step_solve ,
5959 patch ("app.controller.chat_controller.load_dotenv" ),
60+ patch ("app.controller.chat_controller.set_current_task_id" ),
6061 patch ("pathlib.Path.mkdir" ),
6162 patch ("pathlib.Path.home" , return_value = MagicMock ()),
6263 ):
@@ -71,9 +72,6 @@ async def mock_generator():
7172
7273 assert isinstance (response , StreamingResponse )
7374 assert response .media_type == "text/event-stream"
74- mock_step_solve .assert_called_once_with (
75- chat_data , mock_request , mock_task_lock
76- )
7775
7876 @pytest .mark .asyncio
7977 async def test_post_chat_sets_environment_variables (
@@ -84,13 +82,14 @@ async def test_post_chat_sets_environment_variables(
8482
8583 with (
8684 patch (
87- "app.controller.chat_controller.create_task_lock " ,
85+ "app.controller.chat_controller.get_or_create_task_lock " ,
8886 return_value = mock_task_lock ,
8987 ),
9088 patch (
9189 "app.controller.chat_controller.step_solve"
9290 ) as mock_step_solve ,
9391 patch ("app.controller.chat_controller.load_dotenv" ),
92+ patch ("app.controller.chat_controller.set_current_task_id" ),
9493 patch ("pathlib.Path.mkdir" ),
9594 patch ("pathlib.Path.home" , return_value = MagicMock ()),
9695 patch .dict (os .environ , {}, clear = True ),
@@ -133,18 +132,24 @@ def test_improve_chat_success(self, mock_task_lock):
133132 # put_queue is invoked when creating the coroutine passed to asyncio.run
134133 mock_task_lock .put_queue .assert_called_once ()
135134
136- def test_improve_chat_task_done_error (self , mock_task_lock ):
137- """Test improvement fails when task is done."""
135+ def test_improve_chat_task_done_resets_to_confirming (self , mock_task_lock ):
136+ """Test improvement when task is done resets status to confirming ."""
138137 task_id = "test_task_123"
139138 supplement_data = SupplementChat (question = "Improve this code" )
140139 mock_task_lock .status = Status .done
141140
142- with patch (
143- "app.controller.chat_controller.get_task_lock" ,
144- return_value = mock_task_lock ,
141+ with (
142+ patch (
143+ "app.controller.chat_controller.get_task_lock" ,
144+ return_value = mock_task_lock ,
145+ ),
146+ patch ("asyncio.run" ) as mock_run ,
145147 ):
146- with pytest .raises (UserException ):
147- improve (task_id , supplement_data )
148+ response = improve (task_id , supplement_data )
149+
150+ assert mock_task_lock .status == Status .confirming
151+ assert isinstance (response , Response )
152+ assert response .status_code == 201
148153
149154 def test_supplement_chat_success (self , mock_task_lock ):
150155 """Test successful chat supplementation."""
@@ -244,16 +249,18 @@ def test_chat_endpoint_integration(
244249 """Test chat endpoint through FastAPI test client."""
245250 with (
246251 patch (
247- "app.controller.chat_controller.create_task_lock "
252+ "app.controller.chat_controller.get_or_create_task_lock "
248253 ) as mock_create_lock ,
249254 patch (
250255 "app.controller.chat_controller.step_solve"
251256 ) as mock_step_solve ,
252257 patch ("app.controller.chat_controller.load_dotenv" ),
258+ patch ("app.controller.chat_controller.set_current_task_id" ),
253259 patch ("pathlib.Path.mkdir" ),
254260 patch ("pathlib.Path.home" , return_value = MagicMock ()),
255261 ):
256262 mock_task_lock = MagicMock ()
263+ mock_task_lock .put_queue = AsyncMock ()
257264 mock_create_lock .return_value = mock_task_lock
258265
259266 async def mock_generator ():
@@ -455,8 +462,12 @@ async def test_post_environment_setup_failure(
455462
456463 with (
457464 patch (
458- "app.controller.chat_controller.create_task_lock "
465+ "app.controller.chat_controller.get_or_create_task_lock "
459466 ) as mock_create_lock ,
467+ patch (
468+ "app.controller.chat_controller.sanitize_env_path" ,
469+ return_value = "/tmp/fake.env" ,
470+ ),
460471 patch (
461472 "app.controller.chat_controller.load_dotenv" ,
462473 side_effect = Exception ("Env load failed" ),
0 commit comments