"""Tests for hooks/_common.py — read_referenced_file and scan_attribution.""" import sys from pathlib import Path HOOKS_DIR = Path(__file__).resolve().parent.parent if str(HOOKS_DIR) not in sys.path: sys.path.insert(0, str(HOOKS_DIR)) import _common # noqa: E402 # --- read_referenced_file: normal cases ------------------------------------ def test_read_referenced_file_returns_content(tmp_path): f = tmp_path / "msg.txt" f.write_text("hello world\n") assert _common.read_referenced_file(str(f)) == "hello world\n" def test_read_referenced_file_expands_user(tmp_path, monkeypatch): # Point HOME at tmp_path so ~/msg.txt resolves under it. monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.setattr(Path, "home", lambda: tmp_path, raising=False) f = tmp_path / "msg.txt" f.write_text("tilde body") assert _common.read_referenced_file("~/msg.txt") == "tilde body" # --- read_referenced_file: error / boundary cases -------------------------- def test_read_referenced_file_missing_returns_none(tmp_path): assert _common.read_referenced_file(str(tmp_path / "nope.txt")) is None def test_read_referenced_file_directory_returns_none(tmp_path): # A directory is not a regular file. assert _common.read_referenced_file(str(tmp_path)) is None def test_read_referenced_file_oversized_returns_none(tmp_path): f = tmp_path / "big.txt" f.write_text("x" * 50) assert _common.read_referenced_file(str(f), max_bytes=10) is None def test_read_referenced_file_at_limit_is_read(tmp_path): f = tmp_path / "edge.txt" f.write_text("12345") # exactly 5 bytes assert _common.read_referenced_file(str(f), max_bytes=5) == "12345" def test_read_referenced_file_invalid_utf8_returns_none(tmp_path): f = tmp_path / "bin.dat" f.write_bytes(b"\xff\xfe\x00bad") assert _common.read_referenced_file(str(f)) is None def test_read_referenced_file_empty_string_path_returns_none(): assert _common.read_referenced_file("") is None # --- scan_attribution sanity (relied on by the file-backed tests) ---------- def test_scan_attribution_catches_coauthor(): assert _common.scan_attribution("Co-Authored-By: Claude") def test_scan_attribution_catches_robot_emoji(): assert _common.scan_attribution("nice work \U0001F916") def test_scan_attribution_clean_text_empty(): assert _common.scan_attribution("fix: tidy up the parser") == []