This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Timmy-time-dashboard/tests/timmy/test_session.py
Alexander Whitestone 9d78eb31d1 ruff (#169)
* polish: streamline nav, extract inline styles, improve tablet UX

- Restructure desktop nav from 8+ flat links + overflow dropdown into
  5 grouped dropdowns (Core, Agents, Intel, System, More) matching
  the mobile menu structure to reduce decision fatigue
- Extract all inline styles from mission_control.html and base.html
  notification elements into mission-control.css with semantic classes
- Replace JS-built innerHTML with secure DOM construction in
  notification loader and chat history
- Add CONNECTING state to connection indicator (amber) instead of
  showing OFFLINE before WebSocket connects
- Add tablet breakpoint (1024px) with larger touch targets for
  Apple Pencil / stylus use and safe-area padding for iPad toolbar
- Add active-link highlighting in desktop dropdown menus
- Rename "Mission Control" page title to "System Overview" to
  disambiguate from the chat home page
- Add "Home — Timmy Time" page title to index.html

https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h

* fix(security): move auth-gate credentials to environment variables

Hardcoded username, password, and HMAC secret in auth-gate.py replaced
with os.environ lookups. Startup now refuses to run if any variable is
unset. Added AUTH_GATE_SECRET/USER/PASS to .env.example.

https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h

* refactor(tooling): migrate from black+isort+bandit to ruff

Replace three separate linting/formatting tools with a single ruff
invocation. Updates tox.ini (lint, format, pre-push, pre-commit envs),
.pre-commit-config.yaml, and CI workflow. Fixes all ruff errors
including unused imports, missing raise-from, and undefined names.
Ruff config maps existing bandit skips to equivalent S-rules.

https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-11 12:23:35 -04:00

210 lines
6.6 KiB
Python

"""Tests for timmy.session — persistent chat session with response sanitization."""
from unittest.mock import MagicMock, patch
import pytest
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture(autouse=True)
def _reset_session_singleton():
"""Reset the module-level singleton between tests."""
import timmy.session as mod
mod._agent = None
yield
mod._agent = None
# ---------------------------------------------------------------------------
# chat()
# ---------------------------------------------------------------------------
def test_chat_returns_string():
"""chat() should return a plain string response."""
mock_agent = MagicMock()
mock_agent.run.return_value = MagicMock(content="Hello, sir.")
with patch("timmy.session._get_agent", return_value=mock_agent):
from timmy.session import chat
result = chat("Hi Timmy")
assert isinstance(result, str)
assert "Hello, sir." in result
def test_chat_passes_session_id():
"""chat() should pass the session_id to agent.run()."""
mock_agent = MagicMock()
mock_agent.run.return_value = MagicMock(content="OK.")
with patch("timmy.session._get_agent", return_value=mock_agent):
from timmy.session import chat
chat("test", session_id="my-session")
_, kwargs = mock_agent.run.call_args
assert kwargs["session_id"] == "my-session"
def test_chat_uses_default_session_id():
"""chat() should use 'dashboard' as the default session_id."""
mock_agent = MagicMock()
mock_agent.run.return_value = MagicMock(content="OK.")
with patch("timmy.session._get_agent", return_value=mock_agent):
from timmy.session import chat
chat("test")
_, kwargs = mock_agent.run.call_args
assert kwargs["session_id"] == "dashboard"
def test_chat_singleton_agent_reused():
"""Calling chat() multiple times should reuse the same agent instance."""
mock_agent = MagicMock()
mock_agent.run.return_value = MagicMock(content="OK.")
with patch("timmy.agent.create_timmy", return_value=mock_agent) as mock_factory:
from timmy.session import chat
chat("first message")
chat("second message")
# Factory called only once (singleton)
mock_factory.assert_called_once()
def test_chat_extracts_user_name():
"""chat() should extract user name from message and persist to memory."""
mock_agent = MagicMock()
mock_agent.run.return_value = MagicMock(content="Nice to meet you!")
mock_mem = MagicMock()
with (
patch("timmy.session._get_agent", return_value=mock_agent),
patch("timmy.memory_system.memory_system", mock_mem),
):
from timmy.session import chat
chat("my name is Alex")
mock_mem.update_user_fact.assert_called_once_with("Name", "Alex")
def test_chat_graceful_degradation_on_memory_failure():
"""chat() should still work if the conversation manager raises."""
mock_agent = MagicMock()
mock_agent.run.return_value = MagicMock(content="I'm operational.")
with (
patch("timmy.session._get_agent", return_value=mock_agent),
patch("timmy.conversation.conversation_manager") as mock_cm,
):
mock_cm.extract_user_name.side_effect = Exception("memory broken")
from timmy.session import chat
result = chat("test message")
assert "operational" in result
# ---------------------------------------------------------------------------
# _clean_response()
# ---------------------------------------------------------------------------
def test_clean_response_strips_json_tool_calls():
"""JSON tool call blocks should be removed from response text."""
from timmy.session import _clean_response
dirty = 'Here is the answer. {"name": "python", "parameters": {"code": "0.15 * 3847.23", "variable_to_return": "result"}} The result is 577.'
clean = _clean_response(dirty)
assert '{"name"' not in clean
assert '"parameters"' not in clean
assert "The result is 577." in clean
def test_clean_response_strips_arguments_format():
"""JSON tool calls using 'arguments' key (OpenAI format) should also be removed."""
from timmy.session import _clean_response
dirty = 'Here is the result. {"name": "shell", "arguments": {"args": ["mkdir", "-p", "/tmp/test"]}} The directory was created.'
clean = _clean_response(dirty)
assert '{"name"' not in clean
assert '"arguments"' not in clean
assert "The directory was created." in clean
def test_clean_response_strips_function_calls():
"""Function-call-style text should be removed."""
from timmy.session import _clean_response
dirty = 'I will search for that. memory_search(query="recall number") Found nothing.'
clean = _clean_response(dirty)
assert "memory_search(" not in clean
assert "Found nothing." in clean
def test_clean_response_strips_chain_of_thought():
"""Chain-of-thought narration lines should be removed."""
from timmy.session import _clean_response
dirty = """Since there's no direct answer in my vault or hot memory, I'll use memory_search.
Using memory_search(query="what is special"), I found a context.
Here's a possible response:
77 is special because it's a prime number."""
clean = _clean_response(dirty)
assert "Since there's no" not in clean
assert "Here's a possible" not in clean
assert "77 is special" in clean
def test_clean_response_preserves_normal_text():
"""Normal text without tool artifacts should pass through unchanged."""
from timmy.session import _clean_response
normal = "The number 77 is the sum of the first seven primes: 2+3+5+7+11+13+17."
assert _clean_response(normal) == normal
def test_clean_response_handles_empty_string():
"""Empty string should be returned as-is."""
from timmy.session import _clean_response
assert _clean_response("") == ""
def test_clean_response_handles_none():
"""None should be returned as-is."""
from timmy.session import _clean_response
assert _clean_response(None) is None
# ---------------------------------------------------------------------------
# reset_session()
# ---------------------------------------------------------------------------
def test_reset_session_clears_context():
"""reset_session() should clear the conversation context."""
with patch("timmy.conversation.conversation_manager") as mock_cm:
from timmy.session import reset_session
reset_session("test-session")
mock_cm.clear_context.assert_called_once_with("test-session")