Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions features/notifications.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ Feature: Notifications
When an authenticated request is made to "/.ghost/activitypub/v1/notifications?limit=200"
Then the request is rejected with a 400

Scenario: Requests for unread notifications count
Scenario: New notifications are marked as unread
Given we are following "Alice"
And we are not following "Bob"
When we get a like notification from "Alice"
And we get a like notification from "Bob"
And we get a reply notification from "Alice"
And we get a reply notification from "Bob"
Then the unread notifications count is 4
Then we have unread notifications
Copy link
Member

Choose a reason for hiding this comment

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

🤔 My concern is here we are just masking a problem? It is valid that we do expect a specific amount of notifications, and if there are more or less, than something is not correct somewhere


Scenario: Reset unread notifications count
Scenario: Visiting the notifications page marks notifications as read
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should talk about "pages" here - I know this aligns better with the action the user is taking, but its not a pattern we follow in any other scenarios and is potentially confusing / misleading. What about just:

Retrieving notifications marks them all as read

Given we are following "Alice"
And we are not following "Bob"
And we get a like notification from "Alice"
And we get a like notification from "Bob"
And we get a reply notification from "Alice"
And we get a reply notification from "Bob"
And the unread notifications count is 4
When we reset unread notifications count
Then the unread notifications count is 0
And we have unread notifications
When we visit the notifications page
Then all notifications are marked as read
18 changes: 12 additions & 6 deletions features/step_definitions/notifications_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import { createActivity, createObject } from '../support/fixtures.js';
import {
waitForItemInNotifications,
waitForUnreadNotifications,
waitForZeroUnreadNotifications,
} from '../support/notifications.js';
import { fetchActivityPub } from '../support/request.js';

Then('the unread notifications count is {int}', async (count) => {
const found = await waitForUnreadNotifications(count);
assert(found);
});

When('we get a like notification from {string}', async function (actorName) {
if (!this.articleId) {
const article = await publishArticle();
Expand Down Expand Up @@ -76,11 +72,21 @@ When('we get a reply notification from {string}', async function (actorName) {
await waitForItemInNotifications(object.id);
});

When('we reset unread notifications count', async () => {
When('we visit the notifications page', async () => {
await fetchActivityPub(
'https://self.test/.ghost/activitypub/v1/notifications/unread/reset',
{
method: 'PUT',
},
);
});

Then('we have unread notifications', async () => {
const unreadNotifications = await waitForUnreadNotifications();
assert(unreadNotifications);
});

Then('all notifications are marked as read', async () => {
const zeroUnreadNotifications = await waitForZeroUnreadNotifications();
assert(zeroUnreadNotifications);
});
47 changes: 41 additions & 6 deletions features/support/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export async function waitForItemInNotifications(
}

export async function waitForUnreadNotifications(
unreadNotificationCount,
options = {
retryCount: 0,
delay: 0,
Expand All @@ -68,23 +67,59 @@ export async function waitForUnreadNotifications(

const responseJson = await response.clone().json();

const found = responseJson.count === unreadNotificationCount;
const unreadNotifications = responseJson.count > 0;

if (found) {
return found;
if (unreadNotifications) {
return true;
}

if (options.retryCount === MAX_RETRIES) {
throw new Error(
`Max retries reached (${MAX_RETRIES}) when waiting for unread notifications. No unread notifications found.`,
);
}

if (options.delay > 0) {
await new Promise((resolve) => setTimeout(resolve, options.delay));
}

return await waitForUnreadNotifications({
retryCount: options.retryCount + 1,
delay: options.delay + 500,
});
}

export async function waitForZeroUnreadNotifications(
Copy link
Member

Choose a reason for hiding this comment

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

I'm not following how this is any different to waitForUnreadNotifications(0)?

options = {
retryCount: 0,
delay: 0,
},
) {
const MAX_RETRIES = 5;

const response = await fetchActivityPub(
'https://self.test/.ghost/activitypub/v1/notifications/unread/count',
);

const responseJson = await response.clone().json();

const zeroUnreadNotifications = responseJson.count === 0;

if (zeroUnreadNotifications) {
return true;
}

if (options.retryCount === MAX_RETRIES) {
throw new Error(
`Max retries reached (${MAX_RETRIES}) when waiting for notifications count ${unreadNotificationCount}. Notification count found ${responseJson.count}`,
`Max retries reached (${MAX_RETRIES}) when waiting for zero unread notifications. Unread notifications found: ${responseJson.count}.`,
);
}

if (options.delay > 0) {
await new Promise((resolve) => setTimeout(resolve, options.delay));
}

return await waitForUnreadNotifications(unreadNotificationCount, {
return await waitForZeroUnreadNotifications({
retryCount: options.retryCount + 1,
delay: options.delay + 500,
});
Expand Down