@stream-io/feeds-client 0.2.18 → 0.2.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/cjs/index.js +94 -25
  3. package/dist/cjs/index.js.map +1 -1
  4. package/dist/cjs/react-bindings.js +26 -55
  5. package/dist/cjs/react-bindings.js.map +1 -1
  6. package/dist/es/index.mjs +86 -17
  7. package/dist/es/index.mjs.map +1 -1
  8. package/dist/es/react-bindings.mjs +19 -48
  9. package/dist/es/react-bindings.mjs.map +1 -1
  10. package/dist/{index-Zde8UE5f.mjs → feeds-client-BObWT4vl.mjs} +191 -92
  11. package/dist/feeds-client-BObWT4vl.mjs.map +1 -0
  12. package/dist/{index--koeDtxd.js → feeds-client-BlR_3zy2.js} +178 -79
  13. package/dist/feeds-client-BlR_3zy2.js.map +1 -0
  14. package/dist/tsconfig.tsbuildinfo +1 -1
  15. package/dist/types/bindings/react/hooks/feed-state-hooks/useOwnCapabilities.d.ts +2 -32
  16. package/dist/types/bindings/react/hooks/feed-state-hooks/useOwnCapabilities.d.ts.map +1 -1
  17. package/dist/types/common/real-time/event-models.d.ts +7 -2
  18. package/dist/types/common/real-time/event-models.d.ts.map +1 -1
  19. package/dist/types/common/types.d.ts +1 -0
  20. package/dist/types/common/types.d.ts.map +1 -1
  21. package/dist/types/feed/event-handlers/activity/handle-activity-added.d.ts.map +1 -1
  22. package/dist/types/feed/event-handlers/activity/handle-activity-updated.d.ts.map +1 -1
  23. package/dist/types/feed/feed.d.ts +1 -1
  24. package/dist/types/feed/feed.d.ts.map +1 -1
  25. package/dist/types/feeds-client/feeds-client.d.ts +9 -1
  26. package/dist/types/feeds-client/feeds-client.d.ts.map +1 -1
  27. package/dist/types/utils/throttling/index.d.ts +3 -0
  28. package/dist/types/utils/throttling/index.d.ts.map +1 -0
  29. package/dist/types/utils/throttling/throttle.d.ts +34 -0
  30. package/dist/types/utils/throttling/throttle.d.ts.map +1 -0
  31. package/dist/types/utils/throttling/throttled-get-batched-own-capabilities.d.ts +14 -0
  32. package/dist/types/utils/throttling/throttled-get-batched-own-capabilities.d.ts.map +1 -0
  33. package/package.json +7 -3
  34. package/react-bindings.d.ts +11 -0
  35. package/react-bindings.js +7 -0
  36. package/react-bindings.mjs +11 -0
  37. package/src/bindings/react/hooks/feed-state-hooks/useOwnCapabilities.ts +21 -73
  38. package/src/common/real-time/event-models.ts +8 -2
  39. package/src/common/types.ts +1 -0
  40. package/src/feed/event-handlers/activity/handle-activity-added.ts +9 -1
  41. package/src/feed/event-handlers/activity/handle-activity-updated.ts +4 -0
  42. package/src/feed/feed.ts +18 -3
  43. package/src/feeds-client/feeds-client.ts +106 -3
  44. package/src/utils/throttling/index.ts +2 -0
  45. package/src/utils/throttling/throttle.ts +123 -0
  46. package/src/utils/throttling/throttled-get-batched-own-capabilities.ts +44 -0
  47. package/dist/index--koeDtxd.js.map +0 -1
  48. package/dist/index-Zde8UE5f.mjs.map +0 -1
  49. package/src/feed/event-handlers/activity/activity-marked-utils.test.ts +0 -208
  50. package/src/feed/event-handlers/activity/activity-reaction-utils.test.ts +0 -371
  51. package/src/feed/event-handlers/activity/handle-activity-added.test.ts +0 -97
  52. package/src/feed/event-handlers/activity/handle-activity-deleted.test.ts +0 -117
  53. package/src/feed/event-handlers/activity/handle-activity-pinned.test.ts +0 -60
  54. package/src/feed/event-handlers/activity/handle-activity-reaction-added.test.ts +0 -257
  55. package/src/feed/event-handlers/activity/handle-activity-reaction-deleted.test.ts +0 -317
  56. package/src/feed/event-handlers/activity/handle-activity-reaction-updated.test.ts +0 -282
  57. package/src/feed/event-handlers/activity/handle-activity-unpinned.test.ts +0 -95
  58. package/src/feed/event-handlers/activity/handle-activity-updated.test.ts +0 -245
  59. package/src/feed/event-handlers/add-aggregated-activities-to-state.test.ts +0 -510
  60. package/src/feed/event-handlers/bookmark/bookmark-utils.test.ts +0 -521
  61. package/src/feed/event-handlers/bookmark/handle-bookmark-added.test.ts +0 -178
  62. package/src/feed/event-handlers/bookmark/handle-bookmark-deleted.test.ts +0 -188
  63. package/src/feed/event-handlers/bookmark/handle-bookmark-updated.test.ts +0 -196
  64. package/src/feed/event-handlers/comment/handle-comment-added.test.ts +0 -271
  65. package/src/feed/event-handlers/comment/handle-comment-deleted.test.ts +0 -255
  66. package/src/feed/event-handlers/comment/handle-comment-reaction-added.test.ts +0 -329
  67. package/src/feed/event-handlers/comment/handle-comment-reaction-deleted.test.ts +0 -343
  68. package/src/feed/event-handlers/comment/handle-comment-reaction-updated.test.ts +0 -350
  69. package/src/feed/event-handlers/comment/handle-comment-updated.test.ts +0 -267
  70. package/src/feed/event-handlers/comment/utils/update-comment-count.test.ts +0 -322
  71. package/src/feed/event-handlers/feed-member/handle-feed-member-added.test.ts +0 -75
  72. package/src/feed/event-handlers/feed-member/handle-feed-member-removed.test.ts +0 -82
  73. package/src/feed/event-handlers/feed-member/handle-feed-member-updated.test.ts +0 -84
  74. package/src/feed/event-handlers/follow/follow-state-update-queue.test.ts +0 -219
  75. package/src/feed/event-handlers/follow/handle-follow-created.test.ts +0 -250
  76. package/src/feed/event-handlers/follow/handle-follow-deleted.test.ts +0 -268
  77. package/src/feed/event-handlers/follow/handle-follow-updated.test.ts +0 -131
  78. package/src/feed/event-handlers/notification-feed/handle-notification-feed-updated.test.ts +0 -182
  79. package/src/feed/event-handlers/story-feeds/handle-story-feeds-updated.test.ts +0 -45
  80. package/src/feed/feed.test.ts +0 -90
  81. package/src/feeds-client/event-handlers/user/handle-user-updated.test.ts +0 -53
  82. package/src/utils/event-triggered-by-connected-user.test.ts +0 -73
  83. package/src/utils/state-update-queue.test.ts +0 -129
  84. package/src/utils/unique-array-merge.test.ts +0 -179
@@ -1,208 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import type {
3
- ActivityMarkEvent,
4
- NotificationStatusResponse,
5
- } from '../../../gen/models';
6
- import { updateNotificationStatusFromActivityMarked } from './handle-activity-marked';
7
-
8
- const createMockActivityMarkEvent = (
9
- overrides: Partial<ActivityMarkEvent> = {},
10
- ): ActivityMarkEvent => ({
11
- created_at: new Date(),
12
- fid: 'user:notification',
13
- custom: {},
14
- type: 'feeds.activity.marked',
15
- ...overrides,
16
- });
17
-
18
- const createMockNotificationStatus = (
19
- overrides: Partial<NotificationStatusResponse> = {},
20
- ): NotificationStatusResponse => ({
21
- unread: 0,
22
- unseen: 0,
23
- ...overrides,
24
- });
25
-
26
- const createMockAggregatedActivity = (group: string) => ({ group });
27
-
28
- describe('activity-marked-utils', () => {
29
- describe('updateNotificationStatusFromActivityMarked', () => {
30
- it('should return unchanged if notification_status is undefined', () => {
31
- const event = createMockActivityMarkEvent({ mark_all_read: true });
32
- const currentStatus = undefined;
33
-
34
- const result = updateNotificationStatusFromActivityMarked(
35
- event,
36
- currentStatus,
37
- );
38
-
39
- expect(result.changed).toBe(false);
40
- });
41
-
42
- it('should handle mark_all_read by adding all aggregated activity groups', () => {
43
- const event = createMockActivityMarkEvent({ mark_all_read: true });
44
- const currentStatus = createMockNotificationStatus({
45
- read_activities: ['existing1'],
46
- });
47
- const aggregatedActivities = [
48
- createMockAggregatedActivity('group1'),
49
- createMockAggregatedActivity('group2'),
50
- createMockAggregatedActivity('group3'),
51
- ];
52
-
53
- const result = updateNotificationStatusFromActivityMarked(
54
- event,
55
- currentStatus,
56
- aggregatedActivities,
57
- );
58
-
59
- expect(result.changed).toBe(true);
60
- expect(result.data?.notification_status.read_activities).toEqual([
61
- 'existing1',
62
- 'group1',
63
- 'group2',
64
- 'group3',
65
- ]);
66
- });
67
-
68
- it('should handle mark_read by adding specific activity IDs', () => {
69
- const event = createMockActivityMarkEvent({
70
- mark_read: ['activity1', 'activity2'],
71
- });
72
- const currentStatus = createMockNotificationStatus({
73
- read_activities: ['existing1'],
74
- });
75
-
76
- const result = updateNotificationStatusFromActivityMarked(
77
- event,
78
- currentStatus,
79
- );
80
-
81
- expect(result.changed).toBe(true);
82
- expect(result.data?.notification_status.read_activities).toEqual([
83
- 'existing1',
84
- 'activity1',
85
- 'activity2',
86
- ]);
87
- });
88
-
89
- it('should handle mark_all_seen by setting last_seen_at to current date', () => {
90
- const event = createMockActivityMarkEvent({ mark_all_seen: true });
91
- const currentStatus = createMockNotificationStatus({
92
- last_seen_at: new Date('2023-01-01'),
93
- });
94
-
95
- const result = updateNotificationStatusFromActivityMarked(
96
- event,
97
- currentStatus,
98
- );
99
-
100
- expect(result.changed).toBe(true);
101
- expect(result.data?.notification_status.last_seen_at).toBeInstanceOf(
102
- Date,
103
- );
104
- expect(
105
- result.data?.notification_status.last_seen_at!.getTime(),
106
- ).toBeGreaterThan(new Date('2023-01-01').getTime());
107
- });
108
-
109
- it('should handle multiple mark flags simultaneously', () => {
110
- const event = createMockActivityMarkEvent({
111
- mark_all_read: true,
112
- mark_all_seen: true,
113
- });
114
- const currentStatus = createMockNotificationStatus({
115
- read_activities: ['existing1'],
116
- last_seen_at: new Date('2023-01-01'),
117
- });
118
- const aggregatedActivities = [
119
- createMockAggregatedActivity('group1'),
120
- createMockAggregatedActivity('group2'),
121
- ];
122
-
123
- const result = updateNotificationStatusFromActivityMarked(
124
- event,
125
- currentStatus,
126
- aggregatedActivities,
127
- );
128
-
129
- expect(result.changed).toBe(true);
130
- expect(result.data?.notification_status.read_activities).toEqual([
131
- 'existing1',
132
- 'group1',
133
- 'group2',
134
- ]);
135
- expect(result.data?.notification_status.last_seen_at).toBeInstanceOf(
136
- Date,
137
- );
138
- });
139
-
140
- it('should deduplicate read activities when adding new ones', () => {
141
- const event = createMockActivityMarkEvent({
142
- mark_read: ['activity1', 'activity1', 'activity2'],
143
- });
144
- const currentStatus = createMockNotificationStatus({
145
- read_activities: ['existing1', 'activity1'],
146
- });
147
-
148
- const result = updateNotificationStatusFromActivityMarked(
149
- event,
150
- currentStatus,
151
- );
152
-
153
- expect(result.changed).toBe(true);
154
- expect(result.data?.notification_status.read_activities).toEqual([
155
- 'existing1',
156
- 'activity1',
157
- 'activity2',
158
- ]);
159
- });
160
-
161
- it('should preserve existing notification status fields', () => {
162
- const event = createMockActivityMarkEvent({ mark_all_seen: true });
163
- const currentStatus = createMockNotificationStatus({
164
- unread: 5,
165
- unseen: 3,
166
- read_activities: ['existing1'],
167
- });
168
-
169
- const result = updateNotificationStatusFromActivityMarked(
170
- event,
171
- currentStatus,
172
- );
173
-
174
- expect(result.changed).toBe(true);
175
- expect(result.data?.notification_status.unread).toBe(5);
176
- expect(result.data?.notification_status.unseen).toBe(3);
177
- expect(result.data?.notification_status.read_activities).toEqual([
178
- 'existing1',
179
- ]);
180
- expect(result.data?.notification_status.last_seen_at).toBeInstanceOf(
181
- Date,
182
- );
183
- });
184
-
185
- it('should handle mark_all_read with no existing read_activities', () => {
186
- const event = createMockActivityMarkEvent({ mark_all_read: true });
187
- const currentStatus = createMockNotificationStatus({
188
- read_activities: undefined,
189
- });
190
- const aggregatedActivities = [
191
- createMockAggregatedActivity('group1'),
192
- createMockAggregatedActivity('group2'),
193
- ];
194
-
195
- const result = updateNotificationStatusFromActivityMarked(
196
- event,
197
- currentStatus,
198
- aggregatedActivities,
199
- );
200
-
201
- expect(result.changed).toBe(true);
202
- expect(result.data?.notification_status.read_activities).toEqual([
203
- 'group1',
204
- 'group2',
205
- ]);
206
- });
207
- });
208
- });
@@ -1,371 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import type {
3
- ActivityReactionAddedEvent,
4
- ActivityReactionDeletedEvent,
5
- ActivityResponse,
6
- FeedsReactionResponse,
7
- } from '../../../gen/models';
8
- import { addReactionToActivities, removeReactionFromActivities } from './';
9
- import {
10
- generateActivityResponse,
11
- generateFeedReactionResponse,
12
- } from '../../../test-utils';
13
-
14
- const addReactionToActivity = (
15
- event: ActivityReactionAddedEvent,
16
- activity: ActivityResponse,
17
- eventBelongsToCurrentUser: boolean,
18
- ) => {
19
- const result = addReactionToActivities(
20
- event,
21
- [activity],
22
- eventBelongsToCurrentUser,
23
- );
24
-
25
- return {
26
- changed: result.changed,
27
- ...result.entities![0],
28
- };
29
- };
30
-
31
- const removeReactionFromActivity = (
32
- event: ActivityReactionAddedEvent,
33
- activity: ActivityResponse,
34
- eventBelongsToCurrentUser: boolean,
35
- ) => {
36
- const result = removeReactionFromActivities(
37
- event,
38
- [activity],
39
- eventBelongsToCurrentUser,
40
- );
41
-
42
- return {
43
- changed: result.changed,
44
- ...result.entities![0],
45
- };
46
- };
47
-
48
- const createMockAddedEvent = (
49
- reaction: FeedsReactionResponse,
50
- activity: ActivityResponse,
51
- ): ActivityReactionAddedEvent => ({
52
- fid: 'test-fid',
53
- reaction,
54
- activity,
55
- created_at: new Date(),
56
- custom: {},
57
- type: 'reaction.added',
58
- });
59
-
60
- const createMockDeletedEvent = (
61
- reaction: FeedsReactionResponse,
62
- activity: ActivityResponse,
63
- ): ActivityReactionDeletedEvent => ({
64
- fid: 'test-fid',
65
- reaction,
66
- activity,
67
- created_at: new Date(),
68
- custom: {},
69
- type: 'reaction.deleted',
70
- });
71
-
72
- describe('activity-reaction-utils', () => {
73
- describe('addReactionToActivity', () => {
74
- it('should add reaction to own_reactions when from current user', () => {
75
- const activity = generateActivityResponse({ id: 'activity1' });
76
- const reaction = generateFeedReactionResponse({
77
- type: 'like',
78
- user: { id: 'user1' },
79
- activity_id: 'activity1',
80
- });
81
- const eventActivity = { ...activity };
82
- eventActivity.latest_reactions = [reaction];
83
- eventActivity.reaction_groups = {
84
- [reaction.type]: {
85
- count: 1,
86
- first_reaction_at: reaction.created_at,
87
- last_reaction_at: reaction.created_at,
88
- sum_scores: 0,
89
- },
90
- };
91
-
92
- const event = createMockAddedEvent(reaction, eventActivity);
93
- const result = addReactionToActivity(event, activity, true);
94
- expect(result.changed).toBe(true);
95
- expect(result.own_reactions).toHaveLength(1);
96
- expect(result.own_reactions[0]).toEqual(reaction);
97
- expect(result.latest_reactions).toHaveLength(1);
98
- expect(result.latest_reactions[0]).toEqual(reaction);
99
- expect(result.reaction_groups.like).toEqual({
100
- count: 1,
101
- first_reaction_at: reaction.created_at,
102
- last_reaction_at: reaction.created_at,
103
- sum_scores: 0,
104
- });
105
- });
106
-
107
- it('should not add reaction to own_reactions when not from current user', () => {
108
- const activity = generateActivityResponse({ id: 'activity1' });
109
- const reaction = generateFeedReactionResponse({
110
- type: 'like',
111
- user: { id: 'user2' },
112
- activity_id: 'activity1',
113
- });
114
- const eventActivity = { ...activity };
115
- eventActivity.latest_reactions = [reaction];
116
- eventActivity.reaction_groups = {
117
- [reaction.type]: {
118
- count: 1,
119
- first_reaction_at: reaction.created_at,
120
- last_reaction_at: reaction.created_at,
121
- sum_scores: 0,
122
- },
123
- };
124
- const event = createMockAddedEvent(reaction, eventActivity);
125
-
126
- const result = addReactionToActivity(event, activity, false);
127
-
128
- expect(result.changed).toBe(true);
129
- expect(result.own_reactions).toHaveLength(0);
130
- expect(result.latest_reactions).toHaveLength(1);
131
- expect(result.latest_reactions[0]).toEqual(reaction);
132
- expect(result.reaction_groups.like).toEqual({
133
- count: 1,
134
- first_reaction_at: reaction.created_at,
135
- last_reaction_at: reaction.created_at,
136
- sum_scores: 0,
137
- });
138
- });
139
- });
140
-
141
- describe('removeReactionFromActivity', () => {
142
- it('should remove reaction from own_reactions when from current user', () => {
143
- const activity = generateActivityResponse({ id: 'activity1' });
144
- const reaction = generateFeedReactionResponse({
145
- type: 'like',
146
- user: { id: 'user1' },
147
- activity_id: 'activity1',
148
- });
149
- const eventActivity = { ...activity };
150
- eventActivity.latest_reactions = [reaction];
151
- eventActivity.reaction_groups = {
152
- [reaction.type]: {
153
- count: 1,
154
- first_reaction_at: reaction.created_at,
155
- last_reaction_at: reaction.created_at,
156
- sum_scores: 0,
157
- },
158
- };
159
- const event = createMockAddedEvent(reaction, eventActivity);
160
- const activityWithReaction = addReactionToActivity(event, activity, true);
161
-
162
- const deleteEventActivity = { ...activityWithReaction };
163
- deleteEventActivity.latest_reactions = [];
164
- deleteEventActivity.reaction_groups = {};
165
- const deleteEvent = createMockDeletedEvent(reaction, deleteEventActivity);
166
- const result = removeReactionFromActivity(
167
- deleteEvent,
168
- activityWithReaction,
169
- true,
170
- );
171
-
172
- expect(result.changed).toBe(true);
173
- expect(result.own_reactions).toHaveLength(0);
174
- expect(result.latest_reactions).toHaveLength(0);
175
- expect(result.reaction_groups.like).toBeUndefined();
176
- });
177
-
178
- it('should not remove reaction from own_reactions when not from current user', () => {
179
- const activity = generateActivityResponse({ id: 'activity1' });
180
- const reaction = generateFeedReactionResponse({
181
- type: 'like',
182
- user: { id: 'user1' },
183
- activity_id: 'activity1',
184
- });
185
- const eventActivity = { ...activity };
186
- eventActivity.latest_reactions = [reaction];
187
- eventActivity.reaction_groups = {
188
- [reaction.type]: {
189
- count: 1,
190
- first_reaction_at: reaction.created_at,
191
- last_reaction_at: reaction.created_at,
192
- sum_scores: 0,
193
- },
194
- };
195
- const event = createMockAddedEvent(reaction, eventActivity);
196
- const activityWithReaction = addReactionToActivity(event, activity, true);
197
-
198
- const deleteEventActivity = { ...activityWithReaction };
199
- deleteEventActivity.latest_reactions = [];
200
- deleteEventActivity.reaction_groups = {};
201
- const deleteEvent = createMockDeletedEvent(reaction, deleteEventActivity);
202
- const result = removeReactionFromActivity(
203
- deleteEvent,
204
- activityWithReaction,
205
- false,
206
- );
207
-
208
- expect(result.changed).toBe(true);
209
- expect(result.own_reactions).toHaveLength(1);
210
- expect(result.latest_reactions).toHaveLength(0);
211
- expect(result.reaction_groups.like).toBeUndefined();
212
- });
213
- });
214
-
215
- describe('addReactionToActivities', () => {
216
- it('should add reaction to activity in activities array', () => {
217
- const activity = generateActivityResponse({ id: 'activity1' });
218
- const activities = [activity];
219
- const reaction = generateFeedReactionResponse({
220
- type: 'like',
221
- user: { id: 'user1' },
222
- activity_id: 'activity1',
223
- });
224
- const eventActivity = { ...activity };
225
- eventActivity.latest_reactions = [reaction];
226
- eventActivity.reaction_groups = {
227
- [reaction.type]: {
228
- count: 1,
229
- first_reaction_at: reaction.created_at,
230
- last_reaction_at: reaction.created_at,
231
- sum_scores: 0,
232
- },
233
- };
234
- const event = createMockAddedEvent(reaction, eventActivity);
235
-
236
- const result = addReactionToActivities(event, activities, true);
237
-
238
- expect(result.changed).toBe(true);
239
- expect(result.entities!).toHaveLength(1);
240
- expect(result.entities![0].own_reactions).toHaveLength(1);
241
- expect(result.entities![0].own_reactions[0]).toEqual(reaction);
242
- });
243
-
244
- it('should return unchanged state if activity not found', () => {
245
- const activity = generateActivityResponse({ id: 'activity1' });
246
- const activities = [activity];
247
- const reaction = generateFeedReactionResponse({
248
- type: 'like',
249
- user: { id: 'user1' },
250
- activity_id: 'activity2',
251
- });
252
- const eventActivity = generateActivityResponse({ id: 'activity2' });
253
- eventActivity.latest_reactions = [reaction];
254
- eventActivity.reaction_groups = {
255
- [reaction.type]: {
256
- count: 1,
257
- first_reaction_at: reaction.created_at,
258
- last_reaction_at: reaction.created_at,
259
- sum_scores: 0,
260
- },
261
- };
262
- const event = createMockAddedEvent(reaction, eventActivity);
263
-
264
- const result = addReactionToActivities(event, activities, true);
265
-
266
- expect(result.changed).toBe(false);
267
- expect(result.entities).toBe(activities);
268
- });
269
-
270
- it('should handle undefined activities', () => {
271
- const activity = generateActivityResponse({ id: 'activity1' });
272
- const reaction = generateFeedReactionResponse({
273
- type: 'like',
274
- user: { id: 'user1' },
275
- activity_id: 'activity1',
276
- });
277
- const eventActivity = { ...activity };
278
- eventActivity.own_reactions = [reaction];
279
- eventActivity.latest_reactions = [reaction];
280
- eventActivity.reaction_groups = {
281
- [reaction.type]: {
282
- count: 1,
283
- first_reaction_at: reaction.created_at,
284
- last_reaction_at: reaction.created_at,
285
- sum_scores: 0,
286
- },
287
- };
288
- const event = createMockAddedEvent(reaction, eventActivity);
289
-
290
- const result = addReactionToActivities(event, undefined, true);
291
-
292
- expect(result.changed).toBe(false);
293
- expect(result.entities).toBeUndefined();
294
- });
295
- });
296
-
297
- describe('removeReactionFromActivities', () => {
298
- it('should remove reaction from activity in activities array', () => {
299
- const activity = generateActivityResponse({ id: 'activity1' });
300
- const reaction = generateFeedReactionResponse({
301
- type: 'like',
302
- user: { id: 'user1' },
303
- activity_id: 'activity1',
304
- });
305
- const eventActivity = { ...activity };
306
- eventActivity.latest_reactions = [reaction];
307
- eventActivity.reaction_groups = {
308
- [reaction.type]: {
309
- count: 1,
310
- first_reaction_at: reaction.created_at,
311
- last_reaction_at: reaction.created_at,
312
- sum_scores: 0,
313
- },
314
- };
315
- const event = createMockAddedEvent(reaction, eventActivity);
316
- const activityWithReaction = addReactionToActivity(event, activity, true);
317
- const activities = [activityWithReaction];
318
-
319
- const deleteEventActivity = { ...activityWithReaction };
320
- deleteEventActivity.latest_reactions = [];
321
- deleteEventActivity.reaction_groups = {};
322
- const deleteEvent = createMockDeletedEvent(reaction, deleteEventActivity);
323
- const result = removeReactionFromActivities(
324
- deleteEvent,
325
- activities,
326
- true,
327
- );
328
-
329
- expect(result.changed).toBe(true);
330
- expect(result.entities).toHaveLength(1);
331
- expect(result.entities![0].own_reactions).toHaveLength(0);
332
- });
333
-
334
- it('should return unchanged state if activity not found', () => {
335
- const activity = generateActivityResponse({ id: 'activity1' });
336
- const activities = [activity];
337
- const reaction = generateFeedReactionResponse({
338
- type: 'like',
339
- user: { id: 'user1' },
340
- activity_id: 'activity2',
341
- });
342
- const eventActivity = generateActivityResponse({ id: 'activity2' });
343
- eventActivity.latest_reactions = [];
344
- eventActivity.reaction_groups = {};
345
- const event = createMockDeletedEvent(reaction, eventActivity);
346
-
347
- const result = removeReactionFromActivities(event, activities, true);
348
-
349
- expect(result.changed).toBe(false);
350
- expect(result.entities).toBe(activities);
351
- });
352
-
353
- it('should handle undefined activities', () => {
354
- const activity = generateActivityResponse({ id: 'activity1' });
355
- const reaction = generateFeedReactionResponse({
356
- type: 'like',
357
- user: { id: 'user1' },
358
- activity_id: 'activity1',
359
- });
360
- const eventActivity = { ...activity };
361
- eventActivity.latest_reactions = [];
362
- eventActivity.reaction_groups = {};
363
- const event = createMockDeletedEvent(reaction, eventActivity);
364
-
365
- const result = removeReactionFromActivities(event, undefined, true);
366
-
367
- expect(result.changed).toBe(false);
368
- expect(result.entities).toBeUndefined();
369
- });
370
- });
371
- });
@@ -1,97 +0,0 @@
1
- import { describe, it, expect, beforeEach, vi } from 'vitest';
2
- import { Feed } from '../../../feed';
3
- import { FeedsClient } from '../../../feeds-client';
4
- import { handleActivityAdded } from './handle-activity-added';
5
- import {
6
- generateActivityAddedEvent,
7
- generateActivityResponse,
8
- generateFeedResponse,
9
- generateOwnUser,
10
- getHumanId,
11
- } from '../../../test-utils';
12
-
13
- describe(handleActivityAdded.name, () => {
14
- let feed: Feed;
15
- let client: FeedsClient;
16
- let currentUserId: string;
17
-
18
- beforeEach(() => {
19
- client = new FeedsClient('mock-api-key');
20
- currentUserId = getHumanId();
21
- client.state.partialNext({
22
- connected_user: generateOwnUser({ id: currentUserId }),
23
- });
24
-
25
- const feedResponse = generateFeedResponse({
26
- id: 'main',
27
- group_id: 'user',
28
- created_by: { id: currentUserId },
29
- });
30
-
31
- feed = new Feed(
32
- client,
33
- feedResponse.group_id,
34
- feedResponse.id,
35
- feedResponse,
36
- );
37
- });
38
-
39
- it('if activities are not initialized, it does nothing', () => {
40
- const event = generateActivityAddedEvent();
41
-
42
- const stateBefore = feed.currentState;
43
- expect(stateBefore.activities).toBeUndefined();
44
-
45
- const hydrateSpy = vi.spyOn(client, 'hydratePollCache');
46
-
47
- handleActivityAdded.call(feed, event);
48
-
49
- const stateAfter = feed.currentState;
50
- expect(stateAfter.activities).toBeUndefined();
51
- expect(hydrateSpy).not.toHaveBeenCalled();
52
- });
53
-
54
- it('prepends new activity when activities already exist', () => {
55
- const existing = generateActivityResponse();
56
- feed.state.partialNext({ activities: [existing] });
57
-
58
- const event = generateActivityAddedEvent();
59
-
60
- handleActivityAdded.call(feed, event);
61
-
62
- const stateAfter = feed.currentState;
63
- expect(stateAfter.activities).toHaveLength(2);
64
- expect(stateAfter.activities?.[0]).toBe(event.activity);
65
- expect(stateAfter.activities?.[1]).toBe(existing);
66
- });
67
-
68
- it('add new activity to the end if addNewActivitiesTo is end', () => {
69
- feed.state.partialNext({ addNewActivitiesTo: 'end' });
70
- const existing = generateActivityResponse();
71
- feed.state.partialNext({ activities: [existing] });
72
- const event = generateActivityAddedEvent();
73
- handleActivityAdded.call(feed, event);
74
-
75
- const stateAfter = feed.currentState;
76
- expect(stateAfter.activities).toHaveLength(2);
77
- expect(stateAfter.activities?.[0]).toBe(existing);
78
- expect(stateAfter.activities?.[1]).toBe(event.activity);
79
- });
80
-
81
- it('does not duplicate if activity already exists', () => {
82
- const existing = generateActivityResponse();
83
- feed.state.partialNext({ activities: [existing] });
84
-
85
- const event = generateActivityAddedEvent({
86
- activity: { id: existing.id },
87
- });
88
-
89
- const stateBefore = feed.currentState;
90
- handleActivityAdded.call(feed, event);
91
- const stateAfter = feed.currentState;
92
-
93
- expect(stateAfter).toBe(stateBefore);
94
- expect(stateAfter.activities).toHaveLength(1);
95
- expect(stateAfter.activities?.[0]).toBe(existing);
96
- });
97
- });