File size: 6,965 Bytes
b5b9c2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Tests for _detect_file_drop — file path detection that prevents
dragged/pasted absolute paths from being mistaken for slash commands."""

import os
import tempfile
from pathlib import Path

import pytest

from cli import _detect_file_drop


# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------

@pytest.fixture()
def tmp_image(tmp_path):
    """Create a temporary .png file and return its path."""
    img = tmp_path / "screenshot.png"
    img.write_bytes(b"\x89PNG\r\n\x1a\n")  # minimal PNG header
    return img


@pytest.fixture()
def tmp_text(tmp_path):
    """Create a temporary .py file and return its path."""
    f = tmp_path / "main.py"
    f.write_text("print('hello')\n")
    return f


@pytest.fixture()
def tmp_image_with_spaces(tmp_path):
    """Create a file whose name contains spaces (like macOS screenshots)."""
    img = tmp_path / "Screenshot 2026-04-01 at 7.25.32 PM.png"
    img.write_bytes(b"\x89PNG\r\n\x1a\n")
    return img


# ---------------------------------------------------------------------------
# Tests: returns None for non-file inputs
# ---------------------------------------------------------------------------

class TestNonFileInputs:
    def test_regular_slash_command(self):
        assert _detect_file_drop("/help") is None

    def test_unknown_slash_command(self):
        assert _detect_file_drop("/xyz") is None

    def test_slash_command_with_args(self):
        assert _detect_file_drop("/config set key value") is None

    def test_empty_string(self):
        assert _detect_file_drop("") is None

    def test_non_slash_input(self):
        assert _detect_file_drop("hello world") is None

    def test_non_string_input(self):
        assert _detect_file_drop(42) is None

    def test_nonexistent_path(self):
        assert _detect_file_drop("/nonexistent/path/to/file.png") is None

    def test_directory_not_file(self, tmp_path):
        """A directory path should not be treated as a file drop."""
        assert _detect_file_drop(str(tmp_path)) is None


# ---------------------------------------------------------------------------
# Tests: image file detection
# ---------------------------------------------------------------------------

class TestImageFileDrop:
    def test_simple_image_path(self, tmp_image):
        result = _detect_file_drop(str(tmp_image))
        assert result is not None
        assert result["path"] == tmp_image
        assert result["is_image"] is True
        assert result["remainder"] == ""

    def test_image_with_trailing_text(self, tmp_image):
        user_input = f"{tmp_image} analyze this please"
        result = _detect_file_drop(user_input)
        assert result is not None
        assert result["path"] == tmp_image
        assert result["is_image"] is True
        assert result["remainder"] == "analyze this please"

    @pytest.mark.parametrize("ext", [".png", ".jpg", ".jpeg", ".gif", ".webp",
                                      ".bmp", ".tiff", ".tif", ".svg", ".ico"])
    def test_all_image_extensions(self, tmp_path, ext):
        img = tmp_path / f"test{ext}"
        img.write_bytes(b"fake")
        result = _detect_file_drop(str(img))
        assert result is not None
        assert result["is_image"] is True

    def test_uppercase_extension(self, tmp_path):
        img = tmp_path / "photo.JPG"
        img.write_bytes(b"fake")
        result = _detect_file_drop(str(img))
        assert result is not None
        assert result["is_image"] is True


# ---------------------------------------------------------------------------
# Tests: non-image file detection
# ---------------------------------------------------------------------------

class TestNonImageFileDrop:
    def test_python_file(self, tmp_text):
        result = _detect_file_drop(str(tmp_text))
        assert result is not None
        assert result["path"] == tmp_text
        assert result["is_image"] is False
        assert result["remainder"] == ""

    def test_non_image_with_trailing_text(self, tmp_text):
        user_input = f"{tmp_text} review this code"
        result = _detect_file_drop(user_input)
        assert result is not None
        assert result["is_image"] is False
        assert result["remainder"] == "review this code"


# ---------------------------------------------------------------------------
# Tests: backslash-escaped spaces (macOS drag-and-drop)
# ---------------------------------------------------------------------------

class TestEscapedSpaces:
    def test_escaped_spaces_in_path(self, tmp_image_with_spaces):
        r"""macOS drags produce paths like /path/to/my\ file.png"""
        escaped = str(tmp_image_with_spaces).replace(' ', '\\ ')
        result = _detect_file_drop(escaped)
        assert result is not None
        assert result["path"] == tmp_image_with_spaces
        assert result["is_image"] is True

    def test_escaped_spaces_with_trailing_text(self, tmp_image_with_spaces):
        escaped = str(tmp_image_with_spaces).replace(' ', '\\ ')
        user_input = f"{escaped} what is this?"
        result = _detect_file_drop(user_input)
        assert result is not None
        assert result["path"] == tmp_image_with_spaces
        assert result["remainder"] == "what is this?"

    def test_tilde_prefixed_path(self, tmp_path, monkeypatch):
        home = tmp_path / "home"
        img = home / "storage" / "shared" / "Pictures" / "cat.png"
        img.parent.mkdir(parents=True, exist_ok=True)
        img.write_bytes(b"\x89PNG\r\n\x1a\n")
        monkeypatch.setenv("HOME", str(home))

        result = _detect_file_drop("~/storage/shared/Pictures/cat.png what is this?")

        assert result is not None
        assert result["path"] == img
        assert result["is_image"] is True
        assert result["remainder"] == "what is this?"


# ---------------------------------------------------------------------------
# Tests: edge cases
# ---------------------------------------------------------------------------

class TestEdgeCases:
    def test_path_with_no_extension(self, tmp_path):
        f = tmp_path / "Makefile"
        f.write_text("all:\n\techo hi\n")
        result = _detect_file_drop(str(f))
        assert result is not None
        assert result["is_image"] is False

    def test_path_that_looks_like_command_but_is_file(self, tmp_path):
        """A file literally named 'help' inside a directory starting with /."""
        f = tmp_path / "help"
        f.write_text("not a command\n")
        result = _detect_file_drop(str(f))
        assert result is not None
        assert result["is_image"] is False

    def test_symlink_to_file(self, tmp_image, tmp_path):
        link = tmp_path / "link.png"
        link.symlink_to(tmp_image)
        result = _detect_file_drop(str(link))
        assert result is not None
        assert result["is_image"] is True