"""Tests for server.utils.text_io (EOL / UTF-8)."""

from pathlib import Path

import pytest

from server.utils.text_io import (
    detect_text_eol,
    normalize_text_eol,
    read_utf8_lf,
    read_utf8_text,
    write_utf8_lf,
)


def test_normalize_text_eol():
    assert normalize_text_eol("a\r\nb\rc") == "a\nb\nc"


def test_detect_text_eol():
    assert detect_text_eol("a\nb") == "LF"
    assert detect_text_eol("a\r\nb") == "CRLF"
    assert detect_text_eol("a\rb") == "CR"


def test_write_utf8_lf_no_cr(tmp_path: Path):
    p = tmp_path / "t.txt"
    write_utf8_lf(p, "line1\r\nline2\r")
    raw = p.read_bytes()
    assert b"\r" not in raw
    assert raw == b"line1\nline2\n"


def test_read_utf8_strips_bom(tmp_path: Path):
    p = tmp_path / "bom.env"
    p.write_bytes(b"\xef\xbb\xbfKEY=value\n")
    assert read_utf8_text(p) == "KEY=value\n"


def test_read_utf8_lf(tmp_path: Path):
    p = tmp_path / "crlf.txt"
    p.write_bytes(b"a\r\nb\r")
    assert read_utf8_lf(p) == "a\nb\n"
