from __future__ import annotations import sys from pathlib import Path import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) from cc_slim.memory import MemoryStore def make_store(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> tuple[MemoryStore, Path]: home = tmp_path / "home" home.mkdir() monkeypatch.setattr("cc_slim.memory.Path.home", lambda: home) workspace = tmp_path / "workspace" workspace.mkdir() return MemoryStore(workspace), workspace def test_memory_initializes_structured_template(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: store, _ = make_store(monkeypatch, tmp_path) content = store.read() assert content.startswith("# Memory") assert "## User Memory" in content assert "## Project Memory" in content assert "## Constraints" in content assert "## Consolidated Facts" in content assert "## Scratch Notes" in content def test_memory_migrates_v1_text_into_scratch_notes(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: store, workspace = make_store(monkeypatch, tmp_path) store.path().write_text("old note\nsecond line\n", encoding="utf-8") migrated = MemoryStore(workspace) sections = migrated.read_sections() assert sections["Scratch Notes"] == "- old note\n second line" def test_read_sections_parses_structured_memory(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: store, _ = make_store(monkeypatch, tmp_path) store.path().write_text( """# Memory ## User Memory - prefers concise output ## Project Memory - use uv run ## Constraints - windows first ## Consolidated Facts - workspace is repo root ## Scratch Notes - temporary note """, encoding="utf-8", ) sections = store.read_sections() assert sections["User Memory"] == "- prefers concise output" assert sections["Project Memory"] == "- use uv run" assert sections["Constraints"] == "- windows first" assert sections["Consolidated Facts"] == "- workspace is repo root" assert sections["Scratch Notes"] == "- temporary note" def test_apply_dream_updates_structured_sections_only(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: store, _ = make_store(monkeypatch, tmp_path) store.path().write_text( """# Memory ## User Memory - old user ## Project Memory - old project ## Constraints - old constraint ## Consolidated Facts - old fact ## Scratch Notes - keep me """, encoding="utf-8", ) store.apply_dream( """## User Memory - new user ## Project Memory - new project ## Constraints - new constraint ## Consolidated Facts - new fact """ ) sections = store.read_sections() assert sections["User Memory"] == "- new user" assert sections["Project Memory"] == "- new project" assert sections["Constraints"] == "- new constraint" assert sections["Consolidated Facts"] == "- new fact" assert sections["Scratch Notes"] == "- keep me" def test_apply_dream_keeps_unmentioned_sections(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: store, _ = make_store(monkeypatch, tmp_path) store.path().write_text( """# Memory ## User Memory - old user ## Project Memory - old project ## Constraints - old constraint ## Consolidated Facts - old fact ## Scratch Notes - keep me """, encoding="utf-8", ) store.apply_dream( """## Project Memory - updated project ## Consolidated Facts - updated fact """ ) sections = store.read_sections() assert sections["User Memory"] == "- old user" assert sections["Project Memory"] == "- updated project" assert sections["Constraints"] == "- old constraint" assert sections["Consolidated Facts"] == "- updated fact" assert sections["Scratch Notes"] == "- keep me"