|
| 1 | +import * as assert from "assert"; |
| 2 | +import { CostTrackingService } from "../../services/cost-tracking.service"; |
| 3 | + |
| 4 | +suite("CostTrackingService", () => { |
| 5 | + let service: CostTrackingService; |
| 6 | + |
| 7 | + setup(() => { |
| 8 | + // Reset singleton state between tests |
| 9 | + service = CostTrackingService.getInstance(); |
| 10 | + service.resetAll(); |
| 11 | + }); |
| 12 | + |
| 13 | + teardown(() => { |
| 14 | + service.resetAll(); |
| 15 | + }); |
| 16 | + |
| 17 | + // ── recordUsage ───────────────────────────────────────────────── |
| 18 | + |
| 19 | + test("recordUsage tracks a single turn", () => { |
| 20 | + const result = service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 1000, 500); |
| 21 | + |
| 22 | + assert.strictEqual(result.inputTokens, 1000); |
| 23 | + assert.strictEqual(result.outputTokens, 500); |
| 24 | + assert.strictEqual(result.totalTokens, 1500); |
| 25 | + assert.strictEqual(result.requestCount, 1); |
| 26 | + assert.strictEqual(result.provider, "anthropic"); |
| 27 | + assert.strictEqual(result.model, "claude-sonnet-4-6"); |
| 28 | + assert.ok(result.estimatedCostUSD > 0); |
| 29 | + }); |
| 30 | + |
| 31 | + test("recordUsage accumulates across multiple turns", () => { |
| 32 | + service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 1000, 500); |
| 33 | + const result = service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 2000, 1000); |
| 34 | + |
| 35 | + assert.strictEqual(result.inputTokens, 3000); |
| 36 | + assert.strictEqual(result.outputTokens, 1500); |
| 37 | + assert.strictEqual(result.totalTokens, 4500); |
| 38 | + assert.strictEqual(result.requestCount, 2); |
| 39 | + }); |
| 40 | + |
| 41 | + test("recordUsage tracks separate conversations independently", () => { |
| 42 | + service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 1000, 500); |
| 43 | + service.recordUsage("t2", "openai", "gpt-4o", 2000, 1000); |
| 44 | + |
| 45 | + const c1 = service.getConversationCost("t1"); |
| 46 | + const c2 = service.getConversationCost("t2"); |
| 47 | + |
| 48 | + assert.strictEqual(c1!.inputTokens, 1000); |
| 49 | + assert.strictEqual(c2!.inputTokens, 2000); |
| 50 | + assert.strictEqual(c1!.provider, "anthropic"); |
| 51 | + assert.strictEqual(c2!.provider, "openai"); |
| 52 | + }); |
| 53 | + |
| 54 | + test("recordUsage uses default pricing for unknown models", () => { |
| 55 | + const result = service.recordUsage("t1", "custom", "some-unknown-model", 1000000, 0); |
| 56 | + // Default pricing: 3/M input → $3.00 for 1M tokens |
| 57 | + assert.strictEqual(result.estimatedCostUSD, 3); |
| 58 | + }); |
| 59 | + |
| 60 | + // ── getConversationCost ───────────────────────────────────────── |
| 61 | + |
| 62 | + test("getConversationCost returns null for untracked thread", () => { |
| 63 | + assert.strictEqual(service.getConversationCost("nonexistent"), null); |
| 64 | + }); |
| 65 | + |
| 66 | + test("getConversationCost returns data for tracked thread", () => { |
| 67 | + service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 100, 50); |
| 68 | + const cost = service.getConversationCost("t1"); |
| 69 | + |
| 70 | + assert.ok(cost !== null); |
| 71 | + assert.strictEqual(cost!.inputTokens, 100); |
| 72 | + assert.strictEqual(cost!.outputTokens, 50); |
| 73 | + }); |
| 74 | + |
| 75 | + // ── resetConversation ─────────────────────────────────────────── |
| 76 | + |
| 77 | + test("resetConversation clears a single conversation", () => { |
| 78 | + service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 100, 50); |
| 79 | + service.recordUsage("t2", "openai", "gpt-4o", 200, 100); |
| 80 | + |
| 81 | + service.resetConversation("t1"); |
| 82 | + |
| 83 | + assert.strictEqual(service.getConversationCost("t1"), null); |
| 84 | + assert.ok(service.getConversationCost("t2") !== null); |
| 85 | + }); |
| 86 | + |
| 87 | + // ── resetAll ──────────────────────────────────────────────────── |
| 88 | + |
| 89 | + test("resetAll clears all conversations", () => { |
| 90 | + service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 100, 50); |
| 91 | + service.recordUsage("t2", "openai", "gpt-4o", 200, 100); |
| 92 | + |
| 93 | + service.resetAll(); |
| 94 | + |
| 95 | + assert.strictEqual(service.getConversationCost("t1"), null); |
| 96 | + assert.strictEqual(service.getConversationCost("t2"), null); |
| 97 | + }); |
| 98 | + |
| 99 | + // ── getCostSummary ────────────────────────────────────────────── |
| 100 | + |
| 101 | + test("getCostSummary returns zero totals when empty", () => { |
| 102 | + const summary = service.getCostSummary(); |
| 103 | + |
| 104 | + assert.strictEqual(summary.totals.inputTokens, 0); |
| 105 | + assert.strictEqual(summary.totals.outputTokens, 0); |
| 106 | + assert.strictEqual(summary.totals.totalTokens, 0); |
| 107 | + assert.strictEqual(summary.totals.estimatedCostUSD, 0); |
| 108 | + assert.strictEqual(summary.totals.requestCount, 0); |
| 109 | + assert.strictEqual(summary.totals.conversationCount, 0); |
| 110 | + assert.deepStrictEqual(summary.providers, []); |
| 111 | + assert.deepStrictEqual(summary.conversations, []); |
| 112 | + }); |
| 113 | + |
| 114 | + test("getCostSummary aggregates a single conversation", () => { |
| 115 | + service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 1000, 500); |
| 116 | + |
| 117 | + const summary = service.getCostSummary(); |
| 118 | + |
| 119 | + assert.strictEqual(summary.totals.inputTokens, 1000); |
| 120 | + assert.strictEqual(summary.totals.outputTokens, 500); |
| 121 | + assert.strictEqual(summary.totals.totalTokens, 1500); |
| 122 | + assert.strictEqual(summary.totals.requestCount, 1); |
| 123 | + assert.strictEqual(summary.totals.conversationCount, 1); |
| 124 | + assert.strictEqual(summary.providers.length, 1); |
| 125 | + assert.strictEqual(summary.providers[0].provider, "anthropic"); |
| 126 | + assert.strictEqual(summary.conversations.length, 1); |
| 127 | + assert.strictEqual(summary.conversations[0].threadId, "t1"); |
| 128 | + }); |
| 129 | + |
| 130 | + test("getCostSummary aggregates multiple conversations per provider", () => { |
| 131 | + service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 1000, 500); |
| 132 | + service.recordUsage("t2", "anthropic", "claude-sonnet-4-6", 2000, 1000); |
| 133 | + |
| 134 | + const summary = service.getCostSummary(); |
| 135 | + |
| 136 | + assert.strictEqual(summary.totals.conversationCount, 2); |
| 137 | + assert.strictEqual(summary.totals.inputTokens, 3000); |
| 138 | + assert.strictEqual(summary.totals.outputTokens, 1500); |
| 139 | + assert.strictEqual(summary.providers.length, 1); |
| 140 | + assert.strictEqual(summary.providers[0].inputTokens, 3000); |
| 141 | + assert.strictEqual(summary.providers[0].requestCount, 2); |
| 142 | + }); |
| 143 | + |
| 144 | + test("getCostSummary groups by provider correctly", () => { |
| 145 | + service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 1000, 500); |
| 146 | + service.recordUsage("t2", "openai", "gpt-4o", 2000, 1000); |
| 147 | + service.recordUsage("t3", "anthropic", "claude-3-5-haiku-20241022", 500, 250); |
| 148 | + |
| 149 | + const summary = service.getCostSummary(); |
| 150 | + |
| 151 | + assert.strictEqual(summary.totals.conversationCount, 3); |
| 152 | + assert.strictEqual(summary.providers.length, 2); |
| 153 | + |
| 154 | + const anthropic = summary.providers.find((p) => p.provider === "anthropic"); |
| 155 | + const openai = summary.providers.find((p) => p.provider === "openai"); |
| 156 | + |
| 157 | + assert.ok(anthropic); |
| 158 | + assert.ok(openai); |
| 159 | + assert.strictEqual(anthropic!.inputTokens, 1500); |
| 160 | + assert.strictEqual(openai!.inputTokens, 2000); |
| 161 | + assert.strictEqual(anthropic!.requestCount, 2); |
| 162 | + assert.strictEqual(openai!.requestCount, 1); |
| 163 | + }); |
| 164 | + |
| 165 | + test("getCostSummary rounds cost to 6 decimal places", () => { |
| 166 | + // Use a known model with known pricing to verify rounding |
| 167 | + service.recordUsage("t1", "anthropic", "claude-sonnet-4-6", 1, 1); |
| 168 | + |
| 169 | + const summary = service.getCostSummary(); |
| 170 | + const costStr = summary.totals.estimatedCostUSD.toString(); |
| 171 | + const decimals = costStr.includes(".") ? costStr.split(".")[1].length : 0; |
| 172 | + assert.ok(decimals <= 6, `Cost should have at most 6 decimals, got ${decimals}`); |
| 173 | + }); |
| 174 | +}); |
0 commit comments