first commit
This commit is contained in:
114
hd-joe/test_joe_wrapper.py
Normal file
114
hd-joe/test_joe_wrapper.py
Normal file
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Tests joe_wrapper.decide() -- pure decision logic, no process launching --
|
||||
against both of Joakim's stated rules:
|
||||
1. An existing hd-managed note routes to hd-joe regardless of cwd.
|
||||
2. A not-yet-existing name routes to hd-joe's create-or-open ONLY when
|
||||
cwd itself is inside an hd-managed folder.
|
||||
"""
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "hd_common"))
|
||||
from hd_core import Config, Cookies
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
from joe_wrapper import decide
|
||||
|
||||
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="joe-wrapper-test-"))
|
||||
root = tmpdir / "notes"
|
||||
hd_dir = root / ".hd"
|
||||
(hd_dir / "notes").mkdir(parents=True)
|
||||
Config(url="https://hedgedoc.example", email=None).save(hd_dir)
|
||||
Cookies(header="session=abc").save(hd_dir)
|
||||
|
||||
note_id = "n1"
|
||||
(hd_dir / "notes" / f"{note_id}.md").write_text("---\ntitle: Masten\n---\n\nbody")
|
||||
tag_dir = root / "sl6cv"
|
||||
tag_dir.mkdir(parents=True)
|
||||
(tag_dir / "Masten").symlink_to(Path("..") / ".hd" / "notes" / f"{note_id}.md")
|
||||
|
||||
elsewhere = tmpdir / "elsewhere"
|
||||
elsewhere.mkdir()
|
||||
|
||||
unrelated_file = elsewhere / "scratch.txt"
|
||||
unrelated_file.write_text("just a normal file")
|
||||
|
||||
try:
|
||||
# --- Rule 1: an existing hd-managed note routes to hd-joe, from ANY cwd ---
|
||||
d = decide(["Masten"], cwd=tag_dir)
|
||||
check("existing note, cwd=tag dir -> hd_joe", d.program == "hd_joe", d)
|
||||
check("existing note -> correct url", d.args[0] == "https://hedgedoc.example", d)
|
||||
check("existing note -> correct note id", d.args[1] == note_id, d)
|
||||
check("existing note -> cookie passed explicitly (cwd-based discovery won't help elsewhere)",
|
||||
"--cookie" in d.args and "session=abc" in d.args, d)
|
||||
|
||||
d2 = decide([str(tag_dir / "Masten")], cwd=elsewhere)
|
||||
check("existing note opened by FULL PATH from an unrelated cwd -> still hd_joe",
|
||||
d2.program == "hd_joe", d2)
|
||||
check("full-path case -> same note id", d2.args[1] == note_id, d2)
|
||||
|
||||
d3 = decide([str(tag_dir / "Masten")], cwd=Path("/"))
|
||||
check("existing note opened from filesystem root -> still hd_joe", d3.program == "hd_joe", d3)
|
||||
|
||||
# --- Rule 2: a NOT-YET-EXISTING name -> hd_joe only if cwd is hd-managed ---
|
||||
d4 = decide(["Brand New Note"], cwd=tag_dir)
|
||||
check("new name, cwd IS hd-managed -> hd_joe (create-or-open)", d4.program == "hd_joe", d4)
|
||||
check("new name -> just the bare name passed through", d4.args == ["Brand New Note"], d4)
|
||||
|
||||
d5 = decide(["Brand New Note"], cwd=elsewhere)
|
||||
check("new name, cwd NOT hd-managed -> real_joe (plain new file)", d5.program == "real_joe", d5)
|
||||
check("real_joe case preserves the original arg", d5.args == ["Brand New Note"], d5)
|
||||
|
||||
# --- An existing file that's a normal, non-hd file -> always real joe ---
|
||||
d6 = decide([str(unrelated_file)], cwd=elsewhere)
|
||||
check("existing unrelated file -> real_joe, never treated as a note", d6.program == "real_joe", d6)
|
||||
|
||||
d7 = decide([str(unrelated_file)], cwd=tag_dir) # even from inside an hd-managed cwd
|
||||
check("existing unrelated file, cwd IS hd-managed -> still real_joe (it's not an hd note)",
|
||||
d7.program == "real_joe", d7)
|
||||
|
||||
# --- Multiple args, or any flag present -> always real joe, untouched ---
|
||||
d8 = decide(["fileA", "fileB"], cwd=tag_dir)
|
||||
check("two files -> real_joe untouched", d8.program == "real_joe" and d8.args == ["fileA", "fileB"], d8)
|
||||
|
||||
d9 = decide(["-wordwrap", "Masten"], cwd=tag_dir)
|
||||
check("a flag present -> real_joe untouched, no second-guessing",
|
||||
d9.program == "real_joe" and d9.args == ["-wordwrap", "Masten"], d9)
|
||||
|
||||
d10 = decide([], cwd=tag_dir)
|
||||
check("no args at all -> real_joe untouched", d10.program == "real_joe" and d10.args == [], d10)
|
||||
|
||||
# --- A dangling symlink pointing INTO .hd/notes/ at a nonexistent id:
|
||||
# path.exists() is False for a broken symlink -- falls through to
|
||||
# rule 2's "doesn't exist yet" branch, not treated as note 1. ---
|
||||
(tag_dir / "Broken").symlink_to(hd_dir / "notes" / "ghost.md")
|
||||
d11 = decide(["Broken"], cwd=tag_dir)
|
||||
check("broken symlink into .hd/notes/ -- cwd is hd-managed, so still hd_joe (create-or-open)",
|
||||
d11.program == "hd_joe", d11)
|
||||
check("broken-symlink case treats it as a plain name, not a resolved note",
|
||||
d11.args == ["Broken"], d11)
|
||||
|
||||
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