|
| 1 | +"""Tests for the RETIRED-marker check in _open_collection_or_explain |
| 2 | +and _check_local_palace_retired.""" |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | + |
| 7 | +def test_open_collection_emits_retired_marker_when_default_path_missing( |
| 8 | + tmp_path, monkeypatch, capsys |
| 9 | +): |
| 10 | + """If ~/.mempalace/RETIRED exists and palace_path is the default |
| 11 | + (~/.mempalace/palace) and the dir is absent, the helper emits the |
| 12 | + marker content instead of 'Run: mempalace init'.""" |
| 13 | + from mempalace import palace |
| 14 | + |
| 15 | + fake_home = tmp_path / "home" |
| 16 | + fake_home.mkdir() |
| 17 | + (fake_home / ".mempalace").mkdir() |
| 18 | + (fake_home / ".mempalace" / "RETIRED").write_text( |
| 19 | + "local palace retired 2026-05-14\nset PALACE_DAEMON_URL\n" |
| 20 | + ) |
| 21 | + |
| 22 | + monkeypatch.setattr(os.path, "expanduser", lambda p: p.replace("~", str(fake_home))) |
| 23 | + |
| 24 | + default_palace = str(fake_home / ".mempalace" / "palace") |
| 25 | + out_lines: list[str] = [] |
| 26 | + result = palace._open_collection_or_explain(default_palace, out=out_lines.append) |
| 27 | + assert result is None |
| 28 | + joined = "\n".join(out_lines) |
| 29 | + assert "RETIRED" in joined |
| 30 | + assert "retired 2026-05-14" in joined |
| 31 | + assert "set PALACE_DAEMON_URL" in joined |
| 32 | + assert "Run: mempalace init" not in joined |
| 33 | + |
| 34 | + |
| 35 | +def test_open_collection_falls_through_to_init_hint_when_no_marker(tmp_path): |
| 36 | + """When no RETIRED marker exists, the helper emits the legacy |
| 37 | + 'No palace found / Run: mempalace init' message.""" |
| 38 | + from mempalace import palace |
| 39 | + |
| 40 | + missing = str(tmp_path / "nope") |
| 41 | + out_lines: list[str] = [] |
| 42 | + result = palace._open_collection_or_explain(missing, out=out_lines.append) |
| 43 | + assert result is None |
| 44 | + joined = "\n".join(out_lines) |
| 45 | + assert "Run: mempalace init" in joined |
| 46 | + assert "RETIRED" not in joined |
| 47 | + |
| 48 | + |
| 49 | +def test_open_collection_ignores_marker_for_non_default_path(tmp_path, monkeypatch): |
| 50 | + """The RETIRED marker only gates the DEFAULT path. An explicit |
| 51 | + --palace <other-dir> bypasses it (used for forensic reads of the |
| 52 | + archived palace).""" |
| 53 | + from mempalace import palace |
| 54 | + |
| 55 | + fake_home = tmp_path / "home" |
| 56 | + (fake_home / ".mempalace").mkdir(parents=True) |
| 57 | + (fake_home / ".mempalace" / "RETIRED").write_text("retired") |
| 58 | + monkeypatch.setattr(os.path, "expanduser", lambda p: p.replace("~", str(fake_home))) |
| 59 | + |
| 60 | + explicit = str(tmp_path / "some-other-palace") |
| 61 | + out_lines: list[str] = [] |
| 62 | + palace._open_collection_or_explain(explicit, out=out_lines.append) |
| 63 | + joined = "\n".join(out_lines) |
| 64 | + assert "RETIRED" not in joined # marker NOT triggered for explicit path |
| 65 | + assert "Run: mempalace init" in joined |
| 66 | + |
| 67 | + |
| 68 | +def test_check_retired_palace_skipped_with_escape_hatch(tmp_path, monkeypatch): |
| 69 | + """MEMPALACE_ALLOW_RETIRED_PALACE=1 lets forensic reads through.""" |
| 70 | + from mempalace.mcp_server import _check_local_palace_retired |
| 71 | + |
| 72 | + fake_home = tmp_path / "home" |
| 73 | + (fake_home / ".mempalace").mkdir(parents=True) |
| 74 | + (fake_home / ".mempalace" / "RETIRED").write_text("retired") |
| 75 | + monkeypatch.setattr(os.path, "expanduser", lambda p: p.replace("~", str(fake_home))) |
| 76 | + monkeypatch.setenv("MEMPALACE_ALLOW_RETIRED_PALACE", "1") |
| 77 | + |
| 78 | + default_palace = str(fake_home / ".mempalace" / "palace") |
| 79 | + _check_local_palace_retired(default_palace) # must not raise |
0 commit comments