|
| 1 | +"""Regression tests for the live-MCP-process pre-flight in `bm reset` (#765).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | + |
| 7 | +import psutil |
| 8 | +import pytest |
| 9 | +import typer |
| 10 | + |
| 11 | +from basic_memory.cli.commands import db as db_cmd |
| 12 | + |
| 13 | + |
| 14 | +class _FakeProc: |
| 15 | + """Minimal stand-in for psutil.Process; only exposes .info.""" |
| 16 | + |
| 17 | + def __init__(self, pid: int, cmdline: list[str] | None): |
| 18 | + self.info = {"pid": pid, "cmdline": cmdline} |
| 19 | + |
| 20 | + |
| 21 | +def _patch_iter(monkeypatch: pytest.MonkeyPatch, procs) -> None: |
| 22 | + """Replace psutil.process_iter with a fixed iterator. |
| 23 | +
|
| 24 | + Procs is intentionally untyped: tests pass a mix of _FakeProc and |
| 25 | + error-raising stand-ins to exercise the per-process exception path. |
| 26 | + """ |
| 27 | + monkeypatch.setattr( |
| 28 | + psutil, |
| 29 | + "process_iter", |
| 30 | + lambda attrs=None: iter(procs), |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +class TestFindLiveMcpProcesses: |
| 35 | + def test_returns_empty_when_no_mcp_processes(self, monkeypatch): |
| 36 | + _patch_iter( |
| 37 | + monkeypatch, |
| 38 | + [ |
| 39 | + _FakeProc(pid=11, cmdline=["python", "-m", "http.server"]), |
| 40 | + _FakeProc(pid=22, cmdline=["bm", "sync"]), |
| 41 | + ], |
| 42 | + ) |
| 43 | + assert db_cmd._find_live_mcp_processes() == [] |
| 44 | + |
| 45 | + def test_matches_basic_memory_mcp_invocations(self, monkeypatch): |
| 46 | + _patch_iter( |
| 47 | + monkeypatch, |
| 48 | + [ |
| 49 | + # Direct `basic-memory mcp`. |
| 50 | + _FakeProc(pid=101, cmdline=["/usr/bin/python", "basic-memory", "mcp"]), |
| 51 | + # `bm mcp` alias entrypoint — must also match (#765 P1). |
| 52 | + _FakeProc(pid=202, cmdline=["bm", "mcp"]), |
| 53 | + # Python module form, underscore name. |
| 54 | + _FakeProc( |
| 55 | + pid=303, |
| 56 | + cmdline=["python", "-m", "basic_memory.cli.main", "mcp"], |
| 57 | + ), |
| 58 | + # Absolute path to the bm script. |
| 59 | + _FakeProc(pid=404, cmdline=["/usr/local/bin/bm", "mcp"]), |
| 60 | + # Windows-style bm.exe. |
| 61 | + _FakeProc(pid=505, cmdline=["C:\\Users\\me\\.venv\\Scripts\\bm.exe", "mcp"]), |
| 62 | + # Should NOT match — `mcp` is a substring of another arg, not a token. |
| 63 | + _FakeProc(pid=606, cmdline=["python", "basic-memory", "mcp-helper"]), |
| 64 | + # Should NOT match — has `mcp` but no basic-memory/bm signature. |
| 65 | + _FakeProc(pid=707, cmdline=["python", "/some/other/server.py", "mcp"]), |
| 66 | + ], |
| 67 | + ) |
| 68 | + result = db_cmd._find_live_mcp_processes() |
| 69 | + pids = sorted(pid for pid, _ in result) |
| 70 | + assert pids == [101, 202, 303, 404, 505] |
| 71 | + |
| 72 | + def test_skips_current_process(self, monkeypatch): |
| 73 | + me = os.getpid() |
| 74 | + _patch_iter( |
| 75 | + monkeypatch, |
| 76 | + [ |
| 77 | + _FakeProc(pid=me, cmdline=["python", "basic-memory", "mcp"]), |
| 78 | + ], |
| 79 | + ) |
| 80 | + # Self-match is suppressed so the helper can be called from inside |
| 81 | + # `bm reset` without flagging the running process. |
| 82 | + assert db_cmd._find_live_mcp_processes() == [] |
| 83 | + |
| 84 | + def test_skips_processes_with_no_cmdline(self, monkeypatch): |
| 85 | + _patch_iter( |
| 86 | + monkeypatch, |
| 87 | + [ |
| 88 | + _FakeProc(pid=1, cmdline=None), # kernel-style process |
| 89 | + _FakeProc(pid=2, cmdline=[]), |
| 90 | + ], |
| 91 | + ) |
| 92 | + assert db_cmd._find_live_mcp_processes() == [] |
| 93 | + |
| 94 | + def test_swallows_per_process_errors(self, monkeypatch): |
| 95 | + """A NoSuchProcess race during iteration must not abort the scan.""" |
| 96 | + |
| 97 | + class _Raising: |
| 98 | + @property |
| 99 | + def info(self): |
| 100 | + raise psutil.NoSuchProcess(pid=999) |
| 101 | + |
| 102 | + _patch_iter( |
| 103 | + monkeypatch, |
| 104 | + [ |
| 105 | + _Raising(), |
| 106 | + _FakeProc(pid=42, cmdline=["python", "basic-memory", "mcp"]), |
| 107 | + ], |
| 108 | + ) |
| 109 | + result = db_cmd._find_live_mcp_processes() |
| 110 | + assert [pid for pid, _ in result] == [42] |
| 111 | + |
| 112 | + |
| 113 | +class TestAbortIfMcpProcessesAlive: |
| 114 | + def test_no_op_when_no_processes(self, monkeypatch): |
| 115 | + monkeypatch.setattr(db_cmd, "_find_live_mcp_processes", lambda: []) |
| 116 | + # Must not raise — destructive work should proceed. |
| 117 | + db_cmd._abort_if_mcp_processes_alive() |
| 118 | + |
| 119 | + def test_exits_with_pids_when_processes_alive(self, monkeypatch, capsys): |
| 120 | + monkeypatch.setattr( |
| 121 | + db_cmd, |
| 122 | + "_find_live_mcp_processes", |
| 123 | + lambda: [(123, "python basic-memory mcp"), (456, "uv run bm mcp wrapper")], |
| 124 | + ) |
| 125 | + with pytest.raises(typer.Exit) as exc_info: |
| 126 | + db_cmd._abort_if_mcp_processes_alive() |
| 127 | + assert exc_info.value.exit_code == 1 |
| 128 | + |
| 129 | + captured = capsys.readouterr() |
| 130 | + # PIDs surface so the user can target the cleanup themselves. |
| 131 | + assert "123" in captured.out |
| 132 | + assert "456" in captured.out |
| 133 | + assert "MCP processes" in captured.out |
| 134 | + |
| 135 | + def test_prints_platform_specific_cleanup_hint_posix(self, monkeypatch, capsys): |
| 136 | + monkeypatch.setattr(os, "name", "posix") |
| 137 | + monkeypatch.setattr( |
| 138 | + db_cmd, |
| 139 | + "_find_live_mcp_processes", |
| 140 | + lambda: [(7, "python basic-memory mcp")], |
| 141 | + ) |
| 142 | + with pytest.raises(typer.Exit): |
| 143 | + db_cmd._abort_if_mcp_processes_alive() |
| 144 | + out = capsys.readouterr().out |
| 145 | + assert "pgrep -fa 'basic-memory mcp'" in out |
| 146 | + |
| 147 | + def test_prints_platform_specific_cleanup_hint_windows(self, monkeypatch, capsys): |
| 148 | + monkeypatch.setattr(os, "name", "nt") |
| 149 | + monkeypatch.setattr( |
| 150 | + db_cmd, |
| 151 | + "_find_live_mcp_processes", |
| 152 | + lambda: [(7, "python basic-memory mcp")], |
| 153 | + ) |
| 154 | + with pytest.raises(typer.Exit): |
| 155 | + db_cmd._abort_if_mcp_processes_alive() |
| 156 | + out = capsys.readouterr().out |
| 157 | + assert "Get-CimInstance" in out |
0 commit comments