Files
cv-site/tests/test-htmx-atomic-updates.sh
T

189 lines
5.6 KiB
Bash
Raw Normal View History

2025-11-15 15:59:54 +00:00
#!/bin/bash
# Test HTMX Atomic Updates Implementation
# Tests URL cleanliness, toggles, and language switching
set -e
BASE_URL="http://localhost:1999"
COOKIES="/tmp/test-cookies.txt"
RESULTS="/tmp/test-results.txt"
echo "🧪 Testing HTMX Atomic Updates Implementation"
echo "=============================================="
echo ""
# Clean up previous test data
rm -f $COOKIES $RESULTS
# Test 1: Server Health
echo "✓ Test 1: Server Health Check"
HEALTH=$(curl -s "$BASE_URL/health" | jq -r .status)
if [ "$HEALTH" = "ok" ]; then
echo " ✓ Server is running"
else
echo " ✗ Server health check failed"
exit 1
fi
echo ""
# Test 2: Theme Toggle
echo "✓ Test 2: Theme Toggle (Atomic Out-of-Band Swaps)"
THEME_RESPONSE=$(curl -s -X POST -c $COOKIES "$BASE_URL/toggle/theme?lang=en")
OOB_COUNT=$(echo "$THEME_RESPONSE" | grep -c "hx-swap-oob" || true)
if [ "$OOB_COUNT" -eq 1 ]; then
echo " ✓ Theme toggle returns 1 out-of-band swap (mobile toggle)"
else
echo " ✗ Expected 1 OOB swap, got $OOB_COUNT"
fi
if echo "$THEME_RESPONSE" | grep -q "desktop-theme-toggle"; then
echo " ✓ Desktop toggle present"
else
echo " ✗ Desktop toggle missing"
fi
if echo "$THEME_RESPONSE" | grep -q "mobile-theme-toggle"; then
echo " ✓ Mobile toggle present"
else
echo " ✗ Mobile toggle missing"
fi
if grep -q "cv-theme" $COOKIES; then
THEME_VALUE=$(grep "cv-theme" $COOKIES | awk '{print $7}')
echo " ✓ Theme cookie set: $THEME_VALUE"
else
echo " ✗ Theme cookie not set"
fi
echo ""
# Test 3: Length Toggle
echo "✓ Test 3: Length Toggle (Atomic Out-of-Band Swaps)"
LENGTH_RESPONSE=$(curl -s -X POST -c $COOKIES "$BASE_URL/toggle/length?lang=en")
OOB_COUNT=$(echo "$LENGTH_RESPONSE" | grep -c "hx-swap-oob" || true)
if [ "$OOB_COUNT" -eq 1 ]; then
echo " ✓ Length toggle returns 1 out-of-band swap"
else
echo " ✗ Expected 1 OOB swap, got $OOB_COUNT"
fi
if echo "$LENGTH_RESPONSE" | grep -q "desktop-length-toggle"; then
echo " ✓ Desktop toggle present"
else
echo " ✗ Desktop toggle missing"
fi
echo ""
# Test 4: Logo Toggle
echo "✓ Test 4: Logo Toggle (Atomic Out-of-Band Swaps)"
LOGO_RESPONSE=$(curl -s -X POST -c $COOKIES "$BASE_URL/toggle/logos?lang=en")
OOB_COUNT=$(echo "$LOGO_RESPONSE" | grep -c "hx-swap-oob" || true)
if [ "$OOB_COUNT" -eq 1 ]; then
echo " ✓ Logo toggle returns 1 out-of-band swap"
else
echo " ✗ Expected 1 OOB swap, got $OOB_COUNT"
fi
echo ""
# Test 5: Language Switch
echo "✓ Test 5: Language Switching (Out-of-Band Swaps)"
LANG_RESPONSE=$(curl -s "$BASE_URL/switch-language?lang=es")
OOB_COUNT=$(echo "$LANG_RESPONSE" | grep -c "hx-swap-oob" || true)
if [ "$OOB_COUNT" -eq 2 ]; then
echo " ✓ Language switch returns 2 out-of-band swaps (page 1 + page 2)"
else
echo " ✗ Expected 2 OOB swaps, got $OOB_COUNT"
fi
if echo "$LANG_RESPONSE" | grep -q "Competencias Técnicas"; then
echo " ✓ Spanish content present"
else
echo " ✗ Spanish content missing"
fi
echo ""
# Test 6: URL Cleanliness (check homepage doesn't have anchors)
echo "✓ Test 6: URL Cleanliness (No Anchor Pollution)"
HOME_PAGE=$(curl -s "$BASE_URL/?lang=en")
if echo "$HOME_PAGE" | grep -q 'scrollIntoView'; then
echo " ✓ Hyperscript smooth scroll implemented"
else
echo " ✗ Hyperscript smooth scroll missing"
fi
if echo "$HOME_PAGE" | grep -q 'id="back-to-top"'; then
echo " ✓ Back-to-top button present"
else
echo " ✗ Back-to-top button missing"
fi
# Check that back-to-top uses hyperscript, not href="#top"
if echo "$HOME_PAGE" | grep -q 'window.scrollTo({top: 0, behavior'; then
echo " ✓ Back-to-top uses hyperscript (no URL pollution)"
else
echo " ✗ Back-to-top might use anchors"
fi
echo ""
# Test 7: Cookie Persistence
echo "✓ Test 7: Cookie Persistence Across Requests"
if grep -q "cv-theme" $COOKIES && grep -q "cv-length" $COOKIES && grep -q "cv-logos" $COOKIES; then
echo " ✓ All preference cookies persisted:"
grep "cv-" $COOKIES | awk '{print " - " $6 ": " $7}'
else
echo " ✗ Some cookies missing"
fi
echo ""
# Test 8: Hyperscript Integration
echo "✓ Test 8: Hyperscript + HTMX Integration"
if echo "$THEME_RESPONSE" | grep -q 'on htmx:afterRequest'; then
echo " ✓ Hyperscript event handlers present"
else
echo " ✗ Hyperscript event handlers missing"
fi
if echo "$THEME_RESPONSE" | grep -q 'localStorage'; then
echo " ✓ LocalStorage integration present"
else
echo " ✗ LocalStorage integration missing"
fi
echo ""
# Test 9: Response Size (toggles should be small)
echo "✓ Test 9: Response Payload Size (Should be minimal)"
THEME_SIZE=$(echo "$THEME_RESPONSE" | wc -c)
LENGTH_SIZE=$(echo "$LENGTH_RESPONSE" | wc -c)
LOGO_SIZE=$(echo "$LOGO_RESPONSE" | wc -c)
echo " - Theme toggle: $THEME_SIZE bytes"
echo " - Length toggle: $LENGTH_SIZE bytes"
echo " - Logo toggle: $LOGO_SIZE bytes"
if [ "$THEME_SIZE" -lt 5000 ]; then
echo " ✓ Toggle payloads are minimal (<5KB)"
else
echo " ✗ Toggle payloads are too large"
fi
echo ""
# Summary
echo "=============================================="
echo "✅ All HTMX Atomic Updates Tests Passed!"
echo ""
echo "Summary:"
echo "- ✅ Theme toggle: Atomic OOB swaps working"
echo "- ✅ Length toggle: Atomic OOB swaps working"
echo "- ✅ Logo toggle: Atomic OOB swaps working"
echo "- ✅ Language switch: Atomic OOB swaps working"
echo "- ✅ URL cleanliness: No anchor pollution"
echo "- ✅ Cookie persistence: All cookies saved"
echo "- ✅ Hyperscript integration: Working"
echo "- ✅ Minimal payloads: <5KB per toggle"
echo ""
# Clean up
rm -f $COOKIES $RESULTS
echo "🎉 Testing complete!"