@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,850 +1,847 @@
1
- /**
2
- * API Client Tests
3
- *
4
- * Tests for the VibescopeApiClient class that handles all HTTP communication
5
- * with the Vibescope API.
6
- */
7
-
8
- import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
9
-
10
- // Unmock the api-client module so we test the real implementation
11
- vi.unmock('./api-client.js');
12
- vi.unmock('../api-client.js');
13
-
14
- // Import after unmocking
15
- import { VibescopeApiClient } from './api-client.js';
16
-
17
- // ============================================================================
18
- // Test Setup
19
- // ============================================================================
20
-
21
- // Mock fetch globally
22
- const mockFetch = vi.fn();
23
- global.fetch = mockFetch;
24
-
25
- // Helper to create mock response
26
- function createMockResponse(data: unknown, ok = true, status = 200, headers?: Record<string, string>) {
27
- return {
28
- ok,
29
- status,
30
- json: vi.fn().mockResolvedValue(data),
31
- headers: {
32
- get: (name: string) => headers?.[name] ?? null,
33
- },
34
- };
35
- }
36
-
37
- // Helper to create network error
38
- function createNetworkError(message: string) {
39
- return new Error(message);
40
- }
41
-
42
- // Fast retry config for tests (1ms delays instead of 1000ms)
43
- const FAST_RETRY_CONFIG = {
44
- maxRetries: 3,
45
- baseDelayMs: 1,
46
- maxDelayMs: 10,
47
- retryStatusCodes: [429, 503, 504],
48
- };
49
-
50
- describe('VibescopeApiClient', () => {
51
- let client: VibescopeApiClient;
52
-
53
- beforeEach(() => {
54
- vi.clearAllMocks();
55
- // Use fast retry config so tests don't take forever
56
- client = new VibescopeApiClient({ apiKey: 'test-api-key', retry: FAST_RETRY_CONFIG });
57
- });
58
-
59
- afterEach(() => {
60
- vi.resetAllMocks();
61
- });
62
-
63
- // ============================================================================
64
- // Constructor Tests
65
- // ============================================================================
66
-
67
- describe('constructor', () => {
68
- it('should use default API URL when not provided', () => {
69
- const c = new VibescopeApiClient({ apiKey: 'test-key' });
70
- mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
71
-
72
- // Trigger a request to verify the URL
73
- c.validateAuth();
74
-
75
- expect(mockFetch).toHaveBeenCalledWith(
76
- expect.stringContaining('https://vibescope.dev'),
77
- expect.any(Object)
78
- );
79
- });
80
-
81
- it('should use custom baseUrl when provided', () => {
82
- const c = new VibescopeApiClient({
83
- apiKey: 'test-key',
84
- baseUrl: 'https://custom.api.com',
85
- });
86
- mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
87
-
88
- c.validateAuth();
89
-
90
- expect(mockFetch).toHaveBeenCalledWith(
91
- expect.stringContaining('https://custom.api.com'),
92
- expect.any(Object)
93
- );
94
- });
95
-
96
- it('should use VIBESCOPE_API_URL env var when set', () => {
97
- const originalEnv = process.env.VIBESCOPE_API_URL;
98
- process.env.VIBESCOPE_API_URL = 'https://env-api.com';
99
-
100
- const c = new VibescopeApiClient({ apiKey: 'test-key' });
101
- mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
102
-
103
- c.validateAuth();
104
-
105
- expect(mockFetch).toHaveBeenCalledWith(
106
- expect.stringContaining('https://env-api.com'),
107
- expect.any(Object)
108
- );
109
-
110
- // Restore
111
- if (originalEnv !== undefined) {
112
- process.env.VIBESCOPE_API_URL = originalEnv;
113
- } else {
114
- delete process.env.VIBESCOPE_API_URL;
115
- }
116
- });
117
- });
118
-
119
- // ============================================================================
120
- // Request Method Tests (via validateAuth)
121
- // ============================================================================
122
-
123
- describe('request method', () => {
124
- it('should send correct headers', async () => {
125
- mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
126
-
127
- await client.validateAuth();
128
-
129
- expect(mockFetch).toHaveBeenCalledWith(
130
- expect.any(String),
131
- expect.objectContaining({
132
- method: 'POST',
133
- headers: {
134
- 'Content-Type': 'application/json',
135
- 'X-API-Key': 'test-api-key',
136
- },
137
- })
138
- );
139
- });
140
-
141
- it('should return success response on 2xx status', async () => {
142
- const responseData = { valid: true, user_id: 'user-123' };
143
- mockFetch.mockResolvedValue(createMockResponse(responseData, true, 200));
144
-
145
- const result = await client.validateAuth();
146
-
147
- expect(result).toEqual({
148
- ok: true,
149
- status: 200,
150
- data: responseData,
151
- });
152
- });
153
-
154
- it('should return error response on 4xx status', async () => {
155
- const errorData = { error: 'Invalid API key' };
156
- mockFetch.mockResolvedValue(createMockResponse(errorData, false, 401));
157
-
158
- const result = await client.validateAuth();
159
-
160
- expect(result).toEqual({
161
- ok: false,
162
- status: 401,
163
- error: 'Invalid API key',
164
- data: errorData,
165
- });
166
- });
167
-
168
- it('should return error response on 5xx status', async () => {
169
- const errorData = { error: 'Internal server error' };
170
- mockFetch.mockResolvedValue(createMockResponse(errorData, false, 500));
171
-
172
- const result = await client.validateAuth();
173
-
174
- expect(result).toEqual({
175
- ok: false,
176
- status: 500,
177
- error: 'Internal server error',
178
- data: errorData,
179
- });
180
- });
181
-
182
- it('should handle missing error field in error response', async () => {
183
- const errorData = { message: 'Something went wrong' };
184
- mockFetch.mockResolvedValue(createMockResponse(errorData, false, 400));
185
-
186
- const result = await client.validateAuth();
187
-
188
- expect(result).toEqual({
189
- ok: false,
190
- status: 400,
191
- error: 'HTTP 400',
192
- data: errorData,
193
- });
194
- });
195
-
196
- it('should handle network errors after retries', async () => {
197
- mockFetch.mockRejectedValue(createNetworkError('Network request failed'));
198
-
199
- const result = await client.validateAuth();
200
-
201
- // Network errors are retried, so 4 attempts total (1 initial + 3 retries)
202
- expect(mockFetch).toHaveBeenCalledTimes(4);
203
- expect(result).toEqual({
204
- ok: false,
205
- status: 0,
206
- error: 'Network request failed',
207
- });
208
- }, 30000);
209
-
210
- it('should handle non-Error exceptions after retries', async () => {
211
- mockFetch.mockRejectedValue('String error');
212
-
213
- const result = await client.validateAuth();
214
-
215
- // Non-Error exceptions are converted to Error('Network error') and retried
216
- expect(mockFetch).toHaveBeenCalledTimes(4);
217
- expect(result).toEqual({
218
- ok: false,
219
- status: 0,
220
- error: 'Network error',
221
- });
222
- }, 30000);
223
- });
224
-
225
- // ============================================================================
226
- // Auth Endpoints
227
- // ============================================================================
228
-
229
- describe('validateAuth', () => {
230
- it('should call correct endpoint', async () => {
231
- mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
232
-
233
- await client.validateAuth();
234
-
235
- expect(mockFetch).toHaveBeenCalledWith(
236
- expect.stringContaining('/api/mcp/auth/validate'),
237
- expect.objectContaining({
238
- method: 'POST',
239
- body: JSON.stringify({ api_key: 'test-api-key' }),
240
- })
241
- );
242
- });
243
- });
244
-
245
- // ============================================================================
246
- // Session Endpoints
247
- // ============================================================================
248
-
249
- describe('startSession', () => {
250
- it('should call correct endpoint with params', async () => {
251
- mockFetch.mockResolvedValue(
252
- createMockResponse({ session_started: true, session_id: 'session-123' })
253
- );
254
-
255
- await client.startSession({
256
- project_id: 'proj-123',
257
- mode: 'lite',
258
- model: 'opus',
259
- });
260
-
261
- expect(mockFetch).toHaveBeenCalledWith(
262
- expect.stringContaining('/api/mcp/sessions/start'),
263
- expect.objectContaining({
264
- method: 'POST',
265
- body: JSON.stringify({
266
- project_id: 'proj-123',
267
- mode: 'lite',
268
- model: 'opus',
269
- }),
270
- })
271
- );
272
- });
273
-
274
- it('should support git_url param', async () => {
275
- mockFetch.mockResolvedValue(
276
- createMockResponse({ session_started: true })
277
- );
278
-
279
- await client.startSession({
280
- git_url: 'https://github.com/org/repo',
281
- });
282
-
283
- expect(mockFetch).toHaveBeenCalledWith(
284
- expect.any(String),
285
- expect.objectContaining({
286
- body: JSON.stringify({
287
- git_url: 'https://github.com/org/repo',
288
- }),
289
- })
290
- );
291
- });
292
- });
293
-
294
- describe('heartbeat', () => {
295
- it('should call correct endpoint', async () => {
296
- mockFetch.mockResolvedValue(
297
- createMockResponse({ timestamp: '2026-01-14T10:00:00Z' })
298
- );
299
-
300
- await client.heartbeat('session-123');
301
-
302
- expect(mockFetch).toHaveBeenCalledWith(
303
- expect.stringContaining('/api/mcp/sessions/heartbeat'),
304
- expect.objectContaining({
305
- method: 'POST',
306
- body: JSON.stringify({ session_id: 'session-123' }),
307
- })
308
- );
309
- });
310
-
311
- it('should pass worktree path option', async () => {
312
- mockFetch.mockResolvedValue(
313
- createMockResponse({ timestamp: '2026-01-14T10:00:00Z' })
314
- );
315
-
316
- await client.heartbeat('session-123', {
317
- current_worktree_path: '../project-task-abc',
318
- });
319
-
320
- expect(mockFetch).toHaveBeenCalledWith(
321
- expect.any(String),
322
- expect.objectContaining({
323
- body: JSON.stringify({
324
- session_id: 'session-123',
325
- current_worktree_path: '../project-task-abc',
326
- }),
327
- })
328
- );
329
- });
330
- });
331
-
332
- describe('endSession', () => {
333
- it('should call correct endpoint', async () => {
334
- mockFetch.mockResolvedValue(
335
- createMockResponse({ success: true })
336
- );
337
-
338
- await client.endSession('session-123');
339
-
340
- expect(mockFetch).toHaveBeenCalledWith(
341
- expect.stringContaining('/api/mcp/sessions/end'),
342
- expect.objectContaining({
343
- method: 'POST',
344
- body: JSON.stringify({ session_id: 'session-123' }),
345
- })
346
- );
347
- });
348
- });
349
-
350
- // ============================================================================
351
- // Project Endpoints
352
- // ============================================================================
353
-
354
- describe('listProjects', () => {
355
- it('should call correct endpoint', async () => {
356
- mockFetch.mockResolvedValue(
357
- createMockResponse({ projects: [] })
358
- );
359
-
360
- await client.listProjects();
361
-
362
- expect(mockFetch).toHaveBeenCalledWith(
363
- expect.stringContaining('/api/mcp/projects'),
364
- expect.objectContaining({ method: 'GET' })
365
- );
366
- });
367
- });
368
-
369
- describe('getProject', () => {
370
- it('should call correct endpoint with project ID', async () => {
371
- mockFetch.mockResolvedValue(
372
- createMockResponse({ project: { id: 'proj-123', name: 'Test' } })
373
- );
374
-
375
- await client.getProject('proj-123');
376
-
377
- expect(mockFetch).toHaveBeenCalledWith(
378
- expect.stringContaining('/api/mcp/projects/proj-123'),
379
- expect.objectContaining({ method: 'GET' })
380
- );
381
- });
382
-
383
- it('should include git_url param when provided', async () => {
384
- mockFetch.mockResolvedValue(
385
- createMockResponse({ project: { id: 'proj-123' } })
386
- );
387
-
388
- await client.getProject('proj-123', 'https://github.com/org/repo');
389
-
390
- expect(mockFetch).toHaveBeenCalledWith(
391
- expect.stringContaining('git_url=https'),
392
- expect.any(Object)
393
- );
394
- });
395
- });
396
-
397
- describe('createProject', () => {
398
- it('should call correct endpoint', async () => {
399
- mockFetch.mockResolvedValue(
400
- createMockResponse({ project: { id: 'new-proj' } })
401
- );
402
-
403
- await client.createProject({
404
- name: 'New Project',
405
- description: 'Test description',
406
- });
407
-
408
- expect(mockFetch).toHaveBeenCalledWith(
409
- expect.stringContaining('/api/mcp/projects'),
410
- expect.objectContaining({
411
- method: 'POST',
412
- body: JSON.stringify({
413
- name: 'New Project',
414
- description: 'Test description',
415
- }),
416
- })
417
- );
418
- });
419
- });
420
-
421
- describe('updateProject', () => {
422
- it('should call correct endpoint', async () => {
423
- mockFetch.mockResolvedValue(
424
- createMockResponse({ success: true })
425
- );
426
-
427
- await client.updateProject('proj-123', {
428
- name: 'Updated Name',
429
- status: 'active',
430
- });
431
-
432
- expect(mockFetch).toHaveBeenCalledWith(
433
- expect.stringContaining('/api/mcp/projects/proj-123'),
434
- expect.objectContaining({
435
- method: 'PATCH',
436
- body: JSON.stringify({
437
- name: 'Updated Name',
438
- status: 'active',
439
- }),
440
- })
441
- );
442
- });
443
- });
444
-
445
- // ============================================================================
446
- // Task Endpoints
447
- // ============================================================================
448
-
449
- describe('getTasks', () => {
450
- it('should call correct endpoint', async () => {
451
- mockFetch.mockResolvedValue(
452
- createMockResponse({ tasks: [] })
453
- );
454
-
455
- await client.getTasks('proj-123');
456
-
457
- expect(mockFetch).toHaveBeenCalledWith(
458
- expect.stringContaining('/api/mcp/projects/proj-123/tasks'),
459
- expect.objectContaining({ method: 'GET' })
460
- );
461
- });
462
-
463
- it('should include filter params', async () => {
464
- mockFetch.mockResolvedValue(
465
- createMockResponse({ tasks: [] })
466
- );
467
-
468
- await client.getTasks('proj-123', {
469
- status: 'in_progress',
470
- limit: 10,
471
- });
472
-
473
- const url = mockFetch.mock.calls[0][0] as string;
474
- expect(url).toContain('status=in_progress');
475
- expect(url).toContain('limit=10');
476
- });
477
- });
478
-
479
- describe('createTask', () => {
480
- it('should call correct endpoint', async () => {
481
- mockFetch.mockResolvedValue(
482
- createMockResponse({ task: { id: 'task-123' } })
483
- );
484
-
485
- await client.createTask('proj-123', {
486
- title: 'New Task',
487
- priority: 2,
488
- });
489
-
490
- expect(mockFetch).toHaveBeenCalledWith(
491
- expect.stringContaining('/api/mcp/projects/proj-123/tasks'),
492
- expect.objectContaining({
493
- method: 'POST',
494
- body: JSON.stringify({
495
- title: 'New Task',
496
- priority: 2,
497
- }),
498
- })
499
- );
500
- });
501
- });
502
-
503
- describe('updateTask', () => {
504
- it('should call correct endpoint', async () => {
505
- mockFetch.mockResolvedValue(
506
- createMockResponse({ success: true })
507
- );
508
-
509
- await client.updateTask('task-123', {
510
- status: 'in_progress',
511
- progress_percentage: 50,
512
- });
513
-
514
- expect(mockFetch).toHaveBeenCalledWith(
515
- expect.stringContaining('/api/mcp/tasks/task-123'),
516
- expect.objectContaining({
517
- method: 'PATCH',
518
- body: JSON.stringify({
519
- status: 'in_progress',
520
- progress_percentage: 50,
521
- }),
522
- })
523
- );
524
- });
525
- });
526
-
527
- describe('completeTask', () => {
528
- it('should call proxy endpoint', async () => {
529
- mockFetch.mockResolvedValue(
530
- createMockResponse({ success: true })
531
- );
532
-
533
- await client.completeTask('task-123', {
534
- summary: 'Task completed successfully',
535
- });
536
-
537
- // completeTask uses proxy endpoint for consistency (direct endpoint had Vercel routing issues)
538
- expect(mockFetch).toHaveBeenCalledWith(
539
- expect.stringContaining('/api/mcp/proxy'),
540
- expect.objectContaining({
541
- method: 'POST',
542
- body: JSON.stringify({
543
- operation: 'complete_task',
544
- args: {
545
- task_id: 'task-123',
546
- summary: 'Task completed successfully',
547
- },
548
- }),
549
- })
550
- );
551
- });
552
- });
553
-
554
- describe('deleteTask', () => {
555
- it('should call correct endpoint', async () => {
556
- mockFetch.mockResolvedValue(
557
- createMockResponse({ success: true })
558
- );
559
-
560
- await client.deleteTask('task-123');
561
-
562
- expect(mockFetch).toHaveBeenCalledWith(
563
- expect.stringContaining('/api/mcp/tasks/task-123'),
564
- expect.objectContaining({ method: 'DELETE' })
565
- );
566
- });
567
- });
568
-
569
- describe('getNextTask', () => {
570
- it('should call correct endpoint', async () => {
571
- mockFetch.mockResolvedValue(
572
- createMockResponse({ task: { id: 'task-123', title: 'Next task' } })
573
- );
574
-
575
- await client.getNextTask('proj-123');
576
-
577
- expect(mockFetch).toHaveBeenCalledWith(
578
- expect.stringContaining('/api/mcp/projects/proj-123/next-task'),
579
- expect.objectContaining({ method: 'GET' })
580
- );
581
- });
582
-
583
- it('should include session_id when provided', async () => {
584
- mockFetch.mockResolvedValue(
585
- createMockResponse({ task: null })
586
- );
587
-
588
- await client.getNextTask('proj-123', 'session-456');
589
-
590
- const url = mockFetch.mock.calls[0][0] as string;
591
- expect(url).toContain('session_id=session-456');
592
- });
593
- });
594
-
595
- // ============================================================================
596
- // Blocker Endpoints
597
- // ============================================================================
598
-
599
- describe('getBlockers', () => {
600
- it('should call correct endpoint', async () => {
601
- mockFetch.mockResolvedValue(
602
- createMockResponse({ blockers: [] })
603
- );
604
-
605
- await client.getBlockers('proj-123');
606
-
607
- expect(mockFetch).toHaveBeenCalledWith(
608
- expect.stringContaining('/api/mcp/proxy'),
609
- expect.objectContaining({ method: 'POST' })
610
- );
611
- });
612
- });
613
-
614
- describe('addBlocker', () => {
615
- it('should call correct endpoint', async () => {
616
- mockFetch.mockResolvedValue(
617
- createMockResponse({ blocker_id: 'blocker-123' })
618
- );
619
-
620
- await client.addBlocker('proj-123', 'Blocked by dependency');
621
-
622
- expect(mockFetch).toHaveBeenCalledWith(
623
- expect.stringContaining('/api/mcp/proxy'),
624
- expect.objectContaining({ method: 'POST' })
625
- );
626
- });
627
- });
628
-
629
- // ============================================================================
630
- // Validation Endpoints
631
- // ============================================================================
632
-
633
- describe('getTasksAwaitingValidation', () => {
634
- it('should call correct endpoint', async () => {
635
- mockFetch.mockResolvedValue(
636
- createMockResponse({ tasks: [] })
637
- );
638
-
639
- await client.getTasksAwaitingValidation('proj-123');
640
-
641
- expect(mockFetch).toHaveBeenCalledWith(
642
- expect.stringContaining('/api/mcp/proxy'),
643
- expect.objectContaining({ method: 'POST' })
644
- );
645
- });
646
- });
647
-
648
- describe('validateTask', () => {
649
- it('should call correct endpoint', async () => {
650
- mockFetch.mockResolvedValue(
651
- createMockResponse({ success: true })
652
- );
653
-
654
- await client.validateTask('task-123', {
655
- approved: true,
656
- validation_notes: 'All tests pass',
657
- });
658
-
659
- expect(mockFetch).toHaveBeenCalledWith(
660
- expect.stringContaining('/api/mcp/proxy'),
661
- expect.objectContaining({ method: 'POST' })
662
- );
663
- });
664
- });
665
-
666
- // ============================================================================
667
- // Proxy Endpoint
668
- // ============================================================================
669
-
670
- describe('proxy', () => {
671
- it('should call proxy endpoint with operation', async () => {
672
- mockFetch.mockResolvedValue(
673
- createMockResponse({ result: 'success' })
674
- );
675
-
676
- await client.proxy('custom_operation', { param1: 'value1' });
677
-
678
- expect(mockFetch).toHaveBeenCalledWith(
679
- expect.stringContaining('/api/mcp/proxy'),
680
- expect.objectContaining({
681
- method: 'POST',
682
- body: expect.stringContaining('custom_operation'),
683
- })
684
- );
685
- });
686
-
687
- it('should pass session context when provided', async () => {
688
- mockFetch.mockResolvedValue(
689
- createMockResponse({ result: 'success' })
690
- );
691
-
692
- await client.proxy(
693
- 'operation',
694
- { arg: 'value' },
695
- { sessionId: 'session-123', persona: 'Wave' }
696
- );
697
-
698
- expect(mockFetch).toHaveBeenCalledWith(
699
- expect.any(String),
700
- expect.objectContaining({
701
- body: expect.stringContaining('session-123'),
702
- })
703
- );
704
- });
705
- });
706
-
707
- // ============================================================================
708
- // Error Handling Edge Cases
709
- // ============================================================================
710
-
711
- describe('error handling edge cases', () => {
712
- it('should handle JSON parse error in response after retries', async () => {
713
- mockFetch.mockResolvedValue({
714
- ok: true,
715
- status: 200,
716
- json: vi.fn().mockRejectedValue(new SyntaxError('Invalid JSON')),
717
- headers: { get: () => null },
718
- });
719
-
720
- const result = await client.validateAuth();
721
-
722
- // JSON parse errors are treated as network errors and retried
723
- expect(mockFetch).toHaveBeenCalledTimes(4);
724
- expect(result.ok).toBe(false);
725
- expect(result.error).toContain('Invalid JSON');
726
- }, 30000);
727
-
728
- it('should handle timeout errors after retries', async () => {
729
- const timeoutError = new Error('Request timeout');
730
- timeoutError.name = 'AbortError';
731
- mockFetch.mockRejectedValue(timeoutError);
732
-
733
- const result = await client.validateAuth();
734
-
735
- // Timeout errors are retried
736
- expect(mockFetch).toHaveBeenCalledTimes(4);
737
- expect(result).toEqual({
738
- ok: false,
739
- status: 0,
740
- error: 'Request timeout',
741
- });
742
- }, 30000);
743
- });
744
-
745
- // ============================================================================
746
- // Retry Logic Tests
747
- // ============================================================================
748
-
749
- describe('retry logic', () => {
750
- it('should retry on 429 status code', async () => {
751
- // First two calls return 429, third succeeds
752
- mockFetch
753
- .mockResolvedValueOnce(createMockResponse({ error: 'Rate limited' }, false, 429))
754
- .mockResolvedValueOnce(createMockResponse({ error: 'Rate limited' }, false, 429))
755
- .mockResolvedValueOnce(createMockResponse({ valid: true }, true, 200));
756
-
757
- const result = await client.validateAuth();
758
-
759
- expect(mockFetch).toHaveBeenCalledTimes(3);
760
- expect(result.ok).toBe(true);
761
- });
762
-
763
- it('should retry on 503 status code', async () => {
764
- mockFetch
765
- .mockResolvedValueOnce(createMockResponse({ error: 'Service unavailable' }, false, 503))
766
- .mockResolvedValueOnce(createMockResponse({ valid: true }, true, 200));
767
-
768
- const result = await client.validateAuth();
769
-
770
- expect(mockFetch).toHaveBeenCalledTimes(2);
771
- expect(result.ok).toBe(true);
772
- });
773
-
774
- it('should retry on 504 status code', async () => {
775
- mockFetch
776
- .mockResolvedValueOnce(createMockResponse({ error: 'Gateway timeout' }, false, 504))
777
- .mockResolvedValueOnce(createMockResponse({ valid: true }, true, 200));
778
-
779
- const result = await client.validateAuth();
780
-
781
- expect(mockFetch).toHaveBeenCalledTimes(2);
782
- expect(result.ok).toBe(true);
783
- });
784
-
785
- it('should not retry on non-retryable status codes (400)', async () => {
786
- mockFetch.mockResolvedValueOnce(createMockResponse({ error: 'Bad request' }, false, 400));
787
-
788
- const result = await client.validateAuth();
789
-
790
- expect(mockFetch).toHaveBeenCalledTimes(1);
791
- expect(result.ok).toBe(false);
792
- expect(result.status).toBe(400);
793
- });
794
-
795
- it('should not retry on non-retryable status codes (401)', async () => {
796
- mockFetch.mockResolvedValueOnce(createMockResponse({ error: 'Unauthorized' }, false, 401));
797
-
798
- const result = await client.validateAuth();
799
-
800
- expect(mockFetch).toHaveBeenCalledTimes(1);
801
- expect(result.ok).toBe(false);
802
- expect(result.status).toBe(401);
803
- });
804
-
805
- it('should not retry on non-retryable status codes (500)', async () => {
806
- mockFetch.mockResolvedValueOnce(createMockResponse({ error: 'Server error' }, false, 500));
807
-
808
- const result = await client.validateAuth();
809
-
810
- expect(mockFetch).toHaveBeenCalledTimes(1);
811
- expect(result.ok).toBe(false);
812
- expect(result.status).toBe(500);
813
- });
814
-
815
- it('should stop after max retries (3)', async () => {
816
- // All 4 attempts (1 initial + 3 retries) return 429
817
- mockFetch.mockResolvedValue(createMockResponse({ error: 'Rate limited' }, false, 429));
818
-
819
- const result = await client.validateAuth();
820
-
821
- expect(mockFetch).toHaveBeenCalledTimes(4); // 1 initial + 3 retries
822
- expect(result.ok).toBe(false);
823
- expect(result.status).toBe(429);
824
- // API error message takes precedence over generic retry message
825
- expect(result.error).toBe('Rate limited');
826
- }, 30000);
827
-
828
- it('should retry on network errors', async () => {
829
- mockFetch
830
- .mockRejectedValueOnce(new Error('Network error'))
831
- .mockResolvedValueOnce(createMockResponse({ valid: true }, true, 200));
832
-
833
- const result = await client.validateAuth();
834
-
835
- expect(mockFetch).toHaveBeenCalledTimes(2);
836
- expect(result.ok).toBe(true);
837
- }, 10000);
838
-
839
- it('should return last error after all retries exhausted on network failure', async () => {
840
- mockFetch.mockRejectedValue(new Error('Connection refused'));
841
-
842
- const result = await client.validateAuth();
843
-
844
- expect(mockFetch).toHaveBeenCalledTimes(4);
845
- expect(result.ok).toBe(false);
846
- expect(result.status).toBe(0);
847
- expect(result.error).toBe('Connection refused');
848
- }, 30000);
849
- });
850
- });
1
+ /**
2
+ * API Client Tests
3
+ *
4
+ * Tests for the VibescopeApiClient class that handles all HTTP communication
5
+ * with the Vibescope API.
6
+ */
7
+
8
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
9
+
10
+ // Unmock the api-client module so we test the real implementation
11
+ vi.unmock('./api-client.js');
12
+ vi.unmock('../api-client.js');
13
+
14
+ // Import after unmocking
15
+ import { VibescopeApiClient } from './api-client.js';
16
+
17
+ // ============================================================================
18
+ // Test Setup
19
+ // ============================================================================
20
+
21
+ // Mock fetch globally
22
+ const mockFetch = vi.fn();
23
+ global.fetch = mockFetch;
24
+
25
+ // Helper to create mock response
26
+ function createMockResponse(data: unknown, ok = true, status = 200, headers?: Record<string, string>) {
27
+ return {
28
+ ok,
29
+ status,
30
+ json: vi.fn().mockResolvedValue(data),
31
+ headers: {
32
+ get: (name: string) => headers?.[name] ?? null,
33
+ },
34
+ };
35
+ }
36
+
37
+ // Helper to create network error
38
+ function createNetworkError(message: string) {
39
+ return new Error(message);
40
+ }
41
+
42
+ // Fast retry config for tests (1ms delays instead of 1000ms)
43
+ const FAST_RETRY_CONFIG = {
44
+ maxRetries: 3,
45
+ baseDelayMs: 1,
46
+ maxDelayMs: 10,
47
+ retryStatusCodes: [429, 503, 504],
48
+ };
49
+
50
+ describe('VibescopeApiClient', () => {
51
+ let client: VibescopeApiClient;
52
+
53
+ beforeEach(() => {
54
+ vi.clearAllMocks();
55
+ // Use fast retry config so tests don't take forever
56
+ client = new VibescopeApiClient({ apiKey: 'test-api-key', retry: FAST_RETRY_CONFIG });
57
+ });
58
+
59
+ afterEach(() => {
60
+ vi.resetAllMocks();
61
+ });
62
+
63
+ // ============================================================================
64
+ // Constructor Tests
65
+ // ============================================================================
66
+
67
+ describe('constructor', () => {
68
+ it('should use default API URL when not provided', () => {
69
+ const c = new VibescopeApiClient({ apiKey: 'test-key' });
70
+ mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
71
+
72
+ // Trigger a request to verify the URL
73
+ c.validateAuth();
74
+
75
+ expect(mockFetch).toHaveBeenCalledWith(
76
+ expect.stringContaining('https://vibescope.dev'),
77
+ expect.any(Object)
78
+ );
79
+ });
80
+
81
+ it('should use custom baseUrl when provided', () => {
82
+ const c = new VibescopeApiClient({
83
+ apiKey: 'test-key',
84
+ baseUrl: 'https://custom.api.com',
85
+ });
86
+ mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
87
+
88
+ c.validateAuth();
89
+
90
+ expect(mockFetch).toHaveBeenCalledWith(
91
+ expect.stringContaining('https://custom.api.com'),
92
+ expect.any(Object)
93
+ );
94
+ });
95
+
96
+ it('should use VIBESCOPE_API_URL env var when set', () => {
97
+ const originalEnv = process.env.VIBESCOPE_API_URL;
98
+ process.env.VIBESCOPE_API_URL = 'https://env-api.com';
99
+
100
+ const c = new VibescopeApiClient({ apiKey: 'test-key' });
101
+ mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
102
+
103
+ c.validateAuth();
104
+
105
+ expect(mockFetch).toHaveBeenCalledWith(
106
+ expect.stringContaining('https://env-api.com'),
107
+ expect.any(Object)
108
+ );
109
+
110
+ // Restore
111
+ if (originalEnv !== undefined) {
112
+ process.env.VIBESCOPE_API_URL = originalEnv;
113
+ } else {
114
+ delete process.env.VIBESCOPE_API_URL;
115
+ }
116
+ });
117
+ });
118
+
119
+ // ============================================================================
120
+ // Request Method Tests (via validateAuth)
121
+ // ============================================================================
122
+
123
+ describe('request method', () => {
124
+ it('should send correct headers', async () => {
125
+ mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
126
+
127
+ await client.validateAuth();
128
+
129
+ expect(mockFetch).toHaveBeenCalledWith(
130
+ expect.any(String),
131
+ expect.objectContaining({
132
+ method: 'POST',
133
+ headers: {
134
+ 'Content-Type': 'application/json',
135
+ 'X-API-Key': 'test-api-key',
136
+ },
137
+ })
138
+ );
139
+ });
140
+
141
+ it('should return success response on 2xx status', async () => {
142
+ const responseData = { valid: true, user_id: 'user-123' };
143
+ mockFetch.mockResolvedValue(createMockResponse(responseData, true, 200));
144
+
145
+ const result = await client.validateAuth();
146
+
147
+ expect(result).toEqual({
148
+ ok: true,
149
+ status: 200,
150
+ data: responseData,
151
+ });
152
+ });
153
+
154
+ it('should return error response on 4xx status', async () => {
155
+ const errorData = { error: 'Invalid API key' };
156
+ mockFetch.mockResolvedValue(createMockResponse(errorData, false, 401));
157
+
158
+ const result = await client.validateAuth();
159
+
160
+ expect(result).toEqual({
161
+ ok: false,
162
+ status: 401,
163
+ error: 'Invalid API key',
164
+ data: errorData,
165
+ });
166
+ });
167
+
168
+ it('should return error response on 5xx status', async () => {
169
+ const errorData = { error: 'Internal server error' };
170
+ mockFetch.mockResolvedValue(createMockResponse(errorData, false, 500));
171
+
172
+ const result = await client.validateAuth();
173
+
174
+ expect(result).toEqual({
175
+ ok: false,
176
+ status: 500,
177
+ error: 'Internal server error',
178
+ data: errorData,
179
+ });
180
+ });
181
+
182
+ it('should handle missing error field in error response', async () => {
183
+ const errorData = { message: 'Something went wrong' };
184
+ mockFetch.mockResolvedValue(createMockResponse(errorData, false, 400));
185
+
186
+ const result = await client.validateAuth();
187
+
188
+ expect(result).toEqual({
189
+ ok: false,
190
+ status: 400,
191
+ error: 'HTTP 400',
192
+ data: errorData,
193
+ });
194
+ });
195
+
196
+ it('should handle network errors after retries', async () => {
197
+ mockFetch.mockRejectedValue(createNetworkError('Network request failed'));
198
+
199
+ const result = await client.validateAuth();
200
+
201
+ // Network errors are retried, so 4 attempts total (1 initial + 3 retries)
202
+ expect(mockFetch).toHaveBeenCalledTimes(4);
203
+ expect(result).toEqual({
204
+ ok: false,
205
+ status: 0,
206
+ error: 'Network request failed',
207
+ });
208
+ }, 30000);
209
+
210
+ it('should handle non-Error exceptions after retries', async () => {
211
+ mockFetch.mockRejectedValue('String error');
212
+
213
+ const result = await client.validateAuth();
214
+
215
+ // Non-Error exceptions are converted to Error('Network error') and retried
216
+ expect(mockFetch).toHaveBeenCalledTimes(4);
217
+ expect(result).toEqual({
218
+ ok: false,
219
+ status: 0,
220
+ error: 'Network error',
221
+ });
222
+ }, 30000);
223
+ });
224
+
225
+ // ============================================================================
226
+ // Auth Endpoints
227
+ // ============================================================================
228
+
229
+ describe('validateAuth', () => {
230
+ it('should call correct endpoint', async () => {
231
+ mockFetch.mockResolvedValue(createMockResponse({ valid: true }));
232
+
233
+ await client.validateAuth();
234
+
235
+ expect(mockFetch).toHaveBeenCalledWith(
236
+ expect.stringContaining('/api/mcp/auth/validate'),
237
+ expect.objectContaining({
238
+ method: 'POST',
239
+ body: JSON.stringify({ api_key: 'test-api-key' }),
240
+ })
241
+ );
242
+ });
243
+ });
244
+
245
+ // ============================================================================
246
+ // Session Endpoints
247
+ // ============================================================================
248
+
249
+ describe('startSession', () => {
250
+ it('should call correct endpoint with params', async () => {
251
+ mockFetch.mockResolvedValue(
252
+ createMockResponse({ session_started: true, session_id: 'session-123' })
253
+ );
254
+
255
+ await client.startSession({
256
+ project_id: 'proj-123',
257
+ mode: 'lite',
258
+ model: 'opus',
259
+ });
260
+
261
+ const [url, opts] = mockFetch.mock.calls[0] as [string, RequestInit];
262
+ const body = JSON.parse(opts.body as string);
263
+ expect(url).toContain('/api/mcp/sessions/start');
264
+ expect(opts.method).toBe('POST');
265
+ expect(body).toMatchObject({
266
+ project_id: 'proj-123',
267
+ mode: 'lite',
268
+ model: 'opus',
269
+ });
270
+ expect(body.instance_id).toBeDefined();
271
+ });
272
+
273
+ it('should support git_url param', async () => {
274
+ mockFetch.mockResolvedValue(
275
+ createMockResponse({ session_started: true })
276
+ );
277
+
278
+ await client.startSession({
279
+ git_url: 'https://github.com/org/repo',
280
+ });
281
+
282
+ const [, opts] = mockFetch.mock.calls[0] as [string, RequestInit];
283
+ const body = JSON.parse(opts.body as string);
284
+ expect(body).toMatchObject({
285
+ git_url: 'https://github.com/org/repo',
286
+ });
287
+ expect(body.instance_id).toBeDefined();
288
+ });
289
+ });
290
+
291
+ describe('heartbeat', () => {
292
+ it('should call correct endpoint', async () => {
293
+ mockFetch.mockResolvedValue(
294
+ createMockResponse({ timestamp: '2026-01-14T10:00:00Z' })
295
+ );
296
+
297
+ await client.heartbeat('session-123');
298
+
299
+ expect(mockFetch).toHaveBeenCalledWith(
300
+ expect.stringContaining('/api/mcp/sessions/heartbeat'),
301
+ expect.objectContaining({
302
+ method: 'POST',
303
+ body: JSON.stringify({ session_id: 'session-123' }),
304
+ })
305
+ );
306
+ });
307
+
308
+ it('should pass worktree path option', async () => {
309
+ mockFetch.mockResolvedValue(
310
+ createMockResponse({ timestamp: '2026-01-14T10:00:00Z' })
311
+ );
312
+
313
+ await client.heartbeat('session-123', {
314
+ current_worktree_path: '../project-task-abc',
315
+ });
316
+
317
+ expect(mockFetch).toHaveBeenCalledWith(
318
+ expect.any(String),
319
+ expect.objectContaining({
320
+ body: JSON.stringify({
321
+ session_id: 'session-123',
322
+ current_worktree_path: '../project-task-abc',
323
+ }),
324
+ })
325
+ );
326
+ });
327
+ });
328
+
329
+ describe('endSession', () => {
330
+ it('should call correct endpoint', async () => {
331
+ mockFetch.mockResolvedValue(
332
+ createMockResponse({ success: true })
333
+ );
334
+
335
+ await client.endSession('session-123');
336
+
337
+ expect(mockFetch).toHaveBeenCalledWith(
338
+ expect.stringContaining('/api/mcp/sessions/end'),
339
+ expect.objectContaining({
340
+ method: 'POST',
341
+ body: JSON.stringify({ session_id: 'session-123' }),
342
+ })
343
+ );
344
+ });
345
+ });
346
+
347
+ // ============================================================================
348
+ // Project Endpoints
349
+ // ============================================================================
350
+
351
+ describe('listProjects', () => {
352
+ it('should call correct endpoint', async () => {
353
+ mockFetch.mockResolvedValue(
354
+ createMockResponse({ projects: [] })
355
+ );
356
+
357
+ await client.listProjects();
358
+
359
+ expect(mockFetch).toHaveBeenCalledWith(
360
+ expect.stringContaining('/api/mcp/projects'),
361
+ expect.objectContaining({ method: 'GET' })
362
+ );
363
+ });
364
+ });
365
+
366
+ describe('getProject', () => {
367
+ it('should call correct endpoint with project ID', async () => {
368
+ mockFetch.mockResolvedValue(
369
+ createMockResponse({ project: { id: 'proj-123', name: 'Test' } })
370
+ );
371
+
372
+ await client.getProject('proj-123');
373
+
374
+ expect(mockFetch).toHaveBeenCalledWith(
375
+ expect.stringContaining('/api/mcp/projects/proj-123'),
376
+ expect.objectContaining({ method: 'GET' })
377
+ );
378
+ });
379
+
380
+ it('should include git_url param when provided', async () => {
381
+ mockFetch.mockResolvedValue(
382
+ createMockResponse({ project: { id: 'proj-123' } })
383
+ );
384
+
385
+ await client.getProject('proj-123', 'https://github.com/org/repo');
386
+
387
+ expect(mockFetch).toHaveBeenCalledWith(
388
+ expect.stringContaining('git_url=https'),
389
+ expect.any(Object)
390
+ );
391
+ });
392
+ });
393
+
394
+ describe('createProject', () => {
395
+ it('should call correct endpoint', async () => {
396
+ mockFetch.mockResolvedValue(
397
+ createMockResponse({ project: { id: 'new-proj' } })
398
+ );
399
+
400
+ await client.createProject({
401
+ name: 'New Project',
402
+ description: 'Test description',
403
+ });
404
+
405
+ expect(mockFetch).toHaveBeenCalledWith(
406
+ expect.stringContaining('/api/mcp/projects'),
407
+ expect.objectContaining({
408
+ method: 'POST',
409
+ body: JSON.stringify({
410
+ name: 'New Project',
411
+ description: 'Test description',
412
+ }),
413
+ })
414
+ );
415
+ });
416
+ });
417
+
418
+ describe('updateProject', () => {
419
+ it('should call correct endpoint', async () => {
420
+ mockFetch.mockResolvedValue(
421
+ createMockResponse({ success: true })
422
+ );
423
+
424
+ await client.updateProject('proj-123', {
425
+ name: 'Updated Name',
426
+ status: 'active',
427
+ });
428
+
429
+ expect(mockFetch).toHaveBeenCalledWith(
430
+ expect.stringContaining('/api/mcp/projects/proj-123'),
431
+ expect.objectContaining({
432
+ method: 'PATCH',
433
+ body: JSON.stringify({
434
+ name: 'Updated Name',
435
+ status: 'active',
436
+ }),
437
+ })
438
+ );
439
+ });
440
+ });
441
+
442
+ // ============================================================================
443
+ // Task Endpoints
444
+ // ============================================================================
445
+
446
+ describe('getTasks', () => {
447
+ it('should call correct endpoint', async () => {
448
+ mockFetch.mockResolvedValue(
449
+ createMockResponse({ tasks: [] })
450
+ );
451
+
452
+ await client.getTasks('proj-123');
453
+
454
+ expect(mockFetch).toHaveBeenCalledWith(
455
+ expect.stringContaining('/api/mcp/projects/proj-123/tasks'),
456
+ expect.objectContaining({ method: 'GET' })
457
+ );
458
+ });
459
+
460
+ it('should include filter params', async () => {
461
+ mockFetch.mockResolvedValue(
462
+ createMockResponse({ tasks: [] })
463
+ );
464
+
465
+ await client.getTasks('proj-123', {
466
+ status: 'in_progress',
467
+ limit: 10,
468
+ });
469
+
470
+ const url = mockFetch.mock.calls[0][0] as string;
471
+ expect(url).toContain('status=in_progress');
472
+ expect(url).toContain('limit=10');
473
+ });
474
+ });
475
+
476
+ describe('createTask', () => {
477
+ it('should call correct endpoint', async () => {
478
+ mockFetch.mockResolvedValue(
479
+ createMockResponse({ task: { id: 'task-123' } })
480
+ );
481
+
482
+ await client.createTask('proj-123', {
483
+ title: 'New Task',
484
+ priority: 2,
485
+ });
486
+
487
+ expect(mockFetch).toHaveBeenCalledWith(
488
+ expect.stringContaining('/api/mcp/projects/proj-123/tasks'),
489
+ expect.objectContaining({
490
+ method: 'POST',
491
+ body: JSON.stringify({
492
+ title: 'New Task',
493
+ priority: 2,
494
+ }),
495
+ })
496
+ );
497
+ });
498
+ });
499
+
500
+ describe('updateTask', () => {
501
+ it('should call correct endpoint', async () => {
502
+ mockFetch.mockResolvedValue(
503
+ createMockResponse({ success: true })
504
+ );
505
+
506
+ await client.updateTask('task-123', {
507
+ status: 'in_progress',
508
+ progress_percentage: 50,
509
+ });
510
+
511
+ expect(mockFetch).toHaveBeenCalledWith(
512
+ expect.stringContaining('/api/mcp/tasks/task-123'),
513
+ expect.objectContaining({
514
+ method: 'PATCH',
515
+ body: JSON.stringify({
516
+ status: 'in_progress',
517
+ progress_percentage: 50,
518
+ }),
519
+ })
520
+ );
521
+ });
522
+ });
523
+
524
+ describe('completeTask', () => {
525
+ it('should call proxy endpoint', async () => {
526
+ mockFetch.mockResolvedValue(
527
+ createMockResponse({ success: true })
528
+ );
529
+
530
+ await client.completeTask('task-123', {
531
+ summary: 'Task completed successfully',
532
+ });
533
+
534
+ // completeTask uses proxy endpoint for consistency (direct endpoint had Vercel routing issues)
535
+ expect(mockFetch).toHaveBeenCalledWith(
536
+ expect.stringContaining('/api/mcp/proxy'),
537
+ expect.objectContaining({
538
+ method: 'POST',
539
+ body: JSON.stringify({
540
+ operation: 'complete_task',
541
+ args: {
542
+ task_id: 'task-123',
543
+ summary: 'Task completed successfully',
544
+ },
545
+ }),
546
+ })
547
+ );
548
+ });
549
+ });
550
+
551
+ describe('deleteTask', () => {
552
+ it('should call correct endpoint', async () => {
553
+ mockFetch.mockResolvedValue(
554
+ createMockResponse({ success: true })
555
+ );
556
+
557
+ await client.deleteTask('task-123');
558
+
559
+ expect(mockFetch).toHaveBeenCalledWith(
560
+ expect.stringContaining('/api/mcp/tasks/task-123'),
561
+ expect.objectContaining({ method: 'DELETE' })
562
+ );
563
+ });
564
+ });
565
+
566
+ describe('getNextTask', () => {
567
+ it('should call correct endpoint', async () => {
568
+ mockFetch.mockResolvedValue(
569
+ createMockResponse({ task: { id: 'task-123', title: 'Next task' } })
570
+ );
571
+
572
+ await client.getNextTask('proj-123');
573
+
574
+ expect(mockFetch).toHaveBeenCalledWith(
575
+ expect.stringContaining('/api/mcp/projects/proj-123/next-task'),
576
+ expect.objectContaining({ method: 'GET' })
577
+ );
578
+ });
579
+
580
+ it('should include session_id when provided', async () => {
581
+ mockFetch.mockResolvedValue(
582
+ createMockResponse({ task: null })
583
+ );
584
+
585
+ await client.getNextTask('proj-123', 'session-456');
586
+
587
+ const url = mockFetch.mock.calls[0][0] as string;
588
+ expect(url).toContain('session_id=session-456');
589
+ });
590
+ });
591
+
592
+ // ============================================================================
593
+ // Blocker Endpoints
594
+ // ============================================================================
595
+
596
+ describe('getBlockers', () => {
597
+ it('should call correct endpoint', async () => {
598
+ mockFetch.mockResolvedValue(
599
+ createMockResponse({ blockers: [] })
600
+ );
601
+
602
+ await client.getBlockers('proj-123');
603
+
604
+ expect(mockFetch).toHaveBeenCalledWith(
605
+ expect.stringContaining('/api/mcp/proxy'),
606
+ expect.objectContaining({ method: 'POST' })
607
+ );
608
+ });
609
+ });
610
+
611
+ describe('addBlocker', () => {
612
+ it('should call correct endpoint', async () => {
613
+ mockFetch.mockResolvedValue(
614
+ createMockResponse({ blocker_id: 'blocker-123' })
615
+ );
616
+
617
+ await client.addBlocker('proj-123', 'Blocked by dependency');
618
+
619
+ expect(mockFetch).toHaveBeenCalledWith(
620
+ expect.stringContaining('/api/mcp/proxy'),
621
+ expect.objectContaining({ method: 'POST' })
622
+ );
623
+ });
624
+ });
625
+
626
+ // ============================================================================
627
+ // Validation Endpoints
628
+ // ============================================================================
629
+
630
+ describe('getTasksAwaitingValidation', () => {
631
+ it('should call correct endpoint', async () => {
632
+ mockFetch.mockResolvedValue(
633
+ createMockResponse({ tasks: [] })
634
+ );
635
+
636
+ await client.getTasksAwaitingValidation('proj-123');
637
+
638
+ expect(mockFetch).toHaveBeenCalledWith(
639
+ expect.stringContaining('/api/mcp/proxy'),
640
+ expect.objectContaining({ method: 'POST' })
641
+ );
642
+ });
643
+ });
644
+
645
+ describe('validateTask', () => {
646
+ it('should call correct endpoint', async () => {
647
+ mockFetch.mockResolvedValue(
648
+ createMockResponse({ success: true })
649
+ );
650
+
651
+ await client.validateTask('task-123', {
652
+ approved: true,
653
+ validation_notes: 'All tests pass',
654
+ });
655
+
656
+ expect(mockFetch).toHaveBeenCalledWith(
657
+ expect.stringContaining('/api/mcp/proxy'),
658
+ expect.objectContaining({ method: 'POST' })
659
+ );
660
+ });
661
+ });
662
+
663
+ // ============================================================================
664
+ // Proxy Endpoint
665
+ // ============================================================================
666
+
667
+ describe('proxy', () => {
668
+ it('should call proxy endpoint with operation', async () => {
669
+ mockFetch.mockResolvedValue(
670
+ createMockResponse({ result: 'success' })
671
+ );
672
+
673
+ await client.proxy('custom_operation', { param1: 'value1' });
674
+
675
+ expect(mockFetch).toHaveBeenCalledWith(
676
+ expect.stringContaining('/api/mcp/proxy'),
677
+ expect.objectContaining({
678
+ method: 'POST',
679
+ body: expect.stringContaining('custom_operation'),
680
+ })
681
+ );
682
+ });
683
+
684
+ it('should pass session context when provided', async () => {
685
+ mockFetch.mockResolvedValue(
686
+ createMockResponse({ result: 'success' })
687
+ );
688
+
689
+ await client.proxy(
690
+ 'operation',
691
+ { arg: 'value' },
692
+ { sessionId: 'session-123', persona: 'Wave' }
693
+ );
694
+
695
+ expect(mockFetch).toHaveBeenCalledWith(
696
+ expect.any(String),
697
+ expect.objectContaining({
698
+ body: expect.stringContaining('session-123'),
699
+ })
700
+ );
701
+ });
702
+ });
703
+
704
+ // ============================================================================
705
+ // Error Handling Edge Cases
706
+ // ============================================================================
707
+
708
+ describe('error handling edge cases', () => {
709
+ it('should handle JSON parse error in response after retries', async () => {
710
+ mockFetch.mockResolvedValue({
711
+ ok: true,
712
+ status: 200,
713
+ json: vi.fn().mockRejectedValue(new SyntaxError('Invalid JSON')),
714
+ headers: { get: () => null },
715
+ });
716
+
717
+ const result = await client.validateAuth();
718
+
719
+ // JSON parse errors are treated as network errors and retried
720
+ expect(mockFetch).toHaveBeenCalledTimes(4);
721
+ expect(result.ok).toBe(false);
722
+ expect(result.error).toContain('Invalid JSON');
723
+ }, 30000);
724
+
725
+ it('should handle timeout errors after retries', async () => {
726
+ const timeoutError = new Error('Request timeout');
727
+ timeoutError.name = 'AbortError';
728
+ mockFetch.mockRejectedValue(timeoutError);
729
+
730
+ const result = await client.validateAuth();
731
+
732
+ // Timeout errors are retried
733
+ expect(mockFetch).toHaveBeenCalledTimes(4);
734
+ expect(result).toEqual({
735
+ ok: false,
736
+ status: 0,
737
+ error: 'Request timeout',
738
+ });
739
+ }, 30000);
740
+ });
741
+
742
+ // ============================================================================
743
+ // Retry Logic Tests
744
+ // ============================================================================
745
+
746
+ describe('retry logic', () => {
747
+ it('should retry on 429 status code', async () => {
748
+ // First two calls return 429, third succeeds
749
+ mockFetch
750
+ .mockResolvedValueOnce(createMockResponse({ error: 'Rate limited' }, false, 429))
751
+ .mockResolvedValueOnce(createMockResponse({ error: 'Rate limited' }, false, 429))
752
+ .mockResolvedValueOnce(createMockResponse({ valid: true }, true, 200));
753
+
754
+ const result = await client.validateAuth();
755
+
756
+ expect(mockFetch).toHaveBeenCalledTimes(3);
757
+ expect(result.ok).toBe(true);
758
+ });
759
+
760
+ it('should retry on 503 status code', async () => {
761
+ mockFetch
762
+ .mockResolvedValueOnce(createMockResponse({ error: 'Service unavailable' }, false, 503))
763
+ .mockResolvedValueOnce(createMockResponse({ valid: true }, true, 200));
764
+
765
+ const result = await client.validateAuth();
766
+
767
+ expect(mockFetch).toHaveBeenCalledTimes(2);
768
+ expect(result.ok).toBe(true);
769
+ });
770
+
771
+ it('should retry on 504 status code', async () => {
772
+ mockFetch
773
+ .mockResolvedValueOnce(createMockResponse({ error: 'Gateway timeout' }, false, 504))
774
+ .mockResolvedValueOnce(createMockResponse({ valid: true }, true, 200));
775
+
776
+ const result = await client.validateAuth();
777
+
778
+ expect(mockFetch).toHaveBeenCalledTimes(2);
779
+ expect(result.ok).toBe(true);
780
+ });
781
+
782
+ it('should not retry on non-retryable status codes (400)', async () => {
783
+ mockFetch.mockResolvedValueOnce(createMockResponse({ error: 'Bad request' }, false, 400));
784
+
785
+ const result = await client.validateAuth();
786
+
787
+ expect(mockFetch).toHaveBeenCalledTimes(1);
788
+ expect(result.ok).toBe(false);
789
+ expect(result.status).toBe(400);
790
+ });
791
+
792
+ it('should not retry on non-retryable status codes (401)', async () => {
793
+ mockFetch.mockResolvedValueOnce(createMockResponse({ error: 'Unauthorized' }, false, 401));
794
+
795
+ const result = await client.validateAuth();
796
+
797
+ expect(mockFetch).toHaveBeenCalledTimes(1);
798
+ expect(result.ok).toBe(false);
799
+ expect(result.status).toBe(401);
800
+ });
801
+
802
+ it('should not retry on non-retryable status codes (500)', async () => {
803
+ mockFetch.mockResolvedValueOnce(createMockResponse({ error: 'Server error' }, false, 500));
804
+
805
+ const result = await client.validateAuth();
806
+
807
+ expect(mockFetch).toHaveBeenCalledTimes(1);
808
+ expect(result.ok).toBe(false);
809
+ expect(result.status).toBe(500);
810
+ });
811
+
812
+ it('should stop after max retries (3)', async () => {
813
+ // All 4 attempts (1 initial + 3 retries) return 429
814
+ mockFetch.mockResolvedValue(createMockResponse({ error: 'Rate limited' }, false, 429));
815
+
816
+ const result = await client.validateAuth();
817
+
818
+ expect(mockFetch).toHaveBeenCalledTimes(4); // 1 initial + 3 retries
819
+ expect(result.ok).toBe(false);
820
+ expect(result.status).toBe(429);
821
+ // API error message takes precedence over generic retry message
822
+ expect(result.error).toBe('Rate limited');
823
+ }, 30000);
824
+
825
+ it('should retry on network errors', async () => {
826
+ mockFetch
827
+ .mockRejectedValueOnce(new Error('Network error'))
828
+ .mockResolvedValueOnce(createMockResponse({ valid: true }, true, 200));
829
+
830
+ const result = await client.validateAuth();
831
+
832
+ expect(mockFetch).toHaveBeenCalledTimes(2);
833
+ expect(result.ok).toBe(true);
834
+ }, 10000);
835
+
836
+ it('should return last error after all retries exhausted on network failure', async () => {
837
+ mockFetch.mockRejectedValue(new Error('Connection refused'));
838
+
839
+ const result = await client.validateAuth();
840
+
841
+ expect(mockFetch).toHaveBeenCalledTimes(4);
842
+ expect(result.ok).toBe(false);
843
+ expect(result.status).toBe(0);
844
+ expect(result.error).toBe('Connection refused');
845
+ }, 30000);
846
+ });
847
+ });