@vibescope/mcp-server 0.2.8 → 0.3.0

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 (97) hide show
  1. package/CHANGELOG.md +84 -84
  2. package/README.md +194 -194
  3. package/dist/api-client.d.ts +41 -5
  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/roles.js +1 -8
  9. package/dist/handlers/session.d.ts +11 -0
  10. package/dist/handlers/session.js +124 -32
  11. package/dist/handlers/tasks.d.ts +8 -0
  12. package/dist/handlers/tasks.js +163 -3
  13. package/dist/handlers/tool-docs.js +840 -828
  14. package/dist/handlers/validation.js +71 -15
  15. package/dist/index.js +73 -73
  16. package/dist/setup.js +6 -6
  17. package/dist/templates/agent-guidelines.js +185 -185
  18. package/dist/templates/help-content.d.ts +24 -0
  19. package/dist/templates/help-content.js +1728 -0
  20. package/dist/tools.js +132 -87
  21. package/dist/utils.d.ts +15 -11
  22. package/dist/utils.js +53 -28
  23. package/docs/TOOLS.md +2406 -2053
  24. package/package.json +1 -1
  25. package/scripts/generate-docs.ts +212 -212
  26. package/scripts/version-bump.ts +203 -203
  27. package/src/api-client.test.ts +723 -723
  28. package/src/api-client.ts +2561 -2499
  29. package/src/cli.test.ts +24 -8
  30. package/src/cli.ts +204 -212
  31. package/src/handlers/__test-setup__.ts +236 -236
  32. package/src/handlers/__test-utils__.ts +87 -87
  33. package/src/handlers/blockers.test.ts +468 -468
  34. package/src/handlers/blockers.ts +163 -163
  35. package/src/handlers/bodies-of-work.test.ts +704 -704
  36. package/src/handlers/bodies-of-work.ts +526 -526
  37. package/src/handlers/connectors.test.ts +834 -834
  38. package/src/handlers/connectors.ts +229 -229
  39. package/src/handlers/cost.test.ts +462 -462
  40. package/src/handlers/cost.ts +285 -285
  41. package/src/handlers/decisions.test.ts +382 -382
  42. package/src/handlers/decisions.ts +153 -153
  43. package/src/handlers/deployment.test.ts +551 -551
  44. package/src/handlers/deployment.ts +541 -541
  45. package/src/handlers/discovery.test.ts +206 -206
  46. package/src/handlers/discovery.ts +392 -390
  47. package/src/handlers/fallback.test.ts +537 -537
  48. package/src/handlers/fallback.ts +194 -194
  49. package/src/handlers/file-checkouts.test.ts +750 -750
  50. package/src/handlers/file-checkouts.ts +185 -185
  51. package/src/handlers/findings.test.ts +633 -633
  52. package/src/handlers/findings.ts +239 -239
  53. package/src/handlers/git-issues.test.ts +631 -631
  54. package/src/handlers/git-issues.ts +136 -136
  55. package/src/handlers/ideas.test.ts +644 -644
  56. package/src/handlers/ideas.ts +207 -207
  57. package/src/handlers/index.ts +84 -84
  58. package/src/handlers/milestones.test.ts +475 -475
  59. package/src/handlers/milestones.ts +180 -180
  60. package/src/handlers/organizations.test.ts +826 -826
  61. package/src/handlers/organizations.ts +315 -315
  62. package/src/handlers/progress.test.ts +269 -269
  63. package/src/handlers/progress.ts +77 -77
  64. package/src/handlers/project.test.ts +546 -546
  65. package/src/handlers/project.ts +239 -239
  66. package/src/handlers/requests.test.ts +303 -303
  67. package/src/handlers/requests.ts +99 -99
  68. package/src/handlers/roles.test.ts +305 -303
  69. package/src/handlers/roles.ts +219 -226
  70. package/src/handlers/session.test.ts +998 -875
  71. package/src/handlers/session.ts +839 -738
  72. package/src/handlers/sprints.test.ts +732 -732
  73. package/src/handlers/sprints.ts +537 -537
  74. package/src/handlers/tasks.test.ts +931 -907
  75. package/src/handlers/tasks.ts +1121 -945
  76. package/src/handlers/tool-categories.test.ts +66 -66
  77. package/src/handlers/tool-docs.ts +1109 -1096
  78. package/src/handlers/types.test.ts +259 -259
  79. package/src/handlers/types.ts +175 -175
  80. package/src/handlers/validation.test.ts +582 -582
  81. package/src/handlers/validation.ts +159 -97
  82. package/src/index.test.ts +674 -0
  83. package/src/index.ts +792 -792
  84. package/src/setup.test.ts +233 -233
  85. package/src/setup.ts +404 -403
  86. package/src/templates/agent-guidelines.ts +210 -210
  87. package/src/templates/help-content.ts +1751 -0
  88. package/src/token-tracking.test.ts +463 -463
  89. package/src/token-tracking.ts +166 -166
  90. package/src/tools.test.ts +416 -0
  91. package/src/tools.ts +3607 -3562
  92. package/src/utils.test.ts +785 -683
  93. package/src/utils.ts +469 -436
  94. package/src/validators.test.ts +223 -223
  95. package/src/validators.ts +249 -249
  96. package/tsconfig.json +16 -16
  97. package/vitest.config.ts +14 -14
@@ -1,463 +1,463 @@
1
- import { describe, it, expect, beforeEach, vi } from 'vitest';
2
- import {
3
- estimateTokens,
4
- createTokenUsage,
5
- trackTokenUsage,
6
- setCurrentModel,
7
- resetTokenUsage,
8
- getTokenUsageSummary,
9
- type TokenUsage,
10
- } from './token-tracking.js';
11
-
12
- // ============================================================================
13
- // estimateTokens Tests
14
- // ============================================================================
15
-
16
- describe('estimateTokens', () => {
17
- it('should return 1 for empty object', () => {
18
- // "{}" is 2 chars, ceil(2/4) = 1
19
- expect(estimateTokens({})).toBe(1);
20
- });
21
-
22
- it('should return 1 for empty array', () => {
23
- // "[]" is 2 chars, ceil(2/4) = 1
24
- expect(estimateTokens([])).toBe(1);
25
- });
26
-
27
- it('should return 1 for empty string', () => {
28
- // '""' is 2 chars, ceil(2/4) = 1
29
- expect(estimateTokens('')).toBe(1);
30
- });
31
-
32
- it('should return 1 for null', () => {
33
- // "null" is 4 chars, ceil(4/4) = 1
34
- expect(estimateTokens(null)).toBe(1);
35
- });
36
-
37
- it('should return 1 for boolean', () => {
38
- // "true" is 4 chars, ceil(4/4) = 1
39
- expect(estimateTokens(true)).toBe(1);
40
- // "false" is 5 chars, ceil(5/4) = 2
41
- expect(estimateTokens(false)).toBe(2);
42
- });
43
-
44
- it('should estimate tokens for simple object', () => {
45
- const obj = { name: 'test' };
46
- // {"name":"test"} is 15 chars, ceil(15/4) = 4
47
- expect(estimateTokens(obj)).toBe(4);
48
- });
49
-
50
- it('should estimate tokens for array of strings', () => {
51
- const arr = ['one', 'two', 'three'];
52
- // ["one","two","three"] is 21 chars, ceil(21/4) = 6
53
- expect(estimateTokens(arr)).toBe(6);
54
- });
55
-
56
- it('should estimate tokens for nested object', () => {
57
- const obj = {
58
- user: {
59
- name: 'John',
60
- age: 30,
61
- },
62
- active: true,
63
- };
64
- // Complex object - just verify it returns a reasonable positive number
65
- const tokens = estimateTokens(obj);
66
- expect(tokens).toBeGreaterThan(0);
67
- expect(tokens).toBeLessThan(100); // Sanity check
68
- });
69
-
70
- it('should estimate tokens for large object', () => {
71
- const obj = {
72
- tasks: Array(100)
73
- .fill(null)
74
- .map((_, i) => ({
75
- id: `task-${i}`,
76
- title: `Task number ${i}`,
77
- status: 'pending',
78
- })),
79
- };
80
- const tokens = estimateTokens(obj);
81
- // Should be a large number for 100 tasks
82
- expect(tokens).toBeGreaterThan(500);
83
- });
84
-
85
- it('should handle numbers', () => {
86
- // "12345" is 5 chars, ceil(5/4) = 2
87
- expect(estimateTokens(12345)).toBe(2);
88
- // "3.14159" is 7 chars, ceil(7/4) = 2
89
- expect(estimateTokens(3.14159)).toBe(2);
90
- });
91
-
92
- it('should handle undefined by treating as null', () => {
93
- // JSON.stringify(undefined) returns undefined, not a string
94
- // Our function handles this gracefully
95
- const tokens = estimateTokens(undefined);
96
- expect(tokens).toBeGreaterThanOrEqual(1);
97
- });
98
-
99
- it('should handle circular reference gracefully and log warning', () => {
100
- const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
101
-
102
- const obj: Record<string, unknown> = { name: 'test' };
103
- obj.self = obj; // Create circular reference
104
-
105
- // Should not throw, should return minimal estimate
106
- const tokens = estimateTokens(obj);
107
- expect(tokens).toBe(1);
108
-
109
- // Should log a warning about the serialization failure
110
- expect(warnSpy).toHaveBeenCalledTimes(1);
111
- expect(warnSpy).toHaveBeenCalledWith(
112
- expect.stringContaining('[Vibescope] Token estimation failed:')
113
- );
114
-
115
- warnSpy.mockRestore();
116
- });
117
-
118
- it('should handle objects with toJSON method', () => {
119
- const obj = {
120
- data: 'test',
121
- toJSON() {
122
- return { serialized: true };
123
- },
124
- };
125
- // toJSON returns {"serialized":true} which is 18 chars, ceil(18/4) = 5
126
- expect(estimateTokens(obj)).toBe(5);
127
- });
128
- });
129
-
130
- // ============================================================================
131
- // createTokenUsage Tests
132
- // ============================================================================
133
-
134
- describe('createTokenUsage', () => {
135
- it('should create fresh token usage object', () => {
136
- const usage = createTokenUsage();
137
-
138
- expect(usage.callCount).toBe(0);
139
- expect(usage.totalTokens).toBe(0);
140
- expect(usage.byTool).toEqual({});
141
- expect(usage.byModel).toEqual({});
142
- expect(usage.currentModel).toBeNull();
143
- });
144
-
145
- it('should create independent instances', () => {
146
- const usage1 = createTokenUsage();
147
- const usage2 = createTokenUsage();
148
-
149
- usage1.callCount = 5;
150
- usage1.byTool['test'] = { calls: 1, tokens: 10 };
151
-
152
- expect(usage2.callCount).toBe(0);
153
- expect(usage2.byTool).toEqual({});
154
- });
155
- });
156
-
157
- // ============================================================================
158
- // trackTokenUsage Tests
159
- // ============================================================================
160
-
161
- describe('trackTokenUsage', () => {
162
- let usage: TokenUsage;
163
-
164
- beforeEach(() => {
165
- usage = createTokenUsage();
166
- });
167
-
168
- it('should increment call count', () => {
169
- trackTokenUsage(usage, 'test_tool', {}, {});
170
- expect(usage.callCount).toBe(1);
171
-
172
- trackTokenUsage(usage, 'test_tool', {}, {});
173
- expect(usage.callCount).toBe(2);
174
- });
175
-
176
- it('should accumulate total tokens', () => {
177
- trackTokenUsage(usage, 'tool1', { key: 'value' }, { result: 'ok' });
178
- const firstTotal = usage.totalTokens;
179
- expect(firstTotal).toBeGreaterThan(0);
180
-
181
- trackTokenUsage(usage, 'tool2', { key: 'value' }, { result: 'ok' });
182
- expect(usage.totalTokens).toBeGreaterThan(firstTotal);
183
- });
184
-
185
- it('should track by tool name', () => {
186
- trackTokenUsage(usage, 'add_task', { title: 'Test' }, { success: true });
187
- trackTokenUsage(usage, 'add_task', { title: 'Test 2' }, { success: true });
188
- trackTokenUsage(usage, 'complete_task', { id: '123' }, { success: true });
189
-
190
- expect(usage.byTool['add_task'].calls).toBe(2);
191
- expect(usage.byTool['complete_task'].calls).toBe(1);
192
- expect(usage.byTool['add_task'].tokens).toBeGreaterThan(0);
193
- });
194
-
195
- it('should track by model when set', () => {
196
- setCurrentModel(usage, 'opus');
197
- trackTokenUsage(usage, 'tool1', { data: 'input' }, { data: 'output' });
198
-
199
- expect(usage.byModel['opus']).toBeDefined();
200
- expect(usage.byModel['opus'].input).toBeGreaterThan(0);
201
- expect(usage.byModel['opus'].output).toBeGreaterThan(0);
202
- });
203
-
204
- it('should not track by model when not set', () => {
205
- trackTokenUsage(usage, 'tool1', { data: 'input' }, { data: 'output' });
206
-
207
- expect(Object.keys(usage.byModel)).toHaveLength(0);
208
- });
209
-
210
- it('should track multiple models separately', () => {
211
- setCurrentModel(usage, 'opus');
212
- trackTokenUsage(usage, 'tool1', { x: 1 }, { y: 2 });
213
-
214
- setCurrentModel(usage, 'sonnet');
215
- trackTokenUsage(usage, 'tool2', { x: 1 }, { y: 2 });
216
- trackTokenUsage(usage, 'tool3', { x: 1 }, { y: 2 });
217
-
218
- expect(Object.keys(usage.byModel)).toHaveLength(2);
219
- expect(usage.byModel['opus']).toBeDefined();
220
- expect(usage.byModel['sonnet']).toBeDefined();
221
- });
222
-
223
- it('should handle empty args and response', () => {
224
- trackTokenUsage(usage, 'empty_tool', {}, {});
225
-
226
- expect(usage.callCount).toBe(1);
227
- expect(usage.totalTokens).toBeGreaterThanOrEqual(2); // At least 2 for {} + {}
228
- expect(usage.byTool['empty_tool'].calls).toBe(1);
229
- });
230
-
231
- it('should handle large args and response', () => {
232
- const largeArgs = { items: Array(1000).fill('item') };
233
- const largeResponse = { results: Array(1000).fill({ ok: true }) };
234
-
235
- trackTokenUsage(usage, 'large_tool', largeArgs, largeResponse);
236
-
237
- expect(usage.totalTokens).toBeGreaterThan(1000);
238
- });
239
- });
240
-
241
- // ============================================================================
242
- // setCurrentModel Tests
243
- // ============================================================================
244
-
245
- describe('setCurrentModel', () => {
246
- it('should set the current model', () => {
247
- const usage = createTokenUsage();
248
-
249
- setCurrentModel(usage, 'opus');
250
- expect(usage.currentModel).toBe('opus');
251
-
252
- setCurrentModel(usage, 'sonnet');
253
- expect(usage.currentModel).toBe('sonnet');
254
- });
255
-
256
- it('should allow clearing the model with null', () => {
257
- const usage = createTokenUsage();
258
-
259
- setCurrentModel(usage, 'opus');
260
- expect(usage.currentModel).toBe('opus');
261
-
262
- setCurrentModel(usage, null);
263
- expect(usage.currentModel).toBeNull();
264
- });
265
- });
266
-
267
- // ============================================================================
268
- // resetTokenUsage Tests
269
- // ============================================================================
270
-
271
- describe('resetTokenUsage', () => {
272
- it('should reset all tracking data', () => {
273
- const usage = createTokenUsage();
274
-
275
- // Add some data
276
- setCurrentModel(usage, 'opus');
277
- trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
278
- trackTokenUsage(usage, 'tool2', { c: 3 }, { d: 4 });
279
-
280
- expect(usage.callCount).toBe(2);
281
- expect(usage.totalTokens).toBeGreaterThan(0);
282
- expect(Object.keys(usage.byTool)).toHaveLength(2);
283
-
284
- // Reset
285
- resetTokenUsage(usage);
286
-
287
- expect(usage.callCount).toBe(0);
288
- expect(usage.totalTokens).toBe(0);
289
- expect(usage.byTool).toEqual({});
290
- expect(usage.byModel).toEqual({});
291
- expect(usage.currentModel).toBeNull();
292
- });
293
-
294
- it('should allow tracking after reset', () => {
295
- const usage = createTokenUsage();
296
-
297
- trackTokenUsage(usage, 'tool1', {}, {});
298
- resetTokenUsage(usage);
299
-
300
- trackTokenUsage(usage, 'tool2', { x: 1 }, { y: 2 });
301
-
302
- expect(usage.callCount).toBe(1);
303
- expect(usage.byTool['tool1']).toBeUndefined();
304
- expect(usage.byTool['tool2']).toBeDefined();
305
- });
306
- });
307
-
308
- // ============================================================================
309
- // getTokenUsageSummary Tests
310
- // ============================================================================
311
-
312
- describe('getTokenUsageSummary', () => {
313
- it('should return empty summary for new usage', () => {
314
- const usage = createTokenUsage();
315
- const summary = getTokenUsageSummary(usage);
316
-
317
- expect(summary.total_calls).toBe(0);
318
- expect(summary.total_tokens).toBe(0);
319
- expect(summary.average_tokens_per_call).toBe(0);
320
- expect(summary.by_tool).toEqual({});
321
- expect(summary.by_model).toEqual({});
322
- expect(summary.current_model).toBeNull();
323
- });
324
-
325
- it('should calculate average tokens per call', () => {
326
- const usage = createTokenUsage();
327
-
328
- // Track 3 calls with roughly similar sizes
329
- trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
330
- trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
331
- trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
332
-
333
- const summary = getTokenUsageSummary(usage);
334
-
335
- expect(summary.total_calls).toBe(3);
336
- expect(summary.average_tokens_per_call).toBeGreaterThan(0);
337
- expect(summary.average_tokens_per_call).toBe(
338
- Math.round(summary.total_tokens / summary.total_calls)
339
- );
340
- });
341
-
342
- it('should include per-tool averages', () => {
343
- const usage = createTokenUsage();
344
-
345
- trackTokenUsage(usage, 'small_tool', { x: 1 }, { y: 2 });
346
- trackTokenUsage(usage, 'small_tool', { x: 1 }, { y: 2 });
347
-
348
- const summary = getTokenUsageSummary(usage);
349
-
350
- expect(summary.by_tool['small_tool'].calls).toBe(2);
351
- expect(summary.by_tool['small_tool'].avg).toBeGreaterThan(0);
352
- expect(summary.by_tool['small_tool'].avg).toBe(
353
- Math.round(summary.by_tool['small_tool'].tokens / summary.by_tool['small_tool'].calls)
354
- );
355
- });
356
-
357
- it('should include model breakdown', () => {
358
- const usage = createTokenUsage();
359
-
360
- setCurrentModel(usage, 'opus');
361
- trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
362
-
363
- setCurrentModel(usage, 'haiku');
364
- trackTokenUsage(usage, 'tool2', { c: 3 }, { d: 4 });
365
-
366
- const summary = getTokenUsageSummary(usage);
367
-
368
- expect(summary.by_model['opus']).toBeDefined();
369
- expect(summary.by_model['haiku']).toBeDefined();
370
- expect(summary.current_model).toBe('haiku');
371
- });
372
-
373
- it('should return a copy of byModel to prevent mutation', () => {
374
- const usage = createTokenUsage();
375
-
376
- setCurrentModel(usage, 'opus');
377
- trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
378
-
379
- const summary = getTokenUsageSummary(usage);
380
-
381
- // Modify the summary
382
- summary.by_model['opus'].input = 999;
383
-
384
- // Original should be unchanged
385
- expect(usage.byModel['opus'].input).not.toBe(999);
386
- });
387
- });
388
-
389
- // ============================================================================
390
- // Integration Tests
391
- // ============================================================================
392
-
393
- describe('Token Tracking Integration', () => {
394
- it('should track a realistic session workflow', () => {
395
- const usage = createTokenUsage();
396
-
397
- // Agent starts with opus model
398
- setCurrentModel(usage, 'opus');
399
-
400
- // Start session
401
- trackTokenUsage(
402
- usage,
403
- 'start_work_session',
404
- { git_url: 'https://github.com/org/repo', model: 'opus' },
405
- {
406
- session_id: '123',
407
- persona: 'Atlas',
408
- next_task: { id: 'task-1', title: 'Fix bug' },
409
- }
410
- );
411
-
412
- // Update task
413
- trackTokenUsage(
414
- usage,
415
- 'update_task',
416
- { task_id: 'task-1', status: 'in_progress', progress_percentage: 25 },
417
- { success: true }
418
- );
419
-
420
- // Complete task
421
- trackTokenUsage(
422
- usage,
423
- 'complete_task',
424
- { task_id: 'task-1', summary: 'Fixed the bug' },
425
- { success: true, next_task: null }
426
- );
427
-
428
- const summary = getTokenUsageSummary(usage);
429
-
430
- expect(summary.total_calls).toBe(3);
431
- expect(summary.by_tool['start_work_session'].calls).toBe(1);
432
- expect(summary.by_tool['update_task'].calls).toBe(1);
433
- expect(summary.by_tool['complete_task'].calls).toBe(1);
434
- expect(summary.by_model['opus']).toBeDefined();
435
- expect(summary.by_model['opus'].input).toBeGreaterThan(0);
436
- expect(summary.by_model['opus'].output).toBeGreaterThan(0);
437
- });
438
-
439
- it('should handle session reset', () => {
440
- const usage = createTokenUsage();
441
-
442
- // First session
443
- setCurrentModel(usage, 'sonnet');
444
- trackTokenUsage(usage, 'tool1', {}, {});
445
- trackTokenUsage(usage, 'tool2', {}, {});
446
-
447
- const firstSummary = getTokenUsageSummary(usage);
448
- expect(firstSummary.total_calls).toBe(2);
449
-
450
- // Reset for new session
451
- resetTokenUsage(usage);
452
-
453
- // Second session
454
- setCurrentModel(usage, 'haiku');
455
- trackTokenUsage(usage, 'tool3', {}, {});
456
-
457
- const secondSummary = getTokenUsageSummary(usage);
458
- expect(secondSummary.total_calls).toBe(1);
459
- expect(secondSummary.by_tool['tool1']).toBeUndefined();
460
- expect(secondSummary.by_tool['tool3']).toBeDefined();
461
- expect(secondSummary.current_model).toBe('haiku');
462
- });
463
- });
1
+ import { describe, it, expect, beforeEach, vi } from 'vitest';
2
+ import {
3
+ estimateTokens,
4
+ createTokenUsage,
5
+ trackTokenUsage,
6
+ setCurrentModel,
7
+ resetTokenUsage,
8
+ getTokenUsageSummary,
9
+ type TokenUsage,
10
+ } from './token-tracking.js';
11
+
12
+ // ============================================================================
13
+ // estimateTokens Tests
14
+ // ============================================================================
15
+
16
+ describe('estimateTokens', () => {
17
+ it('should return 1 for empty object', () => {
18
+ // "{}" is 2 chars, ceil(2/4) = 1
19
+ expect(estimateTokens({})).toBe(1);
20
+ });
21
+
22
+ it('should return 1 for empty array', () => {
23
+ // "[]" is 2 chars, ceil(2/4) = 1
24
+ expect(estimateTokens([])).toBe(1);
25
+ });
26
+
27
+ it('should return 1 for empty string', () => {
28
+ // '""' is 2 chars, ceil(2/4) = 1
29
+ expect(estimateTokens('')).toBe(1);
30
+ });
31
+
32
+ it('should return 1 for null', () => {
33
+ // "null" is 4 chars, ceil(4/4) = 1
34
+ expect(estimateTokens(null)).toBe(1);
35
+ });
36
+
37
+ it('should return 1 for boolean', () => {
38
+ // "true" is 4 chars, ceil(4/4) = 1
39
+ expect(estimateTokens(true)).toBe(1);
40
+ // "false" is 5 chars, ceil(5/4) = 2
41
+ expect(estimateTokens(false)).toBe(2);
42
+ });
43
+
44
+ it('should estimate tokens for simple object', () => {
45
+ const obj = { name: 'test' };
46
+ // {"name":"test"} is 15 chars, ceil(15/4) = 4
47
+ expect(estimateTokens(obj)).toBe(4);
48
+ });
49
+
50
+ it('should estimate tokens for array of strings', () => {
51
+ const arr = ['one', 'two', 'three'];
52
+ // ["one","two","three"] is 21 chars, ceil(21/4) = 6
53
+ expect(estimateTokens(arr)).toBe(6);
54
+ });
55
+
56
+ it('should estimate tokens for nested object', () => {
57
+ const obj = {
58
+ user: {
59
+ name: 'John',
60
+ age: 30,
61
+ },
62
+ active: true,
63
+ };
64
+ // Complex object - just verify it returns a reasonable positive number
65
+ const tokens = estimateTokens(obj);
66
+ expect(tokens).toBeGreaterThan(0);
67
+ expect(tokens).toBeLessThan(100); // Sanity check
68
+ });
69
+
70
+ it('should estimate tokens for large object', () => {
71
+ const obj = {
72
+ tasks: Array(100)
73
+ .fill(null)
74
+ .map((_, i) => ({
75
+ id: `task-${i}`,
76
+ title: `Task number ${i}`,
77
+ status: 'pending',
78
+ })),
79
+ };
80
+ const tokens = estimateTokens(obj);
81
+ // Should be a large number for 100 tasks
82
+ expect(tokens).toBeGreaterThan(500);
83
+ });
84
+
85
+ it('should handle numbers', () => {
86
+ // "12345" is 5 chars, ceil(5/4) = 2
87
+ expect(estimateTokens(12345)).toBe(2);
88
+ // "3.14159" is 7 chars, ceil(7/4) = 2
89
+ expect(estimateTokens(3.14159)).toBe(2);
90
+ });
91
+
92
+ it('should handle undefined by treating as null', () => {
93
+ // JSON.stringify(undefined) returns undefined, not a string
94
+ // Our function handles this gracefully
95
+ const tokens = estimateTokens(undefined);
96
+ expect(tokens).toBeGreaterThanOrEqual(1);
97
+ });
98
+
99
+ it('should handle circular reference gracefully and log warning', () => {
100
+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
101
+
102
+ const obj: Record<string, unknown> = { name: 'test' };
103
+ obj.self = obj; // Create circular reference
104
+
105
+ // Should not throw, should return minimal estimate
106
+ const tokens = estimateTokens(obj);
107
+ expect(tokens).toBe(1);
108
+
109
+ // Should log a warning about the serialization failure
110
+ expect(warnSpy).toHaveBeenCalledTimes(1);
111
+ expect(warnSpy).toHaveBeenCalledWith(
112
+ expect.stringContaining('[Vibescope] Token estimation failed:')
113
+ );
114
+
115
+ warnSpy.mockRestore();
116
+ });
117
+
118
+ it('should handle objects with toJSON method', () => {
119
+ const obj = {
120
+ data: 'test',
121
+ toJSON() {
122
+ return { serialized: true };
123
+ },
124
+ };
125
+ // toJSON returns {"serialized":true} which is 18 chars, ceil(18/4) = 5
126
+ expect(estimateTokens(obj)).toBe(5);
127
+ });
128
+ });
129
+
130
+ // ============================================================================
131
+ // createTokenUsage Tests
132
+ // ============================================================================
133
+
134
+ describe('createTokenUsage', () => {
135
+ it('should create fresh token usage object', () => {
136
+ const usage = createTokenUsage();
137
+
138
+ expect(usage.callCount).toBe(0);
139
+ expect(usage.totalTokens).toBe(0);
140
+ expect(usage.byTool).toEqual({});
141
+ expect(usage.byModel).toEqual({});
142
+ expect(usage.currentModel).toBeNull();
143
+ });
144
+
145
+ it('should create independent instances', () => {
146
+ const usage1 = createTokenUsage();
147
+ const usage2 = createTokenUsage();
148
+
149
+ usage1.callCount = 5;
150
+ usage1.byTool['test'] = { calls: 1, tokens: 10 };
151
+
152
+ expect(usage2.callCount).toBe(0);
153
+ expect(usage2.byTool).toEqual({});
154
+ });
155
+ });
156
+
157
+ // ============================================================================
158
+ // trackTokenUsage Tests
159
+ // ============================================================================
160
+
161
+ describe('trackTokenUsage', () => {
162
+ let usage: TokenUsage;
163
+
164
+ beforeEach(() => {
165
+ usage = createTokenUsage();
166
+ });
167
+
168
+ it('should increment call count', () => {
169
+ trackTokenUsage(usage, 'test_tool', {}, {});
170
+ expect(usage.callCount).toBe(1);
171
+
172
+ trackTokenUsage(usage, 'test_tool', {}, {});
173
+ expect(usage.callCount).toBe(2);
174
+ });
175
+
176
+ it('should accumulate total tokens', () => {
177
+ trackTokenUsage(usage, 'tool1', { key: 'value' }, { result: 'ok' });
178
+ const firstTotal = usage.totalTokens;
179
+ expect(firstTotal).toBeGreaterThan(0);
180
+
181
+ trackTokenUsage(usage, 'tool2', { key: 'value' }, { result: 'ok' });
182
+ expect(usage.totalTokens).toBeGreaterThan(firstTotal);
183
+ });
184
+
185
+ it('should track by tool name', () => {
186
+ trackTokenUsage(usage, 'add_task', { title: 'Test' }, { success: true });
187
+ trackTokenUsage(usage, 'add_task', { title: 'Test 2' }, { success: true });
188
+ trackTokenUsage(usage, 'complete_task', { id: '123' }, { success: true });
189
+
190
+ expect(usage.byTool['add_task'].calls).toBe(2);
191
+ expect(usage.byTool['complete_task'].calls).toBe(1);
192
+ expect(usage.byTool['add_task'].tokens).toBeGreaterThan(0);
193
+ });
194
+
195
+ it('should track by model when set', () => {
196
+ setCurrentModel(usage, 'opus');
197
+ trackTokenUsage(usage, 'tool1', { data: 'input' }, { data: 'output' });
198
+
199
+ expect(usage.byModel['opus']).toBeDefined();
200
+ expect(usage.byModel['opus'].input).toBeGreaterThan(0);
201
+ expect(usage.byModel['opus'].output).toBeGreaterThan(0);
202
+ });
203
+
204
+ it('should not track by model when not set', () => {
205
+ trackTokenUsage(usage, 'tool1', { data: 'input' }, { data: 'output' });
206
+
207
+ expect(Object.keys(usage.byModel)).toHaveLength(0);
208
+ });
209
+
210
+ it('should track multiple models separately', () => {
211
+ setCurrentModel(usage, 'opus');
212
+ trackTokenUsage(usage, 'tool1', { x: 1 }, { y: 2 });
213
+
214
+ setCurrentModel(usage, 'sonnet');
215
+ trackTokenUsage(usage, 'tool2', { x: 1 }, { y: 2 });
216
+ trackTokenUsage(usage, 'tool3', { x: 1 }, { y: 2 });
217
+
218
+ expect(Object.keys(usage.byModel)).toHaveLength(2);
219
+ expect(usage.byModel['opus']).toBeDefined();
220
+ expect(usage.byModel['sonnet']).toBeDefined();
221
+ });
222
+
223
+ it('should handle empty args and response', () => {
224
+ trackTokenUsage(usage, 'empty_tool', {}, {});
225
+
226
+ expect(usage.callCount).toBe(1);
227
+ expect(usage.totalTokens).toBeGreaterThanOrEqual(2); // At least 2 for {} + {}
228
+ expect(usage.byTool['empty_tool'].calls).toBe(1);
229
+ });
230
+
231
+ it('should handle large args and response', () => {
232
+ const largeArgs = { items: Array(1000).fill('item') };
233
+ const largeResponse = { results: Array(1000).fill({ ok: true }) };
234
+
235
+ trackTokenUsage(usage, 'large_tool', largeArgs, largeResponse);
236
+
237
+ expect(usage.totalTokens).toBeGreaterThan(1000);
238
+ });
239
+ });
240
+
241
+ // ============================================================================
242
+ // setCurrentModel Tests
243
+ // ============================================================================
244
+
245
+ describe('setCurrentModel', () => {
246
+ it('should set the current model', () => {
247
+ const usage = createTokenUsage();
248
+
249
+ setCurrentModel(usage, 'opus');
250
+ expect(usage.currentModel).toBe('opus');
251
+
252
+ setCurrentModel(usage, 'sonnet');
253
+ expect(usage.currentModel).toBe('sonnet');
254
+ });
255
+
256
+ it('should allow clearing the model with null', () => {
257
+ const usage = createTokenUsage();
258
+
259
+ setCurrentModel(usage, 'opus');
260
+ expect(usage.currentModel).toBe('opus');
261
+
262
+ setCurrentModel(usage, null);
263
+ expect(usage.currentModel).toBeNull();
264
+ });
265
+ });
266
+
267
+ // ============================================================================
268
+ // resetTokenUsage Tests
269
+ // ============================================================================
270
+
271
+ describe('resetTokenUsage', () => {
272
+ it('should reset all tracking data', () => {
273
+ const usage = createTokenUsage();
274
+
275
+ // Add some data
276
+ setCurrentModel(usage, 'opus');
277
+ trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
278
+ trackTokenUsage(usage, 'tool2', { c: 3 }, { d: 4 });
279
+
280
+ expect(usage.callCount).toBe(2);
281
+ expect(usage.totalTokens).toBeGreaterThan(0);
282
+ expect(Object.keys(usage.byTool)).toHaveLength(2);
283
+
284
+ // Reset
285
+ resetTokenUsage(usage);
286
+
287
+ expect(usage.callCount).toBe(0);
288
+ expect(usage.totalTokens).toBe(0);
289
+ expect(usage.byTool).toEqual({});
290
+ expect(usage.byModel).toEqual({});
291
+ expect(usage.currentModel).toBeNull();
292
+ });
293
+
294
+ it('should allow tracking after reset', () => {
295
+ const usage = createTokenUsage();
296
+
297
+ trackTokenUsage(usage, 'tool1', {}, {});
298
+ resetTokenUsage(usage);
299
+
300
+ trackTokenUsage(usage, 'tool2', { x: 1 }, { y: 2 });
301
+
302
+ expect(usage.callCount).toBe(1);
303
+ expect(usage.byTool['tool1']).toBeUndefined();
304
+ expect(usage.byTool['tool2']).toBeDefined();
305
+ });
306
+ });
307
+
308
+ // ============================================================================
309
+ // getTokenUsageSummary Tests
310
+ // ============================================================================
311
+
312
+ describe('getTokenUsageSummary', () => {
313
+ it('should return empty summary for new usage', () => {
314
+ const usage = createTokenUsage();
315
+ const summary = getTokenUsageSummary(usage);
316
+
317
+ expect(summary.total_calls).toBe(0);
318
+ expect(summary.total_tokens).toBe(0);
319
+ expect(summary.average_tokens_per_call).toBe(0);
320
+ expect(summary.by_tool).toEqual({});
321
+ expect(summary.by_model).toEqual({});
322
+ expect(summary.current_model).toBeNull();
323
+ });
324
+
325
+ it('should calculate average tokens per call', () => {
326
+ const usage = createTokenUsage();
327
+
328
+ // Track 3 calls with roughly similar sizes
329
+ trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
330
+ trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
331
+ trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
332
+
333
+ const summary = getTokenUsageSummary(usage);
334
+
335
+ expect(summary.total_calls).toBe(3);
336
+ expect(summary.average_tokens_per_call).toBeGreaterThan(0);
337
+ expect(summary.average_tokens_per_call).toBe(
338
+ Math.round(summary.total_tokens / summary.total_calls)
339
+ );
340
+ });
341
+
342
+ it('should include per-tool averages', () => {
343
+ const usage = createTokenUsage();
344
+
345
+ trackTokenUsage(usage, 'small_tool', { x: 1 }, { y: 2 });
346
+ trackTokenUsage(usage, 'small_tool', { x: 1 }, { y: 2 });
347
+
348
+ const summary = getTokenUsageSummary(usage);
349
+
350
+ expect(summary.by_tool['small_tool'].calls).toBe(2);
351
+ expect(summary.by_tool['small_tool'].avg).toBeGreaterThan(0);
352
+ expect(summary.by_tool['small_tool'].avg).toBe(
353
+ Math.round(summary.by_tool['small_tool'].tokens / summary.by_tool['small_tool'].calls)
354
+ );
355
+ });
356
+
357
+ it('should include model breakdown', () => {
358
+ const usage = createTokenUsage();
359
+
360
+ setCurrentModel(usage, 'opus');
361
+ trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
362
+
363
+ setCurrentModel(usage, 'haiku');
364
+ trackTokenUsage(usage, 'tool2', { c: 3 }, { d: 4 });
365
+
366
+ const summary = getTokenUsageSummary(usage);
367
+
368
+ expect(summary.by_model['opus']).toBeDefined();
369
+ expect(summary.by_model['haiku']).toBeDefined();
370
+ expect(summary.current_model).toBe('haiku');
371
+ });
372
+
373
+ it('should return a copy of byModel to prevent mutation', () => {
374
+ const usage = createTokenUsage();
375
+
376
+ setCurrentModel(usage, 'opus');
377
+ trackTokenUsage(usage, 'tool1', { a: 1 }, { b: 2 });
378
+
379
+ const summary = getTokenUsageSummary(usage);
380
+
381
+ // Modify the summary
382
+ summary.by_model['opus'].input = 999;
383
+
384
+ // Original should be unchanged
385
+ expect(usage.byModel['opus'].input).not.toBe(999);
386
+ });
387
+ });
388
+
389
+ // ============================================================================
390
+ // Integration Tests
391
+ // ============================================================================
392
+
393
+ describe('Token Tracking Integration', () => {
394
+ it('should track a realistic session workflow', () => {
395
+ const usage = createTokenUsage();
396
+
397
+ // Agent starts with opus model
398
+ setCurrentModel(usage, 'opus');
399
+
400
+ // Start session
401
+ trackTokenUsage(
402
+ usage,
403
+ 'start_work_session',
404
+ { git_url: 'https://github.com/org/repo', model: 'opus' },
405
+ {
406
+ session_id: '123',
407
+ persona: 'Atlas',
408
+ next_task: { id: 'task-1', title: 'Fix bug' },
409
+ }
410
+ );
411
+
412
+ // Update task
413
+ trackTokenUsage(
414
+ usage,
415
+ 'update_task',
416
+ { task_id: 'task-1', status: 'in_progress', progress_percentage: 25 },
417
+ { success: true }
418
+ );
419
+
420
+ // Complete task
421
+ trackTokenUsage(
422
+ usage,
423
+ 'complete_task',
424
+ { task_id: 'task-1', summary: 'Fixed the bug' },
425
+ { success: true, next_task: null }
426
+ );
427
+
428
+ const summary = getTokenUsageSummary(usage);
429
+
430
+ expect(summary.total_calls).toBe(3);
431
+ expect(summary.by_tool['start_work_session'].calls).toBe(1);
432
+ expect(summary.by_tool['update_task'].calls).toBe(1);
433
+ expect(summary.by_tool['complete_task'].calls).toBe(1);
434
+ expect(summary.by_model['opus']).toBeDefined();
435
+ expect(summary.by_model['opus'].input).toBeGreaterThan(0);
436
+ expect(summary.by_model['opus'].output).toBeGreaterThan(0);
437
+ });
438
+
439
+ it('should handle session reset', () => {
440
+ const usage = createTokenUsage();
441
+
442
+ // First session
443
+ setCurrentModel(usage, 'sonnet');
444
+ trackTokenUsage(usage, 'tool1', {}, {});
445
+ trackTokenUsage(usage, 'tool2', {}, {});
446
+
447
+ const firstSummary = getTokenUsageSummary(usage);
448
+ expect(firstSummary.total_calls).toBe(2);
449
+
450
+ // Reset for new session
451
+ resetTokenUsage(usage);
452
+
453
+ // Second session
454
+ setCurrentModel(usage, 'haiku');
455
+ trackTokenUsage(usage, 'tool3', {}, {});
456
+
457
+ const secondSummary = getTokenUsageSummary(usage);
458
+ expect(secondSummary.total_calls).toBe(1);
459
+ expect(secondSummary.by_tool['tool1']).toBeUndefined();
460
+ expect(secondSummary.by_tool['tool3']).toBeDefined();
461
+ expect(secondSummary.current_model).toBe('haiku');
462
+ });
463
+ });