-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefs.test.ts
More file actions
814 lines (696 loc) · 29 KB
/
Copy pathrefs.test.ts
File metadata and controls
814 lines (696 loc) · 29 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
import { beforeEach, describe, expect, it, vi } from 'vitest'
const apiMocks = vi.hoisted(() => ({
getCommsClient: vi.fn(),
fetchWorkspaces: vi.fn(),
getWorkspaceUsers: vi.fn(),
getWorkspaceGroups: vi.fn(),
getGroup: vi.fn(),
}))
vi.mock('./api.js', () => ({
getCommsClient: apiMocks.getCommsClient,
fetchWorkspaces: apiMocks.fetchWorkspaces,
getWorkspaceUsers: apiMocks.getWorkspaceUsers,
getWorkspaceGroups: apiMocks.getWorkspaceGroups,
getGroup: apiMocks.getGroup,
}))
import {
classifyCommsUrl,
extractId,
getDirectChannelId,
isIdRef,
looksLikeRawId,
parseCommsUrl,
parseNumericIdRefs,
parseRef,
partitionNotifyIds,
resolveChannelId,
resolveChannelMemberRefs,
resolveChannelRef,
resolveCommentId,
resolveConversationId,
resolveGroupRef,
resolveMessageId,
resolveThreadId,
resolveUserRefs,
} from './refs.js'
describe('isIdRef', () => {
it('returns true for id: prefixed strings', () => {
expect(isIdRef('id:123')).toBe(true)
expect(isIdRef('id:abc123')).toBe(true)
})
it('returns false for non-id refs', () => {
expect(isIdRef('123')).toBe(false)
expect(isIdRef('workspace-name')).toBe(false)
expect(isIdRef('https://comms.todoist.com/a/123')).toBe(false)
})
})
describe('extractId', () => {
it('extracts ID from id: prefix', () => {
expect(extractId('id:123')).toBe('123')
expect(extractId('id:abc123')).toBe('abc123')
})
it('returns bare strings as ID', () => {
expect(extractId('123')).toBe('123')
expect(extractId('7YpL3oZ4kZ9vP7Q1tR2sX3z')).toBe('7YpL3oZ4kZ9vP7Q1tR2sX3z')
})
it('trims whitespace', () => {
expect(extractId(' 123 ')).toBe('123')
expect(extractId('id: 456')).toBe('456')
})
it('throws on empty input', () => {
expect(() => extractId('')).toThrow('Invalid ID')
expect(() => extractId('id:')).toThrow('Invalid ID')
})
})
describe('parseNumericIdRefs', () => {
it('parses comma-separated numeric refs', () => {
expect(parseNumericIdRefs('id:10, 20', 'user')).toEqual([10, 20])
})
it('returns null when any ref needs fuzzy resolution', () => {
expect(parseNumericIdRefs('id:10,alice@doist.com', 'user')).toBeNull()
})
it('rejects empty values', () => {
expect(() => parseNumericIdRefs('id:10,,20', 'user')).toThrow('Invalid user reference list')
})
})
describe('looksLikeRawId', () => {
it('detects numeric strings', () => {
expect(looksLikeRawId('123456')).toBe(true)
})
it('detects base58 alphanumeric strings', () => {
expect(looksLikeRawId('7YpL3oZ4kZ9vP7Q1tR2sX3z')).toBe(true)
})
it('rejects plain names and spaces', () => {
expect(looksLikeRawId('workspace')).toBe(false)
expect(looksLikeRawId('CbjxNkWHJBwcaVkoTCRgM')).toBe(false)
expect(looksLikeRawId('CustomerSuccessLeadership')).toBe(false)
expect(looksLikeRawId('workspace one')).toBe(false)
})
})
describe('parseCommsUrl', () => {
it('parses workspace URL', () => {
const result = parseCommsUrl('https://comms.todoist.com/a/12345')
expect(result).toEqual({ workspaceId: 12345 })
})
it('parses short workspace URL', () => {
const result = parseCommsUrl('https://comms.todoist.com/12345')
expect(result).toEqual({ workspaceId: 12345 })
})
it('parses channel URL with base58 id', () => {
const result = parseCommsUrl('https://comms.todoist.com/a/12345/ch/7YpL3oZ4kZ9vP7Q1tR2sX3z')
expect(result).toEqual({ workspaceId: 12345, channelId: '7YpL3oZ4kZ9vP7Q1tR2sX3z' })
})
it('parses short channel URL with base58 id', () => {
const result = parseCommsUrl('https://comms.todoist.com/12345/ch/7YpL3oZ4kZ9vP7Q1tR2sX3z')
expect(result).toEqual({ workspaceId: 12345, channelId: '7YpL3oZ4kZ9vP7Q1tR2sX3z' })
})
it.each([
[
'short thread URL',
'https://comms.todoist.com/12345/ch/CH1/t/TH1',
{ workspaceId: 12345, channelId: 'CH1', threadId: 'TH1' },
],
[
'thread URL',
'https://comms.todoist.com/a/12345/ch/CH1/t/TH1',
{ workspaceId: 12345, channelId: 'CH1', threadId: 'TH1' },
],
[
'short thread with comment URL',
'https://comms.todoist.com/12345/ch/CH1/t/TH1/c/CM1',
{ workspaceId: 12345, channelId: 'CH1', threadId: 'TH1', commentId: 'CM1' },
],
[
'thread with comment URL',
'https://comms.todoist.com/a/12345/ch/CH1/t/TH1/c/CM1',
{ workspaceId: 12345, channelId: 'CH1', threadId: 'TH1', commentId: 'CM1' },
],
[
'people URL as workspace-only',
'https://comms.todoist.com/12345/people/u/678',
{ workspaceId: 12345 },
],
])('parses %s', (_description, url, expected) => {
expect(parseCommsUrl(url)).toEqual(expected)
})
it.each([
[
'inbox thread URL',
'https://comms.todoist.com/12345/inbox/t/TH1/',
{ workspaceId: 12345, threadId: 'TH1' },
],
[
'inbox thread with comment URL',
'https://comms.todoist.com/12345/inbox/t/TH1/c/CM1',
{ workspaceId: 12345, threadId: 'TH1', commentId: 'CM1' },
],
[
'saved thread URL',
'https://comms.todoist.com/12345/saved/t/TH1',
{ workspaceId: 12345, threadId: 'TH1' },
],
[
'saved thread with comment URL',
'https://comms.todoist.com/12345/saved/t/TH1/c/CM1',
{ workspaceId: 12345, threadId: 'TH1', commentId: 'CM1' },
],
])('parses %s', (_description, url, expected) => {
expect(parseCommsUrl(url)).toEqual(expected)
})
it.each([
['inbox root URL', 'https://comms.todoist.com/12345/inbox'],
['inbox done URL', 'https://comms.todoist.com/12345/inbox/done'],
['inbox done thread-like URL', 'https://comms.todoist.com/12345/inbox/done/t/TH1'],
['missing thread id', 'https://comms.todoist.com/12345/inbox/t'],
['missing comment id', 'https://comms.todoist.com/12345/inbox/t/TH1/c'],
['comment-only path', 'https://comms.todoist.com/12345/inbox/c/CM1'],
['wrong marker after thread id', 'https://comms.todoist.com/12345/inbox/t/TH1/x/CM1'],
['extra segment after thread id', 'https://comms.todoist.com/12345/inbox/t/TH1/extra'],
[
'extra segment after comment id',
'https://comms.todoist.com/12345/inbox/t/TH1/c/CM1/extra',
],
['msg suffix after thread id', 'https://comms.todoist.com/12345/inbox/t/TH1/msg/CV1'],
['saved URL with extra segment', 'https://comms.todoist.com/12345/saved/t/TH1/extra'],
])('leaves %s workspace-only', (_description, url) => {
expect(parseCommsUrl(url)).toEqual({ workspaceId: 12345 })
})
it('parses conversation URL', () => {
const result = parseCommsUrl('https://comms.todoist.com/a/12345/msg/CV1')
expect(result).toEqual({ workspaceId: 12345, conversationId: 'CV1' })
})
it('parses short conversation URL', () => {
const result = parseCommsUrl('https://comms.todoist.com/12345/msg/CV1')
expect(result).toEqual({ workspaceId: 12345, conversationId: 'CV1' })
})
it('parses message URL', () => {
const result = parseCommsUrl('https://comms.todoist.com/a/12345/msg/CV1/m/MS1')
expect(result).toEqual({ workspaceId: 12345, conversationId: 'CV1', messageId: 'MS1' })
})
it('parses short message URL', () => {
const result = parseCommsUrl('https://comms.todoist.com/12345/msg/CV1/m/MS1')
expect(result).toEqual({ workspaceId: 12345, conversationId: 'CV1', messageId: 'MS1' })
})
it('parses staging URLs', () => {
const result = parseCommsUrl('https://comms.staging.todoist.com/12345/msg/CV1')
expect(result).toEqual({ workspaceId: 12345, conversationId: 'CV1' })
})
it('parses local URLs', () => {
const result = parseCommsUrl('https://comms.local.todoist.com/12345/msg/CV1')
expect(result).toEqual({ workspaceId: 12345, conversationId: 'CV1' })
})
it('returns null for non-Comms URLs', () => {
expect(parseCommsUrl('https://google.com')).toBeNull()
expect(parseCommsUrl('https://example.com/a/123')).toBeNull()
})
it('returns null for unsupported protocols', () => {
expect(parseCommsUrl('ftp://comms.todoist.com/12345/msg/CV1')).toBeNull()
})
it('returns null for invalid URLs', () => {
expect(parseCommsUrl('not-a-url')).toBeNull()
})
})
describe('parseRef', () => {
it('parses id: refs', () => {
expect(parseRef('id:123')).toEqual({ type: 'id', id: '123' })
})
it('parses bare numbers', () => {
expect(parseRef('456')).toEqual({ type: 'id', id: '456' })
})
it('parses base58 IDs', () => {
expect(parseRef('7YpL3oZ4kZ9vP7Q1tR2sX3z')).toEqual({
type: 'id',
id: '7YpL3oZ4kZ9vP7Q1tR2sX3z',
})
})
it('keeps all-letter generated IDs out of global name parsing', () => {
expect(parseRef('CbjxNkWHJBwcaVkoTCRgM')).toEqual({
type: 'name',
name: 'CbjxNkWHJBwcaVkoTCRgM',
})
})
it('parses URLs', () => {
const result = parseRef('https://comms.todoist.com/a/12345/ch/CH1/t/TH1')
expect(result).toEqual({
type: 'url',
parsed: { workspaceId: 12345, channelId: 'CH1', threadId: 'TH1' },
})
})
it('parses names', () => {
expect(parseRef('My Workspace')).toEqual({ type: 'name', name: 'My Workspace' })
})
it('trims surrounding whitespace', () => {
expect(parseRef(' 456 ')).toEqual({ type: 'id', id: '456' })
expect(parseRef(' My Workspace ')).toEqual({ type: 'name', name: 'My Workspace' })
})
})
describe('getDirectChannelId', () => {
it('returns ids and channel URL ids', () => {
expect(getDirectChannelId('id:CH1')).toBe('CH1')
expect(getDirectChannelId('CH1')).toBe('CH1')
expect(getDirectChannelId('https://comms.todoist.com/a/12345/ch/CH1/t/TH1')).toBe('CH1')
})
it('returns null for fuzzy names', () => {
expect(getDirectChannelId('Engineering')).toBeNull()
})
it('rejects URLs that do not identify a channel', () => {
expect(() => getDirectChannelId('https://comms.todoist.com/a/12345/msg/CV1')).toThrow(
'Invalid channel reference',
)
})
})
describe('resolveThreadId', () => {
it('resolves id: refs', () => {
expect(resolveThreadId('id:TH1')).toBe('TH1')
})
it('resolves base58 ids', () => {
expect(resolveThreadId('7YpL3oZ4kZ9vP7Q1tR2sX3z')).toBe('7YpL3oZ4kZ9vP7Q1tR2sX3z')
})
it('resolves generated Comms IDs without digits', () => {
expect(resolveThreadId('CbjxNkWHJBwcaVkoTCRgM')).toBe('CbjxNkWHJBwcaVkoTCRgM')
})
it.each([
['thread URL', 'https://comms.todoist.com/a/12345/ch/CH1/t/TH1'],
['thread URL with comment suffix', 'https://comms.todoist.com/a/12345/ch/CH1/t/TH1/c/CM1'],
['inbox thread URL', 'https://comms.todoist.com/12345/inbox/t/TH1/'],
[
'inbox thread URL with comment suffix',
'https://comms.todoist.com/12345/inbox/t/TH1/c/CM1',
],
['saved thread URL', 'https://comms.todoist.com/12345/saved/t/TH1'],
[
'saved thread URL with comment suffix',
'https://comms.todoist.com/12345/saved/t/TH1/c/CM1',
],
])('resolves %s', (_description, url) => {
expect(resolveThreadId(url)).toBe('TH1')
})
it('throws on invalid refs', () => {
expect(() => resolveThreadId('invalid name')).toThrow('Invalid thread reference')
})
})
describe('resolveCommentId', () => {
it('resolves id: refs', () => {
expect(resolveCommentId('id:CM1')).toBe('CM1')
})
it('resolves comment URLs', () => {
expect(resolveCommentId('https://comms.todoist.com/a/12345/ch/CH1/t/TH1/c/CM1')).toBe('CM1')
})
})
describe('resolveChannelId', () => {
it('resolves id: refs', () => {
expect(resolveChannelId('id:CH1')).toBe('CH1')
})
it('resolves channel URLs', () => {
expect(resolveChannelId('https://comms.todoist.com/a/12345/ch/CH1')).toBe('CH1')
})
})
describe('resolveChannelRef', () => {
function createChannel(id: string, name: string, overrides: Record<string, unknown> = {}) {
return {
id,
name,
public: true,
workspaceId: 1,
archived: false,
creator: 1,
created: new Date('2026-01-01T00:00:00Z'),
version: 1,
...overrides,
}
}
const mockGetChannel = vi.fn()
const mockGetChannels = vi.fn()
const mockGetPublicChannels = vi.fn()
/**
* For name refs, resolveChannelRef merges joined channels (getChannels — membership-scoped,
* includes both active + archived) with public channels (getPublicChannels — workspace-scoped,
* finds unjoined-but-public channels). Tests default both to empty unless overridden.
*/
function mockChannelLists(joined: unknown[] = [], publicChannels: unknown[] = []) {
mockGetChannels.mockResolvedValue(joined)
mockGetPublicChannels.mockResolvedValue(publicChannels)
}
beforeEach(() => {
vi.clearAllMocks()
apiMocks.getCommsClient.mockResolvedValue({
channels: {
getChannel: mockGetChannel,
getChannels: mockGetChannels,
},
workspaces: {
getPublicChannels: mockGetPublicChannels,
},
})
})
it('fetches channel by id: ref via getChannel', async () => {
const ch = createChannel('CH1', 'engineering')
mockGetChannel.mockResolvedValue(ch)
const result = await resolveChannelRef('id:CH1', 1)
expect(mockGetChannel).toHaveBeenCalledWith('CH1')
expect(mockGetChannels).not.toHaveBeenCalled()
expect(result).toEqual(ch)
})
it('fetches channel by Comms URL via getChannel', async () => {
const ch = createChannel('CH1', 'engineering')
mockGetChannel.mockResolvedValue(ch)
const result = await resolveChannelRef('https://comms.todoist.com/a/1/ch/CH1', 1)
expect(mockGetChannel).toHaveBeenCalledWith('CH1')
expect(result).toEqual(ch)
})
it('throws CHANNEL_NOT_FOUND when id: ref resolves to a channel in another workspace', async () => {
mockGetChannel.mockResolvedValue(createChannel('CH1', 'engineering', { workspaceId: 2 }))
await expect(resolveChannelRef('id:CH1', 1)).rejects.toHaveProperty(
'code',
'CHANNEL_NOT_FOUND',
)
})
it('throws CHANNEL_NOT_FOUND when URL workspaceId conflicts with expected workspaceId', async () => {
await expect(
resolveChannelRef('https://comms.todoist.com/a/2/ch/CH1', 1),
).rejects.toHaveProperty('code', 'CHANNEL_NOT_FOUND')
expect(mockGetChannel).not.toHaveBeenCalled()
})
it('resolves exact case-insensitive name match against joined channels without fetching public list', async () => {
const ch = createChannel('CHGEN', 'General')
mockChannelLists([ch, createChannel('CHLEAD', 'Leadership')])
const result = await resolveChannelRef('general', 1)
expect(result).toEqual(ch)
// Common case: exact match in joined list short-circuits before the
// workspace-wide getPublicChannels call.
expect(mockGetPublicChannels).not.toHaveBeenCalled()
})
it('resolves unique substring name match', async () => {
const ch = createChannel('CHMKT', 'Marketing')
mockChannelLists([createChannel('CHGEN', 'General'), ch])
const result = await resolveChannelRef('market', 1)
expect(result).toEqual(ch)
})
it('throws AMBIGUOUS_CHANNEL on multiple substring matches', async () => {
mockChannelLists([
createChannel('CHENG', 'Engineering'),
createChannel('CHEOP', 'Engineering-Ops'),
])
await expect(resolveChannelRef('eng', 1)).rejects.toHaveProperty(
'code',
'AMBIGUOUS_CHANNEL',
)
})
it('throws CHANNEL_NOT_FOUND when no match', async () => {
mockChannelLists([createChannel('CHGEN', 'General')])
await expect(resolveChannelRef('nope', 1)).rejects.toHaveProperty(
'code',
'CHANNEL_NOT_FOUND',
)
})
it('resolves unjoined-but-public channel by name', async () => {
const publicCh = createChannel('CHPUB1', 'Old Public Channel')
mockChannelLists([createChannel('CHGEN', 'General')], [publicCh])
const result = await resolveChannelRef('Old Public Channel', 1)
expect(result).toEqual(publicCh)
})
it('resolves unjoined-but-public channel by substring', async () => {
const publicCh = createChannel('CHSMOKE', 'tw-cli-smoke-test-channel')
mockChannelLists([createChannel('CHGEN', 'General')], [publicCh])
const result = await resolveChannelRef('smoke-test', 1)
expect(result).toEqual(publicCh)
})
it('deduplicates channels appearing in both joined and public lists', async () => {
// A public channel the user has joined appears in both responses. Use distinct
// object instances with the same id — that's what two API calls actually return,
// and it ensures dedupe is by id (not reference equality). A substring query
// exercises the dedupe step: without it, matchByName sees two partial matches
// for the same channel id and throws AMBIGUOUS_CHANNEL. An exact-match query
// wouldn't catch a regression because matchByName returns on the first .find.
const joinedCopy = createChannel('CHJP', 'Engineering', { public: true })
const publicCopy = createChannel('CHJP', 'Engineering', { public: true })
mockChannelLists([joinedCopy], [publicCopy])
const result = await resolveChannelRef('eng', 1)
expect(result).toEqual(joinedCopy)
})
it('throws AMBIGUOUS_CHANNEL on substring matches spanning joined and public lists', async () => {
mockChannelLists(
[createChannel('CHENG', 'Engineering')],
[createChannel('CHEOP', 'Engineering-Ops')],
)
await expect(resolveChannelRef('eng', 1)).rejects.toHaveProperty(
'code',
'AMBIGUOUS_CHANNEL',
)
})
})
describe('resolveConversationId', () => {
it('resolves id: refs', () => {
expect(resolveConversationId('id:CV1')).toBe('CV1')
})
it('resolves conversation URLs', () => {
expect(resolveConversationId('https://comms.todoist.com/a/12345/msg/CV1')).toBe('CV1')
})
})
describe('resolveMessageId', () => {
it('resolves id: refs', () => {
expect(resolveMessageId('id:MS1')).toBe('MS1')
})
it('resolves message URLs', () => {
expect(resolveMessageId('https://comms.todoist.com/a/12345/msg/CV1/m/MS1')).toBe('MS1')
})
})
describe('partitionNotifyIds', () => {
it('separates user IDs from group IDs', () => {
const groupIds = new Set(['GR1', 'GR2'])
const result = partitionNotifyIds(['1', 'GR1', '2', 'GR2', '3'], groupIds)
expect(result.userIds).toEqual([1, 2, 3])
expect(result.groupIds).toEqual(['GR1', 'GR2'])
})
it('returns all as users when no groups match', () => {
const groupIds = new Set<string>(['GR99'])
const result = partitionNotifyIds(['1', '2', '3'], groupIds)
expect(result.userIds).toEqual([1, 2, 3])
expect(result.groupIds).toEqual([])
})
it('returns all as groups when all match', () => {
const groupIds = new Set(['GR1', 'GR2', 'GR3'])
const result = partitionNotifyIds(['GR1', 'GR2', 'GR3'], groupIds)
expect(result.userIds).toEqual([])
expect(result.groupIds).toEqual(['GR1', 'GR2', 'GR3'])
})
it('handles empty input', () => {
const result = partitionNotifyIds([], new Set(['GR1']))
expect(result.userIds).toEqual([])
expect(result.groupIds).toEqual([])
})
it('rejects non-numeric, non-group ids as malformed user refs', () => {
const groupIds = new Set(['GR1'])
expect(() => partitionNotifyIds(['abcxyz'], groupIds)).toThrow('Invalid notify ID')
})
})
describe('classifyCommsUrl', () => {
it.each([
['thread URL', 'https://comms.todoist.com/a/20/ch/CH1/t/TH1', 'thread'],
['thread+comment URL', 'https://comms.todoist.com/a/20/ch/CH1/t/TH1/c/CM1', 'comment'],
['inbox thread URL', 'https://comms.todoist.com/20/inbox/t/TH1/', 'thread'],
['inbox thread+comment URL', 'https://comms.todoist.com/20/inbox/t/TH1/c/CM1', 'comment'],
['saved thread URL', 'https://comms.todoist.com/20/saved/t/TH1', 'thread'],
['saved thread+comment URL', 'https://comms.todoist.com/20/saved/t/TH1/c/CM1', 'comment'],
['conversation URL', 'https://comms.todoist.com/a/20/msg/CV1', 'conversation'],
['short conversation URL', 'https://comms.todoist.com/20/msg/CV1', 'conversation'],
['message URL', 'https://comms.todoist.com/a/20/msg/CV1/m/MS1', 'message'],
] as const)('classifies %s', (_description, url, entityType) => {
expect(classifyCommsUrl(url)).toEqual({ entityType, url })
})
it.each([
['inbox root URL', 'https://comms.todoist.com/20/inbox'],
['inbox done URL', 'https://comms.todoist.com/20/inbox/done'],
['inbox done thread-like URL', 'https://comms.todoist.com/20/inbox/done/t/TH1'],
['inbox thread with extra segment', 'https://comms.todoist.com/20/inbox/t/TH1/extra'],
['inbox thread with msg suffix', 'https://comms.todoist.com/20/inbox/t/TH1/msg/CV1'],
['saved thread with extra segment', 'https://comms.todoist.com/20/saved/t/TH1/extra'],
['workspace-only URL', 'https://comms.todoist.com/a/20'],
['channel-only URL', 'https://comms.todoist.com/a/20/ch/CH1'],
['malformed account URL', 'https://comms.todoist.com/a/ch/CH1/t/TH1'],
['non-Comms URL', 'https://google.com/a/20/t/200'],
['invalid string', 'not-a-url'],
])('returns null for %s', (_description, url) => {
expect(classifyCommsUrl(url)).toBeNull()
})
})
describe('resolveGroupRef', () => {
const sampleGroups = [
{
id: 'GR100',
name: 'Frontend',
workspaceId: 1,
userIds: [1, 2],
description: '',
version: 1,
},
{
id: 'GR200',
name: 'Backend',
workspaceId: 1,
userIds: [3],
description: '',
version: 1,
},
{
id: 'GR300',
name: 'Frontend Leads',
workspaceId: 1,
userIds: [1],
description: '',
version: 1,
},
]
beforeEach(() => {
vi.clearAllMocks()
apiMocks.getWorkspaceGroups.mockResolvedValue(sampleGroups)
apiMocks.getGroup.mockImplementation(async (id: string) => {
const group = sampleGroups.find((g) => g.id === id)
if (!group) throw new Error(`Group ${id} not found`)
return group
})
})
it('resolves by raw ID via getGroup', async () => {
const group = await resolveGroupRef('GR100', 1)
expect(group.id).toBe('GR100')
expect(group.name).toBe('Frontend')
expect(apiMocks.getGroup).toHaveBeenCalledWith('GR100', 1)
expect(apiMocks.getWorkspaceGroups).not.toHaveBeenCalled()
})
it('resolves by id: prefix via getGroup', async () => {
const group = await resolveGroupRef('id:GR200', 1)
expect(group.id).toBe('GR200')
expect(apiMocks.getGroup).toHaveBeenCalledWith('GR200', 1)
})
it('throws GROUP_NOT_FOUND for missing ID', async () => {
apiMocks.getGroup.mockRejectedValue(new Error('Not found'))
await expect(resolveGroupRef('id:GR999', 1)).rejects.toMatchObject({
code: 'GROUP_NOT_FOUND',
})
})
it('throws GROUP_NOT_FOUND when group belongs to different workspace', async () => {
apiMocks.getGroup.mockResolvedValue({ ...sampleGroups[0], workspaceId: 999 })
await expect(resolveGroupRef('id:GR100', 1)).rejects.toMatchObject({
code: 'GROUP_NOT_FOUND',
})
})
it('resolves by exact name (case-insensitive)', async () => {
const group = await resolveGroupRef('frontend', 1)
expect(group.id).toBe('GR100')
})
it('resolves by unique name substring', async () => {
const group = await resolveGroupRef('Back', 1)
expect(group.id).toBe('GR200')
})
it('throws AMBIGUOUS_GROUP when name matches multiple groups', async () => {
await expect(resolveGroupRef('Front', 1)).rejects.toMatchObject({
code: 'AMBIGUOUS_GROUP',
})
})
it('throws GROUP_NOT_FOUND when name matches nothing', async () => {
await expect(resolveGroupRef('Marketing', 1)).rejects.toMatchObject({
code: 'GROUP_NOT_FOUND',
})
})
})
describe('resolveUserRefs', () => {
const sampleUsers = [
{ id: 1, fullName: 'Alice Smith', email: 'alice@doist.com' },
{ id: 2, fullName: 'Bob Jones', email: 'bob@doist.com' },
{ id: 3, fullName: 'Carol Smith', email: 'carol@doist.com' },
]
beforeEach(() => {
vi.clearAllMocks()
apiMocks.getWorkspaceUsers.mockResolvedValue(sampleUsers)
})
it('resolves a single id: ref', async () => {
const ids = await resolveUserRefs('id:42', 1)
expect(ids).toEqual([42])
expect(apiMocks.getWorkspaceUsers).not.toHaveBeenCalled()
})
it('resolves comma-separated mixed refs', async () => {
const ids = await resolveUserRefs('id:1, bob@doist.com', 1)
expect(ids).toEqual([1, 2])
})
it('throws AMBIGUOUS_USER when name matches multiple', async () => {
await expect(resolveUserRefs('Smith', 1)).rejects.toMatchObject({
code: 'AMBIGUOUS_USER',
})
})
it('throws USER_NOT_FOUND for unknown name', async () => {
await expect(resolveUserRefs('nobody', 1)).rejects.toMatchObject({
code: 'USER_NOT_FOUND',
})
})
})
describe('resolveChannelMemberRefs', () => {
const sampleGroups = [
{
id: 'GR100',
name: 'Frontend',
workspaceId: 1,
userIds: [1, 2],
description: '',
version: 1,
},
{
id: 'GR200',
name: 'Backend',
workspaceId: 1,
userIds: [3, 4],
description: '',
version: 1,
},
]
const sampleUsers = [
{ id: 1, fullName: 'Alice', email: 'alice@doist.com' },
{ id: 2, fullName: 'Bob', email: 'bob@doist.com' },
{ id: 3, fullName: 'Carol', email: 'carol@doist.com' },
]
beforeEach(() => {
vi.clearAllMocks()
apiMocks.getWorkspaceUsers.mockResolvedValue(sampleUsers)
apiMocks.getWorkspaceGroups.mockResolvedValue(sampleGroups)
apiMocks.getGroup.mockImplementation(async (id: string) => {
const group = sampleGroups.find((g) => g.id === id)
if (!group) throw new Error(`Group ${id} not found`)
return group
})
})
it('throws MISSING_USERS for an empty ref list', async () => {
await expect(resolveChannelMemberRefs([], 1)).rejects.toMatchObject({
code: 'MISSING_USERS',
})
})
it('preserves input order and dedupes across users and group expansion', async () => {
const { userIds, expandedFrom } = await resolveChannelMemberRefs(
['id:3', 'group:Frontend', 'id:1'],
1,
)
// 3 first, then group expands to 1,2 (3 already seen stays put), 1 already seen
expect(userIds).toEqual([3, 1, 2])
expect(expandedFrom).toEqual([{ groupId: 'GR100', groupName: 'Frontend', userIds: [1, 2] }])
})
it('accepts a case-insensitive group: prefix', async () => {
const { userIds, expandedFrom } = await resolveChannelMemberRefs(['GROUP:Frontend'], 1)
expect(userIds).toEqual([1, 2])
expect(expandedFrom).toHaveLength(1)
})
it('preserves order across interleaved users and multiple groups', async () => {
const { userIds, expandedFrom } = await resolveChannelMemberRefs(
['id:5', 'group:Frontend', 'id:1', 'group:Backend'],
1,
)
// 5, then Frontend → 1,2 (1 dedup'd later), then Backend → 3,4
expect(userIds).toEqual([5, 1, 2, 3, 4])
expect(expandedFrom).toEqual([
{ groupId: 'GR100', groupName: 'Frontend', userIds: [1, 2] },
{ groupId: 'GR200', groupName: 'Backend', userIds: [3, 4] },
])
})
it('rejects an empty group: reference', async () => {
await expect(resolveChannelMemberRefs(['group:'], 1)).rejects.toMatchObject({
code: 'INVALID_REF',
})
})
})