def test_send_pull_request_target_branch_with_fork(
    mock_get, mock_post, mock_run, mock_issue, mock_output_dir
):
    """Test that target_branch works correctly when using a fork."""
    repo_path = os.path.join(mock_output_dir, 'repo')
    fork_owner = 'fork-owner'
    target_branch = 'custom-target'

    # Mock API responses
    mock_get.side_effect = [
        MagicMock(status_code=404),  # Branch doesn't exist
        MagicMock(status_code=200),  # Target branch exists
    ]

    mock_post.return_value.json.return_value = {
        'html_url': 'https://github.com/test-owner/test-repo/pull/1'
    }

    # Mock subprocess.run calls
    mock_run.side_effect = [
        MagicMock(returncode=0),  # git checkout -b
        MagicMock(returncode=0),  # git push
    ]

    # Call the function with fork_owner and target_branch
    send_pull_request(
        issue=mock_issue,
        token='test-token',
        username='test-user',
        platform=ProviderType.GITHUB,
        patch_dir=repo_path,
        pr_type='ready',
        fork_owner=fork_owner,
        target_branch=target_branch,
    )

    # Assert API calls
    assert mock_get.call_count == 2

    # Verify target branch was checked in original repo, not fork
    target_branch_check = mock_get.call_args_list[1]
    assert (
        target_branch_check[0][0]
        == f'https://api.github.com/repos/test-owner/test-repo/branches/{target_branch}'
    )

    # Check PR creation
    mock_post.assert_called_once()
    post_data = mock_post.call_args[1]['json']
    assert post_data['base'] == target_branch  # PR should target the specified branch
    assert (
        post_data['head'] == 'fork-owner:openhands-fix-issue-42'
    )  # Branch name should be standard

    # Check that push was to fork
    push_call = mock_run.call_args_list[1]
    assert f'https://test-user:test-token@github.com/{fork_owner}/test-repo.git' in str(
        push_call
    )
Test that target_branch works correctly when using a fork.
tests/unit/resolver/github/test_send_pull_request.py