"""Run with: python3 output/checks.py (or python3 checks.py from output/). Standard library only. All handler writes are confined to a temporary directory. Never archives, truncates or removes the runnable site's submissions.jsonl. This tests Handler.do_POST in-process, NOT a live HTTP server or browser. """ import hashlib import io import json import os from decimal import Decimal from pathlib import Path import tempfile out = Path(__file__).resolve().parent evidence = out / 'evidence' evidence.mkdir(exist_ok=True) server = out / 'site/server.py' manifest = json.loads((evidence / 'original-submission-manifest.json').read_text()) assert hashlib.sha256(server.read_bytes()).hexdigest() == manifest['site/server.py'] # The original snapshot's server was verified byte-identical to the supplied input. original = out.parent / 'site/server.py' if original.exists(): assert original.read_bytes() == server.read_bytes() live_log = out / 'site/submissions.jsonl' live_before = live_log.read_bytes() if live_log.exists() else None starting_cwd = Path.cwd() namespace = {'__file__': str(server), '__name__': 'server_under_test'} try: exec(compile(server.read_text(), str(server), 'exec'), namespace) finally: os.chdir(starting_cwd) Handler = namespace['Handler'] results = [] with tempfile.TemporaryDirectory(dir=evidence) as tmp: try: os.chdir(tmp) def records(): p = Path('submissions.jsonl') return [json.loads(line) for line in p.read_text().splitlines()] if p.exists() else [] def request(label, data, expected): body = json.dumps(data).encode() handler = Handler.__new__(Handler) handler.path = '/api/leads' handler.headers = {'Content-Length': str(len(body))} handler.rfile = io.BytesIO(body) handler.wfile = io.BytesIO() codes = [] handler.send_response = lambda code: codes.append(code) handler.send_header = lambda *args: None handler.end_headers = lambda: None before = records() handler.do_POST() after = records() response = json.loads(handler.wfile.getvalue()) assert codes == [expected], (label, codes) if expected == 201: assert after == before + [data], label assert response == {'ok': True}, label else: assert after == before, label assert response == {'error': 'Check name, email and seats (1-8).'}, label results.append({'case': label, 'status': codes[0], 'records_added': len(after)-len(before), 'response': response}) valid = {'name': 'Server Tester', 'email': 'server@example.test', 'seats': 1} for seats in [1, 2, 3, 8]: request('valid seats ' + str(seats), dict(valid, seats=seats), 201) for seats in ['', 0, -1, 1.5, 9, None, True, '3']: request('invalid seats ' + repr(seats), dict(valid, seats=seats), 400) for name in ['', ' ', None]: request('invalid name ' + repr(name), dict(valid, name=name), 400) for email in ['', 'not-an-email', 'tester@example', None]: request('invalid email ' + repr(email), dict(valid, email=email), 400) request('non-object JSON', [], 400) finally: os.chdir(starting_cwd) # Independent arithmetic cross-check, not execution of the JavaScript calculator. calculations = [] for seats, expected in [(1, '268.80'), (2, '537.60'), (3, '725.76'), (8, '1935.36')]: subtotal = Decimal(seats) * Decimal('240') after_discount = subtotal * (Decimal('.90') if seats >= 3 else Decimal('1')) total = format(after_discount * Decimal('1.12'), '.2f') assert total == expected calculations.append({'seats': seats, 'expected': expected, 'calculated': total}) live_after = live_log.read_bytes() if live_log.exists() else None assert live_before == live_after, 'Live records unexpectedly changed' report = { 'server_matches_original_snapshot': True, 'server_matches_supplied_input': True if original.exists() else 'Input not present; verified snapshot hash instead', 'server_handler_tests': results, 'independent_decimal_calculations': calculations, 'live_submissions_unchanged': True, 'note': 'In-process handler checks, not live HTTP. Decimal checks are independent arithmetic, not browser/JavaScript execution.' } (evidence / 'review-server-checks.json').write_text(json.dumps(report, indent=2) + '\n') print(json.dumps({'handler_tests_passed': len(results), 'decimal_examples_passed': len(calculations), 'live_submissions_unchanged': True}, indent=2))