aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / allen / science / test_science_service.py
| 1 | import argparse |
| 2 | import http.client |
| 3 | import importlib.util |
| 4 | import json |
| 5 | from pathlib import Path |
| 6 | import tempfile |
| 7 | import threading |
| 8 | import unittest |
| 9 | from unittest.mock import patch |
| 10 | import allen_science as service |
| 11 | from science_protocol import content |
| 12 | |
| 13 | |
| 14 | class ServiceControls(unittest.TestCase): |
| 15 | def test_manifest_identity_bound_to_binary(self): |
| 16 | with tempfile.TemporaryDirectory() as d: |
| 17 | path=Path(d)/'engine.json' |
| 18 | with patch.object(service,'MANIFEST',path): |
| 19 | with self.assertRaises(FileNotFoundError): service.load_manifest() |
| 20 | path.write_text(json.dumps({'commit':'a'*40,'binary_sha256':'b'*64})) |
| 21 | with self.assertRaises(ValueError): service.load_manifest() |
| 22 | path.write_text(json.dumps({'commit':'a'*40,'binary_sha256':service.AIEN_MATH_PIN})) |
| 23 | self.assertEqual(service.load_manifest()['binary_sha256'],service.AIEN_MATH_PIN) |
| 24 | path.write_text(json.dumps({'commit':'main','binary_sha256':service.AIEN_MATH_PIN})) |
| 25 | with self.assertRaises(ValueError): service.load_manifest() |
| 26 | def test_unknown_tool_does_not_execute(self): |
| 27 | with patch.object(service,'run_math') as run: |
| 28 | with self.assertRaises(ValueError): service.perform('deploy',{'command':'do it'}) |
| 29 | run.assert_not_called() |
| 30 | def test_unsupported_never_claims_computed(self): |
| 31 | with patch.object(service,'run_math',return_value={'ok':False,'output':'Integrate[f[x],x]'}): |
| 32 | result=service.perform('math_compute',{'title':'Math','expression':'Integrate[f[x],x]'}) |
| 33 | update=result['events'][1]['data'] |
| 34 | self.assertEqual(update['provenance']['status'],'unsupported') |
| 35 | self.assertIn('NOT SUPPORTED',result['result']) |
| 36 | self.assertNotIn('Integrate[f[x],x]',update['content']['text']) |
| 37 | self.assertEqual(update['provenance']['result'],'Integrate[f[x],x]') |
| 38 | def test_long_result_not_silently_cut(self): |
| 39 | # Protocol/control fixture, not a math accuracy test. |
| 40 | raw='a'*8100 |
| 41 | with patch.object(service,'run_math',return_value={'ok':True,'output':raw}): |
| 42 | result=service.perform('math_compute',{'title':'Math','expression':'Expand[(a+b)^2]'}) |
| 43 | self.assertEqual(result['events'][1]['data']['content']['text'],raw) |
| 44 | self.assertEqual(result['events'][1]['data']['provenance']['result'],raw) |
| 45 | self.assertIn('exceeds the model context limit',result['result']) |
| 46 | self.assertNotIn('exact output',result['result']) |
| 47 | def test_drafts_are_not_computed(self): |
| 48 | args={'title':'Paper','markdown':'Draft contains unverified model prose','citations':[]} |
| 49 | out=service.perform('paper',args) |
| 50 | self.assertEqual(out['events'][1]['data']['provenance']['status'],'unverified') |
| 51 | self.assertEqual(out['events'][1]['data']['content']['markdown'],args['markdown']) |
| 52 | def test_unknown_content_fields_and_bad_wasm(self): |
| 53 | content('math',{'text':'symbolic result'}) |
| 54 | with self.assertRaises(ValueError): content('math',{'text':'result','html':'<script>bad</script>'}) |
| 55 | with self.assertRaises(ValueError): content('omega',{'source':'ir','language':'omega','wasm':'!!!!'}) |
| 56 | with self.assertRaises(ValueError): content('doc',{'markdown':'draft','citations':[],'title':'extra'}) |
| 57 | def test_tools_option_is_a_subset_of_the_protocol(self): |
| 58 | self.assertEqual(service.allowed_tools('math_compute'),frozenset({'math_compute'})) |
| 59 | for bad in ('','deploy','math_compute,math_compute','math_compute,'): |
| 60 | with self.assertRaises(argparse.ArgumentTypeError): service.allowed_tools(bad) |
| 61 | def test_math_only_instance_refuses_other_tools(self): |
| 62 | server=service.Server(('127.0.0.1',0),service.Handler) |
| 63 | server.tools=frozenset({'math_compute'}) |
| 64 | threading.Thread(target=server.serve_forever,daemon=True).start() |
| 65 | self.addCleanup(server.server_close) |
| 66 | self.addCleanup(server.shutdown) |
| 67 | def call(tool,arguments): |
| 68 | conn=http.client.HTTPConnection(*server.server_address,timeout=10) |
| 69 | body=json.dumps({'tool':tool,'arguments':arguments}) |
| 70 | conn.request('POST','/compute',body,{'Content-Type':'application/json'}) |
| 71 | resp=conn.getresponse() |
| 72 | return resp.status,json.loads(resp.read()) |
| 73 | with patch.object(service,'run_math',return_value={'ok':True,'output':'1/3'}) as math, \ |
| 74 | patch.object(service,'run_omega') as omega: |
| 75 | status,out=call('math_compute',{'title':'Math','expression':'Integrate[x^2,{x,0,1}]'}) |
| 76 | self.assertEqual(status,200) |
| 77 | self.assertEqual(out['events'][1]['data']['content']['text'],'1/3') |
| 78 | status,out=call('omega_compile',{'title':'Omega','source':'1 2 + .'}) |
| 79 | self.assertEqual((status,out),(403,{'error':'tool_not_allowed'})) |
| 80 | status,out=call('paper',{'title':'Paper','markdown':'draft','citations':[]}) |
| 81 | self.assertEqual((status,out),(403,{'error':'tool_not_allowed'})) |
| 82 | omega.assert_not_called() |
| 83 | self.assertEqual(math.call_count,1) |
| 84 | def test_default_instance_keeps_every_tool(self): |
| 85 | self.assertEqual(service.Server.tools,frozenset(service.TOOLS)) |
| 86 | |
| 87 | if __name__=='__main__': unittest.main(verbosity=2) |