"""Exercises the unmodified server.py POST handler in-process (the sandbox blocks sockets), from a temporary copy, and checks the app.js price formula in Python. Standard library only. Run from anywhere: python output/checks/server_check.py""" import io, json, os, shutil, tempfile, filecmp, importlib.util from email.message import Message HERE = os.path.dirname(os.path.abspath(__file__)) SITE = os.path.join(HERE, '..', 'site') ORIGINAL = os.path.join(HERE, '..', '..', 'site', 'server.py') if os.path.exists(ORIGINAL): print('output/site/server.py identical to original site/server.py:', filecmp.cmp(os.path.join(SITE, 'server.py'), ORIGINAL, shallow=False)) else: print('original site/server.py not found next to output/; identity comparison skipped') cwd = os.getcwd() tmp = tempfile.mkdtemp() shutil.copy(os.path.join(SITE, 'server.py'), tmp) spec = importlib.util.spec_from_file_location('srv', os.path.join(tmp, 'server.py')) srv = importlib.util.module_from_spec(spec); spec.loader.exec_module(srv) # chdirs into tmp def post(path, payload): body = json.dumps(payload).encode() h = srv.Handler.__new__(srv.Handler) h.rfile, h.wfile = io.BytesIO(body), io.BytesIO() h.path, h.command, h.request_version = path, 'POST', 'HTTP/1.1' h.requestline = f'POST {path} HTTP/1.1'; h.client_address = ('127.0.0.1', 0) h.close_connection = True; h.log_message = lambda *a: None m = Message(); m['Content-Length'] = str(len(body)); m['Content-Type'] = 'application/json'; h.headers = m h.do_POST() raw = h.wfile.getvalue() return raw.split(b'\r\n', 1)[0].decode(), raw.split(b'\r\n\r\n', 1)[-1].decode() def lines(): return open('submissions.jsonl').read().splitlines() if os.path.exists('submissions.jsonl') else [] failures = 0 try: cases = [ # (path, payload, expected status code, expected records added) ('/api/leads', {'name': 'Ada Tester', 'email': 'ada@example.test', 'seats': 3}, 201, 1), ('/api/lead', {'name': 'Old Path', 'email': 'old@example.test', 'seats': 1}, 404, 0), # URL used by original app.js ('/api/leads', {'name': ' ', 'email': 'x@example.test', 'seats': 1}, 400, 0), ('/api/leads', {'name': 'Bad Mail', 'email': 'not-an-email', 'seats': 1}, 400, 0), ('/api/leads', {'name': 'Zero', 'email': 'z@example.test', 'seats': 0}, 400, 0), ('/api/leads', {'name': 'Nine', 'email': 'n@example.test', 'seats': 9}, 400, 0), ('/api/leads', {'name': 'Frac', 'email': 'f@example.test', 'seats': 2.5}, 400, 0), ] for path, payload, exp_code, exp_added in cases: before = len(lines()); status, body = post(path, payload); added = len(lines()) - before ok = f' {exp_code} ' in f' {status} ' and added == exp_added failures += not ok print(f'POST {path} {payload} -> {status}; body {body[:60]!r}; records added: {added}', 'OK' if ok else 'FAIL') print('final temp submissions.jsonl:', lines()) finally: os.chdir(cwd); shutil.rmtree(tmp) def total(seats): # mirrors calculateTotal in output/site/app.js sub = seats * 240; d = 0.10 if seats >= 3 else 0 return round(sub * (1 - d) * 1.12 * 100) / 100 for s, exp in [(1, '268.80'), (2, '537.60'), (3, '725.76'), (8, '1935.36')]: ok = f'{total(s):.2f}' == exp; failures += not ok print(f'{s} seat(s): {total(s):.2f} expected {exp}', 'OK' if ok else 'FAIL') # hand arithmetic: 1: 240*1.12=268.80; 2: 480*1.12=537.60; 3: 720*0.9=648*1.12=725.76; 8: 1920*0.9=1728*1.12=1935.36 print('FAILURES:', failures) raise SystemExit(1 if failures else 0)