# testcheck - 回归测试格式化输出库
# 提供 PASS/FAIL 计数、分节标题、汇总报告
import t, c
import stdio
from w32.win32console import SetConsoleOutputCP, SetConsoleCP

CP_UTF8: t.CDefine = 65001

_pass_count: t.CInt = 0
_fail_count: t.CInt = 0
_total_pass: t.CInt = 0
_total_fail: t.CInt = 0


def begin(name: str):
    """开始测试套件，打印头部并重置当前套件计数器（累计计数不变）"""
    global _pass_count, _fail_count
    _pass_count = 0
    _fail_count = 0
    SetConsoleOutputCP(CP_UTF8)
    SetConsoleCP(CP_UTF8)
    stdio.printf("=== %s ===\n\n", name)
    stdio.fflush(None)


def section(name: str):
    """打印分节标题"""
    stdio.printf("--- %s ---\n", name)
    stdio.fflush(None)


def ok(msg: str):
    """打印 PASS 并计数"""
    global _pass_count
    _pass_count += 1
    stdio.printf("PASS: %s\n", msg)
    stdio.fflush(None)


def fail(msg: str):
    """打印 FAIL 并计数"""
    global _fail_count
    _fail_count += 1
    stdio.printf("FAIL: %s\n", msg)
    stdio.fflush(None)


def check(cond: t.CInt, ok_msg: str, fail_msg: str):
    """条件检查，自动选择 PASS/FAIL"""
    if cond:
        ok(ok_msg)
    else:
        fail(fail_msg)


def info(msg: str):
    """打印信息消息（不计入 PASS/FAIL）"""
    stdio.printf("%s\n", msg)
    stdio.fflush(None)


def end() -> t.CInt:
    """打印当前套件汇总，累加到总计，返回当前套件失败数"""
    global _total_pass, _total_fail
    _total_pass += _pass_count
    _total_fail += _fail_count
    stdio.printf("\n--- Summary ---\n")
    stdio.printf("PASS: %d, FAIL: %d\n", _pass_count, _fail_count)
    stdio.fflush(None)
    return _fail_count


def summary() -> t.CInt:
    """打印所有套件的总计汇总，返回总失败数"""
    stdio.printf("\n=== Total Summary ===\n")
    stdio.printf("Total PASS: %d, Total FAIL: %d\n", _total_pass, _total_fail)
    stdio.fflush(None)
    return _total_fail
