-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathcommunitiesStore.test.ts
More file actions
386 lines (332 loc) · 12.3 KB
/
Copy pathcommunitiesStore.test.ts
File metadata and controls
386 lines (332 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import type {Chat, ChatFull} from '@layer';
import '@helpers/peerIdPolyfill';
import type {
CommunityDialog,
CommunityPeerLinkRequestsState
} from '@appManagers/appCommunitiesManager';
import {
reconcileCommunityDialog,
reconcileCommunityDialogs,
reconcileCommunityFull,
reconcileCommunityFulls,
reconcileCommunityPeerLinkRequests,
reconcileCommunityPeerLinkRequestsState,
collectCollapsedCommunityDialogsKey,
useCollapsedCommunityDialogsKey,
useCommunity,
useCommunityDialog,
useCommunityFull,
useJoinedCommunities,
useCommunityPendingRequestsCount,
useCommunityPeerLinkRequests
} from '@stores/communities';
import {setAppStateSilent} from '@stores/appState';
import {reconcilePeer, reconcilePeers} from '@stores/peers';
const COMMUNITY_ID = 101 as ChatId;
const OTHER_COMMUNITY_ID = 102 as ChatId;
function makeCommunity(
id: ChatId,
title: string
): Chat.community {
return {
_: 'community',
id,
title,
pFlags: {}
} as Chat.community;
}
function makeCommunityFull(id: ChatId, about: string, peerIds: UserId[] = []) {
return {
_: 'communityFull',
id,
about,
chat_photo: {
_: 'photoEmpty',
id: 0
},
linked_peers: peerIds.map((userId) => ({
_: 'communityPeer',
pFlags: {},
peer: {_: 'peerUser', user_id: userId}
}))
} as ChatFull.communityFull;
}
function makeCommunityDialog(
id: ChatId,
unreadCount: number,
peerIds: PeerId[] = []
) {
const dialogs = peerIds.map((peerId) => ({
_: 'dialog',
peerId
})) as CommunityDialog['dialogs'];
return {
_: 'communityDialog',
communityId: id,
pFlags: {},
notifySettings: {_: 'peerNotifySettings'},
dialogs,
lastDialogs: dialogs.slice(),
sortDate: 0,
unreadCount,
unreadMessagesCount: unreadCount,
unreadUnmutedCount: unreadCount,
unreadMarked: false
} as CommunityDialog;
}
function makePeerLinkRequests(
totalCount: number,
peerIds: UserId[] = []
): CommunityPeerLinkRequestsState {
return {
loaded: true,
totalCount,
requests: peerIds.map((userId) => ({
_: 'communityPeerRequest',
pFlags: {},
peer: {_: 'peerUser', user_id: userId},
requested_by: 1,
date: 1
}))
};
}
describe('communities Solid stores', () => {
beforeEach(() => {
reconcilePeers({});
reconcileCommunityFulls({});
reconcileCommunityDialogs({});
reconcileCommunityPeerLinkRequests({});
setAppStateSilent('joinedCommunityIds', null);
});
test('reconciles keyed communities and removes deleted values', () => {
const community = useCommunity(() => COMMUNITY_ID);
reconcilePeer(COMMUNITY_ID.toPeerId(true), makeCommunity(COMMUNITY_ID, 'First'));
const firstValue = community();
expect(firstValue.title).toBe('First');
reconcilePeer(COMMUNITY_ID.toPeerId(true), makeCommunity(COMMUNITY_ID, 'Updated'));
expect(community()).toBe(firstValue);
expect(community().title).toBe('Updated');
reconcilePeer(COMMUNITY_ID.toPeerId(true));
expect(community()).toBeUndefined();
});
test('reconciles full Community snapshots and drops stale keys', () => {
const full = useCommunityFull(() => COMMUNITY_ID);
reconcileCommunityFulls({
[COMMUNITY_ID]: makeCommunityFull(COMMUNITY_ID, 'First'),
[OTHER_COMMUNITY_ID]: makeCommunityFull(OTHER_COMMUNITY_ID, 'Other')
});
expect(full().about).toBe('First');
reconcileCommunityFulls({
[OTHER_COMMUNITY_ID]: makeCommunityFull(OTHER_COMMUNITY_ID, 'Updated')
});
expect(full()).toBeUndefined();
reconcileCommunityFull(COMMUNITY_ID, makeCommunityFull(COMMUNITY_ID, 'Restored'));
expect(full().about).toBe('Restored');
});
test('reactively reconciles the removed-users count to zero', () => {
const full = useCommunityFull(() => COMMUNITY_ID);
const initial = makeCommunityFull(COMMUNITY_ID, 'About');
initial.kicked_count = 1;
reconcileCommunityFull(COMMUNITY_ID, initial);
const stored = full();
const updated = makeCommunityFull(COMMUNITY_ID, 'About');
updated.kicked_count = 0;
reconcileCommunityFull(COMMUNITY_ID, updated);
expect(full()).toBe(stored);
expect(full().kicked_count).toBe(0);
});
test('derives joined Communities from the mirrored app state order', () => {
const joined = useJoinedCommunities();
reconcilePeers({
[COMMUNITY_ID.toPeerId(true)]: makeCommunity(COMMUNITY_ID, 'First'),
[OTHER_COMMUNITY_ID.toPeerId(true)]: makeCommunity(OTHER_COMMUNITY_ID, 'Other')
});
setAppStateSilent('joinedCommunityIds', [
OTHER_COMMUNITY_ID,
COMMUNITY_ID
]);
expect(joined()?.map((community) => community.id)).toEqual([
OTHER_COMMUNITY_ID,
COMMUNITY_ID
]);
});
test('reconciles and deletes computed Community dialogs', () => {
const dialog = useCommunityDialog(() => COMMUNITY_ID);
reconcileCommunityDialog(COMMUNITY_ID, makeCommunityDialog(COMMUNITY_ID, 3));
const firstValue = dialog();
expect(firstValue.unreadCount).toBe(3);
reconcileCommunityDialog(COMMUNITY_ID, makeCommunityDialog(COMMUNITY_ID, 1));
expect(dialog()).toBe(firstValue);
expect(dialog().unreadCount).toBe(1);
reconcileCommunityDialog(COMMUNITY_ID);
expect(dialog()).toBeUndefined();
});
test('derives the collapsed Community dialog projection key', () => {
const key = useCollapsedCommunityDialogsKey();
setAppStateSilent('joinedCommunityIds', [COMMUNITY_ID]);
reconcilePeer(
COMMUNITY_ID.toPeerId(true),
makeCommunity(COMMUNITY_ID, 'Community')
);
expect(key()).toBe('');
const collapsedCommunity = makeCommunity(COMMUNITY_ID, 'Community');
collapsedCommunity.pFlags.collapsed_in_dialogs = true;
reconcilePeer(COMMUNITY_ID.toPeerId(true), collapsedCommunity);
expect(key()).toBe(`${COMMUNITY_ID}:`);
const peerId = 201 as PeerId;
reconcileCommunityDialog(
COMMUNITY_ID,
makeCommunityDialog(COMMUNITY_ID, 0, [peerId])
);
expect(key()).toBe(`${COMMUNITY_ID}:${peerId}`);
const leftCommunity = makeCommunity(COMMUNITY_ID, 'Community');
leftCommunity.pFlags.collapsed_in_dialogs = true;
leftCommunity.pFlags.left = true;
reconcilePeer(COMMUNITY_ID.toPeerId(true), leftCommunity);
expect(key()).toBe('');
});
// The key used to be derived with Object.values(peers). That enumerates the whole peer
// store and, because it is a reactive proxy, subscribes the memo to every peer — so an
// unrelated user coming online re-ran the entire walk (~1s of main thread on a large
// account). Assert it now touches ONLY the Community ids and never enumerates.
test('derives the projection key without enumerating the peer store', () => {
const collapsed = makeCommunity(COMMUNITY_ID, 'Community');
collapsed.pFlags.collapsed_in_dialogs = true;
const backing: Record<string, any> = {
[COMMUNITY_ID.toPeerId(true)]: collapsed
};
for(let i = 0; i < 500; ++i) {
backing[900 + i] = {_: 'user', id: 900 + i, pFlags: {}};
}
const readKeys: string[] = [];
let enumerated = 0;
const peers = new Proxy(backing, {
get(target, prop) {
if(typeof prop === 'string') readKeys.push(prop);
return target[prop as string];
},
ownKeys(target) {
++enumerated;
return Reflect.ownKeys(target);
}
});
const key = collectCollapsedCommunityDialogsKey(
peers as any,
{[COMMUNITY_ID]: makeCommunityDialog(COMMUNITY_ID, 0, [301 as PeerId])} as any,
[COMMUNITY_ID]
);
expect(key).toBe(`${COMMUNITY_ID}:301`);
expect(enumerated).toBe(0);
expect(readKeys).toEqual(['' + COMMUNITY_ID.toPeerId(true)]);
});
test('still reacts to a Community collapsing and to its dialog list', () => {
const key = useCollapsedCommunityDialogsKey();
setAppStateSilent('joinedCommunityIds', [OTHER_COMMUNITY_ID]);
const community = makeCommunity(OTHER_COMMUNITY_ID, 'Other');
reconcilePeer(OTHER_COMMUNITY_ID.toPeerId(true), community);
expect(key()).toBe('');
const collapsed = makeCommunity(OTHER_COMMUNITY_ID, 'Other');
collapsed.pFlags.collapsed_in_dialogs = true;
reconcilePeer(OTHER_COMMUNITY_ID.toPeerId(true), collapsed);
expect(key()).toBe(`${OTHER_COMMUNITY_ID}:`);
reconcileCommunityDialog(
OTHER_COMMUNITY_ID,
makeCommunityDialog(OTHER_COMMUNITY_ID, 0, [301 as PeerId])
);
expect(key()).toBe(`${OTHER_COMMUNITY_ID}:301`);
});
// A Community known only through the mirrored dialogs map (not yet in
// joinedCommunityIds) still has to show up in the key.
test('picks up a Community that only exists in the dialogs store', () => {
const key = useCollapsedCommunityDialogsKey();
setAppStateSilent('joinedCommunityIds', null);
const collapsed = makeCommunity(COMMUNITY_ID, 'Community');
collapsed.pFlags.collapsed_in_dialogs = true;
reconcilePeer(COMMUNITY_ID.toPeerId(true), collapsed);
reconcileCommunityDialog(
COMMUNITY_ID,
makeCommunityDialog(COMMUNITY_ID, 0, [401 as PeerId])
);
expect(key()).toBe(`${COMMUNITY_ID}:401`);
});
test('reconciles peer-link request snapshots and keyed updates', () => {
const requests = useCommunityPeerLinkRequests(() => COMMUNITY_ID);
reconcileCommunityPeerLinkRequests({
[COMMUNITY_ID]: makePeerLinkRequests(2)
});
const firstValue = requests();
expect(firstValue.totalCount).toBe(2);
reconcileCommunityPeerLinkRequestsState(COMMUNITY_ID, makePeerLinkRequests(1));
expect(requests()).toBe(firstValue);
expect(requests().totalCount).toBe(1);
reconcileCommunityPeerLinkRequests({});
expect(requests()).toBeUndefined();
});
test('uses the live peer-link request count over the full snapshot', () => {
const count = useCommunityPendingRequestsCount(() => COMMUNITY_ID);
const full = makeCommunityFull(COMMUNITY_ID, 'About');
full.peer_link_requests_pending = 2;
reconcileCommunityFull(COMMUNITY_ID, full);
expect(count()).toBe(2);
reconcileCommunityPeerLinkRequestsState(
COMMUNITY_ID,
makePeerLinkRequests(3)
);
expect(count()).toBe(3);
const updatedFull = makeCommunityFull(COMMUNITY_ID, 'About');
updatedFull.peer_link_requests_pending = 4;
reconcileCommunityFull(COMMUNITY_ID, updatedFull);
expect(count()).toBe(3);
reconcileCommunityPeerLinkRequestsState(COMMUNITY_ID);
expect(count()).toBe(4);
});
test('does not reuse nested array items for a different peer after reorder', () => {
const firstUserId = 201 as UserId;
const secondUserId = 202 as UserId;
const firstPeerId = firstUserId as PeerId;
const secondPeerId = secondUserId as PeerId;
const dialog = useCommunityDialog(() => COMMUNITY_ID);
const full = useCommunityFull(() => COMMUNITY_ID);
const requests = useCommunityPeerLinkRequests(() => COMMUNITY_ID);
reconcileCommunityDialog(
COMMUNITY_ID,
makeCommunityDialog(COMMUNITY_ID, 0, [firstPeerId, secondPeerId])
);
reconcileCommunityFull(
COMMUNITY_ID,
makeCommunityFull(COMMUNITY_ID, 'About', [firstUserId, secondUserId])
);
reconcileCommunityPeerLinkRequestsState(
COMMUNITY_ID,
makePeerLinkRequests(2, [firstUserId, secondUserId])
);
const oldFirstDialog = dialog().dialogs[0];
const oldSecondDialog = dialog().dialogs[1];
const oldFirstLinkedPeer = full().linked_peers[0];
const oldSecondLinkedPeer = full().linked_peers[1];
const oldFirstRequest = requests().requests[0];
const oldSecondRequest = requests().requests[1];
reconcileCommunityDialog(
COMMUNITY_ID,
makeCommunityDialog(COMMUNITY_ID, 0, [secondPeerId, firstPeerId])
);
reconcileCommunityFull(
COMMUNITY_ID,
makeCommunityFull(COMMUNITY_ID, 'About', [secondUserId, firstUserId])
);
reconcileCommunityPeerLinkRequestsState(
COMMUNITY_ID,
makePeerLinkRequests(2, [secondUserId, firstUserId])
);
expect(dialog().dialogs.map((item) => item.peerId)).toEqual([
secondPeerId,
firstPeerId
]);
expect(dialog().dialogs[0]).not.toBe(oldFirstDialog);
expect(dialog().dialogs[1]).not.toBe(oldSecondDialog);
expect(full().linked_peers[0]).not.toBe(oldFirstLinkedPeer);
expect(full().linked_peers[1]).not.toBe(oldSecondLinkedPeer);
expect(requests().requests[0]).not.toBe(oldFirstRequest);
expect(requests().requests[1]).not.toBe(oldSecondRequest);
});
});