@vibescope/mcp-server 0.5.0 → 0.5.2

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 (162) hide show
  1. package/CHANGELOG.md +84 -84
  2. package/README.md +194 -194
  3. package/dist/api-client/tasks.d.ts +1 -0
  4. package/dist/cli-init.js +21 -21
  5. package/dist/cli.js +26 -26
  6. package/dist/handlers/session.js +3 -1
  7. package/dist/handlers/tasks.js +7 -1
  8. package/dist/handlers/tool-docs.js +1216 -1216
  9. package/dist/index.js +73 -73
  10. package/dist/templates/agent-guidelines.d.ts +1 -1
  11. package/dist/templates/agent-guidelines.js +205 -205
  12. package/dist/templates/help-content.js +1621 -1621
  13. package/dist/tools/bodies-of-work.js +6 -6
  14. package/dist/tools/cloud-agents.js +22 -22
  15. package/dist/tools/milestones.js +2 -2
  16. package/dist/tools/requests.js +1 -1
  17. package/dist/tools/session.js +11 -11
  18. package/dist/tools/sprints.js +9 -9
  19. package/dist/tools/tasks.js +43 -35
  20. package/dist/tools/worktrees.js +14 -14
  21. package/dist/utils.js +11 -11
  22. package/docs/TOOLS.md +2687 -2685
  23. package/package.json +53 -53
  24. package/scripts/generate-docs.ts +212 -212
  25. package/scripts/version-bump.ts +203 -203
  26. package/src/api-client/blockers.ts +86 -86
  27. package/src/api-client/bodies-of-work.ts +194 -194
  28. package/src/api-client/chat.ts +50 -50
  29. package/src/api-client/connectors.ts +152 -152
  30. package/src/api-client/cost.ts +185 -185
  31. package/src/api-client/decisions.ts +87 -87
  32. package/src/api-client/deployment.ts +313 -313
  33. package/src/api-client/discovery.ts +81 -81
  34. package/src/api-client/fallback.ts +52 -52
  35. package/src/api-client/file-checkouts.ts +115 -115
  36. package/src/api-client/findings.ts +100 -100
  37. package/src/api-client/git-issues.ts +88 -88
  38. package/src/api-client/ideas.ts +112 -112
  39. package/src/api-client/index.ts +592 -592
  40. package/src/api-client/milestones.ts +83 -83
  41. package/src/api-client/organizations.ts +185 -185
  42. package/src/api-client/progress.ts +94 -94
  43. package/src/api-client/project.ts +181 -181
  44. package/src/api-client/requests.ts +54 -54
  45. package/src/api-client/session.ts +220 -220
  46. package/src/api-client/sprints.ts +227 -227
  47. package/src/api-client/subtasks.ts +57 -57
  48. package/src/api-client/tasks.ts +451 -450
  49. package/src/api-client/types.ts +32 -32
  50. package/src/api-client/validation.ts +60 -60
  51. package/src/api-client/worktrees.ts +53 -53
  52. package/src/api-client.test.ts +847 -847
  53. package/src/api-client.ts +2728 -2728
  54. package/src/cli-init.ts +558 -558
  55. package/src/cli.test.ts +284 -284
  56. package/src/cli.ts +204 -204
  57. package/src/handlers/__test-setup__.ts +240 -240
  58. package/src/handlers/__test-utils__.ts +89 -89
  59. package/src/handlers/blockers.test.ts +468 -468
  60. package/src/handlers/blockers.ts +172 -172
  61. package/src/handlers/bodies-of-work.test.ts +704 -704
  62. package/src/handlers/bodies-of-work.ts +526 -526
  63. package/src/handlers/chat.test.ts +185 -185
  64. package/src/handlers/chat.ts +101 -101
  65. package/src/handlers/cloud-agents.test.ts +438 -438
  66. package/src/handlers/cloud-agents.ts +156 -156
  67. package/src/handlers/connectors.test.ts +834 -834
  68. package/src/handlers/connectors.ts +229 -229
  69. package/src/handlers/cost.test.ts +462 -462
  70. package/src/handlers/cost.ts +285 -285
  71. package/src/handlers/decisions.test.ts +382 -382
  72. package/src/handlers/decisions.ts +153 -153
  73. package/src/handlers/deployment.test.ts +551 -551
  74. package/src/handlers/deployment.ts +570 -570
  75. package/src/handlers/discovery.test.ts +206 -206
  76. package/src/handlers/discovery.ts +433 -433
  77. package/src/handlers/fallback.test.ts +537 -537
  78. package/src/handlers/fallback.ts +194 -194
  79. package/src/handlers/file-checkouts.test.ts +750 -750
  80. package/src/handlers/file-checkouts.ts +185 -185
  81. package/src/handlers/findings.test.ts +633 -633
  82. package/src/handlers/findings.ts +239 -239
  83. package/src/handlers/git-issues.test.ts +631 -631
  84. package/src/handlers/git-issues.ts +136 -136
  85. package/src/handlers/ideas.test.ts +644 -644
  86. package/src/handlers/ideas.ts +207 -207
  87. package/src/handlers/index.ts +93 -93
  88. package/src/handlers/milestones.test.ts +475 -475
  89. package/src/handlers/milestones.ts +180 -180
  90. package/src/handlers/organizations.test.ts +826 -826
  91. package/src/handlers/organizations.ts +315 -315
  92. package/src/handlers/progress.test.ts +269 -269
  93. package/src/handlers/progress.ts +77 -77
  94. package/src/handlers/project.test.ts +546 -546
  95. package/src/handlers/project.ts +245 -245
  96. package/src/handlers/requests.test.ts +303 -303
  97. package/src/handlers/requests.ts +99 -99
  98. package/src/handlers/roles.test.ts +305 -305
  99. package/src/handlers/roles.ts +219 -219
  100. package/src/handlers/session.test.ts +998 -998
  101. package/src/handlers/session.ts +1107 -1105
  102. package/src/handlers/sprints.test.ts +732 -732
  103. package/src/handlers/sprints.ts +537 -537
  104. package/src/handlers/tasks.test.ts +931 -931
  105. package/src/handlers/tasks.ts +1144 -1137
  106. package/src/handlers/tool-categories.test.ts +66 -66
  107. package/src/handlers/tool-docs.test.ts +511 -511
  108. package/src/handlers/tool-docs.ts +1595 -1595
  109. package/src/handlers/types.test.ts +259 -259
  110. package/src/handlers/types.ts +176 -176
  111. package/src/handlers/validation.test.ts +582 -582
  112. package/src/handlers/validation.ts +164 -164
  113. package/src/handlers/version.ts +63 -63
  114. package/src/index.test.ts +674 -674
  115. package/src/index.ts +884 -884
  116. package/src/setup.test.ts +243 -243
  117. package/src/setup.ts +410 -410
  118. package/src/templates/agent-guidelines.ts +233 -233
  119. package/src/templates/help-content.ts +1751 -1751
  120. package/src/token-tracking.test.ts +463 -463
  121. package/src/token-tracking.ts +167 -167
  122. package/src/tools/blockers.ts +122 -122
  123. package/src/tools/bodies-of-work.ts +283 -283
  124. package/src/tools/chat.ts +72 -72
  125. package/src/tools/cloud-agents.ts +101 -101
  126. package/src/tools/connectors.ts +191 -191
  127. package/src/tools/cost.ts +111 -111
  128. package/src/tools/decisions.ts +111 -111
  129. package/src/tools/deployment.ts +455 -455
  130. package/src/tools/discovery.ts +76 -76
  131. package/src/tools/fallback.ts +111 -111
  132. package/src/tools/features.ts +154 -154
  133. package/src/tools/file-checkouts.ts +145 -145
  134. package/src/tools/findings.ts +101 -101
  135. package/src/tools/git-issues.ts +130 -130
  136. package/src/tools/ideas.ts +162 -162
  137. package/src/tools/index.ts +145 -145
  138. package/src/tools/milestones.ts +118 -118
  139. package/src/tools/organizations.ts +224 -224
  140. package/src/tools/persona-templates.ts +25 -25
  141. package/src/tools/progress.ts +73 -73
  142. package/src/tools/project.ts +210 -210
  143. package/src/tools/requests.ts +68 -68
  144. package/src/tools/roles.ts +112 -112
  145. package/src/tools/session.ts +181 -181
  146. package/src/tools/sprints.ts +298 -298
  147. package/src/tools/tasks.ts +583 -575
  148. package/src/tools/tools.test.ts +222 -222
  149. package/src/tools/types.ts +9 -9
  150. package/src/tools/validation.ts +75 -75
  151. package/src/tools/version.ts +34 -34
  152. package/src/tools/worktrees.ts +66 -66
  153. package/src/tools.test.ts +416 -416
  154. package/src/utils.test.ts +1014 -1014
  155. package/src/utils.ts +586 -586
  156. package/src/validators.test.ts +223 -223
  157. package/src/validators.ts +249 -249
  158. package/src/version.ts +162 -162
  159. package/tsconfig.json +16 -16
  160. package/vitest.config.ts +14 -14
  161. package/dist/tools.d.ts +0 -2
  162. package/dist/tools.js +0 -3602
package/src/utils.test.ts CHANGED
@@ -1,1014 +1,1014 @@
1
- import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
2
- import {
3
- AGENT_PERSONAS,
4
- FALLBACK_ACTIVITIES,
5
- getRandomFallbackActivity,
6
- selectPersona,
7
- RateLimiter,
8
- canValidateTask,
9
- isValidStatusTransition,
10
- isValidDeploymentStatusTransition,
11
- extractProjectNameFromGitUrl,
12
- normalizeGitUrl,
13
- getErrorMessage,
14
- hasErrorProperty,
15
- extractApiError,
16
- capPagination,
17
- PAGINATION_LIMITS,
18
- } from './utils.js';
19
-
20
- // ============================================================================
21
- // Agent Personas Tests
22
- // ============================================================================
23
-
24
- describe('AGENT_PERSONAS', () => {
25
- it('should have unique personas', () => {
26
- expect(AGENT_PERSONAS.length).toBeGreaterThanOrEqual(22);
27
- const uniquePersonas = new Set(AGENT_PERSONAS);
28
- expect(uniquePersonas.size).toBe(AGENT_PERSONAS.length);
29
- });
30
-
31
- it('should contain expected personas', () => {
32
- // Sci-fi & fantasy character names
33
- expect(AGENT_PERSONAS).toContain('Yoda');
34
- expect(AGENT_PERSONAS).toContain('Gandalf');
35
- expect(AGENT_PERSONAS).toContain('Spock');
36
- expect(AGENT_PERSONAS).toContain('Neo');
37
- });
38
-
39
- it('should have short memorable names', () => {
40
- // All personas should be reasonably short for dashboard display
41
- // Max 8 chars to accommodate names like "Morpheus"
42
- for (const persona of AGENT_PERSONAS) {
43
- expect(persona.length).toBeLessThanOrEqual(8);
44
- }
45
- });
46
- });
47
-
48
- // ============================================================================
49
- // Fallback Activities Tests
50
- // ============================================================================
51
-
52
- describe('FALLBACK_ACTIVITIES', () => {
53
- it('should have 12 activities', () => {
54
- expect(FALLBACK_ACTIVITIES).toHaveLength(12);
55
- });
56
-
57
- it('should have required fields on each activity', () => {
58
- for (const activity of FALLBACK_ACTIVITIES) {
59
- expect(activity).toHaveProperty('activity');
60
- expect(activity).toHaveProperty('title');
61
- expect(activity).toHaveProperty('description');
62
- expect(activity).toHaveProperty('prompt');
63
- expect(typeof activity.activity).toBe('string');
64
- expect(typeof activity.title).toBe('string');
65
- expect(typeof activity.description).toBe('string');
66
- expect(typeof activity.prompt).toBe('string');
67
- }
68
- });
69
-
70
- it('should have unique activity identifiers', () => {
71
- const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
72
- const uniqueIds = new Set(activityIds);
73
- expect(uniqueIds.size).toBe(FALLBACK_ACTIVITIES.length);
74
- });
75
-
76
- it('should include critical activities', () => {
77
- const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
78
- expect(activityIds).toContain('feature_ideation');
79
- expect(activityIds).toContain('code_review');
80
- expect(activityIds).toContain('security_review');
81
- expect(activityIds).toContain('test_coverage');
82
- expect(activityIds).toContain('validate_completed_tasks');
83
- });
84
- });
85
-
86
- describe('getRandomFallbackActivity', () => {
87
- it('should return a valid fallback activity', () => {
88
- const activity = getRandomFallbackActivity();
89
- expect(activity).toHaveProperty('activity');
90
- expect(activity).toHaveProperty('title');
91
- expect(activity).toHaveProperty('description');
92
- expect(activity).toHaveProperty('prompt');
93
- });
94
-
95
- it('should return activity from the pool', () => {
96
- const activity = getRandomFallbackActivity();
97
- const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
98
- expect(activityIds).toContain(activity.activity);
99
- });
100
- });
101
-
102
- // ============================================================================
103
- // Persona Selection Tests
104
- // ============================================================================
105
-
106
- describe('selectPersona', () => {
107
- it('should return a random persona when none are in use', () => {
108
- const usedPersonas = new Set<string>();
109
- const instanceId = 'abc123-def456';
110
-
111
- const selected = selectPersona(usedPersonas, instanceId);
112
-
113
- expect(AGENT_PERSONAS).toContain(selected);
114
- });
115
-
116
- it('should skip personas that are in use', () => {
117
- const usedPersonas = new Set<string>([AGENT_PERSONAS[0], AGENT_PERSONAS[1]]);
118
- const instanceId = 'abc123-def456';
119
-
120
- const selected = selectPersona(usedPersonas, instanceId);
121
-
122
- expect(usedPersonas.has(selected)).toBe(false);
123
- expect(AGENT_PERSONAS).toContain(selected);
124
- });
125
-
126
- it('should return fallback name when all personas are in use', () => {
127
- const usedPersonas = new Set(AGENT_PERSONAS);
128
- const instanceId = 'abc123-def456';
129
-
130
- const selected = selectPersona(usedPersonas, instanceId);
131
-
132
- expect(selected).toBe('Agent-abc123');
133
- });
134
-
135
- it('should handle partial instance ID', () => {
136
- const usedPersonas = new Set(AGENT_PERSONAS);
137
- const instanceId = 'xyz';
138
-
139
- const selected = selectPersona(usedPersonas, instanceId);
140
-
141
- expect(selected).toBe('Agent-xyz');
142
- });
143
-
144
- it('should be case-sensitive for used personas', () => {
145
- const firstPersona = AGENT_PERSONAS[0];
146
- const usedPersonas = new Set([firstPersona.toLowerCase(), firstPersona.toUpperCase()]); // Different case
147
- const instanceId = 'abc123-def456';
148
-
149
- const selected = selectPersona(usedPersonas, instanceId);
150
-
151
- // Original case versions are still available (case-sensitive match)
152
- expect(AGENT_PERSONAS).toContain(selected);
153
- expect(usedPersonas.has(selected)).toBe(false);
154
- });
155
- });
156
-
157
- // ============================================================================
158
- // Rate Limiter Tests
159
- // ============================================================================
160
-
161
- describe('RateLimiter', () => {
162
- let rateLimiter: RateLimiter;
163
-
164
- beforeEach(() => {
165
- vi.useFakeTimers();
166
- rateLimiter = new RateLimiter(5, 1000); // 5 requests per second for testing
167
- });
168
-
169
- afterEach(() => {
170
- vi.useRealTimers();
171
- });
172
-
173
- describe('constructor', () => {
174
- it('should use default values when none provided', () => {
175
- const defaultLimiter = new RateLimiter();
176
- const result = defaultLimiter.check('test');
177
- expect(result.remaining).toBe(59); // 60 - 1
178
- });
179
-
180
- it('should use custom values', () => {
181
- const customLimiter = new RateLimiter(10, 5000);
182
- const result = customLimiter.check('test');
183
- expect(result.remaining).toBe(9); // 10 - 1
184
- expect(result.resetIn).toBe(5000);
185
- });
186
- });
187
-
188
- describe('check', () => {
189
- it('should allow first request', () => {
190
- const result = rateLimiter.check('user-1');
191
-
192
- expect(result.allowed).toBe(true);
193
- expect(result.remaining).toBe(4);
194
- expect(result.resetIn).toBe(1000);
195
- });
196
-
197
- it('should decrement remaining count on subsequent requests', () => {
198
- rateLimiter.check('user-1');
199
- const result = rateLimiter.check('user-1');
200
-
201
- expect(result.allowed).toBe(true);
202
- expect(result.remaining).toBe(3);
203
- });
204
-
205
- it('should block requests when limit exceeded', () => {
206
- // Use up all requests
207
- for (let i = 0; i < 5; i++) {
208
- rateLimiter.check('user-1');
209
- }
210
-
211
- const result = rateLimiter.check('user-1');
212
-
213
- expect(result.allowed).toBe(false);
214
- expect(result.remaining).toBe(0);
215
- });
216
-
217
- it('should track different keys separately', () => {
218
- // Use up user-1's limit
219
- for (let i = 0; i < 5; i++) {
220
- rateLimiter.check('user-1');
221
- }
222
-
223
- // user-2 should still be allowed
224
- const result = rateLimiter.check('user-2');
225
-
226
- expect(result.allowed).toBe(true);
227
- expect(result.remaining).toBe(4);
228
- });
229
-
230
- it('should reset after window expires', () => {
231
- // Use up all requests
232
- for (let i = 0; i < 5; i++) {
233
- rateLimiter.check('user-1');
234
- }
235
-
236
- // Blocked
237
- expect(rateLimiter.check('user-1').allowed).toBe(false);
238
-
239
- // Advance time past window
240
- vi.advanceTimersByTime(1001);
241
-
242
- // Should be allowed again
243
- const result = rateLimiter.check('user-1');
244
- expect(result.allowed).toBe(true);
245
- expect(result.remaining).toBe(4);
246
- });
247
-
248
- it('should report correct resetIn time', () => {
249
- rateLimiter.check('user-1');
250
-
251
- // Advance time by 300ms
252
- vi.advanceTimersByTime(300);
253
-
254
- const result = rateLimiter.check('user-1');
255
- expect(result.resetIn).toBeLessThanOrEqual(700);
256
- expect(result.resetIn).toBeGreaterThan(0);
257
- });
258
- });
259
-
260
- describe('cleanup', () => {
261
- it('should remove expired entries', () => {
262
- rateLimiter.check('user-1');
263
- rateLimiter.check('user-2');
264
-
265
- expect(rateLimiter.getRequestCount('user-1')).toBe(1);
266
- expect(rateLimiter.getRequestCount('user-2')).toBe(1);
267
-
268
- // Advance past window
269
- vi.advanceTimersByTime(1001);
270
-
271
- rateLimiter.cleanup();
272
-
273
- expect(rateLimiter.getRequestCount('user-1')).toBeUndefined();
274
- expect(rateLimiter.getRequestCount('user-2')).toBeUndefined();
275
- });
276
-
277
- it('should keep non-expired entries', () => {
278
- rateLimiter.check('user-1');
279
-
280
- // Advance but not past window
281
- vi.advanceTimersByTime(500);
282
-
283
- rateLimiter.cleanup();
284
-
285
- expect(rateLimiter.getRequestCount('user-1')).toBe(1);
286
- });
287
- });
288
-
289
- describe('getRequestCount', () => {
290
- it('should return undefined for unknown key', () => {
291
- expect(rateLimiter.getRequestCount('unknown')).toBeUndefined();
292
- });
293
-
294
- it('should return correct count', () => {
295
- rateLimiter.check('user-1');
296
- rateLimiter.check('user-1');
297
- rateLimiter.check('user-1');
298
-
299
- expect(rateLimiter.getRequestCount('user-1')).toBe(3);
300
- });
301
- });
302
- });
303
-
304
- // ============================================================================
305
- // Task Validation Tests
306
- // ============================================================================
307
-
308
- describe('canValidateTask', () => {
309
- it('should allow validation of completed tasks', () => {
310
- const task = {
311
- status: 'completed',
312
- validated_at: null,
313
- working_agent_session_id: null,
314
- };
315
-
316
- const result = canValidateTask(task, 'session-123');
317
-
318
- expect(result.canValidate).toBe(true);
319
- expect(result.reason).toBeUndefined();
320
- });
321
-
322
- it('should reject non-completed tasks', () => {
323
- const statuses = ['pending', 'in_progress', 'cancelled'];
324
-
325
- for (const status of statuses) {
326
- const task = {
327
- status,
328
- validated_at: null,
329
- working_agent_session_id: null,
330
- };
331
-
332
- const result = canValidateTask(task, 'session-123');
333
-
334
- expect(result.canValidate).toBe(false);
335
- expect(result.reason).toBe('Can only validate completed tasks');
336
- }
337
- });
338
-
339
- it('should reject already validated tasks', () => {
340
- const task = {
341
- status: 'completed',
342
- validated_at: '2026-01-12T00:00:00Z',
343
- working_agent_session_id: null,
344
- };
345
-
346
- const result = canValidateTask(task, 'session-123');
347
-
348
- expect(result.canValidate).toBe(false);
349
- expect(result.reason).toBe('Task has already been validated');
350
- });
351
-
352
- it('should handle null validator session', () => {
353
- const task = {
354
- status: 'completed',
355
- validated_at: null,
356
- working_agent_session_id: null,
357
- };
358
-
359
- const result = canValidateTask(task, null);
360
-
361
- expect(result.canValidate).toBe(true);
362
- });
363
- });
364
-
365
- // ============================================================================
366
- // Status Transition Tests
367
- // ============================================================================
368
-
369
- describe('isValidStatusTransition', () => {
370
- describe('from pending', () => {
371
- it('should allow transition to in_progress', () => {
372
- const result = isValidStatusTransition('pending', 'in_progress');
373
- expect(result.isValid).toBe(true);
374
- });
375
-
376
- it('should allow transition to cancelled', () => {
377
- const result = isValidStatusTransition('pending', 'cancelled');
378
- expect(result.isValid).toBe(true);
379
- });
380
-
381
- it('should reject transition to completed', () => {
382
- const result = isValidStatusTransition('pending', 'completed');
383
- expect(result.isValid).toBe(false);
384
- expect(result.reason).toContain('Cannot transition');
385
- });
386
- });
387
-
388
- describe('from in_progress', () => {
389
- it('should allow transition to completed', () => {
390
- const result = isValidStatusTransition('in_progress', 'completed');
391
- expect(result.isValid).toBe(true);
392
- });
393
-
394
- it('should allow transition to pending', () => {
395
- const result = isValidStatusTransition('in_progress', 'pending');
396
- expect(result.isValid).toBe(true);
397
- });
398
-
399
- it('should allow transition to cancelled', () => {
400
- const result = isValidStatusTransition('in_progress', 'cancelled');
401
- expect(result.isValid).toBe(true);
402
- });
403
- });
404
-
405
- describe('from completed', () => {
406
- it('should allow transition to in_progress (validation failure)', () => {
407
- const result = isValidStatusTransition('completed', 'in_progress');
408
- expect(result.isValid).toBe(true);
409
- });
410
-
411
- it('should reject transition to pending', () => {
412
- const result = isValidStatusTransition('completed', 'pending');
413
- expect(result.isValid).toBe(false);
414
- });
415
-
416
- it('should reject transition to cancelled', () => {
417
- const result = isValidStatusTransition('completed', 'cancelled');
418
- expect(result.isValid).toBe(false);
419
- });
420
- });
421
-
422
- describe('from cancelled', () => {
423
- it('should allow transition to pending (reactivate)', () => {
424
- const result = isValidStatusTransition('cancelled', 'pending');
425
- expect(result.isValid).toBe(true);
426
- });
427
-
428
- it('should reject transition to in_progress', () => {
429
- const result = isValidStatusTransition('cancelled', 'in_progress');
430
- expect(result.isValid).toBe(false);
431
- });
432
-
433
- it('should reject transition to completed', () => {
434
- const result = isValidStatusTransition('cancelled', 'completed');
435
- expect(result.isValid).toBe(false);
436
- });
437
- });
438
-
439
- describe('unknown status', () => {
440
- it('should reject unknown current status', () => {
441
- const result = isValidStatusTransition('unknown', 'pending');
442
- expect(result.isValid).toBe(false);
443
- expect(result.reason).toContain('Unknown current status');
444
- });
445
- });
446
- });
447
-
448
- // ============================================================================
449
- // Deployment Status Transition Tests
450
- // ============================================================================
451
-
452
- describe('isValidDeploymentStatusTransition', () => {
453
- describe('from pending', () => {
454
- it('should allow pending → validating', () => {
455
- const result = isValidDeploymentStatusTransition('pending', 'validating');
456
- expect(result.isValid).toBe(true);
457
- });
458
-
459
- it('should allow pending → failed', () => {
460
- const result = isValidDeploymentStatusTransition('pending', 'failed');
461
- expect(result.isValid).toBe(true);
462
- });
463
-
464
- it('should reject pending → ready', () => {
465
- const result = isValidDeploymentStatusTransition('pending', 'ready');
466
- expect(result.isValid).toBe(false);
467
- });
468
-
469
- it('should reject pending → deployed', () => {
470
- const result = isValidDeploymentStatusTransition('pending', 'deployed');
471
- expect(result.isValid).toBe(false);
472
- });
473
- });
474
-
475
- describe('from validating', () => {
476
- it('should allow validating → ready', () => {
477
- const result = isValidDeploymentStatusTransition('validating', 'ready');
478
- expect(result.isValid).toBe(true);
479
- });
480
-
481
- it('should allow validating → failed', () => {
482
- const result = isValidDeploymentStatusTransition('validating', 'failed');
483
- expect(result.isValid).toBe(true);
484
- });
485
-
486
- it('should reject validating → deployed', () => {
487
- const result = isValidDeploymentStatusTransition('validating', 'deployed');
488
- expect(result.isValid).toBe(false);
489
- });
490
- });
491
-
492
- describe('from ready', () => {
493
- it('should allow ready → deploying', () => {
494
- const result = isValidDeploymentStatusTransition('ready', 'deploying');
495
- expect(result.isValid).toBe(true);
496
- });
497
-
498
- it('should allow ready → failed', () => {
499
- const result = isValidDeploymentStatusTransition('ready', 'failed');
500
- expect(result.isValid).toBe(true);
501
- });
502
-
503
- it('should reject ready → pending', () => {
504
- const result = isValidDeploymentStatusTransition('ready', 'pending');
505
- expect(result.isValid).toBe(false);
506
- });
507
- });
508
-
509
- describe('from deploying', () => {
510
- it('should allow deploying → deployed', () => {
511
- const result = isValidDeploymentStatusTransition('deploying', 'deployed');
512
- expect(result.isValid).toBe(true);
513
- });
514
-
515
- it('should allow deploying → failed', () => {
516
- const result = isValidDeploymentStatusTransition('deploying', 'failed');
517
- expect(result.isValid).toBe(true);
518
- });
519
-
520
- it('should reject deploying → pending', () => {
521
- const result = isValidDeploymentStatusTransition('deploying', 'pending');
522
- expect(result.isValid).toBe(false);
523
- });
524
- });
525
-
526
- describe('terminal states', () => {
527
- it('should reject deployed → any transition', () => {
528
- const result = isValidDeploymentStatusTransition('deployed', 'pending');
529
- expect(result.isValid).toBe(false);
530
- expect(result.reason).toContain('terminal state');
531
- });
532
-
533
- it('should reject failed → any transition', () => {
534
- const result = isValidDeploymentStatusTransition('failed', 'pending');
535
- expect(result.isValid).toBe(false);
536
- expect(result.reason).toContain('terminal state');
537
- });
538
- });
539
-
540
- describe('unknown status', () => {
541
- it('should reject unknown current status', () => {
542
- const result = isValidDeploymentStatusTransition('unknown', 'pending');
543
- expect(result.isValid).toBe(false);
544
- expect(result.reason).toContain('Unknown current status');
545
- });
546
- });
547
- });
548
-
549
- // ============================================================================
550
- // Git URL Parsing Tests
551
- // ============================================================================
552
-
553
- describe('extractProjectNameFromGitUrl', () => {
554
- it('should extract project name from https GitHub URL', () => {
555
- expect(extractProjectNameFromGitUrl('https://github.com/user/repo')).toBe('repo');
556
- });
557
-
558
- it('should extract project name from https GitHub URL with .git', () => {
559
- expect(extractProjectNameFromGitUrl('https://github.com/user/repo.git')).toBe('repo');
560
- });
561
-
562
- it('should extract project name from SSH GitHub URL', () => {
563
- expect(extractProjectNameFromGitUrl('git@github.com:user/repo.git')).toBe('repo');
564
- });
565
-
566
- it('should extract project name from SSH URL without .git', () => {
567
- expect(extractProjectNameFromGitUrl('git@github.com:user/repo')).toBe('repo');
568
- });
569
-
570
- it('should extract project name from GitLab URL', () => {
571
- expect(extractProjectNameFromGitUrl('https://gitlab.com/user/project-name')).toBe('project-name');
572
- });
573
-
574
- it('should extract project name from ssh:// protocol URL', () => {
575
- expect(extractProjectNameFromGitUrl('ssh://git@github.com/user/repo.git')).toBe('repo');
576
- });
577
-
578
- it('should handle nested paths (monorepo)', () => {
579
- expect(extractProjectNameFromGitUrl('https://github.com/org/monorepo')).toBe('monorepo');
580
- });
581
-
582
- it('should handle dashes and underscores in project names', () => {
583
- expect(extractProjectNameFromGitUrl('https://github.com/user/my-awesome_project')).toBe('my-awesome_project');
584
- });
585
-
586
- it('should return default for empty string', () => {
587
- expect(extractProjectNameFromGitUrl('')).toBe('my-project');
588
- });
589
-
590
- it('should return default for malformed URL', () => {
591
- expect(extractProjectNameFromGitUrl('not-a-url')).toBe('my-project');
592
- });
593
-
594
- it('should handle real Vibescope URL', () => {
595
- expect(extractProjectNameFromGitUrl('https://github.com/Nonatomic/Vibescope')).toBe('Vibescope');
596
- });
597
- });
598
-
599
- // ============================================================================
600
- // normalizeGitUrl Tests
601
- // ============================================================================
602
-
603
- describe('normalizeGitUrl', () => {
604
- describe('null and undefined handling', () => {
605
- it('returns null for null input', () => {
606
- expect(normalizeGitUrl(null)).toBe(null);
607
- });
608
-
609
- it('returns null for undefined input', () => {
610
- expect(normalizeGitUrl(undefined)).toBe(null);
611
- });
612
-
613
- it('returns null for empty string', () => {
614
- expect(normalizeGitUrl('')).toBe(null);
615
- });
616
-
617
- it('returns null for whitespace-only string', () => {
618
- expect(normalizeGitUrl(' ')).toBe(null);
619
- });
620
- });
621
-
622
- describe('SSH format conversion', () => {
623
- it('converts SSH format to HTTPS', () => {
624
- expect(normalizeGitUrl('git@github.com:owner/repo')).toBe('https://github.com/owner/repo');
625
- });
626
-
627
- it('converts SSH format with .git suffix', () => {
628
- expect(normalizeGitUrl('git@github.com:owner/repo.git')).toBe('https://github.com/owner/repo');
629
- });
630
-
631
- it('converts GitLab SSH format', () => {
632
- expect(normalizeGitUrl('git@gitlab.com:group/project')).toBe('https://gitlab.com/group/project');
633
- });
634
-
635
- it('converts Bitbucket SSH format', () => {
636
- expect(normalizeGitUrl('git@bitbucket.org:team/repo')).toBe('https://bitbucket.org/team/repo');
637
- });
638
-
639
- it('handles SSH format with nested paths', () => {
640
- expect(normalizeGitUrl('git@github.com:org/group/repo')).toBe('https://github.com/org/group/repo');
641
- });
642
-
643
- it('converts self-hosted git SSH format', () => {
644
- expect(normalizeGitUrl('git@selfhosted.example.com:user/repo.git')).toBe('https://selfhosted.example.com/user/repo');
645
- });
646
- });
647
-
648
- describe('.git suffix removal', () => {
649
- it('removes .git suffix from HTTPS URL', () => {
650
- expect(normalizeGitUrl('https://github.com/owner/repo.git')).toBe('https://github.com/owner/repo');
651
- });
652
-
653
- it('handles URL without .git suffix', () => {
654
- expect(normalizeGitUrl('https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
655
- });
656
-
657
- it('does not remove .git from middle of URL', () => {
658
- expect(normalizeGitUrl('https://github.com/owner/repo.github.io')).toBe('https://github.com/owner/repo.github.io');
659
- });
660
- });
661
-
662
- describe('trailing slash removal', () => {
663
- it('removes single trailing slash', () => {
664
- expect(normalizeGitUrl('https://github.com/owner/repo/')).toBe('https://github.com/owner/repo');
665
- });
666
-
667
- it('removes multiple trailing slashes', () => {
668
- expect(normalizeGitUrl('https://github.com/owner/repo///')).toBe('https://github.com/owner/repo');
669
- });
670
-
671
- it('handles URL without trailing slash', () => {
672
- expect(normalizeGitUrl('https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
673
- });
674
- });
675
-
676
- describe('case normalization', () => {
677
- it('lowercases the entire URL', () => {
678
- expect(normalizeGitUrl('https://GitHub.com/Owner/Repo')).toBe('https://github.com/owner/repo');
679
- });
680
-
681
- it('lowercases mixed case URLs', () => {
682
- expect(normalizeGitUrl('https://GITHUB.COM/NONATOMIC/VIBESCOPE')).toBe('https://github.com/nonatomic/vibescope');
683
- });
684
-
685
- it('lowercases SSH format before conversion', () => {
686
- expect(normalizeGitUrl('git@GitHub.com:Owner/Repo')).toBe('https://github.com/owner/repo');
687
- });
688
- });
689
-
690
- describe('protocol handling', () => {
691
- it('upgrades http to https', () => {
692
- expect(normalizeGitUrl('http://github.com/owner/repo')).toBe('https://github.com/owner/repo');
693
- });
694
-
695
- it('preserves existing https protocol', () => {
696
- expect(normalizeGitUrl('https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
697
- });
698
-
699
- it('adds https protocol to bare domain URLs', () => {
700
- expect(normalizeGitUrl('github.com/owner/repo')).toBe('https://github.com/owner/repo');
701
- });
702
- });
703
-
704
- describe('whitespace handling', () => {
705
- it('trims leading whitespace', () => {
706
- expect(normalizeGitUrl(' https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
707
- });
708
-
709
- it('trims trailing whitespace', () => {
710
- expect(normalizeGitUrl('https://github.com/owner/repo ')).toBe('https://github.com/owner/repo');
711
- });
712
-
713
- it('trims both leading and trailing whitespace', () => {
714
- expect(normalizeGitUrl(' https://github.com/owner/repo ')).toBe('https://github.com/owner/repo');
715
- });
716
- });
717
-
718
- describe('combined transformations', () => {
719
- it('handles SSH with .git, different casing', () => {
720
- expect(normalizeGitUrl('git@GitHub.com:Nonatomic/Vibescope.git')).toBe('https://github.com/nonatomic/vibescope');
721
- });
722
-
723
- it('handles HTTPS with trailing slash, .git suffix, and casing', () => {
724
- expect(normalizeGitUrl('https://GitHub.com/Nonatomic/Vibescope.git/')).toBe('https://github.com/nonatomic/vibescope');
725
- });
726
-
727
- it('handles http with trailing slashes and casing', () => {
728
- expect(normalizeGitUrl('http://GitHub.com/Owner/Repo//')).toBe('https://github.com/owner/repo');
729
- });
730
-
731
- it('handles bare domain with .git and trailing slash', () => {
732
- expect(normalizeGitUrl('github.com/owner/repo.git/')).toBe('https://github.com/owner/repo');
733
- });
734
- });
735
-
736
- describe('real-world examples', () => {
737
- it('normalizes Vibescope registered URL', () => {
738
- expect(normalizeGitUrl('https://github.com/Nonatomic/Vibescope')).toBe('https://github.com/nonatomic/vibescope');
739
- });
740
-
741
- it('normalizes Vibescope with .git suffix', () => {
742
- expect(normalizeGitUrl('https://github.com/Nonatomic/Vibescope.git')).toBe('https://github.com/nonatomic/vibescope');
743
- });
744
-
745
- it('normalizes Vibescope SSH format', () => {
746
- expect(normalizeGitUrl('git@github.com:Nonatomic/Vibescope.git')).toBe('https://github.com/nonatomic/vibescope');
747
- });
748
-
749
- it('normalizes Vibescope with different casing', () => {
750
- expect(normalizeGitUrl('https://github.com/nonatomic/vibescope')).toBe('https://github.com/nonatomic/vibescope');
751
- });
752
-
753
- it('normalizes Vibescope with trailing slash', () => {
754
- expect(normalizeGitUrl('https://github.com/Nonatomic/Vibescope/')).toBe('https://github.com/nonatomic/vibescope');
755
- });
756
-
757
- it('all Vibescope variants normalize to same value', () => {
758
- const canonical = 'https://github.com/nonatomic/vibescope';
759
-
760
- const variants = [
761
- 'https://github.com/Nonatomic/Vibescope',
762
- 'https://github.com/Nonatomic/Vibescope.git',
763
- 'git@github.com:Nonatomic/Vibescope.git',
764
- 'https://github.com/nonatomic/vibescope',
765
- 'https://github.com/Nonatomic/Vibescope/',
766
- 'https://GITHUB.COM/NONATOMIC/VIBESCOPE',
767
- 'http://github.com/Nonatomic/Vibescope',
768
- 'github.com/Nonatomic/Vibescope',
769
- ];
770
-
771
- for (const variant of variants) {
772
- expect(normalizeGitUrl(variant)).toBe(canonical);
773
- }
774
- });
775
- });
776
-
777
- describe('edge cases', () => {
778
- it('handles complex paths with dashes and underscores', () => {
779
- expect(normalizeGitUrl('git@github.com:my-org/my_awesome-project.git')).toBe('https://github.com/my-org/my_awesome-project');
780
- });
781
-
782
- it('handles nested paths', () => {
783
- expect(normalizeGitUrl('https://gitlab.com/group/subgroup/repo.git')).toBe('https://gitlab.com/group/subgroup/repo');
784
- });
785
-
786
- it('handles URLs with port numbers', () => {
787
- expect(normalizeGitUrl('https://github.com:443/user/repo.git')).toBe('https://github.com:443/user/repo');
788
- });
789
- });
790
- });
791
-
792
- // ============================================================================
793
- // Error Handling Utilities Tests
794
- // ============================================================================
795
-
796
- describe('getErrorMessage', () => {
797
- it('extracts message from Error object', () => {
798
- const error = new Error('Something went wrong');
799
- expect(getErrorMessage(error)).toBe('Something went wrong');
800
- });
801
-
802
- it('extracts message from TypeError', () => {
803
- const error = new TypeError('Invalid type');
804
- expect(getErrorMessage(error)).toBe('Invalid type');
805
- });
806
-
807
- it('returns string errors as-is', () => {
808
- expect(getErrorMessage('Direct error string')).toBe('Direct error string');
809
- });
810
-
811
- it('converts numbers to string', () => {
812
- expect(getErrorMessage(404)).toBe('404');
813
- });
814
-
815
- it('converts null to string', () => {
816
- expect(getErrorMessage(null)).toBe('null');
817
- });
818
-
819
- it('converts undefined to string', () => {
820
- expect(getErrorMessage(undefined)).toBe('undefined');
821
- });
822
-
823
- it('converts objects to string', () => {
824
- expect(getErrorMessage({ foo: 'bar' })).toBe('[object Object]');
825
- });
826
-
827
- it('handles empty Error message', () => {
828
- const error = new Error('');
829
- expect(getErrorMessage(error)).toBe('');
830
- });
831
- });
832
-
833
- describe('hasErrorProperty', () => {
834
- it('returns true for object with error property', () => {
835
- expect(hasErrorProperty({ error: 'some error' })).toBe(true);
836
- });
837
-
838
- it('returns true for object with error as object', () => {
839
- expect(hasErrorProperty({ error: { message: 'nested' } })).toBe(true);
840
- });
841
-
842
- it('returns true for object with null error', () => {
843
- expect(hasErrorProperty({ error: null })).toBe(true);
844
- });
845
-
846
- it('returns false for null', () => {
847
- expect(hasErrorProperty(null)).toBe(false);
848
- });
849
-
850
- it('returns false for undefined', () => {
851
- expect(hasErrorProperty(undefined)).toBe(false);
852
- });
853
-
854
- it('returns false for string', () => {
855
- expect(hasErrorProperty('error')).toBe(false);
856
- });
857
-
858
- it('returns false for object without error property', () => {
859
- expect(hasErrorProperty({ success: true })).toBe(false);
860
- });
861
-
862
- it('returns false for empty object', () => {
863
- expect(hasErrorProperty({})).toBe(false);
864
- });
865
-
866
- it('returns false for array', () => {
867
- expect(hasErrorProperty(['error'])).toBe(false);
868
- });
869
- });
870
-
871
- describe('extractApiError', () => {
872
- it('extracts string error from response', () => {
873
- expect(extractApiError({ error: 'Not found' })).toBe('Not found');
874
- });
875
-
876
- it('extracts message from nested error object', () => {
877
- expect(extractApiError({ error: { message: 'Validation failed' } })).toBe('Validation failed');
878
- });
879
-
880
- it('returns "Unknown error" for non-string error without message', () => {
881
- expect(extractApiError({ error: { code: 500 } })).toBe('Unknown error');
882
- });
883
-
884
- it('returns null for data without error property', () => {
885
- expect(extractApiError({ success: true })).toBeNull();
886
- });
887
-
888
- it('returns null for null input', () => {
889
- expect(extractApiError(null)).toBeNull();
890
- });
891
-
892
- it('returns null for undefined input', () => {
893
- expect(extractApiError(undefined)).toBeNull();
894
- });
895
-
896
- it('returns null for string input', () => {
897
- expect(extractApiError('error')).toBeNull();
898
- });
899
-
900
- it('returns null for empty object', () => {
901
- expect(extractApiError({})).toBeNull();
902
- });
903
-
904
- it('handles empty string error', () => {
905
- expect(extractApiError({ error: '' })).toBe('');
906
- });
907
-
908
- it('returns "Unknown error" for error as number', () => {
909
- expect(extractApiError({ error: 404 })).toBe('Unknown error');
910
- });
911
-
912
- it('returns "Unknown error" for error as boolean', () => {
913
- expect(extractApiError({ error: true })).toBe('Unknown error');
914
- });
915
-
916
- it('handles deeply nested message', () => {
917
- // Only extracts from immediate .message property
918
- expect(extractApiError({ error: { nested: { message: 'deep' } } })).toBe('Unknown error');
919
- });
920
- });
921
-
922
- // ============================================================================
923
- // Pagination Utilities Tests
924
- // ============================================================================
925
-
926
- describe('PAGINATION_LIMITS', () => {
927
- it('should have expected default values', () => {
928
- expect(PAGINATION_LIMITS.DEFAULT_MAX_LIMIT).toBe(50);
929
- expect(PAGINATION_LIMITS.DEFAULT_LIMIT).toBe(20);
930
- expect(PAGINATION_LIMITS.TASK_LIMIT).toBe(20);
931
- });
932
- });
933
-
934
- describe('capPagination', () => {
935
- describe('limit capping', () => {
936
- it('should cap limit to maxLimit when exceeded', () => {
937
- const { cappedLimit } = capPagination(100, 0, 50);
938
- expect(cappedLimit).toBe(50);
939
- });
940
-
941
- it('should use limit when under maxLimit', () => {
942
- const { cappedLimit } = capPagination(25, 0, 50);
943
- expect(cappedLimit).toBe(25);
944
- });
945
-
946
- it('should use default maxLimit when not specified', () => {
947
- const { cappedLimit } = capPagination(100, 0);
948
- expect(cappedLimit).toBe(PAGINATION_LIMITS.DEFAULT_MAX_LIMIT);
949
- });
950
-
951
- it('should enforce minimum limit of 1', () => {
952
- const { cappedLimit } = capPagination(0, 0, 50);
953
- expect(cappedLimit).toBe(1);
954
- });
955
-
956
- it('should handle negative limit', () => {
957
- const { cappedLimit } = capPagination(-5, 0, 50);
958
- expect(cappedLimit).toBe(1);
959
- });
960
-
961
- it('should use DEFAULT_LIMIT when limit is undefined', () => {
962
- const { cappedLimit } = capPagination(undefined, 0, 50);
963
- expect(cappedLimit).toBe(PAGINATION_LIMITS.DEFAULT_LIMIT);
964
- });
965
- });
966
-
967
- describe('offset safety', () => {
968
- it('should keep positive offset unchanged', () => {
969
- const { safeOffset } = capPagination(10, 50, 50);
970
- expect(safeOffset).toBe(50);
971
- });
972
-
973
- it('should convert negative offset to 0', () => {
974
- const { safeOffset } = capPagination(10, -10, 50);
975
- expect(safeOffset).toBe(0);
976
- });
977
-
978
- it('should handle zero offset', () => {
979
- const { safeOffset } = capPagination(10, 0, 50);
980
- expect(safeOffset).toBe(0);
981
- });
982
-
983
- it('should use 0 when offset is undefined', () => {
984
- const { safeOffset } = capPagination(10, undefined, 50);
985
- expect(safeOffset).toBe(0);
986
- });
987
- });
988
-
989
- describe('combined behavior', () => {
990
- it('should return both cappedLimit and safeOffset', () => {
991
- const result = capPagination(100, -5, 20);
992
- expect(result).toEqual({
993
- cappedLimit: 20,
994
- safeOffset: 0,
995
- });
996
- });
997
-
998
- it('should work with task limit constant', () => {
999
- const result = capPagination(50, 10, PAGINATION_LIMITS.TASK_LIMIT);
1000
- expect(result).toEqual({
1001
- cappedLimit: 20,
1002
- safeOffset: 10,
1003
- });
1004
- });
1005
-
1006
- it('should handle all undefined values', () => {
1007
- const result = capPagination(undefined, undefined);
1008
- expect(result).toEqual({
1009
- cappedLimit: PAGINATION_LIMITS.DEFAULT_LIMIT,
1010
- safeOffset: 0,
1011
- });
1012
- });
1013
- });
1014
- });
1
+ import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
2
+ import {
3
+ AGENT_PERSONAS,
4
+ FALLBACK_ACTIVITIES,
5
+ getRandomFallbackActivity,
6
+ selectPersona,
7
+ RateLimiter,
8
+ canValidateTask,
9
+ isValidStatusTransition,
10
+ isValidDeploymentStatusTransition,
11
+ extractProjectNameFromGitUrl,
12
+ normalizeGitUrl,
13
+ getErrorMessage,
14
+ hasErrorProperty,
15
+ extractApiError,
16
+ capPagination,
17
+ PAGINATION_LIMITS,
18
+ } from './utils.js';
19
+
20
+ // ============================================================================
21
+ // Agent Personas Tests
22
+ // ============================================================================
23
+
24
+ describe('AGENT_PERSONAS', () => {
25
+ it('should have unique personas', () => {
26
+ expect(AGENT_PERSONAS.length).toBeGreaterThanOrEqual(22);
27
+ const uniquePersonas = new Set(AGENT_PERSONAS);
28
+ expect(uniquePersonas.size).toBe(AGENT_PERSONAS.length);
29
+ });
30
+
31
+ it('should contain expected personas', () => {
32
+ // Sci-fi & fantasy character names
33
+ expect(AGENT_PERSONAS).toContain('Yoda');
34
+ expect(AGENT_PERSONAS).toContain('Gandalf');
35
+ expect(AGENT_PERSONAS).toContain('Spock');
36
+ expect(AGENT_PERSONAS).toContain('Neo');
37
+ });
38
+
39
+ it('should have short memorable names', () => {
40
+ // All personas should be reasonably short for dashboard display
41
+ // Max 8 chars to accommodate names like "Morpheus"
42
+ for (const persona of AGENT_PERSONAS) {
43
+ expect(persona.length).toBeLessThanOrEqual(8);
44
+ }
45
+ });
46
+ });
47
+
48
+ // ============================================================================
49
+ // Fallback Activities Tests
50
+ // ============================================================================
51
+
52
+ describe('FALLBACK_ACTIVITIES', () => {
53
+ it('should have 12 activities', () => {
54
+ expect(FALLBACK_ACTIVITIES).toHaveLength(12);
55
+ });
56
+
57
+ it('should have required fields on each activity', () => {
58
+ for (const activity of FALLBACK_ACTIVITIES) {
59
+ expect(activity).toHaveProperty('activity');
60
+ expect(activity).toHaveProperty('title');
61
+ expect(activity).toHaveProperty('description');
62
+ expect(activity).toHaveProperty('prompt');
63
+ expect(typeof activity.activity).toBe('string');
64
+ expect(typeof activity.title).toBe('string');
65
+ expect(typeof activity.description).toBe('string');
66
+ expect(typeof activity.prompt).toBe('string');
67
+ }
68
+ });
69
+
70
+ it('should have unique activity identifiers', () => {
71
+ const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
72
+ const uniqueIds = new Set(activityIds);
73
+ expect(uniqueIds.size).toBe(FALLBACK_ACTIVITIES.length);
74
+ });
75
+
76
+ it('should include critical activities', () => {
77
+ const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
78
+ expect(activityIds).toContain('feature_ideation');
79
+ expect(activityIds).toContain('code_review');
80
+ expect(activityIds).toContain('security_review');
81
+ expect(activityIds).toContain('test_coverage');
82
+ expect(activityIds).toContain('validate_completed_tasks');
83
+ });
84
+ });
85
+
86
+ describe('getRandomFallbackActivity', () => {
87
+ it('should return a valid fallback activity', () => {
88
+ const activity = getRandomFallbackActivity();
89
+ expect(activity).toHaveProperty('activity');
90
+ expect(activity).toHaveProperty('title');
91
+ expect(activity).toHaveProperty('description');
92
+ expect(activity).toHaveProperty('prompt');
93
+ });
94
+
95
+ it('should return activity from the pool', () => {
96
+ const activity = getRandomFallbackActivity();
97
+ const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
98
+ expect(activityIds).toContain(activity.activity);
99
+ });
100
+ });
101
+
102
+ // ============================================================================
103
+ // Persona Selection Tests
104
+ // ============================================================================
105
+
106
+ describe('selectPersona', () => {
107
+ it('should return a random persona when none are in use', () => {
108
+ const usedPersonas = new Set<string>();
109
+ const instanceId = 'abc123-def456';
110
+
111
+ const selected = selectPersona(usedPersonas, instanceId);
112
+
113
+ expect(AGENT_PERSONAS).toContain(selected);
114
+ });
115
+
116
+ it('should skip personas that are in use', () => {
117
+ const usedPersonas = new Set<string>([AGENT_PERSONAS[0], AGENT_PERSONAS[1]]);
118
+ const instanceId = 'abc123-def456';
119
+
120
+ const selected = selectPersona(usedPersonas, instanceId);
121
+
122
+ expect(usedPersonas.has(selected)).toBe(false);
123
+ expect(AGENT_PERSONAS).toContain(selected);
124
+ });
125
+
126
+ it('should return fallback name when all personas are in use', () => {
127
+ const usedPersonas = new Set(AGENT_PERSONAS);
128
+ const instanceId = 'abc123-def456';
129
+
130
+ const selected = selectPersona(usedPersonas, instanceId);
131
+
132
+ expect(selected).toBe('Agent-abc123');
133
+ });
134
+
135
+ it('should handle partial instance ID', () => {
136
+ const usedPersonas = new Set(AGENT_PERSONAS);
137
+ const instanceId = 'xyz';
138
+
139
+ const selected = selectPersona(usedPersonas, instanceId);
140
+
141
+ expect(selected).toBe('Agent-xyz');
142
+ });
143
+
144
+ it('should be case-sensitive for used personas', () => {
145
+ const firstPersona = AGENT_PERSONAS[0];
146
+ const usedPersonas = new Set([firstPersona.toLowerCase(), firstPersona.toUpperCase()]); // Different case
147
+ const instanceId = 'abc123-def456';
148
+
149
+ const selected = selectPersona(usedPersonas, instanceId);
150
+
151
+ // Original case versions are still available (case-sensitive match)
152
+ expect(AGENT_PERSONAS).toContain(selected);
153
+ expect(usedPersonas.has(selected)).toBe(false);
154
+ });
155
+ });
156
+
157
+ // ============================================================================
158
+ // Rate Limiter Tests
159
+ // ============================================================================
160
+
161
+ describe('RateLimiter', () => {
162
+ let rateLimiter: RateLimiter;
163
+
164
+ beforeEach(() => {
165
+ vi.useFakeTimers();
166
+ rateLimiter = new RateLimiter(5, 1000); // 5 requests per second for testing
167
+ });
168
+
169
+ afterEach(() => {
170
+ vi.useRealTimers();
171
+ });
172
+
173
+ describe('constructor', () => {
174
+ it('should use default values when none provided', () => {
175
+ const defaultLimiter = new RateLimiter();
176
+ const result = defaultLimiter.check('test');
177
+ expect(result.remaining).toBe(59); // 60 - 1
178
+ });
179
+
180
+ it('should use custom values', () => {
181
+ const customLimiter = new RateLimiter(10, 5000);
182
+ const result = customLimiter.check('test');
183
+ expect(result.remaining).toBe(9); // 10 - 1
184
+ expect(result.resetIn).toBe(5000);
185
+ });
186
+ });
187
+
188
+ describe('check', () => {
189
+ it('should allow first request', () => {
190
+ const result = rateLimiter.check('user-1');
191
+
192
+ expect(result.allowed).toBe(true);
193
+ expect(result.remaining).toBe(4);
194
+ expect(result.resetIn).toBe(1000);
195
+ });
196
+
197
+ it('should decrement remaining count on subsequent requests', () => {
198
+ rateLimiter.check('user-1');
199
+ const result = rateLimiter.check('user-1');
200
+
201
+ expect(result.allowed).toBe(true);
202
+ expect(result.remaining).toBe(3);
203
+ });
204
+
205
+ it('should block requests when limit exceeded', () => {
206
+ // Use up all requests
207
+ for (let i = 0; i < 5; i++) {
208
+ rateLimiter.check('user-1');
209
+ }
210
+
211
+ const result = rateLimiter.check('user-1');
212
+
213
+ expect(result.allowed).toBe(false);
214
+ expect(result.remaining).toBe(0);
215
+ });
216
+
217
+ it('should track different keys separately', () => {
218
+ // Use up user-1's limit
219
+ for (let i = 0; i < 5; i++) {
220
+ rateLimiter.check('user-1');
221
+ }
222
+
223
+ // user-2 should still be allowed
224
+ const result = rateLimiter.check('user-2');
225
+
226
+ expect(result.allowed).toBe(true);
227
+ expect(result.remaining).toBe(4);
228
+ });
229
+
230
+ it('should reset after window expires', () => {
231
+ // Use up all requests
232
+ for (let i = 0; i < 5; i++) {
233
+ rateLimiter.check('user-1');
234
+ }
235
+
236
+ // Blocked
237
+ expect(rateLimiter.check('user-1').allowed).toBe(false);
238
+
239
+ // Advance time past window
240
+ vi.advanceTimersByTime(1001);
241
+
242
+ // Should be allowed again
243
+ const result = rateLimiter.check('user-1');
244
+ expect(result.allowed).toBe(true);
245
+ expect(result.remaining).toBe(4);
246
+ });
247
+
248
+ it('should report correct resetIn time', () => {
249
+ rateLimiter.check('user-1');
250
+
251
+ // Advance time by 300ms
252
+ vi.advanceTimersByTime(300);
253
+
254
+ const result = rateLimiter.check('user-1');
255
+ expect(result.resetIn).toBeLessThanOrEqual(700);
256
+ expect(result.resetIn).toBeGreaterThan(0);
257
+ });
258
+ });
259
+
260
+ describe('cleanup', () => {
261
+ it('should remove expired entries', () => {
262
+ rateLimiter.check('user-1');
263
+ rateLimiter.check('user-2');
264
+
265
+ expect(rateLimiter.getRequestCount('user-1')).toBe(1);
266
+ expect(rateLimiter.getRequestCount('user-2')).toBe(1);
267
+
268
+ // Advance past window
269
+ vi.advanceTimersByTime(1001);
270
+
271
+ rateLimiter.cleanup();
272
+
273
+ expect(rateLimiter.getRequestCount('user-1')).toBeUndefined();
274
+ expect(rateLimiter.getRequestCount('user-2')).toBeUndefined();
275
+ });
276
+
277
+ it('should keep non-expired entries', () => {
278
+ rateLimiter.check('user-1');
279
+
280
+ // Advance but not past window
281
+ vi.advanceTimersByTime(500);
282
+
283
+ rateLimiter.cleanup();
284
+
285
+ expect(rateLimiter.getRequestCount('user-1')).toBe(1);
286
+ });
287
+ });
288
+
289
+ describe('getRequestCount', () => {
290
+ it('should return undefined for unknown key', () => {
291
+ expect(rateLimiter.getRequestCount('unknown')).toBeUndefined();
292
+ });
293
+
294
+ it('should return correct count', () => {
295
+ rateLimiter.check('user-1');
296
+ rateLimiter.check('user-1');
297
+ rateLimiter.check('user-1');
298
+
299
+ expect(rateLimiter.getRequestCount('user-1')).toBe(3);
300
+ });
301
+ });
302
+ });
303
+
304
+ // ============================================================================
305
+ // Task Validation Tests
306
+ // ============================================================================
307
+
308
+ describe('canValidateTask', () => {
309
+ it('should allow validation of completed tasks', () => {
310
+ const task = {
311
+ status: 'completed',
312
+ validated_at: null,
313
+ working_agent_session_id: null,
314
+ };
315
+
316
+ const result = canValidateTask(task, 'session-123');
317
+
318
+ expect(result.canValidate).toBe(true);
319
+ expect(result.reason).toBeUndefined();
320
+ });
321
+
322
+ it('should reject non-completed tasks', () => {
323
+ const statuses = ['pending', 'in_progress', 'cancelled'];
324
+
325
+ for (const status of statuses) {
326
+ const task = {
327
+ status,
328
+ validated_at: null,
329
+ working_agent_session_id: null,
330
+ };
331
+
332
+ const result = canValidateTask(task, 'session-123');
333
+
334
+ expect(result.canValidate).toBe(false);
335
+ expect(result.reason).toBe('Can only validate completed tasks');
336
+ }
337
+ });
338
+
339
+ it('should reject already validated tasks', () => {
340
+ const task = {
341
+ status: 'completed',
342
+ validated_at: '2026-01-12T00:00:00Z',
343
+ working_agent_session_id: null,
344
+ };
345
+
346
+ const result = canValidateTask(task, 'session-123');
347
+
348
+ expect(result.canValidate).toBe(false);
349
+ expect(result.reason).toBe('Task has already been validated');
350
+ });
351
+
352
+ it('should handle null validator session', () => {
353
+ const task = {
354
+ status: 'completed',
355
+ validated_at: null,
356
+ working_agent_session_id: null,
357
+ };
358
+
359
+ const result = canValidateTask(task, null);
360
+
361
+ expect(result.canValidate).toBe(true);
362
+ });
363
+ });
364
+
365
+ // ============================================================================
366
+ // Status Transition Tests
367
+ // ============================================================================
368
+
369
+ describe('isValidStatusTransition', () => {
370
+ describe('from pending', () => {
371
+ it('should allow transition to in_progress', () => {
372
+ const result = isValidStatusTransition('pending', 'in_progress');
373
+ expect(result.isValid).toBe(true);
374
+ });
375
+
376
+ it('should allow transition to cancelled', () => {
377
+ const result = isValidStatusTransition('pending', 'cancelled');
378
+ expect(result.isValid).toBe(true);
379
+ });
380
+
381
+ it('should reject transition to completed', () => {
382
+ const result = isValidStatusTransition('pending', 'completed');
383
+ expect(result.isValid).toBe(false);
384
+ expect(result.reason).toContain('Cannot transition');
385
+ });
386
+ });
387
+
388
+ describe('from in_progress', () => {
389
+ it('should allow transition to completed', () => {
390
+ const result = isValidStatusTransition('in_progress', 'completed');
391
+ expect(result.isValid).toBe(true);
392
+ });
393
+
394
+ it('should allow transition to pending', () => {
395
+ const result = isValidStatusTransition('in_progress', 'pending');
396
+ expect(result.isValid).toBe(true);
397
+ });
398
+
399
+ it('should allow transition to cancelled', () => {
400
+ const result = isValidStatusTransition('in_progress', 'cancelled');
401
+ expect(result.isValid).toBe(true);
402
+ });
403
+ });
404
+
405
+ describe('from completed', () => {
406
+ it('should allow transition to in_progress (validation failure)', () => {
407
+ const result = isValidStatusTransition('completed', 'in_progress');
408
+ expect(result.isValid).toBe(true);
409
+ });
410
+
411
+ it('should reject transition to pending', () => {
412
+ const result = isValidStatusTransition('completed', 'pending');
413
+ expect(result.isValid).toBe(false);
414
+ });
415
+
416
+ it('should reject transition to cancelled', () => {
417
+ const result = isValidStatusTransition('completed', 'cancelled');
418
+ expect(result.isValid).toBe(false);
419
+ });
420
+ });
421
+
422
+ describe('from cancelled', () => {
423
+ it('should allow transition to pending (reactivate)', () => {
424
+ const result = isValidStatusTransition('cancelled', 'pending');
425
+ expect(result.isValid).toBe(true);
426
+ });
427
+
428
+ it('should reject transition to in_progress', () => {
429
+ const result = isValidStatusTransition('cancelled', 'in_progress');
430
+ expect(result.isValid).toBe(false);
431
+ });
432
+
433
+ it('should reject transition to completed', () => {
434
+ const result = isValidStatusTransition('cancelled', 'completed');
435
+ expect(result.isValid).toBe(false);
436
+ });
437
+ });
438
+
439
+ describe('unknown status', () => {
440
+ it('should reject unknown current status', () => {
441
+ const result = isValidStatusTransition('unknown', 'pending');
442
+ expect(result.isValid).toBe(false);
443
+ expect(result.reason).toContain('Unknown current status');
444
+ });
445
+ });
446
+ });
447
+
448
+ // ============================================================================
449
+ // Deployment Status Transition Tests
450
+ // ============================================================================
451
+
452
+ describe('isValidDeploymentStatusTransition', () => {
453
+ describe('from pending', () => {
454
+ it('should allow pending → validating', () => {
455
+ const result = isValidDeploymentStatusTransition('pending', 'validating');
456
+ expect(result.isValid).toBe(true);
457
+ });
458
+
459
+ it('should allow pending → failed', () => {
460
+ const result = isValidDeploymentStatusTransition('pending', 'failed');
461
+ expect(result.isValid).toBe(true);
462
+ });
463
+
464
+ it('should reject pending → ready', () => {
465
+ const result = isValidDeploymentStatusTransition('pending', 'ready');
466
+ expect(result.isValid).toBe(false);
467
+ });
468
+
469
+ it('should reject pending → deployed', () => {
470
+ const result = isValidDeploymentStatusTransition('pending', 'deployed');
471
+ expect(result.isValid).toBe(false);
472
+ });
473
+ });
474
+
475
+ describe('from validating', () => {
476
+ it('should allow validating → ready', () => {
477
+ const result = isValidDeploymentStatusTransition('validating', 'ready');
478
+ expect(result.isValid).toBe(true);
479
+ });
480
+
481
+ it('should allow validating → failed', () => {
482
+ const result = isValidDeploymentStatusTransition('validating', 'failed');
483
+ expect(result.isValid).toBe(true);
484
+ });
485
+
486
+ it('should reject validating → deployed', () => {
487
+ const result = isValidDeploymentStatusTransition('validating', 'deployed');
488
+ expect(result.isValid).toBe(false);
489
+ });
490
+ });
491
+
492
+ describe('from ready', () => {
493
+ it('should allow ready → deploying', () => {
494
+ const result = isValidDeploymentStatusTransition('ready', 'deploying');
495
+ expect(result.isValid).toBe(true);
496
+ });
497
+
498
+ it('should allow ready → failed', () => {
499
+ const result = isValidDeploymentStatusTransition('ready', 'failed');
500
+ expect(result.isValid).toBe(true);
501
+ });
502
+
503
+ it('should reject ready → pending', () => {
504
+ const result = isValidDeploymentStatusTransition('ready', 'pending');
505
+ expect(result.isValid).toBe(false);
506
+ });
507
+ });
508
+
509
+ describe('from deploying', () => {
510
+ it('should allow deploying → deployed', () => {
511
+ const result = isValidDeploymentStatusTransition('deploying', 'deployed');
512
+ expect(result.isValid).toBe(true);
513
+ });
514
+
515
+ it('should allow deploying → failed', () => {
516
+ const result = isValidDeploymentStatusTransition('deploying', 'failed');
517
+ expect(result.isValid).toBe(true);
518
+ });
519
+
520
+ it('should reject deploying → pending', () => {
521
+ const result = isValidDeploymentStatusTransition('deploying', 'pending');
522
+ expect(result.isValid).toBe(false);
523
+ });
524
+ });
525
+
526
+ describe('terminal states', () => {
527
+ it('should reject deployed → any transition', () => {
528
+ const result = isValidDeploymentStatusTransition('deployed', 'pending');
529
+ expect(result.isValid).toBe(false);
530
+ expect(result.reason).toContain('terminal state');
531
+ });
532
+
533
+ it('should reject failed → any transition', () => {
534
+ const result = isValidDeploymentStatusTransition('failed', 'pending');
535
+ expect(result.isValid).toBe(false);
536
+ expect(result.reason).toContain('terminal state');
537
+ });
538
+ });
539
+
540
+ describe('unknown status', () => {
541
+ it('should reject unknown current status', () => {
542
+ const result = isValidDeploymentStatusTransition('unknown', 'pending');
543
+ expect(result.isValid).toBe(false);
544
+ expect(result.reason).toContain('Unknown current status');
545
+ });
546
+ });
547
+ });
548
+
549
+ // ============================================================================
550
+ // Git URL Parsing Tests
551
+ // ============================================================================
552
+
553
+ describe('extractProjectNameFromGitUrl', () => {
554
+ it('should extract project name from https GitHub URL', () => {
555
+ expect(extractProjectNameFromGitUrl('https://github.com/user/repo')).toBe('repo');
556
+ });
557
+
558
+ it('should extract project name from https GitHub URL with .git', () => {
559
+ expect(extractProjectNameFromGitUrl('https://github.com/user/repo.git')).toBe('repo');
560
+ });
561
+
562
+ it('should extract project name from SSH GitHub URL', () => {
563
+ expect(extractProjectNameFromGitUrl('git@github.com:user/repo.git')).toBe('repo');
564
+ });
565
+
566
+ it('should extract project name from SSH URL without .git', () => {
567
+ expect(extractProjectNameFromGitUrl('git@github.com:user/repo')).toBe('repo');
568
+ });
569
+
570
+ it('should extract project name from GitLab URL', () => {
571
+ expect(extractProjectNameFromGitUrl('https://gitlab.com/user/project-name')).toBe('project-name');
572
+ });
573
+
574
+ it('should extract project name from ssh:// protocol URL', () => {
575
+ expect(extractProjectNameFromGitUrl('ssh://git@github.com/user/repo.git')).toBe('repo');
576
+ });
577
+
578
+ it('should handle nested paths (monorepo)', () => {
579
+ expect(extractProjectNameFromGitUrl('https://github.com/org/monorepo')).toBe('monorepo');
580
+ });
581
+
582
+ it('should handle dashes and underscores in project names', () => {
583
+ expect(extractProjectNameFromGitUrl('https://github.com/user/my-awesome_project')).toBe('my-awesome_project');
584
+ });
585
+
586
+ it('should return default for empty string', () => {
587
+ expect(extractProjectNameFromGitUrl('')).toBe('my-project');
588
+ });
589
+
590
+ it('should return default for malformed URL', () => {
591
+ expect(extractProjectNameFromGitUrl('not-a-url')).toBe('my-project');
592
+ });
593
+
594
+ it('should handle real Vibescope URL', () => {
595
+ expect(extractProjectNameFromGitUrl('https://github.com/Nonatomic/Vibescope')).toBe('Vibescope');
596
+ });
597
+ });
598
+
599
+ // ============================================================================
600
+ // normalizeGitUrl Tests
601
+ // ============================================================================
602
+
603
+ describe('normalizeGitUrl', () => {
604
+ describe('null and undefined handling', () => {
605
+ it('returns null for null input', () => {
606
+ expect(normalizeGitUrl(null)).toBe(null);
607
+ });
608
+
609
+ it('returns null for undefined input', () => {
610
+ expect(normalizeGitUrl(undefined)).toBe(null);
611
+ });
612
+
613
+ it('returns null for empty string', () => {
614
+ expect(normalizeGitUrl('')).toBe(null);
615
+ });
616
+
617
+ it('returns null for whitespace-only string', () => {
618
+ expect(normalizeGitUrl(' ')).toBe(null);
619
+ });
620
+ });
621
+
622
+ describe('SSH format conversion', () => {
623
+ it('converts SSH format to HTTPS', () => {
624
+ expect(normalizeGitUrl('git@github.com:owner/repo')).toBe('https://github.com/owner/repo');
625
+ });
626
+
627
+ it('converts SSH format with .git suffix', () => {
628
+ expect(normalizeGitUrl('git@github.com:owner/repo.git')).toBe('https://github.com/owner/repo');
629
+ });
630
+
631
+ it('converts GitLab SSH format', () => {
632
+ expect(normalizeGitUrl('git@gitlab.com:group/project')).toBe('https://gitlab.com/group/project');
633
+ });
634
+
635
+ it('converts Bitbucket SSH format', () => {
636
+ expect(normalizeGitUrl('git@bitbucket.org:team/repo')).toBe('https://bitbucket.org/team/repo');
637
+ });
638
+
639
+ it('handles SSH format with nested paths', () => {
640
+ expect(normalizeGitUrl('git@github.com:org/group/repo')).toBe('https://github.com/org/group/repo');
641
+ });
642
+
643
+ it('converts self-hosted git SSH format', () => {
644
+ expect(normalizeGitUrl('git@selfhosted.example.com:user/repo.git')).toBe('https://selfhosted.example.com/user/repo');
645
+ });
646
+ });
647
+
648
+ describe('.git suffix removal', () => {
649
+ it('removes .git suffix from HTTPS URL', () => {
650
+ expect(normalizeGitUrl('https://github.com/owner/repo.git')).toBe('https://github.com/owner/repo');
651
+ });
652
+
653
+ it('handles URL without .git suffix', () => {
654
+ expect(normalizeGitUrl('https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
655
+ });
656
+
657
+ it('does not remove .git from middle of URL', () => {
658
+ expect(normalizeGitUrl('https://github.com/owner/repo.github.io')).toBe('https://github.com/owner/repo.github.io');
659
+ });
660
+ });
661
+
662
+ describe('trailing slash removal', () => {
663
+ it('removes single trailing slash', () => {
664
+ expect(normalizeGitUrl('https://github.com/owner/repo/')).toBe('https://github.com/owner/repo');
665
+ });
666
+
667
+ it('removes multiple trailing slashes', () => {
668
+ expect(normalizeGitUrl('https://github.com/owner/repo///')).toBe('https://github.com/owner/repo');
669
+ });
670
+
671
+ it('handles URL without trailing slash', () => {
672
+ expect(normalizeGitUrl('https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
673
+ });
674
+ });
675
+
676
+ describe('case normalization', () => {
677
+ it('lowercases the entire URL', () => {
678
+ expect(normalizeGitUrl('https://GitHub.com/Owner/Repo')).toBe('https://github.com/owner/repo');
679
+ });
680
+
681
+ it('lowercases mixed case URLs', () => {
682
+ expect(normalizeGitUrl('https://GITHUB.COM/NONATOMIC/VIBESCOPE')).toBe('https://github.com/nonatomic/vibescope');
683
+ });
684
+
685
+ it('lowercases SSH format before conversion', () => {
686
+ expect(normalizeGitUrl('git@GitHub.com:Owner/Repo')).toBe('https://github.com/owner/repo');
687
+ });
688
+ });
689
+
690
+ describe('protocol handling', () => {
691
+ it('upgrades http to https', () => {
692
+ expect(normalizeGitUrl('http://github.com/owner/repo')).toBe('https://github.com/owner/repo');
693
+ });
694
+
695
+ it('preserves existing https protocol', () => {
696
+ expect(normalizeGitUrl('https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
697
+ });
698
+
699
+ it('adds https protocol to bare domain URLs', () => {
700
+ expect(normalizeGitUrl('github.com/owner/repo')).toBe('https://github.com/owner/repo');
701
+ });
702
+ });
703
+
704
+ describe('whitespace handling', () => {
705
+ it('trims leading whitespace', () => {
706
+ expect(normalizeGitUrl(' https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
707
+ });
708
+
709
+ it('trims trailing whitespace', () => {
710
+ expect(normalizeGitUrl('https://github.com/owner/repo ')).toBe('https://github.com/owner/repo');
711
+ });
712
+
713
+ it('trims both leading and trailing whitespace', () => {
714
+ expect(normalizeGitUrl(' https://github.com/owner/repo ')).toBe('https://github.com/owner/repo');
715
+ });
716
+ });
717
+
718
+ describe('combined transformations', () => {
719
+ it('handles SSH with .git, different casing', () => {
720
+ expect(normalizeGitUrl('git@GitHub.com:Nonatomic/Vibescope.git')).toBe('https://github.com/nonatomic/vibescope');
721
+ });
722
+
723
+ it('handles HTTPS with trailing slash, .git suffix, and casing', () => {
724
+ expect(normalizeGitUrl('https://GitHub.com/Nonatomic/Vibescope.git/')).toBe('https://github.com/nonatomic/vibescope');
725
+ });
726
+
727
+ it('handles http with trailing slashes and casing', () => {
728
+ expect(normalizeGitUrl('http://GitHub.com/Owner/Repo//')).toBe('https://github.com/owner/repo');
729
+ });
730
+
731
+ it('handles bare domain with .git and trailing slash', () => {
732
+ expect(normalizeGitUrl('github.com/owner/repo.git/')).toBe('https://github.com/owner/repo');
733
+ });
734
+ });
735
+
736
+ describe('real-world examples', () => {
737
+ it('normalizes Vibescope registered URL', () => {
738
+ expect(normalizeGitUrl('https://github.com/Nonatomic/Vibescope')).toBe('https://github.com/nonatomic/vibescope');
739
+ });
740
+
741
+ it('normalizes Vibescope with .git suffix', () => {
742
+ expect(normalizeGitUrl('https://github.com/Nonatomic/Vibescope.git')).toBe('https://github.com/nonatomic/vibescope');
743
+ });
744
+
745
+ it('normalizes Vibescope SSH format', () => {
746
+ expect(normalizeGitUrl('git@github.com:Nonatomic/Vibescope.git')).toBe('https://github.com/nonatomic/vibescope');
747
+ });
748
+
749
+ it('normalizes Vibescope with different casing', () => {
750
+ expect(normalizeGitUrl('https://github.com/nonatomic/vibescope')).toBe('https://github.com/nonatomic/vibescope');
751
+ });
752
+
753
+ it('normalizes Vibescope with trailing slash', () => {
754
+ expect(normalizeGitUrl('https://github.com/Nonatomic/Vibescope/')).toBe('https://github.com/nonatomic/vibescope');
755
+ });
756
+
757
+ it('all Vibescope variants normalize to same value', () => {
758
+ const canonical = 'https://github.com/nonatomic/vibescope';
759
+
760
+ const variants = [
761
+ 'https://github.com/Nonatomic/Vibescope',
762
+ 'https://github.com/Nonatomic/Vibescope.git',
763
+ 'git@github.com:Nonatomic/Vibescope.git',
764
+ 'https://github.com/nonatomic/vibescope',
765
+ 'https://github.com/Nonatomic/Vibescope/',
766
+ 'https://GITHUB.COM/NONATOMIC/VIBESCOPE',
767
+ 'http://github.com/Nonatomic/Vibescope',
768
+ 'github.com/Nonatomic/Vibescope',
769
+ ];
770
+
771
+ for (const variant of variants) {
772
+ expect(normalizeGitUrl(variant)).toBe(canonical);
773
+ }
774
+ });
775
+ });
776
+
777
+ describe('edge cases', () => {
778
+ it('handles complex paths with dashes and underscores', () => {
779
+ expect(normalizeGitUrl('git@github.com:my-org/my_awesome-project.git')).toBe('https://github.com/my-org/my_awesome-project');
780
+ });
781
+
782
+ it('handles nested paths', () => {
783
+ expect(normalizeGitUrl('https://gitlab.com/group/subgroup/repo.git')).toBe('https://gitlab.com/group/subgroup/repo');
784
+ });
785
+
786
+ it('handles URLs with port numbers', () => {
787
+ expect(normalizeGitUrl('https://github.com:443/user/repo.git')).toBe('https://github.com:443/user/repo');
788
+ });
789
+ });
790
+ });
791
+
792
+ // ============================================================================
793
+ // Error Handling Utilities Tests
794
+ // ============================================================================
795
+
796
+ describe('getErrorMessage', () => {
797
+ it('extracts message from Error object', () => {
798
+ const error = new Error('Something went wrong');
799
+ expect(getErrorMessage(error)).toBe('Something went wrong');
800
+ });
801
+
802
+ it('extracts message from TypeError', () => {
803
+ const error = new TypeError('Invalid type');
804
+ expect(getErrorMessage(error)).toBe('Invalid type');
805
+ });
806
+
807
+ it('returns string errors as-is', () => {
808
+ expect(getErrorMessage('Direct error string')).toBe('Direct error string');
809
+ });
810
+
811
+ it('converts numbers to string', () => {
812
+ expect(getErrorMessage(404)).toBe('404');
813
+ });
814
+
815
+ it('converts null to string', () => {
816
+ expect(getErrorMessage(null)).toBe('null');
817
+ });
818
+
819
+ it('converts undefined to string', () => {
820
+ expect(getErrorMessage(undefined)).toBe('undefined');
821
+ });
822
+
823
+ it('converts objects to string', () => {
824
+ expect(getErrorMessage({ foo: 'bar' })).toBe('[object Object]');
825
+ });
826
+
827
+ it('handles empty Error message', () => {
828
+ const error = new Error('');
829
+ expect(getErrorMessage(error)).toBe('');
830
+ });
831
+ });
832
+
833
+ describe('hasErrorProperty', () => {
834
+ it('returns true for object with error property', () => {
835
+ expect(hasErrorProperty({ error: 'some error' })).toBe(true);
836
+ });
837
+
838
+ it('returns true for object with error as object', () => {
839
+ expect(hasErrorProperty({ error: { message: 'nested' } })).toBe(true);
840
+ });
841
+
842
+ it('returns true for object with null error', () => {
843
+ expect(hasErrorProperty({ error: null })).toBe(true);
844
+ });
845
+
846
+ it('returns false for null', () => {
847
+ expect(hasErrorProperty(null)).toBe(false);
848
+ });
849
+
850
+ it('returns false for undefined', () => {
851
+ expect(hasErrorProperty(undefined)).toBe(false);
852
+ });
853
+
854
+ it('returns false for string', () => {
855
+ expect(hasErrorProperty('error')).toBe(false);
856
+ });
857
+
858
+ it('returns false for object without error property', () => {
859
+ expect(hasErrorProperty({ success: true })).toBe(false);
860
+ });
861
+
862
+ it('returns false for empty object', () => {
863
+ expect(hasErrorProperty({})).toBe(false);
864
+ });
865
+
866
+ it('returns false for array', () => {
867
+ expect(hasErrorProperty(['error'])).toBe(false);
868
+ });
869
+ });
870
+
871
+ describe('extractApiError', () => {
872
+ it('extracts string error from response', () => {
873
+ expect(extractApiError({ error: 'Not found' })).toBe('Not found');
874
+ });
875
+
876
+ it('extracts message from nested error object', () => {
877
+ expect(extractApiError({ error: { message: 'Validation failed' } })).toBe('Validation failed');
878
+ });
879
+
880
+ it('returns "Unknown error" for non-string error without message', () => {
881
+ expect(extractApiError({ error: { code: 500 } })).toBe('Unknown error');
882
+ });
883
+
884
+ it('returns null for data without error property', () => {
885
+ expect(extractApiError({ success: true })).toBeNull();
886
+ });
887
+
888
+ it('returns null for null input', () => {
889
+ expect(extractApiError(null)).toBeNull();
890
+ });
891
+
892
+ it('returns null for undefined input', () => {
893
+ expect(extractApiError(undefined)).toBeNull();
894
+ });
895
+
896
+ it('returns null for string input', () => {
897
+ expect(extractApiError('error')).toBeNull();
898
+ });
899
+
900
+ it('returns null for empty object', () => {
901
+ expect(extractApiError({})).toBeNull();
902
+ });
903
+
904
+ it('handles empty string error', () => {
905
+ expect(extractApiError({ error: '' })).toBe('');
906
+ });
907
+
908
+ it('returns "Unknown error" for error as number', () => {
909
+ expect(extractApiError({ error: 404 })).toBe('Unknown error');
910
+ });
911
+
912
+ it('returns "Unknown error" for error as boolean', () => {
913
+ expect(extractApiError({ error: true })).toBe('Unknown error');
914
+ });
915
+
916
+ it('handles deeply nested message', () => {
917
+ // Only extracts from immediate .message property
918
+ expect(extractApiError({ error: { nested: { message: 'deep' } } })).toBe('Unknown error');
919
+ });
920
+ });
921
+
922
+ // ============================================================================
923
+ // Pagination Utilities Tests
924
+ // ============================================================================
925
+
926
+ describe('PAGINATION_LIMITS', () => {
927
+ it('should have expected default values', () => {
928
+ expect(PAGINATION_LIMITS.DEFAULT_MAX_LIMIT).toBe(50);
929
+ expect(PAGINATION_LIMITS.DEFAULT_LIMIT).toBe(20);
930
+ expect(PAGINATION_LIMITS.TASK_LIMIT).toBe(20);
931
+ });
932
+ });
933
+
934
+ describe('capPagination', () => {
935
+ describe('limit capping', () => {
936
+ it('should cap limit to maxLimit when exceeded', () => {
937
+ const { cappedLimit } = capPagination(100, 0, 50);
938
+ expect(cappedLimit).toBe(50);
939
+ });
940
+
941
+ it('should use limit when under maxLimit', () => {
942
+ const { cappedLimit } = capPagination(25, 0, 50);
943
+ expect(cappedLimit).toBe(25);
944
+ });
945
+
946
+ it('should use default maxLimit when not specified', () => {
947
+ const { cappedLimit } = capPagination(100, 0);
948
+ expect(cappedLimit).toBe(PAGINATION_LIMITS.DEFAULT_MAX_LIMIT);
949
+ });
950
+
951
+ it('should enforce minimum limit of 1', () => {
952
+ const { cappedLimit } = capPagination(0, 0, 50);
953
+ expect(cappedLimit).toBe(1);
954
+ });
955
+
956
+ it('should handle negative limit', () => {
957
+ const { cappedLimit } = capPagination(-5, 0, 50);
958
+ expect(cappedLimit).toBe(1);
959
+ });
960
+
961
+ it('should use DEFAULT_LIMIT when limit is undefined', () => {
962
+ const { cappedLimit } = capPagination(undefined, 0, 50);
963
+ expect(cappedLimit).toBe(PAGINATION_LIMITS.DEFAULT_LIMIT);
964
+ });
965
+ });
966
+
967
+ describe('offset safety', () => {
968
+ it('should keep positive offset unchanged', () => {
969
+ const { safeOffset } = capPagination(10, 50, 50);
970
+ expect(safeOffset).toBe(50);
971
+ });
972
+
973
+ it('should convert negative offset to 0', () => {
974
+ const { safeOffset } = capPagination(10, -10, 50);
975
+ expect(safeOffset).toBe(0);
976
+ });
977
+
978
+ it('should handle zero offset', () => {
979
+ const { safeOffset } = capPagination(10, 0, 50);
980
+ expect(safeOffset).toBe(0);
981
+ });
982
+
983
+ it('should use 0 when offset is undefined', () => {
984
+ const { safeOffset } = capPagination(10, undefined, 50);
985
+ expect(safeOffset).toBe(0);
986
+ });
987
+ });
988
+
989
+ describe('combined behavior', () => {
990
+ it('should return both cappedLimit and safeOffset', () => {
991
+ const result = capPagination(100, -5, 20);
992
+ expect(result).toEqual({
993
+ cappedLimit: 20,
994
+ safeOffset: 0,
995
+ });
996
+ });
997
+
998
+ it('should work with task limit constant', () => {
999
+ const result = capPagination(50, 10, PAGINATION_LIMITS.TASK_LIMIT);
1000
+ expect(result).toEqual({
1001
+ cappedLimit: 20,
1002
+ safeOffset: 10,
1003
+ });
1004
+ });
1005
+
1006
+ it('should handle all undefined values', () => {
1007
+ const result = capPagination(undefined, undefined);
1008
+ expect(result).toEqual({
1009
+ cappedLimit: PAGINATION_LIMITS.DEFAULT_LIMIT,
1010
+ safeOffset: 0,
1011
+ });
1012
+ });
1013
+ });
1014
+ });