|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | +import { parseApiPrice, calculateApiCostAnthropic, calculateApiCostOpenAI } from "../cost" |
| 3 | +import type { ModelInfo } from "@roo-code/types" |
| 4 | + |
| 5 | +describe("parseApiPrice", () => { |
| 6 | + it("should handle zero as a number", () => { |
| 7 | + expect(parseApiPrice(0)).toBe(0) |
| 8 | + }) |
| 9 | + |
| 10 | + it("should handle zero as a string", () => { |
| 11 | + expect(parseApiPrice("0")).toBe(0) |
| 12 | + }) |
| 13 | + |
| 14 | + it("should handle positive numbers", () => { |
| 15 | + expect(parseApiPrice(0.0002)).toBe(200) |
| 16 | + expect(parseApiPrice(0.00002)).toBe(20) |
| 17 | + }) |
| 18 | + |
| 19 | + it("should handle positive number strings", () => { |
| 20 | + expect(parseApiPrice("0.0002")).toBe(200) |
| 21 | + expect(parseApiPrice("0.00002")).toBe(20) |
| 22 | + }) |
| 23 | + |
| 24 | + it("should return undefined for null", () => { |
| 25 | + expect(parseApiPrice(null)).toBeUndefined() |
| 26 | + }) |
| 27 | + |
| 28 | + it("should return undefined for undefined", () => { |
| 29 | + expect(parseApiPrice(undefined)).toBeUndefined() |
| 30 | + }) |
| 31 | + |
| 32 | + it("should return undefined for empty string", () => { |
| 33 | + expect(parseApiPrice("")).toBeUndefined() |
| 34 | + }) |
| 35 | +}) |
| 36 | + |
| 37 | +describe("calculateApiCostAnthropic", () => { |
| 38 | + const modelInfo: ModelInfo = { |
| 39 | + maxTokens: 4096, |
| 40 | + contextWindow: 200000, |
| 41 | + supportsImages: true, |
| 42 | + supportsPromptCache: true, |
| 43 | + inputPrice: 300, |
| 44 | + outputPrice: 1500, |
| 45 | + cacheWritesPrice: 375, |
| 46 | + cacheReadsPrice: 30, |
| 47 | + } |
| 48 | + |
| 49 | + it("should calculate cost without caching", () => { |
| 50 | + const cost = calculateApiCostAnthropic(modelInfo, 1000, 500) |
| 51 | + expect(cost).toBeCloseTo(0.3 + 0.75, 10) |
| 52 | + }) |
| 53 | + |
| 54 | + it("should calculate cost with cache creation", () => { |
| 55 | + const cost = calculateApiCostAnthropic(modelInfo, 1000, 500, 2000) |
| 56 | + expect(cost).toBeCloseTo(0.3 + 0.75 + 0.75, 10) |
| 57 | + }) |
| 58 | + |
| 59 | + it("should calculate cost with cache reads", () => { |
| 60 | + const cost = calculateApiCostAnthropic(modelInfo, 1000, 500, 0, 3000) |
| 61 | + expect(cost).toBeCloseTo(0.3 + 0.75 + 0.09, 10) |
| 62 | + }) |
| 63 | + |
| 64 | + it("should handle zero cost for free models", () => { |
| 65 | + const freeModel: ModelInfo = { |
| 66 | + maxTokens: 4096, |
| 67 | + contextWindow: 200000, |
| 68 | + supportsImages: false, |
| 69 | + supportsPromptCache: false, |
| 70 | + inputPrice: 0, |
| 71 | + outputPrice: 0, |
| 72 | + } |
| 73 | + const cost = calculateApiCostAnthropic(freeModel, 1000, 500) |
| 74 | + expect(cost).toBe(0) |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +describe("calculateApiCostOpenAI", () => { |
| 79 | + const modelInfo: ModelInfo = { |
| 80 | + maxTokens: 4096, |
| 81 | + contextWindow: 128000, |
| 82 | + supportsImages: true, |
| 83 | + supportsPromptCache: true, |
| 84 | + inputPrice: 150, |
| 85 | + outputPrice: 600, |
| 86 | + cacheWritesPrice: 187.5, |
| 87 | + cacheReadsPrice: 15, |
| 88 | + } |
| 89 | + |
| 90 | + it("should calculate cost without caching", () => { |
| 91 | + const cost = calculateApiCostOpenAI(modelInfo, 1000, 500) |
| 92 | + expect(cost).toBeCloseTo(0.15 + 0.3, 10) |
| 93 | + }) |
| 94 | + |
| 95 | + it("should subtract cached tokens from input tokens", () => { |
| 96 | + const cost = calculateApiCostOpenAI(modelInfo, 5000, 500, 2000, 1000) |
| 97 | + // 5000 total - 2000 cache creation - 1000 cache read = 2000 non-cached |
| 98 | + // Cost: (2000 * 0.00015) + (2000 * 0.0001875) + (1000 * 0.000015) + (500 * 0.0006) |
| 99 | + expect(cost).toBeCloseTo(0.3 + 0.375 + 0.015 + 0.3, 10) |
| 100 | + }) |
| 101 | + |
| 102 | + it("should handle zero cost for free models", () => { |
| 103 | + const freeModel: ModelInfo = { |
| 104 | + maxTokens: 4096, |
| 105 | + contextWindow: 128000, |
| 106 | + supportsImages: false, |
| 107 | + supportsPromptCache: false, |
| 108 | + inputPrice: 0, |
| 109 | + outputPrice: 0, |
| 110 | + } |
| 111 | + const cost = calculateApiCostOpenAI(freeModel, 1000, 500) |
| 112 | + expect(cost).toBe(0) |
| 113 | + }) |
| 114 | +}) |
0 commit comments