"""E2E: Bubble sort optimization with unittest framework."""

from __future__ import annotations

import os
from pathlib import Path

from .utilities import E2ETestConfig, run_optimization, run_with_retries

_FIXTURES = Path(__file__).resolve().parent.parent / "code_to_optimize"


def _run(expected_improvement_pct: int) -> bool:
    config = E2ETestConfig(
        file_path=Path("bubble_sort.py"),
        function_name="sorter",
        min_improvement_x=0.30,
        no_gen_tests=True,
    )
    return run_optimization(
        _FIXTURES,
        config,
        expected_improvement_pct,
    )


def test_bubblesort_unittest() -> None:
    """Optimize bubble sort via unittest, no test generation."""
    pct = int(os.getenv("EXPECTED_IMPROVEMENT_PCT", "300"))
    assert 0 == run_with_retries(_run, pct)


if __name__ == "__main__":
    raise SystemExit(
        run_with_retries(
            _run,
            int(os.getenv("EXPECTED_IMPROVEMENT_PCT", "300")),
        ),
    )
