From 426d164df46be993b84bde56162ee3a3fe2b1f35 Mon Sep 17 00:00:00 2001 From: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> Date: Mon, 18 May 2026 17:28:50 -0300 Subject: [PATCH 1/2] test(hooks): isolate test_hooks_cli from test_cli side effect (#1510) Nine save/log/precompact tests in test_hooks_cli.py passed only because test_cli.py (alphabetically earlier) created ~/.mempalace in the session tmp HOME as a side effect, satisfying the _palace_root_exists() kill-switch. Run in isolation they short-circuited and failed (9 failed, 80 passed, 1 skipped). Add a module autouse fixture that points PALACE_ROOT/STATE_DIR at a per-test palace root that exists, so every test is robust standalone and future tests don't inherit the trap. Kill-switch tests that need the absent path call _redirect_palace_root after the fixture; monkeypatch last-write-wins keeps their absent/file root and teardown restores the real module value. Isolation: pytest tests/test_hooks_cli.py -> 100 passed, 1 skipped. Ordering preserved: test_cli + test_hooks_cli -> 165 passed. Closes #1510 --- tests/test_hooks_cli.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_hooks_cli.py b/tests/test_hooks_cli.py index 234bef2bce..e4309fc28b 100644 --- a/tests/test_hooks_cli.py +++ b/tests/test_hooks_cli.py @@ -31,6 +31,31 @@ ) +@pytest.fixture(autouse=True) +def _isolated_existing_palace_root(monkeypatch, tmp_path): + """Give every test an isolated, *existing* PALACE_ROOT/STATE_DIR. + + Regression for #1510: nine save / log / precompact tests assumed + ``~/.mempalace`` existed and only passed in the full suite because an + earlier test file (``test_cli.py``) created it as a side effect, so + the ``_palace_root_exists()`` kill-switch was satisfied. Run in + isolation they short-circuited and failed. + + Defaulting every test to a per-test palace root that exists makes + them robust on their own and protects future tests from the same + trap. Tests that exercise the absent-root kill-switch path call + ``_redirect_palace_root`` (or set their own PALACE_ROOT) *after* this + fixture; ``monkeypatch``'s last-write-wins means they keep their + absent/file root and teardown still restores the real module value. + """ + root = tmp_path / ".mempalace" + root.mkdir(exist_ok=True) + monkeypatch.setattr(hooks_cli_mod, "PALACE_ROOT", root) + monkeypatch.setattr(hooks_cli_mod, "STATE_DIR", root / "hook_state") + monkeypatch.setattr(hooks_cli_mod, "_state_dir_initialized", False) + return root + + # --- _mempalace_python --- From 90fece7628941e27474241b8cfa0a65280d02f67 Mon Sep 17 00:00:00 2001 From: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> Date: Mon, 18 May 2026 17:39:12 -0300 Subject: [PATCH 2/2] test(hooks): also isolate _MINE_PID_DIR + create the state dir (#1510) Review feedback: _MINE_PID_DIR is derived from STATE_DIR at module import (hooks_cli.py:277), so patching STATE_DIR alone left mine-spawning tests writing PID files under the import-time location instead of the per-test root. Patch _MINE_PID_DIR too, and create the state dir so the fixture's 'existing' docstring is accurate. Isolation still 100 passed/1 skipped; ordering still 165 passed. --- tests/test_hooks_cli.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/test_hooks_cli.py b/tests/test_hooks_cli.py index e4309fc28b..8062c6a75a 100644 --- a/tests/test_hooks_cli.py +++ b/tests/test_hooks_cli.py @@ -43,15 +43,23 @@ def _isolated_existing_palace_root(monkeypatch, tmp_path): Defaulting every test to a per-test palace root that exists makes them robust on their own and protects future tests from the same - trap. Tests that exercise the absent-root kill-switch path call + trap. ``_MINE_PID_DIR`` is patched too: it is derived from + ``STATE_DIR`` *at module import* (hooks_cli.py:277), so patching + ``STATE_DIR`` alone would leave mine-spawning tests writing PID files + under the import-time location instead of the per-test root. The + state dir is created so the docstring's "existing" promise holds. + + Tests that exercise the absent-root kill-switch path call ``_redirect_palace_root`` (or set their own PALACE_ROOT) *after* this fixture; ``monkeypatch``'s last-write-wins means they keep their absent/file root and teardown still restores the real module value. """ root = tmp_path / ".mempalace" - root.mkdir(exist_ok=True) + state_dir = root / "hook_state" + state_dir.mkdir(parents=True, exist_ok=True) monkeypatch.setattr(hooks_cli_mod, "PALACE_ROOT", root) - monkeypatch.setattr(hooks_cli_mod, "STATE_DIR", root / "hook_state") + monkeypatch.setattr(hooks_cli_mod, "STATE_DIR", state_dir) + monkeypatch.setattr(hooks_cli_mod, "_MINE_PID_DIR", state_dir / "mine_pids") monkeypatch.setattr(hooks_cli_mod, "_state_dir_initialized", False) return root