105 lines
3.4 KiB
Python
105 lines
3.4 KiB
Python
"""文件变更追踪器测试."""
|
||
import hashlib
|
||
from pathlib import Path
|
||
|
||
from src.core.file_tracker import FileTracker
|
||
|
||
|
||
def test_compute_file_hash(tmp_path: Path):
|
||
"""计算文件 SHA256 哈希."""
|
||
file = tmp_path / "test.md"
|
||
file.write_text("hello world", encoding="utf-8")
|
||
result = FileTracker.compute_hash(str(file))
|
||
expected = hashlib.sha256(b"hello world").hexdigest()
|
||
assert result == expected
|
||
|
||
|
||
def test_compute_hash_deterministic(tmp_path: Path):
|
||
"""同一文件内容产生相同哈希."""
|
||
file = tmp_path / "a.md"
|
||
file.write_text("same content", encoding="utf-8")
|
||
assert FileTracker.compute_hash(str(file)) == FileTracker.compute_hash(str(file))
|
||
|
||
|
||
def test_hash_changes_with_content(tmp_path: Path):
|
||
"""内容变更导致哈希不同."""
|
||
file = tmp_path / "b.md"
|
||
file.write_text("v1", encoding="utf-8")
|
||
h1 = FileTracker.compute_hash(str(file))
|
||
file.write_text("v2", encoding="utf-8")
|
||
h2 = FileTracker.compute_hash(str(file))
|
||
assert h1 != h2
|
||
|
||
|
||
def test_is_stale_new_file(tmp_path: Path):
|
||
"""新文件(无记录)视为过期."""
|
||
file = tmp_path / "new.md"
|
||
file.write_text("content", encoding="utf-8")
|
||
tracker = FileTracker(tmp_path / "tracker.json")
|
||
record = tracker.get_record(str(file))
|
||
assert record is None
|
||
assert tracker.is_stale(str(file)) is True
|
||
|
||
|
||
def test_is_stale_unchanged_file(tmp_path: Path):
|
||
"""未修改文件视为未过期."""
|
||
file = tmp_path / "unchanged.md"
|
||
file.write_text("stable", encoding="utf-8")
|
||
tracker = FileTracker(tmp_path / "tracker.json")
|
||
tracker.mark_ingested(str(file))
|
||
assert tracker.is_stale(str(file)) is False
|
||
|
||
|
||
def test_is_stale_modified_file(tmp_path: Path):
|
||
"""修改后文件视为过期."""
|
||
file = tmp_path / "modified.md"
|
||
file.write_text("v1", encoding="utf-8")
|
||
tracker = FileTracker(tmp_path / "tracker.json")
|
||
tracker.mark_ingested(str(file))
|
||
file.write_text("v2", encoding="utf-8")
|
||
assert tracker.is_stale(str(file)) is True
|
||
|
||
|
||
def test_mark_ingested_updates_record(tmp_path: Path):
|
||
"""mark_ingested 创建/更新记录."""
|
||
file = tmp_path / "x.md"
|
||
file.write_text("hello", encoding="utf-8")
|
||
tracker = FileTracker(tmp_path / "tracker.json")
|
||
tracker.mark_ingested(str(file))
|
||
record = tracker.get_record(str(file))
|
||
assert record is not None
|
||
assert record["hash"] == FileTracker.compute_hash(str(file))
|
||
assert "ingested_at" in record
|
||
|
||
|
||
def test_persistence_across_instances(tmp_path: Path):
|
||
"""tracker 数据持久化到 JSON,跨实例可读."""
|
||
file = tmp_path / "p.md"
|
||
file.write_text("persist me", encoding="utf-8")
|
||
db_path = tmp_path / "tracker.json"
|
||
|
||
t1 = FileTracker(db_path)
|
||
t1.mark_ingested(str(file))
|
||
|
||
t2 = FileTracker(db_path)
|
||
assert t2.is_stale(str(file)) is False
|
||
|
||
|
||
def test_file_deleted_considered_stale(tmp_path: Path):
|
||
"""文件被删除后视为过期."""
|
||
file = tmp_path / "tmp.md"
|
||
file.write_text("temp", encoding="utf-8")
|
||
tracker = FileTracker(tmp_path / "tracker.json")
|
||
tracker.mark_ingested(str(file))
|
||
file.unlink()
|
||
assert tracker.is_stale(str(file)) is True
|
||
|
||
|
||
def test_binary_file_hash(tmp_path: Path):
|
||
"""二进制文件也能正确计算哈希(如 PDF)."""
|
||
file = tmp_path / "doc.pdf"
|
||
file.write_bytes(b"\x00\x01\x02\x03")
|
||
h = FileTracker.compute_hash(str(file))
|
||
assert len(h) == 64
|
||
assert h == hashlib.sha256(b"\x00\x01\x02\x03").hexdigest()
|