Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test chat service #2693

Conversation

MohitMaulekhi
Copy link
Contributor

@MohitMaulekhi MohitMaulekhi commented Jan 4, 2025

What kind of change does this PR introduce?

Unit testing for chat service.

Issue Number:

Fixes #2617

Did you add tests for your changes?

yes

Snapshots/Videos:

image

Does this PR introduce a breaking change?

No

Have you read the contributing guide?

yes

Summary by CodeRabbit

  • Tests
    • Enhanced test coverage for ChatService
    • Added new test cases to validate chatListStream and chatMessagesStream
    • Improved assertions for chat message and chat list data validation

Copy link
Contributor

coderabbitai bot commented Jan 4, 2025

Walkthrough

The pull request focuses on enhancing the test coverage for the ChatService in the chat_service_test.dart file. The changes introduce new test cases that validate the behavior of various methods in the service, including stream type checks and detailed message and chat list verification. The modifications aim to improve the robustness of the testing framework by adding more comprehensive assertions and checking the properties of returned data structures.

Changes

File Change Summary
test/service_tests/chat_service_test.dart - Added imports for ChatListTileDataModel and ChatMessage
- Renamed id to chatId for clarity
- Enhanced sendMessageToDirectChat test with Completer and message verification
- Added new test for getDirectChatsByUserId with list validation
- Improved getDirectChatMessagesByChatId test
- Added two new tests for chatListStream and chatMessagesStream stream type checks

Assessment against linked issues

Objective Addressed Explanation
100% Coverage for chat_service.dart [#2617] Partial progress, more tests might be needed for full coverage
Validate all methods in chat_service.dart [#2617] Additional tests may be required to cover all methods comprehensively
Maintain existing functionality [#2617] No functionality appears to have been removed

Poem

🐰 In the realm of tests, a rabbit's delight,
ChatService now shines, with coverage so bright!
Streams checked, messages verified with care,
Our code leaps forward without a single scare! 🚀
Unittests dance, reliability takes flight! 🧪


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Jan 4, 2025

Our Pull Request Approval Process

Thanks for contributing!

Testing Your Code

Remember, your PRs won't be reviewed until these criteria are met:

  1. We don't merge PRs with poor code quality.
    1. Follow coding best practices such that CodeRabbit.ai approves your PR.
  2. We don't merge PRs with failed tests.
    1. When tests fail, click on the Details link to learn more.
    2. Write sufficient tests for your changes (CodeCov Patch Test). Your testing level must be better than the target threshold of the repository
    3. Tests may fail if you edit sensitive files. Ask to add the ignore-sensitive-files-pr label if the edits are necessary.
  3. We cannot merge PRs with conflicting files. These must be fixed.

Our policies make our code better.

Reviewers

Do not assign reviewers. Our Queue Monitors will review your PR and assign them.
When your PR has been assigned reviewers contact them to get your code reviewed and approved via:

  1. comments in this PR or
  2. our slack channel

Reviewing Your Code

Your reviewer(s) will have the following roles:

  1. arbitrators of future discussions with other contributors about the validity of your changes
  2. point of contact for evaluating the validity of your work
  3. person who verifies matching issues by others that should be closed.
  4. person who gives general guidance in fixing your tests

Other

🎯 Please be considerate of our volunteers' time. Contacting the person who assigned the reviewers is not advised unless they ask for your input. Do not @ the person who did the assignment otherwise.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
test/service_tests/chat_service_test.dart (3)

24-32: Enhance error handling and finalize logging.

  1. Consider removing or handling potential mutation errors more explicitly. Currently, if gqlAuthMutation fails, the code doesn’t handle that scenario.
  2. The print(messages.first.id) at line 71 seems like a debugging artifact. Consider removing or converting it to a test expectation or logging statement if it's truly necessary.

Also applies to: 41-60, 63-76


Line range hint 78-130: Potential improvement to reduce reliance on real backend structure.

Tests currently rely on the exact schema of directChatsByUserID. Consider adopting a domain-specific mock or factory pattern for your test data, so that changes to the backend schema do not break tests unnecessarily.


Line range hint 132-156: Check for increased coverage and handling of multiple messages.

Similar to the previous test, you’re collecting messages in a list and completing a Completer. Consider writing a test for multiple messages or lack thereof to ensure the logic in getDirectChatMessagesByChatId holds under all scenarios.

Also applies to: 165-185

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 918eb7e and 422d04b.

📒 Files selected for processing (1)
  • test/service_tests/chat_service_test.dart (7 hunks)
🔇 Additional comments (4)
test/service_tests/chat_service_test.dart (4)

4-11: Imports look good.

The new imports for dart:async, ChatListTileDataModel, and ChatMessage are appropriate for the added tests.


187-190: Stream type checks are meaningful and well-integrated.

Verifying the returned stream types helps ensure that the streams output the correct model objects. This is a sound addition to improve reliability and developer confidence.

Also applies to: 192-194


Line range hint 78-103: Verify data structure correctness.

The test effectively checks the structure of the returned chat data, including user details and chat IDs. Ensure that the real endpoint returns data in the same structure (e.g., confirm the shape of 'directChatsByUserID').

Also applies to: 112-130

✅ Verification successful

Data structure for directChatsByUserID is consistent across the codebase

The verification shows that the data structure is consistent across all relevant files:

  • GraphQL Query (lib/utils/chat_queries.dart and docs): Defines the structure with _id, users array containing _id, firstName, and email
  • Service Implementation (lib/services/chat_service.dart): Processes the data as a List and maps it to ChatListTileDataModel
  • Test Files: Both chat_queries_test.dart and chat_service_test.dart maintain the same structure in their test data

The test's mock data correctly mirrors the structure used in the actual implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
#!/bin/bash
# Description: Verify that 'directChatsByUserID' is consistently structured across the codebase.
rg -A 5 'directChatsByUserID'

Length of output: 2895


55-60: Caution with stream subscription.

While the logic to wait for the first emitted item using Completer is valid, it might be beneficial to handle edge cases, such as when no message arrives or if multiple messages arrive before completer.complete() is called.

Also applies to: 67-70

Copy link

codecov bot commented Jan 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.24%. Comparing base (e578953) to head (422d04b).
Report is 4 commits behind head on develop-postgres.

Additional details and impacted files
@@                 Coverage Diff                  @@
##           develop-postgres    #2693      +/-   ##
====================================================
+ Coverage             96.22%   96.24%   +0.02%     
====================================================
  Files                   189      189              
  Lines                  9994     9994              
====================================================
+ Hits                   9617     9619       +2     
+ Misses                  377      375       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@noman2002 noman2002 merged commit bb59156 into PalisadoesFoundation:develop-postgres Jan 4, 2025
13 checks passed
@MohitMaulekhi MohitMaulekhi deleted the unit-test-chat-service branch January 5, 2025 15:49
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.

2 participants