"""工程量清单本地分析单元测试。"""
import unittest

from utils.bill_analysis import (
    analyze_boq_pages,
    filter_bill_pages,
    parse_bill_text,
)


class TestParseBillText(unittest.TestCase):
    def test_code_name_unit_qty(self):
        text = '010101001001 挖土方 m3 100.5 土壤类别:三类土'
        r = parse_bill_text(text)
        self.assertIn('categories', r)
        self.assertTrue(r['categories'])
        cat = r['categories'][0]
        self.assertEqual(cat['name'], '未分类')
        self.assertEqual(len(cat['items']), 1)
        it = cat['items'][0]
        self.assertEqual(it['code'], '010101001001')
        self.assertIn('挖土', it['name'])
        self.assertEqual(it['unit'], 'm3')
        self.assertEqual(it['quantity'], '100.5')

    def test_hierarchical_line_prefix(self):
        text = '1.1 010101001001 基础开挖 m3 50'
        r = parse_bill_text(text)
        it = r['categories'][0]['items'][0]
        self.assertEqual(it['code'], '010101001001')


class TestFilterBillPages(unittest.TestCase):
    def test_two_pages_gap_fill(self):
        p0 = '目录 前言'
        p1 = '分部分项工程量清单\n项目编码 项目名称 工程量\n010101001001 项 m3 1'
        p2 = '续表无表头\n010101002001 土 m2 2'
        p3 = '规费 税金 社会保险费 住房公积金 其他说明'
        pages, meta = filter_bill_pages([p0, p1, p2, p3])
        self.assertEqual(meta['total_pages'], 4)
        self.assertGreaterEqual(len(pages), 2)
        merged = '\n'.join(pages)
        self.assertIn('010101001001', merged)
        self.assertIn('010101002001', merged)

    def test_analyze_scanned_empty(self):
        r = analyze_boq_pages(['', '  ', ''])
        self.assertTrue(r.get('scanned'))


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