45 lines
2.3 KiB
Python
45 lines
2.3 KiB
Python
import unittest
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
from system_manage.ai_hub_organize import organize, split_markdown, token_bound
|
|
|
|
|
|
class OrganizeTests(unittest.TestCase):
|
|
def test_split_preserves_long_unbroken_unicode_and_image_tokens(self):
|
|
text = '# 标题\n' + '超长段落42' * 8000 + '\nAIHUBIMAGE0001TOKEN\n结束'
|
|
chunks = split_markdown(text, 2048)
|
|
self.assertEqual(''.join(chunks), text)
|
|
self.assertTrue(all(token_bound(chunk) <= 2048 for chunk in chunks))
|
|
self.assertTrue(any('AIHUBIMAGE0001TOKEN' in chunk for chunk in chunks))
|
|
|
|
def test_long_document_is_organized_and_merged_with_checkpoints(self):
|
|
source = '\n'.join(f'## section {i}\n' + 'fact42 ' * 1000 for i in range(12))
|
|
stages, saved, calls = [], [], []
|
|
def invoke(prompt, content):
|
|
calls.append(content)
|
|
return SimpleNamespace(content='# Result\n- fact42', response_metadata={})
|
|
result = organize(source, '整理事实', invoke, context_tokens=8192, output_tokens=1024,
|
|
progress=stages.append, checkpoint=saved.append)
|
|
self.assertIn('fact42', result)
|
|
self.assertGreater(len(calls), 2)
|
|
self.assertGreater(len(saved), 2)
|
|
self.assertTrue(any('合并' in stage['stage'] for stage in stages))
|
|
|
|
def test_truncated_output_is_not_accepted_as_success(self):
|
|
with self.assertRaisesRegex(ValueError, '截断'):
|
|
organize('facts', '整理', lambda *args: SimpleNamespace(
|
|
content='partial', response_metadata={'finish_reason': 'length'}))
|
|
|
|
def test_failed_chunk_keeps_completed_checkpoints_and_sanitizes_provider_error(self):
|
|
saved, calls = [], []
|
|
def invoke(*args):
|
|
calls.append(1)
|
|
if len(calls) > 1:
|
|
raise RuntimeError('private credential must not escape')
|
|
return '# saved42'
|
|
with patch('system_manage.ai_hub_organize.time.sleep'):
|
|
with self.assertRaisesRegex(ValueError, '已保留') as failure:
|
|
organize('word42 ' * 5000, '整理', invoke, context_tokens=4096, output_tokens=512, checkpoint=saved.append)
|
|
self.assertNotIn('private', str(failure.exception))
|
|
self.assertEqual(saved, [['# saved42']])
|