File size: 2,172 Bytes
99caad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import unittest
from unittest.mock import Mock
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from ContextService.service import ContextService
from ContextService.models import Snapshot
from APIGateway.app.errors import GatewayError


class LongContextTests(unittest.TestCase):
    def setUp(self):
        self.engine=create_engine('sqlite://')
        Snapshot.__table__.create(self.engine)
        self.context=ContextService.__new__(ContextService)
        self.context.Session=sessionmaker(self.engine)
        self.context.workspace=Mock()
        self.context.knowledge=Mock()
        self.user={'tenant_id':'test','user_id':'owner'}
        self.context.summary=Mock(return_value={'personal_context':[],
            'confirmed_facts':{'description':'长事实'*10000},'facts':[]})

    def tearDown(self):
        self.engine.dispose()

    def test_long_evidence_and_messages_preserve_their_ends(self):
        text='会议记录'*40000+'原文末尾'
        messages=[{'role':'user','content':'先前消息'*6000}, {'role':'user','content':text}]
        self.context.workspace.recent_messages.return_value=messages
        self.context.knowledge.citation.return_value={'citation_id':'c1','snippet':text}
        options={'task':'meeting','citations':[{'citation_id':'c1','label':'K1'}]}
        value=self.context.build(self.user,'workspace',**options)
        self.assertEqual(value['messages'],messages)
        self.assertEqual(value['citations'][0]['snippet'],text)
        self.assertGreater(value['used_chars'],16000)
        self.assertIsNone(value['budget_chars'])
        self.assertEqual(value['omitted_message_count'],0)
        self.assertEqual(value['snapshot_id'],self.context.build(self.user,'workspace',**options)['snapshot_id'])
        self.context.knowledge.citation.assert_called_with(self.user,'c1','workspace')

    def test_unauthorized_evidence_is_still_rejected(self):
        self.context.knowledge.citation.side_effect=GatewayError(404,'NOT_FOUND','无权读取')
        with self.assertRaises(GatewayError):
            self.context.build(self.user,'workspace',citations=[{'citation_id':'private'}])