@@ -175,6 +175,61 @@ def test_cmd_init_normalizes_wing_name_for_topics_registry(mock_config_cls, tmp_
175175 assert mock_register .call_args .kwargs ["wing" ] == "my_cool_app"
176176
177177
178+ def test_cmd_init_honors_palace_flag (tmp_path , monkeypatch ):
179+ """Regression for #1313: ``cmd_init`` must honor ``--palace`` instead of
180+ silently writing to ``~/.mempalace``. Mirrors the env-var pattern used
181+ by ``cmd_mine`` / ``cmd_status`` / ``mcp_server`` so every downstream
182+ read of ``cfg.palace_path`` (Pass 0, ``cfg.init()``, post-init mine)
183+ routes to the user-specified location.
184+ """
185+ project = tmp_path / "project"
186+ project .mkdir ()
187+ palace = tmp_path / "custom_palace"
188+
189+ # Make sure no leftover env var from another test leaks in — we want to
190+ # verify that --palace ALONE drives the resolution. Prime monkeypatch's
191+ # undo list with setenv first so that the env var ``cmd_init`` writes
192+ # below is rolled back at teardown (``delenv(raising=False)`` on a
193+ # missing key registers no undo entry, which would leak into the next
194+ # test).
195+ monkeypatch .setenv ("MEMPALACE_PALACE_PATH" , "" )
196+ monkeypatch .setenv ("MEMPAL_PALACE_PATH" , "" )
197+ monkeypatch .delenv ("MEMPALACE_PALACE_PATH" )
198+ monkeypatch .delenv ("MEMPAL_PALACE_PATH" )
199+
200+ args = argparse .Namespace (
201+ dir = str (project ),
202+ palace = str (palace ),
203+ yes = True ,
204+ auto_mine = False ,
205+ )
206+
207+ captured = {}
208+
209+ def fake_pass_zero (project_dir , palace_dir , llm_provider ):
210+ # Capture the palace_dir Pass 0 sees — this is the smoking-gun
211+ # value for the bug. Pre-fix it was always ~/.mempalace.
212+ captured ["pass_zero_palace_dir" ] = palace_dir
213+ return None
214+
215+ with (
216+ patch ("mempalace.entity_detector.scan_for_detection" , return_value = []),
217+ patch ("mempalace.room_detector_local.detect_rooms_local" ),
218+ patch ("mempalace.cli._run_pass_zero" , side_effect = fake_pass_zero ),
219+ patch ("mempalace.cli._maybe_run_mine_after_init" ),
220+ ):
221+ cmd_init (args )
222+
223+ expected = str (palace )
224+ # Pass 0 must have been handed the --palace location, not ~/.mempalace.
225+ assert captured ["pass_zero_palace_dir" ] == expected
226+ # And the env var must point at the custom palace so any downstream
227+ # ``cfg.palace_path`` read in this process resolves correctly too.
228+ import os
229+
230+ assert os .environ .get ("MEMPALACE_PALACE_PATH" ) == os .path .abspath (expected )
231+
232+
178233@patch ("mempalace.cli.MempalaceConfig" )
179234def test_cmd_init_with_entities_zero_total (mock_config_cls , tmp_path , capsys ):
180235 """When entities detected but total is 0, prints 'No entities' message."""
0 commit comments