| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { parseUsagePriceForm, usagePriceForm } from "./usagePriceForm.ts"; |
| 4 | |
| 5 | describe("model price entry", () => { |
| 6 | it("preserves exact model IDs and fractional rates", () => { |
| 7 | expect( |
| 8 | parseUsagePriceForm({ |
| 9 | ...usagePriceForm(), |
| 10 | model: " vendor/Example-Model ", |
| 11 | inputCostPerMillionTokens: "2.5", |
| 12 | outputCostPerMillionTokens: "10", |
| 13 | }), |
| 14 | ).toEqual({ |
| 15 | model: "vendor/Example-Model", |
| 16 | price: { inputCostPerMillionTokens: 2.5, outputCostPerMillionTokens: 10 }, |
| 17 | }); |
| 18 | }); |
| 19 | |
| 20 | it("keeps free cache tokens distinct from blank cache prices", () => { |
| 21 | const parsed = parseUsagePriceForm({ |
| 22 | ...usagePriceForm(), |
| 23 | model: "example-model", |
| 24 | inputCostPerMillionTokens: "2", |
| 25 | outputCostPerMillionTokens: "8", |
| 26 | cacheReadCostPerMillionTokens: "0", |
| 27 | cacheWriteCostPerMillionTokens: " ", |
| 28 | }); |
| 29 | expect(parsed?.price).toEqual({ |
| 30 | inputCostPerMillionTokens: 2, |
| 31 | outputCostPerMillionTokens: 8, |
| 32 | cacheReadCostPerMillionTokens: 0, |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | it("preserves all prices when editing, including small fractional prices", () => { |
| 37 | const price = { |
| 38 | inputCostPerMillionTokens: 0, |
| 39 | outputCostPerMillionTokens: 8, |
| 40 | cacheReadCostPerMillionTokens: 0.0000001, |
| 41 | cacheWriteCostPerMillionTokens: 3.5, |
| 42 | }; |
| 43 | expect(parseUsagePriceForm(usagePriceForm("example-model", price))).toEqual({ |
| 44 | model: "example-model", |
| 45 | price, |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | it.each(["", " ", "-1", "Infinity", "NaN", "1e999", "$2", "0x10"])( |
| 50 | "rejects invalid required prices: %j", |
| 51 | (inputCostPerMillionTokens) => { |
| 52 | expect( |
| 53 | parseUsagePriceForm({ |
| 54 | ...usagePriceForm("example-model", { |
| 55 | inputCostPerMillionTokens: 2, |
| 56 | outputCostPerMillionTokens: 8, |
| 57 | }), |
| 58 | inputCostPerMillionTokens, |
| 59 | }), |
| 60 | ).toBeNull(); |
| 61 | }, |
| 62 | ); |
| 63 | |
| 64 | it("rejects invalid optional rates and missing model IDs", () => { |
| 65 | const form = usagePriceForm("example-model", { |
| 66 | inputCostPerMillionTokens: 2, |
| 67 | outputCostPerMillionTokens: 8, |
| 68 | }); |
| 69 | expect(parseUsagePriceForm({ ...form, cacheReadCostPerMillionTokens: "-1" })).toBeNull(); |
| 70 | expect(parseUsagePriceForm({ ...form, cacheWriteCostPerMillionTokens: "invalid" })).toBeNull(); |
| 71 | expect(parseUsagePriceForm({ ...form, model: " " })).toBeNull(); |
| 72 | }); |
| 73 | }); |