#!/usr/bin/python3
"""Tests for pure static methods in sbo_create.handlers.files.FileHandler."""

import pytest

from sbo_create.handlers.files import FileHandler


# ---------------------------------------------------------------------------
# find_elements
# ---------------------------------------------------------------------------

INFO_LINES = [
    'PRGNAM="myapp"',
    'VERSION="1.0"',
    'HOMEPAGE="https://example.com"',
    'DOWNLOAD="https://example.com/myapp-1.0.tar.gz"',
    'MD5SUM="abc123"',
    'DOWNLOAD_x86_64=""',
    'MD5SUM_x86_64=""',
    'REQUIRES=""',
    'MAINTAINER="John Doe"',
    'EMAIL="john@example.com"',
]

FIND_ELEMENTS_CASES = [
    # (start_label, stop_label, expected_substring)
    ('DOWNLOAD="', 'MD5SUM="', 'https://example.com/myapp-1.0.tar.gz'),
    ('DOWNLOAD_x86_64="', 'MD5SUM_x86_64="', ''),
]


@pytest.mark.parametrize('start,stop,expected', FIND_ELEMENTS_CASES)
def test_find_elements(start: str, stop: str, expected: str) -> None:
    result = FileHandler.find_elements(start, stop, INFO_LINES)
    assert expected in result


def test_find_elements_empty_lines() -> None:
    result = FileHandler.find_elements('DOWNLOAD="', 'MD5SUM="', [])
    assert result == ''


def test_find_elements_strips_quotes() -> None:
    result = FileHandler.find_elements('DOWNLOAD="', 'MD5SUM="', INFO_LINES)
    assert '"' not in result


# ---------------------------------------------------------------------------
# info_find_element
# ---------------------------------------------------------------------------

INFO_FIND_CASES = [
    ('PRGNAM=', 'myapp'),
    ('VERSION=', '1.0'),
    ('HOMEPAGE=', 'https://example.com'),
    ('MAINTAINER=', 'John Doe'),
    ('EMAIL=', 'john@example.com'),
    ('REQUIRES=', ''),
]


@pytest.mark.parametrize('tag,expected', INFO_FIND_CASES)
def test_info_find_element(tag: str, expected: str) -> None:
    result = FileHandler.info_find_element(tag, INFO_LINES)
    assert result == expected


def test_info_find_element_missing_tag() -> None:
    result = FileHandler.info_find_element('NONEXISTENT=', INFO_LINES)
    assert result == ''


def test_info_find_element_empty_lines() -> None:
    result = FileHandler.info_find_element('PRGNAM=', [])
    assert result == ''
