Files
DocsGPT/tests/api/user/test_conversations.py
T
Alex e351f45d88 Feat notification system (#2472)
* feat: SSE notification system

Adds a per-user SSE pipe (GET /api/events) plus a per-message
chat-stream reconnect endpoint (GET /api/messages/<id>/events).

Backend substrate:
- application/events/ — durable journal (Redis Streams) + live
  pub/sub for user-scoped events, with publish_user_event() as
  the worker-side entrypoint.
- application/streaming/ — broadcast_channel for pub/sub fanout
  and event_replay for the per-message snapshot+tail path.
- application/storage/db/repositories/message_events.py +
  alembic 0007 — Postgres journal for chat-stream events.
- application/worker.py — ingest/reingest/remote/connector/
  attachment/mcp_oauth tasks publish queued/progress/completed/
  failed envelopes alongside their existing status updates.

Frontend client:
- frontend/src/events/ — connect/reconnect, Last-Event-ID cursor,
  backoff with jitter. Each tab runs its own connection; no
  cross-tab dedup (future work).
- frontend/src/notifications/ — recentEvents ring, cursor
  tracking, tool-approval toast.
- frontend/src/upload/uploadSlice.ts — extraReducers for
  source.ingest.* and attachment.* events.

Coverage: 132 SSE tests across events substrate, replay, journal,
routes, and worker publishes.

* refactor(attachments): remove polling, SSE-only

frontend/src/components/MessageInput.tsx no longer runs a 2s
setInterval against getTaskStatus for every processing
attachment. The attachment.* SSE reducers in uploadSlice.ts are
now the sole driver of attachment state transitions.

* feat(connector): consume source.ingest.* SSE, remove polling

frontend/src/components/ConnectorTree.tsx now mirrors FileTree's
slice-walking pattern: it watches notifications.recentEvents
for source.ingest.{completed,failed} envelopes matching the
sync's source id, and no longer polls /task_status every 2s.

* refactor(source-ingest): remove polling, SSE-only

frontend/src/upload/Upload.tsx and
frontend/src/components/FileTree.tsx no longer run getTaskStatus
polling fallbacks. The source.ingest.* SSE reducers in
uploadSlice.ts and FileTree's slice walk are now the sole
drivers of upload/reingest state transitions.

* refactor(mcp-oauth): carry authorization_url in SSE, remove polling

application/worker.py::mcp_oauth now publishes
authorization_url on the mcp.oauth.awaiting_redirect envelope.
frontend/src/modals/MCPServerModal.tsx consumes it from SSE
instead of polling /oauth_status/<task_id> every 1s.

The URL is generated inside DocsGPTOAuth.redirect_handler when
the FastMCP client triggers OAuth. The worker now plumbs a
publish callback through tool_config -> MCPTool -> DocsGPTOAuth
so the awaiting_redirect publish fires from inside the handler
at the exact point the URL becomes known. The legacy Redis
mcp_oauth_status setex writes and the GET
/api/mcp_server/oauth_status/<task_id> endpoint are kept as
belt-and-suspenders; nothing in the frontend reads them now.

* feat(source-ingest): plumb limited flag through SSE for token-cap UX

application/worker.py::ingest_worker and remote_worker now publish
``limited: bool`` on the source.ingest.completed envelope.
uploadSlice routes ``payload.limited === true`` to a failed status
with a ``tokenLimitReached`` flag, and UploadToast surfaces the
translated tokenLimit i18n string. No worker code path sets
limited=true today; this is a forward-looking contract so when
token-cap detection lands, the UX is already wired.

* refactor(mcp-oauth): read status from SSE journal, drop polling endpoint

MCPOAuthManager.get_oauth_status now walks the per-user SSE Streams
journal (user:{user_id}:stream) for the latest mcp.oauth.* envelope
matching the task id, returning the status string derived from the
event type suffix and the payload fields. The worker is the single
source of truth — its publish_user_event calls write the same
record the SSE client receives live.

Removed:
- /api/mcp_server/oauth_status/<task_id> route in
  application/api/user/tools/mcp.py
- mcp_oauth_status worker function and mcp_oauth_status_task Celery
  wrapper
- All mcp_oauth_status:{task_id} Redis setex writes (4 in mcp_oauth,
  2 in DocsGPTOAuth.redirect_handler / callback_handler)
- The update_status closure in mcp_oauth that wrote the polling
  payload

Tests updated:
- get_oauth_status now takes (task_id, user_id); new coverage walks
  a fake xrevrange response for the completed envelope, the no-match
  case, and a Redis-down case
- Removed TestMCPOAuthStatus route tests and TestMcpOauthStatusTask
  celery-wrapper test
- Removed the two oauth_status methods from the integration runner

mcp_oauth:auth_url/state/code/error Redis keys remain — they are
the OAuth flow's own state (not the dropped polling payload).

* chore(mcp-oauth): delete orphaned getMCPOAuthStatus client

The /api/mcp_server/oauth_status/<task_id> endpoint was removed in
the prior commit; the corresponding userService method and the
MCP_OAUTH_STATUS endpoint constant had no remaining callers in the
frontend, so they're deleted along with it.

* fix(events): drop live publish when journal write fails

application/events/publisher.py returned an envelope to live
pubsub subscribers even when the XADD to the durable journal
failed. The envelope had no ``id`` field, which bypassed the SSE
route's dedup floor and broke ``Last-Event-ID`` semantics for any
reconnecting client.

Best-effort delivery means dropping consistently, not delivering
inconsistent state. Now: if the journal write fails the publisher
returns None and skips the live publish entirely.

* fix(notifications): dedupe sseEventReceived against immediate dupes

Snapshot replay + live tail can both deliver the same id when the
live pubsub frame and the replay XRANGE overlap. The route's own
dedup floor catches the common case, but consumers walking
``recentEvents`` (FileTree, ConnectorTree, MCPServerModal,
ToolApprovalToast) would otherwise act on the same envelope
twice when a duplicate slipped through.

Belt-and-suspenders: short-circuit when the most recent id in
the ring matches the incoming one.

* fix(events): skip replay budget INCR when no snapshot work possible

_allow_replay incremented the per-user counter on every
/api/events GET, including no-op connects from a fresh client
with no cursor against an empty backlog. React StrictMode dev
double-mounts plus a few tabs trivially tripped the default
30-per-60s budget on idle reconnects.

XLEN pre-check: when last_event_id is None and the user stream
is empty, the connect can't do snapshot work — return True
without INCR. Cursor-bearing connects still INCR unconditionally
(probing the cursor's relationship to stream contents would
require a redundant XRANGE).

* fix(streaming): tighten journal contract + recover from seq collisions

Two related fixes to application/streaming/message_journal.py.

1. record_event now rejects non-dict payloads at the gate. The
   live path (base.py::_emit) wrapped non-dicts as
   {"value": payload}; the replay path in event_replay synthesized
   {"type": event_type}. A reconnecting client would receive a
   different envelope than the one originally streamed. Now both
   paths see byte-identical envelopes because non-dicts can't be
   journaled at all. The corresponding event_replay fallback is
   replaced with a warn-and-skip for any legacy rows.

2. record_event handles IntegrityError on (message_id, sequence_no)
   collisions by reading latest_sequence_no and retrying once with
   latest+1. The most likely cause is a stale seq seed on a
   continuation retry where the route read MAX(seq) from a
   separate connection before another writer committed past it.
   Previously the error was swallowed and the event silently
   dropped from the journal; now it lands at the next available
   seq. The live pubsub publish uses the materialised seq so the
   journal row and the live frame agree.

* perf(streaming): batch message_events INSERTs per stream

complete_stream previously opened a fresh db_session() per yielded
event, doing one Postgres INSERT + commit per chunk on the WSGI
thread. Streaming answers emit ~100s of answer chunks per response,
so the route was paying ~100 PG roundtrips per stream serialized on
commit latency.

New BatchedJournalWriter in application/streaming/message_journal.py
accumulates rows per stream and flushes on three triggers:
- size: buffer reaches 16 entries
- time: 100ms elapsed since the last flush
- lifecycle: close() at end-of-stream

Live pubsub publishes still fire synchronously per record(), so
subscribers see events in real time — only the durable journal write
is amortized. On bulk INSERT IntegrityError the writer falls back to
per-row record() with the existing seq+1 retry so a single colliding
seq doesn't drop the rest of the batch.

complete_stream wires journal_writer.close() into every exit path
(happy end, tool-approval-paused end, GeneratorExit, error handler)
so the terminal event is committed before the generator returns —
otherwise a reconnecting client could snapshot up to the last flush
boundary and live-tail waiting for an end that's still in memory.

Repository gets bulk_record() — one SQLAlchemy executemany INSERT
for the bulk path. All-or-nothing on collision (Postgres aborts the
whole batch); the writer's per-row fallback handles recovery.

* chore(upload): drop dead UploadTask.lastEventAt field

The lastEventAt field on UploadTask had no remaining consumers — the
matching Attachment.lastEventAt was cleaned up earlier. Remove the
field declaration and the slice write site.

* chore(frontend): drop orphaned getTaskStatus client

After the polling-removal sweep no caller in frontend/src/ references
userService.getTaskStatus or endpoints.USER.TASK_STATUS. The backend
route /api/task_status itself stays — agents, webhooks, e2e specs,
and the public docs still depend on it.

* docs(repo): remove stale planning docs from repo root

notification-channel-design.md, plan.md, and reminder-tool-design.md
were leftover Claude planning artifacts from the SSE substrate work
that landed accidentally. CLAUDE.md prohibits creating planning docs
unless asked — delete them.

* docs(message-events): clarify repo vs wrapper payload contract

MessageEventsRepository.record accepts any JSONB-compatible value; the
streaming wrapper record_event tightens this to dicts only because the
live and replay paths reconstruct non-dict payloads differently. Spell
the split out so the next reader of the repo method doesn't assume the
wrapper's contract applies here.

* refactor(events): raise on malformed stream id instead of lex fallback

stream_id_compare's lex-fallback branch was a footgun: a malformed id
that sorts lex-greater than a real one would pin live-tail dedup
forever, dropping every subsequent legitimate event silently. Both
current callers in application/api/events/routes.py pre-validate
inputs against _STREAM_ID_RE before calling, so changing the function
to raise ValueError is a no-op on the happy path and turns the future-
caller footgun into a loud failure.

* test(tasks): cover cleanup_message_events task body

Adds skipped-when-no-POSTGRES_URI and happy-path coverage for the
Celery janitor. The skipped path returns the documented short-circuit
shape without touching the repo. The happy path seeds a backdated
row, runs the task against the pg_conn fixture, and asserts the
retention window's row is deleted while in-window rows survive.
Mirrors the TestCleanupPendingToolState pattern.

* fix(notifications): treat /c/new as no current conversation

useMatch('/c/:conversationId') treats the literal URL /c/new as a
real conversation id, so the toast suppression check confused
'user is on /c/new' with 'user is on the conversation needing
approval'. Explicit guard: when the matched id is 'new', fall
through to the no-match case so approval toasts still surface.

* docs(events): enumerate publish_user_event None-return paths

The function returns Optional[str] today, with None conflating five
distinct outcomes (missing args / push disabled / unserialisable /
Redis down / XADD failed). Every current call site is fire-and-
forget and ignores the return, so the right move is to document the
five cases rather than promote to an enum return — keeps the API
small while making the diagnostic surface (logs) obvious. If a
future caller needs to react differently per reason, promote then.

* refactor(sources): move source-id derivation out of worker module

application/api/user/sources/upload.py imported _derive_source_id
from application.worker — pulling the entire Celery worker module
into the API process at import time just for a two-line helper.

Move DOCSGPT_INGEST_NAMESPACE and the derivation function to a
new application/storage/db/source_ids.py module that both layers
can import without that dependency edge. worker.py re-exports the
old names (_derive_source_id, DOCSGPT_INGEST_NAMESPACE) for
backward-compatible imports from tests and any other in-tree
callers; new code should import from the new module directly.

* fix(cache): enable Redis health_check_interval to surface half-open TCP

Without health_check_interval, a half-open TCP socket (NAT silently
dropped state, ELB idle-close) can leave pubsub.get_message hanging
past the SSE generator's keepalive cadence — the kernel never
surfaces the dead socket because no payload is in flight. Setting
health_check_interval=10 makes redis-py ping every 10s when
otherwise idle, so the next get_message after the dead window
raises and the SSE loop falls into its reconnect path instead of
silently freezing on the user.

* chore(events): rename attachment.processing.progress to attachment.progress

The event-type taxonomy was inconsistent: source ingest emits
source.ingest.progress (three segments) while attachments emitted
attachment.processing.progress (four segments). Drops the
.processing. infix for parity. Worker publish sites, the slice
reducer's match, and the worker tests all flip together.

No external consumers — the event type is purely internal between
the publisher and the in-tab slice; safe to rename in one commit.

* feat: events cleanup

* fix: better docs

* fix: e2e tests
2026-05-15 12:23:31 +01:00

772 lines
25 KiB
Python

import uuid
from contextlib import contextmanager
from unittest.mock import patch
import pytest
from flask import Flask
@pytest.fixture
def app():
app = Flask(__name__)
return app
@contextmanager
def _patch_conversations_db(conn):
@contextmanager
def _yield_conn():
yield conn
with patch(
"application.api.user.conversations.routes.db_session", _yield_conn
), patch(
"application.api.user.conversations.routes.db_readonly", _yield_conn
):
yield
def _seed_conversation(pg_conn, user_id, name="Test Conv"):
"""Create a conversation and return its PG uuid id as str."""
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
repo = ConversationsRepository(pg_conn)
conv = repo.create(user_id, name=name)
return str(conv["id"])
@pytest.mark.unit
class TestDeleteConversation:
pass
def test_returns_401_unauthenticated(self, app):
from application.api.user.conversations.routes import DeleteConversation
with app.test_request_context("/api/delete_conversation?id=abc"):
from flask import request
request.decoded_token = None
response = DeleteConversation().post()
assert response.status_code == 401
def test_returns_400_missing_id(self, app):
from application.api.user.conversations.routes import DeleteConversation
with app.test_request_context("/api/delete_conversation"):
from flask import request
request.decoded_token = {"sub": "user1"}
response = DeleteConversation().post()
assert response.status_code == 400
@pytest.mark.unit
class TestDeleteAllConversations:
pass
def test_returns_401_unauthenticated(self, app):
from application.api.user.conversations.routes import DeleteAllConversations
with app.test_request_context("/api/delete_all_conversations"):
from flask import request
request.decoded_token = None
response = DeleteAllConversations().get()
assert response.status_code == 401
@pytest.mark.unit
class TestGetConversations:
pass
def test_returns_401_unauthenticated(self, app):
from application.api.user.conversations.routes import GetConversations
with app.test_request_context("/api/get_conversations"):
from flask import request
request.decoded_token = None
response = GetConversations().get()
assert response.status_code == 401
@pytest.mark.unit
class TestGetSingleConversation:
pass
def test_returns_400_missing_id(self, app):
from application.api.user.conversations.routes import GetSingleConversation
with app.test_request_context("/api/get_single_conversation"):
from flask import request
request.decoded_token = {"sub": "user1"}
response = GetSingleConversation().get()
assert response.status_code == 400
@pytest.mark.unit
class TestUpdateConversationName:
pass
def test_returns_400_missing_fields(self, app):
from application.api.user.conversations.routes import UpdateConversationName
with app.test_request_context(
"/api/update_conversation_name",
method="POST",
json={"id": str(uuid.uuid4().hex[:24])},
):
from flask import request
request.decoded_token = {"sub": "user1"}
response = UpdateConversationName().post()
assert response.status_code == 400
@pytest.mark.unit
class TestSubmitFeedback:
pass
def test_returns_400_missing_fields(self, app):
from application.api.user.conversations.routes import SubmitFeedback
with app.test_request_context(
"/api/feedback",
method="POST",
json={"feedback": "LIKE"},
):
from flask import request
request.decoded_token = {"sub": "user1"}
response = SubmitFeedback().post()
assert response.status_code == 400
# ---------------------------------------------------------------------------
# Happy-path tests exercising real PG via the ephemeral pg_conn fixture.
# ---------------------------------------------------------------------------
class TestDeleteConversationHappy:
def test_deletes_existing_conversation(self, app, pg_conn):
from application.api.user.conversations.routes import DeleteConversation
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
user = "user-del"
conv_id = _seed_conversation(pg_conn, user)
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/delete_conversation?id={conv_id}"
):
from flask import request
request.decoded_token = {"sub": user}
response = DeleteConversation().post()
assert response.status_code == 200
assert response.json["success"] is True
# Gone
assert ConversationsRepository(pg_conn).get_any(conv_id, user) is None
def test_delete_nonexistent_still_returns_200(self, app, pg_conn):
"""get_any returns None, so delete is a no-op but endpoint succeeds."""
from application.api.user.conversations.routes import DeleteConversation
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/delete_conversation?id={uuid.uuid4()}"
):
from flask import request
request.decoded_token = {"sub": "u"}
response = DeleteConversation().post()
assert response.status_code == 200
def test_db_error_returns_400(self, app):
from application.api.user.conversations.routes import DeleteConversation
@contextmanager
def _broken():
raise RuntimeError("boom")
yield
with patch(
"application.api.user.conversations.routes.db_session", _broken
), app.test_request_context("/api/delete_conversation?id=abc"):
from flask import request
request.decoded_token = {"sub": "u"}
response = DeleteConversation().post()
assert response.status_code == 400
class TestDeleteAllConversationsHappy:
def test_deletes_all_conversations(self, app, pg_conn):
from application.api.user.conversations.routes import (
DeleteAllConversations,
)
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
user = "user-delall"
_seed_conversation(pg_conn, user, name="a")
_seed_conversation(pg_conn, user, name="b")
with _patch_conversations_db(pg_conn), app.test_request_context(
"/api/delete_all_conversations"
):
from flask import request
request.decoded_token = {"sub": user}
response = DeleteAllConversations().get()
assert response.status_code == 200
assert ConversationsRepository(pg_conn).list_for_user(user) == []
def test_db_error_returns_400(self, app):
from application.api.user.conversations.routes import (
DeleteAllConversations,
)
@contextmanager
def _broken():
raise RuntimeError("boom")
yield
with patch(
"application.api.user.conversations.routes.db_session", _broken
), app.test_request_context("/api/delete_all_conversations"):
from flask import request
request.decoded_token = {"sub": "u"}
response = DeleteAllConversations().get()
assert response.status_code == 400
class TestGetConversationsHappy:
def test_returns_list_of_conversations(self, app, pg_conn):
from application.api.user.conversations.routes import GetConversations
user = "user-list"
c1 = _seed_conversation(pg_conn, user, name="one")
c2 = _seed_conversation(pg_conn, user, name="two")
with _patch_conversations_db(pg_conn), app.test_request_context(
"/api/get_conversations"
):
from flask import request
request.decoded_token = {"sub": user}
response = GetConversations().get()
assert response.status_code == 200
ids = {c["id"] for c in response.json}
assert c1 in ids and c2 in ids
# agent_id, is_shared_usage, shared_token keys present
for c in response.json:
assert "agent_id" in c
assert "is_shared_usage" in c
assert "shared_token" in c
def test_db_error_returns_400(self, app):
from application.api.user.conversations.routes import GetConversations
@contextmanager
def _broken():
raise RuntimeError("boom")
yield
with patch(
"application.api.user.conversations.routes.db_readonly", _broken
), app.test_request_context("/api/get_conversations"):
from flask import request
request.decoded_token = {"sub": "u"}
response = GetConversations().get()
assert response.status_code == 400
class TestGetSingleConversationHappy:
def test_returns_401_unauthenticated(self, app):
from application.api.user.conversations.routes import (
GetSingleConversation,
)
with app.test_request_context("/api/get_single_conversation?id=x"):
from flask import request
request.decoded_token = None
response = GetSingleConversation().get()
assert response.status_code == 401
def test_returns_404_not_found(self, app, pg_conn):
from application.api.user.conversations.routes import (
GetSingleConversation,
)
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/get_single_conversation?id={uuid.uuid4()}"
):
from flask import request
request.decoded_token = {"sub": "u"}
response = GetSingleConversation().get()
assert response.status_code == 404
def test_returns_conversation_with_messages(self, app, pg_conn):
from application.api.user.conversations.routes import (
GetSingleConversation,
)
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
user = "user-get"
conv_id = _seed_conversation(pg_conn, user, name="chat")
# Append a message
ConversationsRepository(pg_conn).append_message(
conv_id,
{
"prompt": "hi",
"response": "hello",
"thought": None,
"sources": [],
"tool_calls": [],
"timestamp": None,
"model_id": None,
},
)
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/get_single_conversation?id={conv_id}"
):
from flask import request
request.decoded_token = {"sub": user}
response = GetSingleConversation().get()
assert response.status_code == 200
data = response.json
assert isinstance(data["queries"], list)
assert data["queries"][0]["prompt"] == "hi"
assert data["queries"][0]["response"] == "hello"
def test_returns_message_with_dict_feedback(self, app, pg_conn):
from application.api.user.conversations.routes import (
GetSingleConversation,
)
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
user = "user-fb"
conv_id = _seed_conversation(pg_conn, user, name="fb")
repo = ConversationsRepository(pg_conn)
repo.append_message(conv_id, {"prompt": "p", "response": "r"})
repo.set_feedback(
conv_id, 0, {"text": "like", "timestamp": "2024-01-01T00:00:00Z"}
)
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/get_single_conversation?id={conv_id}"
):
from flask import request
request.decoded_token = {"sub": user}
response = GetSingleConversation().get()
assert response.status_code == 200
q = response.json["queries"][0]
assert q["feedback"] == "like"
assert q["feedback_timestamp"] == "2024-01-01T00:00:00Z"
def test_db_error_returns_400(self, app):
from application.api.user.conversations.routes import (
GetSingleConversation,
)
@contextmanager
def _broken():
raise RuntimeError("boom")
yield
with patch(
"application.api.user.conversations.routes.db_readonly", _broken
), app.test_request_context("/api/get_single_conversation?id=abc"):
from flask import request
request.decoded_token = {"sub": "u"}
response = GetSingleConversation().get()
assert response.status_code == 400
@pytest.mark.unit
class TestGetMessageTail:
"""Tail-poll endpoint (``GET /api/messages/<id>/tail``) used by the
frontend to recover a placeholder/streaming row after a refresh.
"""
def _seed_in_flight_message(self, pg_conn, owner_user_id):
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
conv_id = _seed_conversation(pg_conn, owner_user_id, name="streaming chat")
repo = ConversationsRepository(pg_conn)
msg = repo.reserve_message(
conv_id,
prompt="what's happening?",
placeholder_response=(
"Response was terminated prior to completion, try regenerating."
),
request_id=str(uuid.uuid4()),
status="streaming",
)
return conv_id, str(msg["id"])
def test_owner_can_tail(self, app, pg_conn):
from application.api.user.conversations.routes import GetMessageTail
owner = "user-owner"
_, msg_id = self._seed_in_flight_message(pg_conn, owner)
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/messages/{msg_id}/tail"
):
from flask import request
request.decoded_token = {"sub": owner}
response = GetMessageTail().get(msg_id)
assert response.status_code == 200
assert response.json["status"] == "streaming"
assert response.json["message_id"] == msg_id
def test_shared_user_can_tail(self, app, pg_conn):
"""A user in ``conversations.shared_with`` must be able to tail
an in-flight placeholder. Without the shared-with predicate
here, ``get_single_conversation`` lets them load the row but
the tail-poll silently 404s and the in-flight bubble never
resolves on the shared user's side.
"""
from application.api.user.conversations.routes import GetMessageTail
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
owner = "user-owner-shared"
shared_user = "user-shared"
conv_id, msg_id = self._seed_in_flight_message(pg_conn, owner)
ConversationsRepository(pg_conn).add_shared_user(conv_id, shared_user)
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/messages/{msg_id}/tail"
):
from flask import request
request.decoded_token = {"sub": shared_user}
response = GetMessageTail().get(msg_id)
assert response.status_code == 200
assert response.json["message_id"] == msg_id
def test_non_member_gets_404(self, app, pg_conn):
from application.api.user.conversations.routes import GetMessageTail
owner = "user-owner-private"
intruder = "user-intruder"
_, msg_id = self._seed_in_flight_message(pg_conn, owner)
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/messages/{msg_id}/tail"
):
from flask import request
request.decoded_token = {"sub": intruder}
response = GetMessageTail().get(msg_id)
assert response.status_code == 404
def test_streaming_row_returns_partial_from_journal(self, app, pg_conn):
"""Mid-stream rows must rebuild from message_events, not return the placeholder."""
from application.api.user.conversations.routes import GetMessageTail
from application.storage.db.repositories.message_events import (
MessageEventsRepository,
)
owner = "user-tail-partial"
_, msg_id = self._seed_in_flight_message(pg_conn, owner)
events_repo = MessageEventsRepository(pg_conn)
events_repo.record(msg_id, 0, "message_id", {"type": "message_id"})
events_repo.record(msg_id, 1, "answer", {"type": "answer", "answer": "Hello"})
events_repo.record(msg_id, 2, "answer", {"type": "answer", "answer": ", world"})
events_repo.record(
msg_id, 3, "source", {"type": "source", "source": [{"id": "s1"}]}
)
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/messages/{msg_id}/tail"
):
from flask import request
request.decoded_token = {"sub": owner}
response = GetMessageTail().get(msg_id)
assert response.status_code == 200
assert response.json["status"] == "streaming"
assert response.json["response"] == "Hello, world"
assert response.json["sources"] == [{"id": "s1"}]
assert "terminated prior to completion" not in (
response.json["response"] or ""
)
def test_streaming_row_with_empty_journal_returns_empty_response(
self, app, pg_conn
):
"""Empty journal returns empty response, not the placeholder."""
from application.api.user.conversations.routes import GetMessageTail
owner = "user-tail-empty"
_, msg_id = self._seed_in_flight_message(pg_conn, owner)
with _patch_conversations_db(pg_conn), app.test_request_context(
f"/api/messages/{msg_id}/tail"
):
from flask import request
request.decoded_token = {"sub": owner}
response = GetMessageTail().get(msg_id)
assert response.status_code == 200
assert response.json["status"] == "streaming"
assert response.json["response"] == ""
class TestUpdateConversationNameHappy:
def test_returns_401_unauthenticated(self, app):
from application.api.user.conversations.routes import (
UpdateConversationName,
)
with app.test_request_context(
"/api/update_conversation_name",
method="POST",
json={"id": "x", "name": "n"},
):
from flask import request
request.decoded_token = None
response = UpdateConversationName().post()
assert response.status_code == 401
def test_renames_conversation(self, app, pg_conn):
from application.api.user.conversations.routes import (
UpdateConversationName,
)
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
user = "user-rename"
conv_id = _seed_conversation(pg_conn, user, name="old")
with _patch_conversations_db(pg_conn), app.test_request_context(
"/api/update_conversation_name",
method="POST",
json={"id": conv_id, "name": "new"},
):
from flask import request
request.decoded_token = {"sub": user}
response = UpdateConversationName().post()
assert response.status_code == 200
got = ConversationsRepository(pg_conn).get_any(conv_id, user)
assert got["name"] == "new"
def test_rename_nonexistent_still_returns_200(self, app, pg_conn):
from application.api.user.conversations.routes import (
UpdateConversationName,
)
with _patch_conversations_db(pg_conn), app.test_request_context(
"/api/update_conversation_name",
method="POST",
json={"id": str(uuid.uuid4()), "name": "n"},
):
from flask import request
request.decoded_token = {"sub": "u"}
response = UpdateConversationName().post()
assert response.status_code == 200
def test_db_error_returns_400(self, app):
from application.api.user.conversations.routes import (
UpdateConversationName,
)
@contextmanager
def _broken():
raise RuntimeError("boom")
yield
with patch(
"application.api.user.conversations.routes.db_session", _broken
), app.test_request_context(
"/api/update_conversation_name",
method="POST",
json={"id": "x", "name": "n"},
):
from flask import request
request.decoded_token = {"sub": "u"}
response = UpdateConversationName().post()
assert response.status_code == 400
class TestSubmitFeedbackHappy:
def test_returns_401_unauthenticated(self, app):
from application.api.user.conversations.routes import SubmitFeedback
with app.test_request_context(
"/api/feedback",
method="POST",
json={
"feedback": "like",
"question_index": 0,
"conversation_id": "x",
},
):
from flask import request
request.decoded_token = None
response = SubmitFeedback().post()
assert response.status_code == 401
def test_submits_feedback(self, app, pg_conn):
from application.api.user.conversations.routes import SubmitFeedback
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
user = "user-fb1"
conv_id = _seed_conversation(pg_conn, user, name="fb")
ConversationsRepository(pg_conn).append_message(
conv_id, {"prompt": "p", "response": "r"}
)
with _patch_conversations_db(pg_conn), app.test_request_context(
"/api/feedback",
method="POST",
json={
"feedback": "LIKE", # uppercase normalized to lowercase
"question_index": 0,
"conversation_id": conv_id,
},
):
from flask import request
request.decoded_token = {"sub": user}
response = SubmitFeedback().post()
assert response.status_code == 200
msgs = ConversationsRepository(pg_conn).get_messages(conv_id)
fb = msgs[0].get("feedback")
assert fb and fb.get("text") == "like"
def test_none_feedback_allowed(self, app, pg_conn):
from application.api.user.conversations.routes import SubmitFeedback
from application.storage.db.repositories.conversations import (
ConversationsRepository,
)
user = "user-fb-none"
conv_id = _seed_conversation(pg_conn, user)
ConversationsRepository(pg_conn).append_message(
conv_id, {"prompt": "p", "response": "r"}
)
with _patch_conversations_db(pg_conn), app.test_request_context(
"/api/feedback",
method="POST",
json={
"feedback": None,
"question_index": 0,
"conversation_id": conv_id,
},
):
from flask import request
request.decoded_token = {"sub": user}
response = SubmitFeedback().post()
assert response.status_code == 200
def test_returns_404_for_missing_conversation(self, app, pg_conn):
from application.api.user.conversations.routes import SubmitFeedback
with _patch_conversations_db(pg_conn), app.test_request_context(
"/api/feedback",
method="POST",
json={
"feedback": "like",
"question_index": 0,
"conversation_id": str(uuid.uuid4()),
},
):
from flask import request
request.decoded_token = {"sub": "u"}
response = SubmitFeedback().post()
assert response.status_code == 404
def test_db_error_returns_400(self, app):
from application.api.user.conversations.routes import SubmitFeedback
@contextmanager
def _broken():
raise RuntimeError("boom")
yield
with patch(
"application.api.user.conversations.routes.db_session", _broken
), app.test_request_context(
"/api/feedback",
method="POST",
json={
"feedback": "like",
"question_index": 0,
"conversation_id": "x",
},
):
from flask import request
request.decoded_token = {"sub": "u"}
response = SubmitFeedback().post()
assert response.status_code == 400