Skip to content

test(push): add tests for direct publish via clientId and batch push publish #2031

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

owenpearson
Copy link
Member

@owenpearson owenpearson commented May 14, 2025

Summary by CodeRabbit

  • Tests
    • Added new tests for push notifications using device ID, client ID, and batch publishing in the browser.
    • Improved test setup for message channels and client initialization to enhance reliability and coverage.

Copy link

coderabbitai bot commented May 14, 2025

Walkthrough

The browser push test suite was expanded with three new tests covering direct push by device ID, direct push by client ID, and batch push notifications. The setup logic was refactored to reinitialize the message channel before each test, and a client ID was added to the REST client initialization. All changes are confined to test logic.

Changes

File(s) Change Summary
test/browser/push.test.js Added three new push notification tests (direct by device ID, direct by client ID, batch). Refactored message channel initialization to beforeEach. Added client ID to REST client initialization. No changes to exports or public APIs.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Runner
    participant Browser as Browser
    participant SW as Service Worker
    participant Ably as Ably REST API

    Test->>Browser: Initialize message channel (beforeEach)
    Test->>Ably: Initialize AblyRest client (with clientId)
    Test->>Browser: Activate push and register SW
    Browser->>SW: Register and activate SW
    Test->>Ably: Send push notification (direct or batch)
    Ably->>Browser: Deliver push notification
    Browser->>SW: Forward push message via message channel
    SW->>Test: Post received push payload
    Test->>Test: Assert payload correctness
Loading

Suggested reviewers

  • VeskeR

Poem

In the warren of code, new tests now appear,
Pushes by client, by device, batch draws near.
Message channels refreshed, bunnies hop with delight,
Payloads delivered, each test shining bright.
With every assertion, the garden grows strong—
🐇 Here’s to push tests, all day long!

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 sequence diagram to generate a sequence diagram of the changes in this 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.

@github-actions github-actions bot temporarily deployed to staging/pull/2031/bundle-report May 14, 2025 10:01 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/2031/typedoc May 14, 2025 10:01 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/2031/features May 14, 2025 10:01 Inactive
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

🧹 Nitpick comments (4)
test/browser/push.test.js (4)

158-159: Fix typo in console.log message.

There's a spelling error in the console.log message.

-            console.log('recieved event', event);
+            console.log('received event', event);

169-169: Fix typo in error message.

There's a spelling error in the error message.

-                reject(new Error('Batch publish recieved error: ' + res.errorMessage));
+                reject(new Error('Batch publish received error: ' + res.errorMessage));

176-184: Enhance batch payload verification.

The current verification only checks the title but doesn't verify the notification body. Additionally, lines 178 and 180 check for data properties that aren't defined in the batch payload.

        for (const receivedPushPayload of receivedPayloads) {
          if (receivedPushPayload.notification.title === 'SingleRecipient') {
-            expect(receivedPushPayload.data).to.deep.equal(batchPayload[0].payload.data);
+            expect(receivedPushPayload.notification.body).to.equal('batch_publish_1');
+            // Only check data if it was included in the payload
+            if (batchPayload[0].payload.data) {
+              expect(receivedPushPayload.data).to.deep.equal(batchPayload[0].payload.data);
+            }
          } else if (receivedPushPayload.notification.title === 'MultipleRecipients') {
-            expect(receivedPushPayload.data).to.deep.equal(batchPayload[1].payload.data);
+            expect(receivedPushPayload.notification.body).to.equal('batch_publish_2');
+            // Only check data if it was included in the payload
+            if (batchPayload[1].payload.data) {
+              expect(receivedPushPayload.data).to.deep.equal(batchPayload[1].payload.data);
+            }
          } else {
            expect.fail('Unexpected payload received');
          }
        }

160-160: Consider making the expected notification count dynamic.

The hardcoded value of 3 expected payloads is brittle if the batch configuration changes.

-            if (receivedPayloads.length === 3) {
+            // Calculate expected count: sum of individual recipients plus recipients in arrays
+            const expectedCount = batchPayload.reduce((count, entry) => {
+              return count + (Array.isArray(entry.recipient) ? entry.recipient.length : 1);
+            }, 0);
+            if (receivedPayloads.length === expectedCount) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b987c6b and ca6922f.

📒 Files selected for processing (1)
  • test/browser/push.test.js (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: test-browser (webkit)
  • GitHub Check: test-node (20.x)
  • GitHub Check: test-node (16.x)
  • GitHub Check: test-browser (chromium)
  • GitHub Check: test-browser (firefox)
  • GitHub Check: test-node (18.x)
  • GitHub Check: test-npm-package
🔇 Additional comments (5)
test/browser/push.test.js (5)

16-16: Good refactoring of the messageChannel initialization.

Moving the messageChannel initialization to the beforeEach hook ensures a fresh channel for each test, preventing potential state contamination between tests.

Also applies to: 46-47


34-34: LGTM: Added clientId to REST client initialization.

The addition of clientId is necessary for the new client ID-based push notification tests.


65-93: Well-structured new test for device ID push notifications.

This test effectively verifies direct publishing to a device ID, with proper async handling and comprehensive payload verification.


95-122: Well-structured new test for client ID push notifications.

This test correctly demonstrates targeting push notifications by client ID, complementing the device ID test case.


124-185: Good implementation of batch publish test.

The test effectively verifies the batch publishing functionality with different recipient configurations (single recipient and multiple recipients).

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

🧹 Nitpick comments (3)
test/browser/push.test.js (3)

66-93: LGTM: Well-structured test for device ID targeting

This test effectively verifies direct push publishing to a specific device ID. The test properly:

  1. Activates push notifications
  2. Creates a push recipient targeting the device ID
  3. Sets up a message channel to receive the notification
  4. Publishes and waits for the notification
  5. Verifies both notification content and data payload

For improved readability, consider adding a comment explaining the overall flow of the test and how the service worker communication works.


124-184: Consider refining the batch publish test implementation

While the test is functional, there are some opportunities for improvement:

  1. The test directly uses rest.request('POST', '/push/batch/publish', ...) instead of using an abstraction like rest.push.admin.publishBatch() that might be available (or could be created for consistency)

  2. The test expects exactly 3 payloads, but it might be better to make this expectation dynamic based on the batch payload structure (single recipient + 2 multiple recipients = 3)

  3. The payload verification logic differs from the direct publish tests - consider making it more consistent across all tests

  4. Add comments explaining the expected number of notifications and why (1 from single recipient + 2 from multiple recipients)


174-183: Enhance verification for batch publishing notifications

The verification logic could be strengthened by also checking the notification body and ensuring all expected payloads are received:

 expect(receivedPayloads.length).to.equal(3);
+
+// Create a map to track which expected notifications we've received
+const receivedNotifications = {
+  'batch_publish_1': 0,
+  'batch_publish_2': 0
+};
+
 for (const receivedPushPayload of receivedPayloads) {
   if (receivedPushPayload.notification.title === 'SingleRecipient') {
     expect(receivedPushPayload.data).to.deep.equal(batchPayload[0].payload.data);
+    expect(receivedPushPayload.notification.body).to.equal('batch_publish_1');
+    receivedNotifications['batch_publish_1']++;
   } else if (receivedPushPayload.notification.title === 'MultipleRecipients') {
     expect(receivedPushPayload.data).to.deep.equal(batchPayload[1].payload.data);
+    expect(receivedPushPayload.notification.body).to.equal('batch_publish_2');
+    receivedNotifications['batch_publish_2']++;
   } else {
     expect.fail('Unexpected payload received');
   }
 }
+
+// Verify we got 1 of the first type and 2 of the second type
+expect(receivedNotifications['batch_publish_1']).to.equal(1);
+expect(receivedNotifications['batch_publish_2']).to.equal(2);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between ca6922f and 56aa1d3.

📒 Files selected for processing (1)
  • test/browser/push.test.js (5 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
test/browser/push.test.js (1)
test/rest/push.test.js (20)
  • rest (396-396)
  • rest (456-456)
  • rest (476-476)
  • rest (496-496)
  • rest (517-517)
  • rest (555-555)
  • rest (575-575)
  • rest (595-595)
  • rest (634-634)
  • rest (678-678)
  • rest (717-717)
  • rest (734-734)
  • pushRecipient (421-426)
  • pushRecipient (524-526)
  • pushRecipient (602-604)
  • pushPayload (409-412)
  • pushPayload (528-531)
  • pushPayload (606-609)
  • pushPayload (643-646)
  • pushPayload (687-690)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: test-node (16.x)
  • GitHub Check: test-node (20.x)
  • GitHub Check: test-browser (firefox)
  • GitHub Check: test-browser (chromium)
  • GitHub Check: test-browser (webkit)
  • GitHub Check: test-node (18.x)
  • GitHub Check: test-npm-package
🔇 Additional comments (3)
test/browser/push.test.js (3)

16-16: LGTM: Good change to initialize messageChannel in beforeEach

Changing messageChannel from a constant to a variable and initializing it fresh before each test ensures proper isolation between test cases, preventing any potential state leakage.

Also applies to: 46-47


34-34: LGTM: Adding clientId to the REST client

The addition of the clientId parameter is necessary for the new client ID-based push notification tests.


95-122: LGTM: Well-structured test for client ID targeting

This test correctly verifies push publishing to a client ID, following the same pattern as the device ID test. The structure is clear and the verification is thorough.

@owenpearson owenpearson requested a review from VeskeR May 22, 2025 08:48
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.

1 participant