aboutsummaryrefslogtreecommitdiff
path: root/hooks/tests/test_common.py
blob: 10e60974fbc54a4f50a5ededa9d887def8debb9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""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") == []