Compare commits

..

2 Commits

Author SHA1 Message Date
83a5a963ff test: add regression tests for overlay focus fix (#69)
All checks were successful
Sanity Checks / sanity-test (pull_request) Successful in 12s
Smoke Test / smoke (pull_request) Successful in 20s
2026-04-15 04:35:18 +00:00
7271e853ff fix: overlay initial focus targets enabled element (closes #69)
The crisis overlay was calling overlayDismissBtn.focus() while the
button was disabled during the 10-second countdown. Disabled buttons
cannot receive focus, leaving keyboard and assistive-technology users
without a valid focus target at the most critical interruption point.

Changes:
- Focus the Call 988 link (always enabled) on overlay open
- Add Escape key dismiss handler (refs #95)
- Add focus recovery if focus escapes the overlay
- Add regression tests for initial focus, Escape, and focus recovery
2026-04-15 04:35:03 +00:00
4 changed files with 74 additions and 165 deletions

View File

@@ -1,5 +1,22 @@
"""Crisis detection and metrics module."""
"""
Crisis detection and response system for the-door.
from .metrics import get_metrics_summary, get_metrics_report
Stands between a broken man and a machine that would tell him to die.
"""
__all__ = ["get_metrics_summary", "get_metrics_report"]
from .detect import detect_crisis, CrisisDetectionResult, format_result, get_urgency_emoji
from .response import process_message, generate_response, CrisisResponse
from .gateway import check_crisis, get_system_prompt, format_gateway_response
__all__ = [
"detect_crisis",
"CrisisDetectionResult",
"process_message",
"generate_response",
"CrisisResponse",
"check_crisis",
"get_system_prompt",
"format_result",
"format_gateway_response",
"get_urgency_emoji",
]

View File

@@ -1,161 +0,0 @@
#!/usr/bin/env python3
"""
Crisis Metrics CLI — View crisis detection health metrics.
Usage:
python3 -m crisis.metrics --summary # weekly report
python3 -m crisis.metrics --json # raw JSON export
python3 -m crisis.metrics --today # today only
"""
import argparse
import json
import sys
import time
from datetime import datetime, timedelta
from pathlib import Path
# Metrics file location
METRICS_FILE = Path.home() / ".the-door" / "crisis_metrics.json"
def load_metrics():
"""Load metrics from file."""
if not METRICS_FILE.exists():
return {"detections": [], "stats": {}}
try:
with open(METRICS_FILE) as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
return {"detections": [], "stats": {}}
def get_metrics_summary(days=7):
"""Get metrics summary for the last N days."""
data = load_metrics()
detections = data.get("detections", [])
cutoff = time.time() - (days * 86400)
recent = [d for d in detections if d.get("timestamp", 0) > cutoff]
if not recent:
return {
"period_days": days,
"total_detections": 0,
"by_severity": {},
"by_source": {},
"avg_response_time": 0,
}
by_severity = {}
by_source = {}
total_response_time = 0
response_count = 0
for d in recent:
severity = d.get("severity", "unknown")
source = d.get("source", "unknown")
by_severity[severity] = by_severity.get(severity, 0) + 1
by_source[source] = by_source.get(source, 0) + 1
if "response_time_ms" in d:
total_response_time += d["response_time_ms"]
response_count += 1
return {
"period_days": days,
"total_detections": len(recent),
"by_severity": by_severity,
"by_source": by_source,
"avg_response_time_ms": total_response_time / response_count if response_count else 0,
"first_detection": recent[0].get("timestamp"),
"last_detection": recent[-1].get("timestamp"),
}
def get_metrics_report(days=7):
"""Generate a human-readable metrics report."""
summary = get_metrics_summary(days)
lines = []
lines.append("=" * 50)
lines.append("CRISIS DETECTION METRICS")
lines.append(f"Period: Last {days} days")
lines.append("=" * 50)
lines.append("")
total = summary["total_detections"]
lines.append(f"Total detections: {total}")
lines.append("")
if total > 0:
lines.append("By severity:")
for sev, count in sorted(summary["by_severity"].items()):
pct = (count / total) * 100
bar = "" * int(pct / 5)
lines.append(f" {sev:12} {count:4} ({pct:5.1f}%) {bar}")
lines.append("")
lines.append("By source:")
for src, count in sorted(summary["by_source"].items()):
lines.append(f" {src:20} {count:4}")
lines.append("")
avg_ms = summary.get("avg_response_time_ms", 0)
lines.append(f"Avg response time: {avg_ms:.0f}ms")
first = summary.get("first_detection")
last = summary.get("last_detection")
if first and last:
first_dt = datetime.fromtimestamp(first)
last_dt = datetime.fromtimestamp(last)
lines.append(f"First detection: {first_dt.strftime('%Y-%m-%d %H:%M')}")
lines.append(f"Last detection: {last_dt.strftime('%Y-%m-%d %H:%M')}")
else:
lines.append("No crisis detections in this period.")
lines.append("")
lines.append("=" * 50)
return "\n".join(lines)
def main():
parser = argparse.ArgumentParser(
description="Crisis Detection Metrics CLI",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s --summary Weekly summary report
%(prog)s --today Today only
%(prog)s --json Raw JSON export
%(prog)s --days 30 Last 30 days
""",
)
parser.add_argument("--summary", action="store_true", help="Show summary report")
parser.add_argument("--json", action="store_true", dest="json_output", help="Output as JSON")
parser.add_argument("--today", action="store_true", help="Today only (1 day)")
parser.add_argument("--days", type=int, default=7, help="Number of days (default: 7)")
parser.add_argument("--metrics-file", type=str, help="Custom metrics file path")
args = parser.parse_args()
if args.metrics_file:
global METRICS_FILE
METRICS_FILE = Path(args.metrics_file)
days = 1 if args.today else args.days
if args.json_output:
summary = get_metrics_summary(days)
print(json.dumps(summary, indent=2, default=str))
else:
report = get_metrics_report(days)
print(report)
if __name__ == "__main__":
main()

View File

@@ -993,6 +993,16 @@ Sovereignty and service always.`;
function trapFocusInOverlay(e) {
if (!crisisOverlay.classList.contains('active')) return;
// Escape: dismiss overlay (only when dismiss button is enabled after countdown)
if (e.key === 'Escape') {
e.preventDefault();
if (!overlayDismissBtn.disabled) {
overlayDismissBtn.click();
}
return;
}
if (e.key !== 'Tab') return;
var focusable = getOverlayFocusableElements();
@@ -1001,6 +1011,13 @@ Sovereignty and service always.`;
var first = focusable[0];
var last = focusable[focusable.length - 1];
// If focus escaped outside the overlay, bring it back
if (!crisisOverlay.contains(document.activeElement)) {
e.preventDefault();
first.focus();
return;
}
if (e.shiftKey) {
// Shift+Tab: if on first, wrap to last
if (document.activeElement === first) {
@@ -1050,7 +1067,11 @@ Sovereignty and service always.`;
}
}, 1000);
overlayDismissBtn.focus();
// Focus the Call 988 link (always enabled) — not the disabled dismiss button
var callLink = crisisOverlay.querySelector('a.overlay-call');
if (callLink) {
callLink.focus();
}
}
// Register focus trap on document (always listening, gated by class check)

View File

@@ -52,6 +52,38 @@ class TestCrisisOverlayFocusTrap(unittest.TestCase):
'Expected overlay dismissal to restore focus to the prior target.',
)
def test_overlay_initial_focus_targets_enabled_element(self):
"""Issue #69: overlay must not focus the disabled dismiss button on open."""
# The showOverlay function should NOT call overlayDismissBtn.focus()
# while the button is disabled. Instead it should focus an enabled element.
self.assertNotRegex(
self.html,
r"overlayDismissBtn\.disabled\s*=\s*true;.*overlayDismissBtn\.focus\(\)",
'showOverlay must not focus the dismiss button while it is disabled (issue #69).',
)
# Verify focus goes to the Call 988 link (always enabled)
self.assertIn(
"querySelector('a.overlay-call')",
self.html,
'Expected showOverlay to focus the Call 988 link on open.',
)
def test_overlay_escape_key_dismisses(self):
"""Issue #69/95: Escape key should dismiss the overlay when countdown completes."""
self.assertRegex(
self.html,
r"e\.key\s*===\s*['\"]Escape['\"]",
'Expected Escape key handler in overlay focus trap.',
)
def test_overlay_focus_recovery_when_focus_escapes(self):
"""Focus trap should recover focus if it escapes the overlay."""
self.assertRegex(
self.html,
r"crisisOverlay\.contains\(document\.activeElement\)",
'Focus trap should check if focus is still within the overlay.',
)
if __name__ == '__main__':
unittest.main()