"""CLI output tests for legacy Windows code pages."""

from __future__ import annotations

import io
import sys

from app import main


class _SummarySettings:
    def safe_summary(self) -> dict[str, object]:
        return {"allowed_domains": ["example.com"], "admin_ids": [1]}


def test_configure_stdio_uses_utf8_for_legacy_streams(monkeypatch) -> None:
    stdout = io.TextIOWrapper(io.BytesIO(), encoding="cp1252")
    stderr = io.TextIOWrapper(io.BytesIO(), encoding="cp1252")
    monkeypatch.setattr(sys, "stdout", stdout)
    monkeypatch.setattr(sys, "stderr", stderr)

    main._configure_stdio()

    assert stdout.encoding.lower() == "utf-8"
    assert stderr.encoding.lower() == "utf-8"


def test_check_config_prints_persian_on_legacy_console(monkeypatch) -> None:
    stdout = io.TextIOWrapper(io.BytesIO(), encoding="cp1252")
    stderr = io.TextIOWrapper(io.BytesIO(), encoding="cp1252")
    monkeypatch.setattr(sys, "stdout", stdout)
    monkeypatch.setattr(sys, "stderr", stderr)
    main._configure_stdio()

    main._check_config(_SummarySettings())  # type: ignore[arg-type]
    stdout.flush()

    output = stdout.buffer.getvalue().decode("utf-8")
    assert "پیکربندی" in output
    assert "example.com" in output
