#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2025-2026 Marcus Quinn
"""Regression tests for report Markdown block rendering helpers."""

from __future__ import annotations

import os
import sys

_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_SCRIPTS_DIR = os.path.join(_REPO_ROOT, ".agents", "scripts")
sys.path.insert(0, _SCRIPTS_DIR)

from report_render_blocks import handle_table, is_markdown_table_separator_row, split_markdown_table_row  # noqa: E402


def assert_equal(actual: object, expected: object, description: str) -> None:
    if actual != expected:
        raise AssertionError(f"{description}: expected {expected!r}, got {actual!r}")


def test_split_markdown_table_row_unescapes_backslash_pairs() -> None:
    row = r"| before \\a | before \\| after | trailing \\ |"
    assert_equal(
        split_markdown_table_row(row),
        [r" before \a ", " before \\", r" after ", r" trailing \ "],
        "table rows unescape double backslashes before text, delimiters, and row end",
    )


def test_split_markdown_table_row_keeps_escaped_pipes_in_cell() -> None:
    row = r"| literal \| pipe | delimiter |"
    assert_equal(
        split_markdown_table_row(row),
        [" literal | pipe ", " delimiter "],
        "odd backslash before pipe keeps the pipe inside the current cell",
    )


def test_handle_table_skips_only_header_separator_row() -> None:
    body: list[str] = []
    states: dict[str, object] = {"table": False, "paragraph": [], "list": False, "list_tag": ""}

    assert handle_table("| Name | Value |", body, states)
    assert handle_table("| --- | --- |", body, states)
    assert handle_table("| --- | --- |", body, states)
    assert handle_table("| - | - |", body, states)
    assert handle_table("| --- | --- |", body, states)

    assert_equal(
        body,
        [
            "<table><thead>",
            "<tr><th>Name</th><th>Value</th></tr></thead><tbody>",
            "<tr><td>---</td><td>---</td></tr>",
            "<tr><td>-</td><td>-</td></tr>",
            "<tr><td>---</td><td>---</td></tr>",
        ],
        "table separator detection only consumes the header separator position",
    )


def test_handle_table_keeps_separator_shaped_first_body_row() -> None:
    body: list[str] = []
    states: dict[str, object] = {"table": False, "paragraph": [], "list": False, "list_tag": ""}

    assert handle_table("| Left | Right |", body, states)
    assert handle_table("| --- | --- |", body, states)
    assert handle_table("| --- | --- |", body, states)

    assert_equal(
        body,
        [
            "<table><thead>",
            "<tr><th>Left</th><th>Right</th></tr></thead><tbody>",
            "<tr><td>---</td><td>---</td></tr>",
        ],
        "the first body row is emitted even when it looks like a Markdown separator",
    )


def test_handle_table_keeps_single_dash_first_body_row() -> None:
    body: list[str] = []
    states: dict[str, object] = {"table": False, "paragraph": [], "list": False, "list_tag": ""}

    if not handle_table("| Left | Right |", body, states):
        raise AssertionError("table header row should be handled")
    if not handle_table("| - | - |", body, states):
        raise AssertionError("single-dash placeholder row should be handled")

    assert_equal(
        body,
        [
            "<table><thead>",
            "<tr><th>Left</th><th>Right</th></tr></thead><tbody>",
            "<tr><td>-</td><td>-</td></tr>",
        ],
        "single-dash placeholder cells are emitted as data rows",
    )


def test_table_separator_requires_three_dashes_per_cell() -> None:
    assert_equal(
        is_markdown_table_separator_row([":---", "---:", ":---:"]),
        True,
        "standard alignment separator cells are recognised",
    )
    assert_equal(
        is_markdown_table_separator_row([":-", "-:"]),
        False,
        "short dash cells with alignment markers are data, not separators",
    )
    assert_equal(
        is_markdown_table_separator_row([]),
        False,
        "an empty list of cells is not a separator row",
    )


def main() -> int:
    test_split_markdown_table_row_unescapes_backslash_pairs()
    test_split_markdown_table_row_keeps_escaped_pipes_in_cell()
    test_handle_table_skips_only_header_separator_row()
    test_handle_table_keeps_separator_shaped_first_body_row()
    test_handle_table_keeps_single_dash_first_body_row()
    test_table_separator_requires_three_dashes_per_cell()
    print("report_render_blocks tests passed")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
