test_s2_relearn_button.py 2.25 KB
#!/usr/bin/env python3
"""S2 RED: Frontend test for relearn button on learn_detail.html"""
import urllib.request, urllib.parse, sys, os

sys.path.insert(0, os.path.expanduser("~/wechat-viewer"))
import local_db

BASE = "http://127.0.0.1:5000"
FAILURES = []

def check(name, condition, detail=""):
    if condition:
        print(f"  PASS {name}")
    else:
        print(f"  FAIL {name}: {detail}")
        FAILURES.append(name)

print("=" * 60)
print("S2: Relearn Button — RED Phase")
print("=" * 60)

# Create a test learn item
local_db.learn_add(
    article_url="https://test/s2-relearn-button-test",
    title="S2 Relearn Button Test",
    dedup_key="t|s2rb|1|z",
    learn_type="memory",
    learn_target="s2_test",
    learn_content="# S2 Test Content",
    confidence="high"
)

# Find it
conn = local_db.get_conn()
rows = conn.execute(
    "SELECT id FROM learn_queue WHERE article_url LIKE ? ORDER BY id DESC LIMIT 1",
    ("%s2-relearn-button%",)
).fetchall()
conn.close()

if not rows:
    print("ERROR: could not create test item")
    sys.exit(1)

test_id = rows[0]["id"]
print(f"Test item ID: {test_id}")

# TC1: Page loads
r = urllib.request.urlopen(f"{BASE}/learn/{test_id}")
html = r.read().decode("utf-8")
check("page returns 200", r.status == 200, f"Got {r.status}")

# TC2: Relearn button exists (check for action-bar with button)
has_action_bar = 'action-bar' in html
check("action-bar in page", has_action_bar, "action-bar div not found")

has_button = 'handleRelearn' in html
check("relearn button JS", has_button, "handleRelearn function not in page")

# TC4: No Jinja2 escape issues
check("no style=\\\\\" bug", 'style=\\\\' not in html, "Found escaped style")
check("no template literal \\\\\\\" bug", 'data-action=\\\\"' not in html, "Found escaped data-action")
check("no inline onclick escaped", 'onclick=\\"' not in html, "Found escaped onclick")

# TC5: No triple-escape
check("no triple escape", '\\\\\\\\\\\\\\"' not in html, "Found triple escape")

print("\n" + "=" * 60)
if FAILURES:
    print(f"RED: {len(FAILURES)} failures (expected for new feature)")
    for f in FAILURES:
        print(f"  ❌ {f}")
    sys.exit(1)
else:
    print("RED: All checks passed (button might already exist)")
    sys.exit(0)  # Exit 0 = no failures = feature exists (GREEN case)