interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / scripts / allen / science / test_tools_dispatcher.py

61 lines · 3221 bytes Raw Blame History
1import importlib.util
2import os
3from pathlib import Path
4import sys
5import unittest
6from unittest.mock import patch
7
8root=Path(os.environ.get('ALLEN_ADAPTER_DIR',str(Path(__file__).parent.parent)))
9repo=Path(os.environ.get('ALLEN_REPO_ADAPTER_DIR',str(root)))
10sys.path.insert(0,str(repo))
11def load(name,path):
12 spec=importlib.util.spec_from_file_location(name,path)
13 module=importlib.util.module_from_spec(spec)
14 spec.loader.exec_module(module)
15 return module
16science=load('dispatcher_science',root/'allen_science.py')
17previous=sys.modules.get('allen_science')
18sys.modules['allen_science']=science
19try: tools=load('gateway_tools',root/'allen_tools.py')
20finally:
21 if previous is None: del sys.modules['allen_science']
22 else: sys.modules['allen_science']=previous
23
24class ToolTurnControls(unittest.TestCase):
25 def test_fixed_registry(self):
26 names=[item['function']['name'] for item in tools.TOOLS]
27 self.assertEqual(len(names),len(set(names)))
28 self.assertEqual(set(names),set(tools.SCIENCE_NAMES)|{'repo_analyze','repo_report'})
29 for bad in ('deploy','shell','contact','submit','remember','send_mail'):
30 with self.assertRaises(ValueError): tools.owner(bad)
31 def test_repository_narrows_capabilities(self):
32 turn=tools.Turn()
33 with patch.object(tools,'call',return_value={'events':[],'result':'UNTRUSTED DATA'}) as call:
34 turn.execute('repo_analyze',{})
35 self.assertEqual({x['function']['name'] for x in turn.available()},{'repo_analyze','repo_report'})
36 for bad in ('math_compute','web_app','omega_compile','paper','deploy'):
37 with self.assertRaises(ValueError): turn.execute(bad,{})
38 self.assertEqual(call.call_count,1)
39 turn.execute('repo_report',{})
40 self.assertEqual(call.call_count,2)
41 def test_attempt_limit_includes_failures(self):
42 turn=tools.Turn()
43 with patch.object(tools,'call',side_effect=ValueError('refused')) as call:
44 for _ in range(3):
45 with self.assertRaises(ValueError): turn.execute('math_compute',{})
46 with self.assertRaises(ValueError): turn.execute('math_compute',{})
47 self.assertEqual(call.call_count,3)
48 def test_new_turn_does_not_inherit_repo_authority(self):
49 first=tools.Turn(); second=tools.Turn()
50 with patch.object(tools,'call',return_value={'events':[],'result':'data'}): first.execute('repo_analyze',{})
51 self.assertFalse(second.repo_only)
52 self.assertIn('math_compute',{x['function']['name'] for x in second.available()})
53 def test_duplicate_json_and_unknown_fields_refused(self):
54 with self.assertRaises(ValueError): tools.parse_call('{"tool":"repo_analyze","tool":"shell","arguments":{}}')
55 with self.assertRaises(ValueError): tools.parse_call('{"tool":"paper","arguments":{},"trusted":true}')
56 def test_valid_science_call_still_admitted(self):
57 name,args=tools.parse_call('{"tool":"math_compute","arguments":{"title":"Math","expression":"Sin[x]"}}')
58 self.assertEqual(name,'math_compute')
59 self.assertEqual(args['expression'],'Sin[x]')
60
61if __name__=='__main__': unittest.main(verbosity=2)