Skip to content

Conversation

@OxLeOx
Copy link
Contributor

@OxLeOx OxLeOx commented Nov 19, 2025

Add comprehensive test coverage for message module

Summary

This PR adds comprehensive unit test coverage for the chainlit.message module, which previously had no test coverage. The test suite includes 34 test cases covering all message classes and their core functionality.

What's Changed

  • Added: backend/tests/test_message.py - Complete test suite for the message module
    • 34 test cases organized into 7 test classes
    • Covers all public APIs and edge cases
    • Follows existing project testing patterns and conventions

Test Coverage

Test Classes

  1. TestMessageBase (7 tests)

    • Serialization/deserialization (to_dict, from_dict)
    • Update and remove operations
    • Token streaming functionality
    • Edge cases (empty tokens, sequence mode)
  2. TestMessage (9 tests)

    • Initialization with various content types (string, dict, None)
    • Sending messages with actions and elements
    • Updating and removing actions
    • Handling non-serializable content
  3. TestErrorMessage (4 tests)

    • Error message initialization
    • Error persistence and failure handling
    • Custom author support
  4. TestAskUserMessage (4 tests)

    • User input prompts
    • Timeout handling
    • Message removal
  5. TestAskFileMessage (4 tests)

    • File upload prompts
    • Multiple file acceptance formats
    • Response handling and timeouts
  6. TestAskActionMessage (3 tests)

    • Action selection prompts
    • Action lifecycle (send/remove)
    • Timeout scenarios
  7. TestMessageIntegration (2 tests)

    • Messages with combined actions and elements
    • Complex update scenarios

Testing

All tests follow the existing project patterns:

  • Uses pytest with pytest-asyncio for async tests
  • Leverages fixtures from conftest.py (mock_chainlit_context, etc.)
  • Consistent with other test files in the project
  • No linting errors

Expected Results

All 34 tests should pass, providing comprehensive coverage of the message module functionality.

Checklist

  • Tests follow existing project patterns
  • All tests pass locally
  • No linting errors
  • Tests cover core functionality
  • Edge cases are handled
  • Code follows project style guidelines

Related

This addresses the lack of test coverage for the chainlit.message module, which is a core component of the Chainlit framework for handling user and assistant messages.

Contribution by Gittensor, learn more at https://gittensor.io/


Summary by cubic

Added comprehensive unit tests for the message module to verify serialization, streaming, send/update/remove, user/file/action/element prompts, and error handling. This strengthens reliability of core messaging flows and prevents regressions.

  • Tests
    • Added backend/tests/test_message.py with 50 pytest-asyncio cases.
    • Covers Message, ErrorMessage, AskUserMessage, AskFileMessage, AskActionMessage, AskElementMessage, plus actions/elements integration.
    • Validates token streaming (incl. sequence), timeouts, non-serializable content, ask-state clearing, and action/element lifecycle.
    • Follows existing patterns; no runtime changes.

Written for commit 3a4ba01. Summary will update automatically on new commits.

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. backend Pertains to the Python backend. labels Nov 19, 2025
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="backend/tests/test_message.py">

<violation number="1" location="backend/tests/test_message.py:135">
`MessageBase.stream_token` calls `stream_start` on the first token (even with `is_sequence=True`) and only invokes `send_token` once `self.streaming` is already True, so this assertion will always fail for a fresh message. Start the stream (e.g., send an initial token or set `message.streaming = True`) before checking for a `send_token` call.</violation>
</file>

Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR

@OxLeOx OxLeOx marked this pull request as draft November 20, 2025 01:22
@OxLeOx OxLeOx marked this pull request as ready for review November 21, 2025 00:59
@dosubot dosubot bot added the dev-tooling Relating to developer/contributor toolings. label Nov 21, 2025
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

@OxLeOx
Copy link
Contributor Author

OxLeOx commented Nov 21, 2025

want your review @asvishnyakov

@asvishnyakov asvishnyakov changed the title Add Test for Message test: add test for message Nov 24, 2025
@asvishnyakov asvishnyakov added this pull request to the merge queue Nov 24, 2025
Merged via the queue into Chainlit:main with commit 4b9fc48 Nov 24, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Pertains to the Python backend. dev-tooling Relating to developer/contributor toolings. size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants