"""digest_lookup entrypoint tests (AGENTIC-WP-0011 T03).""" import json import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from session_memory.core.store import Store # noqa: E402 from session_memory.digest_lookup import lookup_digest, main, resolve_store_paths # noqa: E402 def _write_config(tmp_path) -> str: store = tmp_path / ".store" toml = tmp_path / "config.toml" toml.write_text( f'[store]\ndb_path = "{store / "m.db"}"\nblob_dir = "{store / "blobs"}"\n' f'cursor = "{store / "c.json"}"\n') return str(toml), str(store) def _seed(store_dir, uid="claude:test-uid"): st = Store(os.path.join(store_dir, "m.db"), os.path.join(store_dir, "blobs")) st.write_digest(uid, { "session_uid": uid, "flavor": "claude", "repo": "agentic-resources", "outcome": "success", "started_at": "2026-06-19T10:00:00Z", "ended_at": "2026-06-19T11:00:00Z", "cost": {"input_tokens": 100, "output_tokens": 25}, "tool_histogram": {"Bash": 10, "Edit": 5}, }) st.close() return uid def test_resolve_store_paths_from_config(tmp_path): cfg_path, store_dir = _write_config(tmp_path) db, blob = resolve_store_paths(config_path=cfg_path) assert db.endswith("m.db") assert blob.endswith("blobs") assert store_dir in db def test_resolve_store_paths_from_env(tmp_path, monkeypatch): db = tmp_path / "custom" / "mem.db" db.parent.mkdir(parents=True) monkeypatch.setenv("HELIX_STORE_DB", str(db)) resolved_db, blob = resolve_store_paths() assert resolved_db == str(db) assert blob == str(tmp_path / "custom" / "blobs") def test_lookup_digest_found_and_missing(tmp_path): cfg_path, store_dir = _write_config(tmp_path) uid = _seed(store_dir) found = lookup_digest(uid, config_path=cfg_path) assert found is not None and found["outcome"] == "success" assert lookup_digest("claude:missing", config_path=cfg_path) is None def test_main_json_success(tmp_path, capsys): cfg_path, store_dir = _write_config(tmp_path) uid = _seed(store_dir) rc = main(["--config", cfg_path, uid, "--json"]) assert rc == 0 data = json.loads(capsys.readouterr().out) assert data["session_uid"] == uid assert data["repo"] == "agentic-resources" def test_main_not_found(tmp_path, capsys): cfg_path, store_dir = _write_config(tmp_path) _seed(store_dir) rc = main(["--config", cfg_path, "claude:missing"]) assert rc == 1 assert "not found" in capsys.readouterr().err.lower()