Skip to content

test: add unit tests for /chat endpoint - #4

Open
lireno wants to merge 1 commit into
mainfrom
test/chat-endpoint-tests
Open

test: add unit tests for /chat endpoint#4
lireno wants to merge 1 commit into
mainfrom
test/chat-endpoint-tests

Conversation

@lireno

@lireno lireno commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Summary

Adds comprehensive unit and integration tests for the /chat endpoint and its underlying functions (openai_chat_stream, convert_to_openai_messages), which previously had zero test coverage.

Changes

Unit tests for convert_to_openai_messages

  • Converts simple user messages
  • Converts assistant messages
  • Converts multiple messages
  • Converts messages with tool invocations (including function_call and function_call_output)
  • Handles tool invocations without results
  • Handles mixed messages (regular + tool invocations)
  • Handles empty messages list

Unit tests for openai_chat_stream

  • Streams text delta events as SSE-formatted strings
  • Streams tool call events (output_item.added, function_call_arguments.delta, function_call_arguments.done)
  • Handles edge case where only one event exists in the stream

Integration tests for /chat endpoint

  • Happy path: validates streaming response with proper SSE format and x-vercel-ai-data-stream header
  • Edge case: empty messages list
  • Edge case: missing API key (403 Forbidden)
  • Edge case: invalid API key (401 Unauthorized)
  • Edge case: missing required fields (422 Unprocessable Entity)
  • Edge case: messages containing tool invocations

Testing

The tests follow existing patterns in the codebase:

  • Pytest fixtures from conftest.py for database and authentication
  • unittest.mock.patch for mocking the OpenAI client in openai_chat_stream
  • FastAPI TestClient for integration tests
  • Same directory structure as other route tests

To run:

cd backend
docker compose exec test-runner pytest aci/server/tests/routes/agent/ -v

🤖 Generated with Claude Code

Add comprehensive tests for the /chat endpoint and underlying
openai_chat_stream / convert_to_openai_messages functions.

Unit tests cover:
- convert_to_openai_messages: user/assistant messages, tool invocations,
  mixed messages, empty lists, tool invocations without results
- openai_chat_stream: text delta streaming, tool call streaming,
  empty stream handling (with mock OpenAI client)

Integration tests cover:
- Happy path: streaming response with proper SSE format and headers
- Empty messages list
- Missing API key (403)
- Invalid API key (401)
- Missing required request body fields (422)
- Messages containing tool invocations

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant