Files
Timmy-time-dashboard/tests/dashboard/test_nexus.py
Perplexity Computer 77cdfde3a2
Some checks failed
Tests / lint (pull_request) Failing after 46s
Tests / test (pull_request) Has been skipped
[perplexity] feat: Nexus v2 — Cognitive Awareness & Introspection Engine (#1090)
Extends the Nexus from a chat-only interface into a full cognitive
awareness space with real-time introspection, persistent sessions,
and sovereignty health monitoring.

New modules (src/timmy/nexus/):
- introspection.py — Aggregates CognitiveTracker, ThinkingEngine, and
  session analytics into a unified IntrospectionSnapshot. Surfaces mood,
  engagement, focus, commitments, and recent thoughts.
- persistence.py — SQLite-backed NexusStore so conversations survive
  process restarts. WAL mode, auto-pruning at 500 messages, session-tag
  isolation for future per-operator sessions.
- sovereignty_pulse.py — Reads the SovereigntyMetricsStore (PR #1331)
  and distils a live health pulse: per-layer sovereignty %, API
  independence rate, crystallization velocity.

Enhanced routes (src/dashboard/routes/nexus.py):
- GET  /nexus          — now serves introspection + pulse alongside chat
- POST /nexus/chat     — persists messages; tracks memory hits
- DELETE /nexus/history — clears both in-memory and SQLite stores
- GET  /nexus/introspect — JSON API for introspection + sovereignty data
- WS   /nexus/ws       — live push of cognitive state, thought stream,
                          and sovereignty pulse every 5 seconds

Enhanced template (nexus.html):
- Cognitive State panel (mood, engagement, focus, depth, commitments)
- Thought Stream viewer (5 most recent, seed type, timestamps)
- Sovereignty Pulse badge + detail panel (per-layer bars, stats)
- Session Analytics grid (message count, avg response, duration)
- WebSocket client with auto-reconnect for live updates

CSS (mission-control.css):
- Full Nexus v2 design system: pulse badges, health indicators,
  cognitive grid, thought stream cards, sovereignty bar meters,
  analytics grid, scrollable sidebar

Tests: 58 total (all green)
- 20 introspection tests (data models, snapshot, fallback, cognitive/thought readers)
- 18 persistence tests (CRUD, ordering, session tags, pruning, reopen)
- 12 sovereignty pulse tests (classify health, snapshot, API independence)
- 8 route/template tests (new panels, WebSocket script, introspect API)

Refs: #1090
2026-03-24 02:48:22 +00:00

124 lines
4.5 KiB
Python

"""Tests for the Nexus v2 conversational awareness routes."""
from unittest.mock import patch
def test_nexus_page_returns_200(client):
"""GET /nexus should render without error."""
response = client.get("/nexus")
assert response.status_code == 200
assert "NEXUS" in response.text
def test_nexus_page_contains_chat_form(client):
"""Nexus page must include the conversational chat form."""
response = client.get("/nexus")
assert response.status_code == 200
assert "/nexus/chat" in response.text
def test_nexus_page_contains_teach_form(client):
"""Nexus page must include the teaching panel form."""
response = client.get("/nexus")
assert response.status_code == 200
assert "/nexus/teach" in response.text
def test_nexus_page_contains_cognitive_panel(client):
"""Nexus v2 page must include the cognitive state panel."""
response = client.get("/nexus")
assert response.status_code == 200
assert "COGNITIVE STATE" in response.text
def test_nexus_page_contains_thought_stream(client):
"""Nexus v2 page must include the thought stream panel."""
response = client.get("/nexus")
assert response.status_code == 200
assert "THOUGHT STREAM" in response.text
def test_nexus_page_contains_sovereignty_pulse(client):
"""Nexus v2 page must include the sovereignty pulse panel."""
response = client.get("/nexus")
assert response.status_code == 200
assert "SOVEREIGNTY PULSE" in response.text
def test_nexus_page_contains_session_analytics(client):
"""Nexus v2 page must include the session analytics panel."""
response = client.get("/nexus")
assert response.status_code == 200
assert "SESSION ANALYTICS" in response.text
def test_nexus_page_contains_websocket_script(client):
"""Nexus v2 page must include the WebSocket connection script."""
response = client.get("/nexus")
assert response.status_code == 200
assert "/nexus/ws" in response.text
def test_nexus_chat_empty_message_returns_empty(client):
"""POST /nexus/chat with blank message returns empty response."""
response = client.post("/nexus/chat", data={"message": " "})
assert response.status_code == 200
assert response.text == ""
def test_nexus_chat_too_long_returns_error(client):
"""POST /nexus/chat with overlong message returns error partial."""
long_msg = "x" * 10_001
response = client.post("/nexus/chat", data={"message": long_msg})
assert response.status_code == 200
assert "too long" in response.text.lower()
def test_nexus_chat_posts_message(client):
"""POST /nexus/chat calls the session chat function and returns a partial."""
with patch("dashboard.routes.nexus.chat", return_value="Hello from Timmy"):
response = client.post("/nexus/chat", data={"message": "hello"})
assert response.status_code == 200
assert "hello" in response.text.lower() or "timmy" in response.text.lower()
def test_nexus_teach_stores_fact(client):
"""POST /nexus/teach should persist a fact and return confirmation."""
with (
patch("dashboard.routes.nexus.store_personal_fact") as mock_store,
patch("dashboard.routes.nexus.recall_personal_facts_with_ids", return_value=[]),
):
mock_store.return_value = None
response = client.post("/nexus/teach", data={"fact": "Timmy loves Python"})
assert response.status_code == 200
assert "Timmy loves Python" in response.text
def test_nexus_teach_empty_fact_returns_empty(client):
"""POST /nexus/teach with blank fact returns empty response."""
response = client.post("/nexus/teach", data={"fact": " "})
assert response.status_code == 200
assert response.text == ""
def test_nexus_clear_history(client):
"""DELETE /nexus/history should clear the conversation log."""
with patch("dashboard.routes.nexus.reset_session"):
response = client.request("DELETE", "/nexus/history")
assert response.status_code == 200
assert "cleared" in response.text.lower()
def test_nexus_introspect_api(client):
"""GET /nexus/introspect should return JSON introspection snapshot."""
response = client.get("/nexus/introspect")
assert response.status_code == 200
data = response.json()
assert "introspection" in data
assert "sovereignty_pulse" in data
assert "cognitive" in data["introspection"]
assert "recent_thoughts" in data["introspection"]
assert "analytics" in data["introspection"]
assert "overall_pct" in data["sovereignty_pulse"]
assert "health" in data["sovereignty_pulse"]