153 lines
8.5 KiB
Python
153 lines
8.5 KiB
Python
import copy
|
||
import json
|
||
import unittest
|
||
from types import SimpleNamespace
|
||
|
||
from system_manage.heading_structure import analyze, manual_edit, model_repair, protect_confirmed, validate_manual
|
||
|
||
|
||
class HeadingRulesTests(unittest.TestCase):
|
||
def test_flat_numbered_outline_preserves_body_media_tables_and_code(self):
|
||
body = '\nBody 42\n\n\n\n| A | B |\n|---|---|\n| 1 | 2 |\n\n```\n# 1.9 Code\n```\n'
|
||
text = '# 1 Root\n# 1.1 Section\n# 1.1.1 Item\n' + body + '# 2 End\n'
|
||
result, report = analyze(text, {'engine': 'mineru'})
|
||
self.assertIn('## 1.1 Section', result)
|
||
self.assertIn('### 1.1.1 Item', result)
|
||
self.assertIn(body, result)
|
||
self.assertEqual([r['level'] for r in report['headings']], [1, 2, 3, 1])
|
||
|
||
def test_original_markdown_and_html_levels_remain_authoritative(self):
|
||
text = '# 1 Root\n#### 1.1 Deliberate\n'
|
||
for fmt in ('md', 'html', 'docx'):
|
||
self.assertEqual(analyze(text, {'format': fmt})[0], text)
|
||
|
||
def test_fourth_to_sixth_decimal_levels_are_not_mistaken_for_ip_addresses(self):
|
||
numbers = ['1', '1.1', '1.1.1', '1.1.1.1', '1.1.1.1.1', '1.1.1.1.1.1']
|
||
text = '\n'.join('# ' + number + ' Section' for number in numbers)
|
||
_, report = analyze(text)
|
||
self.assertEqual([r['level'] for r in report['headings']], [1, 2, 3, 4, 5, 6])
|
||
|
||
def test_unstructured_chinese_enumeration_is_not_promoted_without_context(self):
|
||
text = '一、选项甲\n二、选项乙\n'
|
||
self.assertEqual(analyze(text)[0], text)
|
||
|
||
def test_identical_titles_keep_distinct_manual_locks_after_reordering(self):
|
||
text, report = analyze('# Root\n## Same\nfirst body\n## Same\nsecond body\n# Other\n', {'format': 'md'})
|
||
second = [r for r in report['headings'] if r['text'] == 'Same'][1]
|
||
target = report['headings'][-1]
|
||
output, moved = manual_edit(text, report, {'id': second['id'], 'kind': 'parent', 'parent_id': target['id']})
|
||
_, restored = analyze(output, previous=report, repair=False)
|
||
validate_manual(restored, moved)
|
||
self.assertIn(second['id'], moved['manual'])
|
||
|
||
def test_branch_moving_does_not_merge_an_unterminated_final_line(self):
|
||
text, report = analyze('# A\nbody A\n# B\nbody B', {'format': 'md'})
|
||
output, _ = manual_edit(text, report, {'id': report['headings'][0]['id'], 'kind': 'parent', 'parent_id': report['headings'][1]['id']})
|
||
self.assertIn('body B\n## A\n', output)
|
||
|
||
def test_dates_ips_versions_and_ordinary_lists_are_not_promoted(self):
|
||
text = '2026 Annual report\n\n192.168.1.1 Gateway\n\n1.0 version\n\n1. First task\n2. Second task\n\n- 1.1 List detail\n'
|
||
result, report = analyze(text)
|
||
self.assertEqual(result, text)
|
||
self.assertFalse(any(r['level'] for r in report['headings']))
|
||
|
||
def test_mixed_chinese_numbering_is_relative_to_observed_schemes(self):
|
||
text = '# 第一章 总则\n# 一、目标\n# (一)范围\n# 1 条件\n# (1)细则\n'
|
||
result, report = analyze(text)
|
||
self.assertEqual([r['level'] for r in report['headings']], [1, 2, 3, 4, 5])
|
||
|
||
def test_bookmark_requires_matching_title_and_page(self):
|
||
source = {'bookmarks': [{'text': 'Section', 'page': 1, 'level': 3}],
|
||
'blocks': [{'text': 'Section', 'page': 1, 'kind': 'title'}]}
|
||
result, report = analyze('# Section\n', source)
|
||
self.assertEqual(result, '### Section\n')
|
||
self.assertEqual(report['headings'][0]['source'], 'pdf_bookmark')
|
||
source['bookmarks'][0]['page'] = 2
|
||
self.assertEqual(analyze('# Section\n', source)[0], '# Section\n')
|
||
|
||
def test_layout_uses_document_wide_size_groups(self):
|
||
source = {'blocks': [{'text': 'Root', 'kind': 'title', 'font_size': 24},
|
||
{'text': 'Child', 'kind': 'title', 'font_size': 18}, {'text': 'Body.', 'kind': 'text', 'font_size': 10}]}
|
||
result, report = analyze('# Root\n# Child\nBody.\n', source)
|
||
self.assertEqual([r['level'] for r in report['headings']], [1, 2])
|
||
self.assertIn('## Child', result)
|
||
|
||
def test_repeated_margin_lines_are_hidden_without_deleting_source(self):
|
||
source = {'blocks': [{'text': 'Repeated header', 'page': i, 'bbox': [0, 1, 100, 9], 'page_height': 800, 'kind': 'text'} for i in range(3)]}
|
||
text = 'Repeated header\n\nRepeated header\n\nRepeated header\n'
|
||
output, report = analyze(text, source)
|
||
self.assertEqual(output, text)
|
||
self.assertEqual(report['excluded_lines'], [0, 2, 4])
|
||
|
||
def test_toc_requires_directory_context_and_does_not_hide_real_body_title(self):
|
||
text = '# Contents\nSection A .... 1\nSection B .... 3\n\n# Section A\nBody\n'
|
||
output, report = analyze(text)
|
||
self.assertEqual(output, text)
|
||
self.assertEqual(report['excluded_lines'], [1, 2])
|
||
self.assertEqual(analyze('Conclusion .... 42\n')[1]['excluded_lines'], [])
|
||
|
||
def test_manual_branch_move_preserves_text_and_locks_descendants(self):
|
||
text, report = analyze('# A\n## B\n### C\nbody42\n# D\n', {'format': 'md'})
|
||
by_text = {h['text']: h for h in report['headings']}
|
||
output, report = manual_edit(text, report, {'id': by_text['B']['id'], 'kind': 'parent', 'parent_id': by_text['D']['id']})
|
||
self.assertEqual(output, '# A\n# D\n## B\n### C\nbody42\n')
|
||
again, fresh = analyze(output, previous=report)
|
||
self.assertEqual(again, output)
|
||
self.assertEqual(len(fresh['manual']), 2)
|
||
self.assertEqual(next(h for h in fresh['headings'] if h['text'] == 'B')['id'], by_text['B']['id'])
|
||
|
||
def test_cycles_and_subtree_depth_over_six_are_rejected(self):
|
||
text, report = analyze('# A\n## B\n###### C\n', {'format': 'md'})
|
||
with self.assertRaises(ValueError):
|
||
manual_edit(text, report, {'id': report['headings'][0]['id'], 'kind': 'parent', 'parent_id': report['headings'][1]['id']})
|
||
with self.assertRaises(ValueError):
|
||
manual_edit(text, report, {'id': report['headings'][1]['id'], 'kind': 'demote'})
|
||
|
||
def test_confirmed_outline_survives_rules_and_model_repair(self):
|
||
text, report = analyze('# Root\n## Child\n', {'format': 'md'})
|
||
text, report = manual_edit(text, report, {'id': report['headings'][1]['id'], 'kind': 'confirm'})
|
||
out, fresh = model_repair(text, {}, report, lambda instruction, payload: json.dumps([
|
||
{'id': r['id'], 'level': 1} for r in json.loads(payload)['batch']]))
|
||
self.assertIn('## Child', out)
|
||
self.assertTrue(next(h for h in fresh['headings'] if h['text'] == 'Child')['confirmed'])
|
||
|
||
def test_model_only_returns_ids_and_levels_and_never_rewrites_body(self):
|
||
text = '# Root\n# Child\n# Detail\nBody 42\n\n\n'
|
||
def generate(instruction, payload):
|
||
data = json.loads(payload)
|
||
self.assertNotIn('Body 42', payload)
|
||
return json.dumps([{'id': item['id'], 'level': i + 1} for i, item in enumerate(data['batch'])])
|
||
result, report = model_repair(text, {}, {}, generate)
|
||
self.assertEqual(result, '# Root\n## Child\n### Detail\nBody 42\n\n\n')
|
||
self.assertEqual(report['model']['status'], 'applied')
|
||
|
||
def test_bad_or_failed_model_responses_fall_back_atomically(self):
|
||
text = '# Root\n# Child\nBody42\n'
|
||
responses = ['[]', '[{"id":"invented","level":2}]', 'not json']
|
||
for response in responses:
|
||
out, report = model_repair(text, {}, {}, lambda *args: response)
|
||
self.assertEqual(out, text)
|
||
self.assertEqual(report['model']['status'], 'fallback')
|
||
def failure(*args):
|
||
raise RuntimeError('secret-provider-details')
|
||
out, report = model_repair(text, {}, {}, failure)
|
||
self.assertEqual(out, text)
|
||
self.assertNotIn('secret-provider-details', json.dumps(report))
|
||
|
||
def test_wrong_manual_ids_and_modified_titles_are_not_trusted(self):
|
||
_, report = analyze('# A\n')
|
||
with self.assertRaises(ValueError):
|
||
validate_manual(report, {'manual': {'unknown': {'text': 'A', 'level': 2}}})
|
||
|
||
def test_summary_cannot_drop_confirmed_title(self):
|
||
text, report = analyze('# Root\n## Child\n', {'format': 'md'})
|
||
text, report = manual_edit(text, report, {'id': report['headings'][1]['id'], 'kind': 'confirm'})
|
||
with self.assertRaises(ValueError):
|
||
protect_confirmed(text, '# Summary\n', report)
|
||
out, _ = protect_confirmed(text, '# Root\n# Child\nSummary body\n', report)
|
||
self.assertIn('## Child', out)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
unittest.main()
|