programme-pulse-chat/tests/test_preferences.py
DJP b70d148b94 Productionise Programme Pulse
Backend
- Routes moved under /api/, JWT bearer auth via @before_request
- DEV_AUTH_BYPASS escape hatch for local dev
- In-memory chat history and report state replaced with Postgres tables
  (preferences, chat_messages, reports, feedback_events) keyed on user
- SQLAlchemy 2.x + Alembic migrations run on container start
- Graceful Airtable failure handling — bad creds no longer 500 the API
- Per-user data isolation via g.user_email from validated token

Frontend
- React + Vite + TypeScript SPA at /programme-pulse/
- MSAL.js (PKCE, sessionStorage, ID token to backend)
- VITE_DEV_AUTH_BYPASS mirrors backend bypass for local dev
- Streaming chat via fetch ReadableStream + SSE parsing
- Charts via chart.js, markdown via react-markdown + remark-gfm
- Full UI parity with the original templates/index.html

Deploy (optical-dev split-build pattern)
- Dockerfile + docker-compose.yml (name: programme-pulse pinned;
  app + Postgres; 127.0.0.1 binding only)
- deploy/apache-programme-pulse.conf.tmpl with flushpackets=on for SSE
- deploy/deploy.sh mirrors OSOP — port auto-pick (5051..5099),
  apache conf render, frontend build in throwaway node container,
  rsync to /var/www/html/programme-pulse, /api/health poll

Tests
- 49 passing; new tests for DB-backed preferences and JWT auth helpers
- SQLite-backed test fixture in tests/conftest.py

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 11:08:28 -04:00

53 lines
1.8 KiB
Python

from src.preferences import (
append_preference,
delete_preference,
list_preferences,
load_preferences,
)
def test_load_preferences_empty():
assert load_preferences("user@oliver.agency") == ""
def test_append_and_list():
pid1 = append_preference("user@oliver.agency", "Prefer concise answers")
pid2 = append_preference("user@oliver.agency", "Always cite the data")
rows = list_preferences("user@oliver.agency")
assert [r["text"] for r in rows] == ["Prefer concise answers", "Always cite the data"]
assert {r["id"] for r in rows} == {pid1, pid2}
def test_load_preferences_formats_as_bullets():
append_preference("user@oliver.agency", "First")
append_preference("user@oliver.agency", "Second")
text = load_preferences("user@oliver.agency")
assert "- First" in text
assert "- Second" in text
def test_per_user_isolation():
append_preference("alice@oliver.agency", "Alice's preference")
append_preference("bob@oliver.agency", "Bob's preference")
alice = list_preferences("alice@oliver.agency")
bob = list_preferences("bob@oliver.agency")
assert [r["text"] for r in alice] == ["Alice's preference"]
assert [r["text"] for r in bob] == ["Bob's preference"]
def test_delete_preference():
pid = append_preference("user@oliver.agency", "To be deleted")
assert delete_preference("user@oliver.agency", pid) is True
assert list_preferences("user@oliver.agency") == []
def test_delete_other_users_preference_fails():
pid = append_preference("alice@oliver.agency", "Alice's secret")
assert delete_preference("bob@oliver.agency", pid) is False
# Still there
assert len(list_preferences("alice@oliver.agency")) == 1
def test_load_preferences_no_user_returns_empty():
assert load_preferences(None) == ""
assert load_preferences("") == ""