import re, time, hmac, hashlib, base64, struct
BASE = "http://localhost:8080"
FAILS = []
BAD = re.compile(r"(Warning:|Notice:|Deprecated:|Fatal error|Something went wrong|Uncaught|Stack trace)")
def totp(secret):
    s = secret.replace(" ", "").upper(); s += "=" * (-len(s) % 8)
    h = hmac.new(base64.b32decode(s), struct.pack(">Q", int(time.time()) // 30), hashlib.sha1).digest(); o = h[19] & 15
    return "%06d" % ((struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000)
def csrf(html):
    m = re.search(r'name="_csrf" value="([^"]+)"', html); return m.group(1) if m else ""
def check(resp, label, code=200, contains=None):
    ok = resp.status_code == code; body = resp.text
    m = BAD.search(body)
    if m: ok = False; label += " | " + re.sub(r"<[^>]+>", " ", body[max(0, m.start()-200):m.start()+400])[:500]
    if contains and contains not in body: ok = False; label += f" | missing '{contains}'"
    print(("PASS " if ok else "FAIL ") + f"{resp.status_code} {label}")
    if not ok: FAILS.append(label)
    return resp
