@webex/internal-plugin-llm 3.12.0-task-refactor.1 → 3.12.0-webex-services-ready.2

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.
@@ -3,6 +3,7 @@ import {assert} from '@webex/test-helper-chai';
3
3
  import sinon from 'sinon';
4
4
  import Mercury from '@webex/internal-plugin-mercury';
5
5
  import LLMService from '@webex/internal-plugin-llm';
6
+ import LLMChannel from '@webex/internal-plugin-llm/src/llm';
6
7
 
7
8
  describe('plugin-llm', () => {
8
9
  const locusUrl = 'locusUrl';
@@ -19,47 +20,182 @@ describe('plugin-llm', () => {
19
20
  },
20
21
  });
21
22
 
23
+ webex.internal.feature = {
24
+ setFeature: sinon.stub().resolves({value: true}),
25
+ getFeature: sinon.stub().resolves(true),
26
+ };
27
+
22
28
  llmService = webex.internal.llm;
23
- llmService.connect = sinon.stub().callsFake(() => {
24
- llmService.connected = true;
25
- });
29
+ llmService.webSocketUrl = 'wss://example.com/socket';
26
30
  llmService.disconnect = sinon.stub().resolves(true);
27
31
  llmService.request = sinon.stub().resolves({
28
32
  headers: {},
29
33
  body: {
30
34
  binding: 'binding',
31
- webSocketUrl: 'url',
35
+ webSocketUrl: 'wss://example.com/socket',
32
36
  },
33
37
  });
38
+ const sockets = new Map();
39
+
40
+ llmService.connect = sinon.stub().callsFake((url, sessionId) => {
41
+ sockets.set(sessionId, {connected: true});
42
+ llmService.getSocket = sinon.stub().callsFake((sid) => sockets.get(sid));
43
+ });
44
+ llmService.connections.set('llm-default-session',{
45
+ webSocketUrl: 'wss://example.com/socket',
46
+ })
34
47
  });
35
48
 
49
+ afterEach(() => sinon.restore());
50
+
36
51
  describe('#registerAndConnect', () => {
37
52
  it('registers connection', async () => {
38
- llmService.register = sinon.stub().resolves({
39
- body: {
40
- binding: 'binding',
41
- webSocketUrl: 'url',
42
- },
53
+ llmService.register = sinon.stub().callsFake(async () => {
54
+ llmService.binding = 'binding';
55
+ llmService.webSocketUrl = 'wss://example.com/socket';
56
+ return {
57
+ body: {
58
+ binding: 'binding',
59
+ webSocketUrl: 'wss://example.com/socket',
60
+ },
61
+ };
43
62
  });
44
- assert.equal(llmService.isConnected(), false);
45
- await llmService.registerAndConnect(locusUrl, datachannelUrl);
46
- assert.equal(llmService.isConnected(), true);
63
+
64
+ assert.equal(llmService.isConnected('llm-default-session'), false);
65
+ await llmService.registerAndConnect(locusUrl, datachannelUrl,undefined);
66
+ assert.equal(llmService.isConnected('llm-default-session'), true);
47
67
  });
48
68
 
49
- it("doesn't registers connection for invalid input", async () => {
50
- llmService.register = sinon.stub().resolves({
51
- body: {
52
- binding: 'binding',
53
- webSocketUrl: 'url',
54
- },
69
+ it("doesn't register connection for invalid input", async () => {
70
+ llmService.register = sinon.stub().callsFake(async () => {
71
+ llmService.binding = 'binding';
72
+ llmService.webSocketUrl = 'wss://example.com/socket';
73
+ return {
74
+ body: {
75
+ binding: 'binding',
76
+ webSocketUrl: 'wss://example.com/socket',
77
+ },
78
+ };
55
79
  });
80
+
56
81
  await llmService.registerAndConnect();
57
82
  assert.equal(llmService.isConnected(), false);
58
83
  });
84
+
85
+ it('registers connection with token', async () => {
86
+ llmService.register = sinon.stub().callsFake(async () => {
87
+ llmService.binding = 'binding';
88
+ llmService.webSocketUrl = 'wss://example.com/socket';
89
+ return {
90
+ body: {
91
+ binding: 'binding',
92
+ webSocketUrl: 'wss://example.com/socket',
93
+ },
94
+ };
95
+ });
96
+
97
+ assert.equal(llmService.isConnected(), false);
98
+
99
+ await llmService.registerAndConnect(locusUrl, datachannelUrl,'abc123');
100
+
101
+ sinon.assert.calledOnceWithExactly(
102
+ llmService.register,
103
+ datachannelUrl,
104
+ 'abc123',
105
+ 'llm-default-session'
106
+ );
107
+
108
+ assert.equal(llmService.isConnected(), true);
109
+ });
110
+
111
+ it('connects with subscriptionAwareSubchannels when token enabled', async () => {
112
+ llmService.isDataChannelTokenEnabled = sinon.stub().returns(true);
113
+
114
+ llmService.register = sinon.stub().callsFake(async () => {
115
+ llmService.binding = 'binding';
116
+ llmService.webSocketUrl = 'wss://example.com/socket';
117
+ return {
118
+ body: {
119
+ binding: 'binding',
120
+ webSocketUrl: 'wss://example.com/socket',
121
+ },
122
+ };
123
+ });
124
+
125
+ const buildSpy = sinon.spy(LLMService, 'buildUrlWithAwareSubchannels');
126
+
127
+ await llmService.registerAndConnect(locusUrl, datachannelUrl,'abc123');
128
+
129
+ sinon.assert.calledOnce(buildSpy);
130
+ sinon.assert.calledOnce(llmService.connect);
131
+
132
+ const calledUrl = llmService.connect.getCall(0).args[0];
133
+ assert.include(calledUrl, 'subscriptionAwareSubchannels=');
134
+ });
135
+
136
+ it('connects without subscriptionAwareSubchannels when token disabled', async () => {
137
+ llmService.isDataChannelTokenEnabled = sinon.stub().returns(false);
138
+
139
+ llmService.register = sinon.stub().callsFake(async () => {
140
+ llmService.binding = 'binding';
141
+ llmService.webSocketUrl = 'wss://example.com/socket';
142
+ return {
143
+ body: {
144
+ binding: 'binding',
145
+ webSocketUrl: 'wss://example.com/socket',
146
+ },
147
+ };
148
+ });
149
+
150
+ const buildSpy = sinon.spy(LLMService, 'buildUrlWithAwareSubchannels');
151
+
152
+ await llmService.registerAndConnect(locusUrl, datachannelUrl);
153
+
154
+ sinon.assert.notCalled(buildSpy);
155
+ sinon.assert.calledOnce(llmService.connect);
156
+
157
+ const calledUrl = llmService.connect.getCall(0).args[0];
158
+ assert.equal(calledUrl, llmService.webSocketUrl);
159
+ });
160
+
161
+ it('connects without subscriptionAwareSubchannels when token enabled BUT token missing', async () => {
162
+ llmService.isDataChannelTokenEnabled = sinon.stub().resolves(true);
163
+
164
+ const buildSpy = sinon.spy(LLMService, 'buildUrlWithAwareSubchannels');
165
+
166
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined);
167
+
168
+ sinon.assert.calledOnce(buildSpy);
169
+ sinon.assert.calledOnce(llmService.connect);
170
+
171
+ const calledUrl = llmService.connect.getCall(0).args[0];
172
+ assert.include(calledUrl, 'subscriptionAwareSubchannels=');
173
+
174
+ buildSpy.restore();
175
+ });
59
176
  });
60
177
 
61
178
  describe('#register', () => {
62
- it('registers connection', async () => {
179
+ beforeEach(() => {
180
+ llmService.isDataChannelTokenEnabled = sinon.stub();
181
+ });
182
+
183
+ it('registers connection with token header', async () => {
184
+ llmService.isDataChannelTokenEnabled.resolves(true);
185
+ await llmService.register(datachannelUrl, 'abc123');
186
+
187
+ sinon.assert.calledOnceWithExactly(
188
+ llmService.request,
189
+ sinon.match({
190
+ method: 'POST',
191
+ url: `${datachannelUrl}`,
192
+ body: {deviceUrl: webex.internal.device.url},
193
+ headers: {'Data-Channel-Auth-Token': 'abc123'},
194
+ })
195
+ );
196
+ });
197
+
198
+ it('registers connection without token header when none provided', async () => {
63
199
  await llmService.register(datachannelUrl);
64
200
 
65
201
  sinon.assert.calledOnceWithExactly(
@@ -68,21 +204,40 @@ describe('plugin-llm', () => {
68
204
  method: 'POST',
69
205
  url: `${datachannelUrl}`,
70
206
  body: {deviceUrl: webex.internal.device.url},
207
+ headers: {},
71
208
  })
72
209
  );
210
+ });
73
211
 
74
- assert.equal(llmService.getBinding(), 'binding');
212
+ it('registers connection without token header when toggle disabled', async () => {
213
+ llmService.isDataChannelTokenEnabled.resolves(false);
214
+
215
+ await llmService.register(datachannelUrl,'abc123');
216
+ sinon.assert.calledOnceWithExactly(
217
+ llmService.request,
218
+ sinon.match({
219
+ method: 'POST',
220
+ url: `${datachannelUrl}`,
221
+ body: {deviceUrl: webex.internal.device.url},
222
+ headers: {},
223
+ })
224
+ );
75
225
  });
76
226
  });
77
227
 
78
228
  describe('#getLocusUrl', () => {
79
229
  it('gets LocusUrl', async () => {
80
- llmService.register = sinon.stub().resolves({
81
- body: {
82
- binding: 'binding',
83
- webSocketUrl: 'url',
84
- },
230
+ llmService.register = sinon.stub().callsFake(async () => {
231
+ llmService.binding = 'binding';
232
+ llmService.webSocketUrl = 'wss://example.com/socket';
233
+ return {
234
+ body: {
235
+ binding: 'binding',
236
+ webSocketUrl: 'wss://example.com/socket',
237
+ },
238
+ };
85
239
  });
240
+
86
241
  await llmService.registerAndConnect(locusUrl, datachannelUrl);
87
242
  assert.equal(llmService.getLocusUrl(), locusUrl);
88
243
  });
@@ -90,11 +245,15 @@ describe('plugin-llm', () => {
90
245
 
91
246
  describe('#getDatachannelUrl', () => {
92
247
  it('gets dataChannel Url', async () => {
93
- llmService.register = sinon.stub().resolves({
94
- body: {
95
- binding: 'binding',
96
- webSocketUrl: 'url',
97
- },
248
+ llmService.register = sinon.stub().callsFake(async () => {
249
+ llmService.binding = 'binding';
250
+ llmService.webSocketUrl = 'wss://example.com/socket';
251
+ return {
252
+ body: {
253
+ binding: 'binding',
254
+ webSocketUrl: 'wss://example.com/socket',
255
+ },
256
+ };
98
257
  });
99
258
  await llmService.registerAndConnect(locusUrl, datachannelUrl);
100
259
  assert.equal(llmService.getDatachannelUrl(), datachannelUrl);
@@ -112,42 +271,443 @@ describe('plugin-llm', () => {
112
271
  });
113
272
  });
114
273
 
115
- describe('disconnectLLM', () => {
274
+ describe('#disconnectLLM', () => {
116
275
  let instance;
117
276
 
118
277
  beforeEach(() => {
119
278
  instance = {
120
279
  disconnect: jest.fn(() => Promise.resolve()),
121
- locusUrl: 'someUrl',
122
- datachannelUrl: 'someUrl',
123
- binding: {},
124
- webSocketUrl: 'someUrl',
125
- disconnectLLM: function (options) {
126
- return this.disconnect(options).then(() => {
127
- this.locusUrl = undefined;
128
- this.datachannelUrl = undefined;
129
- this.binding = undefined;
130
- this.webSocketUrl = undefined;
280
+ connections: new Map([
281
+ ['llm-default-session', { foo: 'bar', datachannelToken: 'session-token' }],
282
+ ]),
283
+
284
+ disconnectLLM: function (
285
+ options,
286
+ sessionId = 'llm-default-session',
287
+ ownerMeetingId = 'meeting-1'
288
+ ) {
289
+ if (!ownerMeetingId) {
290
+ return Promise.reject(new Error('ownerMeetingId is required'));
291
+ }
292
+
293
+ return this.disconnect(options, sessionId).then(() => {
294
+ this.connections.delete(sessionId);
131
295
  });
132
- }
296
+ },
133
297
  };
134
298
  });
135
299
 
136
- it('should call disconnect and clear relevant properties', async () => {
137
- await instance.disconnectLLM({});
300
+ it('calls disconnect and clears session connection (including token stored in session)', async () => {
301
+ await instance.disconnectLLM({ code: 3000, reason: 'bye' }, 'llm-default-session', 'meeting-1');
302
+
303
+ expect(instance.disconnect).toHaveBeenCalledWith(
304
+ { code: 3000, reason: 'bye' },
305
+ 'llm-default-session'
306
+ );
307
+
308
+ expect(instance.connections.has('llm-default-session')).toBe(false);
309
+ });
310
+
311
+ it('disconnectLLM supports legacy call with options only', async () => {
312
+ llmService.disconnect = sinon.stub().resolves(true);
313
+
314
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 'llm-default-session');
315
+
316
+ const options = {code: 1000, reason: 'legacy'};
317
+ const disconnected = await llmService.disconnectLLM(options);
318
+
319
+ assert.equal(disconnected, true);
320
+ sinon.assert.calledOnceWithExactly(llmService.disconnect, options, 'llm-default-session');
321
+ assert.equal(llmService.getAllConnections().has('llm-default-session'), false);
322
+ });
323
+
324
+ it('disconnectLLM supports legacy call with options and sessionId', async () => {
325
+ llmService.disconnect = sinon.stub().resolves(true);
326
+
327
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 's1');
328
+
329
+ const options = {code: 1000, reason: 'legacy'};
330
+ const disconnected = await llmService.disconnectLLM(options, 's1');
331
+
332
+ assert.equal(disconnected, true);
333
+ sinon.assert.calledOnceWithExactly(llmService.disconnect, options, 's1');
334
+ assert.equal(llmService.getAllConnections().has('s1'), false);
335
+ });
336
+
337
+ it('disconnectLLM treats null sessionId as default session', async () => {
338
+ llmService.disconnect = sinon.stub().resolves(true);
339
+
340
+ await llmService.registerAndConnect(
341
+ locusUrl,
342
+ datachannelUrl,
343
+ undefined,
344
+ 'llm-default-session'
345
+ );
346
+
347
+ const options = {code: 1000, reason: 'legacy-null-session'};
348
+ const disconnected = await llmService.disconnectLLM(options, null);
349
+
350
+ assert.equal(disconnected, true);
351
+ sinon.assert.calledOnceWithExactly(llmService.disconnect, options, 'llm-default-session');
352
+ assert.equal(llmService.getAllConnections().has('llm-default-session'), false);
353
+ });
354
+
355
+ it('propagates disconnect errors', async () => {
356
+ instance.disconnect.mockRejectedValue(new Error('disconnect failed'));
357
+
358
+ await expect(
359
+ instance.disconnectLLM({ code: 3000, reason: 'bye' }, 'llm-default-session', 'meeting-1')
360
+ ).rejects.toThrow('disconnect failed');
361
+ });
362
+ });
363
+
364
+ describe('#setRefreshHandler', () => {
365
+ beforeEach(() => {
366
+ llmService.setRefreshHandler = LLMChannel.prototype.setRefreshHandler.bind(llmService);
367
+ llmService.refreshDataChannelToken = LLMChannel.prototype.refreshDataChannelToken.bind(llmService);
368
+ });
369
+
370
+ it('defaults to llm-default-session when sessionId is omitted', () => {
371
+ const handler = sinon.stub().resolves({ body: { datachannelToken: 'legacyToken' } });
138
372
 
139
- expect(instance.disconnect).toHaveBeenCalledWith({});
140
- expect(instance.locusUrl).toBeUndefined();
141
- expect(instance.datachannelUrl).toBeUndefined();
142
- expect(instance.binding).toBeUndefined();
143
- expect(instance.webSocketUrl).toBeUndefined();
373
+ llmService.setRefreshHandler(handler);
374
+
375
+ return llmService.refreshDataChannelToken().then((result) => {
376
+ assert.equal(result.body.datachannelToken, 'legacyToken');
377
+ sinon.assert.calledOnce(handler);
378
+ });
379
+ });
380
+
381
+ it('stores the provided handler', () => {
382
+ const handler = sinon.stub().resolves({ body: { datachannelToken: 'newToken' } });
383
+ llmService.setRefreshHandler(handler, 'llm-default-session');
384
+
385
+ return llmService.refreshDataChannelToken().then((result) => {
386
+ assert.equal(result.body.datachannelToken, 'newToken');
387
+ sinon.assert.calledOnce(handler);
388
+ });
144
389
  });
145
390
 
146
- it('should handle errors from disconnect gracefully', async () => {
147
- instance.disconnect.mockRejectedValue(new Error('Disconnect failed'));
391
+ it('stores handlers per session id', () => {
392
+ const handlerS1 = sinon.stub().resolves({ body: { datachannelToken: 'token-s1' } });
393
+ const handlerS2 = sinon.stub().resolves({ body: { datachannelToken: 'token-s2' } });
148
394
 
149
- await expect(instance.disconnectLLM({})).rejects.toThrow('Disconnect failed');
395
+ llmService.setRefreshHandler(handlerS1, 's1');
396
+ llmService.setRefreshHandler(handlerS2, 's2');
397
+
398
+ return Promise.all([
399
+ llmService.refreshDataChannelToken('s1'),
400
+ llmService.refreshDataChannelToken('s2'),
401
+ ]).then(([resultS1, resultS2]) => {
402
+ assert.equal(resultS1.body.datachannelToken, 'token-s1');
403
+ assert.equal(resultS2.body.datachannelToken, 'token-s2');
404
+ });
150
405
  });
151
406
  });
407
+
408
+ describe('#isDataChannelTokenEnabled', () => {
409
+ it('works correctly', async () => {
410
+ webex.internal.feature.getFeature.returns(true);
411
+
412
+ const result = await llmService.isDataChannelTokenEnabled();
413
+
414
+ sinon.assert.calledOnceWithExactly(
415
+ webex.internal.feature.getFeature,
416
+ 'developer',
417
+ 'data-channel-with-jwt-token'
418
+ );
419
+
420
+ assert.equal(result, true);
421
+ });
422
+ });
423
+
424
+ describe('#refreshDataChannelToken', () => {
425
+ beforeEach(() => {
426
+ llmService.setRefreshHandler = LLMChannel.prototype.setRefreshHandler.bind(llmService);
427
+ llmService.refreshDataChannelToken = LLMChannel.prototype.refreshDataChannelToken.bind(llmService);
428
+ });
429
+
430
+ it('returns null and logs warn if no handler is set', async () => {
431
+ const warnSpy = llmService.logger.warn
432
+
433
+ const result = await llmService.refreshDataChannelToken();
434
+
435
+ assert.equal(result, null);
436
+
437
+ sinon.assert.calledOnce(warnSpy);
438
+ sinon.assert.calledWithMatch(
439
+ warnSpy,
440
+ sinon.match('LLM refreshHandler is not set for session')
441
+ );
442
+ });
443
+
444
+ it('returns token when handler resolves', async () => {
445
+ const mockToken = { body: { datachannelToken: 'newToken', isPracticeSession: false } };
446
+ const handler = sinon.stub().resolves(mockToken);
447
+
448
+ llmService.setRefreshHandler(handler, 'llm-default-session');
449
+
450
+ const token = await llmService.refreshDataChannelToken();
451
+
452
+ assert.equal(token, mockToken);
453
+ sinon.assert.calledOnce(handler);
454
+ });
455
+
456
+ it('uses the handler for the provided session id', async () => {
457
+ const mockToken = { body: { datachannelToken: 'session-token', isPracticeSession: true } };
458
+ const handler = sinon.stub().resolves(mockToken);
459
+
460
+ llmService.setRefreshHandler(handler, 's2');
461
+
462
+ const token = await llmService.refreshDataChannelToken('s2');
463
+
464
+ assert.equal(token, mockToken);
465
+ sinon.assert.calledOnce(handler);
466
+ });
467
+
468
+ it('logs warn and returns null when handler rejects', async () => {
469
+ const handler = sinon.stub().rejects(new Error('throw error'));
470
+ llmService.setRefreshHandler(handler, 'llm-default-session');
471
+
472
+ const warnSpy = llmService.logger.warn
473
+
474
+ const result = await llmService.refreshDataChannelToken();
475
+
476
+ assert.equal(result, null);
477
+
478
+ sinon.assert.calledOnce(warnSpy);
479
+ sinon.assert.calledWithMatch(
480
+ warnSpy,
481
+ sinon.match('DataChannel token refresh failed'),
482
+ );
483
+ });
484
+ });
485
+
486
+ describe('#getDatachannelToken / #setDatachannelToken', () => {
487
+ it('sets and gets datachannel token', () => {
488
+ llmService.setDatachannelToken('abc123','llm-default-session');
489
+ assert.equal(llmService.getDatachannelToken('llm-default-session'), 'abc123');
490
+ llmService.setDatachannelToken('123abc','llm-practice-session');
491
+ assert.equal(llmService.getDatachannelToken('llm-practice-session'), '123abc');
492
+ });
493
+
494
+ it('supports arbitrary session id token keys', () => {
495
+ llmService.setDatachannelToken('token-s1', 'session-custom-1');
496
+
497
+ assert.equal(llmService.getDatachannelToken('session-custom-1'), 'token-s1');
498
+ });
499
+
500
+ });
501
+
502
+ describe('#setOwnerMeetingId / #getOwnerMeetingId', () => {
503
+ it('stores and returns the owner meeting id for the default session', () => {
504
+ // beforeEach seeds connections with the default session entry
505
+ llmService.setOwnerMeetingId('meeting-1');
506
+
507
+ assert.equal(llmService.getOwnerMeetingId(), 'meeting-1');
508
+ });
509
+
510
+ it('returns undefined when no owner has been set yet', () => {
511
+ assert.equal(llmService.getOwnerMeetingId(), undefined);
512
+ });
513
+
514
+ it('is a no-op when there is no session data for the given sessionId', () => {
515
+ // Default session exists (seeded in beforeEach), but an arbitrary
516
+ // session id does not — setOwnerMeetingId must not create entries.
517
+ llmService.setOwnerMeetingId('meeting-1', 'unknown-session');
518
+
519
+ assert.equal(llmService.getOwnerMeetingId('unknown-session'), undefined);
520
+ });
521
+
522
+ it('allows clearing ownership by passing undefined', () => {
523
+ llmService.setOwnerMeetingId('meeting-1');
524
+ assert.equal(llmService.getOwnerMeetingId(), 'meeting-1');
525
+
526
+ llmService.setOwnerMeetingId(undefined);
527
+
528
+ assert.equal(llmService.getOwnerMeetingId(), undefined);
529
+ });
530
+
531
+ it('tracks ownership per session id', () => {
532
+ llmService.connections.set('session-A', {webSocketUrl: 'wss://a'});
533
+ llmService.connections.set('session-B', {webSocketUrl: 'wss://b'});
534
+
535
+ llmService.setOwnerMeetingId('meeting-A', 'session-A');
536
+ llmService.setOwnerMeetingId('meeting-B', 'session-B');
537
+
538
+ assert.equal(llmService.getOwnerMeetingId('session-A'), 'meeting-A');
539
+ assert.equal(llmService.getOwnerMeetingId('session-B'), 'meeting-B');
540
+ });
541
+
542
+ it('clears ownerMeetingId naturally when disconnectLLM deletes the session entry', async () => {
543
+ llmService.register = sinon.stub().callsFake(async () => ({
544
+ body: {binding: 'binding', webSocketUrl: 'wss://example.com/socket'},
545
+ }));
546
+
547
+ await llmService.registerAndConnect(locusUrl, datachannelUrl);
548
+ llmService.setOwnerMeetingId('meeting-1');
549
+ assert.equal(llmService.getOwnerMeetingId(), 'meeting-1');
550
+
551
+ await llmService.disconnectLLM(
552
+ {code: 3050, reason: 'done (permanent)'},
553
+ 'llm-default-session',
554
+ 'meeting-1'
555
+ );
556
+
557
+ // Session entry was deleted, so ownerMeetingId is gone.
558
+ assert.equal(llmService.getOwnerMeetingId(), undefined);
559
+ });
560
+ });
561
+
562
+ describe('multi-connection logic', () => {
563
+ const locusUrl2 = 'locusUrl2';
564
+ const datachannelUrl2 = 'datachannelUrl2';
565
+
566
+ it('tracks multiple sessions independently', async () => {
567
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 's1');
568
+ await llmService.registerAndConnect(locusUrl2, datachannelUrl2, undefined, 's2');
569
+
570
+ assert.equal(llmService.isConnected('s1'), true);
571
+ assert.equal(llmService.isConnected('s2'), true);
572
+ assert.equal(llmService.getLocusUrl('s1'), locusUrl);
573
+ assert.equal(llmService.getLocusUrl('s2'), locusUrl2);
574
+ assert.equal(llmService.getDatachannelUrl('s1'), datachannelUrl);
575
+ assert.equal(llmService.getDatachannelUrl('s2'), datachannelUrl2);
576
+
577
+ const all = llmService.getAllConnections();
578
+ assert.equal(all.has('s1'), true);
579
+ assert.equal(all.has('s2'), true);
580
+ });
581
+
582
+ it('disconnectLLM clears only the targeted session', async () => {
583
+ llmService.disconnect = sinon.stub().resolves(true);
584
+
585
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 's1');
586
+ await llmService.registerAndConnect(locusUrl2, datachannelUrl2, undefined, 's2');
587
+
588
+ const options = {code: 1000, reason: 'test'};
589
+ const disconnected = await llmService.disconnectLLM(options, 's1', 'meeting-1');
590
+
591
+ assert.equal(disconnected, true);
592
+ sinon.assert.calledOnceWithExactly(llmService.disconnect, options, 's1');
593
+
594
+ const all = llmService.getAllConnections();
595
+ assert.equal(all.has('s1'), false);
596
+ assert.equal(all.has('s2'), true);
597
+ assert.equal(llmService.getDatachannelToken('s1'), undefined);
598
+ });
599
+
600
+ it('disconnectLLM skips disconnect when ownerMeetingId does not match', async () => {
601
+ llmService.disconnect = sinon.stub().resolves(true);
602
+
603
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 's1');
604
+ llmService.setOwnerMeetingId('meeting-1', 's1');
605
+
606
+ const options = {code: 1000, reason: 'test'};
607
+ const disconnected = await llmService.disconnectLLM(options, 's1', 'meeting-2');
608
+
609
+ assert.equal(disconnected, false);
610
+ sinon.assert.notCalled(llmService.disconnect);
611
+ assert.equal(llmService.getAllConnections().has('s1'), true);
612
+ });
613
+
614
+ it('disconnectAllLLM clears all sessions', async () => {
615
+ llmService.disconnectAll = sinon.stub().resolves(true);
616
+
617
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 's1');
618
+ await llmService.registerAndConnect(locusUrl2, datachannelUrl2, undefined, 's2');
619
+
620
+ await llmService.disconnectAllLLM({code: 1000, reason: 'all'});
621
+
622
+ sinon.assert.calledOnce(llmService.disconnectAll);
623
+ assert.equal(llmService.getAllConnections().size, 0);
624
+ });
625
+ });
626
+
627
+ describe('#getLocusUrlByDatachannelUrl', () => {
628
+ const locusUrl2 = 'https://locus-b.wbx2.com/locus/api/v1/loci/456';
629
+ const datachannelUrl2 = 'https://board-b.wbx2.com/datachannel/api/v1/locus/ps-encoded/registrations';
630
+
631
+ // Ampersand State.extend() does not always propagate prototype methods
632
+ // added to child classes, so we bind the method from the class prototype
633
+ // directly onto the test instance.
634
+ beforeEach(() => {
635
+ llmService.getLocusUrlByDatachannelUrl = LLMChannel.prototype.getLocusUrlByDatachannelUrl.bind(llmService);
636
+ });
637
+
638
+ it('returns locusUrl when request URL matches a session datachannelUrl', async () => {
639
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 's1');
640
+ await llmService.registerAndConnect(locusUrl2, datachannelUrl2, undefined, 's2');
641
+
642
+ const result = llmService.getLocusUrlByDatachannelUrl(datachannelUrl2 + '/some-path');
643
+
644
+ assert.equal(result, locusUrl2);
645
+ });
646
+
647
+ it('returns undefined when no session matches the request URL', async () => {
648
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 's1');
649
+
650
+ const result = llmService.getLocusUrlByDatachannelUrl('https://unknown.example.com/path');
651
+
652
+ assert.equal(result, undefined);
653
+ });
654
+
655
+ it('matches locusUrl when request host is rewritten but pathname matches', async () => {
656
+ await llmService.registerAndConnect(locusUrl2, datachannelUrl2, undefined, 's2');
657
+
658
+ const rewrittenHostRequestUrl =
659
+ 'https://hostmap-rewritten.example.com/datachannel/api/v1/locus/ps-encoded/registrations/events';
660
+ const result = llmService.getLocusUrlByDatachannelUrl(rewrittenHostRequestUrl);
661
+
662
+ assert.equal(result, locusUrl2);
663
+ });
664
+
665
+ it('returns undefined when no connections exist', () => {
666
+ const result = llmService.getLocusUrlByDatachannelUrl(datachannelUrl);
667
+
668
+ assert.equal(result, undefined);
669
+ });
670
+ });
671
+
672
+ describe('#getSessionIdByDatachannelUrl', () => {
673
+ const datachannelUrl2 = 'https://board-b.wbx2.com/datachannel/api/v1/locus/ps-encoded/registrations';
674
+
675
+ beforeEach(() => {
676
+ llmService.getSessionIdByDatachannelUrl = LLMChannel.prototype.getSessionIdByDatachannelUrl.bind(llmService);
677
+ });
678
+
679
+ it('returns sessionId when request URL matches a session datachannelUrl', async () => {
680
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 's1');
681
+ await llmService.registerAndConnect('https://locus-b.wbx2.com/locus/api/v1/loci/456', datachannelUrl2, undefined, 's2');
682
+
683
+ const result = llmService.getSessionIdByDatachannelUrl(datachannelUrl2 + '/some-path');
684
+
685
+ assert.equal(result, 's2');
686
+ });
687
+
688
+ it('returns undefined when no session matches the request URL', async () => {
689
+ await llmService.registerAndConnect(locusUrl, datachannelUrl, undefined, 's1');
690
+
691
+ const result = llmService.getSessionIdByDatachannelUrl('https://unknown.example.com/path');
692
+
693
+ assert.equal(result, undefined);
694
+ });
695
+
696
+ it('matches sessionId when request host is rewritten but pathname matches', async () => {
697
+ await llmService.registerAndConnect(
698
+ 'https://locus-b.wbx2.com/locus/api/v1/loci/456',
699
+ datachannelUrl2,
700
+ undefined,
701
+ 's2'
702
+ );
703
+
704
+ const rewrittenHostRequestUrl =
705
+ 'https://hostmap-rewritten.example.com/datachannel/api/v1/locus/ps-encoded/registrations/messages';
706
+ const result = llmService.getSessionIdByDatachannelUrl(rewrittenHostRequestUrl);
707
+
708
+ assert.equal(result, 's2');
709
+ });
710
+ });
711
+
152
712
  });
153
713
  });