aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / allen / test_math.py
| 1 | """In-process checks for allen_math (POST /allen/api/math) against a stub science service.""" |
| 2 | import json |
| 3 | import os |
| 4 | import sys |
| 5 | import threading |
| 6 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 7 | |
| 8 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 9 | import allen_math # noqa: E402 |
| 10 | |
| 11 | REPLY = {"events": [{"event": "pane.open", "data": {"v": 1, "id": "p_1", "kind": "math", "title": "t"}}, |
| 12 | {"event": "pane.update", "data": {"v": 1, "id": "p_1", "revision": 1, "content": {"text": "1/3"}, |
| 13 | "provenance": {"status": "computed"}}}], |
| 14 | "result": "aien-math exact output (not a proof of correctness): 1/3"} |
| 15 | seen = [] |
| 16 | mode = {"reply": REPLY} |
| 17 | |
| 18 | |
| 19 | class Science(BaseHTTPRequestHandler): |
| 20 | def do_POST(self): |
| 21 | seen.append(json.loads(self.rfile.read(int(self.headers["Content-Length"])))) |
| 22 | body = json.dumps(mode["reply"]).encode() |
| 23 | self.send_response(200) |
| 24 | self.send_header("Content-Length", str(len(body))) |
| 25 | self.end_headers() |
| 26 | self.wfile.write(body) |
| 27 | |
| 28 | def log_message(self, *_): |
| 29 | pass |
| 30 | |
| 31 | |
| 32 | class Buckets: |
| 33 | def __init__(self, capacity, refill_s): |
| 34 | self.left = capacity |
| 35 | |
| 36 | def take(self, key): |
| 37 | self.left -= 1 |
| 38 | return 0 if self.left >= 0 else 5 |
| 39 | |
| 40 | def sweep(self): |
| 41 | pass |
| 42 | |
| 43 | |
| 44 | class Stats: |
| 45 | def add(self, _): |
| 46 | pass |
| 47 | |
| 48 | |
| 49 | class App: |
| 50 | stats = Stats() |
| 51 | |
| 52 | def client_key(self, _): |
| 53 | return "k" |
| 54 | |
| 55 | |
| 56 | class H: |
| 57 | def __init__(self, app, req): |
| 58 | self.app, self.req, self.out = app, req, None |
| 59 | |
| 60 | def read_json(self): |
| 61 | return self.req |
| 62 | |
| 63 | def refuse(self, code, err, msg, retry=None): |
| 64 | self.out = (code, {"error": err, "message": msg}) |
| 65 | |
| 66 | def send_json(self, code, obj, extra=None): |
| 67 | self.out = (code, obj) |
| 68 | |
| 69 | |
| 70 | def main(): |
| 71 | srv = ThreadingHTTPServer(("127.0.0.1", 0), Science) |
| 72 | threading.Thread(target=srv.serve_forever, daemon=True).start() |
| 73 | allen_math.SCIENCE = ("127.0.0.1", srv.server_address[1]) |
| 74 | app = App() |
| 75 | allen_math.init(app, Buckets) |
| 76 | fails = 0 |
| 77 | |
| 78 | def check(name, ok): |
| 79 | nonlocal fails |
| 80 | print(("PASS " if ok else "FAIL ") + name) |
| 81 | fails += not ok |
| 82 | |
| 83 | h = H(app, {"expression": " Integrate[x^2,{x,0,1}] ", "title": "Area"}) |
| 84 | allen_math.post(h) |
| 85 | check("computes through math_compute", h.out == (200, REPLY) and seen[-1] == { |
| 86 | "tool": "math_compute", "arguments": {"title": "Area", "expression": "Integrate[x^2,{x,0,1}]"}}) |
| 87 | for bad in ({"expression": ""}, {"expression": 3}, {"expression": "1", "x": 1}, ["1"], {"expression": "a\x00"}): |
| 88 | h = H(app, bad) |
| 89 | allen_math.post(h) |
| 90 | check("refuses %r" % (bad,), h.out[0] == 400) |
| 91 | h = H(app, {"expression": "1+" * 3000}) |
| 92 | allen_math.post(h) |
| 93 | check("refuses an over-long expression", h.out[0] == 413) |
| 94 | mode["reply"] = {"events": [{"event": "shell", "data": {}}], "result": ""} |
| 95 | h = H(app, {"expression": "1+1"}) |
| 96 | allen_math.post(h) |
| 97 | check("refuses an unapproved event from the service", h.out[0] == 503) |
| 98 | mode["reply"] = REPLY |
| 99 | for _ in range(allen_math.PER_MINUTE): |
| 100 | allen_math.post(H(app, {"expression": "1+1"})) |
| 101 | h = H(app, {"expression": "1+1"}) |
| 102 | allen_math.post(h) |
| 103 | check("rate limits per visitor", h.out[0] == 429) |
| 104 | srv.shutdown() |
| 105 | allen_math.SCIENCE = ("127.0.0.1", 9) |
| 106 | app2 = App() |
| 107 | allen_math.init(app2, Buckets) |
| 108 | h = H(app2, {"expression": "1+1"}) |
| 109 | allen_math.post(h) |
| 110 | check("a missing service is 503, not a crash", h.out[0] == 503) |
| 111 | sys.exit(1 if fails else 0) |
| 112 | |
| 113 | |
| 114 | if __name__ == "__main__": |
| 115 | main() |