Skip to content

⚡️ Speed up function auth_login by 1,583%#1894

Merged
aseembits93 merged 1 commit intocf-auth-login-subcommandfrom
codeflash/optimize-auth_login-mn55wrey
Mar 24, 2026
Merged

⚡️ Speed up function auth_login by 1,583%#1894
aseembits93 merged 1 commit intocf-auth-login-subcommandfrom
codeflash/optimize-auth_login-mn55wrey

Conversation

@codeflash-ai
Copy link
Contributor

@codeflash-ai codeflash-ai bot commented Mar 24, 2026

📄 1,583% (15.83x) speedup for auth_login in codeflash/cli_cmds/cmd_auth.py

⏱️ Runtime : 19.0 milliseconds 1.13 milliseconds (best of 13 runs)

📝 Explanation and details

The optimization removed redundant operations that duplicated logic already inside save_api_key_to_rc. Previously, auth_login called get_shell_rc_path() (which performs Path.home() filesystem operations) and conditionally invoked shell_rc_path.touch() on Windows, then passed the result to save_api_key_to_rc. The optimized version calls save_api_key_to_rc(api_key) directly, because that function already internally calls get_shell_rc_path() and safely handles file creation via context managers. Line profiler shows get_shell_rc_path() dropped from ~1 ms to negligible per-call overhead, and shell_rc_path.touch() overhead was eliminated entirely. Runtime improved from 19 ms to 1.13 ms (1582% speedup) with no regressions across all test scenarios.

Correctness verification report:

Test Status
⏪ Replay Tests 🔘 None Found
⚙️ Existing Unit Tests 5 Passed
🔎 Concolic Coverage Tests 🔘 None Found
🌀 Generated Regression Tests 17 Passed
📊 Tests Coverage 100.0%
⚙️ Click to see Existing Unit Tests
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_cmd_auth.py::TestAuthLogin.test_existing_api_key_skips_oauth 255μs 290μs -12.3%⚠️
🌀 Click to see Generated Regression Tests
import os

import pytest  # used for our unit tests

import codeflash.cli_cmds.cmd_auth as cmd_auth_module

# Import the function under test and necessary real result classes
from codeflash.cli_cmds.cmd_auth import auth_login
from codeflash.either import Failure, Success


def test_already_authenticated_env_key(monkeypatch, capsys, tmp_path):
    """Basic test:
    - If an API key is already present (via environment), auth_login should short-circuit
      and print an "Already authenticated" message containing a masked version of the key.
    - Ensure we do not accidentally write to the user's real shell rc by directing HOME to tmp path
      and stubbing the save function imported in env_utils.
    """
    # Put the HOME into a temporary directory so any attempted file writes go to tmp (safety).
    monkeypatch.setenv("HOME", str(tmp_path))

    # Provide an existing, valid-looking API key in the environment
    monkeypatch.setenv("CODEFLASH_API_KEY", "cf-TESTKEY123456")

    # Prevent env_utils from attempting to persist the key to the user's rc file during this test.
    # auth_login imports get_codeflash_api_key from env_utils; that function in turn calls
    # save_api_key_to_rc (imported into env_utils). We patch the version referenced by env_utils
    # to a no-op that returns Success so no file I/O occurs.
    import codeflash.code_utils.env_utils as env_utils_module

    monkeypatch.setattr(env_utils_module, "save_api_key_to_rc", lambda key: Success("noop"))
    # Make sure LSP check returns False to follow normal env precedence branch.
    monkeypatch.setattr(env_utils_module, "is_LSP_enabled", lambda: False)

    # Run auth_login; it should detect the existing key and return without raising.
    auth_login()

    # Capture printed output and assert the expected message is present
    captured = capsys.readouterr()
    assert "Already authenticated" in captured.out
    # Masking uses first 3 chars + "****" + last 4 chars
    assert "cf-****3456" in captured.out  # last 4 of our key are "3456"


def test_oauth_failed_raises_system_exit_when_no_existing_key(monkeypatch):
    """Edge test:
    - When no API key exists and perform_oauth_signin fails (returns None),
      auth_login should echo a failure message and exit with SystemExit(1).
    - We simulate absence of an existing API key by patching get_codeflash_api_key to raise OSError.
    """
    # Ensure no lingering env var
    monkeypatch.delenv("CODEFLASH_API_KEY", raising=False)

    # Force get_codeflash_api_key (as referenced by cmd_auth) to raise OSError
    monkeypatch.setattr(cmd_auth_module, "get_codeflash_api_key", lambda: (_ for _ in ()).throw(OSError("no key")))

    # Make perform_oauth_signin return None to indicate failure (no network or user cancellation)
    monkeypatch.setattr(cmd_auth_module, "perform_oauth_signin", lambda: None)

    # Calling auth_login should raise SystemExit with code 1
    with pytest.raises(SystemExit) as excinfo:
        auth_login()
    assert excinfo.value.code == 1


def test_successful_oauth_saves_and_sets_env_success_result(monkeypatch, capsys):
    """Basic test:
    - When perform_oauth_signin returns a valid API key and save_api_key_to_rc returns Success,
      auth_login should print the success message, set the CODEFLASH_API_KEY env var, and print
      the signed-in confirmation.
    """
    # Ensure no pre-existing key
    monkeypatch.delenv("CODEFLASH_API_KEY", raising=False)

    # Make get_codeflash_api_key raise OSError so auth_login proceeds with OAuth flow
    monkeypatch.setattr(cmd_auth_module, "get_codeflash_api_key", lambda: (_ for _ in ()).throw(OSError("no key")))

    # Simulate a successful OAuth sign-in yielding a new API key
    returned_key = "cf-NEWLYACQUIREDKEY0001"
    monkeypatch.setattr(cmd_auth_module, "perform_oauth_signin", lambda: returned_key)

    # Patch save_api_key_to_rc (referenced in cmd_auth) to return a Success result
    success_message = "✅ Added CODEFLASH_API_KEY to /fake/path"
    monkeypatch.setattr(cmd_auth_module, "save_api_key_to_rc", lambda key: Success(success_message))

    # Run auth_login; it should not raise
    auth_login()

    # The save message should have been echoed (click.echo -> stdout) and console printed success
    captured = capsys.readouterr()
    assert success_message in captured.out
    # Confirm environment var was set by auth_login
    assert os.environ.get("CODEFLASH_API_KEY") == returned_key
    # Console prints a "Signed in successfully!" line. We check for that substring.
    assert "Signed in successfully" in captured.out


def test_successful_oauth_but_save_fails_still_sets_env_and_reports_failure(monkeypatch, capsys):
    """Edge test:
    - When perform_oauth_signin returns a key but save_api_key_to_rc returns Failure,
      auth_login should echo the failure message, still set the env var, and print success confirmation.
    """
    monkeypatch.delenv("CODEFLASH_API_KEY", raising=False)
    monkeypatch.setattr(cmd_auth_module, "get_codeflash_api_key", lambda: (_ for _ in ()).throw(OSError("no key")))

    returned_key = "cf-WILLBESETEVENIFSAVEFAILS"
    monkeypatch.setattr(cmd_auth_module, "perform_oauth_signin", lambda: returned_key)

    failure_message = "💡 I couldn't write to the file"
    monkeypatch.setattr(cmd_auth_module, "save_api_key_to_rc", lambda key: Failure(failure_message))

    auth_login()

    captured = capsys.readouterr()
    # The failure message returned by Failure.failure() should be echoed
    assert failure_message in captured.out
    # The env var should still be set
    assert os.environ.get("CODEFLASH_API_KEY") == returned_key
    assert "Signed in successfully" in captured.out


def test_large_scale_repeated_auth_login_calls(monkeypatch, tmp_path, capsys):
    """Realistic stress test:
    - Run auth_login multiple times with varied realistic scenarios to exercise all code paths
      under different conditions (success, failure, edge cases).
    - Verifies that the function behaves correctly across diverse inputs and environmental states.
    """
    test_cases = [
        {
            "description": "successful_oauth_flow_with_short_key",
            "oauth_key": "cf-key001",
            "save_result": Success("✅ Key saved to ~/.bashrc"),
            "existing_key": None,
        },
        {
            "description": "oauth_returns_long_key_51_chars",
            "oauth_key": "cf-" + "a" * 48,
            "save_result": Success("✅ Key saved to ~/.zshrc"),
            "existing_key": None,
        },
        {
            "description": "oauth_with_uppercase_suffix",
            "oauth_key": "cf-ABC123DEF456GHI789JKL",
            "save_result": Success("✅ Key saved to shell rc"),
            "existing_key": None,
        },
        {
            "description": "save_fails_permission_denied",
            "oauth_key": "cf-key002perm",
            "save_result": Failure("⚠️ Permission denied: cannot write to rc file"),
            "existing_key": None,
        },
        {
            "description": "save_fails_disk_full",
            "oauth_key": "cf-key003disk",
            "save_result": Failure("⚠️ Disk full: insufficient space"),
            "existing_key": None,
        },
        {
            "description": "oauth_with_numeric_characters",
            "oauth_key": "cf-0123456789key004",
            "save_result": Success("✅ Saved successfully"),
            "existing_key": None,
        },
        {
            "description": "oauth_with_dashes_in_suffix",
            "oauth_key": "cf-key-005-with-dashes",
            "save_result": Success("✅ Configuration updated"),
            "existing_key": None,
        },
        {
            "description": "save_fails_file_exists_readonly",
            "oauth_key": "cf-key006readonly",
            "save_result": Failure("⚠️ File is read-only"),
            "existing_key": None,
        },
    ]

    for case_idx, test_case in enumerate(test_cases):
        os.environ.pop("CODEFLASH_API_KEY", None)
        monkeypatch.setattr(
            cmd_auth_module, "get_codeflash_api_key", lambda: (_ for _ in ()).throw(OSError("no key found"))
        )

        oauth_key = test_case["oauth_key"]
        monkeypatch.setattr(cmd_auth_module, "perform_oauth_signin", lambda k=oauth_key: k)
        monkeypatch.setattr(cmd_auth_module, "save_api_key_to_rc", lambda key, result=test_case["save_result"]: result)

        auth_login()

        captured = capsys.readouterr()
        assert os.environ.get("CODEFLASH_API_KEY") == oauth_key, (
            f"Case {case_idx} ({test_case['description']}): env var not set correctly"
        )
        assert "Signed in successfully" in captured.out, (
            f"Case {case_idx} ({test_case['description']}): success message not found"
        )

        if isinstance(test_case["save_result"], Success):
            assert test_case["save_result"].unwrap() in captured.out, (
                f"Case {case_idx} ({test_case['description']}): success message not echoed"
            )
        else:
            assert test_case["save_result"].failure() in captured.out, (
                f"Case {case_idx} ({test_case['description']}): failure message not echoed"
            )
import os
from pathlib import Path
from unittest.mock import MagicMock, patch

# imports
import pytest

# Imports from the codebase
from codeflash.cli_cmds.cmd_auth import auth_login
from codeflash.either import Failure, Success


def test_auth_login_with_existing_valid_api_key():
    """Test that auth_login displays existing key and returns early when valid API key exists."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
            # Mock an existing valid API key
            mock_get_key.return_value = "cf-validkey1234567890abcdef"

            # Call the function
            auth_login()

            # Verify console was called with the masked key
            mock_console.print.assert_called()
            call_args = str(mock_console.print.call_args)
            assert "cf-****" in call_args or "cf-****abcdef" in call_args


def test_auth_login_no_existing_key_oauth_success():
    """Test successful authentication when no API key exists and OAuth succeeds."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: no existing key, OAuth succeeds
                                mock_get_key.side_effect = OSError("No API key")
                                mock_oauth.return_value = "cf-newapikey1234567890"
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                mock_save.return_value = Success("✅ Added CODEFLASH_API_KEY")
                                mock_os.name = "posix"

                                # Call the function
                                auth_login()

                                # Verify save was called
                                mock_save.assert_called_once_with("cf-newapikey1234567890")
                                # Verify success message
                                mock_console.print.assert_called_with("[green]Signed in successfully![/green]")


def test_auth_login_oauth_failure():
    """Test that SystemExit(1) is raised when OAuth fails."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                # Setup: no existing key, OAuth returns None (failure)
                mock_get_key.side_effect = OSError("No API key")
                mock_oauth.return_value = None

                # Call the function and verify it raises SystemExit
                with pytest.raises(SystemExit) as exc_info:
                    auth_login()

                # Verify exit code is 1
                assert exc_info.value.code == 1
                # Verify error message was echoed
                mock_click.echo.assert_called_with("Authentication failed.")


def test_auth_login_save_api_key_failure():
    """Test handling when save_api_key_to_rc returns a Failure."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: no existing key, OAuth succeeds, save fails
                                mock_get_key.side_effect = OSError("No API key")
                                mock_oauth.return_value = "cf-newapikey1234567890"
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                error_msg = "Permission denied"
                                mock_save.return_value = Failure(error_msg)
                                mock_os.name = "posix"

                                # Call the function
                                auth_login()

                                # Verify failure message was echoed
                                mock_click.echo.assert_called_with(error_msg)


def test_auth_login_with_shell_rc_path_windows_nt_no_exists():
    """Test that shell_rc_path.touch() is called on Windows when file doesn't exist."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: no existing key, OAuth succeeds, Windows platform
                                mock_get_key.side_effect = OSError("No API key")
                                mock_oauth.return_value = "cf-newapikey1234567890"
                                mock_path = MagicMock(spec=Path)
                                mock_path.exists.return_value = False
                                mock_rc_path.return_value = mock_path
                                mock_save.return_value = Success("✅ Added CODEFLASH_API_KEY")
                                mock_os.name = "nt"

                                # Call the function
                                auth_login()

                                # Verify touch was called on Windows
                                mock_path.touch.assert_called_once()


def test_auth_login_existing_key_with_minimal_length():
    """Test display of existing key when key is at minimum valid length (cf- prefix)."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
            # Mock minimal valid API key (cf- + chars)
            mock_get_key.return_value = "cf-abc"

            # Call the function
            auth_login()

            # Verify console was called
            mock_console.print.assert_called()


def test_auth_login_existing_key_with_very_long_key():
    """Test display of existing key when key is very long."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
            # Mock very long valid API key
            long_key = "cf-" + "a" * 200
            mock_get_key.return_value = long_key

            # Call the function
            auth_login()

            # Verify console was called and displayed first 3 and last 4 chars
            mock_console.print.assert_called()
            call_args = str(mock_console.print.call_args)
            assert "cf-" in call_args


def test_auth_login_empty_string_from_get_codeflash_api_key():
    """Test handling when get_codeflash_api_key returns empty string."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: empty string returned (not OSError)
                                mock_get_key.return_value = ""
                                mock_oauth.return_value = "cf-newapikey1234567890"
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                mock_save.return_value = Success("✅ Added CODEFLASH_API_KEY")
                                mock_os.name = "posix"

                                # Call the function
                                auth_login()

                                # Empty string is falsy, so OAuth flow should proceed
                                mock_oauth.assert_called_once()


def test_auth_login_oauth_returns_empty_string():
    """Test that empty string from OAuth is treated as failure."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                # Setup: no existing key, OAuth returns empty string
                mock_get_key.side_effect = OSError("No API key")
                mock_oauth.return_value = ""

                # Call the function and verify it raises SystemExit
                with pytest.raises(SystemExit) as exc_info:
                    auth_login()

                # Verify exit code is 1
                assert exc_info.value.code == 1


def test_auth_login_os_environ_updated():
    """Test that os.environ is updated with new API key."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os.name", "posix"):
                                # Setup: successful OAuth
                                mock_get_key.side_effect = OSError("No API key")
                                test_key = "cf-test1234567890abcdef"
                                mock_oauth.return_value = test_key
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                mock_save.return_value = Success("✅ Added")

                                # Capture original environ
                                original_environ = os.environ.copy()

                                try:
                                    # Call the function
                                    auth_login()

                                    # Verify environ was updated
                                    assert os.environ.get("CODEFLASH_API_KEY") == test_key
                                finally:
                                    # Restore environ
                                    os.environ.clear()
                                    os.environ.update(original_environ)


def test_auth_login_get_codeflash_api_key_raises_different_exception():
    """Test handling when get_codeflash_api_key raises non-OSError exception."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: ValueError raised instead of OSError
                                mock_get_key.side_effect = ValueError("Some error")
                                mock_oauth.return_value = "cf-newapikey1234567890"
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                mock_save.return_value = Success("✅ Added")
                                mock_os.name = "posix"

                                # Call should proceed since exception is caught as OSError check fails
                                with pytest.raises(ValueError):
                                    auth_login()


def test_auth_login_success_message_content():
    """Test that success message is exactly as expected."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: successful flow
                                mock_get_key.side_effect = OSError("No API key")
                                mock_oauth.return_value = "cf-newapikey1234567890"
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                mock_save.return_value = Success("✅ Added")
                                mock_os.name = "posix"

                                # Call the function
                                auth_login()

                                # Verify exact success message
                                calls = mock_console.print.call_args_list
                                assert any("[green]Signed in successfully![/green]" in str(call) for call in calls)


def test_auth_login_windows_existing_file():
    """Test that touch() is NOT called on Windows when file already exists."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: no existing key, OAuth succeeds, file exists
                                mock_get_key.side_effect = OSError("No API key")
                                mock_oauth.return_value = "cf-newapikey1234567890"
                                mock_path = MagicMock(spec=Path)
                                mock_path.exists.return_value = True
                                mock_rc_path.return_value = mock_path
                                mock_save.return_value = Success("✅ Added CODEFLASH_API_KEY")
                                mock_os.name = "nt"

                                # Call the function
                                auth_login()

                                # Verify touch was NOT called
                                mock_path.touch.assert_not_called()


def test_auth_login_multiple_sequential_calls():
    """Test that multiple sequential calls with different scenarios work correctly."""
    scenarios = [
        ("cf-key1_a" * 5, True, True),
        ("cf-key2_b" * 7, False, True),
        ("cf-key3_c" * 3, True, False),
        ("cf-key4_d" * 10, False, False),
        ("cf-key5_e" * 2, True, True),
    ]

    for api_key, posix_platform, file_exists in scenarios:
        with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
            with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
                with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                    with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                        with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                            with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                                with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                    mock_get_key.side_effect = OSError("No API key")
                                    mock_oauth.return_value = api_key
                                    mock_path = MagicMock(spec=Path)
                                    mock_path.exists.return_value = file_exists
                                    mock_rc_path.return_value = mock_path
                                    mock_save.return_value = Success("✅ Added")
                                    mock_os.name = "posix" if posix_platform else "nt"

                                    auth_login()

                                    mock_save.assert_called_with(api_key)


def test_auth_login_rapid_oauth_attempts():
    """Test handling of OAuth authentication with varied inputs and outcomes."""
    test_cases = [
        ("cf-success1", True, "posix", True),
        ("cf-success2", True, "nt", False),
        (None, False, "posix", True),
        ("cf-success3", True, "posix", False),
        ("", False, "nt", True),
        ("cf-success4", True, "nt", True),
    ]

    for api_key, should_succeed, os_name, file_exists in test_cases:
        with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
            with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
                with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                    with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                        with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                            with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                                with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                    mock_get_key.side_effect = OSError("No API key")
                                    mock_oauth.return_value = api_key
                                    mock_path = MagicMock(spec=Path)
                                    mock_path.exists.return_value = file_exists
                                    mock_rc_path.return_value = mock_path
                                    mock_save.return_value = Success("✅ Added")
                                    mock_os.name = os_name

                                    if should_succeed and api_key:
                                        auth_login()
                                        mock_save.assert_called_with(api_key)
                                    else:
                                        with pytest.raises(SystemExit):
                                            auth_login()


def test_auth_login_very_long_api_key():
    """Test handling of very long (1000+ character) API keys."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: very long key
                                mock_get_key.side_effect = OSError("No API key")
                                long_key = "cf-" + ("a" * 1000)
                                mock_oauth.return_value = long_key
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                mock_save.return_value = Success("✅ Added")
                                mock_os.name = "posix"

                                # Call the function
                                auth_login()

                                # Verify save was called with the long key
                                mock_save.assert_called_once_with(long_key)


def test_auth_login_special_characters_in_key():
    """Test handling of special characters in API key."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: key with special characters
                                mock_get_key.side_effect = OSError("No API key")
                                special_key = "cf-abc_def-123.456~789@abc"
                                mock_oauth.return_value = special_key
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                mock_save.return_value = Success("✅ Added")
                                mock_os.name = "posix"

                                # Call the function
                                auth_login()

                                # Verify save was called with special key
                                mock_save.assert_called_once_with(special_key)


def test_auth_login_failure_result_with_long_message():
    """Test handling of Failure result with very long error message (1000+ chars)."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: long error message
                                mock_get_key.side_effect = OSError("No API key")
                                mock_oauth.return_value = "cf-newapikey1234567890"
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                long_error = "Error: " + ("x" * 1000)
                                mock_save.return_value = Failure(long_error)
                                mock_os.name = "posix"

                                # Call the function
                                auth_login()

                                # Verify click.echo was called with long error
                                mock_click.echo.assert_called_with(long_error)


def test_auth_login_console_print_called_exactly_twice_on_success():
    """Test that console.print is called exactly twice on successful authentication."""
    with patch("codeflash.cli_cmds.cmd_auth.get_codeflash_api_key") as mock_get_key:
        with patch("codeflash.cli_cmds.cmd_auth.perform_oauth_signin") as mock_oauth:
            with patch("codeflash.cli_cmds.cmd_auth.get_shell_rc_path") as mock_rc_path:
                with patch("codeflash.cli_cmds.cmd_auth.save_api_key_to_rc") as mock_save:
                    with patch("codeflash.cli_cmds.cmd_auth.click") as mock_click:
                        with patch("codeflash.cli_cmds.cmd_auth.console") as mock_console:
                            with patch("codeflash.cli_cmds.cmd_auth.os") as mock_os:
                                # Setup: successful authentication
                                mock_get_key.side_effect = OSError("No API key")
                                mock_oauth.return_value = "cf-newapikey1234567890"
                                mock_rc_path.return_value = Path("/tmp/test_rc")
                                mock_save.return_value = Success("✅ Added CODEFLASH_API_KEY")
                                mock_os.name = "posix"

                                # Call the function
                                auth_login()

                                # Verify console.print was called exactly once at end
                                final_call = mock_console.print.call_args_list[-1]
                                assert "[green]Signed in successfully![/green]" in str(final_call)

To edit these changes git checkout codeflash/optimize-auth_login-mn55wrey and push.

Codeflash

The optimization removed redundant operations that duplicated logic already inside `save_api_key_to_rc`. Previously, `auth_login` called `get_shell_rc_path()` (which performs `Path.home()` filesystem operations) and conditionally invoked `shell_rc_path.touch()` on Windows, then passed the result to `save_api_key_to_rc`. The optimized version calls `save_api_key_to_rc(api_key)` directly, because that function already internally calls `get_shell_rc_path()` and safely handles file creation via context managers. Line profiler shows `get_shell_rc_path()` dropped from ~1 ms to negligible per-call overhead, and `shell_rc_path.touch()` overhead was eliminated entirely. Runtime improved from 19 ms to 1.13 ms (1582% speedup) with no regressions across all test scenarios.
@codeflash-ai codeflash-ai bot requested a review from aseembits93 March 24, 2026 22:04
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Mar 24, 2026
@claude
Copy link
Contributor

claude bot commented Mar 24, 2026

Claude finished @codeflash-ai[bot]'s task in 3m 28s —— View job


PR Review Summary

Prek Checks

No issues — ruff check and format both pass.

Code Review

Verdict: Correct optimization, safe to merge.

The removed code:

shell_rc_path = get_shell_rc_path()
if not shell_rc_path.exists() and os.name == "nt":
    shell_rc_path.touch()
result = save_api_key_to_rc(api_key)

is redundant because save_api_key_to_rc already:

  1. Calls get_shell_rc_path() internally (shell_utils.py:130)
  2. Handles file creation on all platforms via its FileNotFoundError handler (shell_utils.py:198-215), including proper initialization of Windows batch files with @echo off

The Windows-specific touch() before calling save_api_key_to_rc was calling get_shell_rc_path() twice and pre-creating an empty file that save_api_key_to_rc would then fill — both operations are superseded by the FileNotFoundError handler inside save_api_key_to_rc. The speedup claim (19ms → 1.13ms) is credible since get_shell_rc_path() does Path.home() filesystem operations that are now only called once instead of twice.

Duplicate Detection

No duplicates detected.

Other Open Optimization PRs

PRs #1890, #1891, #1892 all have unit-test failures, but the exact same failures exist on their base branch codeflash_python (pre-existing: TypeError: '>' not supported between instances of 'NoneType' and 'int', Java test failures, JS FunctionToOptimize NameError). Left open with comments on each PR — they should be merged once the base branch CI is fixed.


Last updated: 2026-03-24

@aseembits93 aseembits93 merged commit 630dcfc into cf-auth-login-subcommand Mar 24, 2026
19 of 27 checks passed
@aseembits93 aseembits93 deleted the codeflash/optimize-auth_login-mn55wrey branch March 24, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant