test: add coverage for file_already_mined mtime check

Covers the check_mtime=True path in palace.py to meet 85% coverage threshold.
This commit is contained in:
bensig
2026-04-09 08:56:28 -07:00
parent c2308a1e36
commit 2448ac0026
+53
View File
@@ -1,12 +1,14 @@
import os import os
import shutil import shutil
import tempfile import tempfile
import time
from pathlib import Path from pathlib import Path
import chromadb import chromadb
import yaml import yaml
from mempalace.miner import mine, scan_project from mempalace.miner import mine, scan_project
from mempalace.palace import file_already_mined
def write_file(path: Path, content: str): 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"] assert scanned_files(project_root, respect_gitignore=False) == ["main.py"]
finally: finally:
shutil.rmtree(tmpdir) 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)