diff --git a/tests/test_miner.py b/tests/test_miner.py index efe55a7..056fdaa 100644 --- a/tests/test_miner.py +++ b/tests/test_miner.py @@ -1,12 +1,14 @@ import os import shutil import tempfile +import time from pathlib import Path import chromadb import yaml from mempalace.miner import mine, scan_project +from mempalace.palace import file_already_mined def write_file(path: Path, content: str): @@ -206,3 +208,54 @@ def test_scan_project_skip_dirs_still_apply_without_override(): assert scanned_files(project_root, respect_gitignore=False) == ["main.py"] finally: shutil.rmtree(tmpdir) + + +def test_file_already_mined_check_mtime(): + tmpdir = tempfile.mkdtemp() + try: + palace_path = os.path.join(tmpdir, "palace") + os.makedirs(palace_path) + client = chromadb.PersistentClient(path=palace_path) + col = client.get_or_create_collection("mempalace_drawers") + + test_file = os.path.join(tmpdir, "test.txt") + with open(test_file, "w") as f: + f.write("hello world") + + mtime = os.path.getmtime(test_file) + + # Not mined yet + assert file_already_mined(col, test_file) is False + assert file_already_mined(col, test_file, check_mtime=True) is False + + # Add it with mtime + col.add( + ids=["d1"], + documents=["hello world"], + metadatas=[{"source_file": test_file, "source_mtime": str(mtime)}], + ) + + # Already mined (no mtime check) + assert file_already_mined(col, test_file) is True + # Already mined (mtime matches) + assert file_already_mined(col, test_file, check_mtime=True) is True + + # Modify file so mtime changes + time.sleep(0.1) + with open(test_file, "w") as f: + f.write("modified content") + + # Still mined without mtime check + assert file_already_mined(col, test_file) is True + # Needs re-mining with mtime check + assert file_already_mined(col, test_file, check_mtime=True) is False + + # Record with no mtime stored should return False for check_mtime + col.add( + ids=["d2"], + documents=["other"], + metadatas=[{"source_file": "/fake/no_mtime.txt"}], + ) + assert file_already_mined(col, "/fake/no_mtime.txt", check_mtime=True) is False + finally: + shutil.rmtree(tmpdir)