test(mcp_registry): cover the atomic ledger write and failed-removal warnings

Two regression tests for this branch's changes, each failing on origin/main:
- ENOSPC at fsync during clear_install leaves mcp_installs.json intact and no
  temp file (the old write_text truncated first and never fsynced);
- for the claude, codex and grok registrars, a removal whose write fails returns
  False, keeps the config bytes, and logs "could not remove 'headroom' from <path>".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Assisted-by: claude-code:claude-opus-5-5
Headline-Session: 6e77a807-8dd4-4a77-a2fa-ddce6dc2515e
Headline-AI-Share: 68% strict / 68% weighted
This commit is contained in:
Tejas Chopra
2026-10-09 18:32:57 -07:00
co-authored by Claude Opus 5.5
parent 6d2fc61de6
commit ad4a74d6d1
2 changed files with 85 additions and 0 deletions
+17
View File
@@ -117,3 +117,20 @@ def test_record_install_recovers_from_unsafe_ledger_shape(tmp_path, contents):
record_install("claude", _spec(), path=ledger)
assert headroom_installed_matching("claude", _spec(), path=ledger)
def test_failed_ledger_write_keeps_the_previous_ledger(monkeypatch, tmp_path):
"""ENOSPC mid-write must not truncate the ledger or leave a temp file."""
ledger = tmp_path / "mcp_installs.json"
record_install("claude", _spec(), path=ledger)
before = ledger.read_bytes()
def no_space(fd: int) -> None:
raise OSError(28, "No space left on device")
monkeypatch.setattr("headroom.fsutil.os.fsync", no_space)
with pytest.raises(OSError, match="No space left"):
clear_install("claude", "serena", path=ledger)
assert ledger.read_bytes() == before
assert [p.name for p in tmp_path.iterdir()] == ["mcp_installs.json"]
@@ -0,0 +1,68 @@
"""A removal whose config write fails returns False and says why, per registrar."""
from __future__ import annotations
import logging
from pathlib import Path
import pytest
from headroom.mcp_registry.base import MCPRegistrar, RegisterStatus, ServerSpec
from headroom.mcp_registry.claude import ClaudeRegistrar
from headroom.mcp_registry.codex import CodexRegistrar
from headroom.mcp_registry.grok import GrokRegistrar
def _no_space(fd: int) -> None:
raise OSError(28, "No space left on device")
@pytest.mark.parametrize(
("make", "config", "logger_name"),
[
(
lambda home: ClaudeRegistrar(claude_cli=None, home_dir=home),
".claude.json",
"headroom.mcp_registry.claude",
),
(
lambda home: CodexRegistrar(home_dir=home),
".codex/config.toml",
"headroom.mcp_registry.codex",
),
(
lambda home: GrokRegistrar(home_dir=home),
".grok/config.toml",
"headroom.mcp_registry.grok",
),
],
ids=["claude", "codex", "grok"],
)
def test_failed_removal_keeps_the_config_and_logs_why(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
make,
config: str,
logger_name: str,
) -> None:
monkeypatch.delenv("GROK_HOME", raising=False)
monkeypatch.delenv("CODEX_HOME", raising=False)
monkeypatch.chdir(tmp_path)
registrar: MCPRegistrar = make(tmp_path)
spec = ServerSpec(name="headroom", command="/usr/bin/python", args=("-m", "headroom.cli"))
assert registrar.register_server(spec).status == RegisterStatus.REGISTERED
path = tmp_path / config
before = path.read_bytes()
monkeypatch.setattr("headroom.fsutil.os.fsync", _no_space)
with caplog.at_level(logging.WARNING, logger=logger_name):
assert registrar.unregister_server("headroom") is False
assert path.read_bytes() == before
messages = [r.getMessage() for r in caplog.records if r.name == logger_name]
assert messages
assert all(
m.startswith(f"could not remove 'headroom' from {path}") and "No space left" in m
for m in messages
)