from dv_requirements_merger.main import merge_requirements

def test_merge_requirements_basic(tmp_path):
    """Tests the merge_requirements function."""
    # Create dummy requirements files
    req1_content = "numpy==1.22.0\npandas==1.4.2\nrequests"
    req2_content = "numpy==1.23.0\nmatplotlib>=3.5.0\nrequests"
    req3_content = "scipy==1.9.0\npandas"

    req1_path = tmp_path / "requirements1.txt"
    req2_path = tmp_path / "requirements2.txt"
    req3_path = tmp_path / "requirements3.txt"

    req1_path.write_text(req1_content)
    req2_path.write_text(req2_content)
    req3_path.write_text(req3_content)

    output_path = tmp_path / "merged_requirements.txt"
    file_paths = [str(req1_path), str(req2_path), str(req3_path)]

    merge_requirements(file_paths, str(output_path))

    with open(output_path, 'r') as f:
        merged_content = f.read()

    expected_content = (
        "matplotlib>=3.5.0\n"
        "# CONFLICT: Found multiple versions for numpy: ==1.22.0 and ==1.23.0\n"
        "# numpy==1.22.0\n"
        "# numpy==1.23.0\n"
        "# CONFLICT: Found multiple versions for pandas: ==1.4.2 and unspecified\n"
        "# pandas==1.4.2\n"
        "# pandas\n"
        "requests\n"
        "scipy==1.9.0\n"
    )

    assert merged_content == expected_content


def test_merge_requirements_latest(tmp_path):
    """Tests the merge_requirements function keeping the latest version."""
    # Create dummy requirements files
    req1_content = "numpy==1.22.0\npandas==1.4.2\nrequests"
    req2_content = "numpy==1.23.0\nmatplotlib>=3.5.0\nrequests"
    req3_content = "scipy==1.9.0\npandas"

    req1_path = tmp_path / "requirements1.txt"
    req2_path = tmp_path / "requirements2.txt"
    req3_path = tmp_path / "requirements3.txt"

    req1_path.write_text(req1_content)
    req2_path.write_text(req2_content)
    req3_path.write_text(req3_content)

    output_path = tmp_path / "merged_requirements.txt"
    file_paths = [str(req1_path), str(req2_path), str(req3_path)]

    merge_requirements(file_paths, str(output_path), 'latest')

    with open(output_path, 'r') as f:
        merged_content = f.read()

    expected_content = (
        "matplotlib>=3.5.0\n"
        "# CONFLICT: Found multiple versions for numpy: ==1.22.0 and ==1.23.0. Keeping latest version\n"
        "numpy==1.23.0\n"
        "# CONFLICT: Found multiple versions for pandas: ==1.4.2 and unspecified. Keeping latest version\n"
        "pandas\n"
        "requests\n"
        "scipy==1.9.0\n"
    )

    assert merged_content == expected_content


def test_merge_requirements_adv(tmp_path):
    """Tests the merge_requirements function."""
    # Create dummy requirements files
    req1_content = "numpy==1.22.0\npandas==1.4.2\nrequests"
    req2_content = "numpy\nmatplotlib>=3.5.0\nrequests"
    req3_content = "scipy==1.9.0\npandas"

    req1_path = tmp_path / "requirements1.txt"
    req2_path = tmp_path / "requirements2.txt"
    req3_path = tmp_path / "requirements3.txt"

    req1_path.write_text(req1_content)
    req2_path.write_text(req2_content)
    req3_path.write_text(req3_content)

    output_path = tmp_path / "merged_requirements.txt"
    file_paths = [str(req1_path), str(req2_path), str(req3_path)]

    merge_requirements(file_paths, str(output_path))

    with open(output_path, 'r') as f:
        merged_content = f.read()

    expected_content = (
        "matplotlib>=3.5.0\n"
        "# CONFLICT: Found multiple versions for numpy: ==1.22.0 and unspecified\n"
        "# numpy==1.22.0\n"
        "# numpy\n"
        "# CONFLICT: Found multiple versions for pandas: ==1.4.2 and unspecified\n"
        "# pandas==1.4.2\n"
        "# pandas\n"
        "requests\n"
        "scipy==1.9.0\n"
    )

    assert merged_content == expected_content
