store_location() answers None for a provider whose store is not a file, but callers used the file as the store's identity: the delivery and async-delegation ledgers rebuilt their scope from the file's parent, the session authority keyed its epoch fence and checked ownership on the resolved file, and handle caches, the media sweep, the subagent roster and the group controls compared or keyed stores by path. With no file each of those raised or skipped its work. Those callers now carry the profile scope: the ledgers open their scope directly (the tests that redirected the ledgers' file now redirect the scope), keys and comparisons use the scope's home key, and the gateway's ownership check keeps main's resolved-file test when the store has a file. persistence_assembly gains store_exists(scope), answered by the store's readiness probe, which replaces the .exists() and .is_file() checks that asked whether a profile has a store before a reader opens it; readers open the profile home rather than its file. Identity that is written down and read back keeps main's format for a file store and names the profile home otherwise: the Kanban owner receipt and the -w worktree lock, whose readers open either. The standalone hosted-rooms import has no legacy file beside a store without one, so it moves nothing. Features that work on the file itself (session repair, journal mode, the dashboard sidebar's file cache) refuse and say they need a file-backed store.
109 lines
4.0 KiB
Python
109 lines
4.0 KiB
Python
from unittest.mock import MagicMock, patch
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from cli import HermesCLI
|
|
from hermes_cli.main_agent_cmds import cmd_insights
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def existing_store(tmp_path, monkeypatch):
|
|
# insights short-circuits before opening when state.db is absent (fresh install).
|
|
monkeypatch.setattr("hermes_cli.config.get_hermes_home", lambda: tmp_path)
|
|
(tmp_path / "state.db").touch()
|
|
return tmp_path / "state.db"
|
|
|
|
|
|
class _InsightsEngineStub:
|
|
calls = []
|
|
engines = []
|
|
|
|
def __init__(self, db, *, persistence=None):
|
|
self.db = db
|
|
self.persistence = persistence
|
|
self.engines.append(self)
|
|
|
|
def generate(self, *, days=30, source=None):
|
|
self.calls.append({"days": days, "source": source})
|
|
return {"days": days, "source": source}
|
|
|
|
def format_terminal(self, report):
|
|
return f"days={report['days']} source={report['source']}"
|
|
|
|
|
|
def _reader_door():
|
|
"""A stand-in for ``open_persistence_reader``: its return value is the context, ``reader`` the handle it yields."""
|
|
door = MagicMock()
|
|
door.reader = door.return_value.__enter__.return_value
|
|
return door
|
|
|
|
|
|
def _run_show_insights(command: str):
|
|
cli_obj = HermesCLI.__new__(HermesCLI)
|
|
door = _reader_door()
|
|
_InsightsEngineStub.calls = []
|
|
_InsightsEngineStub.engines = []
|
|
with patch("persistence_assembly.open_persistence_reader", door), \
|
|
patch("agent.insights.InsightsEngine", _InsightsEngineStub):
|
|
cli_obj._show_insights(command)
|
|
assert [engine.persistence for engine in _InsightsEngineStub.engines] == [door.reader]
|
|
return _InsightsEngineStub.calls, door
|
|
|
|
|
|
def test_cli_insights_accepts_positional_days(capsys):
|
|
calls, door = _run_show_insights("/insights 7")
|
|
|
|
assert calls == [{"days": 7, "source": None}]
|
|
door.return_value.__exit__.assert_called_once()
|
|
assert "days=7 source=None" in capsys.readouterr().out
|
|
|
|
|
|
def test_cli_insights_keeps_days_flag_and_source(capsys):
|
|
calls, door = _run_show_insights("/insights --days 14 --source discord")
|
|
|
|
assert calls == [{"days": 14, "source": "discord"}]
|
|
door.return_value.__exit__.assert_called_once()
|
|
assert "days=14 source=discord" in capsys.readouterr().out
|
|
|
|
|
|
def test_insights_skips_open_when_store_missing(capsys, existing_store):
|
|
# Fresh install: no state.db yet → no open at all (a read-only open needs an existing file).
|
|
existing_store.unlink()
|
|
for run in (lambda: HermesCLI.__new__(HermesCLI)._show_insights("/insights 7"),
|
|
lambda: cmd_insights(SimpleNamespace(days=30, source=None))):
|
|
with patch("persistence_assembly.open_persistence_reader") as door:
|
|
run()
|
|
door.assert_not_called()
|
|
assert "No session data yet." in capsys.readouterr().out
|
|
|
|
|
|
def test_show_insights_opens_read_only(existing_store):
|
|
cli_obj = HermesCLI.__new__(HermesCLI)
|
|
door = _reader_door()
|
|
with patch("persistence_assembly.open_persistence_reader", door), \
|
|
patch("agent.insights.InsightsEngine", _InsightsEngineStub):
|
|
_InsightsEngineStub.calls = []
|
|
cli_obj._show_insights("/insights 7")
|
|
door.assert_called_once_with(existing_store.parent)
|
|
door.return_value.__exit__.assert_called_once()
|
|
|
|
|
|
def test_subcommand_insights_opens_read_only(existing_store):
|
|
door = _reader_door()
|
|
with patch("persistence_assembly.open_persistence_reader", door), \
|
|
patch("agent.insights.InsightsEngine", _InsightsEngineStub):
|
|
cmd_insights(SimpleNamespace(days=30, source=None))
|
|
door.assert_called_once_with(existing_store.parent)
|
|
door.return_value.__exit__.assert_called_once()
|
|
|
|
|
|
def test_subcommand_insights_closes_database_when_generation_fails(capsys):
|
|
door = _reader_door()
|
|
with patch("persistence_assembly.open_persistence_reader", door), \
|
|
patch("agent.insights.InsightsEngine", side_effect=RuntimeError("boom")):
|
|
cmd_insights(SimpleNamespace(days=30, source=None))
|
|
|
|
door.return_value.__exit__.assert_called_once()
|
|
assert "Error generating insights: boom" in capsys.readouterr().out
|