from pathlib import Path from app.config import settings from app.core.render_paths import ( ensure_group_writable_dir, resolve_public_asset_url, resolve_result_path, result_path_to_storage_key, result_path_to_public_url, ) def test_result_path_to_public_url_for_canonical_render(monkeypatch, tmp_path): upload_dir = tmp_path / "uploads" render_file = upload_dir / "renders" / "line-1" / "bearing.png" render_file.parent.mkdir(parents=True, exist_ok=True) render_file.write_bytes(b"png") monkeypatch.setattr(settings, "upload_dir", str(upload_dir)) result_path = str(render_file) assert result_path_to_public_url(result_path, require_exists=True) == "/renders/line-1/bearing.png" assert resolve_result_path(result_path) == render_file assert resolve_public_asset_url("/renders/line-1/bearing.png") == render_file def test_result_path_to_public_url_for_legacy_shared_render(monkeypatch, tmp_path): upload_dir = tmp_path / "uploads" render_file = upload_dir / "renders" / "line-2" / "legacy.png" render_file.parent.mkdir(parents=True, exist_ok=True) render_file.write_bytes(b"png") monkeypatch.setattr(settings, "upload_dir", str(upload_dir)) legacy_path = "/shared/renders/line-2/legacy.png" assert resolve_result_path(legacy_path) == render_file assert result_path_to_public_url(legacy_path, require_exists=True) == "/renders/line-2/legacy.png" def test_result_path_to_public_url_hides_missing_or_non_public_paths(monkeypatch, tmp_path): upload_dir = tmp_path / "uploads" step_render = upload_dir / "step_files" / "renders" / "line_123.png" step_render.parent.mkdir(parents=True, exist_ok=True) step_render.write_bytes(b"png") monkeypatch.setattr(settings, "upload_dir", str(upload_dir)) missing_public = str(upload_dir / "renders" / "line-3" / "missing.png") assert result_path_to_public_url(str(step_render), require_exists=True) is None assert result_path_to_public_url(missing_public, require_exists=True) is None def test_result_path_to_storage_key_normalizes_legacy_and_public_variants(monkeypatch, tmp_path): upload_dir = tmp_path / "uploads" render_file = upload_dir / "renders" / "line-4" / "normalized.png" render_file.parent.mkdir(parents=True, exist_ok=True) render_file.write_bytes(b"png") monkeypatch.setattr(settings, "upload_dir", str(upload_dir)) assert result_path_to_storage_key(str(render_file)) == "renders/line-4/normalized.png" assert result_path_to_storage_key("/shared/renders/line-4/normalized.png") == "renders/line-4/normalized.png" assert result_path_to_storage_key("/renders/line-4/normalized.png") == "renders/line-4/normalized.png" def test_ensure_group_writable_dir_normalizes_existing_upload_tree(monkeypatch, tmp_path): upload_dir = tmp_path / "uploads" target = upload_dir / "step_files" / "renders" target.mkdir(parents=True, exist_ok=True) upload_dir.chmod(0o755) (upload_dir / "step_files").chmod(0o755) target.chmod(0o755) monkeypatch.setattr(settings, "upload_dir", str(upload_dir)) ensured = ensure_group_writable_dir(target) assert ensured == target for path in (upload_dir, upload_dir / "step_files", target): mode = path.stat().st_mode & 0o7777 assert mode & 0o020 assert mode & 0o010 assert mode & 0o2000