Files
karaoke-gen/tests/integration/test_cli_edit_lyrics.py
T

230 lines
13 KiB
Python

import os
import pytest
import sys
import json
from karaoke_gen.karaoke_gen import KaraokePrep
from karaoke_gen.file_handler import FileHandler
from unittest.mock import MagicMock, call, patch, AsyncMock, ANY
from karaoke_gen.utils.gen_cli import async_main
from karaoke_gen.karaoke_finalise.karaoke_finalise import KaraokeFinalise
# Register the asyncio marker
pytest.mark.asyncio = pytest.mark.asyncio
# Set the default event loop policy to 'auto'
pytest_configure = lambda config: config.addinivalue_line("markers", "asyncio: mark test as async")
@pytest.mark.asyncio
@pytest.mark.integration
async def test_cli_edit_lyrics_integration(mocker): # Remove tmp_path
"""Tests the --edit-lyrics CLI workflow using high-level mocks."""
# --- 1. Define Test Variables ---
artist = "Mock Edit Artist"
title = "Mock Edit Title"
brand_prefix = "EDIT"
brand_code = f"{brand_prefix}-9998" # Use a distinct code
existing_track_dir_name = f"{brand_code} - {artist} - {title}"
mock_track_dir_path = f"/fake/path/to/{existing_track_dir_name}" # Mock path
# --- 2. Mock Core Components and External Interactions ---
# Mock KaraokePrep methods
# Mock extract_artist_title_from_dir_name to avoid needing real directory
# mocker.patch(\'karaoke_gen.karaoke_gen.KaraokePrep.extract_artist_title_from_dir_name\', return_value=(artist, title)) # REMOVED - Logic is in gen_cli
# Mock the process method entirely or spy on __init__ and process
prep_init_spy = mocker.spy(KaraokePrep, '__init__')
# Mock the process method to avoid its internal logic, return a dummy result
mock_prep_result = {
'artist': artist,
'title': title,
'track_output_dir': mock_track_dir_path,
'lyrics_results': { # Include dummy lyrics results needed by finalise path
'corrected_lyrics_text_filepath': f'{mock_track_dir_path}/{artist} - {title} (Lyrics Corrected).txt',
'lrc_file': f'{mock_track_dir_path}/{artist} - {title} (Karaoke).lrc',
'ass_file': f'{mock_track_dir_path}/{artist} - {title} (Karaoke).ass',
},
'input_audio_wav': f'{mock_track_dir_path}/{artist} - {title} (Original).wav', # Dummy path
'base_name': f'{artist} - {title}',
'input_media_path': f'{mock_track_dir_path}/{artist} - {title} (Original).wav',
# Add other keys if KaraokeFinalise strictly requires them
}
prep_process_mock = mocker.patch.object(KaraokePrep, 'process', new_callable=AsyncMock, return_value=[mock_prep_result]) # Return list
# Mock FileHandler backup method - Use patch.object instead of spy
# backup_spy = mocker.spy(FileHandler, \'backup_existing_outputs\')
mock_backup = mocker.patch.object(FileHandler, 'backup_existing_outputs', return_value=f'{mock_track_dir_path}/{artist} - {title} (Original).wav')
# Mock LyricsProcessor (part of KaraokePrep, but good to ensure it\'s called)
# Mock transcribe_lyrics to return dummy data needed by KaraokePrep mock result
# mock_transcribe = mocker.patch( # REMOVED - Not needed as KaraokePrep.process is mocked
# \'karaoke_gen.lyrics_processor.LyricsProcessor.transcribe_lyrics\',
# new_callable=AsyncMock, # Make it async
# return_value={
# \'corrected_lyrics_text\': \'Edited lyrics line 1\\nEdited lyrics line 2\',
# \'corrected_lyrics_text_filepath\': f\'{mock_track_dir_path}/{artist} - {title} (Lyrics Corrected).txt\',
# \'lyrics_output_dir\': f\'{mock_track_dir_path}/lyrics\',
# \'lrc_file\': f\'{mock_track_dir_path}/{artist} - {title} (Karaoke).lrc\',
# \'ass_file\': f\'{mock_track_dir_path}/{artist} - {title} (Karaoke).ass\',
# }
# )
# Mock KaraokeFinalise methods
# Mock detect_best_aac_codec called during __init__ to avoid os.popen
mocker.patch('karaoke_gen.karaoke_finalise.karaoke_finalise.KaraokeFinalise.detect_best_aac_codec', return_value='aac_at')
finalise_init_spy = mocker.spy(KaraokeFinalise, '__init__')
# Mock the process method to avoid its internal logic, provide a more complete return dict
mock_finalise_result = {
'artist': artist,
'title': title,
'video_with_vocals': f'{mock_track_dir_path}/{artist} - {title} (With Vocals).mkv',
'video_with_instrumental': f'{mock_track_dir_path}/{artist} - {title} (Karaoke).mp4',
'final_video': f'{mock_track_dir_path}/{artist} - {title} (Final Karaoke Lossless 4k).mp4',
'final_video_mkv': f'{mock_track_dir_path}/{artist} - {title} (Final Karaoke Lossless 4k).mkv',
'final_video_lossy': f'{mock_track_dir_path}/{artist} - {title} (Final Karaoke Lossy 4k).mp4',
'final_video_720p': f'{mock_track_dir_path}/{artist} - {title} (Final Karaoke Lossy 720p).mp4',
'final_karaoke_cdg_zip': f'{mock_track_dir_path}/{artist} - {title} (Final Karaoke CDG).zip',
'final_karaoke_txt_zip': f'{mock_track_dir_path}/{artist} - {title} (Final Karaoke TXT).zip',
'youtube_url': 'https://youtu.be/mock_replaced_id',
'dropbox_url': 'https://fake.sharing.link/mock_edit_folder', # Keep for potential future use
'brand_code': brand_code, # Important for edit mode
'new_brand_code_dir_path': mock_track_dir_path, # In edit mode, dir path doesn't change
'brand_code_dir_sharing_link': 'https://fake.sharing.link/mock_edit_folder'
}
finalise_process_mock = mocker.patch.object(KaraokeFinalise, 'process', return_value=mock_finalise_result)
# Mock necessary imports or high-level functions causing issues
mocker.patch('importlib.metadata.version', return_value="0.0.0-mock") # Prevent email.parser error
# Mock file system interactions needed by the CLI/core logic *before* mocks take over
mocker.patch('os.getcwd', return_value=mock_track_dir_path)
mocker.patch('os.path.isdir', return_value=True) # Assume dirs exist
mocker.patch('os.path.isfile', return_value=True) # Assume files exist (e.g., configs)
mocker.patch('os.path.exists', return_value=True) # Assume paths exist
mocker.patch('os.listdir', return_value=[f"{artist} - {title} (With Vocals).mkv"]) # Make find_with_vocals_file happy
# Use mocker.mock_open to simulate reading empty bytes (enough for gettext magic number)
mock_opener = mocker.mock_open(read_data=b'\x00\x00\x00\x00')
mocker.patch('builtins.open', mock_opener)
mocker.patch('io.open', mock_opener)
mocker.patch('json.load', return_value={}) # Prevent reading JSON config
mocker.patch('json.loads', return_value={"cdg": {}}) # Prevent decoding JSON string
mocker.patch('json.dump', MagicMock()) # Prevent writing JSON
# Mock shutil operations
mocker.patch('shutil.move', MagicMock())
mocker.patch('shutil.copytree', MagicMock())
mocker.patch('shutil.rmtree', MagicMock())
mocker.patch('shutil.copy2', MagicMock())
# Mock os operations
mocker.patch('os.makedirs', MagicMock())
mocker.patch('os.remove', MagicMock())
mocker.patch('os.rename', MagicMock())
mocker.patch('os.system', MagicMock(return_value=0)) # Return success
# Mock external calls
mocker.patch('subprocess.run', return_value=MagicMock(returncode=0, stdout="", stderr=""))
mocker.patch('requests.post', MagicMock())
mocker.patch('pyperclip.copy', MagicMock())
mocker.patch('googleapiclient.discovery.build', return_value=MagicMock())
mocker.patch('google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file', MagicMock())
# Mock specific finalise methods related to external APIs
mocker.patch('karaoke_gen.karaoke_finalise.karaoke_finalise.KaraokeFinalise.authenticate_youtube', return_value=MagicMock())
mocker.patch('karaoke_gen.karaoke_finalise.karaoke_finalise.KaraokeFinalise.check_if_video_title_exists_on_youtube_channel', return_value='existing_video_id') # Crucial for replace logic
mocker.patch('karaoke_gen.karaoke_finalise.karaoke_finalise.KaraokeFinalise.upload_final_mp4_to_youtube_with_title_thumbnail', MagicMock())
mocker.patch('karaoke_gen.karaoke_finalise.karaoke_finalise.KaraokeFinalise.authenticate_gmail', return_value=MagicMock())
mocker.patch('karaoke_gen.karaoke_finalise.karaoke_finalise.KaraokeFinalise.draft_completion_email', MagicMock())
mocker.patch('karaoke_gen.karaoke_finalise.karaoke_finalise.KaraokeFinalise.post_discord_notification', MagicMock())
mocker.patch('karaoke_gen.karaoke_finalise.karaoke_finalise.KaraokeFinalise.sync_public_share_dir_to_rclone_destination', MagicMock())
mocker.patch('karaoke_gen.karaoke_finalise.karaoke_finalise.KaraokeFinalise.generate_organised_folder_sharing_link', return_value="https://fake.sharing.link/mock_edit_folder")
# Mock pydub
mocker.patch('pydub.AudioSegment.from_file', return_value=MagicMock(duration_seconds=10)) # Need duration
mocker.patch('pydub.audio_segment.mediainfo_json', return_value={"format": {"duration": "10.0"}})
# Mock lyrics review server
# mocker.patch(\'lyrics_transcriber.review.server.ReviewServer.start\', MagicMock()) # REMOVED - Not needed with prep.process mocked
# Mock input
mocker.patch('builtins.input', return_value="y") # Auto-confirm prompts if any slip through
# --- 3. Construct Args and Call async_main ---
discord_webhook_url = "https://discord.com/api/webhooks/FAKE/EDIT_URL"
rclone_destination = "googledrive:TestNomadKaraokeEdit"
# Use mock paths for file arguments
mock_style_params_path = "/fake/data/styles.json"
mock_yt_secrets_path = "/fake/data/kgenclientsecret_edit.json"
mock_yt_desc_path = "/fake/data/ytdesc_edit.txt"
mock_email_template_path = "/fake/data/emailtemplate_edit.txt"
mock_organised_dir = "/fake/organised"
mock_public_share_dir = "/fake/public_share_edit"
test_argv = [
"gen_cli.py",
"--edit-lyrics",
"--style_params_json", mock_style_params_path,
"--enable_cdg", # Keep features enabled to test paths
"--enable_txt",
"--organised_dir", mock_organised_dir,
"--organised_dir_rclone_root", f"andrewdropboxfull:organised", # Use simplified root
"--public_share_dir", mock_public_share_dir,
"--brand_prefix", brand_prefix,
"--youtube_client_secrets_file", mock_yt_secrets_path,
"--rclone_destination", rclone_destination,
"--youtube_description_file", mock_yt_desc_path,
"--discord_webhook_url", discord_webhook_url,
"--email_template_file", mock_email_template_path,
"-y", # Non-interactive
]
mocker.patch.object(sys, 'argv', test_argv)
print("--- Calling async_main (edit-lyrics simplified test) ---")
await async_main()
print("--- async_main finished (edit-lyrics simplified test) ---")
# --- 4. Assertions ---
# Verify KaraokePrep initialization and process call
prep_init_spy.assert_called_once()
_, prep_init_kwargs = prep_init_spy.call_args
assert prep_init_kwargs.get('artist') == artist
assert prep_init_kwargs.get('title') == title
assert prep_init_kwargs.get('skip_separation') is True # Edit mode should skip separation
assert prep_init_kwargs.get('create_track_subfolders') is False # Edit mode processes in place
# Check that KaraokePrep.process was called (we mocked its return value)
prep_process_mock.assert_called_once()
# Verify backup was called (important for edit mode)
# The first argument to backup_existing_outputs is 'self' (the FileHandler instance)
mock_backup.assert_called_once_with(mock_track_dir_path, artist, title)
# Verify lyrics transcription was called (important for edit mode)
# mock_transcribe.assert_called_once() # REMOVED - Not called because KaraokePrep.process is mocked
# Verify KaraokeFinalise initialization and process call
finalise_init_spy.assert_called_once()
_, finalise_init_kwargs = finalise_init_spy.call_args
# Assert only the kwargs that are actually passed to __init__ and relevant
# assert finalise_init_kwargs.get(\'artist\') == artist # Not an init arg
# assert finalise_init_kwargs.get(\'title\') == title # Not an init arg
# assert finalise_init_kwargs.get(\'base_name\') == f\"{artist} - {title}\" # Not an init arg
# assert finalise_init_kwargs.get(\'brand_code\') == brand_code # Not an init arg
# assert finalise_init_kwargs.get(\'track_output_dir\') == mock_track_dir_path # Not an init arg
assert finalise_init_kwargs.get('keep_brand_code') is True # Edit mode should keep brand code
assert finalise_init_kwargs.get('non_interactive') is True
# Check that KaraokeFinalise.process was called with replace_existing=True
finalise_process_mock.assert_called_once()
finalise_call_args, finalise_call_kwargs = finalise_process_mock.call_args
assert finalise_call_kwargs.get('replace_existing') is True # Crucial check for edit mode
# Optional: Verify specific external mocks if needed (e.g., YouTube upload called)
# Example: Check if the mocked upload function was called within the finalise logic path
# Need to mock the specific upload function called by finalise.process if not mocking process itself.
# Since we mocked finalise.process, we can't assert calls *within* it easily.
# If asserting internal calls is critical, mock *those* methods instead of the whole process method.
# For this simplified test, verifying the process calls with correct flags is sufficient.
# Remove all the previous complex code below this line if it existed.
# The test above is self-contained.