from pathlib import Path

import pytest

from .. import utils as script_utils
from .utils import run_shell_command


@pytest.fixture(scope="session")
def html_linkchecker_result():
    git_root = script_utils.get_git_root()
    script_path = git_root / Path("scripts", "linkchecker.fish")
    test_html = Path(git_root, "scripts", "tests", ".linkchecker.test.html")

    result = run_shell_command(script_path, str(test_html))
    return result


def test_invalid_port_error(html_linkchecker_result):
    assert (
        "Internal linkchecker: 1" in html_linkchecker_result.stderr
    ), "URL error not found in output"
    assert (
        html_linkchecker_result.returncode != 0
    ), "Linkchecker script should have failed"


def test_invalid_asset_error(html_linkchecker_result):
    assert (
        "External linkchecker: 1" in html_linkchecker_result.stderr
    ), "Invalid asset error not found in output"
    assert (
        html_linkchecker_result.returncode != 0
    ), "Linkchecker script should have failed"


if __name__ == "__main__":
    pytest.main([__file__])
