"""Side A (WITH skill) — step 2: verification tests.
Selectors from the reconnaissance step. Asserts the spec values:
tip on the bill alone; per-person must track the people input.
Also captures console logs (skill's console_logging.py pattern)."""
from playwright.sync_api import sync_playwright

URL = "http://localhost:8932/index.html"
failures = []
console_logs = []

def check(name, actual, expected):
    ok = actual == expected
    print(f"{'PASS' if ok else 'FAIL'}: {name} — expected {expected}, got {actual}")
    if not ok:
        failures.append(f"{name}: expected {expected}, got {actual}")

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    page.on("console", lambda msg: console_logs.append(f"[{msg.type}] {msg.text}"))
    page.goto(URL)
    page.wait_for_load_state("networkidle")

    # Defaults: bill 100, tax 10%, tip 15%, people 2
    check("tax amount", page.inner_text("#taxAmount"), "10.00")
    check("tip amount (15% of bill 100)", page.inner_text("#tipAmount"), "15.00")
    check("total", page.inner_text("#total"), "125.00")
    check("each pays (125 / 2)", page.inner_text("#perPerson"), "62.50")

    # Change ONLY the people count: per-person must move to total / 4
    before = page.inner_text("#perPerson")
    page.fill("#people", "4")
    page.wait_for_timeout(300)
    after = page.inner_text("#perPerson")
    print(f"perPerson before people change: {before}; after setting people=4: {after}")
    check("each pays updates when people changes", after != before, True)
    check("each pays (125 / 4)", after, "31.25")

    # Change the bill: everything must recompute (bill 200, tax 10%, tip 15%, people 4)
    page.fill("#bill", "200")
    page.wait_for_timeout(300)
    check("tip amount (15% of bill 200)", page.inner_text("#tipAmount"), "30.00")
    check("total (200 + 20 tax + 30 tip)", page.inner_text("#total"), "250.00")
    check("each pays (250 / 4)", page.inner_text("#perPerson"), "62.50")

    browser.close()

print(f"\nConsole messages captured: {len(console_logs)}")
for line in console_logs:
    print(" ", line)

print(f"\n=== SIDE A RESULT: {len(failures)} failing check(s) ===")
for f in failures:
    print("BUG EVIDENCE:", f)
