Compare commits

..

2 Commits

Author SHA1 Message Date
21d38bb18a fix: replace blocking alert() with inline toast (#73)
All checks were successful
Sanity Checks / sanity-test (pull_request) Successful in 44s
Smoke Test / smoke (pull_request) Successful in 45s
2026-04-16 02:11:10 +00:00
6c92c81a61 fix: replace blocking alert() with inline toast (#73) 2026-04-16 02:11:06 +00:00
3 changed files with 95 additions and 91 deletions

View File

@@ -475,26 +475,6 @@ html, body {
margin-bottom: 24px;
}
.modal-status {
min-height: 22px;
margin: 0 0 16px;
font-size: 0.9rem;
line-height: 1.45;
color: #8b949e;
}
.modal-status.is-visible {
display: block;
}
.modal-status.success {
color: #3fb950;
}
.modal-status.error {
color: #ff7b72;
}
.form-group {
margin-bottom: 16px;
}
@@ -633,6 +613,31 @@ html, body {
top: 8px;
outline: 2px solid #58a6ff;
}
/* Toast notification */
.toast-notification {
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%) translateY(100px);
padding: 12px 24px;
border-radius: 8px;
font-size: 0.9rem;
font-weight: 500;
z-index: 10001;
opacity: 0;
transition: transform 0.3s ease, opacity 0.3s ease;
pointer-events: none;
max-width: 90vw;
text-align: center;
}
.toast-notification.visible {
transform: translateX(-50%) translateY(0);
opacity: 1;
}
.toast-notification.success { background: #238636; color: #fff; }
.toast-notification.error { background: #da3633; color: #fff; }
</style>
</head>
<body>
@@ -757,7 +762,6 @@ html, body {
<textarea id="sp-environment" placeholder="e.g., Giving my car keys to a friend, locking away meds..."></textarea>
</div>
</div>
<div id="safety-plan-status" class="modal-status" role="status" aria-live="polite" aria-atomic="true"></div>
<div class="modal-footer">
<button class="btn btn-secondary" id="cancel-safety-plan">Cancel</button>
<button class="btn btn-primary" id="save-safety-plan">Save Plan</button>
@@ -839,7 +843,6 @@ Sovereignty and service always.`;
var closeSafetyPlan = document.getElementById('close-safety-plan');
var cancelSafetyPlan = document.getElementById('cancel-safety-plan');
var saveSafetyPlan = document.getElementById('save-safety-plan');
var safetyPlanStatus = document.getElementById('safety-plan-status');
var clearChatBtn = document.getElementById('clear-chat-btn');
// ===== STATE =====
@@ -1205,24 +1208,12 @@ Sovereignty and service always.`;
} catch (e) {}
}
function setSafetyPlanStatus(message, type) {
safetyPlanStatus.textContent = message;
safetyPlanStatus.className = 'modal-status is-visible ' + (type || '');
}
function clearSafetyPlanStatus() {
safetyPlanStatus.textContent = '';
safetyPlanStatus.className = 'modal-status';
}
closeSafetyPlan.addEventListener('click', function() {
clearSafetyPlanStatus();
safetyPlanModal.classList.remove('active');
_restoreSafetyPlanFocus();
});
cancelSafetyPlan.addEventListener('click', function() {
clearSafetyPlanStatus();
safetyPlanModal.classList.remove('active');
_restoreSafetyPlanFocus();
});
@@ -1237,9 +1228,11 @@ Sovereignty and service always.`;
};
try {
localStorage.setItem('timmy_safety_plan', JSON.stringify(plan));
setSafetyPlanStatus('Safety plan saved locally.', 'success');
safetyPlanModal.classList.remove('active');
_restoreSafetyPlanFocus();
showToast('Safety plan saved locally.', 'success');
} catch (e) {
setSafetyPlanStatus('Error saving plan.', 'error');
showToast('Error saving plan.', 'error');
}
});
@@ -1317,7 +1310,6 @@ Sovereignty and service always.`;
// Wire open buttons to activate focus trap
safetyPlanBtn.addEventListener('click', function() {
clearSafetyPlanStatus();
loadSafetyPlan();
safetyPlanModal.classList.add('active');
_activateSafetyPlanFocusTrap(safetyPlanBtn);
@@ -1326,8 +1318,6 @@ Sovereignty and service always.`;
// Crisis panel safety plan button (if crisis panel is visible)
if (crisisSafetyPlanBtn) {
crisisSafetyPlanBtn.addEventListener('click', function() {
clearSafetyPlanStatus();
clearSafetyPlanStatus();
loadSafetyPlan();
safetyPlanModal.classList.add('active');
_activateSafetyPlanFocusTrap(crisisSafetyPlanBtn);
@@ -1487,6 +1477,22 @@ Sovereignty and service always.`;
msgInput.focus();
}
// ===== TOAST NOTIFICATION =====
var _toastEl = document.getElementById('toast-notification');
var _toastTimer = null;
function showToast(message, type) {
type = type || 'success';
_toastEl.textContent = message;
_toastEl.className = 'toast-notification ' + type;
void _toastEl.offsetHeight; // force reflow before transition
_toastEl.classList.add('visible');
if (_toastTimer) clearTimeout(_toastTimer);
_toastTimer = setTimeout(function() {
_toastEl.classList.remove('visible');
}, 3000);
}
// ===== BOOT =====
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
@@ -1496,5 +1502,8 @@ Sovereignty and service always.`;
})();
</script>
<div id="toast-notification" class="toast-notification"
role="status" aria-live="polite" aria-atomic="true"></div>
</body>
</html>

View File

@@ -1,52 +0,0 @@
import pathlib
import re
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[1]
INDEX_HTML = ROOT / 'index.html'
class TestSafetyPlanSaveFeedback(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.html = INDEX_HTML.read_text()
def test_modal_has_inline_status_live_region(self):
self.assertRegex(
self.html,
r'<div[^>]+id="safety-plan-status"[^>]+role="status"[^>]+aria-live="polite"[^>]*>',
'Expected an inline polite live region for safety plan save feedback.',
)
def test_save_feedback_does_not_use_blocking_alerts(self):
self.assertNotIn(
"alert('Safety plan saved locally.')",
self.html,
'Expected success feedback to stop using blocking alert().',
)
self.assertNotIn(
"alert('Error saving plan.')",
self.html,
'Expected error feedback to stop using blocking alert().',
)
def test_save_logic_updates_inline_status_for_success_and_error(self):
self.assertRegex(
self.html,
r'function\s+setSafetyPlanStatus\s*\(',
'Expected a helper to update inline save feedback.',
)
self.assertRegex(
self.html,
r"setSafetyPlanStatus\('Safety plan saved locally\.'\s*,\s*'success'\)",
'Expected success path to update inline status.',
)
self.assertRegex(
self.html,
r"setSafetyPlanStatus\('Error saving plan\.'\s*,\s*'error'\)",
'Expected error path to update inline status.',
)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,47 @@
"""Tests for inline toast notification replacing blocking alert() — issue #73."""
import pathlib
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[1]
INDEX_HTML = ROOT / "index.html"
class TestToastNotification(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.html = INDEX_HTML.read_text(encoding="utf-8")
def test_no_blocking_alerts_in_safety_plan_save(self):
"""Safety plan save handler must not use alert()."""
idx = self.html.find("localStorage.setItem('timmy_safety_plan'")
self.assertGreater(idx, 0, "Safety plan save handler not found")
section = self.html[idx : idx + 300]
self.assertNotIn(
"alert(",
section,
"Safety plan save handler still uses blocking alert()",
)
def test_toast_element_exists_in_dom(self):
self.assertIn('id="toast-notification"', self.html)
def test_toast_has_aria_live(self):
self.assertIn('aria-live="polite"', self.html)
def test_showToast_function_defined(self):
self.assertIn("function showToast(", self.html)
def test_toast_css_classes_present(self):
for cls in (".toast-notification", ".visible", ".success", ".error"):
self.assertIn(cls, self.html, f"Missing CSS class {cls}")
def test_toast_auto_dismiss_via_timeout(self):
idx = self.html.find("function showToast")
self.assertGreater(idx, 0, "showToast function not found")
self.assertIn("setTimeout", self.html[idx:])
def test_showToast_replaces_alert():
html = INDEX_HTML.read_text(encoding="utf-8")
assert "showToast('Safety plan saved locally.'" in html
assert "showToast('Error saving plan.'" in html