@vibescope/mcp-server 0.2.9 → 0.3.1

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 (95) hide show
  1. package/CHANGELOG.md +84 -84
  2. package/README.md +194 -194
  3. package/dist/api-client.d.ts +36 -0
  4. package/dist/api-client.js +34 -0
  5. package/dist/cli.d.ts +1 -1
  6. package/dist/cli.js +30 -38
  7. package/dist/handlers/discovery.js +2 -0
  8. package/dist/handlers/session.d.ts +11 -0
  9. package/dist/handlers/session.js +101 -0
  10. package/dist/handlers/tasks.d.ts +8 -0
  11. package/dist/handlers/tasks.js +163 -3
  12. package/dist/handlers/tool-docs.js +840 -828
  13. package/dist/handlers/validation.js +49 -2
  14. package/dist/index.js +73 -73
  15. package/dist/setup.js +6 -6
  16. package/dist/templates/agent-guidelines.js +185 -185
  17. package/dist/templates/help-content.js +1622 -1544
  18. package/dist/tools.js +130 -74
  19. package/dist/utils.d.ts +15 -11
  20. package/dist/utils.js +53 -28
  21. package/docs/TOOLS.md +2407 -2053
  22. package/package.json +51 -51
  23. package/scripts/generate-docs.ts +212 -212
  24. package/scripts/version-bump.ts +203 -203
  25. package/src/api-client.test.ts +723 -723
  26. package/src/api-client.ts +2561 -2499
  27. package/src/cli.test.ts +24 -8
  28. package/src/cli.ts +204 -212
  29. package/src/handlers/__test-setup__.ts +236 -236
  30. package/src/handlers/__test-utils__.ts +87 -87
  31. package/src/handlers/blockers.test.ts +468 -468
  32. package/src/handlers/blockers.ts +163 -163
  33. package/src/handlers/bodies-of-work.test.ts +704 -704
  34. package/src/handlers/bodies-of-work.ts +526 -526
  35. package/src/handlers/connectors.test.ts +834 -834
  36. package/src/handlers/connectors.ts +229 -229
  37. package/src/handlers/cost.test.ts +462 -462
  38. package/src/handlers/cost.ts +285 -285
  39. package/src/handlers/decisions.test.ts +382 -382
  40. package/src/handlers/decisions.ts +153 -153
  41. package/src/handlers/deployment.test.ts +551 -551
  42. package/src/handlers/deployment.ts +541 -541
  43. package/src/handlers/discovery.test.ts +206 -206
  44. package/src/handlers/discovery.ts +392 -390
  45. package/src/handlers/fallback.test.ts +537 -537
  46. package/src/handlers/fallback.ts +194 -194
  47. package/src/handlers/file-checkouts.test.ts +750 -750
  48. package/src/handlers/file-checkouts.ts +185 -185
  49. package/src/handlers/findings.test.ts +633 -633
  50. package/src/handlers/findings.ts +239 -239
  51. package/src/handlers/git-issues.test.ts +631 -631
  52. package/src/handlers/git-issues.ts +136 -136
  53. package/src/handlers/ideas.test.ts +644 -644
  54. package/src/handlers/ideas.ts +207 -207
  55. package/src/handlers/index.ts +84 -84
  56. package/src/handlers/milestones.test.ts +475 -475
  57. package/src/handlers/milestones.ts +180 -180
  58. package/src/handlers/organizations.test.ts +826 -826
  59. package/src/handlers/organizations.ts +315 -315
  60. package/src/handlers/progress.test.ts +269 -269
  61. package/src/handlers/progress.ts +77 -77
  62. package/src/handlers/project.test.ts +546 -546
  63. package/src/handlers/project.ts +239 -239
  64. package/src/handlers/requests.test.ts +303 -303
  65. package/src/handlers/requests.ts +99 -99
  66. package/src/handlers/roles.test.ts +305 -305
  67. package/src/handlers/roles.ts +219 -219
  68. package/src/handlers/session.test.ts +998 -875
  69. package/src/handlers/session.ts +839 -730
  70. package/src/handlers/sprints.test.ts +732 -732
  71. package/src/handlers/sprints.ts +537 -537
  72. package/src/handlers/tasks.test.ts +931 -907
  73. package/src/handlers/tasks.ts +1121 -945
  74. package/src/handlers/tool-categories.test.ts +66 -66
  75. package/src/handlers/tool-docs.ts +1109 -1096
  76. package/src/handlers/types.test.ts +259 -259
  77. package/src/handlers/types.ts +175 -175
  78. package/src/handlers/validation.test.ts +582 -582
  79. package/src/handlers/validation.ts +164 -113
  80. package/src/index.test.ts +674 -0
  81. package/src/index.ts +792 -792
  82. package/src/setup.test.ts +233 -233
  83. package/src/setup.ts +404 -403
  84. package/src/templates/agent-guidelines.ts +210 -210
  85. package/src/templates/help-content.ts +1751 -1673
  86. package/src/token-tracking.test.ts +463 -463
  87. package/src/token-tracking.ts +166 -166
  88. package/src/tools.test.ts +416 -0
  89. package/src/tools.ts +3611 -3555
  90. package/src/utils.test.ts +785 -683
  91. package/src/utils.ts +469 -436
  92. package/src/validators.test.ts +223 -223
  93. package/src/validators.ts +249 -249
  94. package/tsconfig.json +16 -16
  95. package/vitest.config.ts +14 -14
@@ -1,826 +1,826 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import {
3
- listOrganizations,
4
- createOrganization,
5
- updateOrganization,
6
- deleteOrganization,
7
- listOrgMembers,
8
- inviteMember,
9
- updateMemberRole,
10
- removeMember,
11
- leaveOrganization,
12
- shareProjectWithOrg,
13
- updateProjectShare,
14
- unshareProject,
15
- listProjectShares,
16
- } from './organizations.js';
17
- import { ValidationError } from '../validators.js';
18
- import { createMockContext } from './__test-utils__.js';
19
- import { mockApiClient } from './__test-setup__.js';
20
-
21
- // ============================================================================
22
- // Test Utilities
23
- // ============================================================================
24
-
25
- const VALID_UUID = '123e4567-e89b-12d3-a456-426614174000';
26
- const OTHER_UUID = '223e4567-e89b-12d3-a456-426614174001';
27
-
28
- // ============================================================================
29
- // listOrganizations Tests
30
- // ============================================================================
31
-
32
- describe('listOrganizations', () => {
33
- beforeEach(() => vi.clearAllMocks());
34
-
35
- it('should return empty array when no organizations', async () => {
36
- mockApiClient.listOrganizations.mockResolvedValue({
37
- ok: true,
38
- data: { organizations: [], count: 0 },
39
- });
40
- const ctx = createMockContext();
41
-
42
- const result = await listOrganizations({}, ctx);
43
-
44
- expect(result.result).toMatchObject({
45
- organizations: [],
46
- count: 0,
47
- });
48
- });
49
-
50
- it('should return organizations with role and joined_at', async () => {
51
- mockApiClient.listOrganizations.mockResolvedValue({
52
- ok: true,
53
- data: {
54
- organizations: [{
55
- id: 'org-1',
56
- name: 'Test Org',
57
- slug: 'test-org',
58
- role: 'admin',
59
- joined_at: '2025-01-14T12:00:00Z',
60
- }],
61
- count: 1,
62
- },
63
- });
64
- const ctx = createMockContext();
65
-
66
- const result = await listOrganizations({}, ctx);
67
-
68
- expect(result.result).toMatchObject({ count: 1 });
69
- });
70
-
71
- it('should return error when query fails', async () => {
72
- mockApiClient.listOrganizations.mockResolvedValue({
73
- ok: false,
74
- error: 'Failed to list organizations',
75
- });
76
- const ctx = createMockContext();
77
-
78
- const result = await listOrganizations({}, ctx);
79
-
80
- expect(result.isError).toBe(true);
81
- expect(result.result).toMatchObject({ error: 'Failed to list organizations' });
82
- });
83
- });
84
-
85
- // ============================================================================
86
- // createOrganization Tests
87
- // ============================================================================
88
-
89
- describe('createOrganization', () => {
90
- beforeEach(() => vi.clearAllMocks());
91
-
92
- it('should throw error for missing name', async () => {
93
- const ctx = createMockContext();
94
- await expect(createOrganization({}, ctx)).rejects.toThrow(ValidationError);
95
- });
96
-
97
- it('should return error when slug is taken', async () => {
98
- mockApiClient.createOrganization.mockResolvedValue({
99
- ok: false,
100
- error: 'Organization slug "test-org" is already taken',
101
- });
102
- const ctx = createMockContext();
103
-
104
- const result = await createOrganization({ name: 'Test Org' }, ctx);
105
-
106
- expect(result.isError).toBe(true);
107
- expect(result.result).toMatchObject({ error: 'Organization slug "test-org" is already taken' });
108
- });
109
-
110
- it('should create organization with auto-generated slug', async () => {
111
- mockApiClient.createOrganization.mockResolvedValue({
112
- ok: true,
113
- data: {
114
- success: true,
115
- organization_id: 'org-1',
116
- slug: 'test-org',
117
- message: 'Organization "Test Org" created. You are the owner.',
118
- },
119
- });
120
- const ctx = createMockContext();
121
-
122
- const result = await createOrganization({ name: 'Test Org' }, ctx);
123
-
124
- expect(result.result).toMatchObject({
125
- success: true,
126
- message: 'Organization "Test Org" created. You are the owner.',
127
- });
128
- });
129
-
130
- it('should create organization with custom slug', async () => {
131
- mockApiClient.createOrganization.mockResolvedValue({
132
- ok: true,
133
- data: { success: true, organization_id: 'org-1', slug: 'my-custom-slug' },
134
- });
135
- const ctx = createMockContext();
136
-
137
- const result = await createOrganization(
138
- { name: 'Test Org', slug: 'my-custom-slug' },
139
- ctx
140
- );
141
-
142
- expect(result.result).toMatchObject({ success: true });
143
- });
144
-
145
- it('should return error when insert fails', async () => {
146
- mockApiClient.createOrganization.mockResolvedValue({
147
- ok: false,
148
- error: 'Failed to create organization',
149
- });
150
- const ctx = createMockContext();
151
-
152
- const result = await createOrganization({ name: 'Test Org' }, ctx);
153
-
154
- expect(result.isError).toBe(true);
155
- expect(result.result).toMatchObject({ error: 'Failed to create organization' });
156
- });
157
- });
158
-
159
- // ============================================================================
160
- // updateOrganization Tests
161
- // ============================================================================
162
-
163
- describe('updateOrganization', () => {
164
- beforeEach(() => vi.clearAllMocks());
165
-
166
- it('should throw error for missing organization_id', async () => {
167
- const ctx = createMockContext();
168
- await expect(
169
- updateOrganization({ name: 'New Name' }, ctx)
170
- ).rejects.toThrow(ValidationError);
171
- });
172
-
173
- it('should throw error for invalid organization_id UUID', async () => {
174
- const ctx = createMockContext();
175
- await expect(
176
- updateOrganization({ organization_id: 'invalid', name: 'New Name' }, ctx)
177
- ).rejects.toThrow(ValidationError);
178
- });
179
-
180
- it('should throw error when no updates provided', async () => {
181
- const ctx = createMockContext();
182
-
183
- await expect(
184
- updateOrganization({ organization_id: VALID_UUID }, ctx)
185
- ).rejects.toThrow(ValidationError);
186
- });
187
-
188
- it('should update organization successfully', async () => {
189
- mockApiClient.updateOrganization.mockResolvedValue({
190
- ok: true,
191
- data: { success: true, organization_id: VALID_UUID },
192
- });
193
- const ctx = createMockContext();
194
-
195
- const result = await updateOrganization(
196
- { organization_id: VALID_UUID, name: 'Updated Org', description: 'New desc' },
197
- ctx
198
- );
199
-
200
- expect(result.result).toMatchObject({ success: true });
201
- });
202
-
203
- it('should return error when update fails', async () => {
204
- mockApiClient.updateOrganization.mockResolvedValue({
205
- ok: false,
206
- error: 'Failed to update organization',
207
- });
208
- const ctx = createMockContext();
209
-
210
- const result = await updateOrganization({ organization_id: VALID_UUID, name: 'New Name' }, ctx);
211
-
212
- expect(result.isError).toBe(true);
213
- expect(result.result).toMatchObject({ error: 'Failed to update organization' });
214
- });
215
- });
216
-
217
- // ============================================================================
218
- // deleteOrganization Tests
219
- // ============================================================================
220
-
221
- describe('deleteOrganization', () => {
222
- beforeEach(() => vi.clearAllMocks());
223
-
224
- it('should throw error for missing organization_id', async () => {
225
- const ctx = createMockContext();
226
- await expect(deleteOrganization({}, ctx)).rejects.toThrow(ValidationError);
227
- });
228
-
229
- it('should throw error for invalid organization_id UUID', async () => {
230
- const ctx = createMockContext();
231
- await expect(
232
- deleteOrganization({ organization_id: 'invalid' }, ctx)
233
- ).rejects.toThrow(ValidationError);
234
- });
235
-
236
- it('should delete organization successfully', async () => {
237
- mockApiClient.deleteOrganization.mockResolvedValue({
238
- ok: true,
239
- data: { success: true, message: 'Organization deleted. All shares have been removed.' },
240
- });
241
- const ctx = createMockContext();
242
-
243
- const result = await deleteOrganization({ organization_id: VALID_UUID }, ctx);
244
-
245
- expect(result.result).toMatchObject({
246
- success: true,
247
- message: 'Organization deleted. All shares have been removed.',
248
- });
249
- });
250
-
251
- it('should return error when delete fails', async () => {
252
- mockApiClient.deleteOrganization.mockResolvedValue({
253
- ok: false,
254
- error: 'Failed to delete organization',
255
- });
256
- const ctx = createMockContext();
257
-
258
- const result = await deleteOrganization({ organization_id: VALID_UUID }, ctx);
259
-
260
- expect(result.isError).toBe(true);
261
- expect(result.result).toMatchObject({ error: 'Failed to delete organization' });
262
- });
263
- });
264
-
265
- // ============================================================================
266
- // listOrgMembers Tests
267
- // ============================================================================
268
-
269
- describe('listOrgMembers', () => {
270
- beforeEach(() => vi.clearAllMocks());
271
-
272
- it('should throw error for missing organization_id', async () => {
273
- const ctx = createMockContext();
274
- await expect(listOrgMembers({}, ctx)).rejects.toThrow(ValidationError);
275
- });
276
-
277
- it('should throw error for invalid organization_id UUID', async () => {
278
- const ctx = createMockContext();
279
- await expect(
280
- listOrgMembers({ organization_id: 'invalid' }, ctx)
281
- ).rejects.toThrow(ValidationError);
282
- });
283
-
284
- it('should return empty array when no members', async () => {
285
- mockApiClient.listOrgMembers.mockResolvedValue({
286
- ok: true,
287
- data: { members: [], count: 0 },
288
- });
289
- const ctx = createMockContext();
290
-
291
- const result = await listOrgMembers({ organization_id: VALID_UUID }, ctx);
292
-
293
- expect(result.result).toMatchObject({ members: [], count: 0 });
294
- });
295
-
296
- it('should return members list', async () => {
297
- mockApiClient.listOrgMembers.mockResolvedValue({
298
- ok: true,
299
- data: {
300
- members: [
301
- { id: 'm-1', user_id: 'u-1', role: 'owner' },
302
- { id: 'm-2', user_id: 'u-2', role: 'member' },
303
- ],
304
- count: 2,
305
- },
306
- });
307
- const ctx = createMockContext();
308
-
309
- const result = await listOrgMembers({ organization_id: VALID_UUID }, ctx);
310
-
311
- expect(result.result).toMatchObject({ count: 2 });
312
- });
313
-
314
- it('should return error when query fails', async () => {
315
- mockApiClient.listOrgMembers.mockResolvedValue({
316
- ok: false,
317
- error: 'Failed to list members',
318
- });
319
- const ctx = createMockContext();
320
-
321
- const result = await listOrgMembers({ organization_id: VALID_UUID }, ctx);
322
-
323
- expect(result.isError).toBe(true);
324
- expect(result.result).toMatchObject({ error: 'Failed to list members' });
325
- });
326
- });
327
-
328
- // ============================================================================
329
- // inviteMember Tests
330
- // ============================================================================
331
-
332
- describe('inviteMember', () => {
333
- beforeEach(() => vi.clearAllMocks());
334
-
335
- it('should throw error for missing organization_id', async () => {
336
- const ctx = createMockContext();
337
- await expect(
338
- inviteMember({ email: 'test@example.com' }, ctx)
339
- ).rejects.toThrow(ValidationError);
340
- });
341
-
342
- it('should throw error for missing email', async () => {
343
- const ctx = createMockContext();
344
- await expect(
345
- inviteMember({ organization_id: VALID_UUID }, ctx)
346
- ).rejects.toThrow(ValidationError);
347
- });
348
-
349
- it('should return error when invite already exists', async () => {
350
- mockApiClient.inviteMember.mockResolvedValue({
351
- ok: false,
352
- error: 'A pending invite already exists for test@example.com',
353
- });
354
- const ctx = createMockContext();
355
-
356
- const result = await inviteMember({ organization_id: VALID_UUID, email: 'test@example.com' }, ctx);
357
-
358
- expect(result.isError).toBe(true);
359
- expect(result.result).toMatchObject({ error: 'A pending invite already exists for test@example.com' });
360
- });
361
-
362
- it('should create invite successfully with default role', async () => {
363
- mockApiClient.inviteMember.mockResolvedValue({
364
- ok: true,
365
- data: {
366
- success: true,
367
- invite_id: 'invite-1',
368
- message: 'Invite sent to test@example.com with role "member"',
369
- },
370
- });
371
- const ctx = createMockContext();
372
-
373
- const result = await inviteMember({
374
- organization_id: VALID_UUID,
375
- email: 'test@example.com',
376
- }, ctx);
377
-
378
- expect(result.result).toMatchObject({
379
- success: true,
380
- message: 'Invite sent to test@example.com with role "member"',
381
- });
382
- });
383
-
384
- it('should return error when insert fails', async () => {
385
- mockApiClient.inviteMember.mockResolvedValue({
386
- ok: false,
387
- error: 'Failed to create invite',
388
- });
389
- const ctx = createMockContext();
390
-
391
- const result = await inviteMember({ organization_id: VALID_UUID, email: 'test@example.com' }, ctx);
392
-
393
- expect(result.isError).toBe(true);
394
- expect(result.result).toMatchObject({ error: 'Failed to create invite' });
395
- });
396
- });
397
-
398
- // ============================================================================
399
- // updateMemberRole Tests
400
- // ============================================================================
401
-
402
- describe('updateMemberRole', () => {
403
- beforeEach(() => vi.clearAllMocks());
404
-
405
- it('should throw error for missing organization_id', async () => {
406
- const ctx = createMockContext();
407
- await expect(
408
- updateMemberRole({ user_id: VALID_UUID, role: 'admin' }, ctx)
409
- ).rejects.toThrow(ValidationError);
410
- });
411
-
412
- it('should throw error for missing user_id', async () => {
413
- const ctx = createMockContext();
414
- await expect(
415
- updateMemberRole({ organization_id: VALID_UUID, role: 'admin' }, ctx)
416
- ).rejects.toThrow(ValidationError);
417
- });
418
-
419
- it('should throw error for missing role', async () => {
420
- const ctx = createMockContext();
421
- await expect(
422
- updateMemberRole({ organization_id: VALID_UUID, user_id: OTHER_UUID }, ctx)
423
- ).rejects.toThrow(ValidationError);
424
- });
425
-
426
- it('should return error when changing own role', async () => {
427
- mockApiClient.updateMemberRole.mockResolvedValue({
428
- ok: false,
429
- error: 'Cannot change your own role',
430
- });
431
- const ctx = createMockContext({ userId: VALID_UUID });
432
-
433
- const result = await updateMemberRole({
434
- organization_id: OTHER_UUID,
435
- user_id: VALID_UUID,
436
- role: 'admin',
437
- }, ctx);
438
-
439
- expect(result.isError).toBe(true);
440
- expect(result.result).toMatchObject({ error: 'Cannot change your own role' });
441
- });
442
-
443
- it('should update member role successfully', async () => {
444
- mockApiClient.updateMemberRole.mockResolvedValue({
445
- ok: true,
446
- data: { success: true, user_id: OTHER_UUID, role: 'admin' },
447
- });
448
- const ctx = createMockContext();
449
-
450
- const result = await updateMemberRole({
451
- organization_id: VALID_UUID,
452
- user_id: OTHER_UUID,
453
- role: 'admin',
454
- }, ctx);
455
-
456
- expect(result.result).toMatchObject({ success: true });
457
- });
458
-
459
- it('should return error when update fails', async () => {
460
- mockApiClient.updateMemberRole.mockResolvedValue({
461
- ok: false,
462
- error: 'Failed to update member role',
463
- });
464
- const ctx = createMockContext();
465
-
466
- const result = await updateMemberRole({
467
- organization_id: VALID_UUID,
468
- user_id: OTHER_UUID,
469
- role: 'admin',
470
- }, ctx);
471
-
472
- expect(result.isError).toBe(true);
473
- expect(result.result).toMatchObject({ error: 'Failed to update member role' });
474
- });
475
- });
476
-
477
- // ============================================================================
478
- // removeMember Tests
479
- // ============================================================================
480
-
481
- describe('removeMember', () => {
482
- beforeEach(() => vi.clearAllMocks());
483
-
484
- it('should throw error for missing organization_id', async () => {
485
- const ctx = createMockContext();
486
- await expect(
487
- removeMember({ user_id: VALID_UUID }, ctx)
488
- ).rejects.toThrow(ValidationError);
489
- });
490
-
491
- it('should throw error for missing user_id', async () => {
492
- const ctx = createMockContext();
493
- await expect(
494
- removeMember({ organization_id: VALID_UUID }, ctx)
495
- ).rejects.toThrow(ValidationError);
496
- });
497
-
498
- it('should remove member successfully', async () => {
499
- mockApiClient.removeMember.mockResolvedValue({
500
- ok: true,
501
- data: {
502
- success: true,
503
- message: 'Member removed. Their org-scoped API keys have been invalidated.',
504
- },
505
- });
506
- const ctx = createMockContext();
507
-
508
- const result = await removeMember({
509
- organization_id: VALID_UUID,
510
- user_id: OTHER_UUID,
511
- }, ctx);
512
-
513
- expect(result.result).toMatchObject({
514
- success: true,
515
- message: 'Member removed. Their org-scoped API keys have been invalidated.',
516
- });
517
- });
518
-
519
- it('should return error when delete fails', async () => {
520
- mockApiClient.removeMember.mockResolvedValue({
521
- ok: false,
522
- error: 'Failed to remove member',
523
- });
524
- const ctx = createMockContext();
525
-
526
- const result = await removeMember({ organization_id: VALID_UUID, user_id: OTHER_UUID }, ctx);
527
-
528
- expect(result.isError).toBe(true);
529
- expect(result.result).toMatchObject({ error: 'Failed to remove member' });
530
- });
531
- });
532
-
533
- // ============================================================================
534
- // leaveOrganization Tests
535
- // ============================================================================
536
-
537
- describe('leaveOrganization', () => {
538
- beforeEach(() => vi.clearAllMocks());
539
-
540
- it('should throw error for missing organization_id', async () => {
541
- const ctx = createMockContext();
542
- await expect(leaveOrganization({}, ctx)).rejects.toThrow(ValidationError);
543
- });
544
-
545
- it('should return error when user is owner', async () => {
546
- mockApiClient.leaveOrganization.mockResolvedValue({
547
- ok: false,
548
- error: 'Owner cannot leave. Transfer ownership first or delete the organization.',
549
- });
550
- const ctx = createMockContext();
551
-
552
- const result = await leaveOrganization({ organization_id: VALID_UUID }, ctx);
553
-
554
- expect(result.isError).toBe(true);
555
- expect(result.result).toMatchObject({ error: 'Owner cannot leave. Transfer ownership first or delete the organization.' });
556
- });
557
-
558
- it('should leave organization successfully', async () => {
559
- mockApiClient.leaveOrganization.mockResolvedValue({
560
- ok: true,
561
- data: { success: true, message: 'You have left the organization.' },
562
- });
563
- const ctx = createMockContext();
564
-
565
- const result = await leaveOrganization({ organization_id: VALID_UUID }, ctx);
566
-
567
- expect(result.result).toMatchObject({
568
- success: true,
569
- message: 'You have left the organization.',
570
- });
571
- });
572
-
573
- it('should return error when delete fails', async () => {
574
- mockApiClient.leaveOrganization.mockResolvedValue({
575
- ok: false,
576
- error: 'Failed to leave organization',
577
- });
578
- const ctx = createMockContext();
579
-
580
- const result = await leaveOrganization({ organization_id: VALID_UUID }, ctx);
581
-
582
- expect(result.isError).toBe(true);
583
- expect(result.result).toMatchObject({ error: 'Failed to leave organization' });
584
- });
585
- });
586
-
587
- // ============================================================================
588
- // shareProjectWithOrg Tests
589
- // ============================================================================
590
-
591
- describe('shareProjectWithOrg', () => {
592
- beforeEach(() => vi.clearAllMocks());
593
-
594
- it('should throw error for missing project_id', async () => {
595
- const ctx = createMockContext();
596
- await expect(
597
- shareProjectWithOrg({ organization_id: VALID_UUID }, ctx)
598
- ).rejects.toThrow(ValidationError);
599
- });
600
-
601
- it('should throw error for missing organization_id', async () => {
602
- const ctx = createMockContext();
603
- await expect(
604
- shareProjectWithOrg({ project_id: VALID_UUID }, ctx)
605
- ).rejects.toThrow(ValidationError);
606
- });
607
-
608
- it('should return error when project not found or not owned', async () => {
609
- mockApiClient.shareProjectWithOrg.mockResolvedValue({
610
- ok: false,
611
- error: 'Project not found or you are not the owner',
612
- });
613
- const ctx = createMockContext();
614
-
615
- const result = await shareProjectWithOrg({ project_id: VALID_UUID, organization_id: OTHER_UUID }, ctx);
616
-
617
- expect(result.isError).toBe(true);
618
- expect(result.result).toMatchObject({ error: 'Project not found or you are not the owner' });
619
- });
620
-
621
- it('should share project successfully with default permission', async () => {
622
- mockApiClient.shareProjectWithOrg.mockResolvedValue({
623
- ok: true,
624
- data: { success: true, share_id: 'share-1', permission: 'read' },
625
- });
626
- const ctx = createMockContext();
627
-
628
- const result = await shareProjectWithOrg({
629
- project_id: VALID_UUID,
630
- organization_id: OTHER_UUID,
631
- }, ctx);
632
-
633
- expect(result.result).toMatchObject({ success: true });
634
- });
635
-
636
- it('should return error when share already exists', async () => {
637
- mockApiClient.shareProjectWithOrg.mockResolvedValue({
638
- ok: false,
639
- error: 'Project is already shared with this organization',
640
- });
641
- const ctx = createMockContext();
642
-
643
- const result = await shareProjectWithOrg({ project_id: VALID_UUID, organization_id: OTHER_UUID }, ctx);
644
-
645
- expect(result.isError).toBe(true);
646
- expect(result.result).toMatchObject({ error: 'Project is already shared with this organization' });
647
- });
648
- });
649
-
650
- // ============================================================================
651
- // updateProjectShare Tests
652
- // ============================================================================
653
-
654
- describe('updateProjectShare', () => {
655
- beforeEach(() => vi.clearAllMocks());
656
-
657
- it('should throw error for missing project_id', async () => {
658
- const ctx = createMockContext();
659
- await expect(
660
- updateProjectShare({ organization_id: VALID_UUID, permission: 'write' }, ctx)
661
- ).rejects.toThrow(ValidationError);
662
- });
663
-
664
- it('should throw error for missing organization_id', async () => {
665
- const ctx = createMockContext();
666
- await expect(
667
- updateProjectShare({ project_id: VALID_UUID, permission: 'write' }, ctx)
668
- ).rejects.toThrow(ValidationError);
669
- });
670
-
671
- it('should throw error for missing permission', async () => {
672
- const ctx = createMockContext();
673
- await expect(
674
- updateProjectShare({ project_id: VALID_UUID, organization_id: OTHER_UUID }, ctx)
675
- ).rejects.toThrow(ValidationError);
676
- });
677
-
678
- it('should update share permission successfully', async () => {
679
- mockApiClient.updateProjectShare.mockResolvedValue({
680
- ok: true,
681
- data: { success: true, share_id: 'share-1', permission: 'write' },
682
- });
683
- const ctx = createMockContext();
684
-
685
- const result = await updateProjectShare({
686
- project_id: VALID_UUID,
687
- organization_id: OTHER_UUID,
688
- permission: 'write',
689
- }, ctx);
690
-
691
- expect(result.result).toMatchObject({ success: true });
692
- });
693
-
694
- it('should return error when update fails', async () => {
695
- mockApiClient.updateProjectShare.mockResolvedValue({
696
- ok: false,
697
- error: 'Failed to update share',
698
- });
699
- const ctx = createMockContext();
700
-
701
- const result = await updateProjectShare({
702
- project_id: VALID_UUID,
703
- organization_id: OTHER_UUID,
704
- permission: 'write',
705
- }, ctx);
706
-
707
- expect(result.isError).toBe(true);
708
- expect(result.result).toMatchObject({ error: 'Failed to update share' });
709
- });
710
- });
711
-
712
- // ============================================================================
713
- // unshareProject Tests
714
- // ============================================================================
715
-
716
- describe('unshareProject', () => {
717
- beforeEach(() => vi.clearAllMocks());
718
-
719
- it('should throw error for missing project_id', async () => {
720
- const ctx = createMockContext();
721
- await expect(
722
- unshareProject({ organization_id: VALID_UUID }, ctx)
723
- ).rejects.toThrow(ValidationError);
724
- });
725
-
726
- it('should throw error for missing organization_id', async () => {
727
- const ctx = createMockContext();
728
- await expect(
729
- unshareProject({ project_id: VALID_UUID }, ctx)
730
- ).rejects.toThrow(ValidationError);
731
- });
732
-
733
- it('should unshare project successfully', async () => {
734
- mockApiClient.unshareProject.mockResolvedValue({
735
- ok: true,
736
- data: {
737
- success: true,
738
- message: 'Project share removed. Org members can no longer access this project.',
739
- },
740
- });
741
- const ctx = createMockContext();
742
-
743
- const result = await unshareProject({
744
- project_id: VALID_UUID,
745
- organization_id: OTHER_UUID,
746
- }, ctx);
747
-
748
- expect(result.result).toMatchObject({
749
- success: true,
750
- message: 'Project share removed. Org members can no longer access this project.',
751
- });
752
- });
753
-
754
- it('should return error when delete fails', async () => {
755
- mockApiClient.unshareProject.mockResolvedValue({
756
- ok: false,
757
- error: 'Failed to unshare project',
758
- });
759
- const ctx = createMockContext();
760
-
761
- const result = await unshareProject({ project_id: VALID_UUID, organization_id: OTHER_UUID }, ctx);
762
-
763
- expect(result.isError).toBe(true);
764
- expect(result.result).toMatchObject({ error: 'Failed to unshare project' });
765
- });
766
- });
767
-
768
- // ============================================================================
769
- // listProjectShares Tests
770
- // ============================================================================
771
-
772
- describe('listProjectShares', () => {
773
- beforeEach(() => vi.clearAllMocks());
774
-
775
- it('should throw error for missing project_id', async () => {
776
- const ctx = createMockContext();
777
- await expect(listProjectShares({}, ctx)).rejects.toThrow(ValidationError);
778
- });
779
-
780
- it('should throw error for invalid project_id UUID', async () => {
781
- const ctx = createMockContext();
782
- await expect(
783
- listProjectShares({ project_id: 'invalid' }, ctx)
784
- ).rejects.toThrow(ValidationError);
785
- });
786
-
787
- it('should return empty array when no shares', async () => {
788
- mockApiClient.listProjectShares.mockResolvedValue({
789
- ok: true,
790
- data: { shares: [], count: 0 },
791
- });
792
- const ctx = createMockContext();
793
-
794
- const result = await listProjectShares({ project_id: VALID_UUID }, ctx);
795
-
796
- expect(result.result).toMatchObject({ shares: [], count: 0 });
797
- });
798
-
799
- it('should return shares list', async () => {
800
- mockApiClient.listProjectShares.mockResolvedValue({
801
- ok: true,
802
- data: {
803
- shares: [{ id: 'share-1', permission: 'read', organization: { name: 'Test Org' } }],
804
- count: 1,
805
- },
806
- });
807
- const ctx = createMockContext();
808
-
809
- const result = await listProjectShares({ project_id: VALID_UUID }, ctx);
810
-
811
- expect(result.result).toMatchObject({ count: 1 });
812
- });
813
-
814
- it('should return error when query fails', async () => {
815
- mockApiClient.listProjectShares.mockResolvedValue({
816
- ok: false,
817
- error: 'Failed to list shares',
818
- });
819
- const ctx = createMockContext();
820
-
821
- const result = await listProjectShares({ project_id: VALID_UUID }, ctx);
822
-
823
- expect(result.isError).toBe(true);
824
- expect(result.result).toMatchObject({ error: 'Failed to list shares' });
825
- });
826
- });
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import {
3
+ listOrganizations,
4
+ createOrganization,
5
+ updateOrganization,
6
+ deleteOrganization,
7
+ listOrgMembers,
8
+ inviteMember,
9
+ updateMemberRole,
10
+ removeMember,
11
+ leaveOrganization,
12
+ shareProjectWithOrg,
13
+ updateProjectShare,
14
+ unshareProject,
15
+ listProjectShares,
16
+ } from './organizations.js';
17
+ import { ValidationError } from '../validators.js';
18
+ import { createMockContext } from './__test-utils__.js';
19
+ import { mockApiClient } from './__test-setup__.js';
20
+
21
+ // ============================================================================
22
+ // Test Utilities
23
+ // ============================================================================
24
+
25
+ const VALID_UUID = '123e4567-e89b-12d3-a456-426614174000';
26
+ const OTHER_UUID = '223e4567-e89b-12d3-a456-426614174001';
27
+
28
+ // ============================================================================
29
+ // listOrganizations Tests
30
+ // ============================================================================
31
+
32
+ describe('listOrganizations', () => {
33
+ beforeEach(() => vi.clearAllMocks());
34
+
35
+ it('should return empty array when no organizations', async () => {
36
+ mockApiClient.listOrganizations.mockResolvedValue({
37
+ ok: true,
38
+ data: { organizations: [], count: 0 },
39
+ });
40
+ const ctx = createMockContext();
41
+
42
+ const result = await listOrganizations({}, ctx);
43
+
44
+ expect(result.result).toMatchObject({
45
+ organizations: [],
46
+ count: 0,
47
+ });
48
+ });
49
+
50
+ it('should return organizations with role and joined_at', async () => {
51
+ mockApiClient.listOrganizations.mockResolvedValue({
52
+ ok: true,
53
+ data: {
54
+ organizations: [{
55
+ id: 'org-1',
56
+ name: 'Test Org',
57
+ slug: 'test-org',
58
+ role: 'admin',
59
+ joined_at: '2025-01-14T12:00:00Z',
60
+ }],
61
+ count: 1,
62
+ },
63
+ });
64
+ const ctx = createMockContext();
65
+
66
+ const result = await listOrganizations({}, ctx);
67
+
68
+ expect(result.result).toMatchObject({ count: 1 });
69
+ });
70
+
71
+ it('should return error when query fails', async () => {
72
+ mockApiClient.listOrganizations.mockResolvedValue({
73
+ ok: false,
74
+ error: 'Failed to list organizations',
75
+ });
76
+ const ctx = createMockContext();
77
+
78
+ const result = await listOrganizations({}, ctx);
79
+
80
+ expect(result.isError).toBe(true);
81
+ expect(result.result).toMatchObject({ error: 'Failed to list organizations' });
82
+ });
83
+ });
84
+
85
+ // ============================================================================
86
+ // createOrganization Tests
87
+ // ============================================================================
88
+
89
+ describe('createOrganization', () => {
90
+ beforeEach(() => vi.clearAllMocks());
91
+
92
+ it('should throw error for missing name', async () => {
93
+ const ctx = createMockContext();
94
+ await expect(createOrganization({}, ctx)).rejects.toThrow(ValidationError);
95
+ });
96
+
97
+ it('should return error when slug is taken', async () => {
98
+ mockApiClient.createOrganization.mockResolvedValue({
99
+ ok: false,
100
+ error: 'Organization slug "test-org" is already taken',
101
+ });
102
+ const ctx = createMockContext();
103
+
104
+ const result = await createOrganization({ name: 'Test Org' }, ctx);
105
+
106
+ expect(result.isError).toBe(true);
107
+ expect(result.result).toMatchObject({ error: 'Organization slug "test-org" is already taken' });
108
+ });
109
+
110
+ it('should create organization with auto-generated slug', async () => {
111
+ mockApiClient.createOrganization.mockResolvedValue({
112
+ ok: true,
113
+ data: {
114
+ success: true,
115
+ organization_id: 'org-1',
116
+ slug: 'test-org',
117
+ message: 'Organization "Test Org" created. You are the owner.',
118
+ },
119
+ });
120
+ const ctx = createMockContext();
121
+
122
+ const result = await createOrganization({ name: 'Test Org' }, ctx);
123
+
124
+ expect(result.result).toMatchObject({
125
+ success: true,
126
+ message: 'Organization "Test Org" created. You are the owner.',
127
+ });
128
+ });
129
+
130
+ it('should create organization with custom slug', async () => {
131
+ mockApiClient.createOrganization.mockResolvedValue({
132
+ ok: true,
133
+ data: { success: true, organization_id: 'org-1', slug: 'my-custom-slug' },
134
+ });
135
+ const ctx = createMockContext();
136
+
137
+ const result = await createOrganization(
138
+ { name: 'Test Org', slug: 'my-custom-slug' },
139
+ ctx
140
+ );
141
+
142
+ expect(result.result).toMatchObject({ success: true });
143
+ });
144
+
145
+ it('should return error when insert fails', async () => {
146
+ mockApiClient.createOrganization.mockResolvedValue({
147
+ ok: false,
148
+ error: 'Failed to create organization',
149
+ });
150
+ const ctx = createMockContext();
151
+
152
+ const result = await createOrganization({ name: 'Test Org' }, ctx);
153
+
154
+ expect(result.isError).toBe(true);
155
+ expect(result.result).toMatchObject({ error: 'Failed to create organization' });
156
+ });
157
+ });
158
+
159
+ // ============================================================================
160
+ // updateOrganization Tests
161
+ // ============================================================================
162
+
163
+ describe('updateOrganization', () => {
164
+ beforeEach(() => vi.clearAllMocks());
165
+
166
+ it('should throw error for missing organization_id', async () => {
167
+ const ctx = createMockContext();
168
+ await expect(
169
+ updateOrganization({ name: 'New Name' }, ctx)
170
+ ).rejects.toThrow(ValidationError);
171
+ });
172
+
173
+ it('should throw error for invalid organization_id UUID', async () => {
174
+ const ctx = createMockContext();
175
+ await expect(
176
+ updateOrganization({ organization_id: 'invalid', name: 'New Name' }, ctx)
177
+ ).rejects.toThrow(ValidationError);
178
+ });
179
+
180
+ it('should throw error when no updates provided', async () => {
181
+ const ctx = createMockContext();
182
+
183
+ await expect(
184
+ updateOrganization({ organization_id: VALID_UUID }, ctx)
185
+ ).rejects.toThrow(ValidationError);
186
+ });
187
+
188
+ it('should update organization successfully', async () => {
189
+ mockApiClient.updateOrganization.mockResolvedValue({
190
+ ok: true,
191
+ data: { success: true, organization_id: VALID_UUID },
192
+ });
193
+ const ctx = createMockContext();
194
+
195
+ const result = await updateOrganization(
196
+ { organization_id: VALID_UUID, name: 'Updated Org', description: 'New desc' },
197
+ ctx
198
+ );
199
+
200
+ expect(result.result).toMatchObject({ success: true });
201
+ });
202
+
203
+ it('should return error when update fails', async () => {
204
+ mockApiClient.updateOrganization.mockResolvedValue({
205
+ ok: false,
206
+ error: 'Failed to update organization',
207
+ });
208
+ const ctx = createMockContext();
209
+
210
+ const result = await updateOrganization({ organization_id: VALID_UUID, name: 'New Name' }, ctx);
211
+
212
+ expect(result.isError).toBe(true);
213
+ expect(result.result).toMatchObject({ error: 'Failed to update organization' });
214
+ });
215
+ });
216
+
217
+ // ============================================================================
218
+ // deleteOrganization Tests
219
+ // ============================================================================
220
+
221
+ describe('deleteOrganization', () => {
222
+ beforeEach(() => vi.clearAllMocks());
223
+
224
+ it('should throw error for missing organization_id', async () => {
225
+ const ctx = createMockContext();
226
+ await expect(deleteOrganization({}, ctx)).rejects.toThrow(ValidationError);
227
+ });
228
+
229
+ it('should throw error for invalid organization_id UUID', async () => {
230
+ const ctx = createMockContext();
231
+ await expect(
232
+ deleteOrganization({ organization_id: 'invalid' }, ctx)
233
+ ).rejects.toThrow(ValidationError);
234
+ });
235
+
236
+ it('should delete organization successfully', async () => {
237
+ mockApiClient.deleteOrganization.mockResolvedValue({
238
+ ok: true,
239
+ data: { success: true, message: 'Organization deleted. All shares have been removed.' },
240
+ });
241
+ const ctx = createMockContext();
242
+
243
+ const result = await deleteOrganization({ organization_id: VALID_UUID }, ctx);
244
+
245
+ expect(result.result).toMatchObject({
246
+ success: true,
247
+ message: 'Organization deleted. All shares have been removed.',
248
+ });
249
+ });
250
+
251
+ it('should return error when delete fails', async () => {
252
+ mockApiClient.deleteOrganization.mockResolvedValue({
253
+ ok: false,
254
+ error: 'Failed to delete organization',
255
+ });
256
+ const ctx = createMockContext();
257
+
258
+ const result = await deleteOrganization({ organization_id: VALID_UUID }, ctx);
259
+
260
+ expect(result.isError).toBe(true);
261
+ expect(result.result).toMatchObject({ error: 'Failed to delete organization' });
262
+ });
263
+ });
264
+
265
+ // ============================================================================
266
+ // listOrgMembers Tests
267
+ // ============================================================================
268
+
269
+ describe('listOrgMembers', () => {
270
+ beforeEach(() => vi.clearAllMocks());
271
+
272
+ it('should throw error for missing organization_id', async () => {
273
+ const ctx = createMockContext();
274
+ await expect(listOrgMembers({}, ctx)).rejects.toThrow(ValidationError);
275
+ });
276
+
277
+ it('should throw error for invalid organization_id UUID', async () => {
278
+ const ctx = createMockContext();
279
+ await expect(
280
+ listOrgMembers({ organization_id: 'invalid' }, ctx)
281
+ ).rejects.toThrow(ValidationError);
282
+ });
283
+
284
+ it('should return empty array when no members', async () => {
285
+ mockApiClient.listOrgMembers.mockResolvedValue({
286
+ ok: true,
287
+ data: { members: [], count: 0 },
288
+ });
289
+ const ctx = createMockContext();
290
+
291
+ const result = await listOrgMembers({ organization_id: VALID_UUID }, ctx);
292
+
293
+ expect(result.result).toMatchObject({ members: [], count: 0 });
294
+ });
295
+
296
+ it('should return members list', async () => {
297
+ mockApiClient.listOrgMembers.mockResolvedValue({
298
+ ok: true,
299
+ data: {
300
+ members: [
301
+ { id: 'm-1', user_id: 'u-1', role: 'owner' },
302
+ { id: 'm-2', user_id: 'u-2', role: 'member' },
303
+ ],
304
+ count: 2,
305
+ },
306
+ });
307
+ const ctx = createMockContext();
308
+
309
+ const result = await listOrgMembers({ organization_id: VALID_UUID }, ctx);
310
+
311
+ expect(result.result).toMatchObject({ count: 2 });
312
+ });
313
+
314
+ it('should return error when query fails', async () => {
315
+ mockApiClient.listOrgMembers.mockResolvedValue({
316
+ ok: false,
317
+ error: 'Failed to list members',
318
+ });
319
+ const ctx = createMockContext();
320
+
321
+ const result = await listOrgMembers({ organization_id: VALID_UUID }, ctx);
322
+
323
+ expect(result.isError).toBe(true);
324
+ expect(result.result).toMatchObject({ error: 'Failed to list members' });
325
+ });
326
+ });
327
+
328
+ // ============================================================================
329
+ // inviteMember Tests
330
+ // ============================================================================
331
+
332
+ describe('inviteMember', () => {
333
+ beforeEach(() => vi.clearAllMocks());
334
+
335
+ it('should throw error for missing organization_id', async () => {
336
+ const ctx = createMockContext();
337
+ await expect(
338
+ inviteMember({ email: 'test@example.com' }, ctx)
339
+ ).rejects.toThrow(ValidationError);
340
+ });
341
+
342
+ it('should throw error for missing email', async () => {
343
+ const ctx = createMockContext();
344
+ await expect(
345
+ inviteMember({ organization_id: VALID_UUID }, ctx)
346
+ ).rejects.toThrow(ValidationError);
347
+ });
348
+
349
+ it('should return error when invite already exists', async () => {
350
+ mockApiClient.inviteMember.mockResolvedValue({
351
+ ok: false,
352
+ error: 'A pending invite already exists for test@example.com',
353
+ });
354
+ const ctx = createMockContext();
355
+
356
+ const result = await inviteMember({ organization_id: VALID_UUID, email: 'test@example.com' }, ctx);
357
+
358
+ expect(result.isError).toBe(true);
359
+ expect(result.result).toMatchObject({ error: 'A pending invite already exists for test@example.com' });
360
+ });
361
+
362
+ it('should create invite successfully with default role', async () => {
363
+ mockApiClient.inviteMember.mockResolvedValue({
364
+ ok: true,
365
+ data: {
366
+ success: true,
367
+ invite_id: 'invite-1',
368
+ message: 'Invite sent to test@example.com with role "member"',
369
+ },
370
+ });
371
+ const ctx = createMockContext();
372
+
373
+ const result = await inviteMember({
374
+ organization_id: VALID_UUID,
375
+ email: 'test@example.com',
376
+ }, ctx);
377
+
378
+ expect(result.result).toMatchObject({
379
+ success: true,
380
+ message: 'Invite sent to test@example.com with role "member"',
381
+ });
382
+ });
383
+
384
+ it('should return error when insert fails', async () => {
385
+ mockApiClient.inviteMember.mockResolvedValue({
386
+ ok: false,
387
+ error: 'Failed to create invite',
388
+ });
389
+ const ctx = createMockContext();
390
+
391
+ const result = await inviteMember({ organization_id: VALID_UUID, email: 'test@example.com' }, ctx);
392
+
393
+ expect(result.isError).toBe(true);
394
+ expect(result.result).toMatchObject({ error: 'Failed to create invite' });
395
+ });
396
+ });
397
+
398
+ // ============================================================================
399
+ // updateMemberRole Tests
400
+ // ============================================================================
401
+
402
+ describe('updateMemberRole', () => {
403
+ beforeEach(() => vi.clearAllMocks());
404
+
405
+ it('should throw error for missing organization_id', async () => {
406
+ const ctx = createMockContext();
407
+ await expect(
408
+ updateMemberRole({ user_id: VALID_UUID, role: 'admin' }, ctx)
409
+ ).rejects.toThrow(ValidationError);
410
+ });
411
+
412
+ it('should throw error for missing user_id', async () => {
413
+ const ctx = createMockContext();
414
+ await expect(
415
+ updateMemberRole({ organization_id: VALID_UUID, role: 'admin' }, ctx)
416
+ ).rejects.toThrow(ValidationError);
417
+ });
418
+
419
+ it('should throw error for missing role', async () => {
420
+ const ctx = createMockContext();
421
+ await expect(
422
+ updateMemberRole({ organization_id: VALID_UUID, user_id: OTHER_UUID }, ctx)
423
+ ).rejects.toThrow(ValidationError);
424
+ });
425
+
426
+ it('should return error when changing own role', async () => {
427
+ mockApiClient.updateMemberRole.mockResolvedValue({
428
+ ok: false,
429
+ error: 'Cannot change your own role',
430
+ });
431
+ const ctx = createMockContext({ userId: VALID_UUID });
432
+
433
+ const result = await updateMemberRole({
434
+ organization_id: OTHER_UUID,
435
+ user_id: VALID_UUID,
436
+ role: 'admin',
437
+ }, ctx);
438
+
439
+ expect(result.isError).toBe(true);
440
+ expect(result.result).toMatchObject({ error: 'Cannot change your own role' });
441
+ });
442
+
443
+ it('should update member role successfully', async () => {
444
+ mockApiClient.updateMemberRole.mockResolvedValue({
445
+ ok: true,
446
+ data: { success: true, user_id: OTHER_UUID, role: 'admin' },
447
+ });
448
+ const ctx = createMockContext();
449
+
450
+ const result = await updateMemberRole({
451
+ organization_id: VALID_UUID,
452
+ user_id: OTHER_UUID,
453
+ role: 'admin',
454
+ }, ctx);
455
+
456
+ expect(result.result).toMatchObject({ success: true });
457
+ });
458
+
459
+ it('should return error when update fails', async () => {
460
+ mockApiClient.updateMemberRole.mockResolvedValue({
461
+ ok: false,
462
+ error: 'Failed to update member role',
463
+ });
464
+ const ctx = createMockContext();
465
+
466
+ const result = await updateMemberRole({
467
+ organization_id: VALID_UUID,
468
+ user_id: OTHER_UUID,
469
+ role: 'admin',
470
+ }, ctx);
471
+
472
+ expect(result.isError).toBe(true);
473
+ expect(result.result).toMatchObject({ error: 'Failed to update member role' });
474
+ });
475
+ });
476
+
477
+ // ============================================================================
478
+ // removeMember Tests
479
+ // ============================================================================
480
+
481
+ describe('removeMember', () => {
482
+ beforeEach(() => vi.clearAllMocks());
483
+
484
+ it('should throw error for missing organization_id', async () => {
485
+ const ctx = createMockContext();
486
+ await expect(
487
+ removeMember({ user_id: VALID_UUID }, ctx)
488
+ ).rejects.toThrow(ValidationError);
489
+ });
490
+
491
+ it('should throw error for missing user_id', async () => {
492
+ const ctx = createMockContext();
493
+ await expect(
494
+ removeMember({ organization_id: VALID_UUID }, ctx)
495
+ ).rejects.toThrow(ValidationError);
496
+ });
497
+
498
+ it('should remove member successfully', async () => {
499
+ mockApiClient.removeMember.mockResolvedValue({
500
+ ok: true,
501
+ data: {
502
+ success: true,
503
+ message: 'Member removed. Their org-scoped API keys have been invalidated.',
504
+ },
505
+ });
506
+ const ctx = createMockContext();
507
+
508
+ const result = await removeMember({
509
+ organization_id: VALID_UUID,
510
+ user_id: OTHER_UUID,
511
+ }, ctx);
512
+
513
+ expect(result.result).toMatchObject({
514
+ success: true,
515
+ message: 'Member removed. Their org-scoped API keys have been invalidated.',
516
+ });
517
+ });
518
+
519
+ it('should return error when delete fails', async () => {
520
+ mockApiClient.removeMember.mockResolvedValue({
521
+ ok: false,
522
+ error: 'Failed to remove member',
523
+ });
524
+ const ctx = createMockContext();
525
+
526
+ const result = await removeMember({ organization_id: VALID_UUID, user_id: OTHER_UUID }, ctx);
527
+
528
+ expect(result.isError).toBe(true);
529
+ expect(result.result).toMatchObject({ error: 'Failed to remove member' });
530
+ });
531
+ });
532
+
533
+ // ============================================================================
534
+ // leaveOrganization Tests
535
+ // ============================================================================
536
+
537
+ describe('leaveOrganization', () => {
538
+ beforeEach(() => vi.clearAllMocks());
539
+
540
+ it('should throw error for missing organization_id', async () => {
541
+ const ctx = createMockContext();
542
+ await expect(leaveOrganization({}, ctx)).rejects.toThrow(ValidationError);
543
+ });
544
+
545
+ it('should return error when user is owner', async () => {
546
+ mockApiClient.leaveOrganization.mockResolvedValue({
547
+ ok: false,
548
+ error: 'Owner cannot leave. Transfer ownership first or delete the organization.',
549
+ });
550
+ const ctx = createMockContext();
551
+
552
+ const result = await leaveOrganization({ organization_id: VALID_UUID }, ctx);
553
+
554
+ expect(result.isError).toBe(true);
555
+ expect(result.result).toMatchObject({ error: 'Owner cannot leave. Transfer ownership first or delete the organization.' });
556
+ });
557
+
558
+ it('should leave organization successfully', async () => {
559
+ mockApiClient.leaveOrganization.mockResolvedValue({
560
+ ok: true,
561
+ data: { success: true, message: 'You have left the organization.' },
562
+ });
563
+ const ctx = createMockContext();
564
+
565
+ const result = await leaveOrganization({ organization_id: VALID_UUID }, ctx);
566
+
567
+ expect(result.result).toMatchObject({
568
+ success: true,
569
+ message: 'You have left the organization.',
570
+ });
571
+ });
572
+
573
+ it('should return error when delete fails', async () => {
574
+ mockApiClient.leaveOrganization.mockResolvedValue({
575
+ ok: false,
576
+ error: 'Failed to leave organization',
577
+ });
578
+ const ctx = createMockContext();
579
+
580
+ const result = await leaveOrganization({ organization_id: VALID_UUID }, ctx);
581
+
582
+ expect(result.isError).toBe(true);
583
+ expect(result.result).toMatchObject({ error: 'Failed to leave organization' });
584
+ });
585
+ });
586
+
587
+ // ============================================================================
588
+ // shareProjectWithOrg Tests
589
+ // ============================================================================
590
+
591
+ describe('shareProjectWithOrg', () => {
592
+ beforeEach(() => vi.clearAllMocks());
593
+
594
+ it('should throw error for missing project_id', async () => {
595
+ const ctx = createMockContext();
596
+ await expect(
597
+ shareProjectWithOrg({ organization_id: VALID_UUID }, ctx)
598
+ ).rejects.toThrow(ValidationError);
599
+ });
600
+
601
+ it('should throw error for missing organization_id', async () => {
602
+ const ctx = createMockContext();
603
+ await expect(
604
+ shareProjectWithOrg({ project_id: VALID_UUID }, ctx)
605
+ ).rejects.toThrow(ValidationError);
606
+ });
607
+
608
+ it('should return error when project not found or not owned', async () => {
609
+ mockApiClient.shareProjectWithOrg.mockResolvedValue({
610
+ ok: false,
611
+ error: 'Project not found or you are not the owner',
612
+ });
613
+ const ctx = createMockContext();
614
+
615
+ const result = await shareProjectWithOrg({ project_id: VALID_UUID, organization_id: OTHER_UUID }, ctx);
616
+
617
+ expect(result.isError).toBe(true);
618
+ expect(result.result).toMatchObject({ error: 'Project not found or you are not the owner' });
619
+ });
620
+
621
+ it('should share project successfully with default permission', async () => {
622
+ mockApiClient.shareProjectWithOrg.mockResolvedValue({
623
+ ok: true,
624
+ data: { success: true, share_id: 'share-1', permission: 'read' },
625
+ });
626
+ const ctx = createMockContext();
627
+
628
+ const result = await shareProjectWithOrg({
629
+ project_id: VALID_UUID,
630
+ organization_id: OTHER_UUID,
631
+ }, ctx);
632
+
633
+ expect(result.result).toMatchObject({ success: true });
634
+ });
635
+
636
+ it('should return error when share already exists', async () => {
637
+ mockApiClient.shareProjectWithOrg.mockResolvedValue({
638
+ ok: false,
639
+ error: 'Project is already shared with this organization',
640
+ });
641
+ const ctx = createMockContext();
642
+
643
+ const result = await shareProjectWithOrg({ project_id: VALID_UUID, organization_id: OTHER_UUID }, ctx);
644
+
645
+ expect(result.isError).toBe(true);
646
+ expect(result.result).toMatchObject({ error: 'Project is already shared with this organization' });
647
+ });
648
+ });
649
+
650
+ // ============================================================================
651
+ // updateProjectShare Tests
652
+ // ============================================================================
653
+
654
+ describe('updateProjectShare', () => {
655
+ beforeEach(() => vi.clearAllMocks());
656
+
657
+ it('should throw error for missing project_id', async () => {
658
+ const ctx = createMockContext();
659
+ await expect(
660
+ updateProjectShare({ organization_id: VALID_UUID, permission: 'write' }, ctx)
661
+ ).rejects.toThrow(ValidationError);
662
+ });
663
+
664
+ it('should throw error for missing organization_id', async () => {
665
+ const ctx = createMockContext();
666
+ await expect(
667
+ updateProjectShare({ project_id: VALID_UUID, permission: 'write' }, ctx)
668
+ ).rejects.toThrow(ValidationError);
669
+ });
670
+
671
+ it('should throw error for missing permission', async () => {
672
+ const ctx = createMockContext();
673
+ await expect(
674
+ updateProjectShare({ project_id: VALID_UUID, organization_id: OTHER_UUID }, ctx)
675
+ ).rejects.toThrow(ValidationError);
676
+ });
677
+
678
+ it('should update share permission successfully', async () => {
679
+ mockApiClient.updateProjectShare.mockResolvedValue({
680
+ ok: true,
681
+ data: { success: true, share_id: 'share-1', permission: 'write' },
682
+ });
683
+ const ctx = createMockContext();
684
+
685
+ const result = await updateProjectShare({
686
+ project_id: VALID_UUID,
687
+ organization_id: OTHER_UUID,
688
+ permission: 'write',
689
+ }, ctx);
690
+
691
+ expect(result.result).toMatchObject({ success: true });
692
+ });
693
+
694
+ it('should return error when update fails', async () => {
695
+ mockApiClient.updateProjectShare.mockResolvedValue({
696
+ ok: false,
697
+ error: 'Failed to update share',
698
+ });
699
+ const ctx = createMockContext();
700
+
701
+ const result = await updateProjectShare({
702
+ project_id: VALID_UUID,
703
+ organization_id: OTHER_UUID,
704
+ permission: 'write',
705
+ }, ctx);
706
+
707
+ expect(result.isError).toBe(true);
708
+ expect(result.result).toMatchObject({ error: 'Failed to update share' });
709
+ });
710
+ });
711
+
712
+ // ============================================================================
713
+ // unshareProject Tests
714
+ // ============================================================================
715
+
716
+ describe('unshareProject', () => {
717
+ beforeEach(() => vi.clearAllMocks());
718
+
719
+ it('should throw error for missing project_id', async () => {
720
+ const ctx = createMockContext();
721
+ await expect(
722
+ unshareProject({ organization_id: VALID_UUID }, ctx)
723
+ ).rejects.toThrow(ValidationError);
724
+ });
725
+
726
+ it('should throw error for missing organization_id', async () => {
727
+ const ctx = createMockContext();
728
+ await expect(
729
+ unshareProject({ project_id: VALID_UUID }, ctx)
730
+ ).rejects.toThrow(ValidationError);
731
+ });
732
+
733
+ it('should unshare project successfully', async () => {
734
+ mockApiClient.unshareProject.mockResolvedValue({
735
+ ok: true,
736
+ data: {
737
+ success: true,
738
+ message: 'Project share removed. Org members can no longer access this project.',
739
+ },
740
+ });
741
+ const ctx = createMockContext();
742
+
743
+ const result = await unshareProject({
744
+ project_id: VALID_UUID,
745
+ organization_id: OTHER_UUID,
746
+ }, ctx);
747
+
748
+ expect(result.result).toMatchObject({
749
+ success: true,
750
+ message: 'Project share removed. Org members can no longer access this project.',
751
+ });
752
+ });
753
+
754
+ it('should return error when delete fails', async () => {
755
+ mockApiClient.unshareProject.mockResolvedValue({
756
+ ok: false,
757
+ error: 'Failed to unshare project',
758
+ });
759
+ const ctx = createMockContext();
760
+
761
+ const result = await unshareProject({ project_id: VALID_UUID, organization_id: OTHER_UUID }, ctx);
762
+
763
+ expect(result.isError).toBe(true);
764
+ expect(result.result).toMatchObject({ error: 'Failed to unshare project' });
765
+ });
766
+ });
767
+
768
+ // ============================================================================
769
+ // listProjectShares Tests
770
+ // ============================================================================
771
+
772
+ describe('listProjectShares', () => {
773
+ beforeEach(() => vi.clearAllMocks());
774
+
775
+ it('should throw error for missing project_id', async () => {
776
+ const ctx = createMockContext();
777
+ await expect(listProjectShares({}, ctx)).rejects.toThrow(ValidationError);
778
+ });
779
+
780
+ it('should throw error for invalid project_id UUID', async () => {
781
+ const ctx = createMockContext();
782
+ await expect(
783
+ listProjectShares({ project_id: 'invalid' }, ctx)
784
+ ).rejects.toThrow(ValidationError);
785
+ });
786
+
787
+ it('should return empty array when no shares', async () => {
788
+ mockApiClient.listProjectShares.mockResolvedValue({
789
+ ok: true,
790
+ data: { shares: [], count: 0 },
791
+ });
792
+ const ctx = createMockContext();
793
+
794
+ const result = await listProjectShares({ project_id: VALID_UUID }, ctx);
795
+
796
+ expect(result.result).toMatchObject({ shares: [], count: 0 });
797
+ });
798
+
799
+ it('should return shares list', async () => {
800
+ mockApiClient.listProjectShares.mockResolvedValue({
801
+ ok: true,
802
+ data: {
803
+ shares: [{ id: 'share-1', permission: 'read', organization: { name: 'Test Org' } }],
804
+ count: 1,
805
+ },
806
+ });
807
+ const ctx = createMockContext();
808
+
809
+ const result = await listProjectShares({ project_id: VALID_UUID }, ctx);
810
+
811
+ expect(result.result).toMatchObject({ count: 1 });
812
+ });
813
+
814
+ it('should return error when query fails', async () => {
815
+ mockApiClient.listProjectShares.mockResolvedValue({
816
+ ok: false,
817
+ error: 'Failed to list shares',
818
+ });
819
+ const ctx = createMockContext();
820
+
821
+ const result = await listProjectShares({ project_id: VALID_UUID }, ctx);
822
+
823
+ expect(result.isError).toBe(true);
824
+ expect(result.result).toMatchObject({ error: 'Failed to list shares' });
825
+ });
826
+ });