@vibescope/mcp-server 0.5.0 → 0.5.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 (161) hide show
  1. package/CHANGELOG.md +84 -84
  2. package/README.md +194 -194
  3. package/dist/api-client/tasks.d.ts +1 -0
  4. package/dist/cli-init.js +21 -21
  5. package/dist/cli.js +26 -26
  6. package/dist/handlers/tasks.js +7 -1
  7. package/dist/handlers/tool-docs.js +1216 -1216
  8. package/dist/index.js +73 -73
  9. package/dist/templates/agent-guidelines.d.ts +1 -1
  10. package/dist/templates/agent-guidelines.js +205 -205
  11. package/dist/templates/help-content.js +1621 -1621
  12. package/dist/tools/bodies-of-work.js +6 -6
  13. package/dist/tools/cloud-agents.js +22 -22
  14. package/dist/tools/milestones.js +2 -2
  15. package/dist/tools/requests.js +1 -1
  16. package/dist/tools/session.js +11 -11
  17. package/dist/tools/sprints.js +9 -9
  18. package/dist/tools/tasks.js +43 -35
  19. package/dist/tools/worktrees.js +14 -14
  20. package/dist/utils.js +11 -11
  21. package/docs/TOOLS.md +2687 -2685
  22. package/package.json +53 -53
  23. package/scripts/generate-docs.ts +212 -212
  24. package/scripts/version-bump.ts +203 -203
  25. package/src/api-client/blockers.ts +86 -86
  26. package/src/api-client/bodies-of-work.ts +194 -194
  27. package/src/api-client/chat.ts +50 -50
  28. package/src/api-client/connectors.ts +152 -152
  29. package/src/api-client/cost.ts +185 -185
  30. package/src/api-client/decisions.ts +87 -87
  31. package/src/api-client/deployment.ts +313 -313
  32. package/src/api-client/discovery.ts +81 -81
  33. package/src/api-client/fallback.ts +52 -52
  34. package/src/api-client/file-checkouts.ts +115 -115
  35. package/src/api-client/findings.ts +100 -100
  36. package/src/api-client/git-issues.ts +88 -88
  37. package/src/api-client/ideas.ts +112 -112
  38. package/src/api-client/index.ts +592 -592
  39. package/src/api-client/milestones.ts +83 -83
  40. package/src/api-client/organizations.ts +185 -185
  41. package/src/api-client/progress.ts +94 -94
  42. package/src/api-client/project.ts +181 -181
  43. package/src/api-client/requests.ts +54 -54
  44. package/src/api-client/session.ts +220 -220
  45. package/src/api-client/sprints.ts +227 -227
  46. package/src/api-client/subtasks.ts +57 -57
  47. package/src/api-client/tasks.ts +451 -450
  48. package/src/api-client/types.ts +32 -32
  49. package/src/api-client/validation.ts +60 -60
  50. package/src/api-client/worktrees.ts +53 -53
  51. package/src/api-client.test.ts +847 -847
  52. package/src/api-client.ts +2728 -2728
  53. package/src/cli-init.ts +558 -558
  54. package/src/cli.test.ts +284 -284
  55. package/src/cli.ts +204 -204
  56. package/src/handlers/__test-setup__.ts +240 -240
  57. package/src/handlers/__test-utils__.ts +89 -89
  58. package/src/handlers/blockers.test.ts +468 -468
  59. package/src/handlers/blockers.ts +172 -172
  60. package/src/handlers/bodies-of-work.test.ts +704 -704
  61. package/src/handlers/bodies-of-work.ts +526 -526
  62. package/src/handlers/chat.test.ts +185 -185
  63. package/src/handlers/chat.ts +101 -101
  64. package/src/handlers/cloud-agents.test.ts +438 -438
  65. package/src/handlers/cloud-agents.ts +156 -156
  66. package/src/handlers/connectors.test.ts +834 -834
  67. package/src/handlers/connectors.ts +229 -229
  68. package/src/handlers/cost.test.ts +462 -462
  69. package/src/handlers/cost.ts +285 -285
  70. package/src/handlers/decisions.test.ts +382 -382
  71. package/src/handlers/decisions.ts +153 -153
  72. package/src/handlers/deployment.test.ts +551 -551
  73. package/src/handlers/deployment.ts +570 -570
  74. package/src/handlers/discovery.test.ts +206 -206
  75. package/src/handlers/discovery.ts +433 -433
  76. package/src/handlers/fallback.test.ts +537 -537
  77. package/src/handlers/fallback.ts +194 -194
  78. package/src/handlers/file-checkouts.test.ts +750 -750
  79. package/src/handlers/file-checkouts.ts +185 -185
  80. package/src/handlers/findings.test.ts +633 -633
  81. package/src/handlers/findings.ts +239 -239
  82. package/src/handlers/git-issues.test.ts +631 -631
  83. package/src/handlers/git-issues.ts +136 -136
  84. package/src/handlers/ideas.test.ts +644 -644
  85. package/src/handlers/ideas.ts +207 -207
  86. package/src/handlers/index.ts +93 -93
  87. package/src/handlers/milestones.test.ts +475 -475
  88. package/src/handlers/milestones.ts +180 -180
  89. package/src/handlers/organizations.test.ts +826 -826
  90. package/src/handlers/organizations.ts +315 -315
  91. package/src/handlers/progress.test.ts +269 -269
  92. package/src/handlers/progress.ts +77 -77
  93. package/src/handlers/project.test.ts +546 -546
  94. package/src/handlers/project.ts +245 -245
  95. package/src/handlers/requests.test.ts +303 -303
  96. package/src/handlers/requests.ts +99 -99
  97. package/src/handlers/roles.test.ts +305 -305
  98. package/src/handlers/roles.ts +219 -219
  99. package/src/handlers/session.test.ts +998 -998
  100. package/src/handlers/session.ts +1105 -1105
  101. package/src/handlers/sprints.test.ts +732 -732
  102. package/src/handlers/sprints.ts +537 -537
  103. package/src/handlers/tasks.test.ts +931 -931
  104. package/src/handlers/tasks.ts +1144 -1137
  105. package/src/handlers/tool-categories.test.ts +66 -66
  106. package/src/handlers/tool-docs.test.ts +511 -511
  107. package/src/handlers/tool-docs.ts +1595 -1595
  108. package/src/handlers/types.test.ts +259 -259
  109. package/src/handlers/types.ts +176 -176
  110. package/src/handlers/validation.test.ts +582 -582
  111. package/src/handlers/validation.ts +164 -164
  112. package/src/handlers/version.ts +63 -63
  113. package/src/index.test.ts +674 -674
  114. package/src/index.ts +884 -884
  115. package/src/setup.test.ts +243 -243
  116. package/src/setup.ts +410 -410
  117. package/src/templates/agent-guidelines.ts +233 -233
  118. package/src/templates/help-content.ts +1751 -1751
  119. package/src/token-tracking.test.ts +463 -463
  120. package/src/token-tracking.ts +167 -167
  121. package/src/tools/blockers.ts +122 -122
  122. package/src/tools/bodies-of-work.ts +283 -283
  123. package/src/tools/chat.ts +72 -72
  124. package/src/tools/cloud-agents.ts +101 -101
  125. package/src/tools/connectors.ts +191 -191
  126. package/src/tools/cost.ts +111 -111
  127. package/src/tools/decisions.ts +111 -111
  128. package/src/tools/deployment.ts +455 -455
  129. package/src/tools/discovery.ts +76 -76
  130. package/src/tools/fallback.ts +111 -111
  131. package/src/tools/features.ts +154 -154
  132. package/src/tools/file-checkouts.ts +145 -145
  133. package/src/tools/findings.ts +101 -101
  134. package/src/tools/git-issues.ts +130 -130
  135. package/src/tools/ideas.ts +162 -162
  136. package/src/tools/index.ts +145 -145
  137. package/src/tools/milestones.ts +118 -118
  138. package/src/tools/organizations.ts +224 -224
  139. package/src/tools/persona-templates.ts +25 -25
  140. package/src/tools/progress.ts +73 -73
  141. package/src/tools/project.ts +210 -210
  142. package/src/tools/requests.ts +68 -68
  143. package/src/tools/roles.ts +112 -112
  144. package/src/tools/session.ts +181 -181
  145. package/src/tools/sprints.ts +298 -298
  146. package/src/tools/tasks.ts +583 -575
  147. package/src/tools/tools.test.ts +222 -222
  148. package/src/tools/types.ts +9 -9
  149. package/src/tools/validation.ts +75 -75
  150. package/src/tools/version.ts +34 -34
  151. package/src/tools/worktrees.ts +66 -66
  152. package/src/tools.test.ts +416 -416
  153. package/src/utils.test.ts +1014 -1014
  154. package/src/utils.ts +586 -586
  155. package/src/validators.test.ts +223 -223
  156. package/src/validators.ts +249 -249
  157. package/src/version.ts +162 -162
  158. package/tsconfig.json +16 -16
  159. package/vitest.config.ts +14 -14
  160. package/dist/tools.d.ts +0 -2
  161. package/dist/tools.js +0 -3602
@@ -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
+ });