# Previous content of test_update_packages.py 

import pytest
import json
from unittest.mock import patch, mock_open, MagicMock, call
import os
import sys
from git.repo.fun import is_git_dir

# Add parent directory to path so we can import update_packages
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from update_packages import update_pkgbuild, commit_and_push, main

@pytest.fixture
def mock_config():
    return {
        'packages': {
            'test-package': {
                'github_repo': 'owner/repo',
                'version_method': 'release',
                'pkgbuild_path': 'test-package/PKGBUILD'
            }
        }
    }

def test_update_pkgbuild():
    package_config = {
        'pkgbuild_path': 'test-package/PKGBUILD'
    }
    
    pkgbuild_content = '''
# Maintainer: Test User <test@example.com>
pkgname=test-package
pkgver=1.2.3
pkgrel=1
    '''
    
    with patch('builtins.open', mock_open(read_data=pkgbuild_content)) as mock_file:
        with patch('subprocess.run') as mock_run:
            update_pkgbuild('test-package', package_config, '1.2.4')
            
            # Check if PKGBUILD was updated correctly
            write_calls = [call.args[0] for call in mock_file().write.call_args_list]
            written_content = ''.join(write_calls)
            assert 'pkgver=1.2.4' in written_content
            
            # Check if .SRCINFO was generated
            mock_run.assert_called_with(
                ['makepkg', '--printsrcinfo'],
                cwd='test-package',
                stdout=mock_file(),
                check=True
            )

def test_commit_and_push():
    mock_repo = MagicMock()
    mock_index = MagicMock()
    mock_repo.index = mock_index
    
    # Create mock file content
    file_content = b'mock file content'
    
    # Mock os.path.dirname to return the directory part
    with patch('git.Repo', return_value=mock_repo):
        with patch('os.path.dirname', return_value='test-package'):
            with patch('os.lstat') as mock_lstat:
                with patch('builtins.open', mock_open(read_data=file_content)) as mock_file:
                    with patch('git.repo.fun.is_git_dir', return_value=True):
                        # Create a mock stat result
                        mock_stat = MagicMock()
                        mock_stat.st_mode = 33188  # Regular file
                        mock_stat.st_size = len(file_content)
                        mock_stat.st_mtime = 1234567890
                        mock_lstat.return_value = mock_stat
                        
                        commit_and_push('test-package', '1.2.4', 'test-package/PKGBUILD')
                        
                        # Check if files were added
                        mock_index.add.assert_called_with(['test-package/PKGBUILD', 'test-package/.SRCINFO'])
                        
                        # Check if commit was made with correct message
                        mock_index.commit.assert_called_with(
                            'Update test-package to 1.2.4',
                            gpgsign=True
                        )
                        
                        # Check if push was called
                        mock_repo.remote().push.assert_called_once()

def test_main():
    config_yaml = '''
packages:
  test-package:
    github_repo: owner/repo
    version_method: release
    pkgbuild_path: test-package/PKGBUILD
'''
    updates_json = '''[
        {
            "name": "test-package",
            "current": "1.2.3",
            "latest": "1.2.4"
        }
    ]'''
    
    with patch.dict('os.environ', {'UPDATES_LIST': updates_json}):
        with patch('builtins.open', mock_open(read_data=config_yaml)):
            with patch('update_packages.update_pkgbuild') as mock_update:
                with patch('update_packages.commit_and_push') as mock_commit:
                    main()
                    
                    # Check if update was attempted
                    mock_update.assert_called_with(
                        'test-package',
                        {'github_repo': 'owner/repo', 'version_method': 'release', 'pkgbuild_path': 'test-package/PKGBUILD'},
                        '1.2.4'
                    )
                    
                    # Check if commit and push was attempted
                    mock_commit.assert_called_with(
                        'test-package',
                        '1.2.4',
                        'test-package/PKGBUILD'
                    ) 