"""Local stack integration checks; creates and retains clearly named test orders. Run: python3 -m local.smoke_test. Standard library only. Honors .env/environment. """ from concurrent.futures import ThreadPoolExecutor import hashlib import http.cookiejar import json import os from pathlib import Path import time from urllib.error import HTTPError from urllib.request import build_opener, HTTPCookieProcessor, Request, urlopen from uuid import uuid4 if Path('.env').exists(): for line in Path('.env').read_text().splitlines(): if line.strip() and not line.startswith('#') and '=' in line: key,value=line.split('=',1) os.environ.setdefault(key,value) BASE='http://localhost:'+os.environ.get('SITE_PORT','8080') class Client: def __init__(self): self.jar=http.cookiejar.CookieJar() self.opener=build_opener(HTTPCookieProcessor(self.jar)) self.operator_client=None def call(self,path,body=None,operator=False,expected=200): headers={'Content-Type':'application/json'} if operator: if self.operator_client is None: self.operator_client=Client() self.operator_client.call('/operator/login',{'username':os.environ.get('OPERATOR_USER','operator'),'password':os.environ.get('OPERATOR_PASSWORD','local-operator-only')}) return self.operator_client.call(path,body,expected=expected) request=Request(BASE+'/api'+path,data=None if body is None else json.dumps(body).encode(),headers=headers) try: with self.opener.open(request,timeout=30) as response: assert response.status==expected,(path,response.status,expected) return json.load(response) except HTTPError as exc: if exc.code!=expected:raise AssertionError((path,exc.code,exc.read().decode())) from exc return json.load(exc) def wait_scan(client, uid, operator=False, expected='clean'): prefix='/operator/uploads' if operator else '/uploads' deadline=time.monotonic()+150 while time.monotonic()