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

feat(refactor): improve email search commands #40

Merged
merged 5 commits into from
Oct 22, 2024

Conversation

pushpak1300
Copy link
Owner

@pushpak1300 pushpak1300 commented Oct 20, 2024

  • Refactored the mailpitHasEmailsBySubject and mailpitHasEmailsByTo commands to include an optional timeout and interval for automatic retries.
  • Updated the mailpitNotHasEmailsBySubject and mailpitNotHasEmailsByTo commands to also support the optional timeout and interval parameters.
  • Added a new private method waitForCondition to handle the retry logic for the email search commands.

Closes #39

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced email checking commands with customizable timeout and interval options for better control over email presence checks.
    • Introduced new commands for verifying email presence and absence based on search queries.
    • Added a new test case to verify email search functionality.
  • Bug Fixes

    • Improved error handling for email checking methods to provide clearer feedback when conditions are not met.
  • Documentation

    • Updated the README and Cypress documentation for clarity and to reflect new command functionalities.
    • Enhanced formatting for improved readability in the setup process and command descriptions.

- Refactored the `mailpitHasEmailsBySubject` and `mailpitHasEmailsByTo` commands to include an optional timeout and interval for automatic retries.
- Updated the `mailpitNotHasEmailsBySubject` and `mailpitNotHasEmailsByTo` commands to also support the optional timeout and interval parameters.
- Added a new private method `waitForCondition` to handle the retry logic for the email search commands.

Closes #39
Copy link

coderabbitai bot commented Oct 20, 2024

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

The pull request introduces enhancements to the cypress-mailpit package documentation and functionality. Key updates include the addition of optional timeout and interval parameters to email checking commands, enabling automatic retries. New methods and improved error handling have been implemented in the MailpitCommands class. Documentation in README.md and cypress.d.ts has been revised for clarity and accuracy, reflecting these changes and ensuring that users understand how to utilize the updated features effectively.

Changes

File Change Summary
README.md Updated command signatures for mailpitHasEmailsBySubject, mailpitHasEmailsByTo, mailpitNotHasEmailsBySubject, and mailpitNotHasEmailsByTo to include timeout and interval. Introduced mailpitHasEmailsBySearch and mailpitNotHasEmailsBySearch. Enhanced overall documentation clarity.
cypress.d.ts Revised method signatures and added optional options parameter for email handling methods. Updated documentation for clarity, marked mailpitGetMailSpamAssainSummary as deprecated.
src/MailpitCommands.ts Added waitForCondition method for asynchronous waiting. Updated email checking methods to use this new method with customizable timeout and interval. Enhanced error handling.
cypress/e2e/mailpit.cy.ts Added a new test case to verify the functionality of mailpitHasEmailsBySearch and mailpitNotHasEmailsBySearch.

Assessment against linked issues

Objective Addressed Explanation
Automatic waiting and retries for queries (Issue #39)

Possibly related PRs

Suggested labels

autorelease: tagged

Poem

In the land of code where emails roam,
We've added retries to bring them home.
With timeouts set and intervals clear,
Checking for messages brings us cheer!
So hop along, dear coder friend,
With cypress-mailpit, your troubles mend! 🐇📧


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.

@pushpak1300 pushpak1300 self-assigned this Oct 20, 2024
@pushpak1300 pushpak1300 added the enhancement New feature or request label Oct 20, 2024
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: 4

🧹 Outside diff range and nitpick comments (7)
cypress.d.ts (5)

31-43: LGTM! Consider adding example usage in the documentation.

The changes to mailpitHasEmailsBySubject align well with the PR objectives. The new options parameter for configurable timeout and interval is a great addition. The updated description and return type specification improve clarity and type safety.

Consider adding a brief example of how to use the new options parameter in the method documentation. This could help users quickly understand how to utilize the new functionality.

/**
 * @example
 * cy.mailpitHasEmailsBySubject('Welcome', 0, 10, { timeout: 10000, interval: 1000 });
 */

46-58: LGTM! Consider adding example usage in the documentation.

The changes to mailpitNotHasEmailsBySubject are consistent with those made to mailpitHasEmailsBySubject and align well with the PR objectives. The new options parameter and updated description improve the method's functionality and clarity.

As suggested for mailpitHasEmailsBySubject, consider adding a brief example of how to use the new options parameter in the method documentation:

/**
 * @example
 * cy.mailpitNotHasEmailsBySubject('Spam', 0, 10, { timeout: 5000, interval: 500 });
 */

69-81: LGTM! Consider adding example usage in the documentation.

The changes to mailpitHasEmailsByTo are consistent with the previous methods and align well with the PR objectives. The new options parameter and updated description enhance the method's functionality and clarity.

As suggested for the previous methods, consider adding a brief example of how to use the new options parameter in the method documentation:

/**
 * @example
 * cy.mailpitHasEmailsByTo('[email protected]', 0, 10, { timeout: 8000, interval: 800 });
 */

84-96: LGTM! Consider adding example usage in the documentation.

The changes to mailpitNotHasEmailsByTo are consistent with the previous methods and align well with the PR objectives. The new options parameter and updated description improve the method's functionality and clarity.

As suggested for the previous methods, consider adding a brief example of how to use the new options parameter in the method documentation:

/**
 * @example
 * cy.mailpitNotHasEmailsByTo('[email protected]', 0, 10, { timeout: 6000, interval: 600 });
 */

129-132: LGTM! Consider correcting the deprecated method name.

The deprecation notice is clear and helpful. However, there's a typo in the deprecated method name.

Consider correcting the spelling of the deprecated method name:

- mailpitGetMailSpamAssainSummary(message?: Message): Chainable<SpamAssassin>;
+ mailpitGetMailSpamAssassinSummary(message?: Message): Chainable<SpamAssassin>;

This will ensure consistency with the correctly spelled replacement method mailpitGetMailSpamAssassinSummary.

README.md (2)

171-177: LGTM! Consider enhancing the example usage.

The addition of timeout and interval parameters aligns well with the PR objectives. This change provides users with the flexibility to customize the retry behavior.

To better illustrate the new functionality, consider updating the example to show how to use the new parameters:

cy.mailpitHasEmailsBySubject('My Test', 0, 50, { timeout: 15000, interval: 2000 }).should('be.true');

Line range hint 1-203: Overall, good improvements with minor adjustments needed.

The changes to the README.md file effectively document the new functionality for automatic waiting and retries in email checking commands. This aligns well with the PR objectives and the linked issue #39.

Please address the following points:

  1. Correct the descriptions for mailpitNotHasEmailsBySubject and mailpitNotHasEmailsByTo.
  2. Update the examples for mailpitHasEmailsBySubject, mailpitNotHasEmailsBySubject, and mailpitNotHasEmailsByTo to demonstrate the usage of the new timeout and interval parameters.

These changes will ensure that the documentation accurately reflects the new functionality and provides clear guidance for users.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 652386c and 74bda0a.

📒 Files selected for processing (3)
  • README.md (1 hunks)
  • cypress.d.ts (4 hunks)
  • src/MailpitCommands.ts (2 hunks)
🧰 Additional context used
🔇 Additional comments (7)
cypress.d.ts (2)

169-169: LGTM! Improved parameter flexibility.

The update to the messages parameter type and description for mailpitSetStatusAsRead is a good improvement. It now allows for both Message and MessageSummary objects, which increases flexibility and aligns with modern TypeScript practices.


175-175: LGTM! Consistent improvement in parameter flexibility.

The update to the messages parameter type and description for mailpitSetStatusAsUnRead is consistent with the changes made to mailpitSetStatusAsRead. This improvement in flexibility, allowing both Message and MessageSummary objects, is a good practice and maintains consistency across related methods.

README.md (1)

180-186: LGTM! Clear documentation and good example usage.

The updates to mailpitHasEmailsByTo are consistent with the changes made to mailpitHasEmailsBySubject. The example usage effectively demonstrates how to use the new timeout and interval parameters.

src/MailpitCommands.ts (4)

139-149: Enhancement of mailpitHasEmailsBySubject with retry logic.

The addition of the options parameter for timeout and interval, along with the use of waitForCondition, enhances the reliability of this method.


152-162: Update to mailpitNotHasEmailsBySubject using waitForCondition.

Including the options parameter and implementing retry logic improves consistency and usability.


169-179: Improved functionality in mailpitHasEmailsByTo with added retry capabilities.

This change aligns the method with the new retry logic, providing better handling for asynchronous email receipt.


182-192: Consistent updates in mailpitNotHasEmailsByTo method.

The method now appropriately supports timeout and interval options, improving user control over the retry behavior.

cypress.d.ts Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/MailpitCommands.ts Show resolved Hide resolved
@TobiasHH
Copy link
Contributor

It would be a nice improvment to have a check by Search.

		/**
		 * Check if mails have emails using a search query.
		 * Automatically retries until the condition is met or timeout is reached.
		 * @param query
		 * @param start
		 * @param limit
		 * @param options Optional. Object with `timeout` and `interval` properties.
		 */
		mailpitHasEmailsBySearch(
			query: string,
			start?: number,
			limit?: number,
			options?: { timeout?: number; interval?: number },
		): Chainable;

		/**
		 * Check if mails do not have emails using a search query.
		 * Automatically retries until the condition is met or timeout is reached.
		 * @param query
		 * @param start
		 * @param limit
		 * @param options Optional. Object with `timeout` and `interval` properties.
		 */
		mailpitNotHasEmailsBySearch(
			query: string,
			start?: number,
			limit?: number,
			options?: { timeout?: number; interval?: number },
		): Chainable;

@TobiasHH
Copy link
Contributor

it("can assert mailpit has emails by search or not", () => {
	cy.mailpitSendMail({
		subject: "My Test",
	});
	cy.mailpitHasEmailsBySearch('subject:"My Test"');
	cy.mailpitNotHasEmailsBySearch('subject:"My Test 2"');
});

@TobiasHH
Copy link
Contributor

TobiasHH commented Oct 21, 2024

static get parentCypressCommands(): string[] {
	return [
		"mailpitDeleteAllEmails",
		"mailpitGetAllMails",
		"mailpitGetMail",
		"mailpitSendMail",
		"mailpitSearchEmails",
		"mailpitHasEmailsBySearch",
		"mailpitNotHasEmailsBySearch",
		"mailpitGetEmailsBySubject",
		"mailpitHasEmailsBySubject",
		"mailpitNotHasEmailsBySubject",
		"mailpitGetEmailsByTo",
		"mailpitHasEmailsByTo",
		"mailpitNotHasEmailsByTo",
		"mailpitSetAllEmailStatusAsRead",
		"mailpitSetAllEmailStatusAsUnRead"
	];
}

mailpitHasEmailsBySearch(
	query: string,
	start = 0,
	limit = 50,
	options: { timeout?: number; interval?: number } = {},
): Cypress.Chainable<MessagesSummary> {
	return this.waitForCondition(
		() => this.mailpitSearchEmails(query, start, limit),
		(result) => result.messages_count > 0,
		options,
	);
}

mailpitNotHasEmailsBySearch(
	query: string,
	start = 0,
	limit = 50,
	options: { timeout?: number; interval?: number } = {},
): Cypress.Chainable<MessagesSummary> {
	return this.waitForCondition(
		() => this.mailpitSearchEmails(query, start, limit),
		(result) => result.messages_count === 0,
		options,
	);
}

@pushpak1300
Copy link
Owner Author

@TobiasHH I have added two extra commands please check.

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: 8

🧹 Outside diff range and nitpick comments (11)
cypress/e2e/mailpit.cy.ts (1)

169-177: LGTM! Consider enhancing test coverage.

The new test case effectively demonstrates the search functionality using both mailpitHasEmailsBySearch and mailpitNotHasEmailsBySearch commands, aligning well with the PR objectives.

To further improve the test coverage:

  1. Consider adding assertions to verify the email content after the search.
  2. Add a test case to demonstrate the usage of optional timeout and interval parameters for these new commands.

Would you like assistance in implementing these suggestions?

README.md (2)

171-178: Approve addition with minor suggestion.

The new mailpitHasEmailsBySearch command is a valuable addition that aligns with the PR objectives. The description and functionality are clear and accurate.

Consider enhancing the example to demonstrate the usage of optional parameters:

cy.mailpitHasEmailsBySearch('subject:My Test', 0, 50, { timeout: 15000, interval: 2000 }).should('be.true');

190-196: Approve update with minor suggestion.

The update to mailpitHasEmailsBySubject command aligns with the PR objectives. The description and functionality are clear and accurate.

Consider enhancing the example to demonstrate the usage of optional parameters:

cy.mailpitHasEmailsBySubject('My Test', 0, 50, { timeout: 15000, interval: 2000 }).should('be.true');
src/MailpitCommands.ts (3)

141-151: LGTM: New mailpitHasEmailsBySearch method implemented correctly

The new method is well-implemented, using the waitForCondition to provide polling behavior. The condition check for messages_count > 0 is correct for a "has" method.

Consider adding a JSDoc comment to describe the method's purpose and parameters, especially the options object, to improve code documentation.


167-178: LGTM: mailpitHasEmailsBySubject updated with polling behavior

The method has been successfully updated to use the new waitForCondition function, implementing the desired polling behavior. The condition check result.messages_count > 0 is correct for a "has" method.

Consider updating the JSDoc comment (if it exists) to reflect the new options parameter and the polling behavior.


180-220: LGTM: Consistent updates to email checking methods

The methods mailpitNotHasEmailsBySubject, mailpitHasEmailsByTo, and mailpitNotHasEmailsByTo have been consistently updated to include the options parameter and use the waitForCondition function. The condition checks are correct for each method:

  • mailpitNotHasEmailsBySubject: result.messages_count === 0
  • mailpitHasEmailsByTo: result.messages_count > 0
  • mailpitNotHasEmailsByTo: result.messages_count === 0

These changes successfully implement the desired polling behavior across all email checking methods.

Consider updating the JSDoc comments (if they exist) for these methods to reflect the new options parameter and the polling behavior.

cypress.d.ts (5)

31-43: Ensure Method Description Reflects Functionality Accurately

The method mailpitHasEmailsBySearch is designed to check if Mailpit has any emails matching the search query. To improve clarity, consider rephrasing the description in line 31.

Suggested change:

- * Check if mailpit has any email with the search query
+ * Check if Mailpit has any emails matching the search query.

60-73: Improve Consistency in Method Documentation

For mailpitHasEmailsBySubject, the description could be clearer and consistent with other method descriptions. Consider updating it to specify that it checks for emails with a specific subject.

Suggested change:

- * Check if mails have emails using the subject.
+ * Check if Mailpit has any emails with the specified subject.

99-111: Enhance Clarity in mailpitHasEmailsByTo Description

The method mailpitHasEmailsByTo checks for emails sent to a specific recipient. To improve understanding, refine the description accordingly.

Suggested change:

- * Check if mails have emails sent to a specific email address.
+ * Check if Mailpit has any emails sent to the specified email address.

153-153: Ensure Consistent Capitalization in Method Descriptions

In the description for mailpitGetMailSpamAssassinSummary, verify that "SpamAssassin" is consistently capitalized according to official naming.


199-199: Clarify Parameter Description for mailpitSetStatusAsRead

The parameter description mentions "Array of Message or MessageSummary objects," but the method accepts a single object or an array. Updating the description adds clarity.

Suggested change:

- * @param messages Array of Message or MessageSummary objects to mark as read
+ * @param messages Optional. A Message or MessageSummary object, or an array of them, to mark as read.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 74bda0a and ab36540.

📒 Files selected for processing (4)
  • README.md (1 hunks)
  • cypress.d.ts (4 hunks)
  • cypress/e2e/mailpit.cy.ts (1 hunks)
  • src/MailpitCommands.ts (3 hunks)
🧰 Additional context used
🔇 Additional comments (5)
README.md (2)

199-205: Approve update.

The update to mailpitHasEmailsByTo command aligns with the PR objectives. The description is clear and accurate, and the example usage effectively demonstrates the use of optional parameters.


171-219: Overall improvements to email search commands are valuable.

The additions and updates to the email search commands in the README significantly enhance the functionality of the cypress-mailpit package. These changes align well with the PR objectives and provide users with more flexible and robust email querying capabilities.

Key improvements:

  1. Addition of mailpitHasEmailsBySearch and mailpitNotHasEmailsBySearch commands.
  2. Updates to existing commands to include optional timeout and interval parameters.
  3. Consistent structure and descriptions for all commands.

While the overall changes are positive, please address the minor issues in descriptions and examples as noted in the previous comments to ensure complete accuracy and clarity for users.

src/MailpitCommands.ts (2)

22-23: LGTM: New commands added to parentCypressCommands

The addition of "mailpitHasEmailsBySearch" and "mailpitNotHasEmailsBySearch" to the parentCypressCommands array is consistent with the new methods implemented in the class. The naming convention follows the existing pattern, maintaining consistency.


Line range hint 1-386: Overall assessment: Good implementation with minor improvements needed

The changes to MailpitCommands.ts successfully implement new email search commands and update existing ones with polling behavior. The additions are consistent and well-structured. However, there are a few areas for improvement:

  1. Add JSDoc comments to new and modified methods for better documentation.
  2. Fix the condition in mailpitNotHasEmailsBySearch method.
  3. Refactor the waitForCondition method to avoid potential issues with Cypress command queueing.

After addressing these points, the implementation will be robust and maintainable.

cypress.d.ts (1)

135-135: ⚠️ Potential issue

Fix Typo in Method Name

The method name mailpitGetMailHTMlBody contains a typo in "HTMl". It should be capitalized as "HTML" to reflect proper naming conventions.

Apply this diff to correct the method name:

- mailpitGetMailHTMlBody(message?: Message): Chainable<string>;
+ mailpitGetMailHTMLBody(message?: Message): Chainable<string>;

Likely invalid or redundant comment.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/MailpitCommands.ts Show resolved Hide resolved
src/MailpitCommands.ts Outdated Show resolved Hide resolved
cypress.d.ts Show resolved Hide resolved
cypress.d.ts Show resolved Hide resolved
cypress.d.ts Show resolved Hide resolved
cypress.d.ts Show resolved Hide resolved
@TobiasHH
Copy link
Contributor

I will have a look at the new changes today evening (german time zone).

@TobiasHH
Copy link
Contributor

I have also a question for another method. As I was running over the mailpit API documentation I found that a delete over search is also possible. https://mailpit.axllent.org/docs/api-v1/view.html#delete-/api/v1/search

It would be nice to have this functionality also in this framework. "mailpitDeleteBySearch"

In my testing I have a shared mailpit instance where I cannot delete all messages before a testrun. Deleting mails by search will enable me to design my tests much more nicely. With this new function I can delete mails by using subject, to, from, etc. .

Should I open another feature request for this or could this be implemented with this branch?

@TobiasHH
Copy link
Contributor

Running over the API I also saw a paramter in the search command.

Timezone identifier used specifically for before: & after: searches (eg: "Pacific/Auckland").

tz: string
Timezone identifier used specifically for before: & after: searches (eg: "Pacific/Auckland").

Is this parameter relevant for the framework?

@pushpak1300
Copy link
Owner Author

@coderabbitai pause

Copy link

coderabbitai bot commented Oct 22, 2024

✅ Actions performed

Reviews paused.

@pushpak1300
Copy link
Owner Author

Running over the API I also saw a paramter in the search command.

Timezone identifier used specifically for before: & after: searches (eg: "Pacific/Auckland").

tz: string Timezone identifier used specifically for before: & after: searches (eg: "Pacific/Auckland").

Is this parameter relevant for the framework?

I guess I can add timezone change in the same MR but I will add mailpitSearchByDelte command in separate MR.

@pushpak1300 pushpak1300 merged commit 947af5e into main Oct 22, 2024
2 checks passed
pushpak1300 added a commit that referenced this pull request Oct 22, 2024
* feat(refactor): add retry ability on email assertion commands

- Refactored the `mailpitHasEmailsBySubject` and `mailpitHasEmailsByTo` commands to include an optional timeout and interval for automatic retries.
- Updated the `mailpitNotHasEmailsBySubject` and `mailpitNotHasEmailsByTo` commands to also support the optional timeout and interval parameters.
-Added the `mailpitHasEmailsBySearch` and `mailpitNotHasEmailsBySearch` commands to also support the optional timeout and interval parameters.
- Added a new private method `waitForCondition` to handle the retry logic for the email search commands.

Closes #39
@pushpak1300 pushpak1300 deleted the feat/add_timeout_and_retry_to_selected_commands branch October 22, 2024 20:31
@coderabbitai coderabbitai bot mentioned this pull request Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: automatic waiting and retries for queries
2 participants