import uuid import memory_store from tools.memory_write import ( user_note_append, user_note_replace, user_note_set, user_note_view, ) def _create_block(tmp_path): # simulate creating a block directly through the store for isolation bid = str(uuid.uuid4()) blk = memory_store._Block(id=bid, label="test", value="", limit=None, attached_agents=[]) # type: ignore memory_store._save_block(blk) # type: ignore return bid def test_append_and_view(tmp_path, monkeypatch): monkeypatch.setattr(memory_store, 'BASE_DIR', tmp_path) tmp_path.mkdir(parents=True, exist_ok=True) bid = _create_block(tmp_path) r1 = user_note_append(bid, "First note") assert "First note" in r1 r2 = user_note_append(bid, "Second note") assert "Second note" in r2 view = user_note_view(bid, last=2) assert "First note" in view and "Second note" in view def test_replace_flow(tmp_path, monkeypatch): monkeypatch.setattr(memory_store, 'BASE_DIR', tmp_path) tmp_path.mkdir(parents=True, exist_ok=True) bid = _create_block(tmp_path) user_note_append(bid, "Alpha") out = user_note_replace(bid, "Alpha-Edited") assert "Alpha-Edited" in out view = user_note_view(bid, last=1) # 'Alpha' substring will still appear inside 'Alpha-Edited'; just assert edited form present assert "Alpha-Edited" in view def test_set_overwrites(tmp_path, monkeypatch): monkeypatch.setattr(memory_store, 'BASE_DIR', tmp_path) tmp_path.mkdir(parents=True, exist_ok=True) bid = _create_block(tmp_path) user_note_append(bid, "One") user_note_append(bid, "Two") user_note_set(bid, "Custom content block") v = user_note_view(bid, last=5) assert "Custom content block" in v # After set, old note format may not persist assert "One" not in v and "Two" not in v def test_errors(tmp_path, monkeypatch): monkeypatch.setattr(memory_store, 'BASE_DIR', tmp_path) tmp_path.mkdir(parents=True, exist_ok=True) # invalid block try: user_note_append("missing", "x") except Exception as e: assert "Block not found" in str(e) bid = _create_block(tmp_path) try: user_note_replace(bid, "") except Exception as e: assert "note cannot be empty" in str(e) try: user_note_replace(bid, "New") except Exception as e: assert "no existing notes" in str(e)