"""目录号格式化与大纲带号写回。"""
import os
import sys
import unittest

_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if _ROOT not in sys.path:
    sys.path.insert(0, _ROOT)

from modules.generator import _parse_outline, _sections_to_outline_text  # noqa: E402
from utils.outline_numbering import format_heading_display, int_to_chinese_numeral  # noqa: E402


class TestOutlineNumbering(unittest.TestCase):
    def test_int_to_chinese(self):
        self.assertEqual(int_to_chinese_numeral(1), "一")
        self.assertEqual(int_to_chinese_numeral(10), "十")
        self.assertEqual(int_to_chinese_numeral(11), "十一")
        self.assertEqual(int_to_chinese_numeral(23), "二十三")

    def test_format_heading(self):
        self.assertEqual(format_heading_display(1, "3", "总体"), "三、总体")
        self.assertEqual(format_heading_display(2, "1.2", "子节"), "1.2 子节")

    def test_sections_to_outline_text_has_numbers(self):
        sections = [
            {"level": 1, "title": "第一章", "number": "1"},
            {"level": 2, "title": "小节", "number": "1.1"},
        ]
        text = _sections_to_outline_text("某项目技术标书", sections)
        self.assertIn("某项目技术标书", text)
        self.assertIn("一、第一章", text)
        self.assertIn("1.1 小节", text)

    def test_parse_roundtrip_numbered_outline(self):
        raw = "标书标题\n一、第一章\n1.1 节A\n"
        _, sections, normalized = _parse_outline(raw)
        self.assertGreaterEqual(len(sections), 2)
        self.assertIn("一、第一章", normalized)
        self.assertIn("1.1 节A", normalized)


if __name__ == "__main__":
    unittest.main()
