@vibescope/mcp-server 0.4.4 → 0.4.6

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 (204) hide show
  1. package/CHANGELOG.md +84 -84
  2. package/README.md +194 -194
  3. package/dist/api-client/bodies-of-work.d.ts +125 -0
  4. package/dist/api-client/bodies-of-work.js +78 -0
  5. package/dist/api-client/chat.d.ts +26 -0
  6. package/dist/api-client/chat.js +20 -0
  7. package/dist/api-client/connectors.d.ts +104 -0
  8. package/dist/api-client/connectors.js +46 -0
  9. package/dist/api-client/deployment.d.ts +190 -0
  10. package/dist/api-client/deployment.js +113 -0
  11. package/dist/api-client/file-checkouts.d.ts +71 -0
  12. package/dist/api-client/file-checkouts.js +43 -0
  13. package/dist/api-client/git-issues.d.ts +55 -0
  14. package/dist/api-client/git-issues.js +34 -0
  15. package/dist/api-client/index.d.ts +619 -1
  16. package/dist/api-client/index.js +148 -0
  17. package/dist/api-client/organizations.d.ts +101 -0
  18. package/dist/api-client/organizations.js +86 -0
  19. package/dist/api-client/progress.d.ts +61 -0
  20. package/dist/api-client/progress.js +34 -0
  21. package/dist/api-client/project.d.ts +1 -0
  22. package/dist/api-client/requests.d.ts +28 -0
  23. package/dist/api-client/requests.js +28 -0
  24. package/dist/api-client/sprints.d.ts +153 -0
  25. package/dist/api-client/sprints.js +82 -0
  26. package/dist/api-client/subtasks.d.ts +37 -0
  27. package/dist/api-client/subtasks.js +23 -0
  28. package/dist/api-client.d.ts +23 -0
  29. package/dist/api-client.js +15 -0
  30. package/dist/cli-init.js +21 -21
  31. package/dist/cli.js +26 -26
  32. package/dist/handlers/blockers.js +4 -0
  33. package/dist/handlers/chat.d.ts +23 -0
  34. package/dist/handlers/chat.js +84 -0
  35. package/dist/handlers/deployment.d.ts +3 -0
  36. package/dist/handlers/deployment.js +23 -0
  37. package/dist/handlers/discovery.js +13 -0
  38. package/dist/handlers/index.d.ts +1 -0
  39. package/dist/handlers/index.js +3 -0
  40. package/dist/handlers/project.js +4 -2
  41. package/dist/handlers/session.js +7 -0
  42. package/dist/handlers/tasks.js +7 -0
  43. package/dist/handlers/tool-docs.js +1204 -1131
  44. package/dist/index.js +73 -73
  45. package/dist/templates/agent-guidelines.d.ts +1 -1
  46. package/dist/templates/agent-guidelines.js +205 -187
  47. package/dist/templates/help-content.js +1621 -1621
  48. package/dist/tools/bodies-of-work.js +6 -6
  49. package/dist/tools/chat.d.ts +1 -0
  50. package/dist/tools/chat.js +24 -0
  51. package/dist/tools/cloud-agents.js +22 -22
  52. package/dist/tools/deployment.js +13 -0
  53. package/dist/tools/features.d.ts +13 -0
  54. package/dist/tools/features.js +151 -0
  55. package/dist/tools/index.d.ts +3 -1
  56. package/dist/tools/index.js +4 -1
  57. package/dist/tools/milestones.js +2 -2
  58. package/dist/tools/project.js +4 -0
  59. package/dist/tools/requests.js +1 -1
  60. package/dist/tools/session.js +11 -11
  61. package/dist/tools/sprints.js +9 -9
  62. package/dist/tools/tasks.js +35 -35
  63. package/dist/tools/worktrees.js +14 -14
  64. package/dist/tools.d.ts +2 -0
  65. package/dist/tools.js +3602 -0
  66. package/dist/utils.js +11 -11
  67. package/docs/TOOLS.md +2663 -2545
  68. package/package.json +53 -53
  69. package/scripts/generate-docs.ts +212 -212
  70. package/scripts/version-bump.ts +203 -203
  71. package/src/api-client/blockers.ts +86 -86
  72. package/src/api-client/bodies-of-work.ts +194 -0
  73. package/src/api-client/chat.ts +50 -0
  74. package/src/api-client/connectors.ts +152 -0
  75. package/src/api-client/cost.ts +185 -185
  76. package/src/api-client/decisions.ts +87 -87
  77. package/src/api-client/deployment.ts +313 -0
  78. package/src/api-client/discovery.ts +81 -81
  79. package/src/api-client/fallback.ts +52 -52
  80. package/src/api-client/file-checkouts.ts +115 -0
  81. package/src/api-client/findings.ts +100 -100
  82. package/src/api-client/git-issues.ts +88 -0
  83. package/src/api-client/ideas.ts +112 -112
  84. package/src/api-client/index.ts +592 -426
  85. package/src/api-client/milestones.ts +83 -83
  86. package/src/api-client/organizations.ts +185 -0
  87. package/src/api-client/progress.ts +94 -0
  88. package/src/api-client/project.ts +180 -179
  89. package/src/api-client/requests.ts +54 -0
  90. package/src/api-client/session.ts +220 -220
  91. package/src/api-client/sprints.ts +227 -0
  92. package/src/api-client/subtasks.ts +57 -0
  93. package/src/api-client/tasks.ts +450 -450
  94. package/src/api-client/types.ts +32 -32
  95. package/src/api-client/validation.ts +60 -60
  96. package/src/api-client/worktrees.ts +53 -53
  97. package/src/api-client.test.ts +847 -850
  98. package/src/api-client.ts +2707 -2672
  99. package/src/cli-init.ts +557 -557
  100. package/src/cli.test.ts +284 -284
  101. package/src/cli.ts +204 -204
  102. package/src/handlers/__test-setup__.ts +240 -236
  103. package/src/handlers/__test-utils__.ts +89 -89
  104. package/src/handlers/blockers.test.ts +468 -468
  105. package/src/handlers/blockers.ts +172 -163
  106. package/src/handlers/bodies-of-work.test.ts +704 -704
  107. package/src/handlers/bodies-of-work.ts +526 -526
  108. package/src/handlers/chat.test.ts +185 -0
  109. package/src/handlers/chat.ts +101 -0
  110. package/src/handlers/cloud-agents.test.ts +438 -438
  111. package/src/handlers/cloud-agents.ts +156 -156
  112. package/src/handlers/connectors.test.ts +834 -834
  113. package/src/handlers/connectors.ts +229 -229
  114. package/src/handlers/cost.test.ts +462 -462
  115. package/src/handlers/cost.ts +285 -285
  116. package/src/handlers/decisions.test.ts +382 -382
  117. package/src/handlers/decisions.ts +153 -153
  118. package/src/handlers/deployment.test.ts +551 -551
  119. package/src/handlers/deployment.ts +570 -541
  120. package/src/handlers/discovery.test.ts +206 -206
  121. package/src/handlers/discovery.ts +427 -414
  122. package/src/handlers/fallback.test.ts +537 -537
  123. package/src/handlers/fallback.ts +194 -194
  124. package/src/handlers/file-checkouts.test.ts +750 -750
  125. package/src/handlers/file-checkouts.ts +185 -185
  126. package/src/handlers/findings.test.ts +633 -633
  127. package/src/handlers/findings.ts +239 -239
  128. package/src/handlers/git-issues.test.ts +631 -631
  129. package/src/handlers/git-issues.ts +136 -136
  130. package/src/handlers/ideas.test.ts +644 -644
  131. package/src/handlers/ideas.ts +207 -207
  132. package/src/handlers/index.ts +93 -90
  133. package/src/handlers/milestones.test.ts +475 -475
  134. package/src/handlers/milestones.ts +180 -180
  135. package/src/handlers/organizations.test.ts +826 -826
  136. package/src/handlers/organizations.ts +315 -315
  137. package/src/handlers/progress.test.ts +269 -269
  138. package/src/handlers/progress.ts +77 -77
  139. package/src/handlers/project.test.ts +546 -546
  140. package/src/handlers/project.ts +242 -239
  141. package/src/handlers/requests.test.ts +303 -303
  142. package/src/handlers/requests.ts +99 -99
  143. package/src/handlers/roles.test.ts +305 -305
  144. package/src/handlers/roles.ts +219 -219
  145. package/src/handlers/session.test.ts +998 -998
  146. package/src/handlers/session.ts +1105 -1093
  147. package/src/handlers/sprints.test.ts +732 -732
  148. package/src/handlers/sprints.ts +537 -537
  149. package/src/handlers/tasks.test.ts +931 -931
  150. package/src/handlers/tasks.ts +1133 -1121
  151. package/src/handlers/tool-categories.test.ts +66 -66
  152. package/src/handlers/tool-docs.test.ts +511 -511
  153. package/src/handlers/tool-docs.ts +1571 -1491
  154. package/src/handlers/types.test.ts +259 -259
  155. package/src/handlers/types.ts +176 -176
  156. package/src/handlers/validation.test.ts +582 -582
  157. package/src/handlers/validation.ts +164 -164
  158. package/src/handlers/version.ts +63 -63
  159. package/src/index.test.ts +674 -674
  160. package/src/index.ts +807 -807
  161. package/src/setup.test.ts +233 -233
  162. package/src/setup.ts +404 -404
  163. package/src/templates/agent-guidelines.ts +233 -215
  164. package/src/templates/help-content.ts +1751 -1751
  165. package/src/token-tracking.test.ts +463 -463
  166. package/src/token-tracking.ts +167 -167
  167. package/src/tools/blockers.ts +122 -122
  168. package/src/tools/bodies-of-work.ts +283 -283
  169. package/src/tools/chat.ts +72 -46
  170. package/src/tools/cloud-agents.ts +101 -101
  171. package/src/tools/connectors.ts +191 -191
  172. package/src/tools/cost.ts +111 -111
  173. package/src/tools/decisions.ts +111 -111
  174. package/src/tools/deployment.ts +455 -442
  175. package/src/tools/discovery.ts +76 -76
  176. package/src/tools/fallback.ts +111 -111
  177. package/src/tools/features.ts +154 -0
  178. package/src/tools/file-checkouts.ts +145 -145
  179. package/src/tools/findings.ts +101 -101
  180. package/src/tools/git-issues.ts +130 -130
  181. package/src/tools/ideas.ts +162 -162
  182. package/src/tools/index.ts +141 -137
  183. package/src/tools/milestones.ts +118 -118
  184. package/src/tools/organizations.ts +224 -224
  185. package/src/tools/progress.ts +73 -73
  186. package/src/tools/project.ts +206 -202
  187. package/src/tools/requests.ts +68 -68
  188. package/src/tools/roles.ts +112 -112
  189. package/src/tools/session.ts +181 -181
  190. package/src/tools/sprints.ts +298 -298
  191. package/src/tools/tasks.ts +550 -550
  192. package/src/tools/tools.test.ts +222 -222
  193. package/src/tools/types.ts +9 -9
  194. package/src/tools/validation.ts +75 -75
  195. package/src/tools/version.ts +34 -34
  196. package/src/tools/worktrees.ts +66 -66
  197. package/src/tools.test.ts +416 -416
  198. package/src/utils.test.ts +1014 -1014
  199. package/src/utils.ts +586 -586
  200. package/src/validators.test.ts +223 -223
  201. package/src/validators.ts +249 -249
  202. package/src/version.ts +109 -109
  203. package/tsconfig.json +16 -16
  204. 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
+ });