@vibescope/mcp-server 0.2.9 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/CHANGELOG.md +84 -84
  2. package/README.md +194 -194
  3. package/dist/api-client.d.ts +36 -0
  4. package/dist/api-client.js +34 -0
  5. package/dist/cli.d.ts +1 -1
  6. package/dist/cli.js +30 -38
  7. package/dist/handlers/discovery.js +2 -0
  8. package/dist/handlers/session.d.ts +11 -0
  9. package/dist/handlers/session.js +101 -0
  10. package/dist/handlers/tasks.d.ts +8 -0
  11. package/dist/handlers/tasks.js +163 -3
  12. package/dist/handlers/tool-docs.js +840 -828
  13. package/dist/handlers/validation.js +49 -2
  14. package/dist/index.js +73 -73
  15. package/dist/setup.js +6 -6
  16. package/dist/templates/agent-guidelines.js +185 -185
  17. package/dist/templates/help-content.js +1622 -1544
  18. package/dist/tools.js +130 -74
  19. package/dist/utils.d.ts +15 -11
  20. package/dist/utils.js +53 -28
  21. package/docs/TOOLS.md +2407 -2053
  22. package/package.json +51 -51
  23. package/scripts/generate-docs.ts +212 -212
  24. package/scripts/version-bump.ts +203 -203
  25. package/src/api-client.test.ts +723 -723
  26. package/src/api-client.ts +2561 -2499
  27. package/src/cli.test.ts +24 -8
  28. package/src/cli.ts +204 -212
  29. package/src/handlers/__test-setup__.ts +236 -236
  30. package/src/handlers/__test-utils__.ts +87 -87
  31. package/src/handlers/blockers.test.ts +468 -468
  32. package/src/handlers/blockers.ts +163 -163
  33. package/src/handlers/bodies-of-work.test.ts +704 -704
  34. package/src/handlers/bodies-of-work.ts +526 -526
  35. package/src/handlers/connectors.test.ts +834 -834
  36. package/src/handlers/connectors.ts +229 -229
  37. package/src/handlers/cost.test.ts +462 -462
  38. package/src/handlers/cost.ts +285 -285
  39. package/src/handlers/decisions.test.ts +382 -382
  40. package/src/handlers/decisions.ts +153 -153
  41. package/src/handlers/deployment.test.ts +551 -551
  42. package/src/handlers/deployment.ts +541 -541
  43. package/src/handlers/discovery.test.ts +206 -206
  44. package/src/handlers/discovery.ts +392 -390
  45. package/src/handlers/fallback.test.ts +537 -537
  46. package/src/handlers/fallback.ts +194 -194
  47. package/src/handlers/file-checkouts.test.ts +750 -750
  48. package/src/handlers/file-checkouts.ts +185 -185
  49. package/src/handlers/findings.test.ts +633 -633
  50. package/src/handlers/findings.ts +239 -239
  51. package/src/handlers/git-issues.test.ts +631 -631
  52. package/src/handlers/git-issues.ts +136 -136
  53. package/src/handlers/ideas.test.ts +644 -644
  54. package/src/handlers/ideas.ts +207 -207
  55. package/src/handlers/index.ts +84 -84
  56. package/src/handlers/milestones.test.ts +475 -475
  57. package/src/handlers/milestones.ts +180 -180
  58. package/src/handlers/organizations.test.ts +826 -826
  59. package/src/handlers/organizations.ts +315 -315
  60. package/src/handlers/progress.test.ts +269 -269
  61. package/src/handlers/progress.ts +77 -77
  62. package/src/handlers/project.test.ts +546 -546
  63. package/src/handlers/project.ts +239 -239
  64. package/src/handlers/requests.test.ts +303 -303
  65. package/src/handlers/requests.ts +99 -99
  66. package/src/handlers/roles.test.ts +305 -305
  67. package/src/handlers/roles.ts +219 -219
  68. package/src/handlers/session.test.ts +998 -875
  69. package/src/handlers/session.ts +839 -730
  70. package/src/handlers/sprints.test.ts +732 -732
  71. package/src/handlers/sprints.ts +537 -537
  72. package/src/handlers/tasks.test.ts +931 -907
  73. package/src/handlers/tasks.ts +1121 -945
  74. package/src/handlers/tool-categories.test.ts +66 -66
  75. package/src/handlers/tool-docs.ts +1109 -1096
  76. package/src/handlers/types.test.ts +259 -259
  77. package/src/handlers/types.ts +175 -175
  78. package/src/handlers/validation.test.ts +582 -582
  79. package/src/handlers/validation.ts +164 -113
  80. package/src/index.test.ts +674 -0
  81. package/src/index.ts +792 -792
  82. package/src/setup.test.ts +233 -233
  83. package/src/setup.ts +404 -403
  84. package/src/templates/agent-guidelines.ts +210 -210
  85. package/src/templates/help-content.ts +1751 -1673
  86. package/src/token-tracking.test.ts +463 -463
  87. package/src/token-tracking.ts +166 -166
  88. package/src/tools.test.ts +416 -0
  89. package/src/tools.ts +3611 -3555
  90. package/src/utils.test.ts +785 -683
  91. package/src/utils.ts +469 -436
  92. package/src/validators.test.ts +223 -223
  93. package/src/validators.ts +249 -249
  94. package/tsconfig.json +16 -16
  95. package/vitest.config.ts +14 -14
package/src/utils.test.ts CHANGED
@@ -1,683 +1,785 @@
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
- } from './utils.js';
14
-
15
- // ============================================================================
16
- // Agent Personas Tests
17
- // ============================================================================
18
-
19
- describe('AGENT_PERSONAS', () => {
20
- it('should have unique personas', () => {
21
- expect(AGENT_PERSONAS.length).toBeGreaterThanOrEqual(22);
22
- const uniquePersonas = new Set(AGENT_PERSONAS);
23
- expect(uniquePersonas.size).toBe(AGENT_PERSONAS.length);
24
- });
25
-
26
- it('should contain expected personas', () => {
27
- // Sci-fi & fantasy character names
28
- expect(AGENT_PERSONAS).toContain('Yoda');
29
- expect(AGENT_PERSONAS).toContain('Gandalf');
30
- expect(AGENT_PERSONAS).toContain('Spock');
31
- expect(AGENT_PERSONAS).toContain('Neo');
32
- });
33
-
34
- it('should have short memorable names', () => {
35
- // All personas should be reasonably short for dashboard display
36
- // Max 8 chars to accommodate names like "Morpheus"
37
- for (const persona of AGENT_PERSONAS) {
38
- expect(persona.length).toBeLessThanOrEqual(8);
39
- }
40
- });
41
- });
42
-
43
- // ============================================================================
44
- // Fallback Activities Tests
45
- // ============================================================================
46
-
47
- describe('FALLBACK_ACTIVITIES', () => {
48
- it('should have 12 activities', () => {
49
- expect(FALLBACK_ACTIVITIES).toHaveLength(12);
50
- });
51
-
52
- it('should have required fields on each activity', () => {
53
- for (const activity of FALLBACK_ACTIVITIES) {
54
- expect(activity).toHaveProperty('activity');
55
- expect(activity).toHaveProperty('title');
56
- expect(activity).toHaveProperty('description');
57
- expect(activity).toHaveProperty('prompt');
58
- expect(typeof activity.activity).toBe('string');
59
- expect(typeof activity.title).toBe('string');
60
- expect(typeof activity.description).toBe('string');
61
- expect(typeof activity.prompt).toBe('string');
62
- }
63
- });
64
-
65
- it('should have unique activity identifiers', () => {
66
- const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
67
- const uniqueIds = new Set(activityIds);
68
- expect(uniqueIds.size).toBe(FALLBACK_ACTIVITIES.length);
69
- });
70
-
71
- it('should include critical activities', () => {
72
- const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
73
- expect(activityIds).toContain('feature_ideation');
74
- expect(activityIds).toContain('code_review');
75
- expect(activityIds).toContain('security_review');
76
- expect(activityIds).toContain('test_coverage');
77
- expect(activityIds).toContain('validate_completed_tasks');
78
- });
79
- });
80
-
81
- describe('getRandomFallbackActivity', () => {
82
- it('should return a valid fallback activity', () => {
83
- const activity = getRandomFallbackActivity();
84
- expect(activity).toHaveProperty('activity');
85
- expect(activity).toHaveProperty('title');
86
- expect(activity).toHaveProperty('description');
87
- expect(activity).toHaveProperty('prompt');
88
- });
89
-
90
- it('should return activity from the pool', () => {
91
- const activity = getRandomFallbackActivity();
92
- const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
93
- expect(activityIds).toContain(activity.activity);
94
- });
95
- });
96
-
97
- // ============================================================================
98
- // Persona Selection Tests
99
- // ============================================================================
100
-
101
- describe('selectPersona', () => {
102
- it('should return a random persona when none are in use', () => {
103
- const usedPersonas = new Set<string>();
104
- const instanceId = 'abc123-def456';
105
-
106
- const selected = selectPersona(usedPersonas, instanceId);
107
-
108
- expect(AGENT_PERSONAS).toContain(selected);
109
- });
110
-
111
- it('should skip personas that are in use', () => {
112
- const usedPersonas = new Set<string>([AGENT_PERSONAS[0], AGENT_PERSONAS[1]]);
113
- const instanceId = 'abc123-def456';
114
-
115
- const selected = selectPersona(usedPersonas, instanceId);
116
-
117
- expect(usedPersonas.has(selected)).toBe(false);
118
- expect(AGENT_PERSONAS).toContain(selected);
119
- });
120
-
121
- it('should return fallback name when all personas are in use', () => {
122
- const usedPersonas = new Set(AGENT_PERSONAS);
123
- const instanceId = 'abc123-def456';
124
-
125
- const selected = selectPersona(usedPersonas, instanceId);
126
-
127
- expect(selected).toBe('Agent-abc123');
128
- });
129
-
130
- it('should handle partial instance ID', () => {
131
- const usedPersonas = new Set(AGENT_PERSONAS);
132
- const instanceId = 'xyz';
133
-
134
- const selected = selectPersona(usedPersonas, instanceId);
135
-
136
- expect(selected).toBe('Agent-xyz');
137
- });
138
-
139
- it('should be case-sensitive for used personas', () => {
140
- const firstPersona = AGENT_PERSONAS[0];
141
- const usedPersonas = new Set([firstPersona.toLowerCase(), firstPersona.toUpperCase()]); // Different case
142
- const instanceId = 'abc123-def456';
143
-
144
- const selected = selectPersona(usedPersonas, instanceId);
145
-
146
- // Original case versions are still available (case-sensitive match)
147
- expect(AGENT_PERSONAS).toContain(selected);
148
- expect(usedPersonas.has(selected)).toBe(false);
149
- });
150
- });
151
-
152
- // ============================================================================
153
- // Rate Limiter Tests
154
- // ============================================================================
155
-
156
- describe('RateLimiter', () => {
157
- let rateLimiter: RateLimiter;
158
-
159
- beforeEach(() => {
160
- vi.useFakeTimers();
161
- rateLimiter = new RateLimiter(5, 1000); // 5 requests per second for testing
162
- });
163
-
164
- afterEach(() => {
165
- vi.useRealTimers();
166
- });
167
-
168
- describe('constructor', () => {
169
- it('should use default values when none provided', () => {
170
- const defaultLimiter = new RateLimiter();
171
- const result = defaultLimiter.check('test');
172
- expect(result.remaining).toBe(59); // 60 - 1
173
- });
174
-
175
- it('should use custom values', () => {
176
- const customLimiter = new RateLimiter(10, 5000);
177
- const result = customLimiter.check('test');
178
- expect(result.remaining).toBe(9); // 10 - 1
179
- expect(result.resetIn).toBe(5000);
180
- });
181
- });
182
-
183
- describe('check', () => {
184
- it('should allow first request', () => {
185
- const result = rateLimiter.check('user-1');
186
-
187
- expect(result.allowed).toBe(true);
188
- expect(result.remaining).toBe(4);
189
- expect(result.resetIn).toBe(1000);
190
- });
191
-
192
- it('should decrement remaining count on subsequent requests', () => {
193
- rateLimiter.check('user-1');
194
- const result = rateLimiter.check('user-1');
195
-
196
- expect(result.allowed).toBe(true);
197
- expect(result.remaining).toBe(3);
198
- });
199
-
200
- it('should block requests when limit exceeded', () => {
201
- // Use up all requests
202
- for (let i = 0; i < 5; i++) {
203
- rateLimiter.check('user-1');
204
- }
205
-
206
- const result = rateLimiter.check('user-1');
207
-
208
- expect(result.allowed).toBe(false);
209
- expect(result.remaining).toBe(0);
210
- });
211
-
212
- it('should track different keys separately', () => {
213
- // Use up user-1's limit
214
- for (let i = 0; i < 5; i++) {
215
- rateLimiter.check('user-1');
216
- }
217
-
218
- // user-2 should still be allowed
219
- const result = rateLimiter.check('user-2');
220
-
221
- expect(result.allowed).toBe(true);
222
- expect(result.remaining).toBe(4);
223
- });
224
-
225
- it('should reset after window expires', () => {
226
- // Use up all requests
227
- for (let i = 0; i < 5; i++) {
228
- rateLimiter.check('user-1');
229
- }
230
-
231
- // Blocked
232
- expect(rateLimiter.check('user-1').allowed).toBe(false);
233
-
234
- // Advance time past window
235
- vi.advanceTimersByTime(1001);
236
-
237
- // Should be allowed again
238
- const result = rateLimiter.check('user-1');
239
- expect(result.allowed).toBe(true);
240
- expect(result.remaining).toBe(4);
241
- });
242
-
243
- it('should report correct resetIn time', () => {
244
- rateLimiter.check('user-1');
245
-
246
- // Advance time by 300ms
247
- vi.advanceTimersByTime(300);
248
-
249
- const result = rateLimiter.check('user-1');
250
- expect(result.resetIn).toBeLessThanOrEqual(700);
251
- expect(result.resetIn).toBeGreaterThan(0);
252
- });
253
- });
254
-
255
- describe('cleanup', () => {
256
- it('should remove expired entries', () => {
257
- rateLimiter.check('user-1');
258
- rateLimiter.check('user-2');
259
-
260
- expect(rateLimiter.getRequestCount('user-1')).toBe(1);
261
- expect(rateLimiter.getRequestCount('user-2')).toBe(1);
262
-
263
- // Advance past window
264
- vi.advanceTimersByTime(1001);
265
-
266
- rateLimiter.cleanup();
267
-
268
- expect(rateLimiter.getRequestCount('user-1')).toBeUndefined();
269
- expect(rateLimiter.getRequestCount('user-2')).toBeUndefined();
270
- });
271
-
272
- it('should keep non-expired entries', () => {
273
- rateLimiter.check('user-1');
274
-
275
- // Advance but not past window
276
- vi.advanceTimersByTime(500);
277
-
278
- rateLimiter.cleanup();
279
-
280
- expect(rateLimiter.getRequestCount('user-1')).toBe(1);
281
- });
282
- });
283
-
284
- describe('getRequestCount', () => {
285
- it('should return undefined for unknown key', () => {
286
- expect(rateLimiter.getRequestCount('unknown')).toBeUndefined();
287
- });
288
-
289
- it('should return correct count', () => {
290
- rateLimiter.check('user-1');
291
- rateLimiter.check('user-1');
292
- rateLimiter.check('user-1');
293
-
294
- expect(rateLimiter.getRequestCount('user-1')).toBe(3);
295
- });
296
- });
297
- });
298
-
299
- // ============================================================================
300
- // Task Validation Tests
301
- // ============================================================================
302
-
303
- describe('canValidateTask', () => {
304
- it('should allow validation of completed tasks', () => {
305
- const task = {
306
- status: 'completed',
307
- validated_at: null,
308
- working_agent_session_id: null,
309
- };
310
-
311
- const result = canValidateTask(task, 'session-123');
312
-
313
- expect(result.canValidate).toBe(true);
314
- expect(result.reason).toBeUndefined();
315
- });
316
-
317
- it('should reject non-completed tasks', () => {
318
- const statuses = ['pending', 'in_progress', 'cancelled'];
319
-
320
- for (const status of statuses) {
321
- const task = {
322
- status,
323
- validated_at: null,
324
- working_agent_session_id: null,
325
- };
326
-
327
- const result = canValidateTask(task, 'session-123');
328
-
329
- expect(result.canValidate).toBe(false);
330
- expect(result.reason).toBe('Can only validate completed tasks');
331
- }
332
- });
333
-
334
- it('should reject already validated tasks', () => {
335
- const task = {
336
- status: 'completed',
337
- validated_at: '2026-01-12T00:00:00Z',
338
- working_agent_session_id: null,
339
- };
340
-
341
- const result = canValidateTask(task, 'session-123');
342
-
343
- expect(result.canValidate).toBe(false);
344
- expect(result.reason).toBe('Task has already been validated');
345
- });
346
-
347
- it('should handle null validator session', () => {
348
- const task = {
349
- status: 'completed',
350
- validated_at: null,
351
- working_agent_session_id: null,
352
- };
353
-
354
- const result = canValidateTask(task, null);
355
-
356
- expect(result.canValidate).toBe(true);
357
- });
358
- });
359
-
360
- // ============================================================================
361
- // Status Transition Tests
362
- // ============================================================================
363
-
364
- describe('isValidStatusTransition', () => {
365
- describe('from pending', () => {
366
- it('should allow transition to in_progress', () => {
367
- const result = isValidStatusTransition('pending', 'in_progress');
368
- expect(result.isValid).toBe(true);
369
- });
370
-
371
- it('should allow transition to cancelled', () => {
372
- const result = isValidStatusTransition('pending', 'cancelled');
373
- expect(result.isValid).toBe(true);
374
- });
375
-
376
- it('should reject transition to completed', () => {
377
- const result = isValidStatusTransition('pending', 'completed');
378
- expect(result.isValid).toBe(false);
379
- expect(result.reason).toContain('Cannot transition');
380
- });
381
- });
382
-
383
- describe('from in_progress', () => {
384
- it('should allow transition to completed', () => {
385
- const result = isValidStatusTransition('in_progress', 'completed');
386
- expect(result.isValid).toBe(true);
387
- });
388
-
389
- it('should allow transition to pending', () => {
390
- const result = isValidStatusTransition('in_progress', 'pending');
391
- expect(result.isValid).toBe(true);
392
- });
393
-
394
- it('should allow transition to cancelled', () => {
395
- const result = isValidStatusTransition('in_progress', 'cancelled');
396
- expect(result.isValid).toBe(true);
397
- });
398
- });
399
-
400
- describe('from completed', () => {
401
- it('should allow transition to in_progress (validation failure)', () => {
402
- const result = isValidStatusTransition('completed', 'in_progress');
403
- expect(result.isValid).toBe(true);
404
- });
405
-
406
- it('should reject transition to pending', () => {
407
- const result = isValidStatusTransition('completed', 'pending');
408
- expect(result.isValid).toBe(false);
409
- });
410
-
411
- it('should reject transition to cancelled', () => {
412
- const result = isValidStatusTransition('completed', 'cancelled');
413
- expect(result.isValid).toBe(false);
414
- });
415
- });
416
-
417
- describe('from cancelled', () => {
418
- it('should allow transition to pending (reactivate)', () => {
419
- const result = isValidStatusTransition('cancelled', 'pending');
420
- expect(result.isValid).toBe(true);
421
- });
422
-
423
- it('should reject transition to in_progress', () => {
424
- const result = isValidStatusTransition('cancelled', 'in_progress');
425
- expect(result.isValid).toBe(false);
426
- });
427
-
428
- it('should reject transition to completed', () => {
429
- const result = isValidStatusTransition('cancelled', 'completed');
430
- expect(result.isValid).toBe(false);
431
- });
432
- });
433
-
434
- describe('unknown status', () => {
435
- it('should reject unknown current status', () => {
436
- const result = isValidStatusTransition('unknown', 'pending');
437
- expect(result.isValid).toBe(false);
438
- expect(result.reason).toContain('Unknown current status');
439
- });
440
- });
441
- });
442
-
443
- // ============================================================================
444
- // Deployment Status Transition Tests
445
- // ============================================================================
446
-
447
- describe('isValidDeploymentStatusTransition', () => {
448
- describe('from pending', () => {
449
- it('should allow pending → validating', () => {
450
- const result = isValidDeploymentStatusTransition('pending', 'validating');
451
- expect(result.isValid).toBe(true);
452
- });
453
-
454
- it('should allow pending → failed', () => {
455
- const result = isValidDeploymentStatusTransition('pending', 'failed');
456
- expect(result.isValid).toBe(true);
457
- });
458
-
459
- it('should reject pending → ready', () => {
460
- const result = isValidDeploymentStatusTransition('pending', 'ready');
461
- expect(result.isValid).toBe(false);
462
- });
463
-
464
- it('should reject pending → deployed', () => {
465
- const result = isValidDeploymentStatusTransition('pending', 'deployed');
466
- expect(result.isValid).toBe(false);
467
- });
468
- });
469
-
470
- describe('from validating', () => {
471
- it('should allow validating → ready', () => {
472
- const result = isValidDeploymentStatusTransition('validating', 'ready');
473
- expect(result.isValid).toBe(true);
474
- });
475
-
476
- it('should allow validating → failed', () => {
477
- const result = isValidDeploymentStatusTransition('validating', 'failed');
478
- expect(result.isValid).toBe(true);
479
- });
480
-
481
- it('should reject validating → deployed', () => {
482
- const result = isValidDeploymentStatusTransition('validating', 'deployed');
483
- expect(result.isValid).toBe(false);
484
- });
485
- });
486
-
487
- describe('from ready', () => {
488
- it('should allow ready → deploying', () => {
489
- const result = isValidDeploymentStatusTransition('ready', 'deploying');
490
- expect(result.isValid).toBe(true);
491
- });
492
-
493
- it('should allow ready → failed', () => {
494
- const result = isValidDeploymentStatusTransition('ready', 'failed');
495
- expect(result.isValid).toBe(true);
496
- });
497
-
498
- it('should reject ready → pending', () => {
499
- const result = isValidDeploymentStatusTransition('ready', 'pending');
500
- expect(result.isValid).toBe(false);
501
- });
502
- });
503
-
504
- describe('from deploying', () => {
505
- it('should allow deploying → deployed', () => {
506
- const result = isValidDeploymentStatusTransition('deploying', 'deployed');
507
- expect(result.isValid).toBe(true);
508
- });
509
-
510
- it('should allow deploying → failed', () => {
511
- const result = isValidDeploymentStatusTransition('deploying', 'failed');
512
- expect(result.isValid).toBe(true);
513
- });
514
-
515
- it('should reject deploying → pending', () => {
516
- const result = isValidDeploymentStatusTransition('deploying', 'pending');
517
- expect(result.isValid).toBe(false);
518
- });
519
- });
520
-
521
- describe('terminal states', () => {
522
- it('should reject deployed → any transition', () => {
523
- const result = isValidDeploymentStatusTransition('deployed', 'pending');
524
- expect(result.isValid).toBe(false);
525
- expect(result.reason).toContain('terminal state');
526
- });
527
-
528
- it('should reject failed → any transition', () => {
529
- const result = isValidDeploymentStatusTransition('failed', 'pending');
530
- expect(result.isValid).toBe(false);
531
- expect(result.reason).toContain('terminal state');
532
- });
533
- });
534
-
535
- describe('unknown status', () => {
536
- it('should reject unknown current status', () => {
537
- const result = isValidDeploymentStatusTransition('unknown', 'pending');
538
- expect(result.isValid).toBe(false);
539
- expect(result.reason).toContain('Unknown current status');
540
- });
541
- });
542
- });
543
-
544
- // ============================================================================
545
- // Git URL Parsing Tests
546
- // ============================================================================
547
-
548
- describe('extractProjectNameFromGitUrl', () => {
549
- it('should extract project name from https GitHub URL', () => {
550
- expect(extractProjectNameFromGitUrl('https://github.com/user/repo')).toBe('repo');
551
- });
552
-
553
- it('should extract project name from https GitHub URL with .git', () => {
554
- expect(extractProjectNameFromGitUrl('https://github.com/user/repo.git')).toBe('repo');
555
- });
556
-
557
- it('should extract project name from SSH GitHub URL', () => {
558
- expect(extractProjectNameFromGitUrl('git@github.com:user/repo.git')).toBe('repo');
559
- });
560
-
561
- it('should extract project name from SSH URL without .git', () => {
562
- expect(extractProjectNameFromGitUrl('git@github.com:user/repo')).toBe('repo');
563
- });
564
-
565
- it('should extract project name from GitLab URL', () => {
566
- expect(extractProjectNameFromGitUrl('https://gitlab.com/user/project-name')).toBe('project-name');
567
- });
568
-
569
- it('should extract project name from ssh:// protocol URL', () => {
570
- expect(extractProjectNameFromGitUrl('ssh://git@github.com/user/repo.git')).toBe('repo');
571
- });
572
-
573
- it('should handle nested paths (monorepo)', () => {
574
- expect(extractProjectNameFromGitUrl('https://github.com/org/monorepo')).toBe('monorepo');
575
- });
576
-
577
- it('should handle dashes and underscores in project names', () => {
578
- expect(extractProjectNameFromGitUrl('https://github.com/user/my-awesome_project')).toBe('my-awesome_project');
579
- });
580
-
581
- it('should return default for empty string', () => {
582
- expect(extractProjectNameFromGitUrl('')).toBe('my-project');
583
- });
584
-
585
- it('should return default for malformed URL', () => {
586
- expect(extractProjectNameFromGitUrl('not-a-url')).toBe('my-project');
587
- });
588
-
589
- it('should handle real Vibescope URL', () => {
590
- expect(extractProjectNameFromGitUrl('https://github.com/Nonatomic/Vibescope')).toBe('Vibescope');
591
- });
592
- });
593
-
594
- // ============================================================================
595
- // normalizeGitUrl Tests
596
- // ============================================================================
597
-
598
- describe('normalizeGitUrl', () => {
599
- describe('removing .git suffix', () => {
600
- it('should remove .git suffix from HTTPS URL', () => {
601
- expect(normalizeGitUrl('https://github.com/user/repo.git')).toBe('https://github.com/user/repo');
602
- });
603
-
604
- it('should not affect URL without .git suffix', () => {
605
- expect(normalizeGitUrl('https://github.com/user/repo')).toBe('https://github.com/user/repo');
606
- });
607
-
608
- it('should only remove .git at the end', () => {
609
- expect(normalizeGitUrl('https://github.com/user/dotgit')).toBe('https://github.com/user/dotgit');
610
- });
611
- });
612
-
613
- describe('converting SSH to HTTPS for GitHub', () => {
614
- it('should convert SSH URL with .git', () => {
615
- expect(normalizeGitUrl('git@github.com:user/repo.git')).toBe('https://github.com/user/repo');
616
- });
617
-
618
- it('should convert SSH URL without .git', () => {
619
- expect(normalizeGitUrl('git@github.com:user/repo')).toBe('https://github.com/user/repo');
620
- });
621
-
622
- it('should convert SSH URL with org path', () => {
623
- expect(normalizeGitUrl('git@github.com:Nonatomic/Vibescope.git')).toBe('https://github.com/Nonatomic/Vibescope');
624
- });
625
- });
626
-
627
- describe('converting SSH to HTTPS for GitLab', () => {
628
- it('should convert SSH URL with .git', () => {
629
- expect(normalizeGitUrl('git@gitlab.com:user/repo.git')).toBe('https://gitlab.com/user/repo');
630
- });
631
-
632
- it('should convert SSH URL without .git', () => {
633
- expect(normalizeGitUrl('git@gitlab.com:user/repo')).toBe('https://gitlab.com/user/repo');
634
- });
635
- });
636
-
637
- describe('converting SSH to HTTPS for Bitbucket', () => {
638
- it('should convert SSH URL with .git', () => {
639
- expect(normalizeGitUrl('git@bitbucket.org:user/repo.git')).toBe('https://bitbucket.org/user/repo');
640
- });
641
-
642
- it('should convert SSH URL without .git', () => {
643
- expect(normalizeGitUrl('git@bitbucket.org:user/repo')).toBe('https://bitbucket.org/user/repo');
644
- });
645
- });
646
-
647
- describe('passthrough behavior', () => {
648
- it('should pass through already-normalized HTTPS GitHub URL', () => {
649
- expect(normalizeGitUrl('https://github.com/user/repo')).toBe('https://github.com/user/repo');
650
- });
651
-
652
- it('should pass through HTTPS GitLab URL', () => {
653
- expect(normalizeGitUrl('https://gitlab.com/user/repo')).toBe('https://gitlab.com/user/repo');
654
- });
655
-
656
- it('should pass through HTTPS Bitbucket URL', () => {
657
- expect(normalizeGitUrl('https://bitbucket.org/user/repo')).toBe('https://bitbucket.org/user/repo');
658
- });
659
-
660
- it('should pass through empty string', () => {
661
- expect(normalizeGitUrl('')).toBe('');
662
- });
663
-
664
- it('should pass through unsupported git hosts', () => {
665
- expect(normalizeGitUrl('git@selfhosted.example.com:user/repo.git')).toBe('git@selfhosted.example.com:user/repo');
666
- });
667
- });
668
-
669
- describe('edge cases', () => {
670
- it('should handle complex paths with dashes and underscores', () => {
671
- expect(normalizeGitUrl('git@github.com:my-org/my_awesome-project.git')).toBe('https://github.com/my-org/my_awesome-project');
672
- });
673
-
674
- it('should handle nested paths', () => {
675
- expect(normalizeGitUrl('https://gitlab.com/group/subgroup/repo.git')).toBe('https://gitlab.com/group/subgroup/repo');
676
- });
677
-
678
- it('should handle URLs with port numbers', () => {
679
- // Port in URL - should just remove .git
680
- expect(normalizeGitUrl('https://github.com:443/user/repo.git')).toBe('https://github.com:443/user/repo');
681
- });
682
- });
683
- });
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
+ } from './utils.js';
14
+
15
+ // ============================================================================
16
+ // Agent Personas Tests
17
+ // ============================================================================
18
+
19
+ describe('AGENT_PERSONAS', () => {
20
+ it('should have unique personas', () => {
21
+ expect(AGENT_PERSONAS.length).toBeGreaterThanOrEqual(22);
22
+ const uniquePersonas = new Set(AGENT_PERSONAS);
23
+ expect(uniquePersonas.size).toBe(AGENT_PERSONAS.length);
24
+ });
25
+
26
+ it('should contain expected personas', () => {
27
+ // Sci-fi & fantasy character names
28
+ expect(AGENT_PERSONAS).toContain('Yoda');
29
+ expect(AGENT_PERSONAS).toContain('Gandalf');
30
+ expect(AGENT_PERSONAS).toContain('Spock');
31
+ expect(AGENT_PERSONAS).toContain('Neo');
32
+ });
33
+
34
+ it('should have short memorable names', () => {
35
+ // All personas should be reasonably short for dashboard display
36
+ // Max 8 chars to accommodate names like "Morpheus"
37
+ for (const persona of AGENT_PERSONAS) {
38
+ expect(persona.length).toBeLessThanOrEqual(8);
39
+ }
40
+ });
41
+ });
42
+
43
+ // ============================================================================
44
+ // Fallback Activities Tests
45
+ // ============================================================================
46
+
47
+ describe('FALLBACK_ACTIVITIES', () => {
48
+ it('should have 12 activities', () => {
49
+ expect(FALLBACK_ACTIVITIES).toHaveLength(12);
50
+ });
51
+
52
+ it('should have required fields on each activity', () => {
53
+ for (const activity of FALLBACK_ACTIVITIES) {
54
+ expect(activity).toHaveProperty('activity');
55
+ expect(activity).toHaveProperty('title');
56
+ expect(activity).toHaveProperty('description');
57
+ expect(activity).toHaveProperty('prompt');
58
+ expect(typeof activity.activity).toBe('string');
59
+ expect(typeof activity.title).toBe('string');
60
+ expect(typeof activity.description).toBe('string');
61
+ expect(typeof activity.prompt).toBe('string');
62
+ }
63
+ });
64
+
65
+ it('should have unique activity identifiers', () => {
66
+ const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
67
+ const uniqueIds = new Set(activityIds);
68
+ expect(uniqueIds.size).toBe(FALLBACK_ACTIVITIES.length);
69
+ });
70
+
71
+ it('should include critical activities', () => {
72
+ const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
73
+ expect(activityIds).toContain('feature_ideation');
74
+ expect(activityIds).toContain('code_review');
75
+ expect(activityIds).toContain('security_review');
76
+ expect(activityIds).toContain('test_coverage');
77
+ expect(activityIds).toContain('validate_completed_tasks');
78
+ });
79
+ });
80
+
81
+ describe('getRandomFallbackActivity', () => {
82
+ it('should return a valid fallback activity', () => {
83
+ const activity = getRandomFallbackActivity();
84
+ expect(activity).toHaveProperty('activity');
85
+ expect(activity).toHaveProperty('title');
86
+ expect(activity).toHaveProperty('description');
87
+ expect(activity).toHaveProperty('prompt');
88
+ });
89
+
90
+ it('should return activity from the pool', () => {
91
+ const activity = getRandomFallbackActivity();
92
+ const activityIds = FALLBACK_ACTIVITIES.map((a) => a.activity);
93
+ expect(activityIds).toContain(activity.activity);
94
+ });
95
+ });
96
+
97
+ // ============================================================================
98
+ // Persona Selection Tests
99
+ // ============================================================================
100
+
101
+ describe('selectPersona', () => {
102
+ it('should return a random persona when none are in use', () => {
103
+ const usedPersonas = new Set<string>();
104
+ const instanceId = 'abc123-def456';
105
+
106
+ const selected = selectPersona(usedPersonas, instanceId);
107
+
108
+ expect(AGENT_PERSONAS).toContain(selected);
109
+ });
110
+
111
+ it('should skip personas that are in use', () => {
112
+ const usedPersonas = new Set<string>([AGENT_PERSONAS[0], AGENT_PERSONAS[1]]);
113
+ const instanceId = 'abc123-def456';
114
+
115
+ const selected = selectPersona(usedPersonas, instanceId);
116
+
117
+ expect(usedPersonas.has(selected)).toBe(false);
118
+ expect(AGENT_PERSONAS).toContain(selected);
119
+ });
120
+
121
+ it('should return fallback name when all personas are in use', () => {
122
+ const usedPersonas = new Set(AGENT_PERSONAS);
123
+ const instanceId = 'abc123-def456';
124
+
125
+ const selected = selectPersona(usedPersonas, instanceId);
126
+
127
+ expect(selected).toBe('Agent-abc123');
128
+ });
129
+
130
+ it('should handle partial instance ID', () => {
131
+ const usedPersonas = new Set(AGENT_PERSONAS);
132
+ const instanceId = 'xyz';
133
+
134
+ const selected = selectPersona(usedPersonas, instanceId);
135
+
136
+ expect(selected).toBe('Agent-xyz');
137
+ });
138
+
139
+ it('should be case-sensitive for used personas', () => {
140
+ const firstPersona = AGENT_PERSONAS[0];
141
+ const usedPersonas = new Set([firstPersona.toLowerCase(), firstPersona.toUpperCase()]); // Different case
142
+ const instanceId = 'abc123-def456';
143
+
144
+ const selected = selectPersona(usedPersonas, instanceId);
145
+
146
+ // Original case versions are still available (case-sensitive match)
147
+ expect(AGENT_PERSONAS).toContain(selected);
148
+ expect(usedPersonas.has(selected)).toBe(false);
149
+ });
150
+ });
151
+
152
+ // ============================================================================
153
+ // Rate Limiter Tests
154
+ // ============================================================================
155
+
156
+ describe('RateLimiter', () => {
157
+ let rateLimiter: RateLimiter;
158
+
159
+ beforeEach(() => {
160
+ vi.useFakeTimers();
161
+ rateLimiter = new RateLimiter(5, 1000); // 5 requests per second for testing
162
+ });
163
+
164
+ afterEach(() => {
165
+ vi.useRealTimers();
166
+ });
167
+
168
+ describe('constructor', () => {
169
+ it('should use default values when none provided', () => {
170
+ const defaultLimiter = new RateLimiter();
171
+ const result = defaultLimiter.check('test');
172
+ expect(result.remaining).toBe(59); // 60 - 1
173
+ });
174
+
175
+ it('should use custom values', () => {
176
+ const customLimiter = new RateLimiter(10, 5000);
177
+ const result = customLimiter.check('test');
178
+ expect(result.remaining).toBe(9); // 10 - 1
179
+ expect(result.resetIn).toBe(5000);
180
+ });
181
+ });
182
+
183
+ describe('check', () => {
184
+ it('should allow first request', () => {
185
+ const result = rateLimiter.check('user-1');
186
+
187
+ expect(result.allowed).toBe(true);
188
+ expect(result.remaining).toBe(4);
189
+ expect(result.resetIn).toBe(1000);
190
+ });
191
+
192
+ it('should decrement remaining count on subsequent requests', () => {
193
+ rateLimiter.check('user-1');
194
+ const result = rateLimiter.check('user-1');
195
+
196
+ expect(result.allowed).toBe(true);
197
+ expect(result.remaining).toBe(3);
198
+ });
199
+
200
+ it('should block requests when limit exceeded', () => {
201
+ // Use up all requests
202
+ for (let i = 0; i < 5; i++) {
203
+ rateLimiter.check('user-1');
204
+ }
205
+
206
+ const result = rateLimiter.check('user-1');
207
+
208
+ expect(result.allowed).toBe(false);
209
+ expect(result.remaining).toBe(0);
210
+ });
211
+
212
+ it('should track different keys separately', () => {
213
+ // Use up user-1's limit
214
+ for (let i = 0; i < 5; i++) {
215
+ rateLimiter.check('user-1');
216
+ }
217
+
218
+ // user-2 should still be allowed
219
+ const result = rateLimiter.check('user-2');
220
+
221
+ expect(result.allowed).toBe(true);
222
+ expect(result.remaining).toBe(4);
223
+ });
224
+
225
+ it('should reset after window expires', () => {
226
+ // Use up all requests
227
+ for (let i = 0; i < 5; i++) {
228
+ rateLimiter.check('user-1');
229
+ }
230
+
231
+ // Blocked
232
+ expect(rateLimiter.check('user-1').allowed).toBe(false);
233
+
234
+ // Advance time past window
235
+ vi.advanceTimersByTime(1001);
236
+
237
+ // Should be allowed again
238
+ const result = rateLimiter.check('user-1');
239
+ expect(result.allowed).toBe(true);
240
+ expect(result.remaining).toBe(4);
241
+ });
242
+
243
+ it('should report correct resetIn time', () => {
244
+ rateLimiter.check('user-1');
245
+
246
+ // Advance time by 300ms
247
+ vi.advanceTimersByTime(300);
248
+
249
+ const result = rateLimiter.check('user-1');
250
+ expect(result.resetIn).toBeLessThanOrEqual(700);
251
+ expect(result.resetIn).toBeGreaterThan(0);
252
+ });
253
+ });
254
+
255
+ describe('cleanup', () => {
256
+ it('should remove expired entries', () => {
257
+ rateLimiter.check('user-1');
258
+ rateLimiter.check('user-2');
259
+
260
+ expect(rateLimiter.getRequestCount('user-1')).toBe(1);
261
+ expect(rateLimiter.getRequestCount('user-2')).toBe(1);
262
+
263
+ // Advance past window
264
+ vi.advanceTimersByTime(1001);
265
+
266
+ rateLimiter.cleanup();
267
+
268
+ expect(rateLimiter.getRequestCount('user-1')).toBeUndefined();
269
+ expect(rateLimiter.getRequestCount('user-2')).toBeUndefined();
270
+ });
271
+
272
+ it('should keep non-expired entries', () => {
273
+ rateLimiter.check('user-1');
274
+
275
+ // Advance but not past window
276
+ vi.advanceTimersByTime(500);
277
+
278
+ rateLimiter.cleanup();
279
+
280
+ expect(rateLimiter.getRequestCount('user-1')).toBe(1);
281
+ });
282
+ });
283
+
284
+ describe('getRequestCount', () => {
285
+ it('should return undefined for unknown key', () => {
286
+ expect(rateLimiter.getRequestCount('unknown')).toBeUndefined();
287
+ });
288
+
289
+ it('should return correct count', () => {
290
+ rateLimiter.check('user-1');
291
+ rateLimiter.check('user-1');
292
+ rateLimiter.check('user-1');
293
+
294
+ expect(rateLimiter.getRequestCount('user-1')).toBe(3);
295
+ });
296
+ });
297
+ });
298
+
299
+ // ============================================================================
300
+ // Task Validation Tests
301
+ // ============================================================================
302
+
303
+ describe('canValidateTask', () => {
304
+ it('should allow validation of completed tasks', () => {
305
+ const task = {
306
+ status: 'completed',
307
+ validated_at: null,
308
+ working_agent_session_id: null,
309
+ };
310
+
311
+ const result = canValidateTask(task, 'session-123');
312
+
313
+ expect(result.canValidate).toBe(true);
314
+ expect(result.reason).toBeUndefined();
315
+ });
316
+
317
+ it('should reject non-completed tasks', () => {
318
+ const statuses = ['pending', 'in_progress', 'cancelled'];
319
+
320
+ for (const status of statuses) {
321
+ const task = {
322
+ status,
323
+ validated_at: null,
324
+ working_agent_session_id: null,
325
+ };
326
+
327
+ const result = canValidateTask(task, 'session-123');
328
+
329
+ expect(result.canValidate).toBe(false);
330
+ expect(result.reason).toBe('Can only validate completed tasks');
331
+ }
332
+ });
333
+
334
+ it('should reject already validated tasks', () => {
335
+ const task = {
336
+ status: 'completed',
337
+ validated_at: '2026-01-12T00:00:00Z',
338
+ working_agent_session_id: null,
339
+ };
340
+
341
+ const result = canValidateTask(task, 'session-123');
342
+
343
+ expect(result.canValidate).toBe(false);
344
+ expect(result.reason).toBe('Task has already been validated');
345
+ });
346
+
347
+ it('should handle null validator session', () => {
348
+ const task = {
349
+ status: 'completed',
350
+ validated_at: null,
351
+ working_agent_session_id: null,
352
+ };
353
+
354
+ const result = canValidateTask(task, null);
355
+
356
+ expect(result.canValidate).toBe(true);
357
+ });
358
+ });
359
+
360
+ // ============================================================================
361
+ // Status Transition Tests
362
+ // ============================================================================
363
+
364
+ describe('isValidStatusTransition', () => {
365
+ describe('from pending', () => {
366
+ it('should allow transition to in_progress', () => {
367
+ const result = isValidStatusTransition('pending', 'in_progress');
368
+ expect(result.isValid).toBe(true);
369
+ });
370
+
371
+ it('should allow transition to cancelled', () => {
372
+ const result = isValidStatusTransition('pending', 'cancelled');
373
+ expect(result.isValid).toBe(true);
374
+ });
375
+
376
+ it('should reject transition to completed', () => {
377
+ const result = isValidStatusTransition('pending', 'completed');
378
+ expect(result.isValid).toBe(false);
379
+ expect(result.reason).toContain('Cannot transition');
380
+ });
381
+ });
382
+
383
+ describe('from in_progress', () => {
384
+ it('should allow transition to completed', () => {
385
+ const result = isValidStatusTransition('in_progress', 'completed');
386
+ expect(result.isValid).toBe(true);
387
+ });
388
+
389
+ it('should allow transition to pending', () => {
390
+ const result = isValidStatusTransition('in_progress', 'pending');
391
+ expect(result.isValid).toBe(true);
392
+ });
393
+
394
+ it('should allow transition to cancelled', () => {
395
+ const result = isValidStatusTransition('in_progress', 'cancelled');
396
+ expect(result.isValid).toBe(true);
397
+ });
398
+ });
399
+
400
+ describe('from completed', () => {
401
+ it('should allow transition to in_progress (validation failure)', () => {
402
+ const result = isValidStatusTransition('completed', 'in_progress');
403
+ expect(result.isValid).toBe(true);
404
+ });
405
+
406
+ it('should reject transition to pending', () => {
407
+ const result = isValidStatusTransition('completed', 'pending');
408
+ expect(result.isValid).toBe(false);
409
+ });
410
+
411
+ it('should reject transition to cancelled', () => {
412
+ const result = isValidStatusTransition('completed', 'cancelled');
413
+ expect(result.isValid).toBe(false);
414
+ });
415
+ });
416
+
417
+ describe('from cancelled', () => {
418
+ it('should allow transition to pending (reactivate)', () => {
419
+ const result = isValidStatusTransition('cancelled', 'pending');
420
+ expect(result.isValid).toBe(true);
421
+ });
422
+
423
+ it('should reject transition to in_progress', () => {
424
+ const result = isValidStatusTransition('cancelled', 'in_progress');
425
+ expect(result.isValid).toBe(false);
426
+ });
427
+
428
+ it('should reject transition to completed', () => {
429
+ const result = isValidStatusTransition('cancelled', 'completed');
430
+ expect(result.isValid).toBe(false);
431
+ });
432
+ });
433
+
434
+ describe('unknown status', () => {
435
+ it('should reject unknown current status', () => {
436
+ const result = isValidStatusTransition('unknown', 'pending');
437
+ expect(result.isValid).toBe(false);
438
+ expect(result.reason).toContain('Unknown current status');
439
+ });
440
+ });
441
+ });
442
+
443
+ // ============================================================================
444
+ // Deployment Status Transition Tests
445
+ // ============================================================================
446
+
447
+ describe('isValidDeploymentStatusTransition', () => {
448
+ describe('from pending', () => {
449
+ it('should allow pending → validating', () => {
450
+ const result = isValidDeploymentStatusTransition('pending', 'validating');
451
+ expect(result.isValid).toBe(true);
452
+ });
453
+
454
+ it('should allow pending → failed', () => {
455
+ const result = isValidDeploymentStatusTransition('pending', 'failed');
456
+ expect(result.isValid).toBe(true);
457
+ });
458
+
459
+ it('should reject pending → ready', () => {
460
+ const result = isValidDeploymentStatusTransition('pending', 'ready');
461
+ expect(result.isValid).toBe(false);
462
+ });
463
+
464
+ it('should reject pending → deployed', () => {
465
+ const result = isValidDeploymentStatusTransition('pending', 'deployed');
466
+ expect(result.isValid).toBe(false);
467
+ });
468
+ });
469
+
470
+ describe('from validating', () => {
471
+ it('should allow validating → ready', () => {
472
+ const result = isValidDeploymentStatusTransition('validating', 'ready');
473
+ expect(result.isValid).toBe(true);
474
+ });
475
+
476
+ it('should allow validating → failed', () => {
477
+ const result = isValidDeploymentStatusTransition('validating', 'failed');
478
+ expect(result.isValid).toBe(true);
479
+ });
480
+
481
+ it('should reject validating → deployed', () => {
482
+ const result = isValidDeploymentStatusTransition('validating', 'deployed');
483
+ expect(result.isValid).toBe(false);
484
+ });
485
+ });
486
+
487
+ describe('from ready', () => {
488
+ it('should allow ready → deploying', () => {
489
+ const result = isValidDeploymentStatusTransition('ready', 'deploying');
490
+ expect(result.isValid).toBe(true);
491
+ });
492
+
493
+ it('should allow ready → failed', () => {
494
+ const result = isValidDeploymentStatusTransition('ready', 'failed');
495
+ expect(result.isValid).toBe(true);
496
+ });
497
+
498
+ it('should reject ready → pending', () => {
499
+ const result = isValidDeploymentStatusTransition('ready', 'pending');
500
+ expect(result.isValid).toBe(false);
501
+ });
502
+ });
503
+
504
+ describe('from deploying', () => {
505
+ it('should allow deploying → deployed', () => {
506
+ const result = isValidDeploymentStatusTransition('deploying', 'deployed');
507
+ expect(result.isValid).toBe(true);
508
+ });
509
+
510
+ it('should allow deploying → failed', () => {
511
+ const result = isValidDeploymentStatusTransition('deploying', 'failed');
512
+ expect(result.isValid).toBe(true);
513
+ });
514
+
515
+ it('should reject deploying → pending', () => {
516
+ const result = isValidDeploymentStatusTransition('deploying', 'pending');
517
+ expect(result.isValid).toBe(false);
518
+ });
519
+ });
520
+
521
+ describe('terminal states', () => {
522
+ it('should reject deployed → any transition', () => {
523
+ const result = isValidDeploymentStatusTransition('deployed', 'pending');
524
+ expect(result.isValid).toBe(false);
525
+ expect(result.reason).toContain('terminal state');
526
+ });
527
+
528
+ it('should reject failed → any transition', () => {
529
+ const result = isValidDeploymentStatusTransition('failed', 'pending');
530
+ expect(result.isValid).toBe(false);
531
+ expect(result.reason).toContain('terminal state');
532
+ });
533
+ });
534
+
535
+ describe('unknown status', () => {
536
+ it('should reject unknown current status', () => {
537
+ const result = isValidDeploymentStatusTransition('unknown', 'pending');
538
+ expect(result.isValid).toBe(false);
539
+ expect(result.reason).toContain('Unknown current status');
540
+ });
541
+ });
542
+ });
543
+
544
+ // ============================================================================
545
+ // Git URL Parsing Tests
546
+ // ============================================================================
547
+
548
+ describe('extractProjectNameFromGitUrl', () => {
549
+ it('should extract project name from https GitHub URL', () => {
550
+ expect(extractProjectNameFromGitUrl('https://github.com/user/repo')).toBe('repo');
551
+ });
552
+
553
+ it('should extract project name from https GitHub URL with .git', () => {
554
+ expect(extractProjectNameFromGitUrl('https://github.com/user/repo.git')).toBe('repo');
555
+ });
556
+
557
+ it('should extract project name from SSH GitHub URL', () => {
558
+ expect(extractProjectNameFromGitUrl('git@github.com:user/repo.git')).toBe('repo');
559
+ });
560
+
561
+ it('should extract project name from SSH URL without .git', () => {
562
+ expect(extractProjectNameFromGitUrl('git@github.com:user/repo')).toBe('repo');
563
+ });
564
+
565
+ it('should extract project name from GitLab URL', () => {
566
+ expect(extractProjectNameFromGitUrl('https://gitlab.com/user/project-name')).toBe('project-name');
567
+ });
568
+
569
+ it('should extract project name from ssh:// protocol URL', () => {
570
+ expect(extractProjectNameFromGitUrl('ssh://git@github.com/user/repo.git')).toBe('repo');
571
+ });
572
+
573
+ it('should handle nested paths (monorepo)', () => {
574
+ expect(extractProjectNameFromGitUrl('https://github.com/org/monorepo')).toBe('monorepo');
575
+ });
576
+
577
+ it('should handle dashes and underscores in project names', () => {
578
+ expect(extractProjectNameFromGitUrl('https://github.com/user/my-awesome_project')).toBe('my-awesome_project');
579
+ });
580
+
581
+ it('should return default for empty string', () => {
582
+ expect(extractProjectNameFromGitUrl('')).toBe('my-project');
583
+ });
584
+
585
+ it('should return default for malformed URL', () => {
586
+ expect(extractProjectNameFromGitUrl('not-a-url')).toBe('my-project');
587
+ });
588
+
589
+ it('should handle real Vibescope URL', () => {
590
+ expect(extractProjectNameFromGitUrl('https://github.com/Nonatomic/Vibescope')).toBe('Vibescope');
591
+ });
592
+ });
593
+
594
+ // ============================================================================
595
+ // normalizeGitUrl Tests
596
+ // ============================================================================
597
+
598
+ describe('normalizeGitUrl', () => {
599
+ describe('null and undefined handling', () => {
600
+ it('returns null for null input', () => {
601
+ expect(normalizeGitUrl(null)).toBe(null);
602
+ });
603
+
604
+ it('returns null for undefined input', () => {
605
+ expect(normalizeGitUrl(undefined)).toBe(null);
606
+ });
607
+
608
+ it('returns null for empty string', () => {
609
+ expect(normalizeGitUrl('')).toBe(null);
610
+ });
611
+
612
+ it('returns null for whitespace-only string', () => {
613
+ expect(normalizeGitUrl(' ')).toBe(null);
614
+ });
615
+ });
616
+
617
+ describe('SSH format conversion', () => {
618
+ it('converts SSH format to HTTPS', () => {
619
+ expect(normalizeGitUrl('git@github.com:owner/repo')).toBe('https://github.com/owner/repo');
620
+ });
621
+
622
+ it('converts SSH format with .git suffix', () => {
623
+ expect(normalizeGitUrl('git@github.com:owner/repo.git')).toBe('https://github.com/owner/repo');
624
+ });
625
+
626
+ it('converts GitLab SSH format', () => {
627
+ expect(normalizeGitUrl('git@gitlab.com:group/project')).toBe('https://gitlab.com/group/project');
628
+ });
629
+
630
+ it('converts Bitbucket SSH format', () => {
631
+ expect(normalizeGitUrl('git@bitbucket.org:team/repo')).toBe('https://bitbucket.org/team/repo');
632
+ });
633
+
634
+ it('handles SSH format with nested paths', () => {
635
+ expect(normalizeGitUrl('git@github.com:org/group/repo')).toBe('https://github.com/org/group/repo');
636
+ });
637
+
638
+ it('converts self-hosted git SSH format', () => {
639
+ expect(normalizeGitUrl('git@selfhosted.example.com:user/repo.git')).toBe('https://selfhosted.example.com/user/repo');
640
+ });
641
+ });
642
+
643
+ describe('.git suffix removal', () => {
644
+ it('removes .git suffix from HTTPS URL', () => {
645
+ expect(normalizeGitUrl('https://github.com/owner/repo.git')).toBe('https://github.com/owner/repo');
646
+ });
647
+
648
+ it('handles URL without .git suffix', () => {
649
+ expect(normalizeGitUrl('https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
650
+ });
651
+
652
+ it('does not remove .git from middle of URL', () => {
653
+ expect(normalizeGitUrl('https://github.com/owner/repo.github.io')).toBe('https://github.com/owner/repo.github.io');
654
+ });
655
+ });
656
+
657
+ describe('trailing slash removal', () => {
658
+ it('removes single trailing slash', () => {
659
+ expect(normalizeGitUrl('https://github.com/owner/repo/')).toBe('https://github.com/owner/repo');
660
+ });
661
+
662
+ it('removes multiple trailing slashes', () => {
663
+ expect(normalizeGitUrl('https://github.com/owner/repo///')).toBe('https://github.com/owner/repo');
664
+ });
665
+
666
+ it('handles URL without trailing slash', () => {
667
+ expect(normalizeGitUrl('https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
668
+ });
669
+ });
670
+
671
+ describe('case normalization', () => {
672
+ it('lowercases the entire URL', () => {
673
+ expect(normalizeGitUrl('https://GitHub.com/Owner/Repo')).toBe('https://github.com/owner/repo');
674
+ });
675
+
676
+ it('lowercases mixed case URLs', () => {
677
+ expect(normalizeGitUrl('https://GITHUB.COM/NONATOMIC/VIBESCOPE')).toBe('https://github.com/nonatomic/vibescope');
678
+ });
679
+
680
+ it('lowercases SSH format before conversion', () => {
681
+ expect(normalizeGitUrl('git@GitHub.com:Owner/Repo')).toBe('https://github.com/owner/repo');
682
+ });
683
+ });
684
+
685
+ describe('protocol handling', () => {
686
+ it('upgrades http to https', () => {
687
+ expect(normalizeGitUrl('http://github.com/owner/repo')).toBe('https://github.com/owner/repo');
688
+ });
689
+
690
+ it('preserves existing https protocol', () => {
691
+ expect(normalizeGitUrl('https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
692
+ });
693
+
694
+ it('adds https protocol to bare domain URLs', () => {
695
+ expect(normalizeGitUrl('github.com/owner/repo')).toBe('https://github.com/owner/repo');
696
+ });
697
+ });
698
+
699
+ describe('whitespace handling', () => {
700
+ it('trims leading whitespace', () => {
701
+ expect(normalizeGitUrl(' https://github.com/owner/repo')).toBe('https://github.com/owner/repo');
702
+ });
703
+
704
+ it('trims trailing whitespace', () => {
705
+ expect(normalizeGitUrl('https://github.com/owner/repo ')).toBe('https://github.com/owner/repo');
706
+ });
707
+
708
+ it('trims both leading and trailing whitespace', () => {
709
+ expect(normalizeGitUrl(' https://github.com/owner/repo ')).toBe('https://github.com/owner/repo');
710
+ });
711
+ });
712
+
713
+ describe('combined transformations', () => {
714
+ it('handles SSH with .git, different casing', () => {
715
+ expect(normalizeGitUrl('git@GitHub.com:Nonatomic/Vibescope.git')).toBe('https://github.com/nonatomic/vibescope');
716
+ });
717
+
718
+ it('handles HTTPS with trailing slash, .git suffix, and casing', () => {
719
+ expect(normalizeGitUrl('https://GitHub.com/Nonatomic/Vibescope.git/')).toBe('https://github.com/nonatomic/vibescope');
720
+ });
721
+
722
+ it('handles http with trailing slashes and casing', () => {
723
+ expect(normalizeGitUrl('http://GitHub.com/Owner/Repo//')).toBe('https://github.com/owner/repo');
724
+ });
725
+
726
+ it('handles bare domain with .git and trailing slash', () => {
727
+ expect(normalizeGitUrl('github.com/owner/repo.git/')).toBe('https://github.com/owner/repo');
728
+ });
729
+ });
730
+
731
+ describe('real-world examples', () => {
732
+ it('normalizes Vibescope registered URL', () => {
733
+ expect(normalizeGitUrl('https://github.com/Nonatomic/Vibescope')).toBe('https://github.com/nonatomic/vibescope');
734
+ });
735
+
736
+ it('normalizes Vibescope with .git suffix', () => {
737
+ expect(normalizeGitUrl('https://github.com/Nonatomic/Vibescope.git')).toBe('https://github.com/nonatomic/vibescope');
738
+ });
739
+
740
+ it('normalizes Vibescope SSH format', () => {
741
+ expect(normalizeGitUrl('git@github.com:Nonatomic/Vibescope.git')).toBe('https://github.com/nonatomic/vibescope');
742
+ });
743
+
744
+ it('normalizes Vibescope with different casing', () => {
745
+ expect(normalizeGitUrl('https://github.com/nonatomic/vibescope')).toBe('https://github.com/nonatomic/vibescope');
746
+ });
747
+
748
+ it('normalizes Vibescope with trailing slash', () => {
749
+ expect(normalizeGitUrl('https://github.com/Nonatomic/Vibescope/')).toBe('https://github.com/nonatomic/vibescope');
750
+ });
751
+
752
+ it('all Vibescope variants normalize to same value', () => {
753
+ const canonical = 'https://github.com/nonatomic/vibescope';
754
+
755
+ const variants = [
756
+ 'https://github.com/Nonatomic/Vibescope',
757
+ 'https://github.com/Nonatomic/Vibescope.git',
758
+ 'git@github.com:Nonatomic/Vibescope.git',
759
+ 'https://github.com/nonatomic/vibescope',
760
+ 'https://github.com/Nonatomic/Vibescope/',
761
+ 'https://GITHUB.COM/NONATOMIC/VIBESCOPE',
762
+ 'http://github.com/Nonatomic/Vibescope',
763
+ 'github.com/Nonatomic/Vibescope',
764
+ ];
765
+
766
+ for (const variant of variants) {
767
+ expect(normalizeGitUrl(variant)).toBe(canonical);
768
+ }
769
+ });
770
+ });
771
+
772
+ describe('edge cases', () => {
773
+ it('handles complex paths with dashes and underscores', () => {
774
+ expect(normalizeGitUrl('git@github.com:my-org/my_awesome-project.git')).toBe('https://github.com/my-org/my_awesome-project');
775
+ });
776
+
777
+ it('handles nested paths', () => {
778
+ expect(normalizeGitUrl('https://gitlab.com/group/subgroup/repo.git')).toBe('https://gitlab.com/group/subgroup/repo');
779
+ });
780
+
781
+ it('handles URLs with port numbers', () => {
782
+ expect(normalizeGitUrl('https://github.com:443/user/repo.git')).toBe('https://github.com:443/user/repo');
783
+ });
784
+ });
785
+ });