feat: add backend tests for AI chat builder + fix conversation_id FK issue
Tests cover session create, send message with tree update, get session, abandon, 404 on missing session, and 503 when AI disabled. Fixed: ai_usage.conversation_id has FK to ai_conversations, not ai_chat_sessions. Chat builder now passes conversation_id=None and tracks session reference in extra_data.chat_session_id. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -106,7 +106,7 @@ async def create_session(
|
||||
await record_ai_usage(
|
||||
user_id=current_user.id,
|
||||
account_id=current_user.account_id,
|
||||
conversation_id=session.id,
|
||||
conversation_id=None,
|
||||
generation_type="chat_message",
|
||||
tier=plan,
|
||||
input_tokens=session.total_input_tokens,
|
||||
@@ -118,7 +118,7 @@ async def create_session(
|
||||
succeeded=True,
|
||||
counts_toward_quota=False,
|
||||
error_code=None,
|
||||
extra_data={"phase": "scoping"},
|
||||
extra_data={"phase": "scoping", "chat_session_id": str(session.id)},
|
||||
db=db,
|
||||
)
|
||||
|
||||
@@ -175,7 +175,7 @@ async def post_message(
|
||||
await record_ai_usage(
|
||||
user_id=current_user.id,
|
||||
account_id=current_user.account_id,
|
||||
conversation_id=session.id,
|
||||
conversation_id=None,
|
||||
generation_type="chat_message",
|
||||
tier=plan,
|
||||
input_tokens=0,
|
||||
@@ -184,7 +184,7 @@ async def post_message(
|
||||
succeeded=False,
|
||||
counts_toward_quota=False,
|
||||
error_code=type(e).__name__,
|
||||
extra_data=None,
|
||||
extra_data={"chat_session_id": str(session.id)},
|
||||
db=db,
|
||||
)
|
||||
await db.commit()
|
||||
@@ -198,7 +198,7 @@ async def post_message(
|
||||
await record_ai_usage(
|
||||
user_id=current_user.id,
|
||||
account_id=current_user.account_id,
|
||||
conversation_id=session.id,
|
||||
conversation_id=None,
|
||||
generation_type="chat_message",
|
||||
tier=plan,
|
||||
input_tokens=input_delta,
|
||||
@@ -210,7 +210,7 @@ async def post_message(
|
||||
succeeded=True,
|
||||
counts_toward_quota=False,
|
||||
error_code=None,
|
||||
extra_data={"phase": session.current_phase},
|
||||
extra_data={"phase": session.current_phase, "chat_session_id": str(session.id)},
|
||||
db=db,
|
||||
)
|
||||
|
||||
@@ -288,7 +288,7 @@ async def generate_tree(
|
||||
await record_ai_usage(
|
||||
user_id=current_user.id,
|
||||
account_id=current_user.account_id,
|
||||
conversation_id=session.id,
|
||||
conversation_id=None,
|
||||
generation_type="chat_generate",
|
||||
tier=plan,
|
||||
input_tokens=session.total_input_tokens - prev_input,
|
||||
@@ -297,7 +297,7 @@ async def generate_tree(
|
||||
succeeded=False,
|
||||
counts_toward_quota=False,
|
||||
error_code="invalid_output",
|
||||
extra_data={"error": str(e)},
|
||||
extra_data={"error": str(e), "chat_session_id": str(session.id)},
|
||||
db=db,
|
||||
)
|
||||
await db.commit()
|
||||
@@ -312,7 +312,7 @@ async def generate_tree(
|
||||
await record_ai_usage(
|
||||
user_id=current_user.id,
|
||||
account_id=current_user.account_id,
|
||||
conversation_id=session.id,
|
||||
conversation_id=None,
|
||||
generation_type="chat_generate",
|
||||
tier=plan,
|
||||
input_tokens=input_delta,
|
||||
@@ -321,7 +321,7 @@ async def generate_tree(
|
||||
succeeded=False,
|
||||
counts_toward_quota=False,
|
||||
error_code=type(e).__name__,
|
||||
extra_data={"error": str(e)},
|
||||
extra_data={"error": str(e), "chat_session_id": str(session.id)},
|
||||
db=db,
|
||||
)
|
||||
await db.commit()
|
||||
@@ -342,7 +342,7 @@ async def generate_tree(
|
||||
await record_ai_usage(
|
||||
user_id=current_user.id,
|
||||
account_id=current_user.account_id,
|
||||
conversation_id=session.id,
|
||||
conversation_id=None,
|
||||
generation_type="chat_generate",
|
||||
tier=plan,
|
||||
input_tokens=input_delta,
|
||||
@@ -354,7 +354,7 @@ async def generate_tree(
|
||||
succeeded=True,
|
||||
counts_toward_quota=True,
|
||||
error_code=None,
|
||||
extra_data=None,
|
||||
extra_data={"chat_session_id": str(session.id)},
|
||||
db=db,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user