@vibescope/mcp-server 0.2.5 → 0.2.7

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 (82) hide show
  1. package/CHANGELOG.md +84 -84
  2. package/README.md +194 -194
  3. package/dist/cli.js +26 -26
  4. package/dist/handlers/tool-docs.js +828 -828
  5. package/dist/index.js +73 -73
  6. package/dist/templates/agent-guidelines.js +185 -185
  7. package/dist/token-tracking.js +4 -2
  8. package/dist/tools.js +65 -65
  9. package/dist/utils.js +11 -11
  10. package/docs/TOOLS.md +2053 -2053
  11. package/package.json +3 -2
  12. package/scripts/generate-docs.ts +212 -212
  13. package/scripts/version-bump.ts +203 -203
  14. package/src/api-client.test.ts +723 -723
  15. package/src/api-client.ts +2499 -2499
  16. package/src/cli.ts +212 -212
  17. package/src/handlers/__test-setup__.ts +236 -236
  18. package/src/handlers/__test-utils__.ts +87 -87
  19. package/src/handlers/blockers.test.ts +468 -468
  20. package/src/handlers/blockers.ts +163 -163
  21. package/src/handlers/bodies-of-work.test.ts +704 -704
  22. package/src/handlers/bodies-of-work.ts +526 -526
  23. package/src/handlers/connectors.test.ts +834 -834
  24. package/src/handlers/connectors.ts +229 -229
  25. package/src/handlers/cost.test.ts +462 -462
  26. package/src/handlers/cost.ts +285 -285
  27. package/src/handlers/decisions.test.ts +382 -382
  28. package/src/handlers/decisions.ts +153 -153
  29. package/src/handlers/deployment.test.ts +551 -551
  30. package/src/handlers/deployment.ts +541 -541
  31. package/src/handlers/discovery.test.ts +206 -206
  32. package/src/handlers/discovery.ts +390 -390
  33. package/src/handlers/fallback.test.ts +537 -537
  34. package/src/handlers/fallback.ts +194 -194
  35. package/src/handlers/file-checkouts.test.ts +750 -750
  36. package/src/handlers/file-checkouts.ts +185 -185
  37. package/src/handlers/findings.test.ts +633 -633
  38. package/src/handlers/findings.ts +239 -239
  39. package/src/handlers/git-issues.test.ts +631 -631
  40. package/src/handlers/git-issues.ts +136 -136
  41. package/src/handlers/ideas.test.ts +644 -644
  42. package/src/handlers/ideas.ts +207 -207
  43. package/src/handlers/index.ts +84 -84
  44. package/src/handlers/milestones.test.ts +475 -475
  45. package/src/handlers/milestones.ts +180 -180
  46. package/src/handlers/organizations.test.ts +826 -826
  47. package/src/handlers/organizations.ts +315 -315
  48. package/src/handlers/progress.test.ts +269 -269
  49. package/src/handlers/progress.ts +77 -77
  50. package/src/handlers/project.test.ts +546 -546
  51. package/src/handlers/project.ts +239 -239
  52. package/src/handlers/requests.test.ts +303 -303
  53. package/src/handlers/requests.ts +99 -99
  54. package/src/handlers/roles.test.ts +303 -303
  55. package/src/handlers/roles.ts +226 -226
  56. package/src/handlers/session.test.ts +875 -875
  57. package/src/handlers/session.ts +738 -738
  58. package/src/handlers/sprints.test.ts +732 -732
  59. package/src/handlers/sprints.ts +537 -537
  60. package/src/handlers/tasks.test.ts +907 -907
  61. package/src/handlers/tasks.ts +945 -945
  62. package/src/handlers/tool-categories.test.ts +66 -66
  63. package/src/handlers/tool-docs.ts +1096 -1096
  64. package/src/handlers/types.test.ts +259 -259
  65. package/src/handlers/types.ts +175 -175
  66. package/src/handlers/validation.test.ts +582 -582
  67. package/src/handlers/validation.ts +97 -97
  68. package/src/index.ts +792 -792
  69. package/src/setup.test.ts +233 -231
  70. package/src/setup.ts +370 -370
  71. package/src/templates/agent-guidelines.ts +210 -210
  72. package/src/token-tracking.test.ts +463 -453
  73. package/src/token-tracking.ts +166 -164
  74. package/src/tools.ts +3562 -3562
  75. package/src/utils.test.ts +683 -683
  76. package/src/utils.ts +436 -436
  77. package/src/validators.test.ts +223 -223
  78. package/src/validators.ts +249 -249
  79. package/tsconfig.json +16 -16
  80. package/vitest.config.ts +14 -14
  81. package/dist/knowledge.d.ts +0 -6
  82. package/dist/knowledge.js +0 -218
@@ -1,907 +1,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_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_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
+ });