Spaces:
Sleeping
Sleeping
Download KnowledgeService/tests/test_connections_intent.py from dellweb3/workos-office: direct link, hf CLI and curl.
- Browser
- Download file 12 kB
-
https://huggingface.co/spaces/dellweb3/workos-office/resolve/main/KnowledgeService/tests/test_connections_intent.py
- Command line
-
hf download hf://spaces/dellweb3/workos-office/KnowledgeService/tests/test_connections_intent.py
-
curl -L -o test_connections_intent.py https://huggingface.co/spaces/dellweb3/workos-office/resolve/main/KnowledgeService/tests/test_connections_intent.py
12 kB
| import asyncio,json,os,tempfile,unittest,uuid | |
| from unittest.mock import patch | |
| import httpx | |
| from APIGateway.app.main import create_app | |
| from APIGateway.app.stubs import Providers | |
| from LLMGateway import LLMGateway | |
| from LLMGateway.admission import Admission,AdmissionError | |
| from ToolGateway.connections import webhook_url | |
| class ConnectionsIntentTests(unittest.IsolatedAsyncioTestCase): | |
| async def asyncSetUp(self): | |
| self.tmp=tempfile.TemporaryDirectory();self.env=patch.dict(os.environ,{'WORKOS_WORKSPACE_DATA_DIR':self.tmp.name, | |
| 'DASHSCOPE_API_KEY':'synthetic-only','DASHSCOPE_BASE_URL':'https://dashscope.aliyuncs.com/compatible-mode/v1', | |
| 'WORKOS_PUBLIC_DEMO':'0','WORKOS_EXTERNAL_SEND_ENABLED':'1','DINGTALK_WEBHOOK':'https://oapi.dingtalk.com/robot/send?access_token=synthetic-only','DINGTALK_SIGNING_SECRET':'test-signing-only'});self.env.start() | |
| self.p=Providers();self.calls=[];self.intent='conversation' | |
| def respond(request): | |
| body=json.loads(request.content);self.calls.append(body) | |
| text=json.dumps({'intent':self.intent,'reason':'测试分类'}) if '判断用户本轮意图' in body['input'][0]['content'] else '你好,可以直接自由提问。' | |
| return httpx.Response(200,json={'status':'completed','output':[{'type':'message','content':[{'type':'output_text','text':text}]}],'usage':{'input_tokens':10,'output_tokens':4}}) | |
| self.p.llm=LLMGateway(self.p.workspace.engine,mode='real',transport=httpx.MockTransport(respond)) | |
| self.c=httpx.AsyncClient(transport=httpx.ASGITransport(app=create_app(self.p)),base_url='http://testserver') | |
| async def login(name,pw):return {'Authorization':'Bearer '+(await self.c.post('/api/v1/auth/login',json={'username':name,'password':pw})).json()['access_token']} | |
| self.admin=await login('admin','admin123');self.lin=await login('lin','demo123');self.zhou=await login('zhou','demo123') | |
| self.wid=(await self.c.post('/api/v1/workspaces',headers={**self.lin,'Idempotency-Key':uuid.uuid4().hex},json={'title':'意图测试'})).json()['workspace_id'] | |
| self.awid=(await self.c.post('/api/v1/workspaces',headers={**self.admin,'Idempotency-Key':uuid.uuid4().hex},json={'title':'连接测试'})).json()['workspace_id'] | |
| async def asyncTearDown(self): | |
| await self.c.aclose();await self.p.runtime.shutdown();await self.p.llm.close();self.p.workspace.engine.dispose();self.env.stop();self.tmp.cleanup() | |
| async def chat(self,text='你好',**options): | |
| return await self.c.post('/api/v1/workspaces/'+self.wid+'/chat',headers={**self.lin,'Idempotency-Key':uuid.uuid4().hex},json={'content':text,**options}) | |
| async def test_greeting_does_not_require_stale_knowledge(self): | |
| r=await self.chat(base_ids=['kb-rules']);self.assertEqual(r.status_code,201,r.text) | |
| self.assertFalse(r.json()['citations']);self.assertEqual(r.json()['intent']['source'],'model');self.assertEqual(len(self.calls),2) | |
| self.assertNotIn('retrieved_evidence',self.calls[-1]['input'][0]['content']);self.assertIsNone(r.json()['employee']) | |
| async def test_writing_and_skill_request_never_execute_workflow(self): | |
| self.intent='writing';r=await self.chat('写一篇春天的公众号文章');self.assertEqual(r.status_code,201,r.text);self.assertFalse(r.json()['base_ids']);self.assertIsNotNone(r.json()['artifact']) | |
| self.intent='skill_build';r=await self.chat('开发一个清理空白的技能');self.assertEqual(r.json()['suggested_employee_id'],'workflow:skill-programmer') | |
| runs=(await self.c.get('/api/v1/workspaces/'+self.wid+'/runs',headers=self.lin)).json()['items'];self.assertFalse(runs) | |
| async def test_internal_query_uses_authorized_data(self): | |
| self.intent='knowledge';r=await self.chat('公文通知要求');self.assertEqual(r.status_code,201,r.text);self.assertTrue(r.json()['base_ids']);self.assertTrue(r.json()['citations']) | |
| self.intent='records';r=await self.chat('查询会议记录');self.assertEqual(r.status_code,201,r.text);self.assertIsNotNone(r.json()['records']);self.assertEqual(r.json()['model_id'],'parameterized-sql') | |
| async def test_connector_catalog_no_secrets_and_public_send_denied(self): | |
| r=await self.c.get('/api/v1/connections',headers=self.lin);self.assertEqual(r.status_code,200);self.assertNotIn('synthetic-only',r.text);self.assertNotIn('test-signing-only',r.text) | |
| self.assertEqual({c['connection_id'] for c in r.json()['items']},{'dingtalk','email','meeting'}) | |
| with patch.dict(os.environ,{'WORKOS_PUBLIC_DEMO':'1'}): | |
| r=await self.c.post('/api/v1/admin/connections/dingtalk/test',json={'workspace_id':self.awid,'confirmed':True},headers={**self.admin,'Idempotency-Key':uuid.uuid4().hex});self.assertEqual(r.status_code,403) | |
| async def test_connector_roles_confirmation_signing_and_replay(self): | |
| requests=[] | |
| def send(request):requests.append(request);return httpx.Response(200,json={'errcode':0}) | |
| self.p.tool.connections.transport=httpx.MockTransport(send);path='/api/v1/admin/connections/dingtalk/test';payload={'workspace_id':self.awid,'confirmed':True};key=uuid.uuid4().hex | |
| self.assertEqual((await self.c.post(path,json=payload,headers={**self.lin,'Idempotency-Key':key})).status_code,403) | |
| self.assertEqual((await self.c.post(path,json={**payload,'confirmed':False},headers={**self.admin,'Idempotency-Key':key})).status_code,422) | |
| self.assertEqual((await self.c.post(path,json={**payload,'workspace_id':self.wid},headers={**self.admin,'Idempotency-Key':key})).status_code,404) | |
| a=await self.c.post(path,json=payload,headers={**self.admin,'Idempotency-Key':key});self.assertEqual(a.status_code,200,a.text);self.assertEqual(a.json()['status'],'succeeded') | |
| b=await self.c.post(path,json=payload,headers={**self.admin,'Idempotency-Key':key});self.assertTrue(b.json()['replayed']);self.assertEqual(len(requests),1) | |
| self.assertEqual(requests[0].url.params['access_token'],'synthetic-only');self.assertIn('sign',requests[0].url.params);self.assertEqual(json.loads(requests[0].content)['text']['content'],'Work OS 连接测试') | |
| async def test_provider_business_error_is_persisted_and_not_resent(self): | |
| count=[] | |
| def send(request):count.append(request);return httpx.Response(200,json={'errcode':310000,'errmsg':'test rejection'}) | |
| self.p.tool.connections.transport=httpx.MockTransport(send);key=uuid.uuid4().hex | |
| for _ in range(2): | |
| r=await self.c.post('/api/v1/admin/connections/dingtalk/test',json={'workspace_id':self.awid,'confirmed':True},headers={**self.admin,'Idempotency-Key':key}) | |
| self.assertEqual(r.json()['status'],'failed');self.assertEqual(r.json()['error']['code'],'CONNECTION_PROVIDER_REJECTED') | |
| self.assertEqual(len(count),1) | |
| async def test_unknown_connector_rejected_before_model(self): | |
| r=await self.chat(connection_ids=['arbitrary-url']);self.assertEqual(r.status_code,422);self.assertFalse(self.calls) | |
| for url in ['http://oapi.dingtalk.com/robot/send?access_token=x','https://evil.example/robot/send?access_token=x','https://oapi.dingtalk.com/robot/send?access_token=x&url=y']: | |
| with self.assertRaises(Exception):webhook_url(url) | |
| async def test_publication_requires_independent_role_hash_and_revision(self): | |
| from CollaborationService.models import Program | |
| from datetime import datetime,timezone | |
| pid='program-'+uuid.uuid4().hex | |
| with self.p.workspace.Session.begin() as db: | |
| db.add(Program(id=pid,tenant='demo-tenant',owner='u-lin',workspace=self.wid,request_key=pid,fingerprint='x'*64,name='清理',description='测试技能', | |
| code='def run(data):\n return {"text": data.get("text", "").strip()}',tests=[{'input':{'text':' x '},'expected':{'text':'x'}},{'input':{},'expected':{'text':''}}], | |
| report={'passed':True},status='tested',revision=1,created_at=datetime.now(timezone.utc).isoformat())) | |
| path='/api/v1/admin/skill-programs/'+pid+'/approve' | |
| r=await self.c.post('/api/v1/skill-programs/'+pid+'/publish',headers=self.lin,json={'expected_revision':1});self.assertEqual(r.json()['status'],'pending_review') | |
| self.assertEqual((await self.c.post(path,headers=self.lin,json={'expected_revision':2})).status_code,403) | |
| with self.assertRaises(Exception) as self_approval:self.p.programs.approve({'tenant_id':'demo-tenant','user_id':'u-lin','roles':['admin']},pid,2) | |
| self.assertEqual(self_approval.exception.code,'INDEPENDENT_REVIEW_REQUIRED') | |
| self.assertEqual((await self.c.post(path,headers=self.admin,json={'expected_revision':1})).status_code,409) | |
| with self.p.workspace.Session.begin() as db:db.get(Program,pid).code='def run(data):\n return {}' | |
| self.assertEqual((await self.c.post(path,headers=self.admin,json={'expected_revision':2})).json()['code'],'SKILL_CODE_CHANGED') | |
| with self.p.workspace.Session.begin() as db:db.get(Program,pid).code='def run(data):\n return {"text": data.get("text", "").strip()}' | |
| approved=await self.c.post(path,headers=self.admin,json={'expected_revision':2});self.assertEqual(approved.status_code,200,approved.text);self.assertEqual(approved.json()['status'],'published') | |
| self.assertEqual(approved.json()['report']['review']['approved_by'],'u-admin') | |
| zwid=(await self.c.post('/api/v1/workspaces',headers={**self.zhou,'Idempotency-Key':uuid.uuid4().hex},json={'title':'共享技能复用'})).json()['workspace_id'] | |
| result=await self.c.post('/api/v1/skill-programs/'+pid+'/test',headers=self.zhou,json={'input':{'text':' y '},'workspace_id':zwid}) | |
| self.assertEqual(result.status_code,200,result.text);self.assertEqual(result.json()['result'],{'text':'y'}) | |
| async def test_selected_unauthorized_library_cannot_be_skipped(self): | |
| r=await self.chat('你好',base_ids=['private-unknown']);self.assertEqual(r.status_code,404);self.assertFalse(self.calls) | |
| class AdmissionTests(unittest.IsolatedAsyncioTestCase): | |
| async def test_per_user_limit_and_cancellation_release(self): | |
| gate=Admission(capacity=2,per_user=1,queue_limit=2);inside=[];release=asyncio.Event() | |
| async def task(user): | |
| async with gate.slot(user,'light'):inside.append(user);await release.wait() | |
| a=asyncio.create_task(task('a'));await asyncio.sleep(0);queued=asyncio.create_task(task('a'));b=asyncio.create_task(task('b'));await asyncio.sleep(.01) | |
| self.assertEqual(inside,['a','b']);self.assertEqual(gate.active,2);queued.cancel();await asyncio.gather(queued,return_exceptions=True);self.assertEqual(len(gate.waiting),0) | |
| release.set();await asyncio.gather(a,b);self.assertEqual(gate.active,0) | |
| async def test_queue_full_and_timeout(self): | |
| gate=Admission(capacity=1,per_user=1,queue_limit=1,wait_seconds=1) | |
| async with gate.slot('a','light'): | |
| async def waiting(): | |
| async with gate.slot('b','heavy'):pass | |
| task=asyncio.create_task(waiting());await asyncio.sleep(.01) | |
| with self.assertRaises(AdmissionError) as full: | |
| async with gate.slot('c','standard'):pass | |
| self.assertEqual(full.exception.code,'LLM_QUEUE_FULL') | |
| with self.assertRaises(AdmissionError) as timeout:await task | |
| self.assertEqual(timeout.exception.code,'LLM_QUEUE_TIMEOUT') | |
| self.assertEqual(gate.active,0);self.assertFalse(gate.waiting) | |
| async def test_account_hourly_cap(self): | |
| gate=Admission(hourly_limit=1) | |
| async with gate.slot('a','light'):pass | |
| with self.assertRaises(AdmissionError) as quota: | |
| async with gate.slot('b','standard'):pass | |
| self.assertEqual(quota.exception.code,'LLM_QUOTA_EXCEEDED') | |