"""Unit tests for v3.6 layer-0 task md sha normalize helpers.

chair_authorization_id=CHAIR-AUTH-TASK-2705PLUS1-V36-TASK-MD-SHA-BOOTSTRAP-260528
"""
from __future__ import annotations

import sys
sys.path.insert(0, "/home/jay/workspace")

import pytest

from scripts.harness.v36.task_md_sha_normalize import (
    PATCH_TYPE_DISPATCH_META_SIDECAR,
    PATCH_TYPE_FORBIDDEN_SEMANTIC_CHANGE,
    PATCH_TYPE_NO_PATCH,
    PATCH_TYPE_RETRY_HEADER_PREPEND,
    PATCH_TYPE_UNKNOWN,
    PATCH_TYPE_WHITESPACE_NORMALIZATION,
    VERBATIM_FALSE,
    VERBATIM_TRUE,
    VERBATIM_UNVERIFIABLE,
    compute_sha256,
    content_verbatim_match,
    detect_patch_type,
    normalize_whitespace,
)


def test_compute_sha256_empty() -> None:
    assert (
        compute_sha256(b"")
        == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    )


def test_compute_sha256_none() -> None:
    assert compute_sha256(None) is None


def test_compute_sha256_text() -> None:
    assert compute_sha256(b"hello") == compute_sha256(b"hello")
    assert compute_sha256(b"hello") != compute_sha256(b"hello\n")


def test_normalize_whitespace_strip() -> None:
    assert normalize_whitespace(b"hello\n") == b"hello"
    assert normalize_whitespace(b"  hello  \n\n") == b"hello"
    assert normalize_whitespace(b"hello") == b"hello"
    assert normalize_whitespace(b"") == b""


def test_normalize_whitespace_none() -> None:
    assert normalize_whitespace(None) is None


def test_normalize_whitespace_utf8() -> None:
    assert normalize_whitespace("한글  \n".encode("utf-8")) == "한글".encode("utf-8")


def test_detect_patch_type_identical() -> None:
    assert detect_patch_type(b"hello", b"hello") == PATCH_TYPE_NO_PATCH


def test_detect_patch_type_trailing_newline() -> None:
    assert (
        detect_patch_type(b"hello\n", b"hello") == PATCH_TYPE_WHITESPACE_NORMALIZATION
    )
    assert (
        detect_patch_type(b"hello", b"hello\n\n") == PATCH_TYPE_WHITESPACE_NORMALIZATION
    )


def test_detect_patch_type_sidecar() -> None:
    base = b"# title\nbody\n"
    sidecar = b"<!-- DISPATCH_META: task_id=task-001 -->\n" + base
    assert detect_patch_type(base, sidecar) == PATCH_TYPE_DISPATCH_META_SIDECAR
    assert detect_patch_type(sidecar, base) == PATCH_TYPE_DISPATCH_META_SIDECAR


def test_detect_patch_type_retry_header() -> None:
    base = b"# title\nbody\n"
    with_retry = b"<!-- RETRY_HEADER: attempt=2 -->\n" + base
    assert detect_patch_type(base, with_retry) == PATCH_TYPE_RETRY_HEADER_PREPEND


def test_detect_patch_type_semantic_change() -> None:
    pre = b"# title\nfoo\n"
    post = b"# title\nbar\n"
    assert detect_patch_type(pre, post) == PATCH_TYPE_FORBIDDEN_SEMANTIC_CHANGE


def test_detect_patch_type_none() -> None:
    assert detect_patch_type(None, b"hello") == PATCH_TYPE_UNKNOWN
    assert detect_patch_type(b"hello", None) == PATCH_TYPE_UNKNOWN
    assert detect_patch_type(None, None) == PATCH_TYPE_UNKNOWN


def test_content_verbatim_match_identical() -> None:
    assert content_verbatim_match(b"hello", b"hello") == VERBATIM_TRUE


def test_content_verbatim_match_strip() -> None:
    assert content_verbatim_match(b"hello\n", b"hello") == VERBATIM_TRUE


def test_content_verbatim_match_diff() -> None:
    assert content_verbatim_match(b"hello", b"world") == VERBATIM_FALSE


def test_content_verbatim_match_unverifiable() -> None:
    assert content_verbatim_match(None, b"hello") == VERBATIM_UNVERIFIABLE
    assert content_verbatim_match(b"hello", None) == VERBATIM_UNVERIFIABLE


def test_content_verbatim_match_sidecar_normalized() -> None:
    base = b"# title\nbody\n"
    sidecar = b"<!-- DISPATCH_META: task_id=task-001 -->\n" + base
    assert content_verbatim_match(base, sidecar) == VERBATIM_TRUE


if __name__ == "__main__":  # pragma: no cover
    pytest.main([__file__, "-v"])
