import json from unittest.mock import MagicMock, patch from flask.ctx import AppContext from flask.testing import FlaskClient from assets.constants import asset_types, authorization from tests.unit.models import au_operations @patch("assets.utils.handlers.g") def test_get_stereo_for_asset_returns_latest_correction_stereo_flac( mock_g: MagicMock, db_fixture: None, fixture_client: FlaskClient, fixture_app: AppContext, ) -> None: mock_g.request_context.jwt_identity_id = ( authorization.LAMBDA_ASSETS_SPATIAL_AUDIO_VALIDATION_UUID ) atmos_asset_upload_id = 1001 other_track_stereo_id = 1002 original_stereo_id = 1003 correction_stereo_id = 1004 queried_track_id = 7788 other_track_id = 9999 asset_upload_filename = "atmos_file" correction_bucket = "prod-encoded-assets" correction_flac_filename = "correction_stereo.flac" au_operations.seed_asset_upload_table( [ { "id": atmos_asset_upload_id, "user_id": "oa:123456", "asset_upload_type_id": 3, "filename": asset_upload_filename, "api_version": 2, "track_unique_id": queried_track_id, "deleted": False, "is_correction": False, }, # Stereo on a DIFFERENT track — must not be returned. Catches a # regression where the track_unique_id filter is dropped or wrong. { "id": other_track_stereo_id, "user_id": "oa:123456", "asset_upload_type_id": 1, "filename": "other_track_stereo", "api_version": 2, "track_unique_id": other_track_id, "deleted": False, "is_correction": False, }, # Original stereo on the queried track — must NOT be returned in # correction-mode lookup, even though it lives on the right track. { "id": original_stereo_id, "user_id": "oa:123456", "asset_upload_type_id": 1, "filename": "original_stereo", "api_version": 2, "track_unique_id": queried_track_id, "deleted": False, "is_correction": False, }, # Correction stereo on the queried track with a higher id — # MUST be the one returned. Catches a regression where # is_correction_mode=True is flipped to False (which would filter # corrections out and surface the original stereo instead). { "id": correction_stereo_id, "user_id": "oa:123456", "asset_upload_type_id": 1, "filename": "correction_stereo", "api_version": 2, "track_unique_id": queried_track_id, "deleted": False, "is_correction": True, }, ] ) au_operations.seed_asset_final_table( [ { "id": 2002, "asset_upload_id": other_track_stereo_id, "asset_type": asset_types.TYPE_FILE_FLAC, "asset_subtype": "none", "bucket": "wrong-bucket", "filename": "wrong_track.flac", }, { "id": 2003, "asset_upload_id": original_stereo_id, "asset_type": asset_types.TYPE_FILE_FLAC, "asset_subtype": "none", "bucket": "wrong-bucket", "filename": "original_stereo.flac", }, { "id": 2004, "asset_upload_id": correction_stereo_id, "asset_type": asset_types.TYPE_FILE_FLAC, "asset_subtype": "none", "bucket": correction_bucket, "filename": correction_flac_filename, }, ] ) result = fixture_client.get(f"/internal/assets/{asset_upload_filename}.wav/stereo") assert result.status_code == 200 assert json.loads(result.data.decode()) == { "bucket": correction_bucket, "key": correction_flac_filename, }