1515@pytest .fixture
1616def temp_home (monkeypatch ):
1717 """Create a temporary directory for testing."""
18+ # Save the original environment variable if it exists
19+ original_env = os .environ .get ("BASIC_MEMORY_PROJECT" )
20+
21+ # Clear environment variable for clean test
22+ if "BASIC_MEMORY_PROJECT" in os .environ :
23+ del os .environ ["BASIC_MEMORY_PROJECT" ]
24+
1825 with TemporaryDirectory () as tempdir :
1926 temp_home = Path (tempdir )
2027 monkeypatch .setattr (Path , "home" , lambda : temp_home )
@@ -24,6 +31,12 @@ def temp_home(monkeypatch):
2431 config_dir .mkdir (parents = True , exist_ok = True )
2532
2633 yield temp_home
34+
35+ # Cleanup: restore original environment variable if it existed
36+ if original_env is not None :
37+ os .environ ["BASIC_MEMORY_PROJECT" ] = original_env
38+ elif "BASIC_MEMORY_PROJECT" in os .environ :
39+ del os .environ ["BASIC_MEMORY_PROJECT" ]
2740
2841
2942@pytest .fixture
@@ -123,17 +136,33 @@ def test_project_default(cli_runner, temp_home):
123136 # Set as default
124137 result = cli_runner .invoke (app , ["project" , "default" , "test" ])
125138 assert result .exit_code == 0
126- assert "Project 'test' set as default" in result .stdout
139+ assert "Project 'test' set as default and activated " in result .stdout
127140
128141 # Verify default was set
129142 config_manager = ConfigManager ()
130143 assert config_manager .default_project == "test"
144+
145+ # Extra verification: check if the environment variable was set
146+ assert os .environ .get ("BASIC_MEMORY_PROJECT" ) == "test"
131147
132148
133149def test_project_current (cli_runner , temp_home ):
134150 """Test showing the current project."""
151+ # Create a bare-bones config.json with main as the default project
152+ config_file = temp_home / DATA_DIR_NAME / CONFIG_FILE_NAME
153+ config_data = {
154+ "projects" : {
155+ "main" : str (temp_home / "basic-memory" ),
156+ },
157+ "default_project" : "main" ,
158+ }
159+ config_file .write_text (json .dumps (config_data ))
160+
161+ # Create the main project directory
162+ main_dir = temp_home / "basic-memory"
163+ main_dir .mkdir (parents = True , exist_ok = True )
135164
136- # Set as default
165+ # Now check the current project
137166 result = cli_runner .invoke (app , ["project" , "current" ])
138167 assert result .exit_code == 0
139168 assert "Current project: main" in result .stdout
@@ -156,3 +185,24 @@ def test_project_option(cli_runner, temp_home, monkeypatch):
156185
157186 # Verify environment variable was set
158187 assert env_vars .get ("BASIC_MEMORY_PROJECT" ) == "test"
188+
189+
190+ def test_project_default_activates_project (cli_runner , temp_home , monkeypatch ):
191+ """Test that setting the default project also activates it in the current session."""
192+ # Create a test environment
193+ env = {}
194+ monkeypatch .setattr (os , "environ" , env )
195+
196+ # Create two test projects
197+ config_manager = ConfigManager ()
198+ config_manager .add_project ("project1" , str (temp_home / "project1" ))
199+
200+ # Set project1 as default using the CLI command
201+ result = cli_runner .invoke (app , ["project" , "default" , "project1" ])
202+ assert result .exit_code == 0
203+ assert "Project 'project1' set as default and activated" in result .stdout
204+
205+ # Verify the environment variable was set
206+ # This is the core of our fix - the set_default_project command now also sets
207+ # the BASIC_MEMORY_PROJECT environment variable to activate the project
208+ assert env .get ("BASIC_MEMORY_PROJECT" ) == "project1"
0 commit comments