clipwright/tests/test_filename_limits.py
juancarlos.cavero bedecdc850 fix(files): bound filenames by bytes, not characters
/api/hook has been returning 500 in prod on any clip whose video title is
non-Latin: OSError 36, "File name too long". sanitize_filename() capped the
title at 100 CHARACTERS, but filesystems cap a name at 255 BYTES, and one
Bengali or Arabic character costs three — so a title was already ~300 bytes
before the pipeline decorated it.

Every derived name stacks on that stem: "_clip_10.mp4", "subtitled_<ts>_",
"temp_hook_<hex8>_", "autosubs_<ts>_.ass". Shipping captions by default added
one more layer, which is what pushed the borderline cases over and surfaced it.

- sanitize_filename now trims to MAX_TITLE_BYTES (120) via truncate_bytes(),
  which never splits a multi-byte character. 120 leaves room for every
  decoration stacked and still lands well under 255.
- The hook overlay stopped embedding the full source name: its uuid already
  guarantees uniqueness, so the name rides along only as a debugging hint and
  is trimmed to 80 bytes.

Deliberately NOT trimmed: the auto-caption output, which must stay exactly
"subtitled_<ts>_<clip filename>" because the subtitle modal's walk-back and
_canonical_clip_file both reconstruct the clean original from it. Length is
bounded upstream instead; a legacy clip that predates the budget raises
OSError 36, which the existing guard turns into "ship it uncaptioned".
2026-07-26 20:59:04 +02:00

64 lines
2.7 KiB
Python

"""Filenames must fit the filesystem, which counts BYTES, not characters.
Regression cover for OSError 36 ("File name too long") killing /api/hook in
prod on 26-jul-2026: a Bengali video title passed the 100-CHARACTER cap while
already being ~300 bytes, and the derived temp name pushed it further.
"""
import pytest
main = pytest.importorskip("main") # cv2 etc., absent in minimal CI
hooks = pytest.importorskip("hooks")
# ext4/APFS cap a single name at 255 bytes.
FS_LIMIT = 255
BENGALI = "মজার_ধাঁধা_শুনুন_আর_হাসুন_সকাল_হলেই_কার_মাথা_হারিয়ে_যায়কমেডি_ভিডিও"
class TestTruncateBytes:
def test_short_text_untouched(self):
assert main.truncate_bytes("hello", 100) == "hello"
def test_trims_to_the_byte_budget(self):
out = main.truncate_bytes(BENGALI * 5, 120)
assert len(out.encode("utf-8")) <= 120
def test_never_splits_a_multibyte_character(self):
# Budget deliberately lands mid-character.
for budget in range(1, 40):
out = main.truncate_bytes(BENGALI, budget)
out.encode("utf-8").decode("utf-8") # must not raise
assert len(out.encode("utf-8")) <= budget
def test_ascii_is_unaffected_by_the_byte_rule(self):
text = "a" * 200
assert main.truncate_bytes(text, 120) == "a" * 120
class TestSanitizeFilename:
def test_non_latin_title_fits_the_budget(self):
out = main.sanitize_filename(BENGALI * 4)
assert len(out.encode("utf-8")) <= main.MAX_TITLE_BYTES
def test_leaves_room_for_every_decoration_the_pipeline_adds(self):
# Worst case actually produced: a hook overlay built on top of an
# auto-captioned clip of a long-titled video.
stem = main.sanitize_filename(BENGALI * 4)
clip = f"{stem}_clip_10.mp4"
captioned = f"subtitled_1785060039_{clip}"
hook_png = f"temp_hook_abcd1234_{captioned}.png"
assert len(hook_png.encode("utf-8")) < FS_LIMIT, len(hook_png.encode("utf-8"))
def test_still_strips_unsafe_characters(self):
assert main.sanitize_filename('a<b>c:d"e/f\\g|h?i*j#k') == "abcdefghijk"
assert main.sanitize_filename("with spaces") == "with_spaces"
def test_the_old_character_cap_would_have_overflowed(self):
# Documents why the rule changed: 100 chars of Bengali is ~300 bytes.
assert len((BENGALI * 4)[:100].encode("utf-8")) > FS_LIMIT
class TestHookTempName:
def test_hook_helper_trims_by_bytes(self):
out = hooks._truncate_bytes(BENGALI * 5, 80)
assert len(out.encode("utf-8")) <= 80
out.encode("utf-8").decode("utf-8")