"""左侧导航顶层菜单顺序回归测试。""" import re import unittest from html.parser import HTMLParser from pathlib import Path BASE_TEMPLATE = Path(__file__).resolve().parents[1] / 'templates' / 'base.html' class TopLevelNavParser(HTMLParser): def __init__(self): super().__init__() self.stack = [] self.active = [] self.captures = {} def handle_starttag(self, tag, attrs): classes = dict(attrs).get('class', '').split() for name in classes: match = re.fullmatch(r'nav-order-(\d+)', name) if match: capture = {'depth': len(self.stack), 'text': []} self.active.append(capture) self.captures[int(match.group(1))] = capture self.stack.append(tag) def handle_startendtag(self, tag, attrs): self.handle_starttag(tag, attrs) self.handle_endtag(tag) def handle_data(self, data): for capture in self.active: capture['text'].append(data) def handle_endtag(self, tag): depth = len(self.stack) - 1 self.active = [capture for capture in self.active if capture['depth'] != depth] if self.stack: self.stack.pop() class NavigationOrderTest(unittest.TestCase): def test_top_level_order_matches_product_requirement(self): html = BASE_TEMPLATE.read_text(encoding='utf-8') expected = [ '数据概览', '基础数据', '库存与出库', '采销协同', '新品下单', '计划预测', '现货采样管理', '订单管理', '知识库', 'AI分析', '系统设置', ] parser = TopLevelNavParser() parser.feed(html) for index, label in enumerate(expected, start=1): with self.subTest(index=index, label=label): self.assertIn(index, parser.captures) self.assertIn(label, ''.join(parser.captures[index]['text'])) def test_sidebar_uses_flex_order(self): html = BASE_TEMPLATE.read_text(encoding='utf-8') self.assertIn('flex-direction: column;', html) self.assertIn('.nav-order-11 { order: 11; }', html) self.assertNotIn('测试问题', html) if __name__ == '__main__': unittest.main()