#!/usr/bin/env python3
# Pure-logic test — no DB, no network.
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))
from whp_monitor_core import compute_signature, ten_minute_bucket

ok = True

# Health signature is stable across time buckets
s1 = compute_signature(kind='health', subsystem='cpu', severity='critical')
s2 = compute_signature(kind='health', subsystem='cpu', severity='critical')
if s1 != s2:
    print(f"FAIL: health signature not stable: {s1} vs {s2}"); ok = False

# Site signatures dedup by (site_id, category) and are NOT time-bucketed, so the
# same ongoing problem maps to one open issue (any passed bucket is ignored).
s3 = compute_signature(kind='site', site_id=42, category='site_down', bucket=100)
s4 = compute_signature(kind='site', site_id=42, category='site_down', bucket=101)
if s3 != s4:
    print(f"FAIL: site signature must be bucket-independent (one issue per site/category): {s3} vs {s4}"); ok = False
# Different category -> different signature
s5 = compute_signature(kind='site', site_id=42, category='site_php_wedged')
if s3 == s5:
    print(f"FAIL: site signature must distinguish category"); ok = False

# Crash signatures dedup per (container, cause) and distinguish both.
c1 = compute_signature(kind='crash', container='haproxy-manager', cause='watchdog_restart')
c2 = compute_signature(kind='crash', container='haproxy-manager', cause='watchdog_restart')
c3 = compute_signature(kind='crash', container='haproxy-manager', cause='watchdog_start')
c4 = compute_signature(kind='crash', container='memcache', cause='watchdog_restart')
if c1 != c2:
    print(f"FAIL: crash signature not stable for same (container,cause)"); ok = False
if c1 == c3:
    print(f"FAIL: crash signature must distinguish cause"); ok = False
if c1 == c4:
    print(f"FAIL: crash signature must distinguish container"); ok = False

# 10-min bucket rounds correctly
if ten_minute_bucket(1_700_000_359) != ten_minute_bucket(1_700_000_000):
    print(f"FAIL: ten_minute_bucket not bucketing to 600s boundary"); ok = False

print("PASS" if ok else "FAIL")
sys.exit(0 if ok else 1)
