Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/chat/message_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ def _move_no_clobber(source: str, candidate: str) -> Optional[str]:
numbered candidate. Returns the final path, or None when the move was not
possible (e.g. filesystems without hard link support).
"""
root, ext = os.path.splitext(candidate)
counter = 1
while True:
try:
os.link(source, candidate)
break
except FileExistsError:
root, ext = os.path.splitext(candidate)
candidate = f"{root}_{counter}{ext}"
counter += 1
except OSError:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_message_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from core.chat.message_elements import (
_build_temp_file_path,
_infer_mime_from_bytes,
_move_no_clobber,
check_base64,
ElementType,
Text,
Expand All @@ -25,6 +26,24 @@
)


# ── _move_no_clobber ─────────────────────────────────────────────

def test_move_no_clobber_increments_from_original_candidate(tmp_path):
source = tmp_path / "source"
source.write_bytes(b"new")
candidate = tmp_path / "download.png"
candidate.write_bytes(b"original")
(tmp_path / "download_1.png").write_bytes(b"first duplicate")

result = _move_no_clobber(str(source), str(candidate))

assert result == str(tmp_path / "download_2.png")
assert not source.exists()
assert candidate.read_bytes() == b"original"
assert (tmp_path / "download_1.png").read_bytes() == b"first duplicate"
assert (tmp_path / "download_2.png").read_bytes() == b"new"


# ── _infer_mime_from_bytes ──────────────────────────────────────────

def test_infer_png():
Expand Down
Loading