"""Phase 5 end-to-end: AI deterioration risk (model, reasons, trend, alert, feedback, external model + fallback, dataset export),
anomaly detection (implausible, jump, flatline, unusual-for-patient, 3-day shift), AI summary (rules engine, de-identified),
medicines (schedule, dispenser + smart-inhaler API, nurse + portal confirmation, missed-dose worker, rescue overuse, adherence),
pilot hardening (security headers, health endpoint, system health page, backup + restore check, security self-check)."""
import json, os, re, subprocess, sys, threading, time, http.server, datetime as _dt
import requests
sys.path.insert(0, "/home/claude/tests")
from e2e_lib import check, csrf, totp, FAILS, BASE

PLAT = "/home/claude/carepulse/platform"
sql = lambda q, db="cp_client_demo": subprocess.check_output(["mysql", "-uroot", "-N", db, "-e", "SET time_zone='+05:30'; " + q]).decode().strip()
csql = lambda q: sql(q, "carepulse_central")
def ok(cond, label):
    print(("PASS " if cond else "FAIL ") + label)
    if not cond: FAILS.append(label)
def php(code):
    return subprocess.run(["php", "-r", "require 'app/bootstrap.php'; Tenant::use(Tenant::byCode('DEMO')); " + code], cwd=PLAT, capture_output=True, text=True).stdout.strip()
def worker(*a):
    return subprocess.run(["php", "cron/worker.php", "--client=DEMO", *a], cwd=PLAT, capture_output=True, text=True).stdout
IST = _dt.timezone(_dt.timedelta(hours=5, minutes=30))
iso = lambda secs_ago=0: (_dt.datetime.now(IST) - _dt.timedelta(seconds=secs_ago)).isoformat(timespec="seconds")

# ---------------- mock external risk model ----------------
MODEL = {"mode": "ok", "calls": 0}
class Model(http.server.BaseHTTPRequestHandler):
    def log_message(self, *a): pass
    def do_POST(self):
        n = int(self.headers.get("Content-Length", 0)); body = json.loads(self.rfile.read(n)); MODEL["calls"] += 1; MODEL["last"] = body; MODEL["auth"] = self.headers.get("Authorization")
        if MODEL["mode"] == "down": self.send_response(503); self.end_headers(); return
        self.send_response(200); self.send_header("Content-Type", "application/json"); self.end_headers()
        self.wfile.write(json.dumps({"score": 42.4, "model": "hosp-xgb-2", "factors": [{"code": "m1", "text": "Hospital model factor", "points": 1.1}]}).encode())
threading.Thread(target=http.server.ThreadingHTTPServer(("127.0.0.1", 9096), Model).serve_forever, daemon=True).start()

# ---------------- sign in ----------------
c = requests.Session()
r = c.get(BASE + "/login")
r = c.post(BASE + "/login", data={"_csrf": csrf(r.text), "client_code": "DEMO", "email": "admin@demo.demo", "password": "Demo@12345"})
if "Set up 2-step" in r.text:
    sec = re.search(r'class="code">([A-Z2-7 ]+)<', r.text).group(1)
    r = c.post(BASE + "/account/2fa", data={"_csrf": csrf(r.text), "code": totp(sec)})
elif "2-step sign-in" in r.text:
    sec = sql("SELECT totp_secret FROM users WHERE email='admin@demo.demo'")
    r = c.post(BASE + "/login/2fa", data={"_csrf": csrf(r.text), "code": totp(sec)})
check(r, "admin signed in", contains="Monitoring dashboard")

sunil = sql("SELECT id FROM patients WHERE name='Sunil Naik'"); ramesh = sql("SELECT id FROM patients WHERE name='Ramesh Kulkarni'")
ansari = sql("SELECT id FROM patients WHERE name='Mohammed Ansari'"); lakshmi = sql("SELECT id FROM patients WHERE name='Lakshmi Iyer'")
gopal = sql("SELECT id FROM patients WHERE name='Gopal Reddy'")

# ---------------- pages ----------------
for path, txt in [("/ai", "Sunil Naik"), ("/ai?setting=home", "AI early warning"), (f"/patients/{sunil}/ai", "Why"), (f"/patients/{sunil}", "AI early warning"),
                  ("/medication", "Mohammed Ansari"), (f"/patients/{ramesh}/medicines", "Metoprolol"), (f"/patients/{ansari}/medicines", "Rescue inhaler"),
                  ("/settings?tab=ai", "cp-risk-1"), ("/deterioration", "AI risk"), ("/dashboard", "Monitoring dashboard"), ("/station", "Central"),
                  ("/alerts", "AI early warning"), ("/analytics", "AI risk alerts")]:
    check(c.get(BASE + path), "page " + path, contains=txt)

# ---------------- risk model ----------------
f = json.loads(sql(f"SELECT factors FROM risk_scores WHERE patient_id={sunil} ORDER BY id DESC LIMIT 1"))
codes = [x["code"] for x in f]
ok(all(k in codes for k in ("news2", "news2_rising", "shock_index")), f"deteriorating ward patient: reasons {codes[:6]}")
hist = [int(x) for x in sql(f"SELECT score FROM risk_scores WHERE patient_id={sunil} ORDER BY computed_at").split()]
ok(len(hist) >= 15 and hist[0] < 30 and hist[-1] >= 60, f"risk history rises over 8 h ({hist[0]} → {hist[-1]})")
first_high = sql(f"SELECT MIN(computed_at) FROM risk_scores WHERE patient_id={sunil} AND score >= 60")
first_n7 = sql(f"SELECT MIN(computed_at) FROM news2_scores WHERE patient_id={sunil} AND score >= 7")
ok(first_high != "NULL" and first_n7 != "NULL" and first_high <= first_n7, f"AI 'high' ({first_high[11:16]}) no later than NEWS2 ≥7 ({first_n7[11:16]})")
ok(int(php("echo Ai::model(['setting'=>'hospital','age'=>40,'latest'=>['hr'=>72,'rr'=>14,'spo2'=>98,'bp_sys'=>122,'temp'=>36.8],'slope'=>[],'baseline'=>[],'news2'=>0,'news2_prev'=>0,'critical_alerts_24h'=>0,'heart_failure'=>false])[0];")) <= 5, "stable patient scores ≤ 5")
ok(php("echo Ai::level(64), Ai::level(35), Ai::level(10);") == "highwatchlow", "levels from settings (60 / 30)")
ok(sql(f"SELECT COUNT(*) FROM alerts WHERE patient_id={sunil} AND alert_type='ai_risk'") == "1", "one AI alert per patient (no duplicates)")
aid = sql(f"SELECT id FROM alerts WHERE patient_id={sunil} AND alert_type='ai_risk'")
r = c.get(BASE + f"/alerts/{aid}"); check(r, "AI alert page shows feedback", contains="Was this AI warning useful")
c.post(BASE + f"/alerts/{aid}/feedback", data={"_csrf": csrf(r.text), "feedback": "not_useful", "feedback_note": "already on sepsis bundle"})
ok(sql(f"SELECT CONCAT(feedback,':',feedback_note) FROM alerts WHERE id={aid}") == "not_useful:already on sepsis bundle", "feedback stored with note")
ok("not useful" in c.get(BASE + "/ai").text, "watchlist shows feedback counts")
# close when risk falls: simulate recovery readings
php("Tenant::setSetting('news2_enabled', '1');")
php(f"$db = DB::tenant(); foreach (['hr'=>78,'rr'=>15,'spo2'=>97,'bp_sys'=>124,'temp'=>36.9,'bp_dia'=>76] as $c=>$v) $db->insert('readings', ['patient_id'=>{sunil},'param_code'=>$c,'value'=>$v,'unit'=>Params::unit($c),'measured_at'=>now(),'source'=>'device','status'=>'valid']); News2::compute({sunil}); Ai::score({sunil});")
ok(sql(f"SELECT status FROM alerts WHERE id={aid}") in ("closed", "acknowledged"), "risk recalculated after recovery")
ok(int(sql(f"SELECT score FROM risk_scores WHERE patient_id={sunil} ORDER BY id DESC LIMIT 1")) < 60, "score falls after vitals recover")
# external model + fallback
r = c.get(BASE + "/settings?tab=ai")
form = {"_csrf": csrf(r.text), "tab": "ai", "ai_enabled": "1", "ai_alerts": "1", "ai_watch": "30", "ai_high": "60", "ai_critical": "85", "ai_model_url": "http://127.0.0.1:9096/score", "ai_model_key": "mk-1",
        "ai_anomaly": "1", "ai_anomaly_alerts": "1", "ai_z": "3.5", "ai_summary": "1", "med_missed_alerts": "1", "med_missed_alert_after": "1", "med_rescue_puffs": "8", "med_whatsapp_reminders": "1"}
check(c.post(BASE + "/settings?tab=ai", data=form), "AI settings saved", contains="aved")
out = php(f"echo json_encode(Ai::score({gopal}));")
ok('"hosp-xgb-2"' in out and '"score":42' in out and MODEL["auth"] == "Bearer mk-1" and "features" in MODEL["last"], "hospital model used (features posted with key)")
MODEL["mode"] = "down"
out = php(f"echo json_encode(Ai::score({gopal}));")
ok('"cp-risk-1"' in out, "model down → falls back to built-in cp-risk-1")
form["ai_model_url"] = ""; c.post(BASE + "/settings?tab=ai", data={**form, "_csrf": csrf(c.get(BASE + "/settings?tab=ai").text)})
r = c.get(BASE + "/ai/export.csv?days=30")
ok("text/csv" in r.headers.get("Content-Type", "") and "outcome_critical_24h" in r.text and "Sunil" not in r.text and "IP880601" not in r.text, "dataset CSV export, no identifiers")

# ---------------- anomalies ----------------
r = c.get(BASE + "/gateways"); r = c.post(BASE + "/gateways/new", data={"_csrf": csrf(r.text), "gw_type": "api", "name": "QA API P5"})
tok = re.search(r'class="code" style="font-size:14px">([^<]+)<', r.text)
H = {"Authorization": "Bearer " + (tok.group(1) if tok else "")}
ok(tok is not None, "API key created")
R = lambda body: requests.post(BASE + "/api/v1/readings", headers=H, json=body).json()
R({"device": {"serial": "QA-HR-77"}, "patient": {"id": int(lakshmi)}, "readings": [{"param": "hr", "value": 280, "measured_at": iso(60)}]})
R({"device": {"serial": "QA-HR-77"}, "patient": {"id": int(lakshmi)}, "readings": [{"param": "hr", "value": 72, "measured_at": iso(240)}, {"param": "hr", "value": 140, "measured_at": iso(200)}]})
php("Tenant::setSetting('cron_last_ai', '0');"); worker()
kinds = sql("SELECT GROUP_CONCAT(DISTINCT kind) FROM anomalies WHERE device_id=(SELECT id FROM devices WHERE serial_no='QA-HR-77')")
ok("jump" in kinds and "implausible" in kinds, f"impossible value and sudden jump flagged ({kinds})")
ok(sql("SELECT COUNT(*) FROM anomalies WHERE kind='flatline' AND patient_id=(SELECT id FROM patients WHERE uhid='IP880602')") == "1", "stuck SpO2 probe flagged (demo)")
ok(sql("SELECT severity FROM alerts WHERE alert_type='anomaly' AND source_code='ANOM:flatline'") == "info", "sensor problems are info-level (no paging)")
# unusual-for-patient: Lakshmi's own glucose baseline, two high-but-within-limit values
base = php(f"echo json_encode(Ai::baseline({lakshmi}, 'pulse'));")
med = json.loads(base)[0]
if med:
    v = round(med + 26)
    for k in (2, 1):
        R({"device": {"serial": "QA-OX-LK"}, "patient": {"id": int(lakshmi)}, "readings": [{"param": "pulse", "value": v, "measured_at": iso(k * 600)}]})
    php("Tenant::setSetting('cron_last_ai', '0');"); worker()
    ok(sql(f"SELECT COUNT(*) FROM anomalies WHERE patient_id={lakshmi} AND kind='outlier'") >= "1", f"pulse {v} unusual for patient (usual {med:.0f})")
    ok(sql(f"SELECT COUNT(*) FROM alerts WHERE patient_id={lakshmi} AND alert_type='anomaly' AND device_id IS NULL") == "1", "repeated unusual readings → one care-team alert")
else:
    ok(False, "no baseline for Lakshmi pulse")
ok(sql(f"SELECT COUNT(*) FROM anomalies WHERE patient_id={gopal} AND kind='shift'") >= "1", "3-day weight shift for heart-failure patient")

# ---------------- AI summary ----------------
r = c.post(BASE + f"/patients/{ramesh}/ai-summary", data={"_csrf": csrf(c.get(BASE + f"/patients/{ramesh}/ai").text), "days": "7"}).json()
ok(r.get("ok") and r.get("engine") == "rules" and "doses taken" in r["text"] and "Ramesh" not in r["text"], "rule-based summary (no key): readings, adherence, no name")
facts = php(f"echo json_encode(Ai::facts({ramesh}));")
ok("Sunil" not in php(f"echo json_encode(Ai::facts({sunil}));"), "alert texts in facts have the name removed")
ok("Ramesh" not in facts and "9820000000" not in facts and "UH2401" not in facts and '"age"' in facts, "facts sent to the LLM are de-identified")

# ---------------- medicines ----------------
DOS = lambda body: requests.post(BASE + "/api/v1/doses", headers=H, json=body)
r = c.get(BASE + f"/patients/{lakshmi}/medicines")
c.post(BASE + f"/patients/{lakshmi}/medicines", data={"_csrf": csrf(r.text), "action": "add", "name": "Glimepiride", "dose": "1 mg", "kind": "tablet", "times": "7:30, 1930", "window_min": "60", "remind": "1"})
mid = sql(f"SELECT id FROM medications WHERE patient_id={lakshmi} AND name='Glimepiride'")
ok(sql(f"SELECT times FROM medications WHERE id={mid}") == "07:30,19:30", "medicine added, times normalised")
# dispenser via API (bound device, slot)
res = DOS({"device": {"serial": "HERO-77812"}, "events": [{"type": "dispensed", "slot": "A1", "at": iso(0)}]})
ok(res.status_code == 200 and res.json()["patient_id"] == int(ramesh), "dispenser event → bound patient")
st = res.json()["results"][0]
ok(st["ok"] and st["status"] in ("taken", "late", "extra"), f"dispensed matched to medicine slot ({st['status']})")
res = DOS({"device": {"serial": "HERO-77812"}, "events": [{"type": "refill_low"}]}).json()
ok(sql(f"SELECT COUNT(*) FROM alerts WHERE patient_id={ramesh} AND source_code='REFILL' AND status<>'closed'") == "1", "refill-low deduplicated (one open alert)")
# a taken event replaces the worker's 'missed' for this morning's slot
miss = sql(f"SELECT DATE_FORMAT(scheduled_at,'%H:%i') FROM dose_events WHERE medication_id=(SELECT id FROM medications WHERE name='Amlodipine') AND status='missed' AND scheduled_at>=CURDATE() LIMIT 1")
if miss:
    slot_ts = _dt.datetime.combine(_dt.date.today(), _dt.time(int(miss[:2]), int(miss[3:]))).replace(tzinfo=IST)
    res = DOS({"device": {"serial": "HERO-77812"}, "events": [{"type": "taken", "medication": "Amlodipine", "at": (slot_ts + _dt.timedelta(minutes=95)).isoformat()}]}).json()
    ok(res["results"][0]["status"] == "late" and sql(f"SELECT status FROM dose_events WHERE id={res['results'][0]['id']}") == "late", "late taken replaces 'missed'")
# smart inhaler puffs
res = DOS({"device": {"serial": "PRP-55120"}, "events": [{"type": "puff", "puffs": 2, "at": iso(30)}]}).json()
ok(res["results"][0]["status"] == "puff", "inhaler actuation recorded as puff")
ok(sql(f"SELECT CONCAT(alert_type,':',value) FROM alerts WHERE patient_id={ansari} AND source_code='RESCUE' AND status<>'closed'").startswith("medication:"), "rescue overuse alert open and updated")
ok(DOS({"device": {"serial": "UNKNOWN-1"}, "events": [{"type": "taken"}]}).status_code == 422, "unknown device without patient → 422")
ok(DOS({"device": {"serial": "HERO-77812"}, "events": [{"type": "taken", "at": iso(-3600)}]}).json()["results"][0].get("error") == "event in the future", "future event rejected")
ok(requests.post(BASE + "/api/v1/doses", json={"events": [{"type": "taken"}]}).status_code == 401, "doses API needs a token")
# nurse records + worker missed
php(f"DB::tenant()->insert('medications', ['patient_id'=>{lakshmi},'name'=>'QA vitamin','kind'=>'tablet','times'=>date('H:i', time()-7200),'window_min'=>30,'start_date'=>date('Y-m-d'),'created_at'=>date('Y-m-d H:i:s', time()-86400)]);")
worker()
ok(sql(f"SELECT status FROM dose_events WHERE patient_id={lakshmi} AND medication_id=(SELECT id FROM medications WHERE name='QA vitamin')") == "missed", "worker marks a slot missed after its window")
ok(sql(f"SELECT COUNT(*) FROM alerts WHERE patient_id={lakshmi} AND alert_type='medication' AND source_code='MISSED' AND status<>'closed'") == "1", "missed-dose alert to care team")
qv = sql("SELECT id FROM medications WHERE name='QA vitamin'")
r = c.get(BASE + f"/patients/{lakshmi}/medicines")
slot = sql(f"SELECT scheduled_at FROM dose_events WHERE medication_id={qv}")
c.post(BASE + f"/patients/{lakshmi}/medicines", data={"_csrf": csrf(r.text), "action": "dose", "id": qv, "status": "taken", "slot": slot})
ok(sql(f"SELECT CONCAT(status,':',source) FROM dose_events WHERE medication_id={qv}") == "late:nurse", "nurse records dose (late, replaces missed)")
ok(sql(f"SELECT COUNT(*) FROM alerts WHERE patient_id={lakshmi} AND source_code='MISSED' AND status<>'closed'") in ("0", "1"), "missed alert refreshed")
a7 = json.loads(php(f"echo json_encode(Meds::adherence({ramesh}, 7));"))
ok(a7["scheduled"] > 0 and 0 < a7["pct"] < 100, f"adherence {a7['pct']}% of {a7['scheduled']} doses")
ok(re.search(r"\d+%</div><div class=\"sm mut\">\d+ of \d+", c.get(BASE + f"/patients/{ramesh}/medicines").text) is not None, "medicines page shows adherence KPIs")
# portal: patient confirms a dose
pr = requests.Session(); r = pr.get(BASE + "/my/login?c=DEMO")
mob = sql(f"SELECT mobile FROM patients WHERE id={lakshmi}")
r = pr.post(BASE + "/my/login", data={"_csrf": csrf(r.text), "mobile": mob})
otp = re.search(r"Test mode OTP: <b>(\d{6})", r.text)
if otp: r = pr.post(BASE + "/my/verify", data={"_csrf": csrf(r.text), "otp": otp.group(1)})
r = pr.get(BASE + "/my")
ok("Today's medicines" in r.text, "portal shows today's medicines")
gid = sql(f"SELECT id FROM medications WHERE patient_id={lakshmi} AND name='Metformin'")
pr.post(BASE + "/my/dose", data={"_csrf": csrf(r.text), "id": gid, "slot": ""})
ok(sql(f"SELECT source FROM dose_events WHERE medication_id={gid} ORDER BY id DESC LIMIT 1") == "portal", "patient confirmed dose from portal")
r = c.get(BASE + f"/patients/{lakshmi}/medicines")
c.post(BASE + f"/patients/{lakshmi}/medicines", data={"_csrf": csrf(r.text), "action": "stop", "id": mid})
ok(sql(f"SELECT active FROM medications WHERE id={mid}") == "0", "medicine stopped")
nurse = requests.Session(); r = nurse.get(BASE + "/login")
r = nurse.post(BASE + "/login", data={"_csrf": csrf(r.text), "client_code": "DEMO", "email": "nurse@demo.demo", "password": "Demo@12345"})
r = nurse.get(BASE + f"/patients/{lakshmi}/medicines")
ok(r.status_code == 200 and "+ Add medicine" not in r.text, "nurse sees medicines but cannot prescribe")


# ---------------- patient app: today's medicines ----------------
ptok = "cpg_DEMO_ab12cd34" + "x" * 24
sql(f"INSERT INTO gateways (name, gw_type, patient_id, status, token_prefix, token_hash) VALUES ('QA Lakshmi phone','android',{lakshmi},'active','ab12cd34',SHA2('{ptok}',256))")
r = requests.get(BASE + "/api/v1/medications", headers={"Authorization": "Bearer " + ptok}).json()
ok(r.get("ok") and any(d["name"] == "Metformin" for d in r["doses"]), "app: today's doses for the paired patient")
met = [d for d in r["doses"] if d["name"] == "Metformin"][0]
r2 = requests.post(BASE + "/api/v1/doses", headers={"Authorization": "Bearer " + ptok}, json={"events": [{"type": "taken", "medication_id": met["medication_id"], "at": iso(0)}]}).json()
ok(r2["ok"] and r2["patient_id"] == int(lakshmi) and r2["results"][0]["ok"], "app: dose confirmed with the patient's own token")
ok(requests.get(BASE + "/api/v1/medications", headers=H).status_code == 403, "medications list refused for a shared API key")

# ---------------- pilot hardening ----------------
r = c.get(BASE + "/dashboard")
hd = {k.lower(): v for k, v in r.headers.items()}
ok("default-src 'self'" in hd.get("content-security-policy", "") and "frame-ancestors 'self'" in hd["content-security-policy"], "Content-Security-Policy header")
ok(hd.get("x-frame-options") == "SAMEORIGIN" and hd.get("x-content-type-options") == "nosniff" and "x-powered-by" not in hd, "frame / sniff headers, no X-Powered-By")
ok("camera=*" in hd.get("permissions-policy", ""), "Permissions-Policy lets the teleconsult frame use camera / mic")
h = requests.get(BASE + "/api/v1/health")
ok(h.status_code == 200 and h.json()["ok"] and h.json()["version"] == "5.0" and "checks" not in h.json(), "public health endpoint: status only")
p = requests.Session()
r = p.get(BASE + "/platform/login")
r = p.post(BASE + "/platform/login", data={"_csrf": csrf(r.text), "email": "owner@caresoft.co.in", "password": "Test@12345abc"})
if "Set up 2-step" in r.text:
    r = p.post(BASE + "/platform/2fa/setup", data={"_csrf": csrf(r.text), "code": totp(re.search(r'class="code">([A-Z2-7 ]+)<', r.text).group(1))})
elif "2-step" in r.text:
    psec = csql("SELECT totp_secret FROM platform_users WHERE email='owner@caresoft.co.in'")
    r2 = p.post(BASE + "/platform/2fa", data={"_csrf": csrf(r.text), "code": totp(psec)})
    if "Platform overview" not in r2.text:
        time.sleep(31 - time.time() % 30); r2 = p.post(BASE + "/platform/2fa", data={"_csrf": csrf(r2.text), "code": totp(psec)})
    r = r2
check(r, "platform owner signed in", contains="Platform overview")
r = p.get(BASE + "/platform/system")
check(r, "system health page", contains="Security self-check")
ok("Background worker" in r.text and "Nightly backup" in r.text and "Restore test" in r.text and "Encryption key" in r.text, "operations + security checks listed")
key = csql("SELECT v FROM platform_settings WHERE k='monitor_key'")
hj = requests.get(BASE + "/api/v1/health?key=" + key).json()
ok("checks" in hj and "worker" in hj["checks"] and "backup" in hj["checks"], "health with monitor key returns every check")
ok("checks" not in requests.get(BASE + "/api/v1/health?key=wrong").json(), "wrong key → no details")
p.post(BASE + "/platform/system", data={"_csrf": csrf(r.text), "action": "contacts", "monitor_emails": "ops@caresoft.co.in, bad-email", "monitor_whatsapp": "98000 11111"})
ok(csql("SELECT v FROM platform_settings WHERE k='monitor_emails'") == "ops@caresoft.co.in" and csql("SELECT v FROM platform_settings WHERE k='monitor_whatsapp'") == "9800011111", "alert contacts saved (invalid e-mail dropped)")
n0 = int(csql("SELECT COUNT(*) FROM backup_runs WHERE kind='backup' AND status='ok'"))
r = p.post(BASE + "/platform/system", data={"_csrf": csrf(p.get(BASE + "/platform/system").text), "action": "backup"})
ok(int(csql("SELECT COUNT(*) FROM backup_runs WHERE kind='backup' AND status='ok'")) == n0 + 1, "backup from console succeeded")
setdir = csql("SELECT set_name FROM backup_runs WHERE kind='backup' ORDER BY id DESC LIMIT 1")
man = json.load(open(f"/tmp/cp_backups/{setdir}/manifest.json"))
ok(man["encrypted"] and any(f.startswith("cp_client_demo") for f in man["files"]) and any(f.startswith("storage") for f in man["files"]), "manifest: encrypted client DB + files with SHA-256")
raw = open(f"/tmp/cp_backups/{setdir}/cp_client_demo.sql.gz.enc", "rb").read(16)
ok(raw.startswith(b"Salted__"), "backup file is AES encrypted (not readable gzip)")
ok(oct(os.stat(f"/tmp/cp_backups/{setdir}/cp_client_demo.sql.gz.enc").st_mode)[-3:] == "600", "backup files readable by owner only")
r = p.post(BASE + "/platform/system", data={"_csrf": csrf(p.get(BASE + "/platform/system").text), "action": "restore"})
ok(csql("SELECT status FROM backup_runs WHERE kind='restore_check' ORDER BY id DESC LIMIT 1") == "ok", "restore test passed (scratch DB restored and compared)")
ok(sql("SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name LIKE 'cp_restorecheck_%'") == "0", "scratch database dropped")
out = subprocess.run(["php", "tools/monitor.php"], cwd=PLAT, capture_output=True, text=True).stdout
ok("ok" in out and "Warning" not in out, "monitor tool runs")
subprocess.run(["php", "cron/worker.php"], cwd=PLAT, capture_output=True)
ok(time.time() - int(csql("SELECT v FROM platform_settings WHERE k='worker_last_run'")) < 120, "worker heartbeat recorded")

print("\n%d failure(s)" % len(FAILS))
for f in FAILS: print(" -", f)
sys.exit(1 if FAILS else 0)
