#!/usr/bin/python3
"""Tests for slpkg.errors.Errors.message()."""

import pytest

from slpkg.errors import Errors


def make_errors() -> Errors:
    """Construct Errors bypassing __init__ (avoids config_load)."""
    err: Errors = object.__new__(Errors)
    err.prog_name = "slpkg"
    err.red = ""
    err.endc = ""
    return err


# ---------------------------------------------------------------------------
# message() raises SystemExit with the correct exit code
# ---------------------------------------------------------------------------


def test_message_raises_systemexit_code_1() -> None:
    err = make_errors()
    with pytest.raises(SystemExit) as exc:
        err.message("some error", 1)
    assert exc.value.code == 1


def test_message_raises_systemexit_code_42() -> None:
    err = make_errors()
    with pytest.raises(SystemExit) as exc:
        err.message("other error", 42)
    assert exc.value.code == 42


def test_message_raises_systemexit_code_0() -> None:
    err = make_errors()
    with pytest.raises(SystemExit) as exc:
        err.message("done", 0)
    assert exc.value.code == 0


# ---------------------------------------------------------------------------
# message() prints the message string to stdout
# ---------------------------------------------------------------------------


def test_message_output_contains_message_text(capsys: pytest.CaptureFixture) -> None:
    err = make_errors()
    with pytest.raises(SystemExit):
        err.message("disk full", 1)
    captured = capsys.readouterr()
    assert "disk full" in captured.out


def test_message_output_contains_prog_name(capsys: pytest.CaptureFixture) -> None:
    err = make_errors()
    with pytest.raises(SystemExit):
        err.message("something went wrong", 1)
    captured = capsys.readouterr()
    assert "slpkg" in captured.out


def test_message_output_contains_error_label(capsys: pytest.CaptureFixture) -> None:
    err = make_errors()
    with pytest.raises(SystemExit):
        err.message("bad argument", 2)
    captured = capsys.readouterr()
    assert "Error" in captured.out


def test_message_always_raises(capsys: pytest.CaptureFixture) -> None:
    """message() must raise SystemExit regardless of message content."""
    err = make_errors()
    for msg, code in [("a", 1), ("b", 2), ("c", 127)]:
        with pytest.raises(SystemExit) as exc:
            err.message(msg, code)
        assert exc.value.code == code
