diff --git a/core/state/state_manager.py b/core/state/state_manager.py index a60a551041..5d1ab84b3f 100644 --- a/core/state/state_manager.py +++ b/core/state/state_manager.py @@ -1,7 +1,6 @@ import asyncio import os.path import re -import sys import traceback from contextlib import asynccontextmanager from typing import TYPE_CHECKING, Optional @@ -143,7 +142,7 @@ class StateManager: self, name: Optional[str] = "temp-project", project_type: Optional[str] = "node", - folder_name: Optional[str] = "temp-project" if "pytest" not in sys.modules else None, + folder_name: Optional[str] = "temp-project", ) -> Project: """ Create a new project and set it as the current one. @@ -162,10 +161,6 @@ class StateManager: # even for a new project, eg. offline changes check and stats updating await state.awaitable_attrs.files - is_test = "pytest" in sys.modules - if is_test: - await session.commit() - log.info( f'Created new project "{name}" (id={project.id}) ' f'with default branch "{branch.name}" (id={branch.id}) ' @@ -179,9 +174,6 @@ class StateManager: self.project = project self.branch = branch - if is_test: - self.file_system = await self.init_file_system(load_existing=False) - return project async def delete_project(self, project_id: UUID) -> bool: diff --git a/tests/agents/test_orchestrator.py b/tests/agents/test_orchestrator.py index 58280bb948..af4855e493 100644 --- a/tests/agents/test_orchestrator.py +++ b/tests/agents/test_orchestrator.py @@ -44,6 +44,7 @@ async def test_offline_changes_check_restores_changes_from_db(): async def test_import_if_new_files(agentcontext): sm, _, ui, _ = agentcontext + sm.file_system = await sm.init_file_system(load_existing=False) state = await sm.commit() orca = Orchestrator(state_manager=sm, ui=ui) @@ -62,7 +63,7 @@ async def test_import_if_new_files(agentcontext): @pytest.mark.asyncio async def test_import_if_modified_files(agentcontext): sm, _, ui, _ = agentcontext - + sm.file_system = await sm.init_file_system(load_existing=False) await sm.commit() await sm.save_file("test.txt", "Hello, world!") state = await sm.commit() @@ -83,7 +84,7 @@ async def test_import_if_modified_files(agentcontext): @pytest.mark.asyncio async def test_import_if_deleted_files(agentcontext): sm, _, ui, _ = agentcontext - + sm.file_system = await sm.init_file_system(load_existing=False) await sm.commit() await sm.save_file("test.txt", "Hello, world!") state = await sm.commit() diff --git a/tests/state/test_state_manager.py b/tests/state/test_state_manager.py index 09587ae5db..f2123772b7 100644 --- a/tests/state/test_state_manager.py +++ b/tests/state/test_state_manager.py @@ -41,10 +41,10 @@ async def test_load_project(mock_get_config, testmanager): mock_get_config.return_value.fs.type = "memory" sm = StateManager(testmanager) project = await sm.create_project("test") - + project_id = project.id + await sm.commit() project_state = await sm.load_project(project_id=project.id) - - assert project_state.branch.project.id == project.id + assert project_state.branch.project.id == project_id @pytest.mark.asyncio @@ -69,15 +69,15 @@ async def test_load_project_branch(mock_get_config, testmanager): mock_get_config.return_value.fs.type = "memory" sm = StateManager(testmanager) project = await sm.create_project("test") - + project_id = project.id + await sm.commit() project_state = await sm.load_project(branch_id=project.branches[0].id) - assert project_state.branch.project.id == project.id + assert project_state.branch.project.id == project_id @pytest.mark.asyncio @patch("core.state.state_manager.get_config") -@pytest.mark.skip(reason="Temporary") async def test_load_nonexistent_step(mock_get_config, testmanager): mock_get_config.return_value.fs.type = "memory" sm = StateManager(testmanager) @@ -93,10 +93,11 @@ async def test_load_specific_step(mock_get_config, testmanager): mock_get_config.return_value.fs.type = "memory" sm = StateManager(testmanager) project = await sm.create_project("test") + project_id = project.id + await sm.commit() + project_state = await sm.load_project(project_id=project_id, step_index=project.branches[0].states[0].step_index) - project_state = await sm.load_project(project_id=project.id, step_index=project.branches[0].states[0].step_index) - - assert project_state.branch.project.id == project.id + assert project_state.branch.project.id == project_id @pytest.mark.asyncio @@ -142,7 +143,7 @@ async def test_save_file(mock_get_config, testmanager): # Create an instance of StateManager with mocked UI sm = StateManager(testmanager, ui) await sm.create_project("test") - + sm.file_system = await sm.init_file_system(load_existing=False) # The initial state in the project is weird because it's both current # and next, and this can play havoc with the SQLAlchemy session and # object caching. Commit it here to get that out of our way. @@ -167,59 +168,58 @@ async def test_save_file(mock_get_config, testmanager): @pytest.mark.asyncio @patch("core.state.state_manager.get_config") async def test_importing_changed_files_to_db(mock_get_config, tmpdir, testmanager): - mock_get_config.return_value.fs = FileSystemConfig(workspace_root=str(tmpdir)) + mock_get_config.return_value.fs = FileSystemConfig(workspace_root=str(tmpdir), type="local") + sm = StateManager(testmanager) - project = await sm.create_project("test") + await sm.create_project("test", "node", "test") + sm.file_system = await sm.init_file_system(load_existing=False) - async with testmanager as session: - session.add(project) - await sm.commit() - await sm.save_file("file1.txt", "this is the content 1") - await sm.save_file("file2.txt", "this is the content 2") - await sm.save_file("file3.txt", "this is the content 3") - await sm.commit() + await sm.commit() + await sm.save_file("file1.txt", "this is the content 1") + await sm.save_file("file2.txt", "this is the content 2") + await sm.save_file("file3.txt", "this is the content 3") + await sm.commit() - os.remove(os.path.join(tmpdir, "test", "file1.txt")) # Remove the first file - with open(os.path.join(tmpdir, "test", "file2.txt"), "a") as f: - f.write("modified") # Change the second file + os.remove(os.path.join(tmpdir, "test", "file1.txt")) # Remove the first file + with open(os.path.join(tmpdir, "test", "file2.txt"), "a") as f: + f.write("modified") # Change the second file - await sm.import_files() - await sm.commit() + await sm.import_files() + await sm.commit() - assert not os.path.exists(os.path.join(tmpdir, "test", "file1.txt")) - assert os.path.exists(os.path.join(tmpdir, "test", "file2.txt")) - assert os.path.exists(os.path.join(tmpdir, "test", "file3.txt")) + assert not os.path.exists(os.path.join(tmpdir, "test", "file1.txt")) + assert os.path.exists(os.path.join(tmpdir, "test", "file2.txt")) + assert os.path.exists(os.path.join(tmpdir, "test", "file3.txt")) - db_files = set(f.path for f in sm.current_state.files) - assert "file1.txt" not in db_files - assert "file2.txt" in db_files - assert "file3.txt" in db_files + db_files = set(f.path for f in sm.current_state.files) + assert "file1.txt" not in db_files + assert "file2.txt" in db_files + assert "file3.txt" in db_files @pytest.mark.asyncio @patch("core.state.state_manager.get_config") async def test_restoring_files_from_db(mock_get_config, tmpdir, testmanager): - mock_get_config.return_value.fs = FileSystemConfig(workspace_root=str(tmpdir)) + mock_get_config.return_value.fs = FileSystemConfig(workspace_root=str(tmpdir), type="local") sm = StateManager(testmanager) - project = await sm.create_project("test1") + await sm.create_project("test1", "node", "test1") + sm.file_system = await sm.init_file_system(load_existing=False) - async with testmanager as session: - session.add(project) - await sm.commit() - await sm.save_file("file1.txt", "this is the content 1") - await sm.save_file("file2.txt", "this is the content 2") - await sm.save_file("file3.txt", "this is the content 3") - await sm.commit() - - os.remove(os.path.join(tmpdir, "test1", "file1.txt")) # Remove the first file - with open(os.path.join(tmpdir, "test1", "file2.txt"), "a") as f: - f.write("modified") # Change the second file - await sm.restore_files() - - assert os.path.exists(os.path.join(tmpdir, "test1", "file1.txt")) - assert os.path.exists(os.path.join(tmpdir, "test1", "file2.txt")) - assert os.path.exists(os.path.join(tmpdir, "test1", "file3.txt")) - - assert open(os.path.join(tmpdir, "test1", "file1.txt")).read() == "this is the content 1" - assert open(os.path.join(tmpdir, "test1", "file2.txt")).read() == "this is the content 2" - assert open(os.path.join(tmpdir, "test1", "file3.txt")).read() == "this is the content 3" + await sm.commit() + await sm.save_file("file1.txt", "this is the content 1") + await sm.save_file("file2.txt", "this is the content 2") + await sm.save_file("file3.txt", "this is the content 3") + await sm.commit() + + os.remove(os.path.join(tmpdir, "test1", "file1.txt")) # Remove the first file + with open(os.path.join(tmpdir, "test1", "file2.txt"), "a") as f: + f.write("modified") # Change the second file + await sm.restore_files() + + assert os.path.exists(os.path.join(tmpdir, "test1", "file1.txt")) + assert os.path.exists(os.path.join(tmpdir, "test1", "file2.txt")) + assert os.path.exists(os.path.join(tmpdir, "test1", "file3.txt")) + + assert open(os.path.join(tmpdir, "test1", "file1.txt")).read() == "this is the content 1" + assert open(os.path.join(tmpdir, "test1", "file2.txt")).read() == "this is the content 2" + assert open(os.path.join(tmpdir, "test1", "file3.txt")).read() == "this is the content 3"