UnisMindMap/tests/unittest/test_heading_levels.py

55 lines
3.0 KiB
Python

"""Official 2.7.1 heading behavior; the optional six-level patch is disabled."""
import copy
import json
from types import SimpleNamespace
import unittest
from unittest.mock import MagicMock, patch
from mineru.backend.pipeline import pipeline_middle_json_mkcontent as pipeline
from mineru.backend.vlm import vlm_middle_json_mkcontent as vlm
from mineru.utils.enum_class import MakeMode
from mineru.utils import llm_aided
class HeadingLevelsTests(unittest.TestCase):
def blocks(self):
return [{'type': 'title', 'level': level, 'bbox': [0, level * 10, 100, level * 10 + 8],
'line_avg_height': 20 - level,
'lines': [{'bbox': [0, 0, 100, 8], 'spans': [{'type': 'text', 'content': f'Heading{level}'}]}]}
for level in range(1, 7)]
def pages(self):
return [{'page_idx': 0, 'page_size': [600, 800], 'para_blocks': self.blocks(), 'discarded_blocks': []}]
def test_both_output_backends_keep_the_official_four_level_limit(self):
for converter in (pipeline, vlm):
with self.subTest(converter=converter.__name__), patch.object(converter, 'detect_lang', return_value='en'):
markdown = converter.union_make(copy.deepcopy(self.pages()), MakeMode.MM_MD)
self.assertEqual(markdown.split('\n\n'), [f'{"#" * min(i, 4)} Heading{i}' for i in range(1, 7)])
content = converter.union_make(copy.deepcopy(self.pages()), MakeMode.CONTENT_LIST)
self.assertEqual([item['text_level'] for item in content], [1, 2, 3, 4, 4, 4])
content_v2 = vlm.union_make(self.pages(), MakeMode.CONTENT_LIST_V2)
self.assertEqual([item['content']['level'] for page in content_v2 for item in page], [1, 2, 3, 4, 4, 4])
def test_out_of_range_levels_still_produce_valid_markdown(self):
for converter in (pipeline, vlm):
self.assertEqual(converter.get_title_level({}), 1)
for value, expected in [(-1, 0), (0, 0), (5, 4), (6, 4), (7, 4), (99, 4)]:
self.assertEqual(converter.get_title_level({'level': value}), expected)
def test_assisted_title_prompt_keeps_the_official_four_level_limit(self):
client = MagicMock()
client.chat.completions.create.return_value = [SimpleNamespace(choices=[SimpleNamespace(
delta=SimpleNamespace(content=json.dumps({str(i): i + 1 for i in range(6)})))])]
pages = self.pages()
with patch.object(llm_aided, 'OpenAI', return_value=client), patch.object(pipeline, 'detect_lang', return_value='en'):
llm_aided.llm_aided_title(pages, {'api_key': 'unused-test-value', 'base_url': 'https://example.invalid', 'model': 'test'})
instruction = client.chat.completions.create.call_args.kwargs['messages'][0]['content']
self.assertIn('标题层级最多为4级', instruction)
self.assertNotIn('标题层级最多为6级', instruction)
self.assertEqual([block['level'] for block in pages[0]['para_blocks']], list(range(1, 7)))
if __name__ == '__main__':
unittest.main(verbosity=2)