@vibescope/mcp-server 0.2.9 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/CHANGELOG.md +84 -84
  2. package/README.md +194 -194
  3. package/dist/api-client.d.ts +36 -0
  4. package/dist/api-client.js +34 -0
  5. package/dist/cli.d.ts +1 -1
  6. package/dist/cli.js +30 -38
  7. package/dist/handlers/discovery.js +2 -0
  8. package/dist/handlers/session.d.ts +11 -0
  9. package/dist/handlers/session.js +101 -0
  10. package/dist/handlers/tasks.d.ts +8 -0
  11. package/dist/handlers/tasks.js +163 -3
  12. package/dist/handlers/tool-docs.js +840 -828
  13. package/dist/handlers/validation.js +49 -2
  14. package/dist/index.js +73 -73
  15. package/dist/setup.js +6 -6
  16. package/dist/templates/agent-guidelines.js +185 -185
  17. package/dist/templates/help-content.js +1622 -1544
  18. package/dist/tools.js +130 -74
  19. package/dist/utils.d.ts +15 -11
  20. package/dist/utils.js +53 -28
  21. package/docs/TOOLS.md +2407 -2053
  22. package/package.json +51 -51
  23. package/scripts/generate-docs.ts +212 -212
  24. package/scripts/version-bump.ts +203 -203
  25. package/src/api-client.test.ts +723 -723
  26. package/src/api-client.ts +2561 -2499
  27. package/src/cli.test.ts +24 -8
  28. package/src/cli.ts +204 -212
  29. package/src/handlers/__test-setup__.ts +236 -236
  30. package/src/handlers/__test-utils__.ts +87 -87
  31. package/src/handlers/blockers.test.ts +468 -468
  32. package/src/handlers/blockers.ts +163 -163
  33. package/src/handlers/bodies-of-work.test.ts +704 -704
  34. package/src/handlers/bodies-of-work.ts +526 -526
  35. package/src/handlers/connectors.test.ts +834 -834
  36. package/src/handlers/connectors.ts +229 -229
  37. package/src/handlers/cost.test.ts +462 -462
  38. package/src/handlers/cost.ts +285 -285
  39. package/src/handlers/decisions.test.ts +382 -382
  40. package/src/handlers/decisions.ts +153 -153
  41. package/src/handlers/deployment.test.ts +551 -551
  42. package/src/handlers/deployment.ts +541 -541
  43. package/src/handlers/discovery.test.ts +206 -206
  44. package/src/handlers/discovery.ts +392 -390
  45. package/src/handlers/fallback.test.ts +537 -537
  46. package/src/handlers/fallback.ts +194 -194
  47. package/src/handlers/file-checkouts.test.ts +750 -750
  48. package/src/handlers/file-checkouts.ts +185 -185
  49. package/src/handlers/findings.test.ts +633 -633
  50. package/src/handlers/findings.ts +239 -239
  51. package/src/handlers/git-issues.test.ts +631 -631
  52. package/src/handlers/git-issues.ts +136 -136
  53. package/src/handlers/ideas.test.ts +644 -644
  54. package/src/handlers/ideas.ts +207 -207
  55. package/src/handlers/index.ts +84 -84
  56. package/src/handlers/milestones.test.ts +475 -475
  57. package/src/handlers/milestones.ts +180 -180
  58. package/src/handlers/organizations.test.ts +826 -826
  59. package/src/handlers/organizations.ts +315 -315
  60. package/src/handlers/progress.test.ts +269 -269
  61. package/src/handlers/progress.ts +77 -77
  62. package/src/handlers/project.test.ts +546 -546
  63. package/src/handlers/project.ts +239 -239
  64. package/src/handlers/requests.test.ts +303 -303
  65. package/src/handlers/requests.ts +99 -99
  66. package/src/handlers/roles.test.ts +305 -305
  67. package/src/handlers/roles.ts +219 -219
  68. package/src/handlers/session.test.ts +998 -875
  69. package/src/handlers/session.ts +839 -730
  70. package/src/handlers/sprints.test.ts +732 -732
  71. package/src/handlers/sprints.ts +537 -537
  72. package/src/handlers/tasks.test.ts +931 -907
  73. package/src/handlers/tasks.ts +1121 -945
  74. package/src/handlers/tool-categories.test.ts +66 -66
  75. package/src/handlers/tool-docs.ts +1109 -1096
  76. package/src/handlers/types.test.ts +259 -259
  77. package/src/handlers/types.ts +175 -175
  78. package/src/handlers/validation.test.ts +582 -582
  79. package/src/handlers/validation.ts +164 -113
  80. package/src/index.test.ts +674 -0
  81. package/src/index.ts +792 -792
  82. package/src/setup.test.ts +233 -233
  83. package/src/setup.ts +404 -403
  84. package/src/templates/agent-guidelines.ts +210 -210
  85. package/src/templates/help-content.ts +1751 -1673
  86. package/src/token-tracking.test.ts +463 -463
  87. package/src/token-tracking.ts +166 -166
  88. package/src/tools.test.ts +416 -0
  89. package/src/tools.ts +3611 -3555
  90. package/src/utils.test.ts +785 -683
  91. package/src/utils.ts +469 -436
  92. package/src/validators.test.ts +223 -223
  93. package/src/validators.ts +249 -249
  94. package/tsconfig.json +16 -16
  95. package/vitest.config.ts +14 -14
@@ -1,907 +1,931 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import {
3
- getNextTask,
4
- addTask,
5
- updateTask,
6
- completeTask,
7
- deleteTask,
8
- addTaskReference,
9
- removeTaskReference,
10
- batchUpdateTasks,
11
- batchCompleteTasks,
12
- addSubtask,
13
- getSubtasks,
14
- getValidationApprovedGitInstructions,
15
- } from './tasks.js';
16
- import { ValidationError } from '../validators.js';
17
- import { createMockContext } from './__test-utils__.js';
18
- import { mockApiClient } from './__test-setup__.js';
19
-
20
- // ============================================================================
21
- // getNextTask Tests
22
- // ============================================================================
23
-
24
- describe('getNextTask', () => {
25
- beforeEach(() => vi.clearAllMocks());
26
-
27
- it('should throw error for missing project_id', async () => {
28
- const ctx = createMockContext();
29
- await expect(getNextTask({}, ctx)).rejects.toThrow(ValidationError);
30
- });
31
-
32
- it('should throw error for invalid project_id', async () => {
33
- const ctx = createMockContext();
34
- await expect(getNextTask({ project_id: 'invalid' }, ctx)).rejects.toThrow(ValidationError);
35
- });
36
-
37
- it('should return next task when available', async () => {
38
- mockApiClient.getNextTask.mockResolvedValue({
39
- ok: true,
40
- data: {
41
- task: { id: 'task-1', title: 'Next task', priority: 1 },
42
- directive: 'Start working',
43
- },
44
- });
45
-
46
- const ctx = createMockContext();
47
- const result = await getNextTask(
48
- { project_id: '123e4567-e89b-12d3-a456-426614174000' },
49
- ctx
50
- );
51
-
52
- expect(result.result.task).toMatchObject({ id: 'task-1' });
53
- });
54
-
55
- it('should return null when no tasks available', async () => {
56
- mockApiClient.getNextTask.mockResolvedValue({
57
- ok: true,
58
- data: { task: null, message: 'No tasks available' },
59
- });
60
-
61
- const ctx = createMockContext();
62
- const result = await getNextTask(
63
- { project_id: '123e4567-e89b-12d3-a456-426614174000' },
64
- ctx
65
- );
66
-
67
- expect(result.result.task).toBeNull();
68
- });
69
-
70
- it('should pass session_id to API', async () => {
71
- mockApiClient.getNextTask.mockResolvedValue({
72
- ok: true,
73
- data: { task: null },
74
- });
75
-
76
- const ctx = createMockContext({ sessionId: 'my-session' });
77
- await getNextTask({ project_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx);
78
-
79
- expect(mockApiClient.getNextTask).toHaveBeenCalledWith(
80
- '123e4567-e89b-12d3-a456-426614174000',
81
- 'my-session'
82
- );
83
- });
84
- });
85
-
86
- // ============================================================================
87
- // addTask Tests
88
- // ============================================================================
89
-
90
- describe('addTask', () => {
91
- beforeEach(() => vi.clearAllMocks());
92
-
93
- it('should throw error for missing project_id', async () => {
94
- const ctx = createMockContext();
95
- await expect(addTask({ title: 'Test task' }, ctx)).rejects.toThrow(ValidationError);
96
- });
97
-
98
- it('should throw error for invalid project_id', async () => {
99
- const ctx = createMockContext();
100
- await expect(
101
- addTask({ project_id: 'invalid', title: 'Test task' }, ctx)
102
- ).rejects.toThrow(ValidationError);
103
- });
104
-
105
- it('should throw error for missing title', async () => {
106
- const ctx = createMockContext();
107
- await expect(
108
- addTask({ project_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx)
109
- ).rejects.toThrow(ValidationError);
110
- });
111
-
112
- it('should throw error for invalid priority', async () => {
113
- const ctx = createMockContext();
114
- await expect(
115
- addTask({
116
- project_id: '123e4567-e89b-12d3-a456-426614174000',
117
- title: 'Test',
118
- priority: 10,
119
- }, ctx)
120
- ).rejects.toThrow(ValidationError);
121
- });
122
-
123
- it('should add task successfully', async () => {
124
- mockApiClient.createTask.mockResolvedValue({
125
- ok: true,
126
- data: { task_id: 'new-task-id', title: 'Test task' },
127
- });
128
-
129
- const ctx = createMockContext();
130
- const result = await addTask(
131
- {
132
- project_id: '123e4567-e89b-12d3-a456-426614174000',
133
- title: 'Test task',
134
- },
135
- ctx
136
- );
137
-
138
- expect(result.result).toMatchObject({
139
- success: true,
140
- task_id: 'new-task-id',
141
- });
142
- });
143
-
144
- it('should include optional fields in API call', async () => {
145
- mockApiClient.createTask.mockResolvedValue({
146
- ok: true,
147
- data: { task_id: 'new-task-id' },
148
- });
149
-
150
- const ctx = createMockContext();
151
- await addTask(
152
- {
153
- project_id: '123e4567-e89b-12d3-a456-426614174000',
154
- title: 'Test task',
155
- description: 'A description',
156
- priority: 2,
157
- estimated_minutes: 30,
158
- blocking: true,
159
- },
160
- ctx
161
- );
162
-
163
- expect(mockApiClient.createTask).toHaveBeenCalledWith(
164
- '123e4567-e89b-12d3-a456-426614174000',
165
- expect.objectContaining({
166
- title: 'Test task',
167
- description: 'A description',
168
- priority: 2,
169
- estimated_minutes: 30,
170
- blocking: true,
171
- })
172
- );
173
- });
174
- });
175
-
176
- // ============================================================================
177
- // updateTask Tests
178
- // ============================================================================
179
-
180
- describe('updateTask', () => {
181
- beforeEach(() => vi.clearAllMocks());
182
-
183
- it('should throw error for missing task_id', async () => {
184
- const ctx = createMockContext();
185
- await expect(updateTask({ status: 'in_progress' }, ctx)).rejects.toThrow(ValidationError);
186
- });
187
-
188
- it('should throw error for invalid task_id', async () => {
189
- const ctx = createMockContext();
190
- await expect(
191
- updateTask({ task_id: 'invalid', status: 'in_progress' }, ctx)
192
- ).rejects.toThrow(ValidationError);
193
- });
194
-
195
- it('should throw error for invalid status', async () => {
196
- const ctx = createMockContext();
197
- await expect(
198
- updateTask({
199
- task_id: '123e4567-e89b-12d3-a456-426614174000',
200
- status: 'invalid',
201
- }, ctx)
202
- ).rejects.toThrow(ValidationError);
203
- });
204
-
205
- it('should throw error for invalid priority', async () => {
206
- const ctx = createMockContext();
207
- await expect(
208
- updateTask({
209
- task_id: '123e4567-e89b-12d3-a456-426614174000',
210
- priority: 0,
211
- }, ctx)
212
- ).rejects.toThrow(ValidationError);
213
- });
214
-
215
- it('should throw error for invalid progress_percentage', async () => {
216
- const ctx = createMockContext();
217
- await expect(
218
- updateTask({
219
- task_id: '123e4567-e89b-12d3-a456-426614174000',
220
- progress_percentage: 150,
221
- }, ctx)
222
- ).rejects.toThrow(ValidationError);
223
- });
224
-
225
- it('should update task successfully', async () => {
226
- mockApiClient.updateTask.mockResolvedValue({
227
- ok: true,
228
- data: { success: true },
229
- });
230
-
231
- const ctx = createMockContext();
232
- const result = await updateTask(
233
- {
234
- task_id: '123e4567-e89b-12d3-a456-426614174000',
235
- status: 'in_progress',
236
- git_branch: 'feature/test-branch',
237
- },
238
- ctx
239
- );
240
-
241
- expect(result.result).toMatchObject({ success: true });
242
- });
243
-
244
- it('should handle agent_task_limit error', async () => {
245
- mockApiClient.updateTask.mockResolvedValue({
246
- ok: false,
247
- error: 'agent_task_limit: Agent already has a task in progress',
248
- });
249
-
250
- const ctx = createMockContext();
251
- const result = await updateTask(
252
- {
253
- task_id: '123e4567-e89b-12d3-a456-426614174000',
254
- status: 'in_progress',
255
- git_branch: 'feature/test-branch',
256
- },
257
- ctx
258
- );
259
-
260
- expect(result.result).toMatchObject({
261
- error: 'agent_task_limit',
262
- });
263
- });
264
-
265
- it('should handle task_claimed error', async () => {
266
- mockApiClient.updateTask.mockResolvedValue({
267
- ok: false,
268
- error: 'task_claimed: Task is being worked on by another agent',
269
- });
270
-
271
- const ctx = createMockContext();
272
- const result = await updateTask(
273
- {
274
- task_id: '123e4567-e89b-12d3-a456-426614174000',
275
- status: 'in_progress',
276
- git_branch: 'feature/test-branch',
277
- },
278
- ctx
279
- );
280
-
281
- expect(result.result).toMatchObject({
282
- error: 'task_claimed',
283
- });
284
- });
285
-
286
- it('should return error when setting in_progress without git_branch', async () => {
287
- const ctx = createMockContext();
288
- const result = await updateTask(
289
- {
290
- task_id: '123e4567-e89b-12d3-a456-426614174000',
291
- status: 'in_progress',
292
- },
293
- ctx
294
- );
295
-
296
- expect(result.result).toMatchObject({
297
- error: 'worktree_required',
298
- message: expect.stringContaining('git_branch is required'),
299
- hint: expect.stringContaining('Create a worktree'),
300
- });
301
- // Should NOT call the API when worktree requirement fails
302
- expect(mockApiClient.updateTask).not.toHaveBeenCalled();
303
- });
304
-
305
- it('should return worktree_example in error response', async () => {
306
- const ctx = createMockContext();
307
- const result = await updateTask(
308
- {
309
- task_id: '123e4567-e89b-12d3-a456-426614174000',
310
- status: 'in_progress',
311
- },
312
- ctx
313
- );
314
-
315
- expect(result.result).toHaveProperty('worktree_example');
316
- expect(result.result.worktree_example).toHaveProperty('command');
317
- expect(result.result.worktree_example).toHaveProperty('then');
318
- expect(result.result).toHaveProperty('skip_option');
319
- });
320
-
321
- it('should succeed when setting in_progress with git_branch', async () => {
322
- mockApiClient.updateTask.mockResolvedValue({
323
- ok: true,
324
- data: { success: true },
325
- });
326
-
327
- const ctx = createMockContext();
328
- const result = await updateTask(
329
- {
330
- task_id: '123e4567-e89b-12d3-a456-426614174000',
331
- status: 'in_progress',
332
- git_branch: 'feature/my-task',
333
- },
334
- ctx
335
- );
336
-
337
- expect(result.result).toMatchObject({ success: true });
338
- expect(result.result).not.toHaveProperty('error');
339
- expect(mockApiClient.updateTask).toHaveBeenCalled();
340
- });
341
-
342
- it('should succeed when using skip_worktree_requirement without git_branch', async () => {
343
- mockApiClient.updateTask.mockResolvedValue({
344
- ok: true,
345
- data: { success: true },
346
- });
347
-
348
- const ctx = createMockContext();
349
- const result = await updateTask(
350
- {
351
- task_id: '123e4567-e89b-12d3-a456-426614174000',
352
- status: 'in_progress',
353
- skip_worktree_requirement: true,
354
- },
355
- ctx
356
- );
357
-
358
- expect(result.result).toMatchObject({ success: true });
359
- expect(result.result).not.toHaveProperty('error');
360
- expect(mockApiClient.updateTask).toHaveBeenCalled();
361
- });
362
-
363
- it('should not require git_branch when updating status other than in_progress', async () => {
364
- mockApiClient.updateTask.mockResolvedValue({
365
- ok: true,
366
- data: { success: true },
367
- });
368
-
369
- const ctx = createMockContext();
370
- const result = await updateTask(
371
- {
372
- task_id: '123e4567-e89b-12d3-a456-426614174000',
373
- status: 'completed',
374
- },
375
- ctx
376
- );
377
-
378
- expect(result.result).toMatchObject({ success: true });
379
- expect(result.result).not.toHaveProperty('error');
380
- expect(mockApiClient.updateTask).toHaveBeenCalled();
381
- });
382
-
383
- it('should not require git_branch when updating progress only', async () => {
384
- mockApiClient.updateTask.mockResolvedValue({
385
- ok: true,
386
- data: { success: true },
387
- });
388
-
389
- const ctx = createMockContext();
390
- const result = await updateTask(
391
- {
392
- task_id: '123e4567-e89b-12d3-a456-426614174000',
393
- progress_percentage: 50,
394
- progress_note: 'Halfway done',
395
- },
396
- ctx
397
- );
398
-
399
- expect(result.result).toMatchObject({ success: true });
400
- expect(mockApiClient.updateTask).toHaveBeenCalled();
401
- });
402
- });
403
-
404
- // ============================================================================
405
- // completeTask Tests
406
- // ============================================================================
407
-
408
- describe('completeTask', () => {
409
- beforeEach(() => vi.clearAllMocks());
410
-
411
- it('should throw error for missing task_id', async () => {
412
- const ctx = createMockContext();
413
- await expect(completeTask({}, ctx)).rejects.toThrow(ValidationError);
414
- });
415
-
416
- it('should throw error for invalid task_id', async () => {
417
- const ctx = createMockContext();
418
- await expect(
419
- completeTask({ task_id: 'invalid' }, ctx)
420
- ).rejects.toThrow(ValidationError);
421
- });
422
-
423
- it('should complete task successfully', async () => {
424
- mockApiClient.completeTask.mockResolvedValue({
425
- ok: true,
426
- data: {
427
- success: true,
428
- directive: 'Start next task',
429
- auto_continue: true,
430
- completed_task_id: '123e4567-e89b-12d3-a456-426614174000',
431
- next_task: { id: 'task-2', title: 'Next task' },
432
- },
433
- });
434
-
435
- const ctx = createMockContext();
436
- const result = await completeTask(
437
- { task_id: '123e4567-e89b-12d3-a456-426614174000' },
438
- ctx
439
- );
440
-
441
- expect(result.result).toMatchObject({
442
- success: true,
443
- auto_continue: true,
444
- next_task: expect.any(Object),
445
- });
446
- });
447
-
448
- it('should include summary in API call', async () => {
449
- mockApiClient.completeTask.mockResolvedValue({
450
- ok: true,
451
- data: { success: true },
452
- });
453
-
454
- const ctx = createMockContext();
455
- await completeTask(
456
- {
457
- task_id: '123e4567-e89b-12d3-a456-426614174000',
458
- summary: 'Completed the feature',
459
- },
460
- ctx
461
- );
462
-
463
- expect(mockApiClient.completeTask).toHaveBeenCalledWith(
464
- '123e4567-e89b-12d3-a456-426614174000',
465
- expect.objectContaining({ summary: 'Completed the feature' })
466
- );
467
- });
468
-
469
- it('should return error when API returns error', async () => {
470
- mockApiClient.completeTask.mockResolvedValue({
471
- ok: false,
472
- error: 'Task not found',
473
- });
474
-
475
- const ctx = createMockContext();
476
- const result = await completeTask({ task_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx);
477
-
478
- expect(result.isError).toBe(true);
479
- expect(result.result).toMatchObject({
480
- error: 'Task not found',
481
- });
482
- });
483
- });
484
-
485
- // ============================================================================
486
- // deleteTask Tests
487
- // ============================================================================
488
-
489
- describe('deleteTask', () => {
490
- beforeEach(() => vi.clearAllMocks());
491
-
492
- it('should throw error for missing task_id', async () => {
493
- const ctx = createMockContext();
494
- await expect(deleteTask({}, ctx)).rejects.toThrow(ValidationError);
495
- });
496
-
497
- it('should throw error for invalid task_id', async () => {
498
- const ctx = createMockContext();
499
- await expect(deleteTask({ task_id: 'invalid' }, ctx)).rejects.toThrow(ValidationError);
500
- });
501
-
502
- it('should delete task successfully', async () => {
503
- mockApiClient.deleteTask.mockResolvedValue({
504
- ok: true,
505
- data: { success: true },
506
- });
507
-
508
- const ctx = createMockContext();
509
- const result = await deleteTask(
510
- { task_id: '123e4567-e89b-12d3-a456-426614174000' },
511
- ctx
512
- );
513
-
514
- expect(result.result).toMatchObject({
515
- success: true,
516
- deleted_id: '123e4567-e89b-12d3-a456-426614174000',
517
- });
518
- });
519
- });
520
-
521
- // ============================================================================
522
- // addTaskReference Tests
523
- // ============================================================================
524
-
525
- describe('addTaskReference', () => {
526
- beforeEach(() => vi.clearAllMocks());
527
-
528
- it('should throw error for missing task_id', async () => {
529
- const ctx = createMockContext();
530
- await expect(
531
- addTaskReference({ url: 'https://example.com' }, ctx)
532
- ).rejects.toThrow(ValidationError);
533
- });
534
-
535
- it('should throw error for missing url', async () => {
536
- const ctx = createMockContext();
537
- await expect(
538
- addTaskReference({ task_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx)
539
- ).rejects.toThrow(ValidationError);
540
- });
541
-
542
- it('should add reference successfully', async () => {
543
- mockApiClient.addTaskReference.mockResolvedValue({
544
- ok: true,
545
- data: { reference: { url: 'https://example.com', label: 'Test' } },
546
- });
547
-
548
- const ctx = createMockContext();
549
- const result = await addTaskReference(
550
- {
551
- task_id: '123e4567-e89b-12d3-a456-426614174000',
552
- url: 'https://example.com',
553
- label: 'Test',
554
- },
555
- ctx
556
- );
557
-
558
- expect(result.result).toMatchObject({
559
- success: true,
560
- reference: expect.any(Object),
561
- });
562
- });
563
-
564
- it('should handle duplicate reference error', async () => {
565
- mockApiClient.addTaskReference.mockResolvedValue({
566
- ok: false,
567
- error: 'Reference already exists',
568
- });
569
-
570
- const ctx = createMockContext();
571
- const result = await addTaskReference(
572
- {
573
- task_id: '123e4567-e89b-12d3-a456-426614174000',
574
- url: 'https://example.com',
575
- },
576
- ctx
577
- );
578
-
579
- expect(result.result).toMatchObject({
580
- success: false,
581
- error: expect.stringContaining('already exists'),
582
- });
583
- });
584
- });
585
-
586
- // ============================================================================
587
- // removeTaskReference Tests
588
- // ============================================================================
589
-
590
- describe('removeTaskReference', () => {
591
- beforeEach(() => vi.clearAllMocks());
592
-
593
- it('should throw error for missing task_id', async () => {
594
- const ctx = createMockContext();
595
- await expect(
596
- removeTaskReference({ url: 'https://example.com' }, ctx)
597
- ).rejects.toThrow(ValidationError);
598
- });
599
-
600
- it('should throw error for missing url', async () => {
601
- const ctx = createMockContext();
602
- await expect(
603
- removeTaskReference({ task_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx)
604
- ).rejects.toThrow(ValidationError);
605
- });
606
-
607
- it('should remove reference successfully', async () => {
608
- mockApiClient.removeTaskReference.mockResolvedValue({
609
- ok: true,
610
- data: { success: true },
611
- });
612
-
613
- const ctx = createMockContext();
614
- const result = await removeTaskReference(
615
- {
616
- task_id: '123e4567-e89b-12d3-a456-426614174000',
617
- url: 'https://example.com',
618
- },
619
- ctx
620
- );
621
-
622
- expect(result.result).toMatchObject({ success: true });
623
- });
624
-
625
- it('should handle not found error', async () => {
626
- mockApiClient.removeTaskReference.mockResolvedValue({
627
- ok: false,
628
- error: 'Reference not found',
629
- });
630
-
631
- const ctx = createMockContext();
632
- const result = await removeTaskReference(
633
- {
634
- task_id: '123e4567-e89b-12d3-a456-426614174000',
635
- url: 'https://example.com',
636
- },
637
- ctx
638
- );
639
-
640
- expect(result.result).toMatchObject({
641
- success: false,
642
- error: expect.stringContaining('not found'),
643
- });
644
- });
645
- });
646
-
647
- // ============================================================================
648
- // batchUpdateTasks Tests
649
- // ============================================================================
650
-
651
- describe('batchUpdateTasks', () => {
652
- beforeEach(() => vi.clearAllMocks());
653
-
654
- it('should throw error for missing updates array', async () => {
655
- const ctx = createMockContext();
656
- await expect(batchUpdateTasks({}, ctx)).rejects.toThrow(ValidationError);
657
- });
658
-
659
- it('should throw error for empty updates array', async () => {
660
- const ctx = createMockContext();
661
- await expect(batchUpdateTasks({ updates: [] }, ctx)).rejects.toThrow(ValidationError);
662
- });
663
-
664
- it('should throw error for too many updates', async () => {
665
- const ctx = createMockContext();
666
- const updates = Array(51).fill({
667
- task_id: '123e4567-e89b-12d3-a456-426614174000',
668
- });
669
- await expect(batchUpdateTasks({ updates }, ctx)).rejects.toThrow(ValidationError);
670
- });
671
-
672
- it('should batch update tasks successfully', async () => {
673
- mockApiClient.batchUpdateTasks.mockResolvedValue({
674
- ok: true,
675
- data: { success: true, updated_count: 2 },
676
- });
677
-
678
- const ctx = createMockContext();
679
- const result = await batchUpdateTasks(
680
- {
681
- updates: [
682
- { task_id: '123e4567-e89b-12d3-a456-426614174000', status: 'in_progress' },
683
- { task_id: '123e4567-e89b-12d3-a456-426614174001', status: 'completed' },
684
- ],
685
- },
686
- ctx
687
- );
688
-
689
- expect(result.result).toMatchObject({
690
- success: true,
691
- total: 2,
692
- succeeded: 2,
693
- });
694
- });
695
- });
696
-
697
- // ============================================================================
698
- // batchCompleteTasks Tests
699
- // ============================================================================
700
-
701
- describe('batchCompleteTasks', () => {
702
- beforeEach(() => vi.clearAllMocks());
703
-
704
- it('should throw error for missing completions array', async () => {
705
- const ctx = createMockContext();
706
- await expect(batchCompleteTasks({}, ctx)).rejects.toThrow(ValidationError);
707
- });
708
-
709
- it('should throw error for empty completions array', async () => {
710
- const ctx = createMockContext();
711
- await expect(batchCompleteTasks({ completions: [] }, ctx)).rejects.toThrow(ValidationError);
712
- });
713
-
714
- it('should batch complete tasks successfully', async () => {
715
- mockApiClient.batchCompleteTasks.mockResolvedValue({
716
- ok: true,
717
- data: {
718
- success: true,
719
- completed_count: 2,
720
- next_task: { id: 'task-3', title: 'Next' },
721
- },
722
- });
723
-
724
- const ctx = createMockContext();
725
- const result = await batchCompleteTasks(
726
- {
727
- completions: [
728
- { task_id: '123e4567-e89b-12d3-a456-426614174000' },
729
- { task_id: '123e4567-e89b-12d3-a456-426614174001', summary: 'Done' },
730
- ],
731
- },
732
- ctx
733
- );
734
-
735
- expect(result.result).toMatchObject({
736
- success: true,
737
- total: 2,
738
- succeeded: 2,
739
- next_task: expect.any(Object),
740
- });
741
- });
742
- });
743
-
744
- // ============================================================================
745
- // addSubtask Tests
746
- // ============================================================================
747
-
748
- describe('addSubtask', () => {
749
- beforeEach(() => vi.clearAllMocks());
750
-
751
- it('should throw error for missing parent_task_id', async () => {
752
- const ctx = createMockContext();
753
- await expect(addSubtask({ title: 'Subtask' }, ctx)).rejects.toThrow(ValidationError);
754
- });
755
-
756
- it('should throw error for missing title', async () => {
757
- const ctx = createMockContext();
758
- await expect(
759
- addSubtask({ parent_task_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx)
760
- ).rejects.toThrow(ValidationError);
761
- });
762
-
763
- it('should add subtask successfully', async () => {
764
- mockApiClient.addSubtask.mockResolvedValue({
765
- ok: true,
766
- data: {
767
- subtask_id: 'subtask-1',
768
- parent_task_id: '123e4567-e89b-12d3-a456-426614174000',
769
- },
770
- });
771
-
772
- const ctx = createMockContext();
773
- const result = await addSubtask(
774
- {
775
- parent_task_id: '123e4567-e89b-12d3-a456-426614174000',
776
- title: 'Subtask 1',
777
- },
778
- ctx
779
- );
780
-
781
- expect(result.result).toMatchObject({
782
- success: true,
783
- subtask_id: 'subtask-1',
784
- });
785
- });
786
-
787
- it('should handle nested subtask error', async () => {
788
- mockApiClient.addSubtask.mockResolvedValue({
789
- ok: false,
790
- error: 'Cannot create subtask of a subtask',
791
- });
792
-
793
- const ctx = createMockContext();
794
- const result = await addSubtask(
795
- {
796
- parent_task_id: '123e4567-e89b-12d3-a456-426614174000',
797
- title: 'Nested subtask',
798
- },
799
- ctx
800
- );
801
-
802
- expect(result.result).toMatchObject({
803
- success: false,
804
- error: 'Cannot create subtask of a subtask',
805
- });
806
- });
807
- });
808
-
809
- // ============================================================================
810
- // getSubtasks Tests
811
- // ============================================================================
812
-
813
- describe('getSubtasks', () => {
814
- beforeEach(() => vi.clearAllMocks());
815
-
816
- it('should throw error for missing parent_task_id', async () => {
817
- const ctx = createMockContext();
818
- await expect(getSubtasks({}, ctx)).rejects.toThrow(ValidationError);
819
- });
820
-
821
- it('should throw error for invalid parent_task_id', async () => {
822
- const ctx = createMockContext();
823
- await expect(
824
- getSubtasks({ parent_task_id: 'invalid' }, ctx)
825
- ).rejects.toThrow(ValidationError);
826
- });
827
-
828
- it('should return subtasks successfully', async () => {
829
- mockApiClient.getSubtasks.mockResolvedValue({
830
- ok: true,
831
- data: {
832
- subtasks: [
833
- { id: 'sub-1', title: 'Subtask 1', status: 'pending' },
834
- { id: 'sub-2', title: 'Subtask 2', status: 'completed' },
835
- ],
836
- stats: { total: 2, completed: 1, progress_percentage: 50 },
837
- },
838
- });
839
-
840
- const ctx = createMockContext();
841
- const result = await getSubtasks(
842
- { parent_task_id: '123e4567-e89b-12d3-a456-426614174000' },
843
- ctx
844
- );
845
-
846
- expect(result.result).toMatchObject({
847
- subtasks: expect.any(Array),
848
- stats: expect.objectContaining({ total: 2 }),
849
- });
850
- });
851
- });
852
-
853
- // ============================================================================
854
- // getValidationApprovedGitInstructions Tests
855
- // ============================================================================
856
-
857
- describe('getValidationApprovedGitInstructions', () => {
858
- it('should return undefined for none workflow', () => {
859
- const result = getValidationApprovedGitInstructions(
860
- { git_workflow: 'none', git_main_branch: 'main' },
861
- 'feature/test'
862
- );
863
- expect(result).toBeUndefined();
864
- });
865
-
866
- it('should return undefined for trunk-based workflow', () => {
867
- const result = getValidationApprovedGitInstructions(
868
- { git_workflow: 'trunk-based', git_main_branch: 'main' },
869
- 'feature/test'
870
- );
871
- expect(result).toBeUndefined();
872
- });
873
-
874
- it('should return undefined when no task branch', () => {
875
- const result = getValidationApprovedGitInstructions(
876
- { git_workflow: 'github-flow', git_main_branch: 'main' },
877
- undefined
878
- );
879
- expect(result).toBeUndefined();
880
- });
881
-
882
- it('should return merge instructions for github-flow', () => {
883
- const result = getValidationApprovedGitInstructions(
884
- { git_workflow: 'github-flow', git_main_branch: 'main' },
885
- 'feature/test'
886
- );
887
-
888
- expect(result).toMatchObject({
889
- target_branch: 'main',
890
- feature_branch: 'feature/test',
891
- steps: expect.any(Array),
892
- cleanup: expect.any(Array),
893
- });
894
- });
895
-
896
- it('should return merge instructions for git-flow with develop', () => {
897
- const result = getValidationApprovedGitInstructions(
898
- { git_workflow: 'git-flow', git_main_branch: 'main', git_develop_branch: 'develop' },
899
- 'feature/test'
900
- );
901
-
902
- expect(result).toMatchObject({
903
- target_branch: 'develop',
904
- feature_branch: 'feature/test',
905
- });
906
- });
907
- });
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import {
3
+ getNextTask,
4
+ addTask,
5
+ updateTask,
6
+ completeTask,
7
+ deleteTask,
8
+ addTaskReference,
9
+ removeTaskReference,
10
+ batchUpdateTasks,
11
+ batchCompleteTasks,
12
+ addSubtask,
13
+ getSubtasks,
14
+ getValidationApprovedGitInstructions,
15
+ } from './tasks.js';
16
+ import { ValidationError } from '../validators.js';
17
+ import { createMockContext } from './__test-utils__.js';
18
+ import { mockApiClient } from './__test-setup__.js';
19
+
20
+ // ============================================================================
21
+ // getNextTask Tests
22
+ // ============================================================================
23
+
24
+ describe('getNextTask', () => {
25
+ beforeEach(() => vi.clearAllMocks());
26
+
27
+ it('should throw error for missing project_id', async () => {
28
+ const ctx = createMockContext();
29
+ await expect(getNextTask({}, ctx)).rejects.toThrow(ValidationError);
30
+ });
31
+
32
+ it('should throw error for invalid project_id', async () => {
33
+ const ctx = createMockContext();
34
+ await expect(getNextTask({ project_id: 'invalid' }, ctx)).rejects.toThrow(ValidationError);
35
+ });
36
+
37
+ it('should return next task when available', async () => {
38
+ mockApiClient.getNextTask.mockResolvedValue({
39
+ ok: true,
40
+ data: {
41
+ task: { id: 'task-1', title: 'Next task', priority: 1 },
42
+ directive: 'Start working',
43
+ },
44
+ });
45
+
46
+ const ctx = createMockContext();
47
+ const result = await getNextTask(
48
+ { project_id: '123e4567-e89b-12d3-a456-426614174000' },
49
+ ctx
50
+ );
51
+
52
+ expect(result.result.task).toMatchObject({ id: 'task-1' });
53
+ });
54
+
55
+ it('should return null when no tasks available', async () => {
56
+ mockApiClient.getNextTask.mockResolvedValue({
57
+ ok: true,
58
+ data: { task: null, message: 'No tasks available' },
59
+ });
60
+
61
+ const ctx = createMockContext();
62
+ const result = await getNextTask(
63
+ { project_id: '123e4567-e89b-12d3-a456-426614174000' },
64
+ ctx
65
+ );
66
+
67
+ expect(result.result.task).toBeNull();
68
+ });
69
+
70
+ it('should pass session_id to API', async () => {
71
+ mockApiClient.getNextTask.mockResolvedValue({
72
+ ok: true,
73
+ data: { task: null },
74
+ });
75
+
76
+ const ctx = createMockContext({ sessionId: 'my-session' });
77
+ await getNextTask({ project_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx);
78
+
79
+ expect(mockApiClient.getNextTask).toHaveBeenCalledWith(
80
+ '123e4567-e89b-12d3-a456-426614174000',
81
+ 'my-session'
82
+ );
83
+ });
84
+ });
85
+
86
+ // ============================================================================
87
+ // addTask Tests
88
+ // ============================================================================
89
+
90
+ describe('addTask', () => {
91
+ beforeEach(() => vi.clearAllMocks());
92
+
93
+ it('should throw error for missing project_id', async () => {
94
+ const ctx = createMockContext();
95
+ await expect(addTask({ title: 'Test task' }, ctx)).rejects.toThrow(ValidationError);
96
+ });
97
+
98
+ it('should throw error for invalid project_id', async () => {
99
+ const ctx = createMockContext();
100
+ await expect(
101
+ addTask({ project_id: 'invalid', title: 'Test task' }, ctx)
102
+ ).rejects.toThrow(ValidationError);
103
+ });
104
+
105
+ it('should throw error for missing title', async () => {
106
+ const ctx = createMockContext();
107
+ await expect(
108
+ addTask({ project_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx)
109
+ ).rejects.toThrow(ValidationError);
110
+ });
111
+
112
+ it('should throw error for invalid priority', async () => {
113
+ const ctx = createMockContext();
114
+ await expect(
115
+ addTask({
116
+ project_id: '123e4567-e89b-12d3-a456-426614174000',
117
+ title: 'Test',
118
+ priority: 10,
119
+ }, ctx)
120
+ ).rejects.toThrow(ValidationError);
121
+ });
122
+
123
+ it('should add task successfully', async () => {
124
+ mockApiClient.createTask.mockResolvedValue({
125
+ ok: true,
126
+ data: { task_id: 'new-task-id', title: 'Test task' },
127
+ });
128
+
129
+ const ctx = createMockContext();
130
+ const result = await addTask(
131
+ {
132
+ project_id: '123e4567-e89b-12d3-a456-426614174000',
133
+ title: 'Test task',
134
+ },
135
+ ctx
136
+ );
137
+
138
+ expect(result.result).toMatchObject({
139
+ success: true,
140
+ task_id: 'new-task-id',
141
+ });
142
+ });
143
+
144
+ it('should include optional fields in API call', async () => {
145
+ mockApiClient.createTask.mockResolvedValue({
146
+ ok: true,
147
+ data: { task_id: 'new-task-id' },
148
+ });
149
+
150
+ const ctx = createMockContext();
151
+ await addTask(
152
+ {
153
+ project_id: '123e4567-e89b-12d3-a456-426614174000',
154
+ title: 'Test task',
155
+ description: 'A description',
156
+ priority: 2,
157
+ estimated_minutes: 30,
158
+ blocking: true,
159
+ },
160
+ ctx
161
+ );
162
+
163
+ expect(mockApiClient.createTask).toHaveBeenCalledWith(
164
+ '123e4567-e89b-12d3-a456-426614174000',
165
+ expect.objectContaining({
166
+ title: 'Test task',
167
+ description: 'A description',
168
+ priority: 2,
169
+ estimated_minutes: 30,
170
+ blocking: true,
171
+ })
172
+ );
173
+ });
174
+ });
175
+
176
+ // ============================================================================
177
+ // updateTask Tests
178
+ // ============================================================================
179
+
180
+ describe('updateTask', () => {
181
+ beforeEach(() => vi.clearAllMocks());
182
+
183
+ it('should throw error for missing task_id', async () => {
184
+ const ctx = createMockContext();
185
+ await expect(updateTask({ status: 'in_progress' }, ctx)).rejects.toThrow(ValidationError);
186
+ });
187
+
188
+ it('should throw error for invalid task_id', async () => {
189
+ const ctx = createMockContext();
190
+ await expect(
191
+ updateTask({ task_id: 'invalid', status: 'in_progress' }, ctx)
192
+ ).rejects.toThrow(ValidationError);
193
+ });
194
+
195
+ it('should throw error for invalid status', async () => {
196
+ const ctx = createMockContext();
197
+ await expect(
198
+ updateTask({
199
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
200
+ status: 'invalid',
201
+ }, ctx)
202
+ ).rejects.toThrow(ValidationError);
203
+ });
204
+
205
+ it('should throw error for invalid priority', async () => {
206
+ const ctx = createMockContext();
207
+ await expect(
208
+ updateTask({
209
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
210
+ priority: 0,
211
+ }, ctx)
212
+ ).rejects.toThrow(ValidationError);
213
+ });
214
+
215
+ it('should throw error for invalid progress_percentage', async () => {
216
+ const ctx = createMockContext();
217
+ await expect(
218
+ updateTask({
219
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
220
+ progress_percentage: 150,
221
+ }, ctx)
222
+ ).rejects.toThrow(ValidationError);
223
+ });
224
+
225
+ it('should update task successfully', async () => {
226
+ mockApiClient.updateTask.mockResolvedValue({
227
+ ok: true,
228
+ data: { success: true },
229
+ });
230
+
231
+ const ctx = createMockContext();
232
+ const result = await updateTask(
233
+ {
234
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
235
+ status: 'in_progress',
236
+ git_branch: 'feature/test-branch',
237
+ },
238
+ ctx
239
+ );
240
+
241
+ expect(result.result).toMatchObject({ success: true });
242
+ });
243
+
244
+ it('should handle agent_task_limit error', async () => {
245
+ mockApiClient.updateTask.mockResolvedValue({
246
+ ok: false,
247
+ error: 'agent_task_limit: Agent already has a task in progress',
248
+ });
249
+
250
+ const ctx = createMockContext();
251
+ const result = await updateTask(
252
+ {
253
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
254
+ status: 'in_progress',
255
+ git_branch: 'feature/test-branch',
256
+ },
257
+ ctx
258
+ );
259
+
260
+ expect(result.result).toMatchObject({
261
+ error: 'agent_task_limit',
262
+ });
263
+ });
264
+
265
+ it('should handle task_claimed error', async () => {
266
+ mockApiClient.updateTask.mockResolvedValue({
267
+ ok: false,
268
+ error: 'task_claimed: Task is being worked on by another agent',
269
+ });
270
+
271
+ const ctx = createMockContext();
272
+ const result = await updateTask(
273
+ {
274
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
275
+ status: 'in_progress',
276
+ git_branch: 'feature/test-branch',
277
+ },
278
+ ctx
279
+ );
280
+
281
+ expect(result.result).toMatchObject({
282
+ error: 'task_already_claimed',
283
+ });
284
+ });
285
+
286
+ it('should return error when setting in_progress without git_branch', async () => {
287
+ const ctx = createMockContext();
288
+ const result = await updateTask(
289
+ {
290
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
291
+ status: 'in_progress',
292
+ },
293
+ ctx
294
+ );
295
+
296
+ expect(result.result).toMatchObject({
297
+ error: 'worktree_required',
298
+ message: expect.stringContaining('git_branch is required'),
299
+ hint: expect.stringContaining('Create a worktree'),
300
+ });
301
+ // Should NOT call the API when worktree requirement fails
302
+ expect(mockApiClient.updateTask).not.toHaveBeenCalled();
303
+ });
304
+
305
+ it('should return worktree_example in error response', async () => {
306
+ const ctx = createMockContext();
307
+ const result = await updateTask(
308
+ {
309
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
310
+ status: 'in_progress',
311
+ },
312
+ ctx
313
+ );
314
+
315
+ expect(result.result).toHaveProperty('worktree_example');
316
+ expect(result.result.worktree_example).toHaveProperty('command');
317
+ expect(result.result.worktree_example).toHaveProperty('then');
318
+ expect(result.result).toHaveProperty('skip_option');
319
+ });
320
+
321
+ it('should succeed when setting in_progress with git_branch', async () => {
322
+ mockApiClient.updateTask.mockResolvedValue({
323
+ ok: true,
324
+ data: { success: true },
325
+ });
326
+
327
+ const ctx = createMockContext();
328
+ const result = await updateTask(
329
+ {
330
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
331
+ status: 'in_progress',
332
+ git_branch: 'feature/my-task',
333
+ },
334
+ ctx
335
+ );
336
+
337
+ expect(result.result).toMatchObject({ success: true });
338
+ expect(result.result).not.toHaveProperty('error');
339
+ expect(mockApiClient.updateTask).toHaveBeenCalled();
340
+ });
341
+
342
+ it('should succeed when using skip_worktree_requirement without git_branch', async () => {
343
+ mockApiClient.updateTask.mockResolvedValue({
344
+ ok: true,
345
+ data: { success: true },
346
+ });
347
+
348
+ const ctx = createMockContext();
349
+ const result = await updateTask(
350
+ {
351
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
352
+ status: 'in_progress',
353
+ skip_worktree_requirement: true,
354
+ },
355
+ ctx
356
+ );
357
+
358
+ expect(result.result).toMatchObject({ success: true });
359
+ expect(result.result).not.toHaveProperty('error');
360
+ expect(mockApiClient.updateTask).toHaveBeenCalled();
361
+ });
362
+
363
+ it('should not require git_branch when updating status other than in_progress', async () => {
364
+ mockApiClient.updateTask.mockResolvedValue({
365
+ ok: true,
366
+ data: { success: true },
367
+ });
368
+
369
+ const ctx = createMockContext();
370
+ const result = await updateTask(
371
+ {
372
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
373
+ status: 'completed',
374
+ },
375
+ ctx
376
+ );
377
+
378
+ expect(result.result).toMatchObject({ success: true });
379
+ expect(result.result).not.toHaveProperty('error');
380
+ expect(mockApiClient.updateTask).toHaveBeenCalled();
381
+ });
382
+
383
+ it('should not require git_branch when updating progress only', async () => {
384
+ mockApiClient.updateTask.mockResolvedValue({
385
+ ok: true,
386
+ data: { success: true },
387
+ });
388
+
389
+ const ctx = createMockContext();
390
+ const result = await updateTask(
391
+ {
392
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
393
+ progress_percentage: 50,
394
+ progress_note: 'Halfway done',
395
+ },
396
+ ctx
397
+ );
398
+
399
+ expect(result.result).toMatchObject({ success: true });
400
+ expect(mockApiClient.updateTask).toHaveBeenCalled();
401
+ });
402
+
403
+ it('should include FIX_ALREADY_EXISTS_GUIDANCE when setting status to in_progress', async () => {
404
+ mockApiClient.updateTask.mockResolvedValue({
405
+ ok: true,
406
+ data: { success: true },
407
+ });
408
+
409
+ const ctx = createMockContext();
410
+ const result = await updateTask(
411
+ {
412
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
413
+ status: 'in_progress',
414
+ git_branch: 'feature/my-task',
415
+ },
416
+ ctx
417
+ );
418
+
419
+ expect(result.result).toMatchObject({ success: true });
420
+ expect(result.result).toHaveProperty('FIX_ALREADY_EXISTS_GUIDANCE');
421
+ expect(result.result.FIX_ALREADY_EXISTS_GUIDANCE).toHaveProperty('message');
422
+ expect(result.result.FIX_ALREADY_EXISTS_GUIDANCE).toHaveProperty('steps');
423
+ expect(result.result.FIX_ALREADY_EXISTS_GUIDANCE).toHaveProperty('rationale');
424
+ expect(result.result.FIX_ALREADY_EXISTS_GUIDANCE.steps).toHaveLength(4);
425
+ });
426
+ });
427
+
428
+ // ============================================================================
429
+ // completeTask Tests
430
+ // ============================================================================
431
+
432
+ describe('completeTask', () => {
433
+ beforeEach(() => vi.clearAllMocks());
434
+
435
+ it('should throw error for missing task_id', async () => {
436
+ const ctx = createMockContext();
437
+ await expect(completeTask({}, ctx)).rejects.toThrow(ValidationError);
438
+ });
439
+
440
+ it('should throw error for invalid task_id', async () => {
441
+ const ctx = createMockContext();
442
+ await expect(
443
+ completeTask({ task_id: 'invalid' }, ctx)
444
+ ).rejects.toThrow(ValidationError);
445
+ });
446
+
447
+ it('should complete task successfully', async () => {
448
+ mockApiClient.completeTask.mockResolvedValue({
449
+ ok: true,
450
+ data: {
451
+ success: true,
452
+ directive: 'Start next task',
453
+ auto_continue: true,
454
+ completed_task_id: '123e4567-e89b-12d3-a456-426614174000',
455
+ next_task: { id: 'task-2', title: 'Next task' },
456
+ },
457
+ });
458
+
459
+ const ctx = createMockContext();
460
+ const result = await completeTask(
461
+ { task_id: '123e4567-e89b-12d3-a456-426614174000' },
462
+ ctx
463
+ );
464
+
465
+ expect(result.result).toMatchObject({
466
+ success: true,
467
+ auto_continue: true,
468
+ next_task: expect.any(Object),
469
+ });
470
+ });
471
+
472
+ it('should include summary in API call', async () => {
473
+ mockApiClient.completeTask.mockResolvedValue({
474
+ ok: true,
475
+ data: { success: true },
476
+ });
477
+
478
+ const ctx = createMockContext();
479
+ await completeTask(
480
+ {
481
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
482
+ summary: 'Completed the feature',
483
+ },
484
+ ctx
485
+ );
486
+
487
+ expect(mockApiClient.completeTask).toHaveBeenCalledWith(
488
+ '123e4567-e89b-12d3-a456-426614174000',
489
+ expect.objectContaining({ summary: 'Completed the feature' })
490
+ );
491
+ });
492
+
493
+ it('should return error when API returns error', async () => {
494
+ mockApiClient.completeTask.mockResolvedValue({
495
+ ok: false,
496
+ error: 'Task not found',
497
+ });
498
+
499
+ const ctx = createMockContext();
500
+ const result = await completeTask({ task_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx);
501
+
502
+ expect(result.isError).toBe(true);
503
+ expect(result.result).toMatchObject({
504
+ error: 'Task not found',
505
+ });
506
+ });
507
+ });
508
+
509
+ // ============================================================================
510
+ // deleteTask Tests
511
+ // ============================================================================
512
+
513
+ describe('deleteTask', () => {
514
+ beforeEach(() => vi.clearAllMocks());
515
+
516
+ it('should throw error for missing task_id', async () => {
517
+ const ctx = createMockContext();
518
+ await expect(deleteTask({}, ctx)).rejects.toThrow(ValidationError);
519
+ });
520
+
521
+ it('should throw error for invalid task_id', async () => {
522
+ const ctx = createMockContext();
523
+ await expect(deleteTask({ task_id: 'invalid' }, ctx)).rejects.toThrow(ValidationError);
524
+ });
525
+
526
+ it('should delete task successfully', async () => {
527
+ mockApiClient.deleteTask.mockResolvedValue({
528
+ ok: true,
529
+ data: { success: true },
530
+ });
531
+
532
+ const ctx = createMockContext();
533
+ const result = await deleteTask(
534
+ { task_id: '123e4567-e89b-12d3-a456-426614174000' },
535
+ ctx
536
+ );
537
+
538
+ expect(result.result).toMatchObject({
539
+ success: true,
540
+ deleted_id: '123e4567-e89b-12d3-a456-426614174000',
541
+ });
542
+ });
543
+ });
544
+
545
+ // ============================================================================
546
+ // addTaskReference Tests
547
+ // ============================================================================
548
+
549
+ describe('addTaskReference', () => {
550
+ beforeEach(() => vi.clearAllMocks());
551
+
552
+ it('should throw error for missing task_id', async () => {
553
+ const ctx = createMockContext();
554
+ await expect(
555
+ addTaskReference({ url: 'https://example.com' }, ctx)
556
+ ).rejects.toThrow(ValidationError);
557
+ });
558
+
559
+ it('should throw error for missing url', async () => {
560
+ const ctx = createMockContext();
561
+ await expect(
562
+ addTaskReference({ task_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx)
563
+ ).rejects.toThrow(ValidationError);
564
+ });
565
+
566
+ it('should add reference successfully', async () => {
567
+ mockApiClient.addTaskReference.mockResolvedValue({
568
+ ok: true,
569
+ data: { reference: { url: 'https://example.com', label: 'Test' } },
570
+ });
571
+
572
+ const ctx = createMockContext();
573
+ const result = await addTaskReference(
574
+ {
575
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
576
+ url: 'https://example.com',
577
+ label: 'Test',
578
+ },
579
+ ctx
580
+ );
581
+
582
+ expect(result.result).toMatchObject({
583
+ success: true,
584
+ reference: expect.any(Object),
585
+ });
586
+ });
587
+
588
+ it('should handle duplicate reference error', async () => {
589
+ mockApiClient.addTaskReference.mockResolvedValue({
590
+ ok: false,
591
+ error: 'Reference already exists',
592
+ });
593
+
594
+ const ctx = createMockContext();
595
+ const result = await addTaskReference(
596
+ {
597
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
598
+ url: 'https://example.com',
599
+ },
600
+ ctx
601
+ );
602
+
603
+ expect(result.result).toMatchObject({
604
+ success: false,
605
+ error: expect.stringContaining('already exists'),
606
+ });
607
+ });
608
+ });
609
+
610
+ // ============================================================================
611
+ // removeTaskReference Tests
612
+ // ============================================================================
613
+
614
+ describe('removeTaskReference', () => {
615
+ beforeEach(() => vi.clearAllMocks());
616
+
617
+ it('should throw error for missing task_id', async () => {
618
+ const ctx = createMockContext();
619
+ await expect(
620
+ removeTaskReference({ url: 'https://example.com' }, ctx)
621
+ ).rejects.toThrow(ValidationError);
622
+ });
623
+
624
+ it('should throw error for missing url', async () => {
625
+ const ctx = createMockContext();
626
+ await expect(
627
+ removeTaskReference({ task_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx)
628
+ ).rejects.toThrow(ValidationError);
629
+ });
630
+
631
+ it('should remove reference successfully', async () => {
632
+ mockApiClient.removeTaskReference.mockResolvedValue({
633
+ ok: true,
634
+ data: { success: true },
635
+ });
636
+
637
+ const ctx = createMockContext();
638
+ const result = await removeTaskReference(
639
+ {
640
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
641
+ url: 'https://example.com',
642
+ },
643
+ ctx
644
+ );
645
+
646
+ expect(result.result).toMatchObject({ success: true });
647
+ });
648
+
649
+ it('should handle not found error', async () => {
650
+ mockApiClient.removeTaskReference.mockResolvedValue({
651
+ ok: false,
652
+ error: 'Reference not found',
653
+ });
654
+
655
+ const ctx = createMockContext();
656
+ const result = await removeTaskReference(
657
+ {
658
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
659
+ url: 'https://example.com',
660
+ },
661
+ ctx
662
+ );
663
+
664
+ expect(result.result).toMatchObject({
665
+ success: false,
666
+ error: expect.stringContaining('not found'),
667
+ });
668
+ });
669
+ });
670
+
671
+ // ============================================================================
672
+ // batchUpdateTasks Tests
673
+ // ============================================================================
674
+
675
+ describe('batchUpdateTasks', () => {
676
+ beforeEach(() => vi.clearAllMocks());
677
+
678
+ it('should throw error for missing updates array', async () => {
679
+ const ctx = createMockContext();
680
+ await expect(batchUpdateTasks({}, ctx)).rejects.toThrow(ValidationError);
681
+ });
682
+
683
+ it('should throw error for empty updates array', async () => {
684
+ const ctx = createMockContext();
685
+ await expect(batchUpdateTasks({ updates: [] }, ctx)).rejects.toThrow(ValidationError);
686
+ });
687
+
688
+ it('should throw error for too many updates', async () => {
689
+ const ctx = createMockContext();
690
+ const updates = Array(51).fill({
691
+ task_id: '123e4567-e89b-12d3-a456-426614174000',
692
+ });
693
+ await expect(batchUpdateTasks({ updates }, ctx)).rejects.toThrow(ValidationError);
694
+ });
695
+
696
+ it('should batch update tasks successfully', async () => {
697
+ mockApiClient.batchUpdateTasks.mockResolvedValue({
698
+ ok: true,
699
+ data: { success: true, updated_count: 2 },
700
+ });
701
+
702
+ const ctx = createMockContext();
703
+ const result = await batchUpdateTasks(
704
+ {
705
+ updates: [
706
+ { task_id: '123e4567-e89b-12d3-a456-426614174000', status: 'in_progress' },
707
+ { task_id: '123e4567-e89b-12d3-a456-426614174001', status: 'completed' },
708
+ ],
709
+ },
710
+ ctx
711
+ );
712
+
713
+ expect(result.result).toMatchObject({
714
+ success: true,
715
+ total: 2,
716
+ succeeded: 2,
717
+ });
718
+ });
719
+ });
720
+
721
+ // ============================================================================
722
+ // batchCompleteTasks Tests
723
+ // ============================================================================
724
+
725
+ describe('batchCompleteTasks', () => {
726
+ beforeEach(() => vi.clearAllMocks());
727
+
728
+ it('should throw error for missing completions array', async () => {
729
+ const ctx = createMockContext();
730
+ await expect(batchCompleteTasks({}, ctx)).rejects.toThrow(ValidationError);
731
+ });
732
+
733
+ it('should throw error for empty completions array', async () => {
734
+ const ctx = createMockContext();
735
+ await expect(batchCompleteTasks({ completions: [] }, ctx)).rejects.toThrow(ValidationError);
736
+ });
737
+
738
+ it('should batch complete tasks successfully', async () => {
739
+ mockApiClient.batchCompleteTasks.mockResolvedValue({
740
+ ok: true,
741
+ data: {
742
+ success: true,
743
+ completed_count: 2,
744
+ next_task: { id: 'task-3', title: 'Next' },
745
+ },
746
+ });
747
+
748
+ const ctx = createMockContext();
749
+ const result = await batchCompleteTasks(
750
+ {
751
+ completions: [
752
+ { task_id: '123e4567-e89b-12d3-a456-426614174000' },
753
+ { task_id: '123e4567-e89b-12d3-a456-426614174001', summary: 'Done' },
754
+ ],
755
+ },
756
+ ctx
757
+ );
758
+
759
+ expect(result.result).toMatchObject({
760
+ success: true,
761
+ total: 2,
762
+ succeeded: 2,
763
+ next_task: expect.any(Object),
764
+ });
765
+ });
766
+ });
767
+
768
+ // ============================================================================
769
+ // addSubtask Tests
770
+ // ============================================================================
771
+
772
+ describe('addSubtask', () => {
773
+ beforeEach(() => vi.clearAllMocks());
774
+
775
+ it('should throw error for missing parent_task_id', async () => {
776
+ const ctx = createMockContext();
777
+ await expect(addSubtask({ title: 'Subtask' }, ctx)).rejects.toThrow(ValidationError);
778
+ });
779
+
780
+ it('should throw error for missing title', async () => {
781
+ const ctx = createMockContext();
782
+ await expect(
783
+ addSubtask({ parent_task_id: '123e4567-e89b-12d3-a456-426614174000' }, ctx)
784
+ ).rejects.toThrow(ValidationError);
785
+ });
786
+
787
+ it('should add subtask successfully', async () => {
788
+ mockApiClient.addSubtask.mockResolvedValue({
789
+ ok: true,
790
+ data: {
791
+ subtask_id: 'subtask-1',
792
+ parent_task_id: '123e4567-e89b-12d3-a456-426614174000',
793
+ },
794
+ });
795
+
796
+ const ctx = createMockContext();
797
+ const result = await addSubtask(
798
+ {
799
+ parent_task_id: '123e4567-e89b-12d3-a456-426614174000',
800
+ title: 'Subtask 1',
801
+ },
802
+ ctx
803
+ );
804
+
805
+ expect(result.result).toMatchObject({
806
+ success: true,
807
+ subtask_id: 'subtask-1',
808
+ });
809
+ });
810
+
811
+ it('should handle nested subtask error', async () => {
812
+ mockApiClient.addSubtask.mockResolvedValue({
813
+ ok: false,
814
+ error: 'Cannot create subtask of a subtask',
815
+ });
816
+
817
+ const ctx = createMockContext();
818
+ const result = await addSubtask(
819
+ {
820
+ parent_task_id: '123e4567-e89b-12d3-a456-426614174000',
821
+ title: 'Nested subtask',
822
+ },
823
+ ctx
824
+ );
825
+
826
+ expect(result.result).toMatchObject({
827
+ success: false,
828
+ error: 'Cannot create subtask of a subtask',
829
+ });
830
+ });
831
+ });
832
+
833
+ // ============================================================================
834
+ // getSubtasks Tests
835
+ // ============================================================================
836
+
837
+ describe('getSubtasks', () => {
838
+ beforeEach(() => vi.clearAllMocks());
839
+
840
+ it('should throw error for missing parent_task_id', async () => {
841
+ const ctx = createMockContext();
842
+ await expect(getSubtasks({}, ctx)).rejects.toThrow(ValidationError);
843
+ });
844
+
845
+ it('should throw error for invalid parent_task_id', async () => {
846
+ const ctx = createMockContext();
847
+ await expect(
848
+ getSubtasks({ parent_task_id: 'invalid' }, ctx)
849
+ ).rejects.toThrow(ValidationError);
850
+ });
851
+
852
+ it('should return subtasks successfully', async () => {
853
+ mockApiClient.getSubtasks.mockResolvedValue({
854
+ ok: true,
855
+ data: {
856
+ subtasks: [
857
+ { id: 'sub-1', title: 'Subtask 1', status: 'pending' },
858
+ { id: 'sub-2', title: 'Subtask 2', status: 'completed' },
859
+ ],
860
+ stats: { total: 2, completed: 1, progress_percentage: 50 },
861
+ },
862
+ });
863
+
864
+ const ctx = createMockContext();
865
+ const result = await getSubtasks(
866
+ { parent_task_id: '123e4567-e89b-12d3-a456-426614174000' },
867
+ ctx
868
+ );
869
+
870
+ expect(result.result).toMatchObject({
871
+ subtasks: expect.any(Array),
872
+ stats: expect.objectContaining({ total: 2 }),
873
+ });
874
+ });
875
+ });
876
+
877
+ // ============================================================================
878
+ // getValidationApprovedGitInstructions Tests
879
+ // ============================================================================
880
+
881
+ describe('getValidationApprovedGitInstructions', () => {
882
+ it('should return undefined for none workflow', () => {
883
+ const result = getValidationApprovedGitInstructions(
884
+ { git_workflow: 'none', git_main_branch: 'main' },
885
+ 'feature/test'
886
+ );
887
+ expect(result).toBeUndefined();
888
+ });
889
+
890
+ it('should return undefined for trunk-based workflow', () => {
891
+ const result = getValidationApprovedGitInstructions(
892
+ { git_workflow: 'trunk-based', git_main_branch: 'main' },
893
+ 'feature/test'
894
+ );
895
+ expect(result).toBeUndefined();
896
+ });
897
+
898
+ it('should return undefined when no task branch', () => {
899
+ const result = getValidationApprovedGitInstructions(
900
+ { git_workflow: 'github-flow', git_main_branch: 'main' },
901
+ undefined
902
+ );
903
+ expect(result).toBeUndefined();
904
+ });
905
+
906
+ it('should return merge instructions for github-flow', () => {
907
+ const result = getValidationApprovedGitInstructions(
908
+ { git_workflow: 'github-flow', git_main_branch: 'main' },
909
+ 'feature/test'
910
+ );
911
+
912
+ expect(result).toMatchObject({
913
+ target_branch: 'main',
914
+ feature_branch: 'feature/test',
915
+ steps: expect.any(Array),
916
+ cleanup: expect.any(Array),
917
+ });
918
+ });
919
+
920
+ it('should return merge instructions for git-flow with develop', () => {
921
+ const result = getValidationApprovedGitInstructions(
922
+ { git_workflow: 'git-flow', git_main_branch: 'main', git_develop_branch: 'develop' },
923
+ 'feature/test'
924
+ );
925
+
926
+ expect(result).toMatchObject({
927
+ target_branch: 'develop',
928
+ feature_branch: 'feature/test',
929
+ });
930
+ });
931
+ });