first commit
This commit is contained in:
156
hd-cli/test_hd_problems.py
Normal file
156
hd-cli/test_hd_problems.py
Normal file
@@ -0,0 +1,156 @@
|
||||
#!/usr/bin/env python3
|
||||
import fcntl
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent.parent / "hd_common"))
|
||||
from hd_core import (
|
||||
NoteState, ReconcileLock, State, content_hash, detect_problems,
|
||||
load_problems, save_problems, Problem,
|
||||
)
|
||||
|
||||
PASS = []
|
||||
FAIL = []
|
||||
|
||||
|
||||
def check(name, cond, detail=""):
|
||||
if cond:
|
||||
PASS.append(name)
|
||||
else:
|
||||
FAIL.append((name, detail))
|
||||
print(f"FAIL: {name} {detail}")
|
||||
|
||||
|
||||
def main():
|
||||
tmpdir = Path(tempfile.mkdtemp(prefix="hd-problems-test-"))
|
||||
root = tmpdir / "notes"
|
||||
hd_dir = root / ".hd"
|
||||
notes_dir = hd_dir / "notes"
|
||||
notes_dir.mkdir(parents=True)
|
||||
|
||||
try:
|
||||
# --- set up a healthy note with a real cached copy + symlink ---
|
||||
note_text = "# Fox Hunt\nfrequencies here"
|
||||
(notes_dir / "id1.md").write_text(note_text)
|
||||
state = State()
|
||||
state.notes["id1"] = NoteState(
|
||||
title="Fox Hunt", tags=["radio"], symlinks={"radio": "Fox Hunt.md"},
|
||||
content_hash=content_hash(note_text),
|
||||
)
|
||||
radio_dir = root / "radio"
|
||||
radio_dir.mkdir(parents=True)
|
||||
(radio_dir / "Fox Hunt.md").symlink_to(Path("..") / ".hd" / "notes" / "id1.md")
|
||||
|
||||
problems = detect_problems(root, state)
|
||||
check("healthy symlink: no problems", problems == [], problems)
|
||||
|
||||
# --- nested_dir: a real directory where a tag symlink is expected ---
|
||||
(radio_dir / "subproject").mkdir()
|
||||
problems = detect_problems(root, state)
|
||||
kinds = {p.kind for p in problems}
|
||||
check("nested directory flagged", "nested_dir" in kinds, problems)
|
||||
nested = next(p for p in problems if p.kind == "nested_dir")
|
||||
check("nested_dir path is correct", nested.path == "radio/subproject", nested.path)
|
||||
shutil.rmtree(radio_dir / "subproject")
|
||||
|
||||
# --- stray_file: cp without -P, content matches a known note ---
|
||||
(radio_dir / "Accidental Copy.md").write_text(note_text) # same content as id1
|
||||
problems = detect_problems(root, state)
|
||||
stray = next((p for p in problems if p.kind == "stray_file"), None)
|
||||
check("stray file detected", stray is not None, problems)
|
||||
check("stray file identified via checksum match", "id1" in (stray.detail if stray else ""),
|
||||
stray.detail if stray else None)
|
||||
check("stray file suggests a fix command", "hdc tag" in (stray.detail if stray else ""),
|
||||
stray.detail if stray else None)
|
||||
|
||||
# --- stray file with NO checksum match: says so plainly, no guess ---
|
||||
(radio_dir / "Accidental Copy.md").unlink()
|
||||
(radio_dir / "Unknown Content.md").write_text("this matches no cached note at all")
|
||||
problems = detect_problems(root, state)
|
||||
unknown_stray = next(p for p in problems if p.kind == "stray_file" and p.path.endswith("Unknown Content.md"))
|
||||
check("unmatched stray file has no fabricated suggestion", unknown_stray.detail == "", unknown_stray.detail)
|
||||
(radio_dir / "Unknown Content.md").unlink()
|
||||
|
||||
# --- broken_symlink: target no longer exists ---
|
||||
(radio_dir / "Ghost.md").symlink_to(Path("..") / ".hd" / "notes" / "does-not-exist.md")
|
||||
problems = detect_problems(root, state)
|
||||
check("broken symlink flagged", any(p.kind == "broken_symlink" for p in problems), problems)
|
||||
(radio_dir / "Ghost.md").unlink()
|
||||
|
||||
# --- foreign_symlink: points somewhere outside .hd/notes/ entirely ---
|
||||
outside_target = tmpdir / "outside.md"
|
||||
outside_target.write_text("not managed by hd")
|
||||
(radio_dir / "Manual Link.md").symlink_to(outside_target)
|
||||
problems = detect_problems(root, state)
|
||||
check("foreign symlink flagged", any(p.kind == "foreign_symlink" for p in problems), problems)
|
||||
(radio_dir / "Manual Link.md").unlink()
|
||||
|
||||
# --- back to healthy: zero problems again ---
|
||||
problems = detect_problems(root, state)
|
||||
check("clean again after removing all the injected problems", problems == [], problems)
|
||||
|
||||
# --- save_problems / load_problems round-trip ---
|
||||
fake_problems = [Problem("stray_file", "radio/x.md", "content matches note 'id7'")]
|
||||
save_problems(hd_dir, fake_problems)
|
||||
check(".hd/problems.json was created", (hd_dir / "problems.json").exists())
|
||||
loaded = load_problems(hd_dir)
|
||||
check("round-tripped problem matches", loaded == fake_problems, loaded)
|
||||
check("Problem.__str__ produces a readable message",
|
||||
"content matches note 'id7'" in str(fake_problems[0]), str(fake_problems[0]))
|
||||
|
||||
# --- ReconcileLock actually provides mutual exclusion (real flock) ---
|
||||
lock_path = hd_dir / ".lock"
|
||||
held = threading.Event()
|
||||
release = threading.Event()
|
||||
|
||||
def holder():
|
||||
with ReconcileLock(hd_dir):
|
||||
held.set()
|
||||
release.wait(timeout=5)
|
||||
|
||||
t = threading.Thread(target=holder, daemon=True)
|
||||
t.start()
|
||||
held.wait(timeout=5)
|
||||
|
||||
# A second, independent file descriptor to the same lock file must
|
||||
# NOT be acquirable non-blocking while the thread above holds it.
|
||||
fd2 = os.open(lock_path, os.O_CREAT | os.O_RDWR, 0o600)
|
||||
try:
|
||||
fcntl.flock(fd2, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
check("flock actually blocks a concurrent holder", False, "acquired when it should have blocked")
|
||||
fcntl.flock(fd2, fcntl.LOCK_UN)
|
||||
except BlockingIOError:
|
||||
check("flock actually blocks a concurrent holder", True)
|
||||
finally:
|
||||
os.close(fd2)
|
||||
|
||||
release.set()
|
||||
t.join(timeout=5)
|
||||
|
||||
# Now that the thread released it, a fresh acquire must succeed.
|
||||
fd3 = os.open(lock_path, os.O_CREAT | os.O_RDWR, 0o600)
|
||||
try:
|
||||
fcntl.flock(fd3, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
check("lock is acquirable again after release", True)
|
||||
fcntl.flock(fd3, fcntl.LOCK_UN)
|
||||
except BlockingIOError:
|
||||
check("lock is acquirable again after release", False, "still blocked")
|
||||
finally:
|
||||
os.close(fd3)
|
||||
|
||||
check("marker file is gone after the lock is released", not (hd_dir / ".reconciling.lock").exists())
|
||||
|
||||
finally:
|
||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||
|
||||
print(f"\n{len(PASS)} passed, {len(FAIL)} failed")
|
||||
return 0 if not FAIL else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user