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

Fix integration tests #177

Merged
merged 4 commits into from
Dec 4, 2024
Merged

Fix integration tests #177

merged 4 commits into from
Dec 4, 2024

Conversation

lawrence-forooghian
Copy link
Collaborator

@lawrence-forooghian lawrence-forooghian commented Dec 4, 2024

Fixes integration tests so that we can start merging other PRs.

Summary by CodeRabbit

  • New Features

    • Introduced a new method for handling room and connection status changes in mock classes.
    • Added a new API version for enhanced message sending functionality.
  • Bug Fixes

    • Improved error handling for missing fields in incoming messages.
    • Implemented a retry mechanism for fetching previous messages to avoid empty results.
  • Documentation

    • Added comments to clarify changes and the rationale behind the new retry logic.
  • Tests

    • Updated test cases to reflect the renaming of properties from latestAction to action in the Message struct.

Upon our integration tests starting to fail, we noticed that Realtime
seems to have renamed the `latestAction` property to `action`. This
change has not yet been reflected in the spec, but has been in JS [1].

I’ve created an issue to get the spec updated [2] but since this test
failure is stopping us from merging anything else I am going to rename
this property until we get a more definitive answer.

Part of #169.

[1] ably/ably-chat-js#427
[2] ably/specification#254
Copy link

coderabbitai bot commented Dec 4, 2024

Warning

Rate limit exceeded

@lawrence-forooghian has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 14 minutes and 1 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 74cbbf4 and 80fe95b.

Walkthrough

The pull request introduces changes primarily focused on renaming the latestAction property to action across various classes and methods related to message handling in the chat application. This modification is reflected in the Message struct, mock classes, and API methods, ensuring consistency in how messages are constructed and processed. Additionally, new functionalities, such as retry mechanisms for message fetching and improved error handling, are introduced, enhancing the overall robustness of the message handling system.

Changes

File Path Change Summary
Example/AblyChatExample/Mocks/Misc.swift Renamed latestAction to action in MockMessagesPaginatedResult class.
Example/AblyChatExample/Mocks/MockClients.swift Renamed latestAction to action in MockMessages class; added onStatusChange method in MockRoom and MockConnection.
Sources/AblyChat/ChatAPI.swift Added apiVersionV2 constant; updated sendMessage method to rename latestAction to action.
Sources/AblyChat/DefaultMessages.swift Renamed latestAction to action; improved error handling in message subscription.
Sources/AblyChat/Message.swift Renamed latestAction to action in Message struct and updated initializer and coding keys accordingly.
Tests/AblyChatTests/ChatAPITests.swift Updated test cases to reflect the renaming of latestAction to action.
Tests/AblyChatTests/IntegrationTests.swift Introduced retry mechanism in basicIntegrationTest for fetching messages.
Tests/AblyChatTests/MessageSubscriptionTests.swift Updated Message instantiation to use action instead of latestAction; added MockPaginatedResult class.
Tests/AblyChatTests/MessageTests.swift Renamed latestAction to action in multiple message instances.
Tests/AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift Renamed key "latestAction" to "action" in mock response items.

Possibly related PRs

  • [ECO-5001] Some spec alignment #156: The changes in this PR involve modifications to the MockClients.swift file, specifically renaming the latestAction property to action in the MockMessages class, which directly relates to the changes made in the main PR regarding the MockMessagesPaginatedResult class.

Suggested reviewers

  • umair-ably
  • maratal

🐇 "In the meadow where messages flow,
The action's name has changed, you know!
With retries and mocks, we play our part,
In the chat's sweet dance, a work of art!
So hop along, let’s test with glee,
For every message, there's joy, you see!" 🌼


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 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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

@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

🧹 Outside diff range and nitpick comments (5)
Sources/AblyChat/Message.swift (1)

12-12: LGTM! Property renaming is consistent.

The renaming from latestAction to action is applied consistently across the property declaration, initializer, and CodingKeys.

Consider adding a comment to document that this is a temporary change pending resolution of issue #254 in the Ably specification repository.

Also applies to: 20-22, 33-33

Tests/AblyChatTests/MessageSubscriptionTests.swift (1)

Line range hint 4-24: Consider improving the mock implementation.

The MockPaginatedResult class currently uses fatalError for all its methods. While this might be sufficient for the current tests, consider implementing meaningful behavior for commonly used methods to make the tests more robust.

Consider implementing at least the basic properties:

class MockPaginatedResult<T: Equatable>: PaginatedResult {
    private let mockItems: [T]
    private let mockHasNext: Bool
    
    init(items: [T] = [], hasNext: Bool = false) {
        self.mockItems = items
        self.mockHasNext = hasNext
    }
    
    var items: [T] { mockItems }
    var hasNext: Bool { mockHasNext }
    var isLast: Bool { !mockHasNext }
    
    // Keep fatalError for less commonly used methods
    var next: (any AblyChat.PaginatedResult<T>)? { fatalError("Not implemented") }
    var first: any AblyChat.PaginatedResult<T> { fatalError("Not implemented") }
    var current: any AblyChat.PaginatedResult<T> { fatalError("Not implemented") }
}
Sources/AblyChat/ChatAPI.swift (1)

Line range hint 4-5: Document the API version transition plan

The coexistence of v1 and v2 API versions should be documented to help users understand the migration path.

Consider adding:

  1. A comment explaining the differences between v1 and v2
  2. Deprecation notice for v1
  3. Migration guide in README.md
Tests/AblyChatTests/IntegrationTests.swift (2)

120-138: Well-documented temporary fix with clear tracking!

The documentation thoroughly explains the reason for the retry mechanism and includes a reference to the tracking issue #175 for future removal. This is a good practice for temporary workarounds.

Consider adding an estimated timeline or condition for removal in the comment, if known. For example: "Revert this once the materialised history system supports live history (tracked in #175)."


139-148: Add safeguards to the retry mechanism

While this is a reasonable temporary workaround, consider adding some safeguards to prevent potential issues:

  1. Add a maximum retry count to prevent infinite loops
  2. Consider making the sleep duration configurable
  3. Add a total timeout duration

Here's a suggested improvement:

+        let maxRetries = 10  // Adjust as needed
+        let sleepDuration: UInt64 = NSEC_PER_SEC  // 1 second
+        var retryCount = 0
         let rxMessagesBeforeSubscribing = try await {
             while true {
+                if retryCount >= maxRetries {
+                    throw NSError(domain: "IntegrationTests",
+                                code: 1,
+                                userInfo: [NSLocalizedDescriptionKey: "Failed to fetch messages after \(maxRetries) retries"])
+                }
                 let messages = try await rxMessageSubscription.getPreviousMessages(params: .init())
                 if !messages.items.isEmpty {
                     return messages
                 }
-                // Wait 1 second before retrying the history fetch
-                try await Task.sleep(nanoseconds: NSEC_PER_SEC)
+                retryCount += 1
+                try await Task.sleep(nanoseconds: sleepDuration)
             }
         }()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5676cac and e7e5e3b.

📒 Files selected for processing (10)
  • Example/AblyChatExample/Mocks/Misc.swift (1 hunks)
  • Example/AblyChatExample/Mocks/MockClients.swift (2 hunks)
  • Sources/AblyChat/ChatAPI.swift (1 hunks)
  • Sources/AblyChat/DefaultMessages.swift (1 hunks)
  • Sources/AblyChat/Message.swift (2 hunks)
  • Tests/AblyChatTests/ChatAPITests.swift (3 hunks)
  • Tests/AblyChatTests/IntegrationTests.swift (1 hunks)
  • Tests/AblyChatTests/MessageSubscriptionTests.swift (1 hunks)
  • Tests/AblyChatTests/MessageTests.swift (4 hunks)
  • Tests/AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift (2 hunks)
🔇 Additional comments (12)
Tests/AblyChatTests/MessageTests.swift (1)

7-7: LGTM! Test cases updated consistently.

All test message instantiations have been correctly updated to use the new action property name while maintaining the original test coverage and functionality.

Also applies to: 18-18, 29-29, 73-73

Tests/AblyChatTests/MessageSubscriptionTests.swift (1)

29-29: LGTM! Message creation updated correctly.

The message instantiation in the map function has been properly updated to use the new action property name.

Example/AblyChatExample/Mocks/Misc.swift (1)

15-15: LGTM: Property rename is consistent with PR objectives

The change from latestAction to action aligns with the temporary fix mentioned in issue #169.

Let's verify the consistency of this change across the codebase:

✅ Verification successful

Property rename from latestAction to action is consistent across the codebase

The verification shows that:

  • No instances of latestAction remain in the codebase
  • The action property is consistently used in Message struct initialization across all files
  • The Message struct definition and all its usages align with the new property name
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining instances of 'latestAction' to ensure complete migration
rg "latestAction" --type swift

Length of output: 30


Script:

#!/bin/bash
# Let's first check if there are any Swift files in the repository
fd "\.swift$"

# Then search for both 'latestAction' and 'action' in Swift files to understand the context better
echo "=== Searching for 'action' property usage ==="
rg "action:" --type swift -B 2 -A 2

# Also check for Message type/struct definition
echo "=== Searching for Message definition ==="
ast-grep --pattern 'struct Message {
  $$$
}'

Length of output: 22530

Tests/AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift (1)

102-102: LGTM: Mock response data updated consistently

The mock response data correctly reflects the property rename from latestAction to action while maintaining the expected action type string format "message.create".

Also applies to: 112-112

Tests/AblyChatTests/ChatAPITests.swift (2)

44-44: LGTM: Test expectations updated consistently

The test expectations have been properly updated to use action instead of latestAction, maintaining test coverage for both message sending and retrieval scenarios.

Also applies to: 94-94, 104-104


Line range hint 94-108: Verify timestamp handling consistency

There's an inconsistency in timestamp handling between the two test messages - one has a specific timestamp while the other has nil. Consider documenting why this difference exists or standardizing the approach.

Let's check if this timestamp pattern is consistent with the actual implementation:

Sources/AblyChat/ChatAPI.swift (2)

Line range hint 49-57: LGTM: Message construction looks correct

The property renaming from latestAction to action is consistent with the changes across the codebase.


Line range hint 71-75: LGTM: Robust error handling implementation

The error handling correctly implements requirement CHA-M3e by converting REST API errors to ARTErrorInfo.

Example/AblyChatExample/Mocks/MockClients.swift (2)

Line range hint 107-114: LGTM: Mock message construction aligns with API changes

The property renaming is consistent with the core API changes.


Line range hint 133-140: LGTM: Mock message sending implementation is correct

The property renaming is consistent and the mock implementation properly simulates message sending.

Sources/AblyChat/DefaultMessages.swift (2)

Line range hint 71-85: LGTM: Comprehensive error handling for message validation

The error handling is thorough with specific error messages for each missing field.


Line range hint 86-94: LGTM: Message construction is complete and consistent

The property renaming is consistent with the API changes and includes all required fields.

Copy link

@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

🧹 Outside diff range and nitpick comments (1)
Tests/AblyChatTests/IntegrationTests.swift (1)

119-148: Consider adding safety bounds to the retry mechanism.

The retry implementation is a good temporary solution for handling empty history results. However, to prevent potential infinite loops in edge cases, consider adding:

  • A maximum retry count
  • A total timeout duration
  • A configurable sleep duration

Example improvement:

 let rxMessagesBeforeSubscribing = try await {
+    let maxRetries = 10  // Maximum number of retry attempts
+    var attempts = 0
     while true {
+        attempts += 1
+        if attempts > maxRetries {
+            throw ChatError.custom(message: "Failed to fetch messages after \(maxRetries) attempts")
+        }
         let messages = try await rxMessageSubscription.getPreviousMessages(params: .init())
         if !messages.items.isEmpty {
             return messages
         }
         try await Task.sleep(nanoseconds: NSEC_PER_SEC)
     }
 }()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e7e5e3b and 74cbbf4.

📒 Files selected for processing (1)
  • Tests/AblyChatTests/IntegrationTests.swift (1 hunks)
🔇 Additional comments (1)
Tests/AblyChatTests/IntegrationTests.swift (1)

137-137: Verify issue tracking for this temporary fix.

The comment references issue #175 for tracking the removal of this workaround.

✅ Verification successful

Issue #175 is still open and tracking the temporary fix

The referenced issue #175 "Remove wait for historical messages in integration tests" exists and is currently open, properly tracking the temporary workaround. The issue is also linked to a Jira task (ECO-5169) for internal tracking.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence and status of the tracking issue

# Check if the issue exists and is still open
gh issue view 175 --json state,title,body

Length of output: 351

This is intermittently failing with

> ✘ Test basicIntegrationTest() recorded an issue at IntegrationTests.swift:162:9: Expectation failed: (rxOccupancyEventFromSubscription.presenceMembers → 0) == 1

This is presumably because the arbitrary 2 second wait isn’t enough. So,
instead, wait until the subscription gives us the presence count that
we’re expecting.

Resolves #149.
Noticed that this seemed to be half-done (the comment next to the
`Task.sleep` suggested there was meant to be an occupancy test to follow
the `leave`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants