-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.test.js
More file actions
351 lines (316 loc) · 15 KB
/
contact.test.js
File metadata and controls
351 lines (316 loc) · 15 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
import { assert } from './test-utils/deps-node.js';
import { Contact } from '../ts/appTemplates/Contact.ts';
/**
* Unit tests for the post-plan-61 CMC-only Contact class.
*
* Surfaces under test:
* - constructor + addAccessObject (raw access tracking)
* - initStreamCache + eventIsAccessible (access-permission-derived stream cache)
* - eventIsFromContact (modifiedBy attribution including replaced-access chain
* and Plan 66 composite ids)
* - aggregateCmc + cmc* getters (Plan 59 Phase 5a)
*/
describe('[CTCT] Contact class', function () {
describe('[CTCS] constructor', function () {
it('[CTA1] should create an empty contact', () => {
const c = new Contact('dr-alice', 'Dr. Alice');
assert.equal(c.remoteUsername, 'dr-alice');
assert.equal(c.displayName, 'Dr. Alice');
assert.deepEqual(c.accessObjects, []);
assert.deepEqual(c.cmcRelationships, []);
assert.equal(c.counterparty, null);
assert.equal(c.kind, 'unknown');
});
it('[CTA2] isPerson reflects remoteUsername presence', () => {
assert.equal(new Contact('u', 'U').isPerson, true);
assert.equal(new Contact(null, 'bridge').isPerson, false);
});
});
describe('[CTAO] addAccessObject', function () {
it('[CTAB] adds access objects and dedups by id', () => {
const c = new Contact('u', 'U');
c.addAccessObject({ id: 'a1', permissions: [] });
c.addAccessObject({ id: 'a1', permissions: [] });
c.addAccessObject({ id: 'a2', permissions: [] });
assert.equal(c.accessObjects.length, 2);
});
it('[CTAC] accessIds is derived from accessObjects', () => {
const c = new Contact('u', 'U');
c.addAccessObject({ id: 'a1' });
c.addAccessObject({ id: 'a2' });
assert.deepEqual(c.accessIds.sort(), ['a1', 'a2']);
});
});
describe('[CTSC] initStreamCache & eventIsAccessible', function () {
const streamsById = {
health: { id: 'health', children: [{ id: 'health-bp', children: [] }] },
diary: { id: 'diary', children: [] },
'health-bp': { id: 'health-bp', children: [] }
};
it('[CTAL] eventIsAccessible returns false before initStreamCache', () => {
const c = new Contact('u', 'U');
assert.equal(c.eventIsAccessible({ streamIds: ['health'] }), false);
});
it('[CTAM] matches events in permitted streams and children', () => {
const c = new Contact('u', 'U');
c.addAccessObject({ id: 'a1', permissions: [{ streamId: 'health', level: 'read' }] });
c.initStreamCache(streamsById);
assert.equal(c.eventIsAccessible({ streamIds: ['health'] }), true);
assert.equal(c.eventIsAccessible({ streamIds: ['health-bp'] }), true);
assert.equal(c.eventIsAccessible({ streamIds: ['diary'] }), false);
});
it('[CTAN] wildcard permission matches everything', () => {
const c = new Contact('u', 'U');
c.addAccessObject({ id: 'a1', permissions: [{ streamId: '*', level: 'manage' }] });
c.initStreamCache(streamsById);
assert.equal(c.eventIsAccessible({ streamIds: ['anything'] }), true);
});
it('[CTAO2] skips deleted accesses', () => {
const c = new Contact('u', 'U');
c.addAccessObject({ id: 'a1', deleted: true, permissions: [{ streamId: 'health', level: 'read' }] });
c.initStreamCache(streamsById);
assert.equal(c.eventIsAccessible({ streamIds: ['health'] }), false);
});
it('[CTAP2] handles events with no streamIds', () => {
const c = new Contact('u', 'U');
c.addAccessObject({ id: 'a1', permissions: [{ streamId: 'health', level: 'read' }] });
c.initStreamCache(streamsById);
assert.equal(c.eventIsAccessible({}), false);
assert.equal(c.eventIsAccessible({ streamIds: null }), false);
});
});
describe('[CTEF] eventIsFromContact', function () {
it('[CTAQ] matches events modified by contact access', () => {
const c = new Contact('u', 'U');
c.addAccessObject({ id: 'a1' });
c.addAccessObject({ id: 'a2' });
assert.equal(c.eventIsFromContact({ modifiedBy: 'a1' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'a2' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'other' }), false);
});
it('[CTAQ2] matches events from previous (replaced) accesses via clientData chain', () => {
const c = new Contact('u', 'U');
c.addAccessObject({
id: 'a3-new',
clientData: {
hdsCollectorClient: {
version: 0,
previousAccessIds: ['a1-old', 'a2-older']
}
}
});
assert.equal(c.eventIsFromContact({ modifiedBy: 'a3-new' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'a1-old' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'a2-older' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'unknown' }), false);
});
it('[CTAQ3] matches across Plan 66 composite ids (base-only equality)', () => {
const c = new Contact('u', 'U');
c.addAccessObject({ id: 'a1:2' });
assert.equal(c.eventIsFromContact({ modifiedBy: 'a1' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'a1:1' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'a1:2' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'b1:0' }), false);
});
it('[CTAR] matches across legacy bridge-access recreate chain (clientData.previousAccessIds)', () => {
const c = new Contact(null, 'bridge');
c.addAccessObject({
id: 'b2',
clientData: { previousAccessIds: ['b1', 'b0'] }
});
assert.equal(c.eventIsFromContact({ modifiedBy: 'b0' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'b1' }), true);
assert.equal(c.eventIsFromContact({ modifiedBy: 'b2' }), true);
});
});
describe('[CTCM] CMC Contact aggregator', function () {
const SCOPE = ':_cmc:apps:hds-patient';
function counterpartyAccess (overrides = {}) {
const cp = {
username: 'drandy',
host: 'demo.datasafe.dev',
apiEndpoint: 'https://abctoken@drandy.demo.datasafe.dev/',
remoteChatStreamId: ':_cmc:apps:hds-collector:foo:chats:pthdstest--demo-datasafe-dev',
remoteCollectorStreamId: ':_cmc:apps:hds-collector:foo:collectors:pthdstest--demo-datasafe-dev',
...(overrides.counterpartyOverrides || {})
};
const cmc = {
role: 'counterparty',
appCode: 'hds-collector',
features: { chat: true, systemMessaging: true },
counterparty: cp,
...(overrides.cmcOverrides || {})
};
return {
id: overrides.id || 'acc-cp-1',
apiEndpoint: 'irrelevant',
permissions: overrides.permissions ?? [{ streamId: 'health', level: 'read' }],
deleted: overrides.deleted ?? null,
clientData: { cmc }
};
}
function acceptEvent (overrides = {}) {
return {
acceptEventId: overrides.acceptEventId || 'evt-accept-1',
counterparty: overrides.counterparty || { username: 'drandy', host: 'demo.datasafe.dev' },
appCode: overrides.appCode || 'hds-collector',
scopeStreamId: SCOPE,
acceptedAt: overrides.acceptedAt ?? 1716000000,
features: overrides.features || { chat: true, systemMessaging: true },
backChannelAccessId: overrides.backChannelAccessId || 'acc-cp-1'
};
}
it('[CTM1] cmcDetectKind splits person vs service by hds-bridge- prefix', () => {
assert.equal(Contact.cmcDetectKind('hds-collector'), 'person');
assert.equal(Contact.cmcDetectKind('hds-patient'), 'person');
assert.equal(Contact.cmcDetectKind('hds-bridge-mira'), 'service');
assert.equal(Contact.cmcDetectKind('hds-bridge-athenahealth'), 'service');
assert.equal(Contact.cmcDetectKind(null), 'unknown');
assert.equal(Contact.cmcDetectKind(undefined), 'unknown');
assert.equal(Contact.cmcDetectKind(''), 'unknown');
});
it('[CTM2] aggregateCmc returns empty when no counterparty accesses', () => {
const out = Contact.aggregateCmc([], [], SCOPE);
assert.deepEqual(out, []);
});
it('[CTM3] aggregateCmc skips deleted accesses', () => {
const a = counterpartyAccess({ deleted: { reason: 'revoked' } });
const out = Contact.aggregateCmc([a], [acceptEvent()], SCOPE);
assert.deepEqual(out, []);
});
it('[CTM4] aggregateCmc skips accesses without cmc.role === counterparty', () => {
const a = counterpartyAccess({ cmcOverrides: { role: 'requester' } });
const out = Contact.aggregateCmc([a], [], SCOPE);
assert.deepEqual(out, []);
});
it('[CTM5] aggregateCmc builds one Contact + one relationship from one access', () => {
const a = counterpartyAccess();
const out = Contact.aggregateCmc([a], [acceptEvent()], SCOPE);
assert.equal(out.length, 1);
const c = out[0];
assert.equal(c.counterparty.username, 'drandy');
assert.equal(c.counterparty.host, 'demo.datasafe.dev');
assert.equal(c.kind, 'person');
assert.equal(c.cmcRelationships.length, 1);
const rel = c.cmcRelationships[0];
assert.equal(rel.accessId, 'acc-cp-1');
assert.equal(rel.acceptEventId, 'evt-accept-1');
assert.equal(rel.counterpartyApiEndpoint, 'https://abctoken@drandy.demo.datasafe.dev/');
assert.equal(rel.appCode, 'hds-collector');
assert.deepEqual(rel.features, { chat: true, systemMessaging: true });
assert.equal(rel.acceptedAt, 1716000000);
assert.equal(rel.localChatStreamId, ':_cmc:apps:hds-patient:chats:drandy--demo-datasafe-dev');
// aggregator populates accessObjects too
assert.equal(c.accessObjects.length, 1);
assert.equal(c.accessObjects[0].id, 'acc-cp-1');
});
it('[CTM6] aggregateCmc groups multiple accesses from the same counterparty into one Contact', () => {
const a1 = counterpartyAccess({ id: 'acc-cp-1' });
const a2 = counterpartyAccess({ id: 'acc-cp-2', permissions: [{ streamId: 'sleep', level: 'read' }] });
const out = Contact.aggregateCmc([a1, a2], [acceptEvent()], SCOPE);
assert.equal(out.length, 1);
assert.equal(out[0].cmcRelationships.length, 2);
});
it('[CTM7] aggregateCmc puts different counterparties into different Contacts', () => {
const a1 = counterpartyAccess({ id: 'acc-cp-1' });
const a2 = counterpartyAccess({
id: 'acc-cp-2',
counterpartyOverrides: { username: 'drother', host: 'demo.datasafe.dev' }
});
const out = Contact.aggregateCmc([a1, a2], [], SCOPE);
assert.equal(out.length, 2);
});
it('[CTM8] aggregateCmc marks bridge contacts as kind=service', () => {
const a = counterpartyAccess({
cmcOverrides: { appCode: 'hds-bridge-mira', features: { chat: false } },
counterpartyOverrides: { username: 'bridgemiratest', host: 'demo.datasafe.dev' }
});
const out = Contact.aggregateCmc([a], [], SCOPE);
assert.equal(out.length, 1);
assert.equal(out[0].kind, 'service');
assert.equal(out[0].counterparty.username, 'bridgemiratest');
});
it('[CTM9] aggregateCmc tolerates missing accept events (acceptedAt: null)', () => {
const a = counterpartyAccess();
const out = Contact.aggregateCmc([a], [], SCOPE);
assert.equal(out.length, 1);
assert.equal(out[0].cmcRelationships[0].acceptedAt, null);
assert.equal(out[0].cmcRelationships[0].acceptEventId, null);
});
it('[CTMA] aggregateCmc drops ghost accept events (Q-C2): accepts without matching access', () => {
const out = Contact.aggregateCmc([], [acceptEvent()], SCOPE);
assert.deepEqual(out, []);
});
it('[CTMB] cmcAllPermissions dedupes across relationships', () => {
const a1 = counterpartyAccess({
id: 'a1',
permissions: [{ streamId: 'health', level: 'read' }, { streamId: 'sleep', level: 'read' }]
});
const a2 = counterpartyAccess({
id: 'a2',
permissions: [{ streamId: 'sleep', level: 'read' }, { streamId: 'mood', level: 'contribute' }]
});
const out = Contact.aggregateCmc([a1, a2], [], SCOPE);
const perms = out[0].cmcAllPermissions;
assert.equal(perms.length, 3);
assert.deepEqual(perms.map(p => p.streamId).sort(), ['health', 'mood', 'sleep']);
});
it('[CTMC] cmcChatStreams only includes chat-enabled relationships', () => {
const a1 = counterpartyAccess({
id: 'a1',
cmcOverrides: { role: 'counterparty', appCode: 'hds-collector', features: { chat: true, systemMessaging: false }, counterparty: { username: 'drandy', host: 'demo.datasafe.dev', apiEndpoint: 'https://t@drandy.x/', remoteChatStreamId: 'r1', remoteCollectorStreamId: 'c1' } }
});
const a2 = counterpartyAccess({
id: 'a2',
cmcOverrides: { role: 'counterparty', appCode: 'hds-collector', features: { chat: false }, counterparty: { username: 'drandy', host: 'demo.datasafe.dev', apiEndpoint: 'https://t@drandy.x/', remoteChatStreamId: null, remoteCollectorStreamId: null } }
});
const out = Contact.aggregateCmc([a1, a2], [], SCOPE);
const streams = out[0].cmcChatStreams;
assert.equal(streams.length, 1);
assert.equal(streams[0].read, 'r1');
assert.equal(streams[0].accessId, 'a1');
});
});
describe('[CTDE] Derived CMC-only getters', function () {
function bareContact (relsCount = 1, acceptedAt = 1700000000, appCode = 'hds-collector', chat = false) {
const c = new Contact('u', 'U');
for (let i = 0; i < relsCount; i++) {
c.cmcRelationships.push({
accessId: 'a' + i,
acceptEventId: 'evt' + i,
counterparty: { username: 'u', host: 'h' },
counterpartyApiEndpoint: null,
remoteChatStreamId: null,
remoteCollectorStreamId: null,
localChatStreamId: 'local',
appCode,
features: { chat, systemMessaging: false },
grantedPermissions: [{ streamId: 'health', level: 'read' }],
acceptedAt,
hdsFormSpec: null
});
}
return c;
}
it('[CTDS] status: Active when any acceptedAt, Incoming when relationships exist without acceptedAt, null otherwise', () => {
assert.equal(bareContact(1, 1700000000).status, 'Active');
assert.equal(bareContact(1, null).status, 'Incoming');
assert.equal(bareContact(0).status, null);
});
it('[CTDH] hasChat reflects any chat-enabled relationship', () => {
assert.equal(bareContact(1, 1700000000, 'hds-collector', true).hasChat, true);
assert.equal(bareContact(1, 1700000000, 'hds-collector', false).hasChat, false);
});
it('[CTDA] appStreamIds returns distinct appCodes', () => {
const c = new Contact('u', 'U');
c.cmcRelationships.push({ appCode: 'hds-collector' });
c.cmcRelationships.push({ appCode: 'hds-collector' });
c.cmcRelationships.push({ appCode: 'hds-bridge-mira' });
assert.deepEqual(c.appStreamIds.sort(), ['hds-bridge-mira', 'hds-collector']);
});
it('[CTDI] isActive ↔ any relationship has acceptedAt', () => {
assert.equal(bareContact(1, 1700000000).isActive, true);
assert.equal(bareContact(1, null).isActive, false);
});
});
});