from pathlib import Path from backend.app.utils.paths import ( derive_subdir, derive_top_level_artist, filehash_from_url, hash_suffixed_name, ) IMPORT = Path("/import") def test_filehash_from_url_extracts_lowercased_md5(): url = "https://c10.patreonusercontent.com/4/patreon-media/p/post/1/AbC123DeF456789012345678901234EF/1/img.png?token=x" assert filehash_from_url(url) == "abc123def456789012345678901234ef" def test_filehash_from_url_none_and_no_hash(): assert filehash_from_url(None) is None assert filehash_from_url("") is None assert filehash_from_url("https://example.test/a.png") is None def test_filehash_from_url_query_unescaping_irrelevant(): # The hash sits in the path before the query, so entity-escaped ampersands # in the query never affect the match (render-time matches escaped srcs). base = "https://cdn.test/p/0123456789abcdef0123456789ABCDEF/x.jpg" assert filehash_from_url(base + "?a=1&b=2") == "0123456789abcdef0123456789abcdef" def test_derive_subdir_nested(): assert derive_subdir(Path("/import/Alice/sub/x.png"), IMPORT) == "Alice/sub" def test_derive_subdir_one_level(): assert derive_subdir(Path("/import/Alice/x.png"), IMPORT) == "Alice" def test_derive_subdir_root(): assert derive_subdir(Path("/import/x.png"), IMPORT) == "" def test_derive_subdir_outside_root(): assert derive_subdir(Path("/elsewhere/x.png"), IMPORT) == "" def test_hash_suffixed_name(): h = "abcdef1234567890" + "0" * 48 assert hash_suffixed_name("photo", h, ".png") == "photo__abcdef1234.png" def test_derive_top_level_artist_present(): assert derive_top_level_artist(Path("/import/Alice/x.png"), IMPORT) == "Alice" def test_derive_top_level_artist_nested(): assert derive_top_level_artist(Path("/import/Alice/sub/x.png"), IMPORT) == "Alice" def test_derive_top_level_artist_root(): assert derive_top_level_artist(Path("/import/x.png"), IMPORT) is None